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.
27 bool DepsLog::OpenForWrite(const string& path, string* err) {
28 file_ = fopen(path.c_str(), "ab");
30 *err = strerror(errno);
33 SetCloseOnExec(fileno(file_));
35 // Opening a file in append mode doesn't set the file pointer to the file's
36 // end on Windows. Do that explicitly.
37 fseek(file_, 0, SEEK_END);
40 if (ftell(log_file_) == 0) {
41 if (fprintf(log_file_, kFileSignature, kCurrentVersion) < 0) {
42 *err = strerror(errno);
51 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
52 const vector<Node*>& nodes) {
53 // Track whether there's any new data to be recorded.
54 bool made_change = false;
56 // Assign ids to all nodes that are missing one.
61 for (vector<Node*>::const_iterator i = nodes.begin();
62 i != nodes.end(); ++i) {
69 // See if the new data is different than the existing data, if any.
71 Deps* deps = GetDeps(node);
73 deps->mtime != mtime ||
74 deps->node_count != (int)nodes.size()) {
77 for (int i = 0; i < (int)nodes.size(); ++i) {
78 if (deps->nodes[i] != nodes[i]) {
86 // Don't write anything if there's no new info.
90 uint16_t size = 4 * (1 + 1 + (uint16_t)nodes.size());
91 size |= 0x8000; // Deps record: set high bit.
92 fwrite(&size, 2, 1, file_);
94 fwrite(&id, 4, 1, file_);
95 int timestamp = mtime;
96 fwrite(×tamp, 4, 1, file_);
97 for (vector<Node*>::const_iterator i = nodes.begin();
98 i != nodes.end(); ++i) {
100 fwrite(&id, 4, 1, file_);
106 void DepsLog::Close() {
111 bool DepsLog::Load(const string& path, State* state, string* err) {
112 METRIC_RECORD(".ninja_deps load");
114 FILE* f = fopen(path.c_str(), "rb");
118 *err = strerror(errno);
124 if (fread(&size, 2, 1, f) < 1)
126 bool is_deps = (size >> 15) != 0;
127 size = size & 0x7FFF;
129 if (fread(buf, size, 1, f) < 1)
133 assert(size % 4 == 0);
134 int* deps_data = reinterpret_cast<int*>(buf);
135 int out_id = deps_data[0];
136 int mtime = deps_data[1];
138 int deps_count = (size / 4) - 2;
140 Deps* deps = new Deps;
142 deps->node_count = deps_count;
143 deps->nodes = new Node*[deps_count];
144 for (int i = 0; i < deps_count; ++i) {
145 assert(deps_data[i] < (int)nodes_.size());
146 assert(nodes_[deps_data[i]]);
147 deps->nodes[i] = nodes_[deps_data[i]];
150 if (out_id >= (int)deps_.size())
151 deps_.resize(out_id + 1);
153 delete deps_[out_id];
154 deps_[out_id] = deps;
156 StringPiece path(buf, size);
157 Node* node = state->GetNode(path);
158 assert(node->id() < 0);
159 node->set_id(nodes_.size());
160 nodes_.push_back(node);
164 *err = strerror(ferror(f));
171 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
174 return deps_[node->id()];
177 bool DepsLog::RecordId(Node* node) {
178 uint16_t size = (uint16_t)node->path().size();
179 fwrite(&size, 2, 1, file_);
180 fwrite(node->path().data(), node->path().size(), 1, file_);
182 node->set_id(nodes_.size());
183 nodes_.push_back(node);