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