summaryrefslogtreecommitdiffstats
path: root/src/util.cc
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2013-04-29 15:57:31 (GMT)
committerNico Weber <nicolasweber@gmx.de>2013-04-29 16:01:55 (GMT)
commitbb10325339898d2833a9f24e9e0252b2c257beb1 (patch)
tree7f78186d78e2e060a26ece346129aba9015d3515 /src/util.cc
parent3699cb6aa2fb29125bf725cb50fbcc593da8a8a1 (diff)
downloadNinja-bb10325339898d2833a9f24e9e0252b2c257beb1.zip
Ninja-bb10325339898d2833a9f24e9e0252b2c257beb1.tar.gz
Ninja-bb10325339898d2833a9f24e9e0252b2c257beb1.tar.bz2
Introduce a Truncate() function that works on POSIX and Windows.
Hopefully fixes the build on Windows.
Diffstat (limited to 'src/util.cc')
-rw-r--r--src/util.cc20
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;
+}