49 file_ = fopen(path.c_str(),
"ab");
51 *err = strerror(errno);
61 fseek(
file_, 0, SEEK_END);
63 if (ftell(
file_) == 0) {
65 *err = strerror(errno);
69 *err = strerror(errno);
73 if (fflush(
file_) != 0) {
74 *err = strerror(errno);
81 const vector<Node*>&
nodes) {
83 nodes.empty() ? NULL : (
Node**)&nodes.front());
89 bool made_change =
false;
97 for (
int i = 0; i < node_count; ++i) {
98 if (nodes[i]->
id() < 0) {
109 deps->
mtime != mtime ||
113 for (
int i = 0; i < node_count; ++i) {
114 if (deps->
nodes[i] != nodes[i]) {
127 unsigned size = 4 * (1 + 1 + node_count);
133 if (fwrite(&size, 4, 1,
file_) < 1)
136 if (fwrite(&
id, 4, 1,
file_) < 1)
138 int timestamp = mtime;
139 if (fwrite(×tamp, 4, 1,
file_) < 1)
141 for (
int i = 0; i < node_count; ++i) {
143 if (fwrite(&
id, 4, 1,
file_) < 1)
146 if (fflush(
file_) != 0)
151 for (
int i = 0; i < node_count; ++i)
152 deps->
nodes[i] = nodes[i];
167 FILE* f = fopen(path.c_str(),
"rb");
171 *err = strerror(errno);
175 bool valid_header =
true;
177 if (!fgets(buf,
sizeof(buf), f) || fread(&version, 4, 1, f) < 1)
178 valid_header =
false;
186 *err =
"deps log version change; rebuilding";
188 *err =
"bad deps log signature or version; starting over";
190 unlink(path.c_str());
197 bool read_failed =
false;
198 int unique_dep_record_count = 0;
199 int total_dep_record_count = 0;
204 if (fread(&size, 4, 1, f) < 1) {
209 bool is_deps = (size >> 31) != 0;
210 size = size & 0x7FFFFFFF;
218 assert(size % 4 == 0);
219 int* deps_data =
reinterpret_cast<int*
>(buf);
220 int out_id = deps_data[0];
221 int mtime = deps_data[1];
223 int deps_count = (size / 4) - 2;
226 for (
int i = 0; i < deps_count; ++i) {
227 assert(deps_data[i] < (
int)
nodes_.size());
228 assert(
nodes_[deps_data[i]]);
232 total_dep_record_count++;
234 ++unique_dep_record_count;
236 int path_size = size - 4;
237 assert(path_size > 0);
239 if (buf[path_size - 1] ==
'\0') --path_size;
240 if (buf[path_size - 1] ==
'\0') --path_size;
241 if (buf[path_size - 1] ==
'\0') --path_size;
249 unsigned checksum = *
reinterpret_cast<unsigned*
>(buf + size - 4);
250 int expected_id = ~checksum;
252 if (
id != expected_id) {
257 assert(node->
id() < 0);
267 *err = strerror(ferror(f));
269 *err =
"premature end of file";
273 if (!
Truncate(path.c_str(), offset, err))
278 *err +=
"; recovering";
285 int kMinCompactionEntryCount = 1000;
286 int kCompactionRatio = 3;
287 if (total_dep_record_count > kMinCompactionEntryCount &&
288 total_dep_record_count > unique_dep_record_count * kCompactionRatio) {
298 if (node->
id() < 0 || node->
id() >= (int)
deps_.size())
305 printf(
"Recompacting deps...\n");
308 string temp_path = path +
".recompact";
312 unlink(temp_path.c_str());
315 if (!new_log.OpenForWrite(temp_path, err))
320 for (vector<Node*>::iterator i =
nodes_.begin(); i !=
nodes_.end(); ++i)
324 for (
int old_id = 0; old_id < (int)
deps_.size(); ++old_id) {
331 if (!new_log.RecordDeps(
nodes_[old_id], deps->
mtime,
341 deps_.swap(new_log.deps_);
342 nodes_.swap(new_log.nodes_);
344 if (unlink(path.c_str()) < 0) {
345 *err = strerror(errno);
349 if (rename(temp_path.c_str(), path.c_str()) < 0) {
350 *err = strerror(errno);
368 if (out_id >= (
int)
deps_.size())
369 deps_.resize(out_id + 1);
371 bool delete_old =
deps_[out_id] != NULL;
373 delete deps_[out_id];
379 int path_size = node->
path().size();
380 int padding = (4 - path_size % 4) % 4;
382 unsigned size = path_size + padding + 4;
387 if (fwrite(&size, 4, 1,
file_) < 1)
389 if (fwrite(node->
path().data(), path_size, 1,
file_) < 1) {
390 assert(node->
path().size() > 0);
393 if (padding && fwrite(
"\0\0", padding, 1,
file_) < 1)
396 unsigned checksum = ~(unsigned)
id;
397 if (fwrite(&checksum, 4, 1,
file_) < 1)
399 if (fflush(
file_) != 0)
const int kCurrentVersion
const char kFileSignature[]
vector< Deps * > deps_
Maps id -> deps of that id.
Node * GetNode(StringPiece path)
StringPiece represents a slice of a string whose memory is managed externally.
Information about a node in the dependency graph: the file, whether it's dirty, mtime, etc.
const unsigned kMaxRecordSize
void SetCloseOnExec(int fd)
Mark a file descriptor to not be inherited on exec()s.
As build commands run they can output extra dependency information (e.g.
vector< Node * > nodes_
Maps id -> Node.
bool OpenForWrite(const string &path, string *err)
Deps * GetDeps(Node *node)
bool Load(const string &path, State *state, string *err)
bool Recompact(const string &path, string *err)
Rewrite the known log entries, throwing away old data.
bool RecordId(Node *node)
#define METRIC_RECORD(name)
The primary interface to metrics.
const string & path() const
bool Truncate(const string &path, size_t size, string *err)
Truncates a file to the given size.
string GetBinding(const string &key)
Returns the shell-escaped value of |key|.
const vector< Deps * > & deps() const
Global state (file status, loaded rules) for a single run.
const vector< Node * > & nodes() const
Used for tests.
bool RecordDeps(Node *node, TimeStamp mtime, const vector< Node * > &nodes)
bool UpdateDeps(int out_id, Deps *deps)
bool IsDepsEntryLiveFor(Node *node)
Returns if the deps entry for a node is still reachable from the manifest.