Verify custom save path is an absolute path.

PiperOrigin-RevId: 952971544
This commit is contained in:
Francis Tsui
2026-07-23 14:55:42 -07:00
committed by Copybara-Service
parent e1a2316c9e
commit 8a1bbd074e
6 changed files with 45 additions and 5 deletions
+4
View File
@@ -142,4 +142,8 @@ std::optional<size_t> Files::GetAvailableDiskSpaceInBytes(
return std::nullopt;
}
bool Files::IsAbsolutePath(const FilePath& path) {
return path.path_.is_absolute();
}
} // namespace nearby
+5
View File
@@ -75,6 +75,11 @@ class Files {
// determined.
static std::optional<size_t> GetAvailableDiskSpaceInBytes(
const FilePath& path);
// Returns true if the path is an absolute path.
// On Posix systems, this is a path that starts with a `/`.
// On Windows, this is a path that starts with a drive letter or a UNC path.
static bool IsAbsolutePath(const FilePath& path);
};
} // namespace nearby
+16
View File
@@ -46,5 +46,21 @@ TEST(FilesTest, CreateHardLinkSuccess) {
Files::RemoveFile(target);
}
#if defined(_WIN32)
TEST(FilesTest, IsAbsolutePathWindows) {
EXPECT_TRUE(Files::IsAbsolutePath(FilePath("C:\\Users\\test\\file.txt")));
EXPECT_TRUE(Files::IsAbsolutePath(FilePath("\\\\server\\share\\file.txt")));
EXPECT_FALSE(Files::IsAbsolutePath(FilePath("file.txt")));
EXPECT_FALSE(Files::IsAbsolutePath(FilePath("C:Users\\test\\file.txt")));
}
#endif
#if defined(__linux__) || defined(__APPLE__)
TEST(FilesTest, IsAbsolutePathPosix) {
EXPECT_TRUE(Files::IsAbsolutePath(FilePath("/Users/test/file.txt")));
EXPECT_FALSE(Files::IsAbsolutePath(FilePath("file.txt")));
}
#endif
} // namespace
} // namespace nearby