make old deps format migration actually work
[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   sscanf(buf, kFileSignature, &version);
132   if (version != kCurrentVersion) {
133     *err = "bad deps log signature or version; starting over";
134     fclose(f);
135     unlink(path.c_str());
136     // Don't report this as a failure.  An empty deps log will cause
137     // us to rebuild the outputs anyway.
138     return true;
139   }
140
141   for (;;) {
142     uint16_t size;
143     if (fread(&size, 2, 1, f) < 1)
144       break;
145     bool is_deps = (size >> 15) != 0;
146     size = size & 0x7FFF;
147
148     if (fread(buf, size, 1, f) < 1)
149       break;
150
151     if (is_deps) {
152       assert(size % 4 == 0);
153       int* deps_data = reinterpret_cast<int*>(buf);
154       int out_id = deps_data[0];
155       int mtime = deps_data[1];
156       deps_data += 2;
157       int deps_count = (size / 4) - 2;
158
159       Deps* deps = new Deps;
160       deps->mtime = mtime;
161       deps->node_count = deps_count;
162       deps->nodes = new Node*[deps_count];
163       for (int i = 0; i < deps_count; ++i) {
164         assert(deps_data[i] < (int)nodes_.size());
165         assert(nodes_[deps_data[i]]);
166         deps->nodes[i] = nodes_[deps_data[i]];
167       }
168
169       if (out_id >= (int)deps_.size())
170         deps_.resize(out_id + 1);
171       if (deps_[out_id])
172         delete deps_[out_id];
173       deps_[out_id] = deps;
174     } else {
175       StringPiece path(buf, size);
176       Node* node = state->GetNode(path);
177       assert(node->id() < 0);
178       node->set_id(nodes_.size());
179       nodes_.push_back(node);
180     }
181   }
182   if (ferror(f)) {
183     *err = strerror(ferror(f));
184     return false;
185   }
186   fclose(f);
187   return true;
188 }
189
190 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
191   if (node->id() < 0)
192     return NULL;
193   return deps_[node->id()];
194 }
195
196 bool DepsLog::RecordId(Node* node) {
197   uint16_t size = (uint16_t)node->path().size();
198   fwrite(&size, 2, 1, file_);
199   fwrite(node->path().data(), node->path().size(), 1, file_);
200
201   node->set_id(nodes_.size());
202   nodes_.push_back(node);
203
204   return true;
205 }