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

+73
View File
@@ -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());