Ninja
disk_interface.cc
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //     http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "disk_interface.h"
00016 
00017 #include <algorithm>
00018 
00019 #include <errno.h>
00020 #include <stdio.h>
00021 #include <string.h>
00022 #include <sys/stat.h>
00023 #include <sys/types.h>
00024 
00025 #ifdef _WIN32
00026 #include <windows.h>
00027 #include <direct.h>  // _mkdir
00028 #endif
00029 
00030 #include "util.h"
00031 
00032 namespace {
00033 
00034 string DirName(const string& path) {
00035 #ifdef _WIN32
00036   const char kPathSeparators[] = "\\/";
00037 #else
00038   const char kPathSeparators[] = "/";
00039 #endif
00040   string::size_type slash_pos = path.find_last_of(kPathSeparators);
00041   if (slash_pos == string::npos)
00042     return string();  // Nothing to do.
00043   const char* const kEnd = kPathSeparators + strlen(kPathSeparators);
00044   while (slash_pos > 0 &&
00045          std::find(kPathSeparators, kEnd, path[slash_pos - 1]) != kEnd)
00046     --slash_pos;
00047   return path.substr(0, slash_pos);
00048 }
00049 
00050 int MakeDir(const string& path) {
00051 #ifdef _WIN32
00052   return _mkdir(path.c_str());
00053 #else
00054   return mkdir(path.c_str(), 0777);
00055 #endif
00056 }
00057 
00058 #ifdef _WIN32
00059 TimeStamp TimeStampFromFileTime(const FILETIME& filetime) {
00060   // FILETIME is in 100-nanosecond increments since the Windows epoch.
00061   // We don't much care about epoch correctness but we do want the
00062   // resulting value to fit in an integer.
00063   uint64_t mtime = ((uint64_t)filetime.dwHighDateTime << 32) |
00064     ((uint64_t)filetime.dwLowDateTime);
00065   mtime /= 1000000000LL / 100; // 100ns -> s.
00066   mtime -= 12622770400LL;  // 1600 epoch -> 2000 epoch (subtract 400 years).
00067   return (TimeStamp)mtime;
00068 }
00069 
00070 TimeStamp StatSingleFile(const string& path, bool quiet) {
00071   WIN32_FILE_ATTRIBUTE_DATA attrs;
00072   if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &attrs)) {
00073     DWORD err = GetLastError();
00074     if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
00075       return 0;
00076     if (!quiet) {
00077       Error("GetFileAttributesEx(%s): %s", path.c_str(),
00078             GetLastErrorString().c_str());
00079     }
00080     return -1;
00081   }
00082   return TimeStampFromFileTime(attrs.ftLastWriteTime);
00083 }
00084 
00085 #pragma warning(push)
00086 #pragma warning(disable: 4996)  // GetVersionExA is deprecated post SDK 8.1.
00087 bool IsWindows7OrLater() {
00088   OSVERSIONINFO version_info = { sizeof(version_info) };
00089   if (!GetVersionEx(&version_info))
00090     Fatal("GetVersionEx: %s", GetLastErrorString().c_str());
00091   return version_info.dwMajorVersion > 6 ||
00092          version_info.dwMajorVersion == 6 && version_info.dwMinorVersion >= 1;
00093 }
00094 #pragma warning(pop)
00095 
00096 bool StatAllFilesInDir(const string& dir, map<string, TimeStamp>* stamps,
00097                        bool quiet) {
00098   // FindExInfoBasic is 30% faster than FindExInfoStandard.
00099   static bool can_use_basic_info = IsWindows7OrLater();
00100   // This is not in earlier SDKs.
00101   const FINDEX_INFO_LEVELS kFindExInfoBasic =
00102       static_cast<FINDEX_INFO_LEVELS>(1);
00103   FINDEX_INFO_LEVELS level =
00104       can_use_basic_info ? kFindExInfoBasic : FindExInfoStandard;
00105   WIN32_FIND_DATAA ffd;
00106   HANDLE find_handle = FindFirstFileExA((dir + "\\*").c_str(), level, &ffd,
00107                                         FindExSearchNameMatch, NULL, 0);
00108 
00109   if (find_handle == INVALID_HANDLE_VALUE) {
00110     DWORD err = GetLastError();
00111     if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
00112       return true;
00113     if (!quiet) {
00114       Error("FindFirstFileExA(%s): %s", dir.c_str(),
00115             GetLastErrorString().c_str());
00116     }
00117     return false;
00118   }
00119   do {
00120     string lowername = ffd.cFileName;
00121     transform(lowername.begin(), lowername.end(), lowername.begin(), ::tolower);
00122     stamps->insert(make_pair(lowername,
00123                              TimeStampFromFileTime(ffd.ftLastWriteTime)));
00124   } while (FindNextFileA(find_handle, &ffd));
00125   FindClose(find_handle);
00126   return true;
00127 }
00128 #endif  // _WIN32
00129 
00130 }  // namespace
00131 
00132 // DiskInterface ---------------------------------------------------------------
00133 
00134 bool DiskInterface::MakeDirs(const string& path) {
00135   string dir = DirName(path);
00136   if (dir.empty())
00137     return true;  // Reached root; assume it's there.
00138   TimeStamp mtime = Stat(dir);
00139   if (mtime < 0)
00140     return false;  // Error.
00141   if (mtime > 0)
00142     return true;  // Exists already; we're done.
00143 
00144   // Directory doesn't exist.  Try creating its parent first.
00145   bool success = MakeDirs(dir);
00146   if (!success)
00147     return false;
00148   return MakeDir(dir);
00149 }
00150 
00151 // RealDiskInterface -----------------------------------------------------------
00152 
00153 TimeStamp RealDiskInterface::Stat(const string& path) const {
00154 #ifdef _WIN32
00155   // MSDN: "Naming Files, Paths, and Namespaces"
00156   // http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
00157   if (!path.empty() && path[0] != '\\' && path.size() > MAX_PATH) {
00158     if (!quiet_) {
00159       Error("Stat(%s): Filename longer than %i characters",
00160             path.c_str(), MAX_PATH);
00161     }
00162     return -1;
00163   }
00164   if (!use_cache_)
00165     return StatSingleFile(path, quiet_);
00166 
00167   string dir = DirName(path);
00168   string base(path.substr(dir.size() ? dir.size() + 1 : 0));
00169 
00170   transform(dir.begin(), dir.end(), dir.begin(), ::tolower);
00171   transform(base.begin(), base.end(), base.begin(), ::tolower);
00172 
00173   Cache::iterator ci = cache_.find(dir);
00174   if (ci == cache_.end()) {
00175     ci = cache_.insert(make_pair(dir, DirCache())).first;
00176     if (!StatAllFilesInDir(dir.empty() ? "." : dir, &ci->second, quiet_)) {
00177       cache_.erase(ci);
00178       return -1;
00179     }
00180   }
00181   DirCache::iterator di = ci->second.find(base);
00182   return di != ci->second.end() ? di->second : 0;
00183 #else
00184   struct stat st;
00185   if (stat(path.c_str(), &st) < 0) {
00186     if (errno == ENOENT || errno == ENOTDIR)
00187       return 0;
00188     if (!quiet_) {
00189       Error("stat(%s): %s", path.c_str(), strerror(errno));
00190     }
00191     return -1;
00192   }
00193   return st.st_mtime;
00194 #endif
00195 }
00196 
00197 bool RealDiskInterface::WriteFile(const string& path, const string& contents) {
00198   FILE* fp = fopen(path.c_str(), "w");
00199   if (fp == NULL) {
00200     Error("WriteFile(%s): Unable to create file. %s",
00201           path.c_str(), strerror(errno));
00202     return false;
00203   }
00204 
00205   if (fwrite(contents.data(), 1, contents.length(), fp) < contents.length())  {
00206     Error("WriteFile(%s): Unable to write to the file. %s",
00207           path.c_str(), strerror(errno));
00208     fclose(fp);
00209     return false;
00210   }
00211 
00212   if (fclose(fp) == EOF) {
00213     Error("WriteFile(%s): Unable to close the file. %s",
00214           path.c_str(), strerror(errno));
00215     return false;
00216   }
00217 
00218   return true;
00219 }
00220 
00221 bool RealDiskInterface::MakeDir(const string& path) {
00222   if (::MakeDir(path) < 0) {
00223     if (errno == EEXIST) {
00224       return true;
00225     }
00226     Error("mkdir(%s): %s", path.c_str(), strerror(errno));
00227     return false;
00228   }
00229   return true;
00230 }
00231 
00232 string RealDiskInterface::ReadFile(const string& path, string* err) {
00233   string contents;
00234   int ret = ::ReadFile(path, &contents, err);
00235   if (ret == -ENOENT) {
00236     // Swallow ENOENT.
00237     err->clear();
00238   }
00239   return contents;
00240 }
00241 
00242 int RealDiskInterface::RemoveFile(const string& path) {
00243   if (remove(path.c_str()) < 0) {
00244     switch (errno) {
00245       case ENOENT:
00246         return 1;
00247       default:
00248         Error("remove(%s): %s", path.c_str(), strerror(errno));
00249         return -1;
00250     }
00251   } else {
00252     return 0;
00253   }
00254 }
00255 
00256 void RealDiskInterface::AllowStatCache(bool allow) {
00257 #ifdef _WIN32
00258   use_cache_ = allow;
00259   if (!use_cache_)
00260     cache_.clear();
00261 #endif
00262 }