From e1136e38a7c5cf84ab16e4f0d50c6a8f4bde0ff4 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 12 Dec 2014 17:55:12 +0000 Subject: [PATCH] Pass a FD to resise_file and add a testcase. I will add a real use in another commit. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224136 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/FileSystem.h | 4 ++-- lib/Support/Unix/Path.inc | 7 ++----- lib/Support/Windows/Path.inc | 16 ++++------------ unittests/Support/Path.cpp | 10 ++++++++++ 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/include/llvm/Support/FileSystem.h b/include/llvm/Support/FileSystem.h index 09561fcb0f2..2bd36c042d4 100644 --- a/include/llvm/Support/FileSystem.h +++ b/include/llvm/Support/FileSystem.h @@ -336,11 +336,11 @@ std::error_code copy_file(const Twine &From, const Twine &To); /// @brief Resize path to size. File is resized as if by POSIX truncate(). /// -/// @param path Input path. +/// @param path Input file descriptor. /// @param size Size to resize to. /// @returns errc::success if \a path has been resized to \a size, otherwise a /// platform-specific error_code. -std::error_code resize_file(const Twine &path, uint64_t size); +std::error_code resize_file(int FD, uint64_t Size); /// @} /// @name Physical Observers diff --git a/lib/Support/Unix/Path.inc b/lib/Support/Unix/Path.inc index 1e1b911b78b..75bcc03bbd3 100644 --- a/lib/Support/Unix/Path.inc +++ b/lib/Support/Unix/Path.inc @@ -286,11 +286,8 @@ std::error_code rename(const Twine &from, const Twine &to) { return std::error_code(); } -std::error_code resize_file(const Twine &path, uint64_t size) { - SmallString<128> path_storage; - StringRef p = path.toNullTerminatedStringRef(path_storage); - - if (::truncate(p.begin(), size) == -1) +std::error_code resize_file(int FD, uint64_t Size) { + if (::ftruncate(FD, Size) == -1) return std::error_code(errno, std::generic_category()); return std::error_code(); diff --git a/lib/Support/Windows/Path.inc b/lib/Support/Windows/Path.inc index 20eae54b181..05c0986d049 100644 --- a/lib/Support/Windows/Path.inc +++ b/lib/Support/Windows/Path.inc @@ -272,21 +272,13 @@ std::error_code rename(const Twine &from, const Twine &to) { return ec; } -std::error_code resize_file(const Twine &path, uint64_t size) { - SmallVector path_utf16; - - if (std::error_code ec = widenPath(path, path_utf16)) - return ec; - - int fd = ::_wopen(path_utf16.begin(), O_BINARY | _O_RDWR, S_IWRITE); - if (fd == -1) - return std::error_code(errno, std::generic_category()); +std::error_code resize_file(int FD, uint64_t Size) { #ifdef HAVE__CHSIZE_S - errno_t error = ::_chsize_s(fd, size); + errno_t error = ::_chsize_s(FD, Size); #else - errno_t error = ::_chsize(fd, size); + errno_t error = ::_chsize(FD, Size); #endif - ::close(fd); + ::close(FD); return std::error_code(error, std::generic_category()); } diff --git a/unittests/Support/Path.cpp b/unittests/Support/Path.cpp index 81d92342764..88baadeca02 100644 --- a/unittests/Support/Path.cpp +++ b/unittests/Support/Path.cpp @@ -638,6 +638,16 @@ TEST_F(FileSystemTest, CarriageReturn) { } #endif +TEST_F(FileSystemTest, Resize) { + int FD; + SmallString<64> TempPath; + ASSERT_NO_ERROR(fs::createTemporaryFile("prefix", "temp", FD, TempPath)); + ASSERT_NO_ERROR(fs::resize_file(FD, 123)); + fs::file_status Status; + ASSERT_NO_ERROR(fs::status(FD, Status)); + ASSERT_EQ(Status.getSize(), 123U); +} + TEST_F(FileSystemTest, FileMapping) { // Create a temp file. int FileDescriptor; -- 2.34.1