24 #elif defined(_MSC_VER) && (_MSC_VER < 1900)
25 typedef __int32 int32_t;
26 typedef unsigned __int32 uint32_t;
52 if (needs_recompaction_) {
53 if (!Recompact(path, err))
64 const vector<Node*>& nodes) {
65 return RecordDeps(node, mtime,
static_cast<int>(nodes.size()), nodes.data());
71 bool made_change =
false;
79 for (
int i = 0; i < node_count; ++i) {
80 if (nodes[i]->
id() < 0) {
81 if (!RecordId(nodes[i]))
89 Deps* deps = GetDeps(node);
91 deps->
mtime != mtime ||
95 for (
int i = 0; i < node_count; ++i) {
96 if (deps->
nodes[i] != nodes[i]) {
109 unsigned size = 4 * (1 + 2 + node_count);
115 if (!OpenForWriteIfNeeded()) {
119 if (fwrite(&size, 4, 1, file_) < 1)
122 if (fwrite(&
id, 4, 1, file_) < 1)
124 uint32_t mtime_part =
static_cast<uint32_t
>(mtime & 0xffffffff);
125 if (fwrite(&mtime_part, 4, 1, file_) < 1)
127 mtime_part =
static_cast<uint32_t
>((mtime >> 32) & 0xffffffff);
128 if (fwrite(&mtime_part, 4, 1, file_) < 1)
130 for (
int i = 0; i < node_count; ++i) {
132 if (fwrite(&
id, 4, 1, file_) < 1)
135 if (fflush(file_) != 0)
139 Deps* deps =
new Deps(mtime, node_count);
140 for (
int i = 0; i < node_count; ++i)
141 deps->
nodes[i] = nodes[i];
142 UpdateDeps(node->
id(), deps);
148 OpenForWriteIfNeeded();
157 FILE* f = fopen(path.c_str(),
"rb");
161 *err = strerror(errno);
176 if (!valid_header || !valid_version) {
178 *err =
"deps log version change; rebuilding";
180 *err =
"bad deps log signature or version; starting over";
188 long offset = ftell(f);
189 bool read_failed =
false;
190 int unique_dep_record_count = 0;
191 int total_dep_record_count = 0;
194 if (fread(&size,
sizeof(size), 1, f) < 1) {
199 bool is_deps = (size >> 31) != 0;
200 size = size & 0x7FFFFFFF;
206 offset += size +
sizeof(size);
209 if ((size % 4) != 0) {
213 int* deps_data =
reinterpret_cast<int*
>(buf);
214 int out_id = deps_data[0];
217 (
uint64_t)(
unsigned int)deps_data[1]);
219 int deps_count = (size / 4) - 3;
221 for (
int i = 0; i < deps_count; ++i) {
222 int node_id = deps_data[i];
223 if (node_id >= (
int)nodes_.size() || !nodes_[node_id]) {
231 Deps* deps =
new Deps(mtime, deps_count);
232 for (
int i = 0; i < deps_count; ++i) {
233 deps->
nodes[i] = nodes_[deps_data[i]];
236 total_dep_record_count++;
237 if (!UpdateDeps(out_id, deps))
238 ++unique_dep_record_count;
240 int path_size = size - 4;
241 if (path_size <= 0) {
246 if (buf[path_size - 1] ==
'\0') --path_size;
247 if (buf[path_size - 1] ==
'\0') --path_size;
248 if (buf[path_size - 1] ==
'\0') --path_size;
261 unsigned checksum = *
reinterpret_cast<unsigned*
>(buf + size - 4);
262 int expected_id = ~checksum;
263 int id =
static_cast<int>(nodes_.size());
264 if (
id != expected_id || node->
id() >= 0) {
269 nodes_.push_back(node);
277 *err = strerror(ferror(f));
279 *err =
"premature end of file";
288 *err +=
"; recovering";
295 int kMinCompactionEntryCount = 1000;
296 int kCompactionRatio = 3;
297 if (total_dep_record_count > kMinCompactionEntryCount &&
298 total_dep_record_count > unique_dep_record_count * kCompactionRatio) {
299 needs_recompaction_ =
true;
308 if (node->
id() < 0 || node->
id() >= (
int)deps_.size())
310 return deps_[node->
id()];
314 for (
size_t id = 0;
id < deps_.size(); ++id) {
315 Deps* deps = deps_[id];
319 if (deps->
nodes[i] == node)
330 string temp_path = path +
".recompact";
342 for (vector<Node*>::iterator i = nodes_.begin(); i != nodes_.end(); ++i)
346 for (
int old_id = 0; old_id < (int)deps_.size(); ++old_id) {
347 Deps* deps = deps_[old_id];
350 if (!IsDepsEntryLiveFor(nodes_[old_id]))
363 deps_.swap(new_log.
deps_);
364 nodes_.swap(new_log.
nodes_);
367 *err = strerror(errno);
371 if (rename(temp_path.c_str(), path.c_str()) < 0) {
372 *err = strerror(errno);
390 if (out_id >= (
int)deps_.size())
391 deps_.resize(out_id + 1);
393 bool delete_old = deps_[out_id] != NULL;
395 delete deps_[out_id];
396 deps_[out_id] = deps;
401 int path_size =
static_cast<int>(node->
path().size());
402 assert(path_size > 0 &&
"Trying to record empty path Node!");
403 int padding = (4 - path_size % 4) % 4;
405 unsigned size = path_size + padding + 4;
411 if (!OpenForWriteIfNeeded()) {
414 if (fwrite(&size, 4, 1, file_) < 1)
416 if (fwrite(node->
path().data(), path_size, 1, file_) < 1) {
419 if (padding && fwrite(
"\0\0", padding, 1, file_) < 1)
421 int id =
static_cast<int>(nodes_.size());
422 unsigned checksum = ~(unsigned)
id;
423 if (fwrite(&checksum, 4, 1, file_) < 1)
425 if (fflush(file_) != 0)
429 nodes_.push_back(node);
435 if (file_path_.empty()) {
438 file_ = fopen(file_path_.c_str(),
"ab");
451 fseek(file_, 0, SEEK_END);
453 if (ftell(file_) == 0) {
461 if (fflush(file_) != 0) {
static const size_t kFileSignatureSize
static const int32_t kCurrentVersion
static constexpr size_t kMaxRecordSize
static const char kFileSignature[]
#define METRIC_RECORD(name)
The primary interface to metrics.
As build commands run they can output extra dependency information (e.g.
Deps * GetDeps(Node *node)
bool UpdateDeps(int out_id, Deps *deps)
Node * GetFirstReverseDepsNode(Node *node)
bool OpenForWriteIfNeeded()
Should be called before using file_.
bool Recompact(const std::string &path, std::string *err)
Rewrite the known log entries, throwing away old data.
bool OpenForWrite(const std::string &path, std::string *err)
static bool IsDepsEntryLiveFor(const Node *node)
Returns if the deps entry for a node is still reachable from the manifest.
std::vector< Node * > nodes_
Maps id -> Node.
bool RecordId(Node *node)
bool RecordDeps(Node *node, TimeStamp mtime, const std::vector< Node * > &nodes)
std::vector< Deps * > deps_
Maps id -> deps of that id.
LoadStatus Load(const std::string &path, State *state, std::string *err)
std::string GetBinding(const std::string &key) const
Returns the shell-escaped value of |key|.
Information about a node in the dependency graph: the file, whether it's dirty, mtime,...
const std::string & path() const
Global state (file status) for a single run.
Node * GetNode(StringPiece path, uint64_t slash_bits)
StringPiece represents a slice of a string whose memory is managed externally.
int platformAwareUnlink(const char *filename)
bool Truncate(const string &path, size_t size, string *err)
void SetCloseOnExec(int fd)
Mark a file descriptor to not be inherited on exec()s.
unsigned long long uint64_t