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.
32 /// mingw has no mktemp. Implement one with the same type as the one
33 /// found in the Windows API.
34 int _mktemp_s(char* templ) {
35 char* ofs = strchr(templ, 'X');
36 sprintf(ofs, "%d", rand() % 1000000);
41 /// Windows has no mkdtemp. Implement it in terms of _mktemp_s.
42 char* mkdtemp(char* name_template) {
43 int err = _mktemp_s(name_template);
49 err = _mkdir(name_template);
59 string GetSystemTempDir() {
62 if (!GetTempPath(sizeof(buf), buf))
66 const char* tempdir = getenv("TMPDIR");
73 } // anonymous namespace
75 StateTestWithBuiltinRules::StateTestWithBuiltinRules() {
78 " command = cat $in > $out\n");
81 Node* StateTestWithBuiltinRules::GetNode(const string& path) {
82 return state_.GetNode(path);
85 void AssertParse(State* state, const char* input) {
86 ManifestParser parser(state, NULL);
88 ASSERT_TRUE(parser.ParseTest(input, &err)) << err;
92 void VirtualFileSystem::Create(const string& path, int time,
93 const string& contents) {
94 files_[path].mtime = time;
95 files_[path].contents = contents;
98 TimeStamp VirtualFileSystem::Stat(const string& path) {
99 FileMap::iterator i = files_.find(path);
100 if (i != files_.end())
101 return i->second.mtime;
105 bool VirtualFileSystem::MakeDir(const string& path) {
106 directories_made_.push_back(path);
107 return true; // success
110 string VirtualFileSystem::ReadFile(const string& path, string* err) {
111 files_read_.push_back(path);
112 FileMap::iterator i = files_.find(path);
113 if (i != files_.end())
114 return i->second.contents;
118 int VirtualFileSystem::RemoveFile(const string& path) {
119 if (find(directories_made_.begin(), directories_made_.end(), path)
120 != directories_made_.end())
122 FileMap::iterator i = files_.find(path);
123 if (i != files_.end()) {
125 files_removed_.insert(path);
132 void ScopedTempDir::CreateAndEnter(const string& name) {
133 // First change into the system temp dir and save it for cleanup.
134 start_dir_ = GetSystemTempDir();
135 if (start_dir_.empty())
136 Fatal("couldn't get system temp dir");
137 if (chdir(start_dir_.c_str()) < 0)
138 Fatal("chdir: %s", strerror(errno));
140 // Create a temporary subdirectory of that.
141 char name_template[1024];
142 strcpy(name_template, name.c_str());
143 strcat(name_template, "-XXXXXX");
144 char* tempname = mkdtemp(name_template);
146 Fatal("mkdtemp: %s", strerror(errno));
147 temp_dir_name_ = tempname;
149 // chdir into the new temporary directory.
150 if (chdir(temp_dir_name_.c_str()) < 0)
151 Fatal("chdir: %s", strerror(errno));
154 void ScopedTempDir::Cleanup() {
155 if (temp_dir_name_.empty())
156 return; // Something went wrong earlier.
158 // Move out of the directory we're about to clobber.
159 if (chdir(start_dir_.c_str()) < 0)
160 Fatal("chdir: %s", strerror(errno));
163 string command = "rmdir /s /q " + temp_dir_name_;
165 string command = "rm -rf " + temp_dir_name_;
167 if (system(command.c_str()) < 0)
168 Fatal("system: %s", strerror(errno));
170 temp_dir_name_.clear();