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 <sstream>
27 #include <windows.h>
28 #include <direct.h> // _mkdir
29 #endif
30 
31 #include "metrics.h"
32 #include "util.h"
33 
34 namespace {
35 
36 string DirName(const string& path) {
37 #ifdef _WIN32
38  static const char kPathSeparators[] = "\\/";
39 #else
40  static const char kPathSeparators[] = "/";
41 #endif
42  static const char* const kEnd = kPathSeparators + sizeof(kPathSeparators) - 1;
43 
44  string::size_type slash_pos = path.find_last_of(kPathSeparators);
45  if (slash_pos == string::npos)
46  return string(); // Nothing to do.
47  while (slash_pos > 0 &&
48  std::find(kPathSeparators, kEnd, path[slash_pos - 1]) != kEnd)
49  --slash_pos;
50  return path.substr(0, slash_pos);
51 }
52 
53 int MakeDir(const string& path) {
54 #ifdef _WIN32
55  return _mkdir(path.c_str());
56 #else
57  return mkdir(path.c_str(), 0777);
58 #endif
59 }
60 
61 #ifdef _WIN32
62 TimeStamp TimeStampFromFileTime(const FILETIME& filetime) {
63  // FILETIME is in 100-nanosecond increments since the Windows epoch.
64  // We don't much care about epoch correctness but we do want the
65  // resulting value to fit in a 64-bit integer.
66  uint64_t mtime = ((uint64_t)filetime.dwHighDateTime << 32) |
67  ((uint64_t)filetime.dwLowDateTime);
68  // 1600 epoch -> 2000 epoch (subtract 400 years).
69  return (TimeStamp)mtime - 12622770400LL * (1000000000LL / 100);
70 }
71 
72 TimeStamp StatSingleFile(const string& path, string* err) {
73  WIN32_FILE_ATTRIBUTE_DATA attrs;
74  if (!GetFileAttributesExA(path.c_str(), GetFileExInfoStandard, &attrs)) {
75  DWORD win_err = GetLastError();
76  if (win_err == ERROR_FILE_NOT_FOUND || win_err == ERROR_PATH_NOT_FOUND)
77  return 0;
78  *err = "GetFileAttributesEx(" + path + "): " + GetLastErrorString();
79  return -1;
80  }
81  return TimeStampFromFileTime(attrs.ftLastWriteTime);
82 }
83 
84 bool IsWindows7OrLater() {
85  OSVERSIONINFOEX version_info =
86  { sizeof(OSVERSIONINFOEX), 6, 1, 0, 0, {0}, 0, 0, 0, 0, 0};
87  DWORDLONG comparison = 0;
88  VER_SET_CONDITION(comparison, VER_MAJORVERSION, VER_GREATER_EQUAL);
89  VER_SET_CONDITION(comparison, VER_MINORVERSION, VER_GREATER_EQUAL);
90  return VerifyVersionInfo(
91  &version_info, VER_MAJORVERSION | VER_MINORVERSION, comparison);
92 }
93 
94 bool StatAllFilesInDir(const string& dir, map<string, TimeStamp>* stamps,
95  string* err) {
96  // FindExInfoBasic is 30% faster than FindExInfoStandard.
97  static bool can_use_basic_info = IsWindows7OrLater();
98  // This is not in earlier SDKs.
99  const FINDEX_INFO_LEVELS kFindExInfoBasic =
100  static_cast<FINDEX_INFO_LEVELS>(1);
101  FINDEX_INFO_LEVELS level =
102  can_use_basic_info ? kFindExInfoBasic : FindExInfoStandard;
103  WIN32_FIND_DATAA ffd;
104  HANDLE find_handle = FindFirstFileExA((dir + "\\*").c_str(), level, &ffd,
105  FindExSearchNameMatch, NULL, 0);
106 
107  if (find_handle == INVALID_HANDLE_VALUE) {
108  DWORD win_err = GetLastError();
109  if (win_err == ERROR_FILE_NOT_FOUND || win_err == ERROR_PATH_NOT_FOUND)
110  return true;
111  *err = "FindFirstFileExA(" + dir + "): " + GetLastErrorString();
112  return false;
113  }
114  do {
115  string lowername = ffd.cFileName;
116  if (lowername == "..") {
117  // Seems to just copy the timestamp for ".." from ".", which is wrong.
118  // This is the case at least on NTFS under Windows 7.
119  continue;
120  }
121  transform(lowername.begin(), lowername.end(), lowername.begin(), ::tolower);
122  stamps->insert(make_pair(lowername,
123  TimeStampFromFileTime(ffd.ftLastWriteTime)));
124  } while (FindNextFileA(find_handle, &ffd));
125  FindClose(find_handle);
126  return true;
127 }
128 #endif // _WIN32
129 
130 } // namespace
131 
132 // DiskInterface ---------------------------------------------------------------
133 
134 bool DiskInterface::MakeDirs(const string& path) {
135  string dir = DirName(path);
136  if (dir.empty())
137  return true; // Reached root; assume it's there.
138  string err;
139  TimeStamp mtime = Stat(dir, &err);
140  if (mtime < 0) {
141  Error("%s", err.c_str());
142  return false;
143  }
144  if (mtime > 0)
145  return true; // Exists already; we're done.
146 
147  // Directory doesn't exist. Try creating its parent first.
148  bool success = MakeDirs(dir);
149  if (!success)
150  return false;
151  return MakeDir(dir);
152 }
153 
154 // RealDiskInterface -----------------------------------------------------------
155 
156 TimeStamp RealDiskInterface::Stat(const string& path, string* err) const {
157  METRIC_RECORD("node stat");
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  ostringstream err_stream;
163  err_stream << "Stat(" << path << "): Filename longer than " << MAX_PATH
164  << " characters";
165  *err = err_stream.str();
166  return -1;
167  }
168  if (!use_cache_)
169  return StatSingleFile(path, err);
170 
171  string dir = DirName(path);
172  string base(path.substr(dir.size() ? dir.size() + 1 : 0));
173  if (base == "..") {
174  // StatAllFilesInDir does not report any information for base = "..".
175  base = ".";
176  dir = path;
177  }
178 
179  transform(dir.begin(), dir.end(), dir.begin(), ::tolower);
180  transform(base.begin(), base.end(), base.begin(), ::tolower);
181 
182  Cache::iterator ci = cache_.find(dir);
183  if (ci == cache_.end()) {
184  ci = cache_.insert(make_pair(dir, DirCache())).first;
185  if (!StatAllFilesInDir(dir.empty() ? "." : dir, &ci->second, err)) {
186  cache_.erase(ci);
187  return -1;
188  }
189  }
190  DirCache::iterator di = ci->second.find(base);
191  return di != ci->second.end() ? di->second : 0;
192 #else
193  struct stat st;
194  if (stat(path.c_str(), &st) < 0) {
195  if (errno == ENOENT || errno == ENOTDIR)
196  return 0;
197  *err = "stat(" + path + "): " + strerror(errno);
198  return -1;
199  }
200  // Some users (Flatpak) set mtime to 0, this should be harmless
201  // and avoids conflicting with our return value of 0 meaning
202  // that it doesn't exist.
203  if (st.st_mtime == 0)
204  return 1;
205 #if defined(__APPLE__) && !defined(_POSIX_C_SOURCE)
206  return ((int64_t)st.st_mtimespec.tv_sec * 1000000000LL +
207  st.st_mtimespec.tv_nsec);
208 #elif (_POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 700 || defined(_BSD_SOURCE) || defined(_SVID_SOURCE) || \
209  defined(__BIONIC__) || (defined (__SVR4) && defined (__sun)) || defined(__FreeBSD__))
210  // For glibc, see "Timestamp files" in the Notes of http://www.kernel.org/doc/man-pages/online/pages/man2/stat.2.html
211  // newlib, uClibc and musl follow the kernel (or Cygwin) headers and define the right macro values above.
212  // For bsd, see https://github.com/freebsd/freebsd/blob/master/sys/sys/stat.h and similar
213  // For bionic, C and POSIX API is always enabled.
214  // For solaris, see https://docs.oracle.com/cd/E88353_01/html/E37841/stat-2.html.
215  return (int64_t)st.st_mtim.tv_sec * 1000000000LL + st.st_mtim.tv_nsec;
216 #elif defined(_AIX)
217  return (int64_t)st.st_mtime * 1000000000LL + st.st_mtime_n;
218 #else
219  return (int64_t)st.st_mtime * 1000000000LL + st.st_mtimensec;
220 #endif
221 #endif
222 }
223 
224 bool RealDiskInterface::WriteFile(const string& path, const string& contents) {
225  FILE* fp = fopen(path.c_str(), "w");
226  if (fp == NULL) {
227  Error("WriteFile(%s): Unable to create file. %s",
228  path.c_str(), strerror(errno));
229  return false;
230  }
231 
232  if (fwrite(contents.data(), 1, contents.length(), fp) < contents.length()) {
233  Error("WriteFile(%s): Unable to write to the file. %s",
234  path.c_str(), strerror(errno));
235  fclose(fp);
236  return false;
237  }
238 
239  if (fclose(fp) == EOF) {
240  Error("WriteFile(%s): Unable to close the file. %s",
241  path.c_str(), strerror(errno));
242  return false;
243  }
244 
245  return true;
246 }
247 
248 bool RealDiskInterface::MakeDir(const string& path) {
249  if (::MakeDir(path) < 0) {
250  if (errno == EEXIST) {
251  return true;
252  }
253  Error("mkdir(%s): %s", path.c_str(), strerror(errno));
254  return false;
255  }
256  return true;
257 }
258 
260  string* contents,
261  string* err) {
262  switch (::ReadFile(path, contents, err)) {
263  case 0: return Okay;
264  case -ENOENT: return NotFound;
265  default: return OtherError;
266  }
267 }
268 
269 int RealDiskInterface::RemoveFile(const string& path) {
270  if (remove(path.c_str()) < 0) {
271  switch (errno) {
272  case ENOENT:
273  return 1;
274  default:
275  Error("remove(%s): %s", path.c_str(), strerror(errno));
276  return -1;
277  }
278  } else {
279  return 0;
280  }
281 }
282 
284 #ifdef _WIN32
285  use_cache_ = allow;
286  if (!use_cache_)
287  cache_.clear();
288 #endif
289 }
virtual Status ReadFile(const string &path, string *contents, string *err)
Read and store in given string.
bool MakeDirs(const string &path)
Create all the parent directories for path; like mkdir -p basename path.
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...
signed long long int64_t
A 64-bit integer type.
Definition: win32port.h:28
virtual bool MakeDir(const string &path)
Create a directory, returning false on failure.
virtual TimeStamp Stat(const string &path, string *err) const
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
Status
Result of ReadFile.
virtual TimeStamp Stat(const string &path, string *err) const =0
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
int64_t TimeStamp
Definition: timestamp.h:31
#define METRIC_RECORD(name)
The primary interface to metrics.
Definition: metrics.h:85
virtual int RemoveFile(const string &path)
Remove the file named path.
void AllowStatCache(bool allow)
Whether stat information can be cached. Only has an effect on Windows.
unsigned long long uint64_t
Definition: win32port.h:29
virtual bool MakeDir(const string &path)=0
Create a directory, returning false on failure.
void Error(const char *msg,...)
Log an error message.
Definition: util.cc:84