Currently <filesystem> and its experimental ancestor use SetFilePointerEx + SetEndOfFile:
|
_FS_DLL int __CLRCALL_PURE_OR_CDECL _Resize(const wchar_t* _Fname, uintmax_t _Newsize) { // change file size |
|
bool _Ok = false; |
|
|
|
HANDLE _Handle = _FilesysOpenFile(_Fname, FILE_GENERIC_WRITE, 0); |
|
|
|
if (_Handle != INVALID_HANDLE_VALUE) { // set file pointer to new size and trim |
|
LARGE_INTEGER _Large; |
|
_Large.QuadPart = _Newsize; |
|
_Ok = SetFilePointerEx(_Handle, _Large, nullptr, FILE_BEGIN) != 0 && SetEndOfFile(_Handle) != 0; |
|
CloseHandle(_Handle); |
|
} |
|
return _Ok ? 0 : GetLastError(); |
|
} |
|
LARGE_INTEGER _Large; |
|
_Large.QuadPart = _New_size; |
|
if (SetFilePointerEx(_Handle._Get(), _Large, nullptr, FILE_BEGIN) == 0 || SetEndOfFile(_Handle._Get()) == 0) { |
|
return __std_win_error{GetLastError()}; |
|
} |
|
|
|
return __std_win_error::_Success; |
The modern way, which is supported starting in Windows Vista is SetFileInformationByHandle with FILE_END_OF_FILE_INFO.
It would make fewer kernel calls and cleaner code.
Probably should wait till #2766 is handled to avoid conflicts.
Currently
<filesystem>and its experimental ancestor useSetFilePointerEx+SetEndOfFile:STL/stl/src/filesys.cpp
Lines 418 to 430 in 17fde2c
STL/stl/src/filesystem.cpp
Lines 696 to 702 in 17fde2c
The modern way, which is supported starting in Windows Vista is
SetFileInformationByHandlewithFILE_END_OF_FILE_INFO.It would make fewer kernel calls and cleaner code.
Probably should wait till #2766 is handled to avoid conflicts.