diff options
Diffstat (limited to 'src')
-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 | ||||
-rw-r--r-- | src/test/cpp/blaze_util_test.cc | 1 | ||||
-rw-r--r-- | src/test/cpp/blaze_util_windows_test.cc | 27 | ||||
-rw-r--r-- | src/test/cpp/util/file_test.cc | 11 | ||||
-rw-r--r-- | src/test/cpp/util/file_windows_test.cc | 22 |
8 files changed, 89 insertions, 28 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)) { diff --git a/src/test/cpp/blaze_util_test.cc b/src/test/cpp/blaze_util_test.cc index a06e8e0a01..ac3165c642 100644 --- a/src/test/cpp/blaze_util_test.cc +++ b/src/test/cpp/blaze_util_test.cc @@ -264,6 +264,7 @@ TEST_F(BlazeUtilTest, MakeAbsolute) { EXPECT_EQ(MakeAbsolute("foo"), blaze_util::GetCwd() + "/foo"); #endif EXPECT_EQ(MakeAbsolute(std::string()), blaze_util::GetCwd()); + EXPECT_EQ(MakeAbsolute("/dev/null"), "/dev/null"); } } // namespace blaze diff --git a/src/test/cpp/blaze_util_windows_test.cc b/src/test/cpp/blaze_util_windows_test.cc index e558617421..763ff563ca 100644 --- a/src/test/cpp/blaze_util_windows_test.cc +++ b/src/test/cpp/blaze_util_windows_test.cc @@ -20,6 +20,7 @@ #include "src/main/cpp/blaze_util.h" #include "src/main/cpp/blaze_util_platform.h" +#include "src/main/cpp/util/file.h" #include "src/main/cpp/util/strings.h" #include "gtest/gtest.h" @@ -154,4 +155,30 @@ TEST(BlazeUtilWindowsTest, TestUnsetEnv) { ASSERT_ENVVAR_UNSET(long_key.c_str()); } +TEST(BlazeUtilWindowsTest, ConvertPathTest) { + EXPECT_EQ("c:\\foo", ConvertPath("C:\\FOO")); + EXPECT_EQ("c:\\blah", ConvertPath("/c/Blah")); + EXPECT_EQ("c:\\", ConvertPath("/c")); + EXPECT_EQ("c:\\", ConvertPath("/c/")); + EXPECT_EQ("c:\\", ConvertPath("c:/")); + EXPECT_EQ("c:\\foo\\bar", ConvertPath("c:/../foo\\BAR\\.\\")); + EXPECT_EQ("nul", MakeAbsolute("NUL")); + EXPECT_EQ("nul", MakeAbsolute("nul")); + EXPECT_EQ("nul", MakeAbsolute("/dev/null")); +} + +TEST(BlazeUtilWindowsTest, TestMakeAbsolute) { + EXPECT_EQ("c:\\foo\\bar", MakeAbsolute("C:\\foo\\BAR")); + EXPECT_EQ("c:\\foo\\bar", MakeAbsolute("C:/foo/bar")); + EXPECT_EQ("c:\\foo\\bar", MakeAbsolute("C:\\foo\\bar\\")); + EXPECT_EQ("c:\\foo\\bar", MakeAbsolute("C:/foo/bar/")); + EXPECT_EQ(blaze_util::AsLower(blaze_util::GetCwd()) + "\\foo", + MakeAbsolute("foo")); + EXPECT_EQ("nul", MakeAbsolute("NUL")); + EXPECT_EQ("nul", MakeAbsolute("Nul")); + EXPECT_EQ("nul", MakeAbsolute("nul")); + EXPECT_EQ(blaze_util::AsLower(blaze_util::GetCwd()), MakeAbsolute("")); + EXPECT_EQ("nul", MakeAbsolute("/dev/null")); +} + } // namespace blaze diff --git a/src/test/cpp/util/file_test.cc b/src/test/cpp/util/file_test.cc index ab943a7bf9..21b0afca51 100644 --- a/src/test/cpp/util/file_test.cc +++ b/src/test/cpp/util/file_test.cc @@ -206,7 +206,7 @@ class CollectingDirectoryEntryConsumer : public DirectoryEntryConsumer { void Consume(const string& name, bool is_directory) override { // Strip the path prefix up to the `rootname` to ease testing on all // platforms. - int index = name.rfind(rootname); + size_t index = name.rfind(rootname); string key = (index == string::npos) ? name : name.substr(index); // Replace backslashes with forward slashes (necessary on Windows only). std::replace(key.begin(), key.end(), '\\', '/'); @@ -248,4 +248,13 @@ TEST(FileTest, ForEachDirectoryEntryTest) { ASSERT_EQ(consumer.entries, expected); } +TEST(FileTest, IsDevNullTest) { + ASSERT_TRUE(IsDevNull("/dev/null")); + ASSERT_FALSE(IsDevNull("dev/null")); + ASSERT_FALSE(IsDevNull("/dev/nul")); + ASSERT_FALSE(IsDevNull("/dev/nulll")); + ASSERT_FALSE(IsDevNull(NULL)); + ASSERT_FALSE(IsDevNull("")); +} + } // namespace blaze_util diff --git a/src/test/cpp/util/file_windows_test.cc b/src/test/cpp/util/file_windows_test.cc index 496c24d02f..709b1d4424 100644 --- a/src/test/cpp/util/file_windows_test.cc +++ b/src/test/cpp/util/file_windows_test.cc @@ -22,6 +22,7 @@ #include "gtest/gtest.h" #include "src/main/cpp/util/file.h" #include "src/main/cpp/util/file_platform.h" +#include "src/main/cpp/util/strings.h" #include "src/main/native/windows/file.h" #include "src/main/native/windows/util.h" #include "src/test/cpp/util/test_util.h" @@ -257,15 +258,12 @@ TEST_F(FileWindowsTest, TestAsAbsoluteWindowsPath) { ASSERT_EQ(L"\\\\?\\c:\\non-existent", actual); WCHAR cwd[MAX_PATH]; - ASSERT_TRUE(::GetCurrentDirectoryW(MAX_PATH, cwd)); - ASSERT_FALSE(bazel::windows::HasUncPrefix(cwd)); - wstring cwdw(cwd); - ASSERT_EQ(cwdw.find_first_of(L'/'), wstring::npos); + wstring cwdw(CstringToWstring(GetCwd().c_str()).get()); wstring expected = wstring(L"\\\\?\\") + cwdw + ((cwdw.back() == L'\\') ? L"non-existent" : L"\\non-existent"); ASSERT_TRUE(AsAbsoluteWindowsPath("non-existent", &actual)); - ASSERT_EQ(expected, actual); + ASSERT_EQ(actual, expected); } TEST_F(FileWindowsTest, TestAsShortWindowsPath) { @@ -568,4 +566,18 @@ TEST_F(FileWindowsTest, TestMakeCanonical) { ASSERT_EQ(dircanon, symcanon); } +TEST(FileTest, IsWindowsDevNullTest) { + ASSERT_TRUE(IsDevNull("nul")); + ASSERT_TRUE(IsDevNull("NUL")); + ASSERT_TRUE(IsDevNull("nuL")); + ASSERT_TRUE(IsDevNull("/dev/null")); + ASSERT_FALSE(IsDevNull("/Dev/Null")); + ASSERT_FALSE(IsDevNull("dev/null")); + ASSERT_FALSE(IsDevNull("/dev/nul")); + ASSERT_FALSE(IsDevNull("/dev/nulll")); + ASSERT_FALSE(IsDevNull("nu")); + ASSERT_FALSE(IsDevNull(NULL)); + ASSERT_FALSE(IsDevNull("")); +} + } // namespace blaze_util |