1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "build_log.h"
22 #include "manifest_parser.h"
33 /// mingw has no mktemp. Implement one with the same type as the one
34 /// found in the Windows API.
35 int _mktemp_s(char* templ) {
36 char* ofs = strchr(templ, 'X');
37 sprintf(ofs, "%d", rand() % 1000000);
42 /// Windows has no mkdtemp. Implement it in terms of _mktemp_s.
43 char* mkdtemp(char* name_template) {
44 int err = _mktemp_s(name_template);
50 err = _mkdir(name_template);
60 string GetSystemTempDir() {
63 if (!GetTempPath(sizeof(buf), buf))
67 const char* tempdir = getenv("TMPDIR");
74 } // anonymous namespace
76 StateTestWithBuiltinRules::StateTestWithBuiltinRules() {
79 " command = cat $in > $out\n");
82 Node* StateTestWithBuiltinRules::GetNode(const string& path) {
83 return state_.GetNode(path);
86 void AssertParse(State* state, const char* input) {
87 ManifestParser parser(state, NULL);
89 ASSERT_TRUE(parser.ParseTest(input, &err)) << err;
93 void AssertHash(const char* expected, uint64_t actual) {
94 ASSERT_EQ(BuildLog::LogEntry::HashCommand(expected), actual);
97 void VirtualFileSystem::Create(const string& path,
98 const string& contents) {
99 files_[path].mtime = now_;
100 files_[path].contents = contents;
101 files_created_.insert(path);
104 TimeStamp VirtualFileSystem::Stat(const string& path) {
105 FileMap::iterator i = files_.find(path);
106 if (i != files_.end())
107 return i->second.mtime;
111 bool VirtualFileSystem::WriteFile(const string& path, const string& contents) {
112 Create(path, contents);
116 bool VirtualFileSystem::MakeDir(const string& path) {
117 directories_made_.push_back(path);
118 return true; // success
121 string VirtualFileSystem::ReadFile(const string& path, string* err) {
122 files_read_.push_back(path);
123 FileMap::iterator i = files_.find(path);
124 if (i != files_.end())
125 return i->second.contents;
129 int VirtualFileSystem::RemoveFile(const string& path) {
130 if (find(directories_made_.begin(), directories_made_.end(), path)
131 != directories_made_.end())
133 FileMap::iterator i = files_.find(path);
134 if (i != files_.end()) {
136 files_removed_.insert(path);
143 void ScopedTempDir::CreateAndEnter(const string& name) {
144 // First change into the system temp dir and save it for cleanup.
145 start_dir_ = GetSystemTempDir();
146 if (start_dir_.empty())
147 Fatal("couldn't get system temp dir");
148 if (chdir(start_dir_.c_str()) < 0)
149 Fatal("chdir: %s", strerror(errno));
151 // Create a temporary subdirectory of that.
152 char name_template[1024];
153 strcpy(name_template, name.c_str());
154 strcat(name_template, "-XXXXXX");
155 char* tempname = mkdtemp(name_template);
157 Fatal("mkdtemp: %s", strerror(errno));
158 temp_dir_name_ = tempname;
160 // chdir into the new temporary directory.
161 if (chdir(temp_dir_name_.c_str()) < 0)
162 Fatal("chdir: %s", strerror(errno));
165 void ScopedTempDir::Cleanup() {
166 if (temp_dir_name_.empty())
167 return; // Something went wrong earlier.
169 // Move out of the directory we're about to clobber.
170 if (chdir(start_dir_.c_str()) < 0)
171 Fatal("chdir: %s", strerror(errno));
174 string command = "rmdir /s /q " + temp_dir_name_;
176 string command = "rm -rf " + temp_dir_name_;
178 if (system(command.c_str()) < 0)
179 Fatal("system: %s", strerror(errno));
181 temp_dir_name_.clear();