diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /util/system/fs_win.cpp | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'util/system/fs_win.cpp')
-rw-r--r-- | util/system/fs_win.cpp | 204 |
1 files changed, 102 insertions, 102 deletions
diff --git a/util/system/fs_win.cpp b/util/system/fs_win.cpp index 4498371365..a410ccac06 100644 --- a/util/system/fs_win.cpp +++ b/util/system/fs_win.cpp @@ -4,38 +4,38 @@ #include <util/folder/dirut.h> #include <util/charset/wide.h> -#include "file.h" +#include "file.h" #include <winioctl.h> namespace NFsPrivate { static LPCWSTR UTF8ToWCHAR(const TStringBuf str, TUtf16String& wstr) { wstr.resize(str.size()); - size_t written = 0; + size_t written = 0; if (!UTF8ToWide(str.data(), str.size(), wstr.begin(), written)) return nullptr; - wstr.erase(written); - static_assert(sizeof(WCHAR) == sizeof(wchar16), "expect sizeof(WCHAR) == sizeof(wchar16)"); + wstr.erase(written); + static_assert(sizeof(WCHAR) == sizeof(wchar16), "expect sizeof(WCHAR) == sizeof(wchar16)"); return (const WCHAR*)wstr.data(); } static TString WCHARToUTF8(const LPWSTR wstr, size_t len) { static_assert(sizeof(WCHAR) == sizeof(wchar16), "expect sizeof(WCHAR) == sizeof(wchar16)"); - return WideToUTF8((wchar16*)wstr, len); + return WideToUTF8((wchar16*)wstr, len); } HANDLE CreateFileWithUtf8Name(const TStringBuf fName, ui32 accessMode, ui32 shareMode, ui32 createMode, ui32 attributes, bool inheritHandle) { TUtf16String wstr; - LPCWSTR wname = UTF8ToWCHAR(fName, wstr); - if (!wname) { - ::SetLastError(ERROR_INVALID_NAME); - return INVALID_HANDLE_VALUE; - } - SECURITY_ATTRIBUTES secAttrs; - secAttrs.bInheritHandle = inheritHandle ? TRUE : FALSE; + LPCWSTR wname = UTF8ToWCHAR(fName, wstr); + if (!wname) { + ::SetLastError(ERROR_INVALID_NAME); + return INVALID_HANDLE_VALUE; + } + SECURITY_ATTRIBUTES secAttrs; + secAttrs.bInheritHandle = inheritHandle ? TRUE : FALSE; secAttrs.lpSecurityDescriptor = nullptr; - secAttrs.nLength = sizeof(secAttrs); + secAttrs.nLength = sizeof(secAttrs); return ::CreateFileW(wname, accessMode, shareMode, &secAttrs, createMode, attributes, nullptr); } @@ -43,10 +43,10 @@ namespace NFsPrivate { TUtf16String op, np; LPCWSTR opPtr = UTF8ToWCHAR(oldPath, op); LPCWSTR npPtr = UTF8ToWCHAR(newPath, np); - if (!opPtr || !npPtr) { - ::SetLastError(ERROR_INVALID_NAME); + if (!opPtr || !npPtr) { + ::SetLastError(ERROR_INVALID_NAME); return false; - } + } return MoveFileExW(opPtr, npPtr, MOVEFILE_REPLACE_EXISTING) != 0; } @@ -54,16 +54,16 @@ namespace NFsPrivate { bool WinRemove(const TString& path) { TUtf16String wstr; LPCWSTR wname = UTF8ToWCHAR(path, wstr); - if (!wname) { - ::SetLastError(ERROR_INVALID_NAME); + if (!wname) { + ::SetLastError(ERROR_INVALID_NAME); return false; - } - WIN32_FILE_ATTRIBUTE_DATA fad; - if (::GetFileAttributesExW(wname, GetFileExInfoStandard, &fad)) { - if (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + } + WIN32_FILE_ATTRIBUTE_DATA fad; + if (::GetFileAttributesExW(wname, GetFileExInfoStandard, &fad)) { + if (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) return ::RemoveDirectoryW(wname) != 0; return ::DeleteFileW(wname) != 0; - } + } return false; } @@ -71,53 +71,53 @@ namespace NFsPrivate { bool WinSymLink(const TString& targetName, const TString& linkName) { TString tName(targetName); { - size_t pos; + size_t pos; while ((pos = tName.find('/')) != TString::npos) - tName.replace(pos, 1, LOCSLASH_S); - } + tName.replace(pos, 1, LOCSLASH_S); + } TUtf16String tstr; - LPCWSTR wname = UTF8ToWCHAR(tName, tstr); + LPCWSTR wname = UTF8ToWCHAR(tName, tstr); TUtf16String lstr; LPCWSTR lname = UTF8ToWCHAR(linkName, lstr); - // we can't create a dangling link to a dir in this way - ui32 attr = ::GetFileAttributesW(wname); - if (attr == INVALID_FILE_ATTRIBUTES) { - TTempBuf result; - if (GetFullPathNameW(lname, result.Size(), (LPWSTR)result.Data(), 0) != 0) { + // we can't create a dangling link to a dir in this way + ui32 attr = ::GetFileAttributesW(wname); + if (attr == INVALID_FILE_ATTRIBUTES) { + TTempBuf result; + if (GetFullPathNameW(lname, result.Size(), (LPWSTR)result.Data(), 0) != 0) { TString fullPath = WideToUTF8(TWtringBuf((const wchar16*)result.Data())); - TStringBuf linkDir(fullPath); - linkDir.RNextTok('\\'); - - if (linkDir) { + TStringBuf linkDir(fullPath); + linkDir.RNextTok('\\'); + + if (linkDir) { TString fullTarget(tName); resolvepath(fullTarget, TString{linkDir}); TUtf16String fullTargetW; - LPCWSTR ptrFullTarget = UTF8ToWCHAR(fullTarget, fullTargetW); - attr = ::GetFileAttributesW(ptrFullTarget); - } + LPCWSTR ptrFullTarget = UTF8ToWCHAR(fullTarget, fullTargetW); + attr = ::GetFileAttributesW(ptrFullTarget); + } } } - return 0 != CreateSymbolicLinkW(lname, wname, attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY) ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0); + return 0 != CreateSymbolicLinkW(lname, wname, attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY) ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0); } bool WinHardLink(const TString& existingPath, const TString& newPath) { TUtf16String ep, np; LPCWSTR epPtr = UTF8ToWCHAR(existingPath, ep); LPCWSTR npPtr = UTF8ToWCHAR(newPath, np); - if (!epPtr || !npPtr) { - ::SetLastError(ERROR_INVALID_NAME); - return false; - } - + if (!epPtr || !npPtr) { + ::SetLastError(ERROR_INVALID_NAME); + return false; + } + return (CreateHardLinkW(npPtr, epPtr, nullptr) != 0); } bool WinExists(const TString& path) { TUtf16String buf; LPCWSTR ptr = UTF8ToWCHAR(path, buf); - return ::GetFileAttributesW(ptr) != INVALID_FILE_ATTRIBUTES; - } + return ::GetFileAttributesW(ptr) != INVALID_FILE_ATTRIBUTES; + } TString WinCurrentWorkingDirectory() { TTempBuf result; @@ -143,76 +143,76 @@ namespace NFsPrivate { LPCWSTR ptr = UTF8ToWCHAR(path, buf); return CreateDirectoryW(ptr, (LPSECURITY_ATTRIBUTES) nullptr); } - // edited part of <Ntifs.h> from Windows DDK + // edited part of <Ntifs.h> from Windows DDK #define SYMLINK_FLAG_RELATIVE 1 - struct TReparseBufferHeader { - USHORT SubstituteNameOffset; - USHORT SubstituteNameLength; - USHORT PrintNameOffset; - USHORT PrintNameLength; - }; - - struct TSymbolicLinkReparseBuffer: public TReparseBufferHeader { - ULONG Flags; // 0 or SYMLINK_FLAG_RELATIVE - wchar16 PathBuffer[1]; - }; - - struct TMountPointReparseBuffer: public TReparseBufferHeader { - wchar16 PathBuffer[1]; - }; - - struct TGenericReparseBuffer { - wchar16 DataBuffer[1]; - }; - - struct REPARSE_DATA_BUFFER { - ULONG ReparseTag; - USHORT ReparseDataLength; - USHORT Reserved; - union { - TSymbolicLinkReparseBuffer SymbolicLinkReparseBuffer; - TMountPointReparseBuffer MountPointReparseBuffer; - TGenericReparseBuffer GenericReparseBuffer; - }; + struct TReparseBufferHeader { + USHORT SubstituteNameOffset; + USHORT SubstituteNameLength; + USHORT PrintNameOffset; + USHORT PrintNameLength; }; - // the end of edited part of <Ntifs.h> + struct TSymbolicLinkReparseBuffer: public TReparseBufferHeader { + ULONG Flags; // 0 or SYMLINK_FLAG_RELATIVE + wchar16 PathBuffer[1]; + }; + + struct TMountPointReparseBuffer: public TReparseBufferHeader { + wchar16 PathBuffer[1]; + }; + + struct TGenericReparseBuffer { + wchar16 DataBuffer[1]; + }; + + struct REPARSE_DATA_BUFFER { + ULONG ReparseTag; + USHORT ReparseDataLength; + USHORT Reserved; + union { + TSymbolicLinkReparseBuffer SymbolicLinkReparseBuffer; + TMountPointReparseBuffer MountPointReparseBuffer; + TGenericReparseBuffer GenericReparseBuffer; + }; + }; + + // the end of edited part of <Ntifs.h> TString WinReadLink(const TString& name) { TFileHandle h = CreateFileWithUtf8Name(name, GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, - FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, true); - TTempBuf buf; - while (true) { - DWORD bytesReturned = 0; + FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, true); + TTempBuf buf; + while (true) { + DWORD bytesReturned = 0; BOOL res = DeviceIoControl(h, FSCTL_GET_REPARSE_POINT, nullptr, 0, buf.Data(), buf.Size(), &bytesReturned, nullptr); - if (res) { - REPARSE_DATA_BUFFER* rdb = (REPARSE_DATA_BUFFER*)buf.Data(); - if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) { - wchar16* str = (wchar16*)&rdb->SymbolicLinkReparseBuffer.PathBuffer[rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(wchar16)]; - size_t len = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar16); - return WideToUTF8(str, len); - } else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) { - wchar16* str = (wchar16*)&rdb->MountPointReparseBuffer.PathBuffer[rdb->MountPointReparseBuffer.SubstituteNameOffset / sizeof(wchar16)]; - size_t len = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar16); - return WideToUTF8(str, len); - } - //this reparse point is unsupported in arcadia + if (res) { + REPARSE_DATA_BUFFER* rdb = (REPARSE_DATA_BUFFER*)buf.Data(); + if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK) { + wchar16* str = (wchar16*)&rdb->SymbolicLinkReparseBuffer.PathBuffer[rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof(wchar16)]; + size_t len = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar16); + return WideToUTF8(str, len); + } else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT) { + wchar16* str = (wchar16*)&rdb->MountPointReparseBuffer.PathBuffer[rdb->MountPointReparseBuffer.SubstituteNameOffset / sizeof(wchar16)]; + size_t len = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar16); + return WideToUTF8(str, len); + } + //this reparse point is unsupported in arcadia return TString(); } else { - if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { - buf = TTempBuf(buf.Size() * 2); - } else { + if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) { + buf = TTempBuf(buf.Size() * 2); + } else { ythrow yexception() << "can't read link " << name; - } + } } } } - // 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 - //(it will be 'create' case really) - /* + // 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 + //(it will be 'create' case really) + /* bool GetObjectId(const char* path, GUID* id) { TFileHandle h = CreateFileWithUtf8Name(path, 0, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS, true); @@ -230,4 +230,4 @@ bool GetObjectId(const char* path, GUID* id) { } */ -} +} |