42 extern char* mkdtemp(
char* name_template);
52 char* mkdtemp(
char* name_template) {
53 int err = _mktemp_s(name_template, strlen(name_template) + 1);
59 err = _mkdir(name_template);
69 string GetSystemTempDir() {
72 if (!GetTempPath(
sizeof(buf), buf))
76 const char* tempdir = getenv(
"TMPDIR");
92 " command = cat $in > $out\n");
97 return state_.GetNode(path, 0);
114 for (vector<Edge*>::const_iterator e = state.
edges_.begin();
115 e != state.
edges_.end(); ++e) {
119 for (vector<Node*>::const_iterator in_node = (*e)->inputs_.begin();
120 in_node != (*e)->inputs_.end(); ++in_node) {
121 const vector<Edge*>& out_edges = (*in_node)->out_edges();
122 EXPECT_NE(find(out_edges.begin(), out_edges.end(), *e),
126 for (vector<Node*>::const_iterator out_node = (*e)->outputs_.begin();
127 out_node != (*e)->outputs_.end(); ++out_node) {
133 set<const Edge*> node_edge_set;
134 for (State::Paths::const_iterator p = state.
paths_.begin();
135 p != state.
paths_.end(); ++p) {
136 const Node* n = p->second;
138 node_edge_set.insert(n->
in_edge());
141 set<const Edge*> edge_set(state.
edges_.begin(), state.
edges_.end());
146 const string& contents) {
147 files_[path].mtime = now_;
148 files_[path].contents = contents;
149 files_created_.insert(path);
153 FileMap::const_iterator i = files_.find(path);
154 if (i != files_.end()) {
155 *err = i->second.stat_error;
156 return i->second.mtime;
162 Create(path, contents);
167 directories_made_.push_back(path);
174 files_read_.push_back(path);
175 FileMap::iterator i = files_.find(path);
176 if (i != files_.end()) {
177 *contents = i->second.contents;
180 *err = strerror(ENOENT);
185 if (find(directories_made_.begin(), directories_made_.end(), path)
186 != directories_made_.end())
188 FileMap::iterator i = files_.find(path);
189 if (i != files_.end()) {
191 files_removed_.insert(path);
200 start_dir_ = GetSystemTempDir();
201 if (start_dir_.empty())
202 Fatal(
"couldn't get system temp dir");
203 if (chdir(start_dir_.c_str()) < 0)
204 Fatal(
"chdir: %s", strerror(errno));
207 char name_template[1024];
208 strcpy(name_template, name.c_str());
209 strcat(name_template,
"-XXXXXX");
210 char* tempname = mkdtemp(name_template);
212 Fatal(
"mkdtemp: %s", strerror(errno));
213 temp_dir_name_ = tempname;
216 if (chdir(temp_dir_name_.c_str()) < 0)
217 Fatal(
"chdir: %s", strerror(errno));
221 if (temp_dir_name_.empty())
225 if (chdir(start_dir_.c_str()) < 0)
226 Fatal(
"chdir: %s", strerror(errno));
229 string command =
"rmdir /s /q " + temp_dir_name_;
231 string command =
"rm -rf " + temp_dir_name_;
233 if (system(command.c_str()) < 0)
234 Fatal(
"system: %s", strerror(errno));
236 temp_dir_name_.clear();
virtual TimeStamp Stat(const std::string &path, std::string *err) const
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
void AssertParse(State *state, const char *input, ManifestParserOptions opts)
Information about a node in the dependency graph: the file, whether it's dirty, mtime, etc.
void Create(const std::string &path, const std::string &contents)
"Create" a file with contents.
StateTestWithBuiltinRules()
void CreateAndEnter(const std::string &name)
Create a temporary directory and chdir into it.
Node * GetNode(const std::string &path)
Short way to get a Node by its path from state_.
virtual bool WriteFile(const std::string &path, const std::string &contents)
Create a file, with the specified name and contents Returns true on success, false on failure...
void AddCatRule(State *state)
Add a "cat" rule to state.
virtual int RemoveFile(const std::string &path)
Remove the file named path.
virtual bool MakeDir(const std::string &path)
Create a directory, returning false on failure.
Status
Result of ReadFile.
void VerifyGraph(const State &state)
virtual Status ReadFile(const std::string &path, std::string *contents, std::string *err)
Read and store in given string.
bool ParseTest(const std::string &input, std::string *err)
Parse a text string of input. Used by tests.
void Fatal(const char *msg,...)
Log a fatal message and exit.
void Cleanup()
Clean up the temporary directory.
static uint64_t HashCommand(StringPiece command)
const std::vector< Edge * > & out_edges() const
Global state (file status) for a single run.
unsigned long long uint64_t
std::vector< Edge * > edges_
All the edges of the graph.
void AssertHash(const char *expected, uint64_t actual)