Skip to content

Commit

Permalink
Refactor filesystem code for clarity
Browse files Browse the repository at this point in the history
This makes the code more readable as well as lowers operations to code paths that actually need them.
  • Loading branch information
AZero13 committed Jun 9, 2022
1 parent 17fde2c commit 56d6012
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 50 deletions.
2 changes: 1 addition & 1 deletion stl/inc/cvt/one_one
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace stdext {
using _Statype = _CSTD mbstate_t;

_STL_DISABLE_DEPRECATED_WARNING
template <class _Elem, unsigned long _Maxcode = 0xffffffff, _STD codecvt_mode _Mode = _STD codecvt_mode{},
template <class _Elem, unsigned long _Maxcode = 0xFFFFFFFFUL, _STD codecvt_mode _Mode = _STD codecvt_mode{},
size_t _Bytes_per_word = sizeof(_Elem)>
class codecvt_one_one
: public _STD
Expand Down
2 changes: 1 addition & 1 deletion stl/inc/cvt/utf8
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace stdext {
using _Statype = _CSTD mbstate_t;

_STL_DISABLE_DEPRECATED_WARNING
template <class _Elem, unsigned long _Maxcode = 0xffffffff, _STD codecvt_mode _Mode = _STD codecvt_mode{}>
template <class _Elem, unsigned long _Maxcode = 0xFFFFFFFFUL, _STD codecvt_mode _Mode = _STD codecvt_mode{}>
class codecvt_utf8
: public _STD
codecvt<_Elem, char, _Statype> { // facet for converting between _Elem and UTF-8 byte sequences
Expand Down
77 changes: 39 additions & 38 deletions stl/src/filesys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,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) != 0) {
if (_Dentry.cFileName[0] != L'.'
|| (_Dentry.cFileName[1] != L'\0'
&& (_Dentry.cFileName[1] != L'.'
Expand All @@ -74,6 +69,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
Expand Down Expand Up @@ -122,13 +120,12 @@ _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
Expand Down Expand Up @@ -173,9 +170,7 @@ _FS_DLL wchar_t* __CLRCALL_PURE_OR_CDECL _Temp_get(wchar_t (&_Dest)[_MAX_FILESYS

_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;
Expand Down Expand Up @@ -226,23 +221,27 @@ _FS_DLL unsigned long long __CLRCALL_PURE_OR_CDECL _Hard_links(const wchar_t* _F
HANDLE _Handle = _FilesysOpenFile(_Fname, FILE_READ_ATTRIBUTES, FILE_FLAG_BACKUP_SEMANTICS);

#ifdef _CRT_APP
if (_Handle == INVALID_HANDLE_VALUE) {
return static_cast<unsigned long long>(-1);
}

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<unsigned long long>(-1);
// get file info
const auto _Ok = GetFileInformationByHandleEx(_Handle, FileStandardInfo, &_Info, sizeof(_Info));
CloseHandle(_Handle);
return _Ok != 0 ? _Info.NumberOfLinks : static_cast<unsigned long long>(-1);
#else // _CRT_APP
if (_Handle == INVALID_HANDLE_VALUE) {
return static_cast<unsigned long long>(-1);
}

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<unsigned long long>(-1);
// get file info
const auto _Ok = GetFileInformationByHandle(_Handle, &_Info) != 0;
CloseHandle(_Handle);
return _Ok != 0 ? _Info.nNumberOfLinks : static_cast<unsigned long long>(-1);
#endif // _CRT_APP
}

Expand Down Expand Up @@ -296,7 +295,7 @@ _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<unsigned long long>(_When) + _Win_ticks_from_epoch;
FILETIME _Ft;
_Ft.dwLowDateTime = static_cast<DWORD>(_Wtime); // intentionally discard upper bits
_Ft.dwLowDateTime = static_cast<DWORD>(_Wtime & 0xFFFFFFFFUL); // intentionally discard upper bits
_Ft.dwHighDateTime = static_cast<DWORD>(_Wtime >> 32);
int _Result = SetFileTime(_Handle, nullptr, nullptr, &_Ft);
CloseHandle(_Handle);
Expand Down Expand Up @@ -351,10 +350,10 @@ _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 _Info1.VolumeSerialNumber == _Info2.VolumeSerialNumber
&& memcmp(&_Info1.FileId, &_Info2.FileId, sizeof(_Info1.FileId)) == 0
? 1
: 0;
}
#else // _CRT_APP
BY_HANDLE_FILE_INFORMATION _Info1 = {0};
Expand All @@ -379,10 +378,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
}
Expand Down Expand Up @@ -464,14 +463,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 ? 0 : -1;
}
_FS_END
22 changes: 12 additions & 10 deletions stl/src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ 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 */)
_Symlink_file_name, _Target_file_name, _Flags | SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)
!= 0) {
return __std_win_error::_Success;
}
Expand Down Expand Up @@ -135,6 +135,8 @@ namespace {
}

_Last_error = __std_win_error{GetLastError()};

#ifndef _CRT_APP
switch (_Last_error) {
case __std_win_error::_Not_supported:
case __std_win_error::_Invalid_parameter:
Expand All @@ -143,7 +145,6 @@ 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) {
Expand Down Expand Up @@ -224,7 +225,7 @@ void __stdcall __std_fs_close_handle(const __std_fs_file_handle _Handle) noexcep
const HANDLE _As_plain_handle = reinterpret_cast<HANDLE>(_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;
}
Expand Down Expand Up @@ -702,6 +703,7 @@ _Success_(return == __std_win_error::_Success) __std_win_error
return __std_win_error::_Success;
}


[[nodiscard]] __std_win_error __stdcall __std_fs_space(_In_z_ const wchar_t* const _Target,
_Out_ uintmax_t* const _Available, _Out_ uintmax_t* const _Total_bytes,
_Out_ uintmax_t* const _Free_bytes) noexcept {
Expand Down Expand Up @@ -989,20 +991,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) != 0) {
return __std_win_error::_Success;
}
return __std_win_error::_Success;

return __std_win_error{GetLastError()};
}

_END_EXTERN_C

0 comments on commit 56d6012

Please sign in to comment.