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.
15 #include "disk_interface.h"
23 #include <sys/types.h>
28 #include <direct.h> // _mkdir
35 string DirName(const string& path) {
37 const char kPathSeparators[] = "\\/";
39 const char kPathSeparators[] = "/";
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)
48 return path.substr(0, slash_pos);
51 int MakeDir(const string& path) {
53 return _mkdir(path.c_str());
55 return mkdir(path.c_str(), 0777);
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;
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)
77 *err = "GetFileAttributesEx(" + path + "): " + GetLastErrorString();
80 return TimeStampFromFileTime(attrs.ftLastWriteTime);
85 #pragma warning(disable: 4996) // GetVersionExA is deprecated post SDK 8.1.
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);
98 bool StatAllFilesInDir(const string& dir, map<string, TimeStamp>* stamps,
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);
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)
115 *err = "FindFirstFileExA(" + dir + "): " + GetLastErrorString();
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);
131 // DiskInterface ---------------------------------------------------------------
133 bool DiskInterface::MakeDirs(const string& path) {
134 string dir = DirName(path);
136 return true; // Reached root; assume it's there.
138 TimeStamp mtime = Stat(dir, &err);
140 Error("%s", err.c_str());
144 return true; // Exists already; we're done.
146 // Directory doesn't exist. Try creating its parent first.
147 bool success = MakeDirs(dir);
153 // RealDiskInterface -----------------------------------------------------------
155 TimeStamp RealDiskInterface::Stat(const string& path, string* err) const {
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
163 *err = err_stream.str();
167 return StatSingleFile(path, err);
169 string dir = DirName(path);
170 string base(path.substr(dir.size() ? dir.size() + 1 : 0));
172 transform(dir.begin(), dir.end(), dir.begin(), ::tolower);
173 transform(base.begin(), base.end(), base.begin(), ::tolower);
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)) {
183 DirCache::iterator di = ci->second.find(base);
184 return di != ci->second.end() ? di->second : 0;
187 if (stat(path.c_str(), &st) < 0) {
188 if (errno == ENOENT || errno == ENOTDIR)
190 *err = "stat(" + path + "): " + strerror(errno);
197 bool RealDiskInterface::WriteFile(const string& path, const string& contents) {
198 FILE* fp = fopen(path.c_str(), "w");
200 Error("WriteFile(%s): Unable to create file. %s",
201 path.c_str(), strerror(errno));
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));
212 if (fclose(fp) == EOF) {
213 Error("WriteFile(%s): Unable to close the file. %s",
214 path.c_str(), strerror(errno));
221 bool RealDiskInterface::MakeDir(const string& path) {
222 if (::MakeDir(path) < 0) {
223 if (errno == EEXIST) {
226 Error("mkdir(%s): %s", path.c_str(), strerror(errno));
232 FileReader::Status RealDiskInterface::ReadFile(const string& path,
235 switch (::ReadFile(path, contents, err)) {
237 case -ENOENT: return NotFound;
238 default: return OtherError;
242 int RealDiskInterface::RemoveFile(const string& path) {
243 if (remove(path.c_str()) < 0) {
248 Error("remove(%s): %s", path.c_str(), strerror(errno));
256 void RealDiskInterface::AllowStatCache(bool allow) {