MSFileSystemBasic.cpp 35 KB

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