Skip to content

Commit

Permalink
Pass a FD to resise_file and add a testcase.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
espindola committed Dec 12, 2014
1 parent 7291e07 commit e1136e3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
4 changes: 2 additions & 2 deletions include/llvm/Support/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions lib/Support/Unix/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 4 additions & 12 deletions lib/Support/Windows/Path.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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<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());
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());
}

Expand Down
10 changes: 10 additions & 0 deletions unittests/Support/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e1136e3

Please sign in to comment.