FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
DirectXShaderCompiler/lib/Support/MSFileSystemBasic.cpp at main · microsoft/DirectXShaderCompiler · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
microsoft
/
DirectXShaderCompiler
Public
Notifications
You must be signed in to change notification settings
Fork
897
Star
3.6k
Code
Issues
654
Pull requests
140
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
DirectXShaderCompiler
/
lib
/
Support
/
MSFileSystemBasic.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1221 lines (1005 loc) · 36.5 KB
Breadcrumbs
DirectXShaderCompiler
/
lib
/
Support
/
MSFileSystemBasic.cpp
Copy path
File metadata and controls
1221 lines (1005 loc) · 36.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
===- llvm/Support/Windows/MSFileSystemBasic.cpp DXComplier Impl *- C++
//
-*-===//
//
/////////////////////////////////////////////////////////////////////////////
//
//
//
MSFileSystemBasic.cpp //
//
Copyright (C) Microsoft Corporation. All rights reserved. //
//
This file is distributed under the University of Illinois Open Source //
//
License. See LICENSE.TXT for details. //
//
//
//
This file implements the DXCompiler specific implementation of the Path
//
API.//
//
//
//
/////////////////////////////////////////////////////////////////////////////
#
ifdef
_WIN32
#
include
"
dxc/Support/WinIncludes.h
"
#
include
<
assert.h
>
#
include
<
d3dcommon.h
>
#
include
<
errno.h
>
#
include
<
fcntl.h
>
#
include
<
io.h
>
#
include
<
new
>
#
include
<
stdint.h
>
#
include
<
sys/stat.h
>
#
include
<
sys/types.h
>
#
include
<
unordered_map
>
#
include
"
dxc/Support/Global.h
"
#
include
"
llvm/Support/MSFileSystem.h
"
#
include
"
dxc/dxcapi.internal.h
"
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
Externally visible functions.
//
/ <summary>Creates an implementation based on IDxcSystemAccess.</summary>
HRESULT
CreateMSFileSystemForIface
(IUnknown *pService,
::llvm::sys::fs::MSFileSystem **pResult) throw();
//
/ <summary>Creates an implementation with no access to system
//
/ resources.</summary>
HRESULT
CreateMSFileSystemBlocked
(::llvm::sys::fs::MSFileSystem **pResult) throw();
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
Helper functions.
static
DWORD
WIN32_FROM_HRESULT
(
HRESULT
hr) {
if
(
SUCCEEDED
(hr))
return
ERROR_SUCCESS
;
if
((
HRESULT
)(hr &
0xFFFF0000
) ==
MAKE_HRESULT
(
SEVERITY_ERROR
,
FACILITY_WIN32
,
0
)) {
//
Could have come from many values, but we choose this one
return
HRESULT_CODE
(hr);
}
if
(hr ==
E_OUTOFMEMORY
)
return
ERROR_OUTOFMEMORY
;
if
(hr ==
E_NOTIMPL
)
return
ERROR_CALL_NOT_IMPLEMENTED
;
return
ERROR_FUNCTION_FAILED
;
}
static
HRESULT
CopyStatStg
(
const
STATSTG
*statStg,
LPWIN32_FIND_DATAW
lpFindFileData) {
HRESULT
hr =
S_OK
;
lpFindFileData->
dwFileAttributes
=
FILE_ATTRIBUTE_NORMAL
;
lpFindFileData->
ftCreationTime
= statStg->
ctime
;
lpFindFileData->
ftLastAccessTime
= statStg->
atime
;
lpFindFileData->
ftLastWriteTime
= statStg->
mtime
;
lpFindFileData->
nFileSizeLow
= statStg->
cbSize
.
LowPart
;
lpFindFileData->
nFileSizeHigh
= statStg->
cbSize
.
HighPart
;
if
(statStg->
pwcsName
!=
nullptr
) {
IFC
(
StringCchCopyW
(lpFindFileData->
cFileName
,
_countof
(lpFindFileData->
cFileName
), statStg->
pwcsName
));
}
Cleanup:
return
hr;
}
static
void
ClearStatStg
(
STATSTG
*statStg) {
DXASSERT_NOMSG
(statStg !=
nullptr
);
if
(statStg->
pwcsName
!=
nullptr
) {
CoTaskMemFree
(statStg->
pwcsName
);
statStg->
pwcsName
=
nullptr
;
}
}
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
IDxcSystemAccess-based MSFileSystem implementation.
struct
MSFileSystemHandle
{
enum
MSFileSystemHandleKind {
MSFileSystemHandleKind_FindHandle,
MSFileSystemHandleKind_FileHandle,
MSFileSystemHandleKind_FileMappingHandle
};
MSFileSystemHandleKind kind;
CComPtr<IUnknown>
storage;
//
For a file handle, the stream or directory handle.
//
For a find handle, the IEnumSTATSTG associated.
CComPtr<IStream>
stream;
//
For a file or console file handle, the stream interface.
int
fd;
//
For a file handle, its file descriptor.
explicit
MSFileSystemHandle
(
int
knownFD)
: kind(MSFileSystemHandleKind_FileHandle), fd(knownFD) {}
explicit
MSFileSystemHandle
(IUnknown *pMapping)
: kind(MSFileSystemHandleKind_FileMappingHandle), storage(pMapping),
fd(
0
) {}
MSFileSystemHandle
(IUnknown *pStorage, IStream *pStream)
: kind(MSFileSystemHandleKind_FileHandle), storage(pStorage),
stream
(pStream), fd(
0
) {}
explicit
MSFileSystemHandle
(IEnumSTATSTG *pEnumSTATG)
: kind(MSFileSystemHandleKind_FindHandle), storage(pEnumSTATG) {}
MSFileSystemHandle
(MSFileSystemHandle &&other) {
kind = other.
kind
;
storage.
p
= other.
storage
.
Detach
();
stream.
p
= other.
stream
.
Detach
();
}
HANDLE
GetHandle
()
const
{
return
(
HANDLE
)
this
; }
IEnumSTATSTG *
GetEnumStatStg
() {
DXASSERT
(kind == MSFileSystemHandleKind_FindHandle,
"
otherwise caller didn't check
"
);
return
(IEnumSTATSTG *)storage.
p
;
}
};
namespace
llvm
{
namespace
sys
{
namespace
fs
{
class
MSFileSystemForIface
:
public
MSFileSystem
{
private:
CComPtr<IDxcSystemAccess> m_system;
typedef
std::unordered_multimap<
LPCVOID
, ID3D10Blob *> TViewMap;
TViewMap m_mappingViews;
MSFileSystemHandle m_knownHandle0;
MSFileSystemHandle m_knownHandle1;
MSFileSystemHandle m_knownHandle2;
HRESULT
AddFindHandle
(IEnumSTATSTG *enumStatStg,
HANDLE
*pResult) throw();
HRESULT
AddFileHandle
(IUnknown *storage, IStream *stream,
HANDLE
*pResult) throw();
HRESULT
AddMappingHandle
(IUnknown *mapping,
HANDLE
*pResult) throw();
HRESULT
AddMappingView
(ID3D10Blob *blob) throw();
HRESULT
EnsureFDAvailable
(
int
fd);
HANDLE
GetHandleForFD
(
int
fd) throw();
void
GetFindHandle
(
HANDLE
findHandle, IEnumSTATSTG **enumStatStg) throw();
int
GetHandleFD
(
HANDLE
fileHandle) throw();
void
GetHandleMapping
(
HANDLE
fileHandle, IUnknown **pResult) throw();
void
GetHandleStorage
(
HANDLE
fileHandle, IUnknown **pResult) throw();
void
GetHandleStream
(
HANDLE
fileHandle, IStream **pResult) throw();
void
CloseInternalHandle
(
HANDLE
findHandle) throw();
void
RemoveMappingView
(
LPCVOID
address) throw();
public:
MSFileSystemForIface
(IDxcSystemAccess *access);
virtual
BOOL
FindNextFileW
(
HANDLE
hFindFile,
LPWIN32_FIND_DATAW
lpFindFileData)
throw
()
override
;
virtual
HANDLE
FindFirstFileW
(
LPCWSTR
lpFileName,
LPWIN32_FIND_DATAW
lpFindFileData)
throw
()
override
;
virtual
void
FindClose
(
HANDLE
findHandle) throw()
override
;
virtual
HANDLE
CreateFileW
(
LPCWSTR
lpFileName,
DWORD
dwDesiredAccess,
DWORD
dwShareMode,
DWORD
dwCreationDisposition,
DWORD
dwFlagsAndAttributes) throw()
override
;
virtual
BOOL
SetFileTime
(
HANDLE
hFile,
const
FILETIME
*lpCreationTime,
const
FILETIME
*lpLastAccessTime,
const
FILETIME
*lpLastWriteTime) throw()
override
;
virtual
BOOL
GetFileInformationByHandle
(
HANDLE
hFile,
LPBY_HANDLE_FILE_INFORMATION
lpFileInformation) throw()
override
;
virtual
DWORD
GetFileType
(
HANDLE
hFile) throw()
override
;
virtual
BOOL
CreateHardLinkW
(
LPCWSTR
lpFileName,
LPCWSTR
lpExistingFileName) throw()
override
;
virtual
BOOL
MoveFileExW
(
LPCWSTR
lpExistingFileName,
LPCWSTR
lpNewFileName,
DWORD
dwFlags) throw()
override
;
virtual
DWORD
GetFileAttributesW
(
LPCWSTR
lpFileName) throw()
override
;
virtual
BOOL
CloseHandle
(
HANDLE
hObject) throw()
override
;
virtual
BOOL
DeleteFileW
(
LPCWSTR
lpFileName) throw()
override
;
virtual
BOOL
RemoveDirectoryW
(
LPCWSTR
lpFileName) throw()
override
;
virtual
BOOL
CreateDirectoryW
(
LPCWSTR
lpPathName) throw()
override
;
virtual
DWORD
GetCurrentDirectoryW
(
DWORD
nBufferLength,
LPWSTR
lpBuffer) throw()
override
;
virtual
DWORD
GetMainModuleFileNameW
(
LPWSTR
lpFilename,
DWORD
nSize) throw()
override
;
virtual
DWORD
GetTempPathW
(
DWORD
nBufferLength,
LPWSTR
lpBuffer) throw()
override
;
virtual
BOOLEAN
CreateSymbolicLinkW
(
LPCWSTR
lpSymlinkFileName,
LPCWSTR
lpTargetFileName,
DWORD
dwFlags) throw()
override
;
virtual
bool
SupportsCreateSymbolicLink
() throw()
override
;
virtual
BOOL
ReadFile
(
HANDLE
hFile,
LPVOID
lpBuffer,
DWORD
nNumberOfBytesToRead,
LPDWORD
lpNumberOfBytesRead) throw()
override
;
virtual
HANDLE
CreateFileMappingW
(
HANDLE
hFile,
DWORD
flProtect,
DWORD
dwMaximumSizeHigh,
DWORD
dwMaximumSizeLow) throw()
override
;
virtual
LPVOID
MapViewOfFile
(
HANDLE
hFileMappingObject,
DWORD
dwDesiredAccess,
DWORD
dwFileOffsetHigh,
DWORD
dwFileOffsetLow,
SIZE_T
dwNumberOfBytesToMap) throw()
override
;
virtual
BOOL
UnmapViewOfFile
(
LPCVOID
lpBaseAddress) throw()
override
;
//
Console APIs.
virtual
bool
FileDescriptorIsDisplayed
(
int
fd) throw()
override
;
virtual
unsigned
GetColumnCount
(
DWORD
nStdHandle) throw()
override
;
virtual
unsigned
GetConsoleOutputTextAttributes
() throw()
override
;
virtual
void
SetConsoleOutputTextAttributes
(
unsigned
attributes)
throw
()
override
;
virtual
void
ResetConsoleOutputTextAttributes
() throw()
override
;
//
CRT APIs.
virtual
int
open_osfhandle
(
intptr_t
osfhandle,
int
flags) throw()
override
;
virtual
intptr_t
get_osfhandle
(
int
fd) throw()
override
;
virtual
int
close
(
int
fd) throw()
override
;
virtual
long
lseek
(
int
fd,
long
offset,
int
origin) throw()
override
;
virtual
int
setmode
(
int
fd,
int
mode) throw()
override
;
virtual
errno_t
resize_file
(
LPCWSTR
path,
uint64_t
size) throw()
override
;
virtual
int
Read
(
int
fd,
void
*buffer,
unsigned
int
count) throw()
override
;
virtual
int
Write
(
int
fd,
const
void
*buffer,
unsigned
int
count) throw()
override
;
#
ifndef
_WIN32
virtual
int
Open
(
const
char
*lpFileName,
int
flags,
mode_t
mode) throw()
override
;
virtual
int
Stat
(
const
char
*lpFileName,
struct
stat
*Status) throw()
override
;
virtual
int
Fstat
(
int
FD
,
struct
stat
*Status) throw()
override
;
#
endif
};
MSFileSystemForIface::MSFileSystemForIface
(IDxcSystemAccess *systemAccess)
: m_system(systemAccess), m_knownHandle0(
0
), m_knownHandle1(
1
),
m_knownHandle2
(
2
) {}
HRESULT
MSFileSystemForIface::AddMappingHandle
(IUnknown *mapping,
HANDLE
*pResult) throw() {
DXASSERT_NOMSG
(mapping !=
nullptr
);
DXASSERT_NOMSG
(pResult !=
nullptr
);
HRESULT
hr =
S_OK
;
MSFileSystemHandle *handle =
nullptr
;
*pResult =
INVALID_HANDLE_VALUE
;
handle =
new
(std::nothrow)
MSFileSystemHandle
(mapping);
IFCOOM
(handle);
*pResult = handle->
GetHandle
();
Cleanup:
return
hr;
}
HRESULT
MSFileSystemForIface::AddMappingView
(ID3D10Blob *blob) throw() {
DXASSERT_NOMSG
(blob !=
nullptr
);
LPVOID
address = blob->
GetBufferPointer
();
try
{
m_mappingViews.
insert
(std::pair<
LPVOID
, ID3D10Blob *>(address, blob));
}
catch
(std::bad_alloc &) {
return
E_OUTOFMEMORY
;
}
blob->
AddRef
();
return
S_OK
;
}
HRESULT
MSFileSystemForIface::AddFindHandle
(IEnumSTATSTG *enumStatStg,
HANDLE
*pResult) throw() {
DXASSERT_NOMSG
(enumStatStg !=
nullptr
);
DXASSERT_NOMSG
(pResult !=
nullptr
);
HRESULT
hr =
S_OK
;
MSFileSystemHandle *handle =
nullptr
;
*pResult =
INVALID_HANDLE_VALUE
;
handle =
new
(std::nothrow)
MSFileSystemHandle
(enumStatStg);
IFCOOM
(handle);
*pResult = handle->
GetHandle
();
Cleanup:
return
hr;
}
HRESULT
MSFileSystemForIface::AddFileHandle
(IUnknown *storage, IStream *stream,
HANDLE
*pResult) throw() {
DXASSERT_NOMSG
(storage !=
nullptr
);
DXASSERT_NOMSG
(pResult !=
nullptr
);
HRESULT
hr =
S_OK
;
MSFileSystemHandle *handle =
nullptr
;
*pResult =
INVALID_HANDLE_VALUE
;
handle =
new
(std::nothrow)
MSFileSystemHandle
(storage, stream);
IFCOOM
(handle);
*pResult = handle->
GetHandle
();
Cleanup:
return
hr;
}
void
MSFileSystemForIface::CloseInternalHandle
(
HANDLE
handle) throw() {
DXASSERT_NOMSG
(handle !=
nullptr
);
DXASSERT_NOMSG
(handle !=
INVALID_HANDLE_VALUE
);
MSFileSystemHandle *fsHandle =
reinterpret_cast
<MSFileSystemHandle *>(handle);
if
(fsHandle == &m_knownHandle0 || fsHandle == &m_knownHandle1 ||
fsHandle == &m_knownHandle2) {
fsHandle->
stream
.
Release
();
fsHandle->
storage
.
Release
();
}
else
{
delete
fsHandle;
}
}
void
MSFileSystemForIface::RemoveMappingView
(
LPCVOID
address) throw() {
TViewMap::iterator i = m_mappingViews.
find
(address);
DXASSERT
(i != m_mappingViews.
end
(),
"
otherwise pointer to view isn't in map
"
);
DXASSERT
(i->
second
!=
nullptr
,
"
otherwise blob is null and should not have been added
"
);
i->
second
->
Release
();
m_mappingViews.
erase
(i);
}
void
MSFileSystemForIface::GetFindHandle
(
HANDLE
findHandle,
IEnumSTATSTG **enumStatStg) throw() {
DXASSERT_NOMSG
(findHandle !=
nullptr
);
DXASSERT_NOMSG
(enumStatStg !=
nullptr
);
MSFileSystemHandle *fsHandle =
reinterpret_cast
<MSFileSystemHandle *>(findHandle);
DXASSERT
(fsHandle->
kind
==
MSFileSystemHandle::MSFileSystemHandleKind_FindHandle,
"
otherwise caller is passing wrong handle to API
"
);
*enumStatStg = fsHandle->
GetEnumStatStg
();
DXASSERT
(*enumStatStg !=
nullptr
,
"
otherwise it should not have been added to handle entry
"
);
(*enumStatStg)->
AddRef
();
}
int
MSFileSystemForIface::GetHandleFD
(
HANDLE
fileHandle) throw() {
DXASSERT_NOMSG
(fileHandle !=
nullptr
);
MSFileSystemHandle *fsHandle =
reinterpret_cast
<MSFileSystemHandle *>(fileHandle);
DXASSERT
(fsHandle->
kind
==
MSFileSystemHandle::MSFileSystemHandleKind_FileHandle,
"
otherwise caller is passing wrong handle to API
"
);
return
fsHandle->
fd
;
}
void
MSFileSystemForIface::GetHandleMapping
(
HANDLE
mapping,
IUnknown **pResult) throw() {
DXASSERT_NOMSG
(mapping !=
nullptr
);
DXASSERT_NOMSG
(pResult !=
nullptr
);
MSFileSystemHandle *fsHandle =
reinterpret_cast
<MSFileSystemHandle *>(mapping);
DXASSERT
(fsHandle->
kind
==
MSFileSystemHandle::MSFileSystemHandleKind_FileMappingHandle,
"
otherwise caller is passing wrong handle to API
"
);
*pResult = fsHandle->
storage
.
p
;
DXASSERT
(*pResult !=
nullptr
,
"
otherwise it should not be requested through GetHandleMapping
"
);
(*pResult)->
AddRef
();
}
void
MSFileSystemForIface::GetHandleStorage
(
HANDLE
fileHandle,
IUnknown **pResult) throw() {
DXASSERT_NOMSG
(fileHandle !=
nullptr
);
DXASSERT_NOMSG
(pResult !=
nullptr
);
MSFileSystemHandle *fsHandle =
reinterpret_cast
<MSFileSystemHandle *>(fileHandle);
DXASSERT
(fsHandle->
kind
==
MSFileSystemHandle::MSFileSystemHandleKind_FileHandle,
"
otherwise caller is passing wrong handle to API
"
);
*pResult = fsHandle->
storage
.
p
;
DXASSERT
(*pResult !=
nullptr
,
"
otherwise it should not be requested through GetHandleStorage
"
);
(*pResult)->
AddRef
();
}
void
MSFileSystemForIface::GetHandleStream
(
HANDLE
fileHandle,
IStream **pResult) throw() {
DXASSERT_NOMSG
(fileHandle !=
nullptr
);
DXASSERT_NOMSG
(pResult !=
nullptr
);
MSFileSystemHandle *fsHandle =
reinterpret_cast
<MSFileSystemHandle *>(fileHandle);
DXASSERT
(fsHandle->
kind
==
MSFileSystemHandle::MSFileSystemHandleKind_FileHandle,
"
otherwise caller is passing wrong handle to API
"
);
*pResult = fsHandle->
stream
.
p
;
DXASSERT
(*pResult !=
nullptr
,
"
otherwise it should not be requested through GetHandleStream
"
);
(*pResult)->
AddRef
();
}
HANDLE
MSFileSystemForIface::FindFirstFileW
(
LPCWSTR
lpFileName,
LPWIN32_FIND_DATAW
lpFindFileData) throw() {
HRESULT
hr =
S_OK
;
CComPtr<IEnumSTATSTG> enumStatStg;
HANDLE
resultValue =
INVALID_HANDLE_VALUE
;
STATSTG
elt;
ULONG
fetched;
ZeroMemory
(&elt,
sizeof
(elt));
ZeroMemory
(lpFindFileData,
sizeof
(*lpFindFileData));
fetched =
0
;
IFC
(m_system->
EnumFiles
(lpFileName, &enumStatStg));
IFC
(enumStatStg->
Next
(
1
, &elt, &fetched));
if
(fetched ==
0
) {
IFC
(
HRESULT_FROM_WIN32
(
ERROR_FILE_NOT_FOUND
));
}
else
{
IFC
(
CopyStatStg
(&elt, lpFindFileData));
IFC
(
AddFindHandle
(enumStatStg, &resultValue));
}
Cleanup:
ClearStatStg
(&elt);
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
return
INVALID_HANDLE_VALUE
;
}
DXASSERT
(resultValue !=
INVALID_HANDLE_VALUE
,
"
otherwise AddFindHandle failed to return a valid handle
"
);
return
resultValue;
}
BOOL
MSFileSystemForIface::FindNextFileW
(
HANDLE
hFindFile,
LPWIN32_FIND_DATAW
lpFindFileData) throw() {
HRESULT
hr =
S_OK
;
CComPtr<IEnumSTATSTG> enumStatStg;
STATSTG
elt;
ULONG
fetched;
ZeroMemory
(&elt,
sizeof
(elt));
ZeroMemory
(lpFindFileData,
sizeof
(*lpFindFileData));
fetched =
0
;
GetFindHandle
(hFindFile, &enumStatStg);
IFC
(enumStatStg->
Next
(
1
, &elt, &fetched));
if
(fetched ==
0
) {
IFC
(
HRESULT_FROM_WIN32
(
ERROR_FILE_NOT_FOUND
));
}
else
{
IFC
(
CopyStatStg
(&elt, lpFindFileData));
}
Cleanup:
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
return
FALSE
;
}
return
TRUE
;
}
void
MSFileSystemForIface::FindClose
(
HANDLE
findHandle) throw() {
CloseInternalHandle
(findHandle);
}
HANDLE
MSFileSystemForIface::CreateFileW
(
LPCWSTR
lpFileName,
DWORD
dwDesiredAccess,
DWORD
dwShareMode,
DWORD
dwCreationDisposition,
DWORD
dwFlagsAndAttributes) throw() {
HRESULT
hr =
S_OK
;
CComPtr<IUnknown> storage;
CComPtr<IStream> stream;
HANDLE
resultHandle =
INVALID_HANDLE_VALUE
;
IFC
(m_system->
OpenStorage
(lpFileName, dwDesiredAccess, dwShareMode,
dwCreationDisposition, dwFlagsAndAttributes,
&storage));
IFC
(storage.
QueryInterface
(&stream));
IFC
(
AddFileHandle
(storage, stream, &resultHandle));
Cleanup:
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
return
INVALID_HANDLE_VALUE
;
}
return
resultHandle;
}
BOOL
MSFileSystemForIface::SetFileTime
(
HANDLE
hFile,
const
FILETIME
*lpCreationTime,
const
FILETIME
*lpLastAccessTime,
const
FILETIME
*lpLastWriteTime) throw() {
HRESULT
hr =
S_OK
;
CComPtr<IUnknown> storage;
GetHandleStorage
(hFile, &storage);
IFC
(m_system->
SetStorageTime
(storage, lpCreationTime, lpLastAccessTime,
lpLastWriteTime));
Cleanup:
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
return
FALSE
;
}
return
TRUE
;
}
BOOL
MSFileSystemForIface::GetFileInformationByHandle
(
HANDLE
hFile,
LPBY_HANDLE_FILE_INFORMATION
lpFileInformation) throw() {
HRESULT
hr =
S_OK
;
CComPtr<IUnknown> storage;
GetHandleStorage
(hFile, &storage);
IFC
(m_system->
GetFileInformationForStorage
(storage, lpFileInformation));
Cleanup:
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
return
FALSE
;
}
return
TRUE
;
}
DWORD
MSFileSystemForIface::GetFileType
(
HANDLE
hFile) throw() {
HRESULT
hr =
S_OK
;
CComPtr<IUnknown> storage;
DWORD
fileType;
GetHandleStorage
(hFile, &storage);
IFC
(m_system->
GetFileTypeForStorage
(storage, &fileType));
if
(fileType ==
FILE_TYPE_UNKNOWN
) {
SetLastError
(
NO_ERROR
);
}
Cleanup:
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
fileType =
FILE_TYPE_UNKNOWN
;
}
return
fileType;
}
BOOL
MSFileSystemForIface::CreateHardLinkW
(
LPCWSTR
lpFileName,
LPCWSTR
lpExistingFileName) throw() {
SetLastError
(
ERROR_FUNCTION_NOT_CALLED
);
return
FALSE
;
}
BOOL
MSFileSystemForIface::MoveFileExW
(
LPCWSTR
lpExistingFileName,
LPCWSTR
lpNewFileName,
DWORD
dwFlags) throw() {
SetLastError
(
ERROR_FUNCTION_NOT_CALLED
);
return
FALSE
;
}
DWORD
MSFileSystemForIface::GetFileAttributesW
(
LPCWSTR
lpFileName) throw() {
HRESULT
hr =
S_OK
;
DWORD
attributes;
IFC
(m_system->
GetFileAttributesForStorage
(lpFileName, &attributes));
Cleanup:
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
attributes =
INVALID_FILE_ATTRIBUTES
;
}
return
attributes;
}
BOOL
MSFileSystemForIface::CloseHandle
(
HANDLE
hObject) throw() {
this
->
CloseInternalHandle
(hObject);
return
TRUE
;
}
BOOL
MSFileSystemForIface::DeleteFileW
(
LPCWSTR
lpFileName) throw() {
SetLastError
(
ERROR_FUNCTION_NOT_CALLED
);
return
FALSE
;
}
BOOL
MSFileSystemForIface::RemoveDirectoryW
(
LPCWSTR
lpFileName) throw() {
SetLastError
(
ERROR_FUNCTION_NOT_CALLED
);
return
FALSE
;
}
BOOL
MSFileSystemForIface::CreateDirectoryW
(
LPCWSTR
lpPathName) throw() {
SetLastError
(
ERROR_FUNCTION_NOT_CALLED
);
return
FALSE
;
}
DWORD
MSFileSystemForIface::GetCurrentDirectoryW
(
DWORD
nBufferLength,
LPWSTR
lpBuffer) throw() {
DWORD
written =
0
;
HRESULT
hr =
S_OK
;
IFC
(m_system->
GetCurrentDirectoryForStorage
(nBufferLength, lpBuffer,
&written));
Cleanup:
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
return
0
;
}
return
written;
}
DWORD
MSFileSystemForIface::GetMainModuleFileNameW
(
LPWSTR
lpFilename,
DWORD
nSize) throw() {
DWORD
written =
0
;
HRESULT
hr =
S_OK
;
IFC
(m_system->
GetMainModuleFileNameW
(nSize, lpFilename, &written));
Cleanup:
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
return
0
;
}
return
written;
}
DWORD
MSFileSystemForIface::GetTempPathW
(
DWORD
nBufferLength,
LPWSTR
lpBuffer) throw() {
DWORD
written =
0
;
HRESULT
hr =
S_OK
;
IFC
(m_system->
GetTempStoragePath
(nBufferLength, lpBuffer, &written));
Cleanup:
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
return
0
;
}
return
written;
}
BOOLEAN
MSFileSystemForIface::CreateSymbolicLinkW
(
LPCWSTR
lpSymlinkFileName,
LPCWSTR
lpTargetFileName,
DWORD
dwFlags) throw() {
SetLastError
(
ERROR_FUNCTION_NOT_CALLED
);
return
FALSE
;
}
bool
MSFileSystemForIface::SupportsCreateSymbolicLink
() throw() {
return
false
;
}
BOOL
MSFileSystemForIface::ReadFile
(
HANDLE
hFile,
LPVOID
lpBuffer,
DWORD
nNumberOfBytesToRead,
LPDWORD
lpNumberOfBytesRead) throw() {
HRESULT
hr =
S_OK
;
CComPtr<IStream> stream;
GetHandleStream
(hFile, &stream);
ULONG
cbRead;
IFC
(stream->
Read
(lpBuffer, nNumberOfBytesToRead, &cbRead));
if
(lpNumberOfBytesRead !=
nullptr
) {
*lpNumberOfBytesRead = cbRead;
}
Cleanup:
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
return
FALSE
;
}
return
TRUE
;
}
HANDLE
MSFileSystemForIface::CreateFileMappingW
(
HANDLE
hFile,
DWORD
flProtect,
DWORD
dwMaximumSizeHigh,
DWORD
dwMaximumSizeLow) throw() {
HRESULT
hr =
S_OK
;
HANDLE
result =
INVALID_HANDLE_VALUE
;
CComPtr<IUnknown> storage;
CComPtr<IUnknown> mapping;
GetHandleStorage
(hFile, &storage);
IFC
(m_system->
CreateStorageMapping
(storage, flProtect, dwMaximumSizeHigh,
dwMaximumSizeLow, &mapping));
IFC
(
AddMappingHandle
(mapping, &result));
Cleanup:
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
return
INVALID_HANDLE_VALUE
;
}
return
result;
}
LPVOID
MSFileSystemForIface::MapViewOfFile
(
HANDLE
hFileMappingObject,
DWORD
dwDesiredAccess,
DWORD
dwFileOffsetHigh,
DWORD
dwFileOffsetLow,
SIZE_T
dwNumberOfBytesToMap) throw() {
HRESULT
hr =
S_OK
;
CComPtr<IUnknown> mapping;
CComPtr<ID3D10Blob> blob;
GetHandleMapping
(hFileMappingObject, &mapping);
IFC
(m_system->
MapViewOfFile
(mapping, dwDesiredAccess, dwFileOffsetHigh,
dwFileOffsetLow, dwNumberOfBytesToMap, &blob));
IFC
(
AddMappingView
(blob));
Cleanup:
if
(
FAILED
(hr)) {
SetLastError
(
WIN32_FROM_HRESULT
(hr));
return
INVALID_HANDLE_VALUE
;
}
return
blob->
GetBufferPointer
();
}
BOOL
MSFileSystemForIface::UnmapViewOfFile
(
LPCVOID
lpBaseAddress) throw() {
RemoveMappingView
(lpBaseAddress);
return
TRUE
;
}
bool
MSFileSystemForIface::FileDescriptorIsDisplayed
(
int
fd) throw() {
return
false
;
}
unsigned
MSFileSystemForIface::GetColumnCount
(
DWORD
nStdHandle) throw() {
return
0
;
}
unsigned
MSFileSystemForIface::GetConsoleOutputTextAttributes
() throw() {
return
0
;
}
void
MSFileSystemForIface::SetConsoleOutputTextAttributes
(
unsigned
attributes) throw() {
return
;
}
void
MSFileSystemForIface::ResetConsoleOutputTextAttributes
() throw() {}
int
MSFileSystemForIface::open_osfhandle
(
intptr_t
osfhandle,
int
flags) throw() {
return
GetHandleFD
((
HANDLE
)osfhandle);
}
HRESULT
MSFileSystemForIface::EnsureFDAvailable
(
int
fd) {
MSFileSystemHandle *ptr;
switch
(fd) {
case
0
:
ptr = &m_knownHandle0;
break
;
case
1
:
ptr = &m_knownHandle1;
break
;
case
2
:
ptr = &m_knownHandle2;
break
;
default
:
return
S_OK
;
}
HRESULT
hr =
S_OK
;
if
(ptr->
storage
==
nullptr
) {
CComPtr<IUnknown> storage;
CComPtr<IStream> stream;
IFC
(m_system->
OpenStdStorage
(fd, &storage));
IFC
(storage.
QueryInterface
(&stream));
ptr->
storage
= storage;
ptr->
stream
= stream;
}
DXASSERT
(ptr->
storage
!=
nullptr
,
"
otherwise we should have failed to initialize
"
);
DXASSERT
(ptr->
stream
!=
nullptr
,
"
otherwise we should have failed to initialize - input/output/error
"
"
should support streams
"
);
Cleanup:
return
hr;
}
HANDLE
MSFileSystemForIface::GetHandleForFD
(
int
fd) throw() {
MSFileSystemHandle *ptr;
switch
(fd) {
case
0
:
ptr = &m_knownHandle0;
break
;
case
1
:
ptr = &m_knownHandle1;
break
;
case
2
:
ptr = &m_knownHandle2;
break
;
default
:
ptr = (MSFileSystemHandle *)(
uintptr_t
)fd;
break
;
}
return
ptr->
GetHandle
();
}
intptr_t
MSFileSystemForIface::get_osfhandle
(
int
fd) throw() {
if
(
FAILED
(
EnsureFDAvailable
(fd))) {
errno =
EBADF
;
return
-
1
;
}
return
(
intptr_t
)
GetHandleForFD
(fd);
}
int
MSFileSystemForIface::close
(
int
fd) throw() {
HANDLE
h =
GetHandleForFD
(fd);
this
->
CloseInternalHandle
(h);
return
0
;
}
long
MSFileSystemForIface::lseek
(
int
fd,
long
offset,
int
origin) throw() {
HRESULT
hr =
S_OK
;
CComPtr<IStream> stream;
LARGE_INTEGER
li;
ULARGE_INTEGER
uli;
if
(
FAILED
(
EnsureFDAvailable
(fd))) {
errno =
EBADF
;
return
-
1
;
}
GetHandleStream
(
GetHandleForFD
(fd), &stream);
li.
HighPart
=
0
;
li.
LowPart
= offset;
IFC
(stream->
Seek
(li, origin, &uli));
Cleanup:
if
(
FAILED
(hr)) {
errno =
EINVAL
;
return
-
1
;
}
if
(uli.
HighPart
>
0
) {
errno =
EOVERFLOW
;
return
-
1
;
}
return
uli.
LowPart
;
}
int
MSFileSystemForIface::setmode
(
int
fd,
int
mode) throw() {
return
0
; }
errno_t
MSFileSystemForIface::resize_file
(
LPCWSTR
path,
uint64_t
size) throw() {
return
EBADF
;
}
int
MSFileSystemForIface::Read
(
int
fd,
void
*buffer,
unsigned
int
count) throw() {
HRESULT
hr =
S_OK
;
CComPtr<IStream> stream;
ULONG
cbRead =
0
;
if
(
FAILED
(
EnsureFDAvailable
(fd))) {
errno =
EBADF
;
return
-
1
;
}
GetHandleStream
(
GetHandleForFD
(fd), &stream);
IFC
(stream->
Read
(buffer, count, &cbRead));
Cleanup:
if
(
FAILED
(hr)) {
errno =
EINVAL
;
return
-
1
;
}
return
(
int
)cbRead;
}
int
MSFileSystemForIface::Write
(
int
fd,
const
void
*buffer,
unsigned
int
count) throw() {
HRESULT
hr =
S_OK
;
CComPtr<IStream> stream;
ULONG
cbWritten =
0
;
if
(
FAILED
(
EnsureFDAvailable
(fd))) {
errno =
EBADF
;
return
-
1
;
}
GetHandleStream
(
GetHandleForFD
(fd), &stream);
IFC
(stream->
Write
(buffer, count, &cbWritten));
Cleanup:
if
(
FAILED
(hr)) {
errno =
EINVAL
;
return
-
1
;
}
return
(
int
)cbWritten;
}
#
ifndef
_WIN32
int
MSFileSystemForIface::Open
(
const
char
*lpFileName,
int
flags,
mode_t
mode) throw() {
SetLastError
(
ERROR_FUNCTION_NOT_CALLED
);
return
FALSE
;
}
int
MSFileSystemForIface::Stat
(
const
char
*lpFileName,
struct
stat
*Status) throw() {
SetLastError
(
ERROR_FUNCTION_NOT_CALLED
);
return
FALSE
;
}
int
MSFileSystemForIface::Fstat
(
int
FD
,
struct
stat
*Status) throw() {
SetLastError
(
ERROR_FUNCTION_NOT_CALLED
);
return
FALSE
;
}
#
endif
}
//
end namespace fs
}
//
end namespace sys
}
//
end namespace llvm
//
/////////////////////////////////////////////////////////////////////////////////////////////////
//
Blocked MSFileSystem implementation.
#
ifndef
NDEBUG
static
void
MSFileSystemBlockedCalled
() {
DebugBreak
(); }
#
else
static
void
MSFileSystemBlockedCalled
() {}
#
endif
static
BOOL
MSFileSystemBlockedErrWin32
() {
MSFileSystemBlockedCalled
();
SetLastError
(
ERROR_FUNCTION_NOT_CALLED
);
return
FALSE
;
}
static
HANDLE
MSFileSystemBlockedHandle
() {
MSFileSystemBlockedCalled
();
SetLastError
(
ERROR_FUNCTION_NOT_CALLED
);
return
INVALID_HANDLE_VALUE
;
}
static
int
MSFileSystemBlockedErrno
() {
MSFileSystemBlockedCalled
();
errno =
EBADF
;
return
-
1
;
}
static
int
MSFileSystemBlockedErrnoT
() {
MSFileSystemBlockedCalled
();
return
EBADF
;
}
namespace
llvm
{
namespace
sys
{
namespace
fs
{
class
MSFileSystemBlocked
:
public
MSFileSystem
{
private:
public:
MSFileSystemBlocked
();
virtual
BOOL
FindNextFileW
(
HANDLE
,
LPWIN32_FIND_DATAW
) throw()
override
{
return
MSFileSystemBlockedErrWin32
();
}
virtual
HANDLE
FindFirstFileW
(
LPCWSTR
lpFileName,
LPWIN32_FIND_DATAW
lpFindFileData)
throw
()
override
{
return
MSFileSystemBlockedHandle
();
}
virtual
void
FindClose
(
HANDLE
findHandle) throw()
override
{
MSFileSystemBlockedCalled
();
}
virtual
HANDLE
CreateFileW
(
LPCWSTR
lpFileName,
DWORD
dwDesiredAccess,
DWORD
dwShareMode,
DWORD
dwCreationDisposition,
DWORD
dwFlagsAndAttributes) throw()
override
{
return
MSFileSystemBlockedHandle
();
}
virtual
BOOL
SetFileTime
(
HANDLE
hFile,
const
FILETIME
*lpCreationTime,
const
FILETIME
*lpLastAccessTime,
const
FILETIME
*lpLastWriteTime) throw()
override
{
return
MSFileSystemBlockedErrWin32
();
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL