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 // The version is stored as 4 bytes after the signature and also serves as a
29 // byte order mark. Signature and version combined are 16 bytes long.
30 const char kFileSignature[] = "# ninjadeps\n";
31 const int kCurrentVersion = 1;
37 bool DepsLog::OpenForWrite(const string& path, string* err) {
38 file_ = fopen(path.c_str(), "ab");
40 *err = strerror(errno);
43 SetCloseOnExec(fileno(file_));
45 // Opening a file in append mode doesn't set the file pointer to the file's
46 // end on Windows. Do that explicitly.
47 fseek(file_, 0, SEEK_END);
49 if (ftell(file_) == 0) {
50 if (fwrite(kFileSignature, sizeof(kFileSignature) - 1, 1, file_) < 1) {
51 *err = strerror(errno);
54 if (fwrite(&kCurrentVersion, 4, 1, file_) < 1) {
55 *err = strerror(errno);
63 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
64 const vector<Node*>& nodes) {
65 return RecordDeps(node, mtime, nodes.size(),
66 nodes.empty() ? NULL : (Node**)&nodes.front());
69 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
70 int node_count, Node** nodes) {
71 // Track whether there's any new data to be recorded.
72 bool made_change = false;
74 // Assign ids to all nodes that are missing one.
79 for (int i = 0; i < node_count; ++i) {
80 if (nodes[i]->id() < 0) {
86 // See if the new data is different than the existing data, if any.
88 Deps* deps = GetDeps(node);
90 deps->mtime != mtime ||
91 deps->node_count != node_count) {
94 for (int i = 0; i < node_count; ++i) {
95 if (deps->nodes[i] != nodes[i]) {
103 // Don't write anything if there's no new info.
107 uint16_t size = 4 * (1 + 1 + (uint16_t)node_count);
108 size |= 0x8000; // Deps record: set high bit.
109 fwrite(&size, 2, 1, file_);
111 fwrite(&id, 4, 1, file_);
112 int timestamp = mtime;
113 fwrite(×tamp, 4, 1, file_);
114 for (int i = 0; i < node_count; ++i) {
116 fwrite(&id, 4, 1, file_);
122 void DepsLog::Close() {
128 bool DepsLog::Load(const string& path, State* state, string* err) {
129 METRIC_RECORD(".ninja_deps load");
131 FILE* f = fopen(path.c_str(), "rb");
135 *err = strerror(errno);
139 if (!fgets(buf, sizeof(buf), f)) {
140 *err = strerror(errno);
144 if (fread(&version, 4, 1, f) < 1) {
145 *err = strerror(errno);
148 if (version != kCurrentVersion) {
149 *err = "bad deps log signature or version; starting over";
151 unlink(path.c_str());
152 // Don't report this as a failure. An empty deps log will cause
153 // us to rebuild the outputs anyway.
159 if (fread(&size, 2, 1, f) < 1)
161 bool is_deps = (size >> 15) != 0;
162 size = size & 0x7FFF;
164 if (fread(buf, size, 1, f) < 1)
168 assert(size % 4 == 0);
169 int* deps_data = reinterpret_cast<int*>(buf);
170 int out_id = deps_data[0];
171 int mtime = deps_data[1];
173 int deps_count = (size / 4) - 2;
175 Deps* deps = new Deps;
177 deps->node_count = deps_count;
178 deps->nodes = new Node*[deps_count];
179 for (int i = 0; i < deps_count; ++i) {
180 assert(deps_data[i] < (int)nodes_.size());
181 assert(nodes_[deps_data[i]]);
182 deps->nodes[i] = nodes_[deps_data[i]];
185 if (out_id >= (int)deps_.size())
186 deps_.resize(out_id + 1);
188 ++dead_record_count_;
189 delete deps_[out_id];
191 deps_[out_id] = deps;
193 StringPiece path(buf, size);
194 Node* node = state->GetNode(path);
195 assert(node->id() < 0);
196 node->set_id(nodes_.size());
197 nodes_.push_back(node);
201 *err = strerror(ferror(f));
208 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
211 return deps_[node->id()];
214 bool DepsLog::Recompact(const string& path, string* err) {
215 METRIC_RECORD(".ninja_deps recompact");
216 printf("Recompacting deps...\n");
218 string temp_path = path + ".recompact";
220 if (!new_log.OpenForWrite(temp_path, err))
223 // Clear all known ids so that new ones can be reassigned.
224 for (vector<Node*>::iterator i = nodes_.begin();
225 i != nodes_.end(); ++i) {
229 // Write out all deps again.
230 for (int old_id = 0; old_id < (int)deps_.size(); ++old_id) {
231 Deps* deps = deps_[old_id];
232 if (!new_log.RecordDeps(nodes_[old_id], deps->mtime,
233 deps->node_count, deps->nodes)) {
241 if (unlink(path.c_str()) < 0) {
242 *err = strerror(errno);
246 if (rename(temp_path.c_str(), path.c_str()) < 0) {
247 *err = strerror(errno);
254 bool DepsLog::RecordId(Node* node) {
255 uint16_t size = (uint16_t)node->path().size();
256 fwrite(&size, 2, 1, file_);
257 fwrite(node->path().data(), node->path().size(), 1, file_);
259 node->set_id(nodes_.size());
260 nodes_.push_back(node);