Skip to content

<filesystem>: Resize files in a better way #2767

Description

Currently <filesystem> and its experimental ancestor use SetFilePointerEx + SetEndOfFile:

STL/stl/src/filesys.cpp

Lines 418 to 430 in 17fde2c

_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();
}

STL/stl/src/filesystem.cpp

Lines 696 to 702 in 17fde2c

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    filesystemC++17 filesystemfixedSomething works now, yay!performanceMust go faster

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions