add a test for the "deps out of date" case
[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 #include <unistd.h>
22
23 #include "graph.h"
24 #include "metrics.h"
25 #include "state.h"
26 #include "util.h"
27
28 namespace {
29 const char kFileSignature[] = "# ninja deps v%d\n";
30 const int kCurrentVersion = 1;
31 }  // anonymous namespace
32
33 DepsLog::~DepsLog() {
34   Close();
35 }
36
37 bool DepsLog::OpenForWrite(const string& path, string* err) {
38   file_ = fopen(path.c_str(), "ab");
39   if (!file_) {
40     *err = strerror(errno);
41     return false;
42   }
43   SetCloseOnExec(fileno(file_));
44
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);
48
49   if (ftell(file_) == 0) {
50     if (fprintf(file_, kFileSignature, kCurrentVersion) < 0) {
51       *err = strerror(errno);
52       return false;
53     }
54   }
55
56   return true;
57 }
58
59 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
60                          const vector<Node*>& nodes) {
61   return RecordDeps(node, mtime, nodes.size(),
62                     nodes.empty() ? NULL : (Node**)&nodes.front());
63 }
64
65 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
66                          int node_count, Node** nodes) {
67   // Track whether there's any new data to be recorded.
68   bool made_change = false;
69
70   // Assign ids to all nodes that are missing one.
71   if (node->id() < 0) {
72     RecordId(node);
73     made_change = true;
74   }
75   for (int i = 0; i < node_count; ++i) {
76     if (nodes[i]->id() < 0) {
77       RecordId(nodes[i]);
78       made_change = true;
79     }
80   }
81
82   // See if the new data is different than the existing data, if any.
83   if (!made_change) {
84     Deps* deps = GetDeps(node);
85     if (!deps ||
86         deps->mtime != mtime ||
87         deps->node_count != node_count) {
88       made_change = true;
89     } else {
90       for (int i = 0; i < node_count; ++i) {
91         if (deps->nodes[i] != nodes[i]) {
92           made_change = true;
93           break;
94         }
95       }
96     }
97   }
98
99   // Don't write anything if there's no new info.
100   if (!made_change)
101     return true;
102
103   uint16_t size = 4 * (1 + 1 + (uint16_t)node_count);
104   size |= 0x8000;  // Deps record: set high bit.
105   fwrite(&size, 2, 1, file_);
106   int id = node->id();
107   fwrite(&id, 4, 1, file_);
108   int timestamp = mtime;
109   fwrite(&timestamp, 4, 1, file_);
110   for (int i = 0; i < node_count; ++i) {
111     id = nodes[i]->id();
112     fwrite(&id, 4, 1, file_);
113   }
114
115   return true;
116 }
117
118 void DepsLog::Close() {
119   if (file_)
120     fclose(file_);
121   file_ = NULL;
122 }
123
124 bool DepsLog::Load(const string& path, State* state, string* err) {
125   METRIC_RECORD(".ninja_deps load");
126   char buf[32 << 10];
127   FILE* f = fopen(path.c_str(), "rb");
128   if (!f) {
129     if (errno == ENOENT)
130       return true;
131     *err = strerror(errno);
132     return false;
133   }
134
135   if (!fgets(buf, sizeof(buf), f)) {
136     *err = strerror(errno);
137     return false;
138   }
139   int version = 0;
140   sscanf(buf, kFileSignature, &version);
141   if (version != kCurrentVersion) {
142     *err = "bad deps log signature or version; starting over";
143     fclose(f);
144     unlink(path.c_str());
145     // Don't report this as a failure.  An empty deps log will cause
146     // us to rebuild the outputs anyway.
147     return true;
148   }
149
150   for (;;) {
151     uint16_t size;
152     if (fread(&size, 2, 1, f) < 1)
153       break;
154     bool is_deps = (size >> 15) != 0;
155     size = size & 0x7FFF;
156
157     if (fread(buf, size, 1, f) < 1)
158       break;
159
160     if (is_deps) {
161       assert(size % 4 == 0);
162       int* deps_data = reinterpret_cast<int*>(buf);
163       int out_id = deps_data[0];
164       int mtime = deps_data[1];
165       deps_data += 2;
166       int deps_count = (size / 4) - 2;
167
168       Deps* deps = new Deps;
169       deps->mtime = mtime;
170       deps->node_count = deps_count;
171       deps->nodes = new Node*[deps_count];
172       for (int i = 0; i < deps_count; ++i) {
173         assert(deps_data[i] < (int)nodes_.size());
174         assert(nodes_[deps_data[i]]);
175         deps->nodes[i] = nodes_[deps_data[i]];
176       }
177
178       if (out_id >= (int)deps_.size())
179         deps_.resize(out_id + 1);
180       if (deps_[out_id]) {
181         ++dead_record_count_;
182         delete deps_[out_id];
183       }
184       deps_[out_id] = deps;
185     } else {
186       StringPiece path(buf, size);
187       Node* node = state->GetNode(path);
188       assert(node->id() < 0);
189       node->set_id(nodes_.size());
190       nodes_.push_back(node);
191     }
192   }
193   if (ferror(f)) {
194     *err = strerror(ferror(f));
195     return false;
196   }
197   fclose(f);
198   return true;
199 }
200
201 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
202   if (node->id() < 0)
203     return NULL;
204   return deps_[node->id()];
205 }
206
207 bool DepsLog::Recompact(const string& path, string* err) {
208   METRIC_RECORD(".ninja_deps recompact");
209   printf("Recompacting deps...\n");
210
211   string temp_path = path + ".recompact";
212   DepsLog new_log;
213   if (!new_log.OpenForWrite(temp_path, err))
214     return false;
215
216   // Clear all known ids so that new ones can be reassigned.
217   for (vector<Node*>::iterator i = nodes_.begin();
218        i != nodes_.end(); ++i) {
219     (*i)->set_id(-1);
220   }
221
222   // Write out all deps again.
223   for (int old_id = 0; old_id < (int)deps_.size(); ++old_id) {
224     Deps* deps = deps_[old_id];
225     if (!new_log.RecordDeps(nodes_[old_id], deps->mtime,
226                             deps->node_count, deps->nodes)) {
227       new_log.Close();
228       return false;
229     }
230   }
231
232   new_log.Close();
233
234   if (unlink(path.c_str()) < 0) {
235     *err = strerror(errno);
236     return false;
237   }
238
239   if (rename(temp_path.c_str(), path.c_str()) < 0) {
240     *err = strerror(errno);
241     return false;
242   }
243
244   return true;
245 }
246
247 bool DepsLog::RecordId(Node* node) {
248   uint16_t size = (uint16_t)node->path().size();
249   fwrite(&size, 2, 1, file_);
250   fwrite(node->path().data(), node->path().size(), 1, file_);
251
252   node->set_id(nodes_.size());
253   nodes_.push_back(node);
254
255   return true;
256 }