23 #include <sys/types.h> 34 string DirName(
const string& path) {
36 const char kPathSeparators[] =
"\\/";
38 const char kPathSeparators[] =
"/";
40 string::size_type slash_pos = path.find_last_of(kPathSeparators);
41 if (slash_pos == string::npos)
43 const char*
const kEnd = kPathSeparators + strlen(kPathSeparators);
44 while (slash_pos > 0 &&
45 std::find(kPathSeparators, kEnd, path[slash_pos - 1]) != kEnd)
47 return path.substr(0, slash_pos);
50 int MakeDir(
const string& path) {
52 return _mkdir(path.c_str());
54 return mkdir(path.c_str(), 0777);
59 TimeStamp TimeStampFromFileTime(
const FILETIME& filetime) {
65 mtime /= 1000000000LL / 100;
66 mtime -= 12622770400LL;
70 TimeStamp StatSingleFile(
const string& path,
bool quiet) {
71 WIN32_FILE_ATTRIBUTE_DATA attrs;
72 if (!GetFileAttributesEx(path.c_str(), GetFileExInfoStandard, &attrs)) {
73 DWORD err = GetLastError();
74 if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
77 Error(
"GetFileAttributesEx(%s): %s", path.c_str(),
78 GetLastErrorString().c_str());
82 return TimeStampFromFileTime(attrs.ftLastWriteTime);
86 #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;
96 bool StatAllFilesInDir(
const string& dir, map<string, TimeStamp>* stamps,
99 static bool can_use_basic_info = IsWindows7OrLater();
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);
109 if (find_handle == INVALID_HANDLE_VALUE) {
110 DWORD err = GetLastError();
111 if (err == ERROR_FILE_NOT_FOUND || err == ERROR_PATH_NOT_FOUND)
114 Error(
"FindFirstFileExA(%s): %s", dir.c_str(),
115 GetLastErrorString().c_str());
120 string lowername = ffd.cFileName;
121 transform(lowername.begin(), lowername.end(), lowername.begin(), ::tolower);
122 stamps->insert(make_pair(lowername,
123 TimeStampFromFileTime(ffd.ftLastWriteTime)));
124 }
while (FindNextFileA(find_handle, &ffd));
125 FindClose(find_handle);
135 string dir = DirName(path);
145 bool success = MakeDirs(dir);
157 if (!path.empty() && path[0] !=
'\\' && path.size() > MAX_PATH) {
159 Error(
"Stat(%s): Filename longer than %i characters",
160 path.c_str(), MAX_PATH);
165 return StatSingleFile(path, quiet_);
167 string dir = DirName(path);
168 string base(path.substr(dir.size() ? dir.size() + 1 : 0));
170 transform(dir.begin(), dir.end(), dir.begin(), ::tolower);
171 transform(base.begin(), base.end(), base.begin(), ::tolower);
173 Cache::iterator ci = cache_.find(dir);
174 if (ci == cache_.end()) {
175 ci = cache_.insert(make_pair(dir, DirCache())).first;
176 if (!StatAllFilesInDir(dir.empty() ?
"." : dir, &ci->second, quiet_)) {
181 DirCache::iterator di = ci->second.find(base);
182 return di != ci->second.end() ? di->second : 0;
185 if (stat(path.c_str(), &st) < 0) {
186 if (errno == ENOENT || errno == ENOTDIR)
189 Error(
"stat(%s): %s", path.c_str(), strerror(errno));
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));
222 if (::MakeDir(path) < 0) {
223 if (errno == EEXIST) {
226 Error(
"mkdir(%s): %s", path.c_str(), strerror(errno));
235 if (ret == -ENOENT) {
243 if (
remove(path.c_str()) < 0) {
248 Error(
"remove(%s): %s", path.c_str(), strerror(errno));
virtual string ReadFile(const string &path, string *err)
Read a file to a string. Fill in |err| on error.
bool MakeDirs(const string &path)
Create all the parent directories for path; like mkdir -p basename path.
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 TimeStamp Stat(const string &path) const
stat() a file, returning the mtime, or 0 if missing and -1 on other errors.
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).
virtual int RemoveFile(const string &path)
Remove the file named path.
void Fatal(const char *msg,...)
Log a fatal message and exit.
void AllowStatCache(bool allow)
Whether stat information can be cached. Only has an effect on Windows.
unsigned long long uint64_t
void Error(const char *msg,...)
Log an error message.