diff options
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/cpp/blaze_util.cc | 6 | ||||
-rw-r--r-- | src/main/cpp/util/file_platform.h | 2 | ||||
-rw-r--r-- | src/main/cpp/util/file_posix.cc | 7 | ||||
-rw-r--r-- | src/main/cpp/util/file_windows.cc | 41 |
4 files changed, 34 insertions, 22 deletions
diff --git a/src/main/cpp/blaze_util.cc b/src/main/cpp/blaze_util.cc index d8a5af6b7b..a5268e646b 100644 --- a/src/main/cpp/blaze_util.cc +++ b/src/main/cpp/blaze_util.cc @@ -41,12 +41,12 @@ const unsigned int kPostShutdownGracePeriodSeconds = 60; const unsigned int kPostKillGracePeriodSeconds = 10; -string MakeAbsolute(const string &path) { +string MakeAbsolute(const string &p) { + string path = ConvertPath(p); if (path.empty()) { return blaze_util::GetCwd(); } - - if (blaze_util::IsAbsolute(path)) { + if (blaze_util::IsDevNull(path.c_str()) || blaze_util::IsAbsolute(path)) { return path; } diff --git a/src/main/cpp/util/file_platform.h b/src/main/cpp/util/file_platform.h index db217f2dee..0a70c1a39c 100644 --- a/src/main/cpp/util/file_platform.h +++ b/src/main/cpp/util/file_platform.h @@ -162,6 +162,8 @@ bool CanExecuteFile(const std::string &path); // Follows symlinks/junctions. bool CanAccessDirectory(const std::string &path); +bool IsDevNull(const char *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 daa465af94..6199d046cc 100644 --- a/src/main/cpp/util/file_posix.cc +++ b/src/main/cpp/util/file_posix.cc @@ -12,11 +12,12 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include <errno.h> #include <dirent.h> // DIR, dirent, opendir, closedir +#include <errno.h> #include <fcntl.h> // O_RDONLY #include <limits.h> // PATH_MAX #include <stdlib.h> // getenv +#include <string.h> // strncmp #include <sys/stat.h> #include <unistd.h> // access, open, close, fsync #include <utime.h> // utime @@ -296,6 +297,10 @@ static bool CanAccess(const string &path, bool read, bool write, bool exec) { return access(path.c_str(), mode) == 0; } +bool IsDevNull(const char *path) { + return path != NULL && *path != 0 && strncmp("/dev/null\0", path, 10) == 0; +} + bool CanReadFile(const std::string &path) { return !IsDirectory(path) && CanAccess(path, true, false, false); } diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc index ca1ff548b3..8637cc1281 100644 --- a/src/main/cpp/util/file_windows.cc +++ b/src/main/cpp/util/file_windows.cc @@ -45,8 +45,6 @@ static unique_ptr<WCHAR[]> GetCwdW(); static char GetCurrentDrive(); -static bool IsDevNull(const string& path); - // Returns true if `path` refers to a directory or (non-dangling) junction. // `path` must be a normalized Windows path, with UNC prefix (and absolute) if // necessary. @@ -179,7 +177,7 @@ bool WindowsFileMtime::GetIfInDistantFuture(const string& path, bool* result) { if (path.empty()) { return false; } - if (IsDevNull(path)) { + if (IsDevNull(path.c_str())) { *result = false; return true; } @@ -462,7 +460,7 @@ bool AsWindowsPath(const string& path, wstring* result) { result->clear(); return true; } - if (IsDevNull(path)) { + if (IsDevNull(path.c_str())) { result->assign(L"NUL"); return true; } @@ -519,7 +517,7 @@ bool AsAbsoluteWindowsPath(const string& path, wstring* result) { result->clear(); return true; } - if (IsDevNull(path)) { + if (IsDevNull(path.c_str())) { result->assign(L"NUL"); return true; } @@ -536,7 +534,7 @@ bool AsAbsoluteWindowsPath(const string& path, wstring* result) { } bool AsShortWindowsPath(const string& path, string* result) { - if (IsDevNull(path)) { + if (IsDevNull(path.c_str())) { result->assign("NUL"); return true; } @@ -604,7 +602,7 @@ static bool OpenFileForReading(const string& filename, HANDLE* result) { return false; } // TODO(laszlocsomor): remove the following check; it won't allow opening NUL. - if (IsDevNull(filename)) { + if (IsDevNull(filename.c_str())) { return true; } wstring wfilename; @@ -636,7 +634,7 @@ int ReadFromHandle(file_handle_type handle, void* data, size_t size, } bool ReadFile(const string& filename, string* content, int max_size) { - if (IsDevNull(filename)) { + if (IsDevNull(filename.c_str())) { // mimic read(2) behavior: we can always read 0 bytes from /dev/null content->clear(); return true; @@ -655,7 +653,7 @@ bool ReadFile(const string& filename, string* content, int max_size) { } bool ReadFile(const string& filename, void* data, size_t size) { - if (IsDevNull(filename)) { + if (IsDevNull(filename.c_str())) { // mimic read(2) behavior: we can always read 0 bytes from /dev/null return true; } @@ -673,7 +671,7 @@ bool ReadFile(const string& filename, void* data, size_t size) { bool WriteFile(const void* data, size_t size, const string& filename, unsigned int perm) { - if (IsDevNull(filename)) { + if (IsDevNull(filename.c_str())) { return true; // mimic write(2) behavior with /dev/null } wstring wpath; @@ -764,7 +762,7 @@ static bool UnlinkPathW(const wstring& path) { } bool UnlinkPath(const string& file_path) { - if (IsDevNull(file_path)) { + if (IsDevNull(file_path.c_str())) { return false; } @@ -918,7 +916,7 @@ bool PathExists(const string& path) { if (path.empty()) { return false; } - if (IsDevNull(path)) { + if (IsDevNull(path.c_str())) { return true; } wstring wpath; @@ -1111,8 +1109,12 @@ bool CanAccessDirectory(const std::string& path) { return true; } -static bool IsDevNull(const string& path) { - return path == "/dev/null" || AsLower(path) == "nul"; +bool IsDevNull(const char* path) { + return path != NULL && *path != 0 && + (strncmp("/dev/null\0", path, 10) == 0 || + ((path[0] == 'N' || path[0] == 'n') && + (path[1] == 'U' || path[1] == 'u') && + (path[2] == 'L' || path[2] == 'l') && path[3] == 0)); } static bool IsDirectoryW(const wstring& path) { @@ -1123,7 +1125,7 @@ static bool IsDirectoryW(const wstring& path) { } bool IsDirectory(const string& path) { - if (path.empty() || IsDevNull(path)) { + if (path.empty() || IsDevNull(path.c_str())) { return false; } wstring wpath; @@ -1172,7 +1174,7 @@ static bool MakeDirectoriesW(const wstring& path) { bool MakeDirectories(const string& path, unsigned int mode) { // TODO(laszlocsomor): respect `mode` to the extent that it's possible on // Windows; it's currently ignored. - if (path.empty() || IsDevNull(path)) { + if (path.empty() || IsDevNull(path.c_str())) { return false; } wstring wpath; @@ -1192,6 +1194,9 @@ static unique_ptr<WCHAR[]> GetCwdW() { if (!::GetCurrentDirectoryW(len, cwd.get())) { pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, "GetCurrentDirectoryW"); } + for (WCHAR* p = cwd.get(); *p != 0; ++p) { + *p = towlower(*p); + } return std::move(cwd); } @@ -1203,7 +1208,7 @@ static char GetCurrentDrive() { unique_ptr<wchar_t[]> cwd = GetCwdW(); wchar_t wdrive = RemoveUncPrefixMaybe(cwd.get())[0]; wchar_t offset = wdrive >= L'A' && wdrive <= L'Z' ? L'A' : L'a'; - return 'A' + wdrive - offset; + return 'a' + wdrive - offset; } bool ChangeDirectory(const string& path) { @@ -1215,7 +1220,7 @@ bool ChangeDirectory(const string& path) { void ForEachDirectoryEntry(const string &path, DirectoryEntryConsumer *consume) { wstring wpath; - if (path.empty() || IsDevNull(path)) { + if (path.empty() || IsDevNull(path.c_str())) { return; } if (!AsWindowsPath(path, &wpath)) { |