Storage: add no-replace rename, hard link and free-space query to FilePort (refs #53)

This commit is contained in:
faumaray committed 2026-12-04 00:19:21 +03:00
1 parent ac30650266
commit 217b20e37a
3 files changed
+148

No files matched your search

+61
View File
@@ -854,3 +854,64 @@ Void::Testing::Test storage_tools_verify_backup_restore_salvage_and_async_stop_r
}
}
} // namespace
Void::Testing::Test storage_file_port_space_link_noreplace_and_no_follow()
{
TempStorageDir dir;
auto port = make_posix_file_port();
Void::Testing::require(port != nullptr);
const auto exists = [&](const std::filesystem::path& path) {
auto result = port->exists(path);
return result.has_value() && *result;
};
auto available = port->available_space(dir.path);
Void::Testing::require(available.has_value());
Void::Testing::expect(*available > 0);
const std::vector<std::byte> payload{std::byte{'a'}, std::byte{'b'}};
const auto src = dir.path / "src.bin";
{
auto file = port->open_file(src, FileOpenMode::CreateNew);
Void::Testing::require(file.has_value());
Void::Testing::require(
(*file)->pwrite_all(0, std::span<const std::byte>{payload})
.has_value());
Void::Testing::require((*file)->sync().has_value());
Void::Testing::require((*file)->close().has_value());
}
// Exclusive create refuses an existing file.
Void::Testing::expect(
!port->open_file(src, FileOpenMode::CreateNew).has_value());
const auto link = dir.path / "link.bin";
Void::Testing::require(port->hard_link(src, link).has_value());
Void::Testing::expect(exists(link));
Void::Testing::require(port->unlink_file(src).has_value());
Void::Testing::expect(exists(link));
// No-replace rename refuses an existing destination.
const auto moved = dir.path / "moved.bin";
{
auto dest = port->open_file(moved, FileOpenMode::CreateNew);
Void::Testing::require(dest.has_value());
Void::Testing::require((*dest)->close().has_value());
}
Void::Testing::expect(!port->rename_noreplace(link, moved).has_value());
Void::Testing::require(port->unlink_file(moved).has_value());
Void::Testing::require(port->rename_noreplace(link, moved).has_value());
Void::Testing::expect(!exists(link));
Void::Testing::expect(exists(moved));
// O_NOFOLLOW: opening through a symlink is rejected.
const auto target = dir.path / "target.bin";
{
auto file = port->open_file(target, FileOpenMode::CreateNew);
Void::Testing::require(file.has_value());
Void::Testing::require((*file)->close().has_value());
}
const auto symlink_path = dir.path / "symlink";
Void::Testing::require(::symlink(target.c_str(), symlink_path.c_str()) == 0);
Void::Testing::expect(
!port->open_file(symlink_path, FileOpenMode::ReadOnly).has_value());
}