fs_win.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. #include "fs_win.h"
  2. #include "defaults.h"
  3. #include "maxlen.h"
  4. #include <util/folder/dirut.h>
  5. #include <util/charset/wide.h>
  6. #include "file.h"
  7. #include <winioctl.h>
  8. namespace NFsPrivate {
  9. static LPCWSTR UTF8ToWCHAR(const TStringBuf str, TUtf16String& wstr) {
  10. wstr.resize(str.size());
  11. size_t written = 0;
  12. if (!UTF8ToWide(str.data(), str.size(), wstr.begin(), written))
  13. return nullptr;
  14. wstr.erase(written);
  15. static_assert(sizeof(WCHAR) == sizeof(wchar16), "expect sizeof(WCHAR) == sizeof(wchar16)");
  16. return (const WCHAR*)wstr.data();
  17. }
  18. static TString WCHARToUTF8(const LPWSTR wstr, size_t len) {
  19. static_assert(sizeof(WCHAR) == sizeof(wchar16), "expect sizeof(WCHAR) == sizeof(wchar16)");
  20. return WideToUTF8((wchar16*)wstr, len);
  21. }
  22. HANDLE CreateFileWithUtf8Name(const TStringBuf fName, ui32 accessMode, ui32 shareMode, ui32 createMode, ui32 attributes, bool inheritHandle) {
  23. TUtf16String wstr;
  24. LPCWSTR wname = UTF8ToWCHAR(fName, wstr);
  25. if (!wname) {
  26. ::SetLastError(ERROR_INVALID_NAME);
  27. return INVALID_HANDLE_VALUE;
  28. }
  29. SECURITY_ATTRIBUTES secAttrs;
  30. secAttrs.bInheritHandle = inheritHandle ? TRUE : FALSE;
  31. secAttrs.lpSecurityDescriptor = nullptr;
  32. secAttrs.nLength = sizeof(secAttrs);
  33. return ::CreateFileW(wname, accessMode, shareMode, &secAttrs, createMode, attributes, nullptr);
  34. }
  35. bool WinRename(const TString& oldPath, const TString& newPath) {
  36. TUtf16String op, np;
  37. LPCWSTR opPtr = UTF8ToWCHAR(oldPath, op);
  38. LPCWSTR npPtr = UTF8ToWCHAR(newPath, np);
  39. if (!opPtr || !npPtr) {
  40. ::SetLastError(ERROR_INVALID_NAME);
  41. return false;
  42. }
  43. return MoveFileExW(opPtr, npPtr, MOVEFILE_REPLACE_EXISTING) != 0;
  44. }
  45. bool WinRemove(const TString& path) {
  46. TUtf16String wstr;
  47. LPCWSTR wname = UTF8ToWCHAR(path, wstr);
  48. if (!wname) {
  49. ::SetLastError(ERROR_INVALID_NAME);
  50. return false;
  51. }
  52. WIN32_FILE_ATTRIBUTE_DATA fad;
  53. if (::GetFileAttributesExW(wname, GetFileExInfoStandard, &fad)) {
  54. if (fad.dwFileAttributes & FILE_ATTRIBUTE_READONLY) {
  55. fad.dwFileAttributes = FILE_ATTRIBUTE_NORMAL;
  56. ::SetFileAttributesW(wname, fad.dwFileAttributes);
  57. }
  58. if (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  59. return ::RemoveDirectoryW(wname) != 0;
  60. return ::DeleteFileW(wname) != 0;
  61. }
  62. return false;
  63. }
  64. bool WinSymLink(const TString& targetName, const TString& linkName) {
  65. TString tName(targetName);
  66. {
  67. size_t pos;
  68. while ((pos = tName.find('/')) != TString::npos)
  69. tName.replace(pos, 1, LOCSLASH_S);
  70. }
  71. TUtf16String tstr;
  72. LPCWSTR wname = UTF8ToWCHAR(tName, tstr);
  73. TUtf16String lstr;
  74. LPCWSTR lname = UTF8ToWCHAR(linkName, lstr);
  75. // we can't create a dangling link to a dir in this way
  76. ui32 attr = ::GetFileAttributesW(wname);
  77. if (attr == INVALID_FILE_ATTRIBUTES) {
  78. TTempBuf result;
  79. if (GetFullPathNameW(lname, result.Size(), (LPWSTR)result.Data(), 0) != 0) {
  80. TString fullPath = WideToUTF8(TWtringBuf((const wchar16*)result.Data()));
  81. TStringBuf linkDir(fullPath);
  82. linkDir.RNextTok('\\');
  83. if (linkDir) {
  84. TString fullTarget(tName);
  85. resolvepath(fullTarget, TString{linkDir});
  86. TUtf16String fullTargetW;
  87. LPCWSTR ptrFullTarget = UTF8ToWCHAR(fullTarget, fullTargetW);
  88. attr = ::GetFileAttributesW(ptrFullTarget);
  89. }
  90. }
  91. }
  92. return 0 != CreateSymbolicLinkW(lname, wname, attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY) ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0);
  93. }
  94. bool WinHardLink(const TString& existingPath, const TString& newPath) {
  95. TUtf16String ep, np;
  96. LPCWSTR epPtr = UTF8ToWCHAR(existingPath, ep);
  97. LPCWSTR npPtr = UTF8ToWCHAR(newPath, np);
  98. if (!epPtr || !npPtr) {
  99. ::SetLastError(ERROR_INVALID_NAME);
  100. return false;
  101. }
  102. return (CreateHardLinkW(npPtr, epPtr, nullptr) != 0);
  103. }
  104. bool WinExists(const TString& path) {
  105. TUtf16String buf;
  106. LPCWSTR ptr = UTF8ToWCHAR(path, buf);
  107. return ::GetFileAttributesW(ptr) != INVALID_FILE_ATTRIBUTES;
  108. }
  109. TString WinCurrentWorkingDirectory() {
  110. TTempBuf result;
  111. LPWSTR buf = reinterpret_cast<LPWSTR>(result.Data());
  112. int r = GetCurrentDirectoryW(result.Size() / sizeof(WCHAR), buf);
  113. if (r == 0)
  114. throw TIoSystemError() << "failed to GetCurrentDirectory";
  115. return WCHARToUTF8(buf, r);
  116. }
  117. bool WinSetCurrentWorkingDirectory(const TString& path) {
  118. TUtf16String wstr;
  119. LPCWSTR wname = UTF8ToWCHAR(path, wstr);
  120. if (!wname) {
  121. ::SetLastError(ERROR_INVALID_NAME);
  122. return false;
  123. }
  124. return SetCurrentDirectoryW(wname);
  125. }
  126. bool WinMakeDirectory(const TString& path) {
  127. TUtf16String buf;
  128. LPCWSTR ptr = UTF8ToWCHAR(path, buf);
  129. return CreateDirectoryW(ptr, (LPSECURITY_ATTRIBUTES) nullptr);
  130. }
  131. // edited part of <Ntifs.h> from Windows DDK
  132. #define SYMLINK_FLAG_RELATIVE 1
  133. struct TReparseBufferHeader {
  134. USHORT SubstituteNameOffset;
  135. USHORT SubstituteNameLength;
  136. USHORT PrintNameOffset;
  137. USHORT PrintNameLength;
  138. };
  139. struct TSymbolicLinkReparseBuffer: public TReparseBufferHeader {
  140. ULONG Flags; // 0 or SYMLINK_FLAG_RELATIVE
  141. wchar16 PathBuffer[1];
  142. };
  143. struct TMountPointReparseBuffer: public TReparseBufferHeader {
  144. wchar16 PathBuffer[1];
  145. };
  146. struct TGenericReparseBuffer {
  147. wchar16 DataBuffer[1];
  148. };
  149. struct REPARSE_DATA_BUFFER {
  150. ULONG ReparseTag;
  151. USHORT ReparseDataLength;
  152. USHORT Reserved;
  153. union {
  154. TSymbolicLinkReparseBuffer SymbolicLinkReparseBuffer;
  155. TMountPointReparseBuffer MountPointReparseBuffer;
  156. TGenericReparseBuffer GenericReparseBuffer;
  157. };
  158. };
  159. // the end of edited part of <Ntifs.h>
  160. // For more info see:
  161. // * https://docs.microsoft.com/en-us/windows/win32/fileio/reparse-points
  162. // * https://docs.microsoft.com/en-us/windows-hardware/drivers/ifs/fsctl-get-reparse-point
  163. // * https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/ntifs/ns-ntifs-_reparse_data_buffer
  164. REPARSE_DATA_BUFFER* ReadReparsePoint(HANDLE h, TTempBuf& buf) {
  165. while (true) {
  166. DWORD bytesReturned = 0;
  167. BOOL res = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, nullptr, 0, buf.Data(), buf.Size(), &bytesReturned, nullptr);
  168. if (res) {
  169. REPARSE_DATA_BUFFER* rdb = (REPARSE_DATA_BUFFER*)buf.Data();
  170. return rdb;
  171. } else {
  172. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  173. buf = TTempBuf(buf.Size() * 2);
  174. } else {
  175. return nullptr;
  176. }
  177. }
  178. }
  179. }
  180. TString WinReadLink(const TString& name) {
  181. TFileHandle h = CreateFileWithUtf8Name(name, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING,
  182. FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, true);
  183. if (h == INVALID_HANDLE_VALUE) {
  184. ythrow TIoSystemError() << "can't open file " << name;
  185. }
  186. TTempBuf buf;
  187. REPARSE_DATA_BUFFER* rdb = ReadReparsePoint(h, buf);
  188. if (rdb == nullptr) {
  189. ythrow TIoSystemError() << "can't read reparse point " << name;
  190. }
  191. if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) {
  192. wchar16* str = (wchar16*)&rdb->SymbolicLinkReparseBuffer.PathBuffer[rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(wchar16)];
  193. size_t len = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar16);
  194. return WideToUTF8(str, len);
  195. } else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) {
  196. wchar16* str = (wchar16*)&rdb->MountPointReparseBuffer.PathBuffer[rdb->MountPointReparseBuffer.SubstituteNameOffset / sizeof(wchar16)];
  197. size_t len = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar16);
  198. return WideToUTF8(str, len);
  199. }
  200. // this reparse point is unsupported in arcadia
  201. return TString();
  202. }
  203. ULONG WinReadReparseTag(HANDLE h) {
  204. TTempBuf buf;
  205. REPARSE_DATA_BUFFER* rdb = ReadReparsePoint(h, buf);
  206. return rdb ? rdb->ReparseTag : 0;
  207. }
  208. // we can't use this function to get an analog of unix inode due to a lot of NTFS folders do not have this GUID
  209. // (it will be 'create' case really)
  210. /*
  211. bool GetObjectId(const char* path, GUID* id) {
  212. TFileHandle h = CreateFileWithUtf8Name(path, 0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
  213. OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, true);
  214. if (h.IsOpen()) {
  215. FILE_OBJECTID_BUFFER fob;
  216. DWORD resSize = 0;
  217. if (DeviceIoControl(h, FSCTL_CREATE_OR_GET_OBJECT_ID, nullptr, 0, &fob, sizeof(fob), &resSize, nullptr)) {
  218. Y_ASSERT(resSize == sizeof(fob));
  219. memcpy(id, &fob.ObjectId, sizeof(GUID));
  220. return true;
  221. }
  222. }
  223. memset(id, 0, sizeof(GUID));
  224. return false;
  225. }
  226. */
  227. } // namespace NFsPrivate