diff options
Diffstat (limited to 'src/util.cc')
-rw-r--r-- | src/util.cc | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc index 91e8fad..e78dda3 100644 --- a/src/util.cc +++ b/src/util.cc @@ -29,6 +29,7 @@ #include <sys/types.h> #ifndef _WIN32 +#include <unistd.h> #include <sys/time.h> #endif @@ -354,3 +355,22 @@ string ElideMiddle(const string& str, size_t width) { } return result; } + +bool Truncate(const string& path, size_t size, string* err) { +#ifdef _WIN32 + int fh; + fh = _sopen(kTestFilename, _O_RDWR | _O_CREAT, _SH_DENYNO, + _S_IREAD | _S_IWRITE); + int success = _chsize(fh, size); + _close(fh); +#else + int success = truncate(path.c_str(), size); +#endif + // Both truncate() and _chsize() return 0 on success and set errno and return + // -1 on failure. + if (success < 0) { + *err = strerror(errno); + return false; + } + return true; +} |