Merge issue-53-fileport: FilePort no-replace rename, hard link and free space (refs #53)
This commit is contained in:
commit
06e5bd5693
3 files changed
+148
No files matched your search
@@ -64,6 +64,17 @@ namespace Void::Storage
|
||||
[[nodiscard]] virtual Result<void> rename_atomic(
|
||||
const std::filesystem::path& from,
|
||||
const std::filesystem::path& to) = 0;
|
||||
/// Rename only when the destination does not exist (atomic on Linux).
|
||||
[[nodiscard]] virtual Result<void> rename_noreplace(
|
||||
const std::filesystem::path& from,
|
||||
const std::filesystem::path& to) = 0;
|
||||
/// Create a hard link; fails when the destination exists.
|
||||
[[nodiscard]] virtual Result<void> hard_link(
|
||||
const std::filesystem::path& from,
|
||||
const std::filesystem::path& to) = 0;
|
||||
/// Bytes available to this process on the filesystem holding `p`.
|
||||
[[nodiscard]] virtual Result<std::uint64_t> available_space(
|
||||
const std::filesystem::path& p) = 0;
|
||||
[[nodiscard]] virtual Result<void> unlink_file(
|
||||
const std::filesystem::path& p) = 0;
|
||||
[[nodiscard]] virtual Result<void> sync_dir(
|
||||
@@ -119,6 +130,9 @@ namespace Void::Storage
|
||||
[[nodiscard]] Result<std::unique_ptr<DirLockHandle>> lock_directory(const std::filesystem::path& dir) override;
|
||||
[[nodiscard]] Result<std::unique_ptr<FileHandle>> open_file(const std::filesystem::path& p, FileOpenMode mode) override;
|
||||
[[nodiscard]] Result<void> rename_atomic(const std::filesystem::path& from, const std::filesystem::path& to) override;
|
||||
[[nodiscard]] Result<void> rename_noreplace(const std::filesystem::path& from, const std::filesystem::path& to) override;
|
||||
[[nodiscard]] Result<void> hard_link(const std::filesystem::path& from, const std::filesystem::path& to) override;
|
||||
[[nodiscard]] Result<std::uint64_t> available_space(const std::filesystem::path& p) override;
|
||||
[[nodiscard]] Result<void> unlink_file(const std::filesystem::path& p) override;
|
||||
[[nodiscard]] Result<void> sync_dir(const std::filesystem::path& dir) override;
|
||||
[[nodiscard]] Result<void> probe_durability_capabilities(const std::filesystem::path& dir) override;
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
#include <format>
|
||||
#include <sys/file.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statvfs.h>
|
||||
#include <sys/syscall.h>
|
||||
#include <unistd.h>
|
||||
#include <utility>
|
||||
|
||||
@@ -364,6 +366,50 @@ namespace Void::Storage
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void> rename_noreplace(
|
||||
const std::filesystem::path& from,
|
||||
const std::filesystem::path& to) override
|
||||
{
|
||||
#if defined(SYS_renameat2)
|
||||
if (::syscall(SYS_renameat2, AT_FDCWD, from.c_str(),
|
||||
AT_FDCWD, to.c_str(), 1U) == 0) {
|
||||
return {};
|
||||
}
|
||||
if (errno != EINVAL && errno != ENOSYS && errno != EOPNOTSUPP) {
|
||||
return Void::Result::fail(errno_error("renameat2", to, errno));
|
||||
}
|
||||
#endif
|
||||
if (::link(from.c_str(), to.c_str()) != 0) {
|
||||
return Void::Result::fail(errno_error("link", to, errno));
|
||||
}
|
||||
if (::unlink(from.c_str()) != 0) {
|
||||
const int err = errno;
|
||||
(void)::unlink(to.c_str());
|
||||
return Void::Result::fail(errno_error("unlink", from, err));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void> hard_link(const std::filesystem::path& from,
|
||||
const std::filesystem::path& to) override
|
||||
{
|
||||
if (::link(from.c_str(), to.c_str()) != 0) {
|
||||
return Void::Result::fail(errno_error("link", to, errno));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<std::uint64_t> available_space(
|
||||
const std::filesystem::path& p) override
|
||||
{
|
||||
struct statvfs info {};
|
||||
if (::statvfs(p.c_str(), &info) != 0) {
|
||||
return Void::Result::fail(errno_error("statvfs", p, errno));
|
||||
}
|
||||
return static_cast<std::uint64_t>(info.f_bavail) *
|
||||
static_cast<std::uint64_t>(info.f_frsize);
|
||||
}
|
||||
|
||||
Result<void> unlink_file(const std::filesystem::path& p) override
|
||||
{
|
||||
if (::unlink(p.c_str()) != 0) {
|
||||
@@ -645,6 +691,33 @@ namespace Void::Storage
|
||||
return delegate_->rename_atomic(from, to);
|
||||
}
|
||||
|
||||
Result<void> FaultInjectingFilePort::rename_noreplace(
|
||||
const std::filesystem::path& from,
|
||||
const std::filesystem::path& to)
|
||||
{
|
||||
const auto mf = check_fault(FaultOp::Rename, to.string());
|
||||
if (mf.triggered) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
mf.error_code,
|
||||
std::format("Storage: injected rename_noreplace fault to '{}'",
|
||||
to.string())));
|
||||
}
|
||||
return delegate_->rename_noreplace(from, to);
|
||||
}
|
||||
|
||||
Result<void> FaultInjectingFilePort::hard_link(
|
||||
const std::filesystem::path& from,
|
||||
const std::filesystem::path& to)
|
||||
{
|
||||
return delegate_->hard_link(from, to);
|
||||
}
|
||||
|
||||
Result<std::uint64_t> FaultInjectingFilePort::available_space(
|
||||
const std::filesystem::path& p)
|
||||
{
|
||||
return delegate_->available_space(p);
|
||||
}
|
||||
|
||||
Result<void> FaultInjectingFilePort::unlink_file(const std::filesystem::path& p)
|
||||
{
|
||||
const auto mf = check_fault(FaultOp::Unlink, p.string());
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
Reference in new issue
Block a user