Windows: Fixed `os::ftruncate()` to use `FileEndOfFileInfo`. This previously used the CRT API `_chsize_s()`, which required a CRT integer file descriptor. Instead, we can achieve the same behavior by calling `SetFileInformationByHandle(FileEndOfFileInfo)`. This is significantly easier than using `SetEndOfFile()`, as that requires (1) saving the original position, (2) seeking to the new position, (3) setting the end of the file at the new position, then (4) seeking back to the original position. Instead, this method just sets the end of the file directly based on the given `length`, much like `ftruncate()` on POSIX systems.
Review: https://reviews.apache.org/r/66455 Project: http://git-wip-us.apache.org/repos/asf/mesos/repo Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/99d53e4d Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/99d53e4d Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/99d53e4d Branch: refs/heads/master Commit: 99d53e4d7522eac4f8b1ffce87ee7997771ed51a Parents: 2d22336 Author: Andrew Schwartzmeyer <[email protected]> Authored: Wed Apr 4 12:07:21 2018 -0700 Committer: Andrew Schwartzmeyer <[email protected]> Committed: Tue May 1 18:36:04 2018 -0700 ---------------------------------------------------------------------- .../stout/include/stout/os/windows/ftruncate.hpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mesos/blob/99d53e4d/3rdparty/stout/include/stout/os/windows/ftruncate.hpp ---------------------------------------------------------------------- diff --git a/3rdparty/stout/include/stout/os/windows/ftruncate.hpp b/3rdparty/stout/include/stout/os/windows/ftruncate.hpp index 1d90d2b..27eadfd 100644 --- a/3rdparty/stout/include/stout/os/windows/ftruncate.hpp +++ b/3rdparty/stout/include/stout/os/windows/ftruncate.hpp @@ -13,24 +13,22 @@ #ifndef __STOUT_OS_WINDOWS_FTRUNCATE_HPP__ #define __STOUT_OS_WINDOWS_FTRUNCATE_HPP__ -#include <io.h> - #include <stout/error.hpp> #include <stout/nothing.hpp> -#include <stout/stringify.hpp> #include <stout/try.hpp> +#include <stout/windows.hpp> #include <stout/os/int_fd.hpp> namespace os { -// Identical in functionality to POSIX standard `ftruncate`. -inline Try<Nothing> ftruncate(const int_fd& fd, __int64 length) +inline Try<Nothing> ftruncate(const int_fd& fd, off_t length) { - if (::_chsize_s(fd.crt(), length) != 0) { - return ErrnoError( - "Failed to truncate file at file descriptor '" + stringify(fd) + "' to " + - stringify(length) + " bytes."); + FILE_END_OF_FILE_INFO info; + info.EndOfFile.QuadPart = length; + if (::SetFileInformationByHandle( + fd, FileEndOfFileInfo, &info, sizeof(info)) == FALSE) { + return WindowsError(); } return Nothing();
