From 217b20e37a4a167e70fe9108d0791126f9c46654 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Fri, 4 Dec 2026 00:19:21 +0300 Subject: [PATCH] Storage: add no-replace rename, hard link and free-space query to FilePort (refs #53) --- include/Void/Storage/FilePort.hpp | 14 ++++++ src/Storage/FilePort.cpp | 73 ++++++++++++++++++++++++++++ tests/standalone/Storage/Storage.cpp | 61 +++++++++++++++++++++++ 3 files changed, 148 insertions(+) diff --git a/include/Void/Storage/FilePort.hpp b/include/Void/Storage/FilePort.hpp index f454798..43d1e8e 100644 --- a/include/Void/Storage/FilePort.hpp +++ b/include/Void/Storage/FilePort.hpp @@ -64,6 +64,17 @@ namespace Void::Storage [[nodiscard]] virtual Result 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 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 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 available_space( + const std::filesystem::path& p) = 0; [[nodiscard]] virtual Result unlink_file( const std::filesystem::path& p) = 0; [[nodiscard]] virtual Result sync_dir( @@ -119,6 +130,9 @@ namespace Void::Storage [[nodiscard]] Result> lock_directory(const std::filesystem::path& dir) override; [[nodiscard]] Result> open_file(const std::filesystem::path& p, FileOpenMode mode) override; [[nodiscard]] Result rename_atomic(const std::filesystem::path& from, const std::filesystem::path& to) override; + [[nodiscard]] Result rename_noreplace(const std::filesystem::path& from, const std::filesystem::path& to) override; + [[nodiscard]] Result hard_link(const std::filesystem::path& from, const std::filesystem::path& to) override; + [[nodiscard]] Result available_space(const std::filesystem::path& p) override; [[nodiscard]] Result unlink_file(const std::filesystem::path& p) override; [[nodiscard]] Result sync_dir(const std::filesystem::path& dir) override; [[nodiscard]] Result probe_durability_capabilities(const std::filesystem::path& dir) override; diff --git a/src/Storage/FilePort.cpp b/src/Storage/FilePort.cpp index 82e21d6..520c2c2 100644 --- a/src/Storage/FilePort.cpp +++ b/src/Storage/FilePort.cpp @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include @@ -364,6 +366,50 @@ namespace Void::Storage return {}; } + Result 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 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 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(info.f_bavail) * + static_cast(info.f_frsize); + } + Result 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 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 FaultInjectingFilePort::hard_link( + const std::filesystem::path& from, + const std::filesystem::path& to) + { + return delegate_->hard_link(from, to); + } + + Result FaultInjectingFilePort::available_space( + const std::filesystem::path& p) + { + return delegate_->available_space(p); + } + Result FaultInjectingFilePort::unlink_file(const std::filesystem::path& p) { const auto mf = check_fault(FaultOp::Unlink, p.string()); diff --git a/tests/standalone/Storage/Storage.cpp b/tests/standalone/Storage/Storage.cpp index d9bf939..3d2ec78 100644 --- a/tests/standalone/Storage/Storage.cpp +++ b/tests/standalone/Storage/Storage.cpp @@ -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 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{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()); +}