diff options
Diffstat (limited to 'src/main/cpp/util')
-rw-r--r-- | src/main/cpp/util/file_platform.h | 18 | ||||
-rw-r--r-- | src/main/cpp/util/file_posix.cc | 14 | ||||
-rw-r--r-- | src/main/cpp/util/file_windows.cc | 17 |
3 files changed, 39 insertions, 10 deletions
diff --git a/src/main/cpp/util/file_platform.h b/src/main/cpp/util/file_platform.h index 91fc086627..2c07c3731d 100644 --- a/src/main/cpp/util/file_platform.h +++ b/src/main/cpp/util/file_platform.h @@ -54,12 +54,18 @@ bool PathExists(const std::string& path); // This is a wrapper around realpath(3). std::string MakeCanonical(const char *path); -// Returns true if the path exists and can be accessed to read/write as desired. -// -// If `exec` is true and the path refers to a file, it means the file must be -// executable; if the path is a directory, it means the directory must be -// openable. -bool CanAccess(const std::string& path, bool read, bool write, bool exec); +// Returns true if `path` exists, is a file or symlink to one, and is readable. +// Follows symlinks. +bool CanReadFile(const std::string &path); + +// Returns true if `path` exists, is a file or symlink to one, and is writable. +// Follows symlinks. +bool CanExecuteFile(const std::string &path); + +// Returns true if `path` exists, is a directory or symlink/junction to one, and +// is both readable and writable. +// Follows symlinks/junctions. +bool CanAccessDirectory(const std::string &path); // Returns true if `path` refers to a directory or a symlink/junction to one. bool IsDirectory(const std::string& path); diff --git a/src/main/cpp/util/file_posix.cc b/src/main/cpp/util/file_posix.cc index 466bae499d..c1a313a1f3 100644 --- a/src/main/cpp/util/file_posix.cc +++ b/src/main/cpp/util/file_posix.cc @@ -234,7 +234,7 @@ string MakeCanonical(const char *path) { } } -bool CanAccess(const string& path, bool read, bool write, bool exec) { +static bool CanAccess(const string &path, bool read, bool write, bool exec) { int mode = 0; if (read) { mode |= R_OK; @@ -248,6 +248,18 @@ bool CanAccess(const string& path, bool read, bool write, bool exec) { return access(path.c_str(), mode) == 0; } +bool CanReadFile(const std::string &path) { + return !IsDirectory(path) && CanAccess(path, true, false, false); +} + +bool CanExecuteFile(const std::string &path) { + return !IsDirectory(path) && CanAccess(path, false, false, true); +} + +bool CanAccessDirectory(const std::string &path) { + return IsDirectory(path) && CanAccess(path, true, true, true); +} + #ifndef __CYGWIN__ bool IsDirectory(const string& path) { struct stat buf; diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc index 3a5b5ac8e4..4293d5ead7 100644 --- a/src/main/cpp/util/file_windows.cc +++ b/src/main/cpp/util/file_windows.cc @@ -548,12 +548,23 @@ bool PathExists(const string& path) { } #ifdef COMPILER_MSVC -bool CanAccess(const string& path, bool read, bool write, bool exec) { +bool CanReadFile(const std::string& path) { // TODO(bazel-team): implement this. - pdie(255, "blaze_util::CanAccess is not implemented on Windows"); + pdie(255, "not implemented on Windows"); + return false; +} + +bool CanExecuteFile(const std::string& path) { + // TODO(bazel-team): implement this. + pdie(255, "not implemented on Windows"); + return false; +} + +bool CanAccessDirectory(const std::string& path) { + // TODO(bazel-team): implement this. + pdie(255, "not implemented on Windows"); return false; } -#else // not COMPILER_MSVC #endif // COMPILER_MSVC static bool IsDirectoryW(const wstring& path) { |