Skip to content

Commit

Permalink
Merge pull request ninja-build#596 from nico/chunk
Browse files Browse the repository at this point in the history
Make sure to not write partial deps entries.
  • Loading branch information
evmar committed Jun 14, 2013
2 parents 75918b8 + 13bad8d commit f4bd670
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/deps_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
const char kFileSignature[] = "# ninjadeps\n";
const int kCurrentVersion = 1;

// Since the size field is 2 bytes and the top bit marks deps entries, a single
// record can be at most 32 kB. Set the buffer size to this and flush the file
// buffer after every record to make sure records aren't written partially.
const int kMaxBufferSize = 1 << 15;

DepsLog::~DepsLog() {
Close();
}
Expand All @@ -48,6 +53,7 @@ bool DepsLog::OpenForWrite(const string& path, string* err) {
*err = strerror(errno);
return false;
}
setvbuf(file_, NULL, _IOFBF, kMaxBufferSize);
SetCloseOnExec(fileno(file_));

// Opening a file in append mode doesn't set the file pointer to the file's
Expand All @@ -64,6 +70,7 @@ bool DepsLog::OpenForWrite(const string& path, string* err) {
return false;
}
}
fflush(file_);

return true;
}
Expand Down Expand Up @@ -124,6 +131,7 @@ bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
id = nodes[i]->id();
fwrite(&id, 4, 1, file_);
}
fflush(file_);

// Update in-memory representation.
Deps* deps = new Deps(mtime, node_count);
Expand Down Expand Up @@ -318,6 +326,7 @@ bool DepsLog::RecordId(Node* node) {
uint16_t size = (uint16_t)node->path().size();
fwrite(&size, 2, 1, file_);
fwrite(node->path().data(), node->path().size(), 1, file_);
fflush(file_);

node->set_id(nodes_.size());
nodes_.push_back(node);
Expand Down

0 comments on commit f4bd670

Please sign in to comment.