Skip to content

Commit

Permalink
Don't write ninja log header to log on every build on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
nico committed May 8, 2012
1 parent ff16370 commit fadd5a3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/build_log.cc
Expand Up @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions src/build_log_test.cc
Expand Up @@ -14,6 +14,7 @@

#include "build_log.h"

#include "util.h"
#include "test.h"

#ifdef _WIN32
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit fadd5a3

Please sign in to comment.