24#elif defined(_MSC_VER) && (_MSC_VER < 1900)
25typedef __int32 int32_t;
26typedef unsigned __int32 uint32_t;
64 const vector<Node*>& nodes) {
71 bool made_change =
false;
79 for (
int i = 0; i < node_count; ++i) {
80 if (
nodes[i]->
id() < 0) {
91 deps->mtime != mtime ||
92 deps->node_count != node_count) {
95 for (
int i = 0; i < node_count; ++i) {
109 unsigned size = 4 * (1 + 2 + node_count);
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)
140 for (
int i = 0; i < node_count; ++i)
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]) {
232 for (
int i = 0; i < deps_count; ++i) {
236 total_dep_record_count++;
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) {
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) {
308 if (node->
id() < 0 || node->
id() >= (
int)
deps_.size())
314 for (
size_t id = 0;
id <
deps_.size(); ++id) {
318 for (
int i = 0; i <
deps->node_count; ++i) {
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) {
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];
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;
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)
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.
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)
const std::vector< Deps * > & deps() const
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)
const std::vector< Node * > & nodes() const
Used for tests.
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