expand DepsLog test, fix two bugs it revealed
[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 "state.h"
24 #include "util.h"
25
26 bool DepsLog::OpenForWrite(const string& path, string* err) {
27   file_ = fopen(path.c_str(), "ab");
28   if (!file_) {
29     *err = strerror(errno);
30     return false;
31   }
32   SetCloseOnExec(fileno(file_));
33
34   // Opening a file in append mode doesn't set the file pointer to the file's
35   // end on Windows. Do that explicitly.
36   fseek(file_, 0, SEEK_END);
37
38   /* XXX
39   if (ftell(log_file_) == 0) {
40     if (fprintf(log_file_, kFileSignature, kCurrentVersion) < 0) {
41       *err = strerror(errno);
42       return false;
43     }
44   }
45   */
46
47   return true;
48 }
49
50 bool DepsLog::RecordDeps(Node* node, TimeStamp mtime,
51                          const vector<Node*>& nodes) {
52   // Assign ids to all nodes that are missing one.
53   if (node->id() < 0)
54     RecordId(node);
55   for (vector<Node*>::const_iterator i = nodes.begin();
56        i != nodes.end(); ++i) {
57     if ((*i)->id() < 0)
58       RecordId(*i);
59   }
60
61   uint16_t size = 4 * (1 + 1 + nodes.size());
62   size |= 0x8000;  // Deps record: set high bit.
63   fwrite(&size, 2, 1, file_);
64   int id = node->id();
65   fwrite(&id, 4, 1, file_);
66   int timestamp = mtime;
67   fwrite(&timestamp, 4, 1, file_);
68   for (vector<Node*>::const_iterator i = nodes.begin();
69        i != nodes.end(); ++i) {
70     id = (*i)->id();
71     fwrite(&id, 4, 1, file_);
72   }
73
74   return true;
75 }
76
77 void DepsLog::Close() {
78   fclose(file_);
79   file_ = NULL;
80 }
81
82 bool DepsLog::Load(const string& path, State* state, string* err) {
83   char buf[32 << 10];
84   FILE* f = fopen(path.c_str(), "rb");
85   if (!f) {
86     if (errno == ENOENT)
87       return true;
88     *err = strerror(errno);
89     return false;
90   }
91
92   for (;;) {
93     uint16_t size;
94     if (fread(&size, 2, 1, f) < 1)
95       break;
96     bool is_deps = (size >> 15) != 0;
97     size = size & 0x7FFF;
98
99     if (fread(buf, size, 1, f) < 1)
100       break;
101
102     if (is_deps) {
103       assert(size % 4 == 0);
104       int* deps_data = reinterpret_cast<int*>(buf);
105       int out_id = deps_data[0];
106       int mtime = deps_data[1];
107       deps_data += 2;
108       int deps_count = (size / 4) - 2;
109
110       Deps* deps = new Deps;
111       deps->mtime = mtime;
112       deps->node_count = deps_count;
113       deps->nodes = new Node*[deps_count];
114       for (int i = 0; i < deps_count; ++i) {
115         assert(deps_data[i] < (int)nodes_.size());
116         assert(nodes_[deps_data[i]]);
117         deps->nodes[i] = nodes_[deps_data[i]];
118       }
119
120       if (out_id >= (int)deps_.size())
121         deps_.resize(out_id + 1);
122       if (deps_[out_id])
123         delete deps_[out_id];
124       deps_[out_id] = deps;
125     } else {
126       StringPiece path(buf, size);
127       Node* node = state->GetNode(path);
128       assert(node->id() < 0);
129       node->set_id(nodes_.size());
130       nodes_.push_back(node);
131     }
132   }
133   if (ferror(f)) {
134     *err = strerror(ferror(f));
135     return false;
136   }
137   fclose(f);
138   return true;
139 }
140
141 DepsLog::Deps* DepsLog::GetDeps(Node* node) {
142   if (node->id() < 0)
143     return NULL;
144   return deps_[node->id()];
145 }
146
147 bool DepsLog::RecordId(Node* node) {
148   uint16_t size = node->path().size();
149   fwrite(&size, 2, 1, file_);
150   fwrite(node->path().data(), node->path().size(), 1, file_);
151
152   node->set_id(nodes_.size());
153   nodes_.push_back(node);
154
155   return true;
156 }