Ninja
disk_interface.cc
Go to the documentation of this file.
1 // Copyright 2011 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 "disk_interface.h"
16 
17 #include <algorithm>
18 
19 #include <errno.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 
25 #ifdef _WIN32
26 #include <windows.h>
27 #include <direct.h> // _mkdir
28 #endif
29 
30 #include "util.h"
31 
32 namespace {
33 
34 string DirName(const string& path) {
35 #ifdef _WIN32
36  const char kPathSeparators[] = "\\/";
37 #else
38  const char kPathSeparators[] = "/";
39 #endif
40  string::size_type slash_pos = path.find_last_of(kPathSeparators);
41  if (slash_pos == string::npos)
42  return string(); // Nothing to do.
43  const char* const kEnd = kPathSeparators + strlen(kPathSeparators);
44  while (slash_pos > 0 &&
45  std::find(kPathSeparators, kEnd, path[slash_pos - 1]) != kEnd)
46  --slash_pos;
47  return path.substr(0, slash_pos);
48 }
49 
50 int MakeDir(const string& path) {
51 #ifdef _WIN32
52  return _mkdir(path.c_str());
53 #else
54  return mkdir(path.c_str(), 0777);
55 #endif
56 }
57 
58 #ifdef _WIN32
59 TimeStamp TimeStampFromFileTime(const FILETIME& filetime) {
60  // FILETIME is in 100-nanosecond increments since the Windows epoch.
61  // We don't much care about epoch correctness but we do want the
62  // resulting value to fit in an integer.
63  uint64_t mtime = ((uint64_t)filetime.dwHighDateTime << 32) |
64  ((uint64_t)filetime.dwLowDateTime);
65  mtime /= 1000000000LL / 100; // 100ns -> s.
66  mtime -= 12622770400LL; // 1600 epoch -> 2000 epoch (subtract 400 years).
67  return (TimeStamp)mtime;
68 }
69 
70 TimeStamp StatSingleFile(const string& path, bool quiet) {
71  WIN32_FILE_ATTRIBUTE_DATA attrs;
72  if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &attrs)) {
73  DWORD err = GetLastError();
74  if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
75  return 0;
76  if (!quiet) {
77  Error("GetFileAttributesEx(%s): %s", path.c_str(),
78  GetLastErrorString().c_str());
79  }
80  return -1;
81  }
82  return TimeStampFromFileTime(attrs.ftLastWriteTime);
83 }
84 
85 #ifdef _MSC_VER
86 #pragma warning(push)
87 #pragma warning(disable: 4996) // GetVersionExA is deprecated post SDK 8.1.
88 #endif
89 bool IsWindows7OrLater() {
90  OSVERSIONINFO version_info = { sizeof(version_info) };
91  if (!GetVersionEx(&version_info))
92  Fatal("GetVersionEx: %s", GetLastErrorString().c_str());
93  return version_info.dwMajorVersion > 6 ||
94  (version_info.dwMajorVersion == 6 && version_info.dwMinorVersion >= 1);
95 }
96 #ifdef _MSC_VER
97 #pragma warning(pop)
98 #endif
99 
100 bool StatAllFilesInDir(const string& dir, map<string, TimeStamp>* stamps,
101  bool quiet) {
102  // FindExInfoBasic is 30% faster than FindExInfoStandard.
103  static bool can_use_basic_info = IsWindows7OrLater();
104  // This is not in earlier SDKs.
105  const FINDEX_INFO_LEVELS kFindExInfoBasic =
106  static_cast<FINDEX_INFO_LEVELS>(1);
107  FINDEX_INFO_LEVELS level =
108  can_use_basic_info ? kFindExInfoBasic : FindExInfoStandard;
109  WIN32_FIND_DATAA ffd;
110  HANDLE find_handle = FindFirstFileExA((dir + "\\*").c_str(), level, &ffd,
111  FindExSearchNameMatch, NULL, 0);
112 
113  if (find_handle == INVALID_HANDLE_VALUE) {
114  DWORD err = GetLastError();
115  if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
116  return true;
117  if (!quiet) {
118  Error("FindFirstFileExA(%s): %s", dir.c_str(),
119  GetLastErrorString().c_str());
120  }
121  return false;
122  }
123  do {
124  string lowername = ffd.cFileName;
125  transform(lowername.begin(), lowername.end(), lowername.begin(), ::tolower);
126  stamps->insert(make_pair(lowername,
127  TimeStampFromFileTime(ffd.ftLastWriteTime)));
128  } while (FindNextFileA(find_handle, &ffd));
129  FindClose(find_handle);
130  return true;
131 }
132 #endif // _WIN32
133 
134 } // namespace
135 
136 // DiskInterface ---------------------------------------------------------------
137 
138 bool DiskInterface::MakeDirs(const string& path) {
139  string dir = DirName(path);
140  if (dir.empty())
141  return true; // Reached root; assume it's there.
142  TimeStamp mtime = Stat(dir);
143  if (mtime < 0)
144  return false; // Error.
145  if (mtime > 0)
146  return true; // Exists already; we're done.
147 
148  // Directory doesn't exist. Try creating its parent first.
149  bool success = MakeDirs(dir);
150  if (!success)
151  return false;
152  return MakeDir(dir);
153 }
154 
155 // RealDiskInterface -----------------------------------------------------------
156 
157 TimeStamp RealDiskInterface::Stat(const string& path) const {
158 #ifdef _WIN32
159  // MSDN: "Naming Files, Paths, and Namespaces"
160  // http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
161  if (!path.empty() && path[0] != '\\' && path.size() > MAX_PATH) {
162  if (!quiet_) {
163  Error("Stat(%s): Filename longer than %i characters",
164  path.c_str(), MAX_PATH);
165  }
166  return -1;
167  }
168  if (!use_cache_)
169  return StatSingleFile(path, quiet_);
170 
171  string dir = DirName(path);
172  string base(path.substr(dir.size() ? dir.size() + 1 : 0));
173 
174  transform(dir.begin(), dir.end(), dir.begin(), ::tolower);
175  transform(base.begin(), base.end(), base.begin(), ::tolower);
176 
177  Cache::iterator ci = cache_.find(dir);
178  if (ci == cache_.end()) {
179  ci = cache_.insert(make_pair(dir, DirCache())).first;
180  if (!StatAllFilesInDir(dir.empty() ? "." : dir, &ci->second, quiet_)) {
181  cache_.erase(ci);
182  return -1;
183  }
184  }
185  DirCache::iterator di = ci->second.find(base);
186  return di != ci->second.end() ? di->second : 0;
187 #else
188  struct stat st;
189  if (stat(path.c_str(), &st) < 0) {
190  if (errno == ENOENT || errno == ENOTDIR)
191  return 0;
192  if (!quiet_) {
193  Error("stat(%s): %s", path.c_str(), strerror(errno));
194  }
195  return -1;
196  }
197  return st.st_mtime;
198 #endif
199 }
200 
201 bool RealDiskInterface::WriteFile(const string& path, const string& contents) {
202  FILE* fp = fopen(path.c_str(), "w");
203  if (fp == NULL) {
204  Error("WriteFile(%s): Unable to create file. %s",
205  path.c_str(), strerror(errno));
206  return false;
207  }
208 
209  if (fwrite(contents.data(), 1, contents.length(), fp) < contents.length()) {
210  Error("WriteFile(%s): Unable to write to the file. %s",
211  path.c_str(), strerror(errno));
212  fclose(fp);
213  return false;
214  }
215 
216  if (fclose(fp) == EOF) {
217  Error("WriteFile(%s): Unable to close the file. %s",
218  path.c_str(), strerror(errno));
219  return false;
220  }
221 
222  return true;
223 }
224 
225 bool RealDiskInterface::MakeDir(const string& path) {
226  if (::MakeDir(path) < 0) {
227  if (errno == EEXIST) {
228  return true;
229  }
230  Error("mkdir(%s): %s", path.c_str(), strerror(errno));
231  return false;
232  }
233  return true;
234 }
235 
236 string RealDiskInterface::ReadFile(const string& path, string* err) {
237  string contents;
238  int ret = ::ReadFile(path, &contents, err);
239  if (ret == -ENOENT) {
240  // Swallow ENOENT.
241  err->clear();
242  }
243  return contents;
244 }
245 
246 int RealDiskInterface::RemoveFile(const string& path) {
247  if (remove(path.c_str()) < 0) {
248  switch (errno) {
249  case ENOENT:
250  return 1;
251  default:
252  Error("remove(%s): %s", path.c_str(), strerror(errno));
253  return -1;
254  }
255  } else {
256  return 0;
257  }
258 }
259 
261 #ifdef _WIN32
262  use_cache_ = allow;
263  if (!use_cache_)
264  cache_.clear();
265 #endif
266 }
virtual string ReadFile(const string &path, string *err)
Read a file to a string. Fill in |err| on error.
bool MakeDirs(const string &path)
Create all the parent directories for path; like mkdir -p basename path.
int TimeStamp
Definition: timestamp.h:22
virtual bool WriteFile(const string &path, const string &contents)
Create a file, with the specified name and contents Returns true on success, false on failure...
virtual TimeStamp Stat(const string &path) const
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
virtual bool MakeDir(const string &path)
Create a directory, returning false on failure.
int ReadFile(const string &path, string *contents, string *err)
Read a file to a string (in text mode: with CRLF conversion on Windows).
Definition: util.cc:340
virtual int RemoveFile(const string &path)
Remove the file named path.
void Fatal(const char *msg,...)
Log a fatal message and exit.
Definition: util.cc:52
void AllowStatCache(bool allow)
Whether stat information can be cached. Only has an effect on Windows.
unsigned long long uint64_t
Definition: win32port.h:22
void Error(const char *msg,...)
Log an error message.
Definition: util.cc:79