23 #elif defined(_MSC_VER) && (_MSC_VER < 1900) 24 typedef __int32 int32_t;
25 typedef unsigned __int32 uint32_t;
52 file_ = fopen(path.c_str(),
"ab");
54 *err = strerror(errno);
64 fseek(
file_, 0, SEEK_END);
66 if (ftell(
file_) == 0) {
68 *err = strerror(errno);
72 *err = strerror(errno);
76 if (fflush(
file_) != 0) {
77 *err = strerror(errno);
84 const vector<Node*>& nodes) {
90 int node_count,
Node** nodes) {
92 bool made_change =
false;
100 for (
int i = 0; i < node_count; ++i) {
101 if (
nodes[i]->
id() < 0) {
112 deps->mtime != mtime ||
113 deps->node_count != node_count) {
116 for (
int i = 0; i < node_count; ++i) {
130 unsigned size = 4 * (1 + 2 + node_count);
136 if (fwrite(&size, 4, 1,
file_) < 1)
139 if (fwrite(&
id, 4, 1,
file_) < 1)
141 uint32_t mtime_part =
static_cast<uint32_t
>(mtime & 0xffffffff);
142 if (fwrite(&mtime_part, 4, 1,
file_) < 1)
144 mtime_part =
static_cast<uint32_t
>((mtime >> 32) & 0xffffffff);
145 if (fwrite(&mtime_part, 4, 1,
file_) < 1)
147 for (
int i = 0; i < node_count; ++i) {
149 if (fwrite(&
id, 4, 1,
file_) < 1)
152 if (fflush(
file_) != 0)
157 for (
int i = 0; i < node_count; ++i)
173 FILE* f = fopen(path.c_str(),
"rb");
177 *err = strerror(errno);
181 bool valid_header =
true;
183 if (!fgets(buf,
sizeof(buf), f) || fread(&version, 4, 1, f) < 1)
184 valid_header =
false;
192 *err =
"deps log version change; rebuilding";
194 *err =
"bad deps log signature or version; starting over";
196 unlink(path.c_str());
203 bool read_failed =
false;
204 int unique_dep_record_count = 0;
205 int total_dep_record_count = 0;
210 if (fread(&size, 4, 1, f) < 1) {
215 bool is_deps = (size >> 31) != 0;
216 size = size & 0x7FFFFFFF;
224 assert(size % 4 == 0);
225 int* deps_data =
reinterpret_cast<int*
>(buf);
226 int out_id = deps_data[0];
229 (
uint64_t)(
unsigned int)deps_data[1]);
231 int deps_count = (size / 4) - 3;
234 for (
int i = 0; i < deps_count; ++i) {
235 assert(deps_data[i] < (
int)
nodes_.size());
236 assert(
nodes_[deps_data[i]]);
240 total_dep_record_count++;
242 ++unique_dep_record_count;
244 int path_size = size - 4;
245 assert(path_size > 0);
247 if (buf[path_size - 1] ==
'\0') --path_size;
248 if (buf[path_size - 1] ==
'\0') --path_size;
249 if (buf[path_size - 1] ==
'\0') --path_size;
262 unsigned checksum = *
reinterpret_cast<unsigned*
>(buf + size - 4);
263 int expected_id = ~checksum;
265 if (
id != expected_id) {
270 assert(node->
id() < 0);
280 *err = strerror(ferror(f));
282 *err =
"premature end of file";
291 *err +=
"; recovering";
298 int kMinCompactionEntryCount = 1000;
299 int kCompactionRatio = 3;
300 if (total_dep_record_count > kMinCompactionEntryCount &&
301 total_dep_record_count > unique_dep_record_count * kCompactionRatio) {
311 if (node->
id() < 0 || node->
id() >= (int)
deps_.size())
320 string temp_path = path +
".recompact";
324 unlink(temp_path.c_str());
327 if (!new_log.OpenForWrite(temp_path, err))
332 for (vector<Node*>::iterator i =
nodes_.begin(); i !=
nodes_.end(); ++i)
336 for (
int old_id = 0; old_id < (int)
deps_.size(); ++old_id) {
343 if (!new_log.RecordDeps(
nodes_[old_id],
deps->mtime,
353 deps_.swap(new_log.deps_);
354 nodes_.swap(new_log.nodes_);
356 if (unlink(path.c_str()) < 0) {
357 *err = strerror(errno);
361 if (rename(temp_path.c_str(), path.c_str()) < 0) {
362 *err = strerror(errno);
380 if (out_id >= (
int)
deps_.size())
381 deps_.resize(out_id + 1);
383 bool delete_old =
deps_[out_id] != NULL;
385 delete deps_[out_id];
391 int path_size = node->
path().size();
392 int padding = (4 - path_size % 4) % 4;
394 unsigned size = path_size + padding + 4;
399 if (fwrite(&size, 4, 1,
file_) < 1)
401 if (fwrite(node->
path().data(), path_size, 1,
file_) < 1) {
402 assert(node->
path().size() > 0);
405 if (padding && fwrite(
"\0\0", padding, 1,
file_) < 1)
408 unsigned checksum = ~(unsigned)
id;
409 if (fwrite(&checksum, 4, 1,
file_) < 1)
411 if (fflush(
file_) != 0)
const vector< Node * > & nodes() const
Used for tests.
const int kCurrentVersion
const char kFileSignature[]
const string & path() const
vector< Deps * > deps_
Maps id -> deps of that id.
Node * GetNode(StringPiece path, uint64_t slash_bits)
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)
const vector< Deps * > & deps() const
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.
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|.
Global state (file status) for a single run.
unsigned long long uint64_t
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.