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