mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Verify custom save path is an absolute path.
PiperOrigin-RevId: 952971544
This commit is contained in:
committed by
Copybara-Service
parent
e1a2316c9e
commit
8a1bbd074e
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user