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 
DirName(const string & path)36 string DirName(const string& path) {
37 #ifdef _WIN32
38   const char kPathSeparators[] = "\\/";
39 #else
40   const char kPathSeparators[] = "/";
41 #endif
42   string::size_type slash_pos = path.find_last_of(kPathSeparators);
43   if (slash_pos == string::npos)
44     return string();  // Nothing to do.
45   const char* const kEnd = kPathSeparators + strlen(kPathSeparators);
46   while (slash_pos > 0 &&
47          std::find(kPathSeparators, kEnd, path[slash_pos - 1]) != kEnd)
48     --slash_pos;
49   return path.substr(0, slash_pos);
50 }
51 
MakeDir(const string & path)52 int MakeDir(const string& path) {
53 #ifdef _WIN32
54   return _mkdir(path.c_str());
55 #else
56   return mkdir(path.c_str(), 0777);
57 #endif
58 }
59 
60 #ifdef _WIN32
TimeStampFromFileTime(const FILETIME & filetime)61 TimeStamp TimeStampFromFileTime(const FILETIME& filetime) {
62   // FILETIME is in 100-nanosecond increments since the Windows epoch.
63   // We don't much care about epoch correctness but we do want the
64   // resulting value to fit in an integer.
65   uint64_t mtime = ((uint64_t)filetime.dwHighDateTime << 32) |
66     ((uint64_t)filetime.dwLowDateTime);
67   mtime /= 1000000000LL / 100; // 100ns -> s.
68   mtime -= 12622770400LL;  // 1600 epoch -> 2000 epoch (subtract 400 years).
69   return (TimeStamp)mtime;
70 }
71 
StatSingleFile(const string & path,string * err)72 TimeStamp StatSingleFile(const string& path, string* err) {
73   WIN32_FILE_ATTRIBUTE_DATA attrs;
74   if (!GetFileAttributesEx(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 
IsWindows7OrLater()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 
StatAllFilesInDir(const string & dir,map<string,TimeStamp> * stamps,string * err)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     transform(lowername.begin(), lowername.end(), lowername.begin(), ::tolower);
117     stamps->insert(make_pair(lowername,
118                              TimeStampFromFileTime(ffd.ftLastWriteTime)));
119   } while (FindNextFileA(find_handle, &ffd));
120   FindClose(find_handle);
121   return true;
122 }
123 #endif  // _WIN32
124 
125 }  // namespace
126 
127 // DiskInterface ---------------------------------------------------------------
128 
MakeDirs(const string & path)129 bool DiskInterface::MakeDirs(const string& path) {
130   string dir = DirName(path);
131   if (dir.empty())
132     return true;  // Reached root; assume it's there.
133   string err;
134   TimeStamp mtime = Stat(dir, &err);
135   if (mtime < 0) {
136     Error("%s", err.c_str());
137     return false;
138   }
139   if (mtime > 0)
140     return true;  // Exists already; we're done.
141 
142   // Directory doesn't exist.  Try creating its parent first.
143   bool success = MakeDirs(dir);
144   if (!success)
145     return false;
146   return MakeDir(dir);
147 }
148 
149 // RealDiskInterface -----------------------------------------------------------
150 
Stat(const string & path,string * err) const151 TimeStamp RealDiskInterface::Stat(const string& path, string* err) const {
152   METRIC_RECORD("node stat");
153 #ifdef _WIN32
154   // MSDN: "Naming Files, Paths, and Namespaces"
155   // http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
156   if (!path.empty() && path[0] != '\\' && path.size() > MAX_PATH) {
157     ostringstream err_stream;
158     err_stream << "Stat(" << path << "): Filename longer than " << MAX_PATH
159                << " characters";
160     *err = err_stream.str();
161     return -1;
162   }
163   if (!use_cache_)
164     return StatSingleFile(path, err);
165 
166   string dir = DirName(path);
167   string base(path.substr(dir.size() ? dir.size() + 1 : 0));
168 
169   transform(dir.begin(), dir.end(), dir.begin(), ::tolower);
170   transform(base.begin(), base.end(), base.begin(), ::tolower);
171 
172   Cache::iterator ci = cache_.find(dir);
173   if (ci == cache_.end()) {
174     ci = cache_.insert(make_pair(dir, DirCache())).first;
175     if (!StatAllFilesInDir(dir.empty() ? "." : dir, &ci->second, err)) {
176       cache_.erase(ci);
177       return -1;
178     }
179   }
180   DirCache::iterator di = ci->second.find(base);
181   return di != ci->second.end() ? di->second : 0;
182 #else
183   struct stat st;
184   if (stat(path.c_str(), &st) < 0) {
185     if (errno == ENOENT || errno == ENOTDIR)
186       return 0;
187     *err = "stat(" + path + "): " + strerror(errno);
188     return -1;
189   }
190   // Some users (Flatpak) set mtime to 0, this should be harmless
191   // and avoids conflicting with our return value of 0 meaning
192   // that it doesn't exist.
193   if (st.st_mtime == 0)
194     return 1;
195   return st.st_mtime;
196 #endif
197 }
198 
WriteFile(const string & path,const string & contents)199 bool RealDiskInterface::WriteFile(const string& path, const string& contents) {
200   FILE* fp = fopen(path.c_str(), "w");
201   if (fp == NULL) {
202     Error("WriteFile(%s): Unable to create file. %s",
203           path.c_str(), strerror(errno));
204     return false;
205   }
206 
207   if (fwrite(contents.data(), 1, contents.length(), fp) < contents.length())  {
208     Error("WriteFile(%s): Unable to write to the file. %s",
209           path.c_str(), strerror(errno));
210     fclose(fp);
211     return false;
212   }
213 
214   if (fclose(fp) == EOF) {
215     Error("WriteFile(%s): Unable to close the file. %s",
216           path.c_str(), strerror(errno));
217     return false;
218   }
219 
220   return true;
221 }
222 
MakeDir(const string & path)223 bool RealDiskInterface::MakeDir(const string& path) {
224   if (::MakeDir(path) < 0) {
225     if (errno == EEXIST) {
226       return true;
227     }
228     Error("mkdir(%s): %s", path.c_str(), strerror(errno));
229     return false;
230   }
231   return true;
232 }
233 
ReadFile(const string & path,string * contents,string * err)234 FileReader::Status RealDiskInterface::ReadFile(const string& path,
235                                                string* contents,
236                                                string* err) {
237   switch (::ReadFile(path, contents, err)) {
238   case 0:       return Okay;
239   case -ENOENT: return NotFound;
240   default:      return OtherError;
241   }
242 }
243 
RemoveFile(const string & path)244 int RealDiskInterface::RemoveFile(const string& path) {
245   if (remove(path.c_str()) < 0) {
246     switch (errno) {
247       case ENOENT:
248         return 1;
249       default:
250         Error("remove(%s): %s", path.c_str(), strerror(errno));
251         return -1;
252     }
253   } else {
254     return 0;
255   }
256 }
257 
AllowStatCache(bool allow)258 void RealDiskInterface::AllowStatCache(bool allow) {
259 #ifdef _WIN32
260   use_cache_ = allow;
261   if (!use_cache_)
262     cache_.clear();
263 #endif
264 }
265