add DepsLog, a new data structure for dependency information
[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 = node->mtime();
67   fwrite(&timestamp, 4, 1, file_);
68   for (vector<Node*>::const_iterator i = nodes.begin();
69        i != nodes.end(); ++i) {
70     id = node->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     *err = strerror(errno);
87     return false;
88   }
89
90   int id = 0;
91   for (;;) {
92     uint16_t size;
93     if (fread(&size, 2, 1, f) < 1)
94       break;
95     bool is_deps = (size >> 15) != 0;
96     size = size & 0x7FFF;
97
98     if (fread(buf, size, 1, f) < 1)
99       break;
100
101     if (is_deps) {
102       assert(size % 4 == 0);
103       int* deps_data = reinterpret_cast<int*>(buf);
104       int out_id = deps_data[0];
105       int mtime = deps_data[1];
106       deps_data += 2;
107       int deps_count = (size / 4) - 2;
108
109       Deps* deps = new Deps;
110       deps->mtime = mtime;
111       deps->node_count = deps_count;
112       deps->nodes = new Node*[deps_count];
113       for (int i = 0; i < deps_count; ++i) {
114         assert(deps_data[i] < (int)nodes_.size());
115         assert(nodes_[deps_data[i]]);
116         deps->nodes[i] = nodes_[deps_data[i]];
117       }
118
119       if (out_id >= (int)deps_.size())
120         deps_.resize(out_id + 1);
121       if (deps_[out_id])
122         delete deps_[out_id];
123       deps_[out_id] = deps;
124     } else {
125       StringPiece path(buf, size);
126       Node* node = state->GetNode(path);
127       assert(node->id() < 0);
128       node->set_id(id);
129       ++id;
130     }
131   }
132   if (ferror(f)) {
133     *err = strerror(ferror(f));
134     return false;
135   }
136   fclose(f);
137   return true;
138 }
139
140 bool DepsLog::RecordId(Node* node) {
141   uint16_t size = node->path().size();
142   fwrite(&size, 2, 1, file_);
143   fwrite(node->path().data(), node->path().size(), 1, file_);
144
145   node->set_id(nodes_.size());
146   nodes_.push_back(node);
147
148   return true;
149 }