summaryrefslogtreecommitdiffstats
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
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.
-rw-r--r--src/build_log_test.cc10
-rw-r--r--src/deps_log.cc4
-rw-r--r--src/deps_log_test.cc4
-rw-r--r--src/util.cc20
-rw-r--r--src/util.h3
5 files changed, 27 insertions, 14 deletions
diff --git a/src/build_log_test.cc b/src/build_log_test.cc
index 2dd6500..4639bc9 100644
--- a/src/build_log_test.cc
+++ b/src/build_log_test.cc
@@ -143,15 +143,7 @@ TEST_F(BuildLogTest, Truncate) {
log2.RecordCommand(state_.edges_[1], 20, 25);
log2.Close();
-#ifndef _WIN32
- ASSERT_EQ(0, truncate(kTestFilename, size));
-#else
- int fh;
- fh = _sopen(kTestFilename, _O_RDWR | _O_CREAT, _SH_DENYNO,
- _S_IREAD | _S_IWRITE);
- ASSERT_EQ(0, _chsize(fh, size));
- _close(fh);
-#endif
+ ASSERT_TRUE(Truncate(kTestFilename, size, &err));
BuildLog log3;
err.clear();
diff --git a/src/deps_log.cc b/src/deps_log.cc
index c52503b..9866540 100644
--- a/src/deps_log.cc
+++ b/src/deps_log.cc
@@ -215,10 +215,8 @@ bool DepsLog::Load(const string& path, State* state, string* err) {
}
fclose(f);
- if (truncate(path.c_str(), offset) < 0) {
- *err = strerror(errno);
+ if (!Truncate(path.c_str(), offset, err))
return false;
- }
// The truncate succeeded; we'll just report the load error as a
// warning because the build can proceed.
diff --git a/src/deps_log_test.cc b/src/deps_log_test.cc
index 9623d17..b3d6b74 100644
--- a/src/deps_log_test.cc
+++ b/src/deps_log_test.cc
@@ -251,11 +251,11 @@ TEST_F(DepsLogTest, Truncated) {
int node_count = 5;
int deps_count = 2;
for (int size = (int)st.st_size; size > 0; --size) {
- ASSERT_EQ(0, truncate(kTestFilename, size));
+ string err;
+ ASSERT_TRUE(Truncate(kTestFilename, size, &err));
State state;
DepsLog log;
- string err;
EXPECT_TRUE(log.Load(kTestFilename, &state, &err));
if (!err.empty()) {
// At some point the log will be so short as to be unparseable.
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;
+}
diff --git a/src/util.h b/src/util.h
index 07381a1..9740565 100644
--- a/src/util.h
+++ b/src/util.h
@@ -76,6 +76,9 @@ double GetLoadAverage();
/// exceeds @a width.
string ElideMiddle(const string& str, size_t width);
+/// Truncates a file to the given size.
+bool Truncate(const string& path, size_t size, string* err);
+
#ifdef _MSC_VER
#define snprintf _snprintf
#define fileno _fileno