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