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