1 // Copyright 2012 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
28 const char kFileSignature[] = "# ninja deps v%d\n";
29 const int kCurrentVersion = 1;
30 } // anonymous namespace
33 bool DepsLog::OpenForWrite(const string& path, string* err) {
34 file_ = fopen(path.c_str(), "ab");
36 *err = strerror(errno);
39 SetCloseOnExec(fileno(file_));
41 // Opening a file in append mode doesn't set the file pointer to the file's
42 // end on Windows. Do that explicitly.
43 fseek(file_, 0, SEEK_END);
45 if (ftell(file_) == 0) {
46 if (fprintf(file_, kFileSignature, kCurrentVersion) < 0) {
47 *err = strerror(errno);
55 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
56 const vector<Node*>& nodes) {
57 // Track whether there's any new data to be recorded.
58 bool made_change = false;
60 // Assign ids to all nodes that are missing one.
65 for (vector<Node*>::const_iterator i = nodes.begin();
66 i != nodes.end(); ++i) {
73 // See if the new data is different than the existing data, if any.
75 Deps* deps = GetDeps(node);
77 deps->mtime != mtime ||
78 deps->node_count != (int)nodes.size()) {
81 for (int i = 0; i < (int)nodes.size(); ++i) {
82 if (deps->nodes[i] != nodes[i]) {
90 // Don't write anything if there's no new info.
94 uint16_t size = 4 * (1 + 1 + (uint16_t)nodes.size());
95 size |= 0x8000; // Deps record: set high bit.
96 fwrite(&size, 2, 1, file_);
98 fwrite(&id, 4, 1, file_);
99 int timestamp = mtime;
100 fwrite(×tamp, 4, 1, file_);
101 for (vector<Node*>::const_iterator i = nodes.begin();
102 i != nodes.end(); ++i) {
104 fwrite(&id, 4, 1, file_);
110 void DepsLog::Close() {
115 bool DepsLog::Load(const string& path, State* state, string* err) {
116 METRIC_RECORD(".ninja_deps load");
118 FILE* f = fopen(path.c_str(), "rb");
122 *err = strerror(errno);
126 if (!fgets(buf, sizeof(buf), f)) {
127 *err = strerror(errno);
131 if (sscanf(buf, kFileSignature, &version) != 1) {
132 *err = "unable to read file signature";
135 if (version != kCurrentVersion) {
136 *err = "bad deps log version; starting over";
137 // Don't report this as a failure. An empty deps log will cause
138 // us to rebuild the outputs anyway.
144 if (fread(&size, 2, 1, f) < 1)
146 bool is_deps = (size >> 15) != 0;
147 size = size & 0x7FFF;
149 if (fread(buf, size, 1, f) < 1)
153 assert(size % 4 == 0);
154 int* deps_data = reinterpret_cast<int*>(buf);
155 int out_id = deps_data[0];
156 int mtime = deps_data[1];
158 int deps_count = (size / 4) - 2;
160 Deps* deps = new Deps;
162 deps->node_count = deps_count;
163 deps->nodes = new Node*[deps_count];
164 for (int i = 0; i < deps_count; ++i) {
165 assert(deps_data[i] < (int)nodes_.size());
166 assert(nodes_[deps_data[i]]);
167 deps->nodes[i] = nodes_[deps_data[i]];
170 if (out_id >= (int)deps_.size())
171 deps_.resize(out_id + 1);
173 delete deps_[out_id];
174 deps_[out_id] = deps;
176 StringPiece path(buf, size);
177 Node* node = state->GetNode(path);
178 assert(node->id() < 0);
179 node->set_id(nodes_.size());
180 nodes_.push_back(node);
184 *err = strerror(ferror(f));
191 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
194 return deps_[node->id()];
197 bool DepsLog::RecordId(Node* node) {
198 uint16_t size = (uint16_t)node->path().size();
199 fwrite(&size, 2, 1, file_);
200 fwrite(node->path().data(), node->path().size(), 1, file_);
202 node->set_id(nodes_.size());
203 nodes_.push_back(node);