MSFileSystemImpl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. //===- llvm/Support/Windows/MSFileSystemImpl.cpp DXComplier Impl *- C++ -*-===//
  2. ///////////////////////////////////////////////////////////////////////////////
  3. // //
  4. // MSFileSystemImpl.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 <fcntl.h>
  13. #ifdef _WIN32
  14. #include <io.h>
  15. #else
  16. #include <sys/types.h>
  17. #include <unistd.h>
  18. #endif
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. #include <stdint.h>
  22. #include <errno.h>
  23. #include <new>
  24. #include "dxc/Support/WinIncludes.h"
  25. #include "dxc/Support/WinAdapter.h"
  26. #include "llvm/Support/MSFileSystem.h"
  27. ///////////////////////////////////////////////////////////////////////////////////////////////////
  28. // Externally visible functions.
  29. HRESULT CreateMSFileSystemForDisk(_COM_Outptr_ ::llvm::sys::fs::MSFileSystem** pResult) throw();
  30. ///////////////////////////////////////////////////////////////////////////////////////////////////
  31. // Win32-and-CRT-based MSFileSystem implementation with direct filesystem access.
  32. namespace llvm {
  33. namespace sys {
  34. namespace fs {
  35. class MSFileSystemForDisk : public MSFileSystem
  36. {
  37. public:
  38. unsigned _defaultAttributes;
  39. MSFileSystemForDisk();
  40. virtual BOOL FindNextFileW(_In_ HANDLE hFindFile, _Out_ LPWIN32_FIND_DATAW lpFindFileData) throw() override;
  41. virtual HANDLE FindFirstFileW(_In_ LPCWSTR lpFileName, _Out_ LPWIN32_FIND_DATAW lpFindFileData) throw() override;
  42. virtual void FindClose(HANDLE findHandle) throw() override;
  43. virtual HANDLE CreateFileW(_In_ LPCWSTR lpFileName, _In_ DWORD dwDesiredAccess, _In_ DWORD dwShareMode, _In_ DWORD dwCreationDisposition, _In_ DWORD dwFlagsAndAttributes) throw() override;
  44. virtual BOOL SetFileTime(_In_ HANDLE hFile, _In_opt_ const FILETIME *lpCreationTime, _In_opt_ const FILETIME *lpLastAccessTime, _In_opt_ const FILETIME *lpLastWriteTime) throw() override;
  45. virtual BOOL GetFileInformationByHandle(_In_ HANDLE hFile, _Out_ LPBY_HANDLE_FILE_INFORMATION lpFileInformation) throw() override;
  46. virtual DWORD GetFileType(_In_ HANDLE hFile) throw() override;
  47. virtual BOOL CreateHardLinkW(_In_ LPCWSTR lpFileName, _In_ LPCWSTR lpExistingFileName) throw() override;
  48. virtual BOOL MoveFileExW(_In_ LPCWSTR lpExistingFileName, _In_opt_ LPCWSTR lpNewFileName, _In_ DWORD dwFlags) throw() override;
  49. virtual DWORD GetFileAttributesW(_In_ LPCWSTR lpFileName) throw() override;
  50. virtual BOOL CloseHandle(_In_ HANDLE hObject) throw() override;
  51. virtual BOOL DeleteFileW(_In_ LPCWSTR lpFileName) throw() override;
  52. virtual BOOL RemoveDirectoryW(_In_ LPCWSTR lpFileName) throw() override;
  53. virtual BOOL CreateDirectoryW(_In_ LPCWSTR lpPathName) throw() override;
  54. _Success_(return != 0 && return < nBufferLength)
  55. virtual DWORD GetCurrentDirectoryW(_In_ DWORD nBufferLength, _Out_writes_to_opt_(nBufferLength, return + 1) LPWSTR lpBuffer) throw() override;
  56. _Success_(return != 0 && return < nSize)
  57. virtual DWORD GetMainModuleFileNameW(__out_ecount_part(nSize, return + 1) LPWSTR lpFilename, DWORD nSize) throw() override;
  58. virtual DWORD GetTempPathW(DWORD nBufferLength, _Out_writes_to_opt_(nBufferLength, return + 1) LPWSTR lpBuffer) throw() override;
  59. virtual BOOLEAN CreateSymbolicLinkW(_In_ LPCWSTR lpSymlinkFileName, _In_ LPCWSTR lpTargetFileName, DWORD dwFlags) throw() override;
  60. virtual bool SupportsCreateSymbolicLink() throw() override;
  61. virtual BOOL ReadFile(_In_ HANDLE hFile, _Out_bytecap_(nNumberOfBytesToRead) LPVOID lpBuffer, _In_ DWORD nNumberOfBytesToRead, _Out_opt_ LPDWORD lpNumberOfBytesRead) throw() override;
  62. virtual HANDLE CreateFileMappingW(_In_ HANDLE hFile, _In_ DWORD flProtect, _In_ DWORD dwMaximumSizeHigh, _In_ DWORD dwMaximumSizeLow) throw() override;
  63. virtual LPVOID MapViewOfFile(_In_ HANDLE hFileMappingObject, _In_ DWORD dwDesiredAccess, _In_ DWORD dwFileOffsetHigh, _In_ DWORD dwFileOffsetLow, _In_ SIZE_T dwNumberOfBytesToMap) throw() override;
  64. virtual BOOL UnmapViewOfFile(_In_ LPCVOID lpBaseAddress) throw() override;
  65. // Console APIs.
  66. virtual bool FileDescriptorIsDisplayed(int fd) throw() override;
  67. virtual unsigned GetColumnCount(DWORD nStdHandle) throw() override;
  68. virtual unsigned GetConsoleOutputTextAttributes() throw() override;
  69. virtual void SetConsoleOutputTextAttributes(unsigned attributes) throw() override;
  70. virtual void ResetConsoleOutputTextAttributes() throw() override;
  71. // CRT APIs.
  72. virtual int open_osfhandle(intptr_t osfhandle, int flags) throw() override;
  73. virtual intptr_t get_osfhandle(int fd) throw() override;
  74. virtual int close(int fd) throw() override;
  75. virtual long lseek(int fd, long offset, int origin) throw() override;
  76. virtual int setmode(int fd, int mode) throw() override;
  77. virtual errno_t resize_file(_In_ LPCWSTR path, uint64_t size) throw() override;
  78. virtual int Read(int fd, _Out_bytecap_(count) void* buffer, unsigned int count) throw() override;
  79. virtual int Write(int fd, _In_bytecount_(count) const void* buffer, unsigned int count) throw() override;
  80. };
  81. MSFileSystemForDisk::MSFileSystemForDisk()
  82. {
  83. _defaultAttributes = GetConsoleOutputTextAttributes();
  84. }
  85. _Use_decl_annotations_
  86. BOOL MSFileSystemForDisk::FindNextFileW(HANDLE hFindFile, LPWIN32_FIND_DATAW lpFindFileData) throw()
  87. {
  88. return ::FindNextFileW(hFindFile, lpFindFileData);
  89. }
  90. _Use_decl_annotations_
  91. HANDLE MSFileSystemForDisk::FindFirstFileW(LPCWSTR lpFileName, LPWIN32_FIND_DATAW lpFindFileData) throw()
  92. {
  93. return ::FindFirstFileW(lpFileName, lpFindFileData);
  94. }
  95. void MSFileSystemForDisk::FindClose(HANDLE findHandle) throw()
  96. {
  97. ::FindClose(findHandle);
  98. }
  99. _Use_decl_annotations_
  100. HANDLE MSFileSystemForDisk::CreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes) throw()
  101. {
  102. return ::CreateFileW(lpFileName, dwDesiredAccess, dwShareMode, nullptr, dwCreationDisposition, dwFlagsAndAttributes, nullptr);
  103. }
  104. _Use_decl_annotations_
  105. BOOL MSFileSystemForDisk::SetFileTime(HANDLE hFile, _In_opt_ const FILETIME *lpCreationTime, _In_opt_ const FILETIME *lpLastAccessTime, _In_opt_ const FILETIME *lpLastWriteTime) throw()
  106. {
  107. return ::SetFileTime(hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime);
  108. }
  109. _Use_decl_annotations_
  110. BOOL MSFileSystemForDisk::GetFileInformationByHandle(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpFileInformation) throw()
  111. {
  112. return ::GetFileInformationByHandle(hFile, lpFileInformation);
  113. }
  114. _Use_decl_annotations_
  115. DWORD MSFileSystemForDisk::GetFileType(HANDLE hFile) throw()
  116. {
  117. return ::GetFileType(hFile);
  118. }
  119. _Use_decl_annotations_
  120. BOOL MSFileSystemForDisk::CreateHardLinkW(LPCWSTR lpFileName, LPCWSTR lpExistingFileName) throw()
  121. {
  122. return ::CreateHardLinkW(lpFileName, lpExistingFileName, nullptr);
  123. }
  124. _Use_decl_annotations_
  125. BOOL MSFileSystemForDisk::MoveFileExW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName, DWORD dwFlags) throw()
  126. {
  127. return ::MoveFileExW(lpExistingFileName, lpNewFileName, dwFlags);
  128. }
  129. _Use_decl_annotations_
  130. DWORD MSFileSystemForDisk::GetFileAttributesW(LPCWSTR lpFileName) throw()
  131. {
  132. return ::GetFileAttributesW(lpFileName);
  133. }
  134. _Use_decl_annotations_
  135. BOOL MSFileSystemForDisk::CloseHandle(HANDLE hObject) throw()
  136. {
  137. return ::CloseHandle(hObject);
  138. }
  139. _Use_decl_annotations_
  140. BOOL MSFileSystemForDisk::DeleteFileW(LPCWSTR lpFileName) throw()
  141. {
  142. return ::DeleteFileW(lpFileName);
  143. }
  144. _Use_decl_annotations_
  145. BOOL MSFileSystemForDisk::RemoveDirectoryW(LPCWSTR lpFileName) throw()
  146. {
  147. return ::RemoveDirectoryW(lpFileName);
  148. }
  149. _Use_decl_annotations_
  150. BOOL MSFileSystemForDisk::CreateDirectoryW(LPCWSTR lpPathName) throw()
  151. {
  152. return ::CreateDirectoryW(lpPathName, nullptr);
  153. }
  154. _Use_decl_annotations_
  155. DWORD MSFileSystemForDisk::GetCurrentDirectoryW(DWORD nBufferLength, LPWSTR lpBuffer) throw()
  156. {
  157. return ::GetCurrentDirectoryW(nBufferLength, lpBuffer);
  158. }
  159. _Use_decl_annotations_
  160. DWORD MSFileSystemForDisk::GetMainModuleFileNameW(LPWSTR lpFilename, DWORD nSize) throw()
  161. {
  162. // Add some code to ensure that the result is null terminated.
  163. if (nSize <= 1)
  164. {
  165. ::SetLastError(ERROR_INSUFFICIENT_BUFFER);
  166. return 0;
  167. }
  168. DWORD result = ::GetModuleFileNameW(nullptr, lpFilename, nSize - 1);
  169. if (result == 0) return result;
  170. lpFilename[result] = L'\0';
  171. return result;
  172. }
  173. _Use_decl_annotations_
  174. DWORD MSFileSystemForDisk::GetTempPathW(DWORD nBufferLength, LPWSTR lpBuffer) throw()
  175. {
  176. return ::GetTempPathW(nBufferLength, lpBuffer);
  177. }
  178. namespace {
  179. typedef BOOLEAN(WINAPI *PtrCreateSymbolicLinkW)(
  180. /*__in*/ LPCWSTR lpSymlinkFileName,
  181. /*__in*/ LPCWSTR lpTargetFileName,
  182. /*__in*/ DWORD dwFlags);
  183. PtrCreateSymbolicLinkW create_symbolic_link_api =
  184. PtrCreateSymbolicLinkW(::GetProcAddress(
  185. ::GetModuleHandleW(L"Kernel32.dll"), "CreateSymbolicLinkW"));
  186. }
  187. _Use_decl_annotations_
  188. BOOLEAN MSFileSystemForDisk::CreateSymbolicLinkW(LPCWSTR lpSymlinkFileName, LPCWSTR lpTargetFileName, DWORD dwFlags) throw()
  189. {
  190. return create_symbolic_link_api(lpSymlinkFileName, lpTargetFileName, dwFlags);
  191. }
  192. bool MSFileSystemForDisk::SupportsCreateSymbolicLink() throw()
  193. {
  194. return create_symbolic_link_api != nullptr;
  195. }
  196. _Use_decl_annotations_
  197. BOOL MSFileSystemForDisk::ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, _Out_opt_ LPDWORD lpNumberOfBytesRead) throw()
  198. {
  199. return ::ReadFile(hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, nullptr);
  200. }
  201. _Use_decl_annotations_
  202. HANDLE MSFileSystemForDisk::CreateFileMappingW(HANDLE hFile, DWORD flProtect, DWORD dwMaximumSizeHigh, DWORD dwMaximumSizeLow) throw()
  203. {
  204. return ::CreateFileMappingW(hFile, nullptr, flProtect, dwMaximumSizeHigh, dwMaximumSizeLow, nullptr);
  205. }
  206. _Use_decl_annotations_
  207. LPVOID MSFileSystemForDisk::MapViewOfFile(HANDLE hFileMappingObject, DWORD dwDesiredAccess, DWORD dwFileOffsetHigh, DWORD dwFileOffsetLow, SIZE_T dwNumberOfBytesToMap) throw()
  208. {
  209. return ::MapViewOfFile(hFileMappingObject, dwDesiredAccess, dwFileOffsetHigh, dwFileOffsetLow, dwNumberOfBytesToMap);
  210. }
  211. _Use_decl_annotations_
  212. BOOL MSFileSystemForDisk::UnmapViewOfFile(LPCVOID lpBaseAddress) throw()
  213. {
  214. return ::UnmapViewOfFile(lpBaseAddress);
  215. }
  216. bool MSFileSystemForDisk::FileDescriptorIsDisplayed(int fd) throw()
  217. {
  218. DWORD Mode; // Unused
  219. return (GetConsoleMode((HANDLE)_get_osfhandle(fd), &Mode) != 0);
  220. }
  221. unsigned MSFileSystemForDisk::GetColumnCount(DWORD nStdHandle) throw()
  222. {
  223. unsigned Columns = 0;
  224. CONSOLE_SCREEN_BUFFER_INFO csbi;
  225. if (::GetConsoleScreenBufferInfo(GetStdHandle(nStdHandle), &csbi))
  226. Columns = csbi.dwSize.X;
  227. return Columns;
  228. }
  229. unsigned MSFileSystemForDisk::GetConsoleOutputTextAttributes() throw()
  230. {
  231. CONSOLE_SCREEN_BUFFER_INFO csbi;
  232. if (::GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi))
  233. return csbi.wAttributes;
  234. return 0;
  235. }
  236. void MSFileSystemForDisk::SetConsoleOutputTextAttributes(unsigned attributes) throw()
  237. {
  238. ::SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attributes);
  239. }
  240. void MSFileSystemForDisk::ResetConsoleOutputTextAttributes() throw()
  241. {
  242. ::SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), _defaultAttributes);
  243. }
  244. int MSFileSystemForDisk::open_osfhandle(intptr_t osfhandle, int flags) throw()
  245. {
  246. return ::_open_osfhandle(osfhandle, flags);
  247. }
  248. intptr_t MSFileSystemForDisk::get_osfhandle(int fd) throw()
  249. {
  250. return ::_get_osfhandle(fd);
  251. }
  252. int MSFileSystemForDisk::close(int fd) throw()
  253. {
  254. return ::_close(fd);
  255. }
  256. long MSFileSystemForDisk::lseek(int fd, long offset, int origin) throw()
  257. {
  258. return ::_lseek(fd, offset, origin);
  259. }
  260. int MSFileSystemForDisk::setmode(int fd, int mode) throw()
  261. {
  262. return ::_setmode(fd, mode);
  263. }
  264. _Use_decl_annotations_
  265. errno_t MSFileSystemForDisk::resize_file(LPCWSTR path, uint64_t size) throw()
  266. {
  267. int fd = ::_wopen(path, O_BINARY | _O_RDWR, S_IWRITE);
  268. if (fd == -1)
  269. return errno;
  270. #ifdef HAVE__CHSIZE_S
  271. errno_t error = ::_chsize_s(fd, size);
  272. #else
  273. errno_t error = ::_chsize(fd, size);
  274. #endif
  275. ::_close(fd);
  276. return error;
  277. }
  278. _Use_decl_annotations_
  279. int MSFileSystemForDisk::Read(int fd, void* buffer, unsigned int count) throw()
  280. {
  281. return ::_read(fd, buffer, count);
  282. }
  283. _Use_decl_annotations_
  284. int MSFileSystemForDisk::Write(int fd, const void* buffer, unsigned int count) throw()
  285. {
  286. return ::_write(fd, buffer, count);
  287. }
  288. } // end namespace fs
  289. } // end namespace sys
  290. } // end namespace llvm
  291. ///////////////////////////////////////////////////////////////////////////////////////////////////
  292. // Externally visible functions.
  293. HRESULT CreateMSFileSystemForDisk(_COM_Outptr_ ::llvm::sys::fs::MSFileSystem** pResult) throw()
  294. {
  295. *pResult = new (std::nothrow) ::llvm::sys::fs::MSFileSystemForDisk();
  296. return (*pResult != nullptr) ? S_OK : E_OUTOFMEMORY;
  297. }