record and check depslog file version
[platform/upstream/ninja.git] / src / deps_log.cc
1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
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
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 #include "deps_log.h"
16
17 #include <assert.h>
18 #include <stdio.h>
19 #include <errno.h>
20 #include <string.h>
21
22 #include "graph.h"
23 #include "metrics.h"
24 #include "state.h"
25 #include "util.h"
26
27 namespace {
28 const char kFileSignature[] = "# ninja deps v%d\n";
29 const int kCurrentVersion = 1;
30 }  // anonymous namespace
31
32
33 bool DepsLog::OpenForWrite(const string& path, string* err) {
34   file_ = fopen(path.c_str(), "ab");
35   if (!file_) {
36     *err = strerror(errno);
37     return false;
38   }
39   SetCloseOnExec(fileno(file_));
40
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);
44
45   if (ftell(file_) == 0) {
46     if (fprintf(file_, kFileSignature, kCurrentVersion) < 0) {
47       *err = strerror(errno);
48       return false;
49     }
50   }
51
52   return true;
53 }
54
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;
59
60   // Assign ids to all nodes that are missing one.
61   if (node->id() < 0) {
62     RecordId(node);
63     made_change = true;
64   }
65   for (vector<Node*>::const_iterator i = nodes.begin();
66        i != nodes.end(); ++i) {
67     if ((*i)->id() < 0) {
68       RecordId(*i);
69       made_change = true;
70     }
71   }
72
73   // See if the new data is different than the existing data, if any.
74   if (!made_change) {
75     Deps* deps = GetDeps(node);
76     if (!deps ||
77         deps->mtime != mtime ||
78         deps->node_count != (int)nodes.size()) {
79       made_change = true;
80     } else {
81       for (int i = 0; i < (int)nodes.size(); ++i) {
82         if (deps->nodes[i] != nodes[i]) {
83           made_change = true;
84           break;
85         }
86       }
87     }
88   }
89
90   // Don't write anything if there's no new info.
91   if (!made_change)
92     return true;
93
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_);
97   int id = node->id();
98   fwrite(&id, 4, 1, file_);
99   int timestamp = mtime;
100   fwrite(&timestamp, 4, 1, file_);
101   for (vector<Node*>::const_iterator i = nodes.begin();
102        i != nodes.end(); ++i) {
103     id = (*i)->id();
104     fwrite(&id, 4, 1, file_);
105   }
106
107   return true;
108 }
109
110 void DepsLog::Close() {
111   fclose(file_);
112   file_ = NULL;
113 }
114
115 bool DepsLog::Load(const string& path, State* state, string* err) {
116   METRIC_RECORD(".ninja_deps load");
117   char buf[32 << 10];
118   FILE* f = fopen(path.c_str(), "rb");
119   if (!f) {
120     if (errno == ENOENT)
121       return true;
122     *err = strerror(errno);
123     return false;
124   }
125
126   if (!fgets(buf, sizeof(buf), f)) {
127     *err = strerror(errno);
128     return false;
129   }
130   int version = 0;
131   if (sscanf(buf, kFileSignature, &version) != 1) {
132     *err = "unable to read file signature";
133     return false;
134   }
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.
139     return true;
140   }
141
142   for (;;) {
143     uint16_t size;
144     if (fread(&size, 2, 1, f) < 1)
145       break;
146     bool is_deps = (size >> 15) != 0;
147     size = size & 0x7FFF;
148
149     if (fread(buf, size, 1, f) < 1)
150       break;
151
152     if (is_deps) {
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];
157       deps_data += 2;
158       int deps_count = (size / 4) - 2;
159
160       Deps* deps = new Deps;
161       deps->mtime = mtime;
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]];
168       }
169
170       if (out_id >= (int)deps_.size())
171         deps_.resize(out_id + 1);
172       if (deps_[out_id])
173         delete deps_[out_id];
174       deps_[out_id] = deps;
175     } else {
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);
181     }
182   }
183   if (ferror(f)) {
184     *err = strerror(ferror(f));
185     return false;
186   }
187   fclose(f);
188   return true;
189 }
190
191 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
192   if (node->id() < 0)
193     return NULL;
194   return deps_[node->id()];
195 }
196
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_);
201
202   node->set_id(nodes_.size());
203   nodes_.push_back(node);
204
205   return true;
206 }