From bb10325339898d2833a9f24e9e0252b2c257beb1 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Mon, 29 Apr 2013 08:57:31 -0700 Subject: Introduce a Truncate() function that works on POSIX and Windows. Hopefully fixes the build on Windows. --- src/build_log_test.cc | 10 +--------- src/deps_log.cc | 4 +--- src/deps_log_test.cc | 4 ++-- src/util.cc | 20 ++++++++++++++++++++ src/util.h | 3 +++ 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 #ifndef _WIN32 +#include #include #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 -- cgit v0.12