From e4f14624f32833356097000d40d20301aa5c363b Mon Sep 17 00:00:00 2001 From: Rose <83477269+AtariDreams@users.noreply.github.com> Date: Thu, 30 Jun 2022 21:58:38 -0400 Subject: [PATCH] Refactor Filesystem Code for Clarity (#2766) Co-authored-by: Stephan T. Lavavej Co-authored-by: nicole mazzuca <83086508+strega-nil-ms@users.noreply.github.com> --- stl/inc/cvt/one_one | 2 +- stl/inc/cvt/utf8 | 2 +- stl/inc/experimental/filesystem | 12 ++-- stl/inc/filesystem | 4 +- stl/src/filesys.cpp | 117 +++++++++++++++----------------- stl/src/filesystem.cpp | 67 +++++++++--------- 6 files changed, 97 insertions(+), 107 deletions(-) diff --git a/stl/inc/cvt/one_one b/stl/inc/cvt/one_one index 802713ed2db..bbfc0caac8e 100644 --- a/stl/inc/cvt/one_one +++ b/stl/inc/cvt/one_one @@ -25,7 +25,7 @@ namespace stdext { using _Statype = _CSTD mbstate_t; _STL_DISABLE_DEPRECATED_WARNING - template class codecvt_one_one : public _STD codecvt<_Elem, char, _Statype> { // facet for converting between _Elem and serialized byte sequences diff --git a/stl/inc/cvt/utf8 b/stl/inc/cvt/utf8 index bf1dc0e94a8..2719e884128 100644 --- a/stl/inc/cvt/utf8 +++ b/stl/inc/cvt/utf8 @@ -25,7 +25,7 @@ namespace stdext { using _Statype = _CSTD mbstate_t; _STL_DISABLE_DEPRECATED_WARNING - template + template class codecvt_utf8 : public _STD codecvt<_Elem, char, _Statype> { // facet for converting between _Elem and UTF-8 byte sequences public: diff --git a/stl/inc/experimental/filesystem b/stl/inc/experimental/filesystem index 01250604026..8a78e8cf300 100644 --- a/stl/inc/experimental/filesystem +++ b/stl/inc/experimental/filesystem @@ -283,7 +283,7 @@ struct _Path_cvt, } wstring_convert, wchar_t> _Wcvt; - wchar_t* _Last1 = &_Fname_wide[0]; + wchar_t* _Last1 = _Fname_wide; while (*_Last1 != L'\0') { ++_Last1; } - return _STD move(_Str.append(_Wcvt.to_bytes(&_Fname_wide[0], _Last1))); + return _STD move(_Str.append(_Wcvt.to_bytes(_Fname_wide, _Last1))); } }; @@ -359,7 +359,7 @@ struct _Path_cvt<_Char8_t, char, _Outtraits, _Xinvalid_argument("invalid UTF8 filename argument"); } - return _STD move(_Str.append(&_Fname_byte[0])); + return _STD move(_Str.append(_Fname_byte)); } }; @@ -1636,7 +1636,7 @@ public: } void pop() { // pop a level - if (1 < _Mylist.size()) { + if (_Mylist.size() > 1) { _Mylist.pop_front(); // something to pop, do it } } diff --git a/stl/inc/filesystem b/stl/inc/filesystem index 8d5b70ccf72..d089a1eebb4 100644 --- a/stl/inc/filesystem +++ b/stl/inc/filesystem @@ -3542,12 +3542,12 @@ namespace filesystem { } _NODISCARD inline bool is_other(const path& _Path) { - // tests whether _Path is an other file (such as a junction) + // tests whether _Path is an "other" file (such as a junction) return _STD filesystem::is_other(_STD filesystem::status(_Path)); } _NODISCARD inline bool is_other(const path& _Path, error_code& _Ec) noexcept { - // tests whether _Path is an other file (such as a junction) + // tests whether _Path is an "other" file (such as a junction) return _STD filesystem::is_other(_STD filesystem::status(_Path, _Ec)); } diff --git a/stl/src/filesys.cpp b/stl/src/filesys.cpp index 9a68da5480d..4ef5b02356a 100644 --- a/stl/src/filesys.cpp +++ b/stl/src/filesys.cpp @@ -15,8 +15,6 @@ #include -#include "awint.hpp" - _FS_BEGIN static file_type _Map_mode(int _Mode) { // map Windows file attributes to file_status constexpr int _File_attribute_regular = @@ -60,12 +58,7 @@ _FS_DLL wchar_t* __CLRCALL_PURE_OR_CDECL _Read_dir( wchar_t (&_Dest)[_MAX_FILESYS_NAME], void* _Handle, file_type& _Ftype) { // read a directory entry WIN32_FIND_DATAW _Dentry; - for (;;) { - if (FindNextFileW(_Handle, &_Dentry) == 0) { // fail - _Ftype = file_type::unknown; - return _Strcpy(_Dest, L""); - } - + while (FindNextFileW(_Handle, &_Dentry)) { if (_Dentry.cFileName[0] != L'.' || (_Dentry.cFileName[1] != L'\0' && (_Dentry.cFileName[1] != L'.' @@ -74,6 +67,9 @@ _FS_DLL wchar_t* __CLRCALL_PURE_OR_CDECL _Read_dir( return _Strcpy(_Dest, &_Dentry.cFileName[0]); } } + + _Ftype = file_type::unknown; + return _Strcpy(_Dest, L""); } static unsigned int _Filesys_code_page() { // determine appropriate code page @@ -122,17 +118,16 @@ _FS_DLL void* __CLRCALL_PURE_OR_CDECL _Open_dir( && (_Dentry.cFileName[1] == L'\0' || _Dentry.cFileName[1] == L'.' && _Dentry.cFileName[2] == L'\0')) { // skip "." and ".." _Read_dir(_Dest, _Handle, _Ftype); - if (_Dest[0] != L'\0') { - return _Handle; + if (_Dest[0] == L'\0') { + // no entries, release handle + _Close_dir(_Handle); + return nullptr; } - - // no entries, release handle - _Close_dir(_Handle); - return nullptr; + return _Handle; } // get file type and return handle - _Strcpy(_Dest, &_Dentry.cFileName[0]); + _Strcpy(_Dest, _Dentry.cFileName); _Ftype = _Map_mode(_Dentry.dwFileAttributes); return _Handle; } @@ -161,21 +156,19 @@ _FS_DLL bool __CLRCALL_PURE_OR_CDECL _Current_set(const wchar_t* _Dirname) { _FS_DLL wchar_t* __CLRCALL_PURE_OR_CDECL _Symlink_get(wchar_t (&_Dest)[_MAX_FILESYS_NAME], const wchar_t*) { // get symlink -- DUMMY _Dest[0] = L'\0'; - return &_Dest[0]; + return _Dest; } _FS_DLL wchar_t* __CLRCALL_PURE_OR_CDECL _Temp_get(wchar_t (&_Dest)[_MAX_FILESYS_NAME]) { // get temp directory wchar_t _Dentry[MAX_PATH]; - return _Strcpy(_Dest, GetTempPathW(MAX_PATH, &_Dentry[0]) == 0 ? L"." : &_Dentry[0]); + return _Strcpy(_Dest, GetTempPathW(MAX_PATH, _Dentry) != 0 ? _Dentry : L"."); } _FS_DLL int __CLRCALL_PURE_OR_CDECL _Make_dir(const wchar_t* _Fname, const wchar_t*) { // make a new directory (ignore attributes) - int _Ans = CreateDirectoryW(_Fname, nullptr); - - if (_Ans != 0) { + if (CreateDirectoryW(_Fname, nullptr)) { return 1; } else if (GetLastError() == ERROR_ALREADY_EXISTS) { return 0; @@ -197,20 +190,23 @@ _FS_DLL file_type __CLRCALL_PURE_OR_CDECL _Stat(const wchar_t* _Fname, perms* _P constexpr perms _Write_perms = perms::owner_write | perms::group_write | perms::others_write; constexpr perms _Readonly_perms = perms::all & ~_Write_perms; - *_Pmode = _Data.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? _Readonly_perms : perms::all; + *_Pmode = (_Data.dwFileAttributes & FILE_ATTRIBUTE_READONLY) != 0u ? _Readonly_perms : perms::all; } return _Map_mode(_Data.dwFileAttributes); } // invalid, get error code - int _Errno = GetLastError(); - - if (_Errno == ERROR_BAD_NETPATH || _Errno == ERROR_BAD_PATHNAME || _Errno == ERROR_FILE_NOT_FOUND - || _Errno == ERROR_INVALID_DRIVE || _Errno == ERROR_INVALID_NAME || _Errno == ERROR_INVALID_PARAMETER - || _Errno == ERROR_PATH_NOT_FOUND) { + switch (GetLastError()) { + case ERROR_FILE_NOT_FOUND: + case ERROR_PATH_NOT_FOUND: + case ERROR_INVALID_NAME: + case ERROR_INVALID_DRIVE: + case ERROR_INVALID_PARAMETER: + case ERROR_BAD_NETPATH: + case ERROR_BAD_PATHNAME: return file_type::not_found; - } else { + default: return file_type::unknown; } } @@ -221,39 +217,39 @@ _FS_DLL file_type __CLRCALL_PURE_OR_CDECL _Lstat(const wchar_t* _Fname, perms* _ return _Stat(_Fname, _Pmode); // symlink not supported } -_FS_DLL unsigned long long __CLRCALL_PURE_OR_CDECL _Hard_links(const wchar_t* _Fname) { +_FS_DLL uintmax_t __CLRCALL_PURE_OR_CDECL _Hard_links(const wchar_t* _Fname) { // get hard link count HANDLE _Handle = _FilesysOpenFile(_Fname, FILE_READ_ATTRIBUTES, FILE_FLAG_BACKUP_SEMANTICS); + if (_Handle == INVALID_HANDLE_VALUE) { + return static_cast(-1); + } + #ifdef _CRT_APP FILE_STANDARD_INFO _Info = {0}; - bool _Ok = false; - if (_Handle != INVALID_HANDLE_VALUE) { // get file info - _Ok = GetFileInformationByHandleEx(_Handle, FileStandardInfo, &_Info, sizeof(_Info)) != 0; - CloseHandle(_Handle); - } - return _Ok ? _Info.NumberOfLinks : static_cast(-1); + // get file info + const auto _Ok = GetFileInformationByHandleEx(_Handle, FileStandardInfo, &_Info, sizeof(_Info)); + CloseHandle(_Handle); + return _Ok ? _Info.NumberOfLinks : static_cast(-1); #else // _CRT_APP BY_HANDLE_FILE_INFORMATION _Info = {0}; - bool _Ok = false; - if (_Handle != INVALID_HANDLE_VALUE) { // get file info - _Ok = GetFileInformationByHandle(_Handle, &_Info) != 0; - CloseHandle(_Handle); - } - return _Ok ? _Info.nNumberOfLinks : static_cast(-1); + // get file info + const auto _Ok = GetFileInformationByHandle(_Handle, &_Info); + CloseHandle(_Handle); + return _Ok ? _Info.nNumberOfLinks : static_cast(-1); #endif // _CRT_APP } -_FS_DLL unsigned long long __CLRCALL_PURE_OR_CDECL _File_size(const wchar_t* _Fname) { // get file size +_FS_DLL uintmax_t __CLRCALL_PURE_OR_CDECL _File_size(const wchar_t* _Fname) { // get file size WIN32_FILE_ATTRIBUTE_DATA _Data; - if (!GetFileAttributesExW(_Fname, GetFileExInfoStandard, &_Data)) { - return static_cast(-1); + if (GetFileAttributesExW(_Fname, GetFileExInfoStandard, &_Data)) { + return static_cast(_Data.nFileSizeHigh) << 32 | _Data.nFileSizeLow; } else { - return static_cast(_Data.nFileSizeHigh) << 32 | _Data.nFileSizeLow; + return static_cast(-1); } } @@ -279,8 +275,8 @@ _FS_DLL int64_t __CLRCALL_PURE_OR_CDECL _Last_write_time(const wchar_t* _Fname) } // success, convert time - unsigned long long _Wtime = static_cast(_Data.ftLastWriteTime.dwHighDateTime) << 32 - | _Data.ftLastWriteTime.dwLowDateTime; + uint64_t _Wtime = + static_cast(_Data.ftLastWriteTime.dwHighDateTime) << 32 | _Data.ftLastWriteTime.dwLowDateTime; return static_cast(_Wtime - _Win_ticks_from_epoch); } @@ -294,9 +290,9 @@ _FS_DLL int __CLRCALL_PURE_OR_CDECL _Set_last_write_time(const wchar_t* _Fname, } // convert to FILETIME and set - unsigned long long _Wtime = static_cast(_When) + _Win_ticks_from_epoch; + uint64_t _Wtime = static_cast(_When) + _Win_ticks_from_epoch; FILETIME _Ft; - _Ft.dwLowDateTime = static_cast(_Wtime); // intentionally discard upper bits + _Ft.dwLowDateTime = static_cast(_Wtime & 0xFFFFFFFFUL); _Ft.dwHighDateTime = static_cast(_Wtime >> 32); int _Result = SetFileTime(_Handle, nullptr, nullptr, &_Ft); CloseHandle(_Handle); @@ -351,10 +347,7 @@ _FS_DLL int __CLRCALL_PURE_OR_CDECL _Equivalent( } else if (!_Ok1 || !_Ok2) { return 0; } else { // test existing files for equivalence - return _Info1.VolumeSerialNumber != _Info2.VolumeSerialNumber - || memcmp(&_Info1.FileId, &_Info2.FileId, sizeof(_Info1.FileId)) != 0 - ? 0 - : 1; + return memcmp(&_Info1, &_Info2, sizeof(_FILE_ID_INFO)) == 0 ? 1 : 0; } #else // _CRT_APP BY_HANDLE_FILE_INFORMATION _Info1 = {0}; @@ -379,10 +372,10 @@ _FS_DLL int __CLRCALL_PURE_OR_CDECL _Equivalent( } else if (!_Ok1 || !_Ok2) { return 0; } else { // test existing files for equivalence - return _Info1.dwVolumeSerialNumber != _Info2.dwVolumeSerialNumber - || _Info1.nFileIndexHigh != _Info2.nFileIndexHigh || _Info1.nFileIndexLow != _Info2.nFileIndexLow - ? 0 - : 1; + return _Info1.dwVolumeSerialNumber == _Info2.dwVolumeSerialNumber + && _Info1.nFileIndexHigh == _Info2.nFileIndexHigh && _Info1.nFileIndexLow == _Info2.nFileIndexLow + ? 1 + : 0; } #endif // _CRT_APP } @@ -395,7 +388,7 @@ _FS_DLL int __CLRCALL_PURE_OR_CDECL _Link(const wchar_t* _Fname1, const wchar_t* (void) _Fname2; return errno = EDOM; // hardlinks not supported #else // _CRT_APP - return CreateHardLinkW(_Fname2, _Fname1, nullptr) != 0 ? 0 : GetLastError(); + return CreateHardLinkW(_Fname2, _Fname1, nullptr) ? 0 : GetLastError(); #endif // _CRT_APP } @@ -406,7 +399,7 @@ _FS_DLL int __CLRCALL_PURE_OR_CDECL _Symlink(const wchar_t* _Fname1, const wchar (void) _Fname2; return errno = EDOM; // symlinks not supported #else // _CRT_APP - return CreateSymbolicLinkW(_Fname2, _Fname1, 0) != 0 ? 0 : GetLastError(); + return CreateSymbolicLinkW(_Fname2, _Fname1, 0) ? 0 : GetLastError(); #endif // _CRT_APP } @@ -449,7 +442,7 @@ _FS_DLL int __CLRCALL_PURE_OR_CDECL _Copy_file(const wchar_t* _Fname1, const wch // take lower bits to undo HRESULT_FROM_WIN32 return _Copy_result & 0x0000FFFFU; #else // defined(_ONECORE) - return CopyFileW(_Fname1, _Fname2, 0) != 0 ? 0 : GetLastError(); + return CopyFileW(_Fname1, _Fname2, 0) ? 0 : GetLastError(); #endif // defined(_ONECORE) } @@ -464,14 +457,16 @@ _FS_DLL int __CLRCALL_PURE_OR_CDECL _Chmod(const wchar_t* _Fname, perms _Newmode // got mode, alter readonly bit DWORD _Oldmode = _Data.dwFileAttributes; - DWORD _Mode = _Oldmode & ~FILE_ATTRIBUTE_READONLY; + DWORD _Mode = _Oldmode; constexpr perms _Write_perms = perms::owner_write | perms::group_write | perms::others_write; if ((_Newmode & _Write_perms) == perms::none) { _Mode |= FILE_ATTRIBUTE_READONLY; + } else { + _Mode &= ~FILE_ATTRIBUTE_READONLY; } - return _Mode == _Oldmode ? 0 : SetFileAttributesW(_Fname, _Mode) != 0 ? 0 : -1; + return _Mode == _Oldmode || SetFileAttributesW(_Fname, _Mode) ? 0 : -1; } _FS_END diff --git a/stl/src/filesystem.cpp b/stl/src/filesystem.cpp index 3602a600e77..db2141074f7 100644 --- a/stl/src/filesystem.cpp +++ b/stl/src/filesystem.cpp @@ -89,14 +89,13 @@ namespace { [[nodiscard]] __std_win_error __stdcall _Create_symlink( const wchar_t* const _Symlink_file_name, const wchar_t* const _Target_file_name, const DWORD _Flags) noexcept { if (__vcrt_CreateSymbolicLinkW( - _Symlink_file_name, _Target_file_name, _Flags | 0x2 /* SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE */) - != 0) { + _Symlink_file_name, _Target_file_name, _Flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)) { return __std_win_error::_Success; } DWORD _Last_error = GetLastError(); if (_Last_error == ERROR_INVALID_PARAMETER) { - if (__vcrt_CreateSymbolicLinkW(_Symlink_file_name, _Target_file_name, _Flags) != 0) { + if (__vcrt_CreateSymbolicLinkW(_Symlink_file_name, _Target_file_name, _Flags)) { return __std_win_error::_Success; } @@ -118,8 +117,8 @@ namespace { const HANDLE _Handle, long long* const _Last_write_filetime) { // read the last write time from _Handle and store it in _Last_write_filetime FILE_BASIC_INFO _Ex_info; - if (GetFileInformationByHandleEx(_Handle, FileBasicInfo, &_Ex_info, sizeof(_Ex_info)) != 0) { - _CSTD memcpy(_Last_write_filetime, &_Ex_info.LastWriteTime, sizeof(*_Last_write_filetime)); + if (GetFileInformationByHandleEx(_Handle, FileBasicInfo, &_Ex_info, sizeof(_Ex_info))) { + *_Last_write_filetime = _Ex_info.LastWriteTime.QuadPart; return __std_win_error::_Success; } @@ -128,13 +127,14 @@ namespace { [[nodiscard]] __std_win_error __stdcall _Get_file_id_by_handle( const HANDLE _Handle, _Out_ FILE_ID_INFO* const _Id) noexcept { - __std_win_error _Last_error; - if (GetFileInformationByHandleEx(_Handle, FileIdInfo, _Id, sizeof(*_Id)) != 0) { + if (GetFileInformationByHandleEx(_Handle, FileIdInfo, _Id, sizeof(*_Id))) { // if we could get FILE_ID_INFO, use that as the source of truth return __std_win_error::_Success; } - _Last_error = __std_win_error{GetLastError()}; + __std_win_error _Last_error{GetLastError()}; + +#ifndef _CRT_APP switch (_Last_error) { case __std_win_error::_Not_supported: case __std_win_error::_Invalid_parameter: @@ -143,10 +143,9 @@ namespace { return _Last_error; // real error, bail to the caller } -#ifndef _CRT_APP // try GetFileInformationByHandle as a fallback BY_HANDLE_FILE_INFORMATION _Info; - if (GetFileInformationByHandle(_Handle, &_Info) != 0) { + if (GetFileInformationByHandle(_Handle, &_Info)) { _Id->VolumeSerialNumber = _Info.dwVolumeSerialNumber; _CSTD memcpy(&_Id->FileId.Identifier[0], &_Info.nFileIndexHigh, 8); _CSTD memset(&_Id->FileId.Identifier[8], 0, 8); @@ -188,7 +187,7 @@ namespace { return __std_win_error{GetLastError()}; } - [[nodiscard]] unsigned long long _Merge_to_ull(unsigned long _High, unsigned long _Low) noexcept { + [[nodiscard]] unsigned long long _Merge_to_ull(DWORD _High, DWORD _Low) noexcept { return (static_cast(_High) << 32) | static_cast(_Low); } } // unnamed namespace @@ -212,7 +211,7 @@ _EXTERN_C } void __stdcall __std_fs_close_handle(const __std_fs_file_handle _Handle) noexcept { // calls CloseHandle - if (_Handle != __std_fs_file_handle::_Invalid && CloseHandle(reinterpret_cast(_Handle)) == 0) { + if (_Handle != __std_fs_file_handle::_Invalid && !CloseHandle(reinterpret_cast(_Handle))) { terminate(); } } @@ -224,7 +223,7 @@ void __stdcall __std_fs_close_handle(const __std_fs_file_handle _Handle) noexcep const HANDLE _As_plain_handle = reinterpret_cast(_Handle); FILE_BASIC_INFO _Ex_info; - if (GetFileInformationByHandleEx(_As_plain_handle, FileBasicInfo, &_Ex_info, sizeof(_Ex_info)) != 0) { + if (GetFileInformationByHandleEx(_As_plain_handle, FileBasicInfo, &_Ex_info, sizeof(_Ex_info))) { *_File_attributes = _Ex_info.FileAttributes; return __std_win_error::_Success; } @@ -419,10 +418,7 @@ void __stdcall __std_fs_directory_iterator_close(_In_ const __std_fs_dir_handle return {false, _Last_error}; } - if (_Source_id.VolumeSerialNumber == _Target_id.VolumeSerialNumber - && _CSTD memcmp(_Source_id.FileId.Identifier, _Target_id.FileId.Identifier, - sizeof(_Source_id.FileId.Identifier)) - == 0) { + if (_CSTD memcmp(&_Source_id, &_Target_id, sizeof(FILE_ID_INFO)) == 0) { // the files are equivalent return {false, __std_win_error::_Sharing_violation}; } @@ -468,7 +464,7 @@ _Success_(return == __std_win_error::_Success) __std_win_error (void) _Existing_file_name; return __std_win_error::_Not_supported; #else // ^^^ defined(_CRT_APP) ^^^ // vvv !defined(_CRT_APP) vvv - if (CreateHardLinkW(_File_name, _Existing_file_name, nullptr) != 0) { + if (CreateHardLinkW(_File_name, _Existing_file_name, nullptr)) { return __std_win_error::_Success; } @@ -484,14 +480,13 @@ _Success_(return == __std_win_error::_Success) __std_win_error [[nodiscard]] __std_win_error __stdcall __std_fs_read_reparse_data_buffer(_In_ const __std_fs_file_handle _Handle, _Out_writes_bytes_(_Buffer_size) void* const _Buffer, _In_ const unsigned long _Buffer_size) noexcept { unsigned long _Bytes_returned; - // If DeviceIoControl fails, it returns 0 and _Bytes_returned is 0. - if (0 - == DeviceIoControl(reinterpret_cast(_Handle), FSCTL_GET_REPARSE_POINT, nullptr, 0, _Buffer, - _Buffer_size, &_Bytes_returned, nullptr)) { - return __std_win_error{GetLastError()}; + if (DeviceIoControl(reinterpret_cast(_Handle), FSCTL_GET_REPARSE_POINT, nullptr, 0, _Buffer, _Buffer_size, + &_Bytes_returned, nullptr)) { + return __std_win_error::_Success; } - return __std_win_error::_Success; + // If DeviceIoControl fails, _Bytes_returned is 0. + return __std_win_error{GetLastError()}; } [[nodiscard]] _Success_(return == __std_win_error::_Success) __std_win_error @@ -586,7 +581,7 @@ _Success_(return == __std_win_error::_Success) __std_win_error return {false, __std_win_error{GetLastError()}}; } // check if FILE_ATTRIBUTE_READONLY is set - if (_Basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY) { + if ((_Basic_info.FileAttributes & FILE_ATTRIBUTE_READONLY) != 0u) { // try to remove FILE_ATTRIBUTE_READONLY _Basic_info.FileAttributes ^= FILE_ATTRIBUTE_READONLY; if (!SetFileInformationByHandle(_Handle._Get(), FileBasicInfo, &_Basic_info, sizeof(_Basic_info))) { @@ -623,7 +618,7 @@ _Success_(return == __std_win_error::_Success) __std_win_error } const DWORD _Readonly_test = _Readonly ? FILE_ATTRIBUTE_READONLY : 0; - if ((_Old_attributes & FILE_ATTRIBUTE_REPARSE_POINT) && _Follow_symlinks) { + if ((_Old_attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0u && _Follow_symlinks) { __std_win_error _Err; _STD _Fs_file _Handle(_Path, __std_access_rights::_File_read_attributes | __std_access_rights::_File_write_attributes, @@ -778,11 +773,11 @@ _Success_(return == __std_win_error::_Success) __std_win_error // Effects: If exists(p) is false or is_directory(p) is false, an error is reported const DWORD _Attributes = GetFileAttributesW(_Target); - if (_Attributes == INVALID_FILE_ATTRIBUTES || !(_Attributes & FILE_ATTRIBUTE_DIRECTORY)) { + if (_Attributes == INVALID_FILE_ATTRIBUTES || (_Attributes & FILE_ATTRIBUTE_DIRECTORY) == 0u) { return {_Size, __std_win_error::_Max}; } - if (_Attributes & FILE_ATTRIBUTE_REPARSE_POINT) { + if ((_Attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0u) { __std_fs_file_handle _Handle; const auto _Last_error = __std_fs_open_handle( &_Handle, _Target, __std_access_rights::_File_read_attributes, __std_fs_file_flags::_Backup_semantics); @@ -894,7 +889,7 @@ _Success_(return == __std_win_error::_Success) __std_win_error if (_Bitmask_includes(_Flags, __std_fs_stats_flags::_Reparse_tag)) { // Calling GetFileInformationByHandleEx with FileAttributeTagInfo fails on FAT file system with // ERROR_INVALID_PARAMETER. We avoid calling this for non-reparse-points. - if (_Info.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { + if ((_Info.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0u) { FILE_ATTRIBUTE_TAG_INFO _TagInfo; if (!GetFileInformationByHandleEx(_Handle._Get(), FileAttributeTagInfo, &_TagInfo, sizeof(_TagInfo))) { return __std_win_error{GetLastError()}; @@ -974,20 +969,20 @@ _Success_(return == __std_win_error::_Success) __std_win_error // If getting the path failed, GetCurrentDirectoryW returns 0; otherwise, returns the size of the expected // directory. const auto _Size = GetCurrentDirectoryW(_Target_size, _Target); - if (_Size == 0 || _Size > _Target_size) { - return {_Size, __std_win_error{GetLastError()}}; + if (_Size != 0 && _Size <= _Target_size) { + return {_Size, __std_win_error::_Success}; } - return {_Size, __std_win_error::_Success}; + return {_Size, __std_win_error{GetLastError()}}; } [[nodiscard]] __std_win_error __stdcall __std_fs_set_current_path(_In_z_ const wchar_t* const _Target) noexcept { // If setting the path failed, SetCurrentDirectoryW returns 0; otherwise returns non-zero. - const auto _Succeeded = SetCurrentDirectoryW(_Target); - if (_Succeeded == 0) { - return __std_win_error{GetLastError()}; + if (SetCurrentDirectoryW(_Target)) { + return __std_win_error::_Success; } - return __std_win_error::_Success; + + return __std_win_error{GetLastError()}; } _END_EXTERN_C