From fadd5a3e8073ad08662cdf86bd1c9ab624667f4c Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 8 May 2012 08:37:27 -0700 Subject: Don't write ninja log header to log on every build on Windows. --- src/build_log.cc | 4 ++++ src/build_log_test.cc | 31 +++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/build_log.cc b/src/build_log.cc index d30625a..b743f70 100644 --- a/src/build_log.cc +++ b/src/build_log.cc @@ -66,6 +66,10 @@ bool BuildLog::OpenForWrite(const string& path, string* err) { setvbuf(log_file_, NULL, _IOLBF, BUFSIZ); SetCloseOnExec(fileno(log_file_)); + // Opening a file in append mode doesn't set the file pointer to the file's + // end on Windows. Do that explicitly. + fseek(log_file_, 0, SEEK_END); + if (ftell(log_file_) == 0) { if (fprintf(log_file_, kFileSignature, kCurrentVersion) < 0) { *err = strerror(errno); diff --git a/src/build_log_test.cc b/src/build_log_test.cc index 9b729c7..08c3083 100644 --- a/src/build_log_test.cc +++ b/src/build_log_test.cc @@ -14,6 +14,7 @@ #include "build_log.h" +#include "util.h" #include "test.h" #ifdef _WIN32 @@ -65,6 +66,36 @@ TEST_F(BuildLogTest, WriteRead) { ASSERT_EQ("out", e1->output); } +TEST_F(BuildLogTest, FirstWriteAddsSignature) { + const char kExpectedVersion[] = "# ninja log vX\n"; + const size_t kVersionPos = strlen(kExpectedVersion) - 2; // Points at 'X'. + + BuildLog log; + string contents, err; + + EXPECT_TRUE(log.OpenForWrite(kTestFilename, &err)); + ASSERT_EQ("", err); + log.Close(); + + ASSERT_EQ(0, ReadFile(kTestFilename, &contents, &err)); + ASSERT_EQ("", err); + if (contents.size() >= kVersionPos) + contents[kVersionPos] = 'X'; + EXPECT_EQ(kExpectedVersion, contents); + + // Opening the file anew shouldn't add a second version string. + EXPECT_TRUE(log.OpenForWrite(kTestFilename, &err)); + ASSERT_EQ("", err); + log.Close(); + + contents.clear(); + ASSERT_EQ(0, ReadFile(kTestFilename, &contents, &err)); + ASSERT_EQ("", err); + if (contents.size() >= kVersionPos) + contents[kVersionPos] = 'X'; + EXPECT_EQ(kExpectedVersion, contents); +} + TEST_F(BuildLogTest, DoubleEntry) { FILE* f = fopen(kTestFilename, "wb"); fprintf(f, "# ninja log v3\n"); -- cgit v0.12