Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions stl/src/filesys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,12 @@ static wchar_t* _Strcpy(wchar_t (&_Dest)[_MAX_FILESYS_NAME], const wchar_t* _Src
}

static HANDLE _FilesysOpenFile(const wchar_t* _Fname, DWORD _Desired_access, DWORD _Flags) {
#ifdef _CRT_APP
CREATEFILE2_EXTENDED_PARAMETERS _Create_file_parameters = {};
_Create_file_parameters.dwSize = sizeof(_Create_file_parameters);
_Create_file_parameters.dwFileFlags = _Flags;

return CreateFile2(_Fname, _Desired_access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, OPEN_EXISTING,
&_Create_file_parameters);
#else // ^^^ defined(_CRT_APP) / !defined(_CRT_APP) vvv
return CreateFileW(_Fname, _Desired_access, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr,
OPEN_EXISTING, _Flags, nullptr);
#endif // ^^^ !defined(_CRT_APP) ^^^
}

_FS_DLL wchar_t* __CLRCALL_PURE_OR_CDECL _Read_dir(
Expand Down Expand Up @@ -228,21 +223,12 @@ _FS_DLL uintmax_t __CLRCALL_PURE_OR_CDECL _Hard_links(const wchar_t* _Fname) noe
return static_cast<uintmax_t>(-1);
}

#ifdef _CRT_APP
FILE_STANDARD_INFO _Info = {0};

// get file info
const auto _Ok = GetFileInformationByHandleEx(_Handle, FileStandardInfo, &_Info, sizeof(_Info));
CloseHandle(_Handle);
return _Ok ? _Info.NumberOfLinks : static_cast<uintmax_t>(-1);
#else // ^^^ defined(_CRT_APP) / !defined(_CRT_APP) vvv
BY_HANDLE_FILE_INFORMATION _Info = {0};

// get file info
const auto _Ok = GetFileInformationByHandle(_Handle, &_Info);
CloseHandle(_Handle);
return _Ok ? _Info.nNumberOfLinks : static_cast<uintmax_t>(-1);
#endif // ^^^ !defined(_CRT_APP) ^^^
}

_FS_DLL uintmax_t __CLRCALL_PURE_OR_CDECL _File_size(const wchar_t* _Fname) noexcept { // get file size
Expand Down Expand Up @@ -321,7 +307,6 @@ _FS_DLL space_info __CLRCALL_PURE_OR_CDECL _Statvfs(const wchar_t* _Fname) noexc
_FS_DLL int __CLRCALL_PURE_OR_CDECL _Equivalent(
const wchar_t* _Fname1, const wchar_t* _Fname2) noexcept { // test for equivalent file names
// See GH-3571: File IDs are only guaranteed to be unique and stable while handles remain open
#ifdef _CRT_APP
_FILE_ID_INFO _Info1 = {0};
_FILE_ID_INFO _Info2 = {0};
bool _Ok1 = false;
Expand Down Expand Up @@ -349,38 +334,6 @@ _FS_DLL int __CLRCALL_PURE_OR_CDECL _Equivalent(
} else { // test existing files for equivalence
return memcmp(&_Info1, &_Info2, sizeof(_FILE_ID_INFO)) == 0 ? 1 : 0;
}
#else // ^^^ defined(_CRT_APP) / !defined(_CRT_APP) vvv
BY_HANDLE_FILE_INFORMATION _Info1 = {0};
BY_HANDLE_FILE_INFORMATION _Info2 = {0};
bool _Ok1 = false;
bool _Ok2 = false;

HANDLE _Handle1 = _FilesysOpenFile(_Fname1, FILE_READ_ATTRIBUTES, FILE_FLAG_BACKUP_SEMANTICS);
if (_Handle1 != INVALID_HANDLE_VALUE) { // get file1 info
_Ok1 = GetFileInformationByHandle(_Handle1, &_Info1) != 0;
}

HANDLE _Handle2 = _FilesysOpenFile(_Fname2, FILE_READ_ATTRIBUTES, FILE_FLAG_BACKUP_SEMANTICS);
if (_Handle2 != INVALID_HANDLE_VALUE) { // get file2 info
_Ok2 = GetFileInformationByHandle(_Handle2, &_Info2) != 0;
CloseHandle(_Handle2);
}

if (_Handle1 != INVALID_HANDLE_VALUE) {
CloseHandle(_Handle1);
}

if (!_Ok1 && !_Ok2) {
return -1;
} 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
? 1
: 0;
}
#endif // ^^^ !defined(_CRT_APP) ^^^
}

