feat(storage): implement original V1 durable Storage engine, tools, and async adapter
This commit is contained in:
1 parent
427cc9a843
commit
fdf0da5ca8
21 files changed
+6966
No files matched your search
@@ -0,0 +1,681 @@
|
||||
#include <Void/Storage/FilePort.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
#include <format>
|
||||
#include <sys/file.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Storage
|
||||
{
|
||||
namespace
|
||||
{
|
||||
[[nodiscard]] StorageErrorCode map_errno(int err) noexcept
|
||||
{
|
||||
switch (err) {
|
||||
case ENOENT:
|
||||
return StorageErrorCode::not_found;
|
||||
case EAGAIN:
|
||||
#if EWOULDBLOCK != EAGAIN
|
||||
case EWOULDBLOCK:
|
||||
#endif
|
||||
case EBUSY:
|
||||
return StorageErrorCode::busy;
|
||||
case EEXIST:
|
||||
return StorageErrorCode::conflict;
|
||||
case ENOSYS:
|
||||
case EOPNOTSUPP:
|
||||
#if ENOTSUP != EOPNOTSUPP
|
||||
case ENOTSUP:
|
||||
#endif
|
||||
return StorageErrorCode::unsupported_capability;
|
||||
case ENOSPC:
|
||||
case EDQUOT:
|
||||
return StorageErrorCode::quota_exceeded;
|
||||
case EINVAL:
|
||||
return StorageErrorCode::invalid_argument;
|
||||
default:
|
||||
return StorageErrorCode::io_error;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] Error errno_error(std::string_view op, const std::filesystem::path& p, int err)
|
||||
{
|
||||
return make_storage_error(
|
||||
map_errno(err),
|
||||
std::format("Storage: {} '{}' failed: {}", op, p.string(), std::strerror(err)),
|
||||
std::error_code{err, std::generic_category()});
|
||||
}
|
||||
|
||||
class PosixDirLock final : public DirLockHandle
|
||||
{
|
||||
public:
|
||||
explicit PosixDirLock(int fd) noexcept : m_fd(fd) {}
|
||||
~PosixDirLock() override
|
||||
{
|
||||
if (m_fd >= 0) {
|
||||
::flock(m_fd, LOCK_UN);
|
||||
::close(m_fd);
|
||||
m_fd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
int m_fd{-1};
|
||||
};
|
||||
|
||||
class PosixFileHandle final : public FileHandle
|
||||
{
|
||||
public:
|
||||
PosixFileHandle(int fd, std::filesystem::path path, std::uint64_t initial_size) noexcept
|
||||
: m_fd(fd), m_path(std::move(path)), m_append_pos(initial_size)
|
||||
{
|
||||
}
|
||||
|
||||
~PosixFileHandle() override
|
||||
{
|
||||
if (m_fd >= 0) {
|
||||
::close(m_fd);
|
||||
m_fd = -1;
|
||||
}
|
||||
}
|
||||
|
||||
Result<std::size_t> pread(std::uint64_t offset, std::span<std::byte> out) override
|
||||
{
|
||||
if (m_fd < 0) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::closed, "Storage: pread on closed handle"));
|
||||
}
|
||||
std::size_t total = 0;
|
||||
while (total < out.size()) {
|
||||
const ssize_t n = ::pread(
|
||||
m_fd,
|
||||
out.data() + total,
|
||||
out.size() - total,
|
||||
static_cast<off_t>(offset + total));
|
||||
if (n > 0) {
|
||||
total += static_cast<std::size_t>(n);
|
||||
} else if (n == 0) {
|
||||
break;
|
||||
} else if (errno == EINTR) {
|
||||
continue;
|
||||
} else {
|
||||
return Void::Result::fail(errno_error("pread", m_path, errno));
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
Result<void> pread_exact(std::uint64_t offset, std::span<std::byte> out) override
|
||||
{
|
||||
auto r = pread(offset, out);
|
||||
if (!r) return Void::Result::fail(std::move(r).error());
|
||||
if (*r != out.size()) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::corrupt,
|
||||
std::format("Storage: short pread on '{}' at offset {} (wanted {}, got {})",
|
||||
m_path.string(), offset, out.size(), *r)));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void> pwrite_all(std::uint64_t offset, ByteView data) override
|
||||
{
|
||||
if (m_fd < 0) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::closed, "Storage: pwrite_all on closed handle"));
|
||||
}
|
||||
std::size_t total = 0;
|
||||
while (total < data.size()) {
|
||||
const ssize_t n = ::pwrite(
|
||||
m_fd,
|
||||
data.data() + total,
|
||||
data.size() - total,
|
||||
static_cast<off_t>(offset + total));
|
||||
if (n > 0) {
|
||||
total += static_cast<std::size_t>(n);
|
||||
} else if (n < 0 && errno == EINTR) {
|
||||
continue;
|
||||
} else {
|
||||
const int err = (n < 0) ? errno : EIO;
|
||||
return Void::Result::fail(errno_error("pwrite", m_path, err));
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<std::uint64_t> append_all(ByteView data) override
|
||||
{
|
||||
std::lock_guard lk{m_mu};
|
||||
if (m_fd < 0) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::closed, "Storage: append_all on closed handle"));
|
||||
}
|
||||
const std::uint64_t start = m_append_pos;
|
||||
auto w = pwrite_all(start, data);
|
||||
if (!w) {
|
||||
// Refresh actual size from fstat so partial failures keep accurate EOF.
|
||||
struct stat st {};
|
||||
if (::fstat(m_fd, &st) == 0 && st.st_size >= 0) {
|
||||
m_append_pos = static_cast<std::uint64_t>(st.st_size);
|
||||
}
|
||||
return Void::Result::fail(std::move(w).error());
|
||||
}
|
||||
m_append_pos = start + data.size();
|
||||
return start;
|
||||
}
|
||||
|
||||
Result<void> sync() override
|
||||
{
|
||||
if (m_fd < 0) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::closed, "Storage: sync on closed handle"));
|
||||
}
|
||||
for (;;) {
|
||||
if (::fsync(m_fd) == 0) {
|
||||
return {};
|
||||
}
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return Void::Result::fail(errno_error("fsync", m_path, errno));
|
||||
}
|
||||
}
|
||||
|
||||
Result<void> truncate(std::uint64_t new_size) override
|
||||
{
|
||||
std::lock_guard lk{m_mu};
|
||||
if (m_fd < 0) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::closed, "Storage: truncate on closed handle"));
|
||||
}
|
||||
for (;;) {
|
||||
if (::ftruncate(m_fd, static_cast<off_t>(new_size)) == 0) {
|
||||
m_append_pos = new_size;
|
||||
return {};
|
||||
}
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return Void::Result::fail(errno_error("ftruncate", m_path, errno));
|
||||
}
|
||||
}
|
||||
|
||||
Result<std::uint64_t> size() const override
|
||||
{
|
||||
if (m_fd < 0) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::closed, "Storage: size on closed handle"));
|
||||
}
|
||||
struct stat st {};
|
||||
if (::fstat(m_fd, &st) != 0) {
|
||||
return Void::Result::fail(errno_error("fstat", m_path, errno));
|
||||
}
|
||||
return st.st_size >= 0 ? static_cast<std::uint64_t>(st.st_size) : 0ULL;
|
||||
}
|
||||
|
||||
Result<void> close() override
|
||||
{
|
||||
std::lock_guard lk{m_mu};
|
||||
if (m_fd < 0) {
|
||||
return {};
|
||||
}
|
||||
const int rc = ::close(m_fd);
|
||||
m_fd = -1;
|
||||
if (rc != 0) {
|
||||
return Void::Result::fail(errno_error("close", m_path, errno));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
mutable std::mutex m_mu{};
|
||||
int m_fd{-1};
|
||||
std::filesystem::path m_path{};
|
||||
std::uint64_t m_append_pos{0};
|
||||
};
|
||||
|
||||
class PosixFilePort final : public FilePort
|
||||
{
|
||||
public:
|
||||
Result<void> ensure_dir(const std::filesystem::path& dir) override
|
||||
{
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(dir, ec);
|
||||
if (ec) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::io_error,
|
||||
std::format("Storage: create_directories '{}' failed: {}",
|
||||
dir.string(), ec.message()),
|
||||
ec));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<bool> exists(const std::filesystem::path& p) override
|
||||
{
|
||||
std::error_code ec;
|
||||
const bool ex = std::filesystem::exists(p, ec);
|
||||
if (ec) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::io_error,
|
||||
std::format("Storage: exists '{}' failed: {}", p.string(), ec.message()),
|
||||
ec));
|
||||
}
|
||||
return ex;
|
||||
}
|
||||
|
||||
Result<std::uint64_t> file_size(const std::filesystem::path& p) override
|
||||
{
|
||||
struct stat st {};
|
||||
if (::lstat(p.c_str(), &st) != 0) {
|
||||
return Void::Result::fail(errno_error("lstat", p, errno));
|
||||
}
|
||||
if (!S_ISREG(st.st_mode)) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::corrupt,
|
||||
std::format("Storage: '{}' is not a regular file", p.string())));
|
||||
}
|
||||
return st.st_size >= 0 ? static_cast<std::uint64_t>(st.st_size) : 0ULL;
|
||||
}
|
||||
|
||||
Result<std::vector<std::string>> list_dir(const std::filesystem::path& dir) override
|
||||
{
|
||||
std::error_code ec;
|
||||
std::vector<std::string> out;
|
||||
for (const auto& entry : std::filesystem::directory_iterator(dir, ec)) {
|
||||
if (ec) break;
|
||||
out.push_back(entry.path().filename().string());
|
||||
}
|
||||
if (ec) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
StorageErrorCode::io_error,
|
||||
std::format("Storage: list_dir '{}' failed: {}", dir.string(), ec.message()),
|
||||
ec));
|
||||
}
|
||||
std::sort(out.begin(), out.end());
|
||||
return out;
|
||||
}
|
||||
|
||||
Result<std::unique_ptr<DirLockHandle>> lock_directory(
|
||||
const std::filesystem::path& dir) override
|
||||
{
|
||||
const auto lock_path = dir / "LOCK";
|
||||
const int fd = ::open(
|
||||
lock_path.c_str(),
|
||||
O_CREAT | O_RDWR | O_NOFOLLOW | O_CLOEXEC,
|
||||
0600);
|
||||
if (fd < 0) {
|
||||
return Void::Result::fail(errno_error("open lock", lock_path, errno));
|
||||
}
|
||||
if (::flock(fd, LOCK_EX | LOCK_NB) != 0) {
|
||||
const int err = errno;
|
||||
::close(fd);
|
||||
return Void::Result::fail(errno_error("flock", lock_path, err));
|
||||
}
|
||||
return std::unique_ptr<DirLockHandle>{new PosixDirLock{fd}};
|
||||
}
|
||||
|
||||
Result<std::unique_ptr<FileHandle>> open_file(
|
||||
const std::filesystem::path& p,
|
||||
FileOpenMode mode) override
|
||||
{
|
||||
int flags = O_NOFOLLOW | O_CLOEXEC;
|
||||
switch (mode) {
|
||||
case FileOpenMode::ReadOnly:
|
||||
flags |= O_RDONLY;
|
||||
break;
|
||||
case FileOpenMode::ReadWriteExisting:
|
||||
flags |= O_RDWR;
|
||||
break;
|
||||
case FileOpenMode::CreateNew:
|
||||
flags |= O_RDWR | O_CREAT | O_EXCL;
|
||||
break;
|
||||
case FileOpenMode::CreateOrOpen:
|
||||
flags |= O_RDWR | O_CREAT;
|
||||
break;
|
||||
}
|
||||
const int fd = ::open(p.c_str(), flags, 0600);
|
||||
if (fd < 0) {
|
||||
return Void::Result::fail(errno_error("open", p, errno));
|
||||
}
|
||||
struct stat st {};
|
||||
if (::fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) {
|
||||
const int err = S_ISREG(st.st_mode) ? errno : EINVAL;
|
||||
::close(fd);
|
||||
return Void::Result::fail(errno_error("fstat", p, err));
|
||||
}
|
||||
const std::uint64_t sz =
|
||||
st.st_size >= 0 ? static_cast<std::uint64_t>(st.st_size) : 0ULL;
|
||||
return std::unique_ptr<FileHandle>{new PosixFileHandle{fd, p, sz}};
|
||||
}
|
||||
|
||||
Result<void> rename_atomic(
|
||||
const std::filesystem::path& from,
|
||||
const std::filesystem::path& to) override
|
||||
{
|
||||
if (::rename(from.c_str(), to.c_str()) != 0) {
|
||||
return Void::Result::fail(errno_error("rename", to, errno));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void> unlink_file(const std::filesystem::path& p) override
|
||||
{
|
||||
if (::unlink(p.c_str()) != 0) {
|
||||
return Void::Result::fail(errno_error("unlink", p, errno));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void> sync_dir(const std::filesystem::path& dir) override
|
||||
{
|
||||
const int fd = ::open(dir.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC);
|
||||
if (fd < 0) {
|
||||
return Void::Result::fail(errno_error("open dir", dir, errno));
|
||||
}
|
||||
int rc = 0;
|
||||
int saved_errno = 0;
|
||||
for (;;) {
|
||||
rc = ::fsync(fd);
|
||||
if (rc == 0) break;
|
||||
if (errno == EINTR) continue;
|
||||
saved_errno = errno;
|
||||
break;
|
||||
}
|
||||
::close(fd);
|
||||
if (rc != 0) {
|
||||
return Void::Result::fail(errno_error("fsync dir", dir, saved_errno));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void> probe_durability_capabilities(
|
||||
const std::filesystem::path& dir) override
|
||||
{
|
||||
const auto p_tmp = dir / std::format(".void-probe-{}.tmp", ::getpid());
|
||||
const auto p_dst = dir / std::format(".void-probe-{}.dst", ::getpid());
|
||||
(void)::unlink(p_tmp.c_str());
|
||||
(void)::unlink(p_dst.c_str());
|
||||
|
||||
auto f = open_file(p_tmp, FileOpenMode::CreateOrOpen);
|
||||
if (!f) return Void::Result::fail(std::move(f).error());
|
||||
const std::byte one{0x5Au};
|
||||
if (auto w = (*f)->append_all(ByteView{&one, 1}); !w) {
|
||||
(void)::unlink(p_tmp.c_str());
|
||||
return Void::Result::fail(std::move(w).error());
|
||||
}
|
||||
if (auto s = (*f)->sync(); !s) {
|
||||
(void)::unlink(p_tmp.c_str());
|
||||
return s;
|
||||
}
|
||||
(void)(*f)->close();
|
||||
if (auto r = rename_atomic(p_tmp, p_dst); !r) {
|
||||
(void)::unlink(p_tmp.c_str());
|
||||
(void)::unlink(p_dst.c_str());
|
||||
return r;
|
||||
}
|
||||
auto sd = sync_dir(dir);
|
||||
(void)::unlink(p_dst.c_str());
|
||||
if (!sd) return sd;
|
||||
return sync_dir(dir);
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::shared_ptr<FilePort> make_posix_file_port()
|
||||
{
|
||||
return std::make_shared<PosixFilePort>();
|
||||
}
|
||||
|
||||
class FaultFileHandle final : public FileHandle
|
||||
{
|
||||
public:
|
||||
FaultFileHandle(
|
||||
FaultInjectingFilePort* owner,
|
||||
std::unique_ptr<FileHandle> inner,
|
||||
std::string path) noexcept
|
||||
: m_owner(owner), m_inner(std::move(inner)), m_path(std::move(path))
|
||||
{
|
||||
}
|
||||
|
||||
Result<std::size_t> pread(std::uint64_t offset, std::span<std::byte> out) override
|
||||
{
|
||||
return m_inner->pread(offset, out);
|
||||
}
|
||||
|
||||
Result<void> pread_exact(std::uint64_t offset, std::span<std::byte> out) override
|
||||
{
|
||||
return m_inner->pread_exact(offset, out);
|
||||
}
|
||||
|
||||
Result<void> pwrite_all(std::uint64_t offset, ByteView data) override
|
||||
{
|
||||
const auto mf = m_owner->check_fault(FaultOp::PWrite, m_path);
|
||||
if (mf.triggered) {
|
||||
if (mf.partial_write_bytes.has_value() && *mf.partial_write_bytes > 0) {
|
||||
const std::size_t n = std::min(*mf.partial_write_bytes, data.size());
|
||||
(void)m_inner->pwrite_all(offset, data.subspan(0, n));
|
||||
}
|
||||
return Void::Result::fail(make_storage_error(
|
||||
mf.error_code,
|
||||
std::format("Storage: injected pwrite fault on '{}'", m_path)));
|
||||
}
|
||||
return m_inner->pwrite_all(offset, data);
|
||||
}
|
||||
|
||||
Result<std::uint64_t> append_all(ByteView data) override
|
||||
{
|
||||
const auto mf = m_owner->check_fault(FaultOp::AppendWrite, m_path);
|
||||
if (mf.triggered) {
|
||||
if (mf.partial_write_bytes.has_value() && *mf.partial_write_bytes > 0) {
|
||||
const std::size_t n = std::min(*mf.partial_write_bytes, data.size());
|
||||
(void)m_inner->append_all(data.subspan(0, n));
|
||||
}
|
||||
return Void::Result::fail(make_storage_error(
|
||||
mf.error_code,
|
||||
std::format("Storage: injected append fault on '{}'", m_path)));
|
||||
}
|
||||
return m_inner->append_all(data);
|
||||
}
|
||||
|
||||
Result<void> sync() override
|
||||
{
|
||||
m_owner->maybe_run_sync_hook();
|
||||
const auto mf = m_owner->check_fault(FaultOp::SyncFile, m_path);
|
||||
if (mf.triggered) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
mf.error_code,
|
||||
std::format("Storage: injected fsync fault on '{}'", m_path)));
|
||||
}
|
||||
return m_inner->sync();
|
||||
}
|
||||
|
||||
Result<void> truncate(std::uint64_t new_size) override
|
||||
{
|
||||
const auto mf = m_owner->check_fault(FaultOp::Truncate, m_path);
|
||||
if (mf.triggered) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
mf.error_code,
|
||||
std::format("Storage: injected truncate fault on '{}'", m_path)));
|
||||
}
|
||||
return m_inner->truncate(new_size);
|
||||
}
|
||||
|
||||
Result<std::uint64_t> size() const override
|
||||
{
|
||||
return m_inner->size();
|
||||
}
|
||||
|
||||
Result<void> close() override
|
||||
{
|
||||
return m_inner->close();
|
||||
}
|
||||
|
||||
private:
|
||||
FaultInjectingFilePort* m_owner;
|
||||
std::unique_ptr<FileHandle> m_inner;
|
||||
std::string m_path;
|
||||
};
|
||||
|
||||
FaultInjectingFilePort::FaultInjectingFilePort(std::shared_ptr<FilePort> delegate)
|
||||
: delegate_(delegate ? std::move(delegate) : make_posix_file_port())
|
||||
{
|
||||
}
|
||||
|
||||
void FaultInjectingFilePort::add_rule(FaultRule rule)
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
rules_.push_back(std::move(rule));
|
||||
}
|
||||
|
||||
void FaultInjectingFilePort::clear_rules()
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
rules_.clear();
|
||||
}
|
||||
|
||||
void FaultInjectingFilePort::set_pause_before_sync(std::function<void()> hook)
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
pause_before_sync_ = std::move(hook);
|
||||
}
|
||||
|
||||
std::uint64_t FaultInjectingFilePort::op_count(FaultOp op) const noexcept
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
return op_counts_[static_cast<std::size_t>(op)];
|
||||
}
|
||||
|
||||
FaultInjectingFilePort::MatchedFault FaultInjectingFilePort::check_fault(
|
||||
FaultOp op,
|
||||
const std::string& path)
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
++op_counts_[static_cast<std::size_t>(op)];
|
||||
for (auto& r : rules_) {
|
||||
if (r.consumed || r.op != op) continue;
|
||||
if (!r.path_contains.empty() && path.find(r.path_contains) == std::string::npos) {
|
||||
continue;
|
||||
}
|
||||
++r.match_count;
|
||||
if (r.hit_index == 0 || r.match_count == r.hit_index) {
|
||||
if (r.hit_index != 0) {
|
||||
r.consumed = true;
|
||||
}
|
||||
return MatchedFault{r.error_code, r.partial_write_bytes, true};
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void FaultInjectingFilePort::maybe_run_sync_hook()
|
||||
{
|
||||
std::function<void()> cb;
|
||||
{
|
||||
std::lock_guard lk{mu_};
|
||||
cb = pause_before_sync_;
|
||||
}
|
||||
if (cb) {
|
||||
cb();
|
||||
}
|
||||
}
|
||||
|
||||
Result<void> FaultInjectingFilePort::ensure_dir(const std::filesystem::path& dir)
|
||||
{
|
||||
return delegate_->ensure_dir(dir);
|
||||
}
|
||||
|
||||
Result<bool> FaultInjectingFilePort::exists(const std::filesystem::path& p)
|
||||
{
|
||||
return delegate_->exists(p);
|
||||
}
|
||||
|
||||
Result<std::uint64_t> FaultInjectingFilePort::file_size(const std::filesystem::path& p)
|
||||
{
|
||||
return delegate_->file_size(p);
|
||||
}
|
||||
|
||||
Result<std::vector<std::string>> FaultInjectingFilePort::list_dir(const std::filesystem::path& dir)
|
||||
{
|
||||
return delegate_->list_dir(dir);
|
||||
}
|
||||
|
||||
Result<std::unique_ptr<DirLockHandle>> FaultInjectingFilePort::lock_directory(
|
||||
const std::filesystem::path& dir)
|
||||
{
|
||||
const auto mf = check_fault(FaultOp::LockDir, dir.string());
|
||||
if (mf.triggered) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
mf.error_code, "Storage: injected directory lock fault"));
|
||||
}
|
||||
return delegate_->lock_directory(dir);
|
||||
}
|
||||
|
||||
Result<std::unique_ptr<FileHandle>> FaultInjectingFilePort::open_file(
|
||||
const std::filesystem::path& p,
|
||||
FileOpenMode mode)
|
||||
{
|
||||
const auto mf = check_fault(FaultOp::OpenFile, p.string());
|
||||
if (mf.triggered) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
mf.error_code,
|
||||
std::format("Storage: injected open_file fault on '{}'", p.string())));
|
||||
}
|
||||
auto h = delegate_->open_file(p, mode);
|
||||
if (!h) return Void::Result::fail(std::move(h).error());
|
||||
return std::unique_ptr<FileHandle>{
|
||||
new FaultFileHandle{this, std::move(*h), p.string()}};
|
||||
}
|
||||
|
||||
Result<void> FaultInjectingFilePort::rename_atomic(
|
||||
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 fault to '{}'", to.string())));
|
||||
}
|
||||
return delegate_->rename_atomic(from, to);
|
||||
}
|
||||
|
||||
Result<void> FaultInjectingFilePort::unlink_file(const std::filesystem::path& p)
|
||||
{
|
||||
const auto mf = check_fault(FaultOp::Unlink, p.string());
|
||||
if (mf.triggered) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
mf.error_code,
|
||||
std::format("Storage: injected unlink fault on '{}'", p.string())));
|
||||
}
|
||||
return delegate_->unlink_file(p);
|
||||
}
|
||||
|
||||
Result<void> FaultInjectingFilePort::sync_dir(const std::filesystem::path& dir)
|
||||
{
|
||||
const auto mf = check_fault(FaultOp::SyncDir, dir.string());
|
||||
if (mf.triggered) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
mf.error_code,
|
||||
std::format("Storage: injected sync_dir fault on '{}'", dir.string())));
|
||||
}
|
||||
return delegate_->sync_dir(dir);
|
||||
}
|
||||
|
||||
Result<void> FaultInjectingFilePort::probe_durability_capabilities(
|
||||
const std::filesystem::path& dir)
|
||||
{
|
||||
const auto mf = check_fault(FaultOp::ProbeDurability, dir.string());
|
||||
if (mf.triggered) {
|
||||
return Void::Result::fail(make_storage_error(
|
||||
mf.error_code, "Storage: injected probe durability fault"));
|
||||
}
|
||||
return delegate_->probe_durability_capabilities(dir);
|
||||
}
|
||||
|
||||
} // namespace Void::Storage
|
||||
Reference in new issue
Block a user