| // Copyright 2011 Google Inc. All Rights Reserved. |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| #include "disk_interface.h" |
| |
| #include <algorithm> |
| |
| #include <errno.h> |
| #include <stdio.h> |
| #include <string.h> |
| #include <sys/stat.h> |
| #include <sys/types.h> |
| |
| #ifdef _WIN32 |
| #include <sstream> |
| #include <windows.h> |
| #include <direct.h> // _mkdir |
| #else |
| #include <unistd.h> |
| #endif |
| |
| #include "metrics.h" |
| #include "util.h" |
| |
| using namespace std; |
| |
| namespace { |
| |
| string DirName(const string& path) { |
| #ifdef _WIN32 |
| static const char kPathSeparators[] = "\\/"; |
| #else |
| static const char kPathSeparators[] = "/"; |
| #endif |
| static const char* const kEnd = kPathSeparators + sizeof(kPathSeparators) - 1; |
| |
| string::size_type slash_pos = path.find_last_of(kPathSeparators); |
| if (slash_pos == string::npos) |
| return string(); // Nothing to do. |
| while (slash_pos > 0 && |
| std::find(kPathSeparators, kEnd, path[slash_pos - 1]) != kEnd) |
| --slash_pos; |
| return path.substr(0, slash_pos); |
| } |
| |
| int MakeDir(const string& path) { |
| #ifdef _WIN32 |
| return _mkdir(path.c_str()); |
| #else |
| return mkdir(path.c_str(), 0777); |
| #endif |
| } |
| |
| } // namespace |
| |
| // DiskInterface --------------------------------------------------------------- |
| |
| bool DiskInterface::MakeDirs(const string& path) { |
| string dir = DirName(path); |
| if (dir.empty()) |
| return true; // Reached root; assume it's there. |
| string err; |
| TimeStamp mtime = Stat(dir, &err); |
| if (mtime < 0) { |
| Error("%s", err.c_str()); |
| return false; |
| } |
| if (mtime > 0) |
| return true; // Exists already; we're done. |
| |
| // Directory doesn't exist. Try creating its parent first. |
| bool success = MakeDirs(dir); |
| if (!success) |
| return false; |
| return MakeDir(dir); |
| } |
| |
| // RealDiskInterface ----------------------------------------------------------- |
| |
| TimeStamp RealDiskInterface::Stat(const string& path, string* err) const { |
| METRIC_RECORD("node stat"); |
| return stat_cache_.Stat(path, err); |
| } |
| |
| bool RealDiskInterface::WriteFile(const string& path, const string& contents) { |
| FILE* fp = fopen(path.c_str(), "w"); |
| if (fp == NULL) { |
| Error("WriteFile(%s): Unable to create file. %s", |
| path.c_str(), strerror(errno)); |
| return false; |
| } |
| |
| stat_cache_.Invalidate(path); |
| if (fwrite(contents.data(), 1, contents.length(), fp) < contents.length()) { |
| Error("WriteFile(%s): Unable to write to the file. %s", |
| path.c_str(), strerror(errno)); |
| fclose(fp); |
| return false; |
| } |
| |
| if (fclose(fp) == EOF) { |
| Error("WriteFile(%s): Unable to close the file. %s", |
| path.c_str(), strerror(errno)); |
| return false; |
| } |
| |
| return true; |
| } |
| |
| bool RealDiskInterface::MakeDir(const string& path) { |
| int ret = ::MakeDir(path); |
| if (ret < 0 && errno == EEXIST) |
| return true; |
| |
| int saved_errno = errno; |
| // Invalidate just in case the call modified something on the disk. |
| stat_cache_.Invalidate(path); |
| if (ret < 0) { |
| Error("mkdir(%s): %s", path.c_str(), strerror(saved_errno)); |
| return false; |
| } |
| return true; |
| } |
| |
| FileReader::Status RealDiskInterface::ReadFile(const string& path, |
| string* contents, |
| string* err) { |
| switch (::ReadFile(path, contents, err)) { |
| case 0: return Okay; |
| case -ENOENT: return NotFound; |
| default: return OtherError; |
| } |
| } |
| |
| int RealDiskInterface::RemoveFile(const string& path) { |
| stat_cache_.Invalidate(path); |
| #ifdef _WIN32 |
| DWORD attributes = GetFileAttributesA(path.c_str()); |
| if (attributes == INVALID_FILE_ATTRIBUTES) { |
| DWORD win_err = GetLastError(); |
| if (win_err == ERROR_FILE_NOT_FOUND || win_err == ERROR_PATH_NOT_FOUND) { |
| return 1; |
| } |
| } else if (attributes & FILE_ATTRIBUTE_READONLY) { |
| // On non-Windows systems, remove() will happily delete read-only files. |
| // On Windows Ninja should behave the same: |
| // https://github.com/ninja-build/ninja/issues/1886 |
| // Skip error checking. If this fails, accept whatever happens below. |
| SetFileAttributesA(path.c_str(), attributes & ~FILE_ATTRIBUTE_READONLY); |
| } |
| if (attributes & FILE_ATTRIBUTE_DIRECTORY) { |
| // remove() deletes both files and directories. On Windows we have to |
| // select the correct function (DeleteFile will yield Permission Denied when |
| // used on a directory) |
| // This fixes the behavior of ninja -t clean in some cases |
| // https://github.com/ninja-build/ninja/issues/828 |
| if (!RemoveDirectoryA(path.c_str())) { |
| DWORD win_err = GetLastError(); |
| if (win_err == ERROR_FILE_NOT_FOUND || win_err == ERROR_PATH_NOT_FOUND) { |
| return 1; |
| } |
| // Report remove(), not RemoveDirectory(), for cross-platform consistency. |
| Error("remove(%s): %s", path.c_str(), GetLastErrorString().c_str()); |
| return -1; |
| } |
| } else { |
| if (!DeleteFileA(path.c_str())) { |
| DWORD win_err = GetLastError(); |
| if (win_err == ERROR_FILE_NOT_FOUND || win_err == ERROR_PATH_NOT_FOUND) { |
| return 1; |
| } |
| // Report as remove(), not DeleteFile(), for cross-platform consistency. |
| Error("remove(%s): %s", path.c_str(), GetLastErrorString().c_str()); |
| return -1; |
| } |
| } |
| #else |
| if (remove(path.c_str()) < 0) { |
| switch (errno) { |
| case ENOENT: |
| return 1; |
| default: |
| Error("remove(%s): %s", path.c_str(), strerror(errno)); |
| return -1; |
| } |
| } |
| #endif |
| return 0; |
| } |
| |
| void RealDiskInterface::AllowStatCache(bool allow) { |
| stat_cache_.Enable(allow); |
| } |
| |
| void RealDiskInterface::Sync() { |
| stat_cache_.Sync(); |
| } |
| |
| void RealDiskInterface::FlushCache() { |
| stat_cache_.Flush(); |
| } |