_FS_DLL int __CLRCALL_PURE_OR_CDECL _Link(const wchar_t* _Fname1, const wchar_t* _Fname2) noexcept {
Expand Down Expand Up @@ -433,7 +386,6 @@ _FS_DLL int __CLRCALL_PURE_OR_CDECL _Unlink(const wchar_t* _Fname) noexcept { //

_FS_DLL int __CLRCALL_PURE_OR_CDECL _Copy_file(const wchar_t* _Fname1, const wchar_t* _Fname2) noexcept {
// copy _Fname1 to _Fname2
#if defined(_ONECORE)
COPYFILE2_EXTENDED_PARAMETERS _Params = {0};
_Params.dwSize = sizeof(COPYFILE2_EXTENDED_PARAMETERS);
_Params.dwCopyFlags = 0;
Expand All @@ -445,9 +397,6 @@ _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) / !defined(_ONECORE) vvv
return CopyFileW(_Fname1, _Fname2, 0) ? 0 : GetLastError();
#endif // ^^^ !defined(_ONECORE) ^^^
}

_FS_DLL int __CLRCALL_PURE_OR_CDECL _Chmod(const wchar_t* _Fname, perms _Newmode) noexcept {
Expand Down
40 changes: 2 additions & 38 deletions stl/src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ namespace {
#define __vcrt_CreateSymbolicLinkW CreateSymbolicLinkW
#endif // ^^^ !defined(_CRT_APP) ^^^

#ifdef _CRT_APP
HANDLE __stdcall __vcp_CreateFile(const wchar_t* const _File_name, const unsigned long _Desired_access,
const unsigned long _Share, SECURITY_ATTRIBUTES* const _Security_attributes,
const unsigned long _Creation_disposition, const unsigned long _Flags_and_attributes,
Expand All @@ -46,9 +45,6 @@ namespace {
_Create_file_parameters.hTemplateFile = _Template_file;
return CreateFile2(_File_name, _Desired_access, _Share, _Creation_disposition, &_Create_file_parameters);
}
#else // ^^^ defined(_CRT_APP) / !defined(_CRT_APP) vvv
#define __vcp_CreateFile CreateFileW
#endif // ^^^ !defined(_CRT_APP) ^^^

[[nodiscard]] __std_win_error __stdcall _Translate_CreateFile_last_error(const HANDLE _Handle) {
if (_Handle != INVALID_HANDLE_VALUE) {
Expand All @@ -60,7 +56,6 @@ namespace {

[[nodiscard]] __std_fs_copy_file_result __stdcall __vcp_Copyfile(
const wchar_t* const _Source, const wchar_t* const _Target, const bool _Fail_if_exists) noexcept {
#if defined(_CRT_APP)
COPYFILE2_EXTENDED_PARAMETERS _Params{};
_Params.dwSize = sizeof(_Params);
_Params.dwCopyFlags = _Fail_if_exists ? COPY_FILE_FAIL_IF_EXISTS : 0;
Expand All @@ -72,13 +67,6 @@ namespace {

// take lower bits to undo HRESULT_FROM_WIN32
return {false, __std_win_error{_Copy_result & 0x0000FFFFU}};
#else // ^^^ defined(_CRT_APP) / !defined(_CRT_APP) vvv
if (CopyFileW(_Source, _Target, _Fail_if_exists)) {
return {true, __std_win_error::_Success};
}

return {false, __std_win_error{GetLastError()}};
#endif // defined(_CRT_APP)
}

[[nodiscard]] __std_win_error __stdcall _Create_symlink(
Expand Down Expand Up @@ -127,31 +115,7 @@ namespace {
return __std_win_error::_Success;
}

__std_win_error _Last_error{GetLastError()};

#ifndef _CRT_APP
switch (_Last_error) {
case __std_win_error::_Not_supported:
case __std_win_error::_Invalid_parameter:
break; // try more things
default:
return _Last_error; // real error, bail to the caller
}

// try GetFileInformationByHandle as a fallback
BY_HANDLE_FILE_INFORMATION _Info;
if (GetFileInformationByHandle(_Handle, &_Info)) {
_Id->VolumeSerialNumber = _Info.dwVolumeSerialNumber;
_CSTD memcpy(&_Id->FileId.Identifier[0], &_Info.nFileIndexHigh, 4);
_CSTD memcpy(&_Id->FileId.Identifier[4], &_Info.nFileIndexLow, 4);
_CSTD memset(&_Id->FileId.Identifier[8], 0, 8);
return __std_win_error::_Success;
}

_Last_error = __std_win_error{GetLastError()};
#endif // !defined(_CRT_APP)

return _Last_error;
return __std_win_error{GetLastError()};
}

[[nodiscard]] _Success_(return == __std_win_error::_Success) __std_win_error
Expand Down Expand Up @@ -401,7 +365,7 @@ void __stdcall __std_fs_directory_iterator_close(_In_ const __std_fs_dir_handle
}

if (!_Do_copy) {
// We only need to test `equivalent()` if we _aren't_ going to `CopyFileW()`,
// We only need to test `equivalent()` if we _aren't_ going to `CopyFile2()`,
// since that call will fail with an `ERROR_SHARING_VIOLATION` anyways.
FILE_ID_INFO _Source_id;
_Last_error = _Get_file_id_by_handle(_Source_handle._Get(), &_Source_id);
Expand Down