From c20a0325f3cc44ad6dd3eb386604e65db7e42267 Mon Sep 17 00:00:00 2001 From: "Michael J. Spencer" Date: Fri, 3 Dec 2010 17:54:07 +0000 Subject: [PATCH] Support/FileSystem: Add resize_file implementation. llvm-svn: 120819 --- llvm/lib/Support/Unix/PathV2.inc | 10 ++++++++++ llvm/lib/Support/Windows/PathV2.inc | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/llvm/lib/Support/Unix/PathV2.inc b/llvm/lib/Support/Unix/PathV2.inc index 8ecf2a4222d5..75cfce822a1f 100644 --- a/llvm/lib/Support/Unix/PathV2.inc +++ b/llvm/lib/Support/Unix/PathV2.inc @@ -213,6 +213,16 @@ error_code rename(const Twine &from, const Twine &to) { return make_error_code(errc::success); } +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) + return error_code(errno, system_category()); + + return make_error_code(errc::success); +} + error_code exists(const Twine &path, bool &result) { SmallString<128> path_storage; StringRef p = path.toNullTerminatedStringRef(path_storage); diff --git a/llvm/lib/Support/Windows/PathV2.inc b/llvm/lib/Support/Windows/PathV2.inc index c97ba5df5935..e98150713bb1 100644 --- a/llvm/lib/Support/Windows/PathV2.inc +++ b/llvm/lib/Support/Windows/PathV2.inc @@ -18,7 +18,10 @@ #include "Windows.h" #include +#include #include +#include +#include using namespace llvm; @@ -290,6 +293,22 @@ error_code rename(const Twine &from, const Twine &to) { return make_error_code(errc::success); } +error_code resize_file(const Twine &path, uint64_t size) { + SmallString<128> path_storage; + SmallVector path_utf16; + + if (error_code ec = UTF8ToUTF16(path.toStringRef(path_storage), + path_utf16)) + return ec; + + int fd = ::_wopen(path_utf16.begin(), O_BINARY, S_IREAD | S_IWRITE); + if (fd == -1) + return error_code(errno, generic_category()); + errno_t error = ::_chsize_s(fd, size); + ::close(fd); + return error_code(error, generic_category()); +} + error_code exists(const Twine &path, bool &result) { SmallString<128> path_storage; SmallVector path_utf16;