MSFileSystemBasic.cpp 34 KB

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