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 return RecordDeps(node, mtime, nodes.size(), (Node**)&nodes.front());
60 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
61 int node_count, Node** nodes) {
62 // Track whether there's any new data to be recorded.
63 bool made_change = false;
65 // Assign ids to all nodes that are missing one.
70 for (int i = 0; i < node_count; ++i) {
71 if (nodes[i]->id() < 0) {
77 // See if the new data is different than the existing data, if any.
79 Deps* deps = GetDeps(node);
81 deps->mtime != mtime ||
82 deps->node_count != node_count) {
85 for (int i = 0; i < node_count; ++i) {
86 if (deps->nodes[i] != nodes[i]) {
94 // Don't write anything if there's no new info.
98 uint16_t size = 4 * (1 + 1 + (uint16_t)node_count);
99 size |= 0x8000; // Deps record: set high bit.
100 fwrite(&size, 2, 1, file_);
102 fwrite(&id, 4, 1, file_);
103 int timestamp = mtime;
104 fwrite(×tamp, 4, 1, file_);
105 for (int i = 0; i < node_count; ++i) {
107 fwrite(&id, 4, 1, file_);
113 void DepsLog::Close() {
118 bool DepsLog::Load(const string& path, State* state, string* err) {
119 METRIC_RECORD(".ninja_deps load");
121 FILE* f = fopen(path.c_str(), "rb");
125 *err = strerror(errno);
129 if (!fgets(buf, sizeof(buf), f)) {
130 *err = strerror(errno);
134 sscanf(buf, kFileSignature, &version);
135 if (version != kCurrentVersion) {
136 *err = "bad deps log signature or version; starting over";
138 unlink(path.c_str());
139 // Don't report this as a failure. An empty deps log will cause
140 // us to rebuild the outputs anyway.
146 if (fread(&size, 2, 1, f) < 1)
148 bool is_deps = (size >> 15) != 0;
149 size = size & 0x7FFF;
151 if (fread(buf, size, 1, f) < 1)
155 assert(size % 4 == 0);
156 int* deps_data = reinterpret_cast<int*>(buf);
157 int out_id = deps_data[0];
158 int mtime = deps_data[1];
160 int deps_count = (size / 4) - 2;
162 Deps* deps = new Deps;
164 deps->node_count = deps_count;
165 deps->nodes = new Node*[deps_count];
166 for (int i = 0; i < deps_count; ++i) {
167 assert(deps_data[i] < (int)nodes_.size());
168 assert(nodes_[deps_data[i]]);
169 deps->nodes[i] = nodes_[deps_data[i]];
172 if (out_id >= (int)deps_.size())
173 deps_.resize(out_id + 1);
175 ++dead_record_count_;
176 delete deps_[out_id];
178 deps_[out_id] = deps;
180 StringPiece path(buf, size);
181 Node* node = state->GetNode(path);
182 assert(node->id() < 0);
183 node->set_id(nodes_.size());
184 nodes_.push_back(node);
188 *err = strerror(ferror(f));
195 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
198 return deps_[node->id()];
201 bool DepsLog::Recompact(const string& path, string* err) {
202 METRIC_RECORD(".ninja_deps recompact");
203 printf("Recompacting deps...\n");
205 string temp_path = path + ".recompact";
207 if (!new_log.OpenForWrite(temp_path, err))
210 // Clear all known ids so that new ones can be reassigned.
211 for (vector<Node*>::iterator i = nodes_.begin();
212 i != nodes_.end(); ++i) {
216 // Write out all deps again.
217 for (int old_id = 0; old_id < (int)deps_.size(); ++old_id) {
218 Deps* deps = deps_[old_id];
219 if (!new_log.RecordDeps(nodes_[old_id], deps->mtime,
220 deps->node_count, deps->nodes)) {
228 if (unlink(path.c_str()) < 0) {
229 *err = strerror(errno);
233 if (rename(temp_path.c_str(), path.c_str()) < 0) {
234 *err = strerror(errno);
241 bool DepsLog::RecordId(Node* node) {
242 uint16_t size = (uint16_t)node->path().size();
243 fwrite(&size, 2, 1, file_);
244 fwrite(node->path().data(), node->path().size(), 1, file_);
246 node->set_id(nodes_.size());
247 nodes_.push_back(node);