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.
30 // The version is stored as 4 bytes after the signature and also serves as a
31 // byte order mark. Signature and version combined are 16 bytes long.
32 const char kFileSignature[] = "# ninjadeps\n";
33 const int kCurrentVersion = 1;
39 bool DepsLog::OpenForWrite(const string& path, string* err) {
40 file_ = fopen(path.c_str(), "ab");
42 *err = strerror(errno);
45 SetCloseOnExec(fileno(file_));
47 // Opening a file in append mode doesn't set the file pointer to the file's
48 // end on Windows. Do that explicitly.
49 fseek(file_, 0, SEEK_END);
51 if (ftell(file_) == 0) {
52 if (fwrite(kFileSignature, sizeof(kFileSignature) - 1, 1, file_) < 1) {
53 *err = strerror(errno);
56 if (fwrite(&kCurrentVersion, 4, 1, file_) < 1) {
57 *err = strerror(errno);
65 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
66 const vector<Node*>& nodes) {
67 return RecordDeps(node, mtime, nodes.size(),
68 nodes.empty() ? NULL : (Node**)&nodes.front());
71 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
72 int node_count, Node** nodes) {
73 // Track whether there's any new data to be recorded.
74 bool made_change = false;
76 // Assign ids to all nodes that are missing one.
81 for (int i = 0; i < node_count; ++i) {
82 if (nodes[i]->id() < 0) {
88 // See if the new data is different than the existing data, if any.
90 Deps* deps = GetDeps(node);
92 deps->mtime != mtime ||
93 deps->node_count != node_count) {
96 for (int i = 0; i < node_count; ++i) {
97 if (deps->nodes[i] != nodes[i]) {
105 // Don't write anything if there's no new info.
109 uint16_t size = 4 * (1 + 1 + (uint16_t)node_count);
110 size |= 0x8000; // Deps record: set high bit.
111 fwrite(&size, 2, 1, file_);
113 fwrite(&id, 4, 1, file_);
114 int timestamp = mtime;
115 fwrite(×tamp, 4, 1, file_);
116 for (int i = 0; i < node_count; ++i) {
118 fwrite(&id, 4, 1, file_);
124 void DepsLog::Close() {
130 bool DepsLog::Load(const string& path, State* state, string* err) {
131 METRIC_RECORD(".ninja_deps load");
133 FILE* f = fopen(path.c_str(), "rb");
137 *err = strerror(errno);
141 bool valid_header = true;
143 if (!fgets(buf, sizeof(buf), f) || fread(&version, 4, 1, f) < 1)
144 valid_header = false;
145 if (!valid_header || strcmp(buf, kFileSignature) != 0 ||
146 version != kCurrentVersion) {
147 *err = "bad deps log signature or version; starting over";
149 unlink(path.c_str());
150 // Don't report this as a failure. An empty deps log will cause
151 // us to rebuild the outputs anyway.
157 if (fread(&size, 2, 1, f) < 1)
159 bool is_deps = (size >> 15) != 0;
160 size = size & 0x7FFF;
162 if (fread(buf, size, 1, f) < 1)
166 assert(size % 4 == 0);
167 int* deps_data = reinterpret_cast<int*>(buf);
168 int out_id = deps_data[0];
169 int mtime = deps_data[1];
171 int deps_count = (size / 4) - 2;
173 Deps* deps = new Deps;
175 deps->node_count = deps_count;
176 deps->nodes = new Node*[deps_count];
177 for (int i = 0; i < deps_count; ++i) {
178 assert(deps_data[i] < (int)nodes_.size());
179 assert(nodes_[deps_data[i]]);
180 deps->nodes[i] = nodes_[deps_data[i]];
183 if (out_id >= (int)deps_.size())
184 deps_.resize(out_id + 1);
186 ++dead_record_count_;
187 delete deps_[out_id];
189 deps_[out_id] = deps;
191 StringPiece path(buf, size);
192 Node* node = state->GetNode(path);
193 assert(node->id() < 0);
194 node->set_id(nodes_.size());
195 nodes_.push_back(node);
199 *err = strerror(ferror(f));
206 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
209 return deps_[node->id()];
212 bool DepsLog::Recompact(const string& path, string* err) {
213 METRIC_RECORD(".ninja_deps recompact");
214 printf("Recompacting deps...\n");
216 string temp_path = path + ".recompact";
218 if (!new_log.OpenForWrite(temp_path, err))
221 // Clear all known ids so that new ones can be reassigned.
222 for (vector<Node*>::iterator i = nodes_.begin();
223 i != nodes_.end(); ++i) {
227 // Write out all deps again.
228 for (int old_id = 0; old_id < (int)deps_.size(); ++old_id) {
229 Deps* deps = deps_[old_id];
230 if (!new_log.RecordDeps(nodes_[old_id], deps->mtime,
231 deps->node_count, deps->nodes)) {
239 if (unlink(path.c_str()) < 0) {
240 *err = strerror(errno);
244 if (rename(temp_path.c_str(), path.c_str()) < 0) {
245 *err = strerror(errno);
252 bool DepsLog::RecordId(Node* node) {
253 uint16_t size = (uint16_t)node->path().size();
254 fwrite(&size, 2, 1, file_);
255 fwrite(node->path().data(), node->path().size(), 1, file_);
257 node->set_id(nodes_.size());
258 nodes_.push_back(node);