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
This commit is contained in:
Rafael Espindola 2014-12-12 17:55:12 +00:00
parent 7291e0706e
commit e1136e38a7
4 changed files with 18 additions and 19 deletions

View File

@ -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(). /// @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. /// @param size Size to resize to.
/// @returns errc::success if \a path has been resized to \a size, otherwise a /// @returns errc::success if \a path has been resized to \a size, otherwise a
/// platform-specific error_code. /// 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 /// @name Physical Observers

View File

@ -286,11 +286,8 @@ std::error_code rename(const Twine &from, const Twine &to) {
return std::error_code(); return std::error_code();
} }
std::error_code resize_file(const Twine &path, uint64_t size) { std::error_code resize_file(int FD, uint64_t Size) {
SmallString<128> path_storage; if (::ftruncate(FD, Size) == -1)
StringRef p = path.toNullTerminatedStringRef(path_storage);
if (::truncate(p.begin(), size) == -1)
return std::error_code(errno, std::generic_category()); return std::error_code(errno, std::generic_category());
return std::error_code(); return std::error_code();

View File

@ -272,21 +272,13 @@ std::error_code rename(const Twine &from, const Twine &to) {
return ec; return ec;
} }
std::error_code resize_file(const Twine &path, uint64_t size) { std::error_code resize_file(int FD, uint64_t Size) {
SmallVector<wchar_t, 128> 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());
#ifdef HAVE__CHSIZE_S #ifdef HAVE__CHSIZE_S
errno_t error = ::_chsize_s(fd, size); errno_t error = ::_chsize_s(FD, Size);
#else #else
errno_t error = ::_chsize(fd, size); errno_t error = ::_chsize(FD, Size);
#endif #endif
::close(fd); ::close(FD);
return std::error_code(error, std::generic_category()); return std::error_code(error, std::generic_category());
} }

View File

@ -638,6 +638,16 @@ TEST_F(FileSystemTest, CarriageReturn) {
} }
#endif #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) { TEST_F(FileSystemTest, FileMapping) {
// Create a temp file. // Create a temp file.
int FileDescriptor; int FileDescriptor;