Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/hyprutils/state/KVStateFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ namespace Hyprutils::State {
* This is a persistent, on-disk file that can be accessed.
* Creating it will populate it with data if it already exists
* A missing file is an empty store. A failed load leaves an empty, bad store.
* Mutations persist immediately. A failed save preserves the previous state and marks the store bad.
* Mutations save immediately, syncing the file before atomic replacement and its parent directory afterwards.
* A failed save preserves the previous in-memory state and marks the store bad, but does not guarantee on-disk rollback.
* If replacement succeeds but directory synchronization fails, the new state is visible but its durability is uncertain.
* Bad stores reject further mutations. Only one writer per file is supported; access is not thread-safe.
*/
class CKVStateFile {
Expand Down
9 changes: 6 additions & 3 deletions src/state/KVStateFile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@ namespace Hyprutils::State {

// nullopt means the file does not exist, not a read error.
virtual std::expected<std::optional<std::string>, std::error_code> read() = 0;
// Failure must leave the previous contents intact.
// Success confirms atomic replacement and file/parent-directory synchronization.
// Failure before replacement preserves the old contents. Failure afterwards is an uncertain commit, not a rollback.
virtual std::expected<void, std::error_code> write(std::string_view contents) = 0;
};

using KVStateClock = std::function<CKVStateEntry::TimePoint()>;
using KVStateClock = std::function<CKVStateEntry::TimePoint()>;
using KVStateFileSync = std::function<int(int)>;

Memory::CUniquePointer<IKVStateFileBackend> makeKVStateFileBackend(std::filesystem::path path);
// The optional sync callback replaces fsync for tests and follows its return/errno convention.
Memory::CUniquePointer<IKVStateFileBackend> makeKVStateFileBackend(std::filesystem::path path, KVStateFileSync sync = {});

struct SKVStateFileAccess {
static Memory::CUniquePointer<CKVStateFile> create(Memory::CUniquePointer<IKVStateFileBackend> backend, KVStateClock clock);
Expand Down
33 changes: 29 additions & 4 deletions src/state/KVStateFileBackend.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
#include "KVStateFile.hpp"

#include <hyprutils/memory/Casts.hpp>
#include <hyprutils/os/FileDescriptor.hpp>
#include <hyprutils/utils/ScopeGuard.hpp>

#include <algorithm>
#include <cerrno>
#include <filesystem>
#include <format>
#include <fstream>
#include <random>
#include <unistd.h>

using namespace Hyprutils::State;
using namespace Hyprutils::Memory;
using namespace Hyprutils::OS;
using namespace Hyprutils::Utils;

static std::unexpected<std::error_code> ioError() {
Expand All @@ -31,7 +35,8 @@ static std::expected<void, std::error_code> writeAll(std::ofstream& stream, std:
namespace {
class CKVStateFileBackend final : public IKVStateFileBackend {
public:
explicit CKVStateFileBackend(std::filesystem::path path) : m_path(std::filesystem::absolute(path, m_pathError)) {}
explicit CKVStateFileBackend(std::filesystem::path path, KVStateFileSync sync) :
m_path(std::filesystem::absolute(path, m_pathError)), m_sync(sync ? std::move(sync) : ::fsync) {}

std::expected<std::optional<std::string>, std::error_code> read() override {
if (m_pathError)
Expand Down Expand Up @@ -73,6 +78,10 @@ namespace {
if (!std::filesystem::is_directory(parentStatus))
return std::unexpected(std::make_error_code(std::errc::not_a_directory));

CFileDescriptor parent{::open(m_path.parent_path().c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC)};
if (!parent.isValid())
return std::unexpected(std::error_code(errno, std::generic_category()));

std::random_device random;
const auto directory = m_path.parent_path() / std::format(".hyprutils-kvstate-{:08x}{:08x}", random(), random());
const auto temporary = directory / "state";
Expand Down Expand Up @@ -104,22 +113,38 @@ namespace {
if (const auto result = writeAll(stream, contents); !result)
return result;

stream.flush();
if (!stream)
return ioError();
if (const auto result = synchronize(stream.native_handle()); !result)
return result;

stream.close();
if (!stream)
return ioError();
std::filesystem::rename(temporary, m_path, error);
if (error)
return std::unexpected(error);

return {};
// The replacement is already visible; a sync failure cannot roll it back.
return synchronize(parent.get());
}

private:
std::expected<void, std::error_code> synchronize(int fd) {
while (m_sync(fd) < 0) {
if (errno != EINTR)
return std::unexpected(std::error_code(errno, std::generic_category()));
}
return {};
}

std::error_code m_pathError;
std::filesystem::path m_path;
KVStateFileSync m_sync;
};
}

CUniquePointer<IKVStateFileBackend> Hyprutils::State::makeKVStateFileBackend(std::filesystem::path path) {
return makeUnique<CKVStateFileBackend>(std::move(path));
CUniquePointer<IKVStateFileBackend> Hyprutils::State::makeKVStateFileBackend(std::filesystem::path path, KVStateFileSync sync) {
return makeUnique<CKVStateFileBackend>(std::move(path), std::move(sync));
}
159 changes: 159 additions & 0 deletions tests/state/KVStateFileBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ namespace {

fs::path m_directory;
};

class KVStateSyncFailureTest : public KVStateFileBackendTest, public testing::WithParamInterface<bool> {};
}

TEST_F(KVStateFileBackendTest, MissingReadDoesNotCreateFiles) {
Expand Down Expand Up @@ -135,6 +137,163 @@ TEST_F(KVStateFileBackendTest, ReplacementIsAtomicAndPrivate) {
EXPECT_EQ(directoryEntries(m_directory), (std::set<fs::path>{"state"}));
}

TEST_F(KVStateFileBackendTest, SynchronizesFlushedFileBeforeRenameAndParentAfterwards) {
const auto path = m_directory / "state";
auto reader = makeKVStateFileBackend(path);
ASSERT_TRUE(reader->write("original"));
struct stat parentStatus{};
ASSERT_EQ(::stat(m_directory.c_str(), &parentStatus), 0);

int syncs = 0;
auto backend = makeKVStateFileBackend(path, [&](int fd) {
++syncs;
struct stat status{};
EXPECT_EQ(::fstat(fd, &status), 0);
const auto visible = reader->read();
EXPECT_TRUE(visible);
if (syncs == 1) {
EXPECT_TRUE(S_ISREG(status.st_mode));
EXPECT_EQ(status.st_mode & 0777, 0600);
if (visible) {
EXPECT_EQ(*visible, std::optional<std::string>{"original"});
}

size_t temporaries = 0;
for (const auto& entry : fs::directory_iterator(m_directory)) {
if (!entry.is_directory())
continue;
++temporaries;
EXPECT_EQ(entry.status().permissions() & fs::perms::mask, fs::perms::owner_all);
const auto temporary = entry.path() / "state";
struct stat temporaryStatus{};
EXPECT_EQ(::stat(temporary.c_str(), &temporaryStatus), 0);
EXPECT_EQ(status.st_dev, temporaryStatus.st_dev);
EXPECT_EQ(status.st_ino, temporaryStatus.st_ino);
std::ifstream contents(temporary, std::ios::binary);
EXPECT_EQ(std::string(std::istreambuf_iterator<char>{contents}, std::istreambuf_iterator<char>{}), "replacement");
EXPECT_FALSE(contents.bad());
}
EXPECT_EQ(temporaries, 1U);
} else {
EXPECT_EQ(syncs, 2);
EXPECT_TRUE(S_ISDIR(status.st_mode));
EXPECT_EQ(status.st_dev, parentStatus.st_dev);
EXPECT_EQ(status.st_ino, parentStatus.st_ino);
if (visible) {
EXPECT_EQ(*visible, std::optional<std::string>{"replacement"});
}
}
return ::fsync(fd);
});

ASSERT_TRUE(backend->write("replacement"));
EXPECT_EQ(syncs, 2);
EXPECT_EQ(directoryEntries(m_directory), (std::set<fs::path>{"state"}));
}

TEST_F(KVStateFileBackendTest, InterruptedFileAndDirectorySyncAreRetried) {
int syncs = 0;
auto backend = makeKVStateFileBackend(m_directory / "state", [&](int fd) {
if (++syncs % 2) {
errno = EINTR;
return -1;
}
return ::fsync(fd);
});

ASSERT_TRUE(backend->write("contents"));
EXPECT_EQ(syncs, 4);
const auto result = backend->read();
ASSERT_TRUE(result);
EXPECT_EQ(*result, std::optional<std::string>{"contents"});
}

TEST_P(KVStateSyncFailureTest, ReportsSyncErrorAndCleansTemporaryForCreationAndReplacement) {
const bool failDirectory = GetParam();
const auto path = m_directory / "state";
auto reader = makeKVStateFileBackend(path);
for (const bool existing : {false, true}) {
SCOPED_TRACE(existing);
if (existing) {
ASSERT_TRUE(reader->write("original"));
}

int syncs = 0;
auto backend = makeKVStateFileBackend(path, [&](int fd) {
++syncs;
struct stat status{};
EXPECT_EQ(::fstat(fd, &status), 0);
if (sc<bool>(S_ISDIR(status.st_mode)) == failDirectory) {
errno = ENOSPC;
return -1;
}
return ::fsync(fd);
});

const auto result = backend->write("replacement");
ASSERT_FALSE(result);
EXPECT_EQ(result.error(), std::error_code(ENOSPC, std::generic_category()));
EXPECT_EQ(syncs, failDirectory ? 2 : 1);
const auto visible = reader->read();
ASSERT_TRUE(visible);
if (failDirectory)
EXPECT_EQ(*visible, std::optional<std::string>{"replacement"});
else if (existing)
EXPECT_EQ(*visible, std::optional<std::string>{"original"});
else
EXPECT_FALSE(visible->has_value());
EXPECT_EQ(directoryEntries(m_directory), (existing || failDirectory ? std::set<fs::path>{"state"} : std::set<fs::path>{}));
}
}

TEST_P(KVStateSyncFailureTest, FailedSavePreservesMemoryAndRejectsLaterMutations) {
const bool failDirectory = GetParam();
const auto path = m_directory / "state";
{
CKVStateFile original{path};
ASSERT_TRUE(original.insert("entry", {{"value", "original"}}));
}

int syncs = 0;
auto backend = makeKVStateFileBackend(path, [&](int fd) {
++syncs;
struct stat status{};
EXPECT_EQ(::fstat(fd, &status), 0);
if (sc<bool>(S_ISDIR(status.st_mode)) == failDirectory) {
errno = EIO;
return -1;
}
return ::fsync(fd);
});
auto state = SKVStateFileAccess::create(std::move(backend), [] { return CKVStateEntry::TimePoint{}; });
ASSERT_TRUE(state->good());
auto* entry = state->get("entry");
ASSERT_NE(entry, nullptr);
const auto modified = entry->lastModified();

EXPECT_FALSE(entry->set("value", "replacement"));
EXPECT_FALSE(state->good());
EXPECT_EQ(state->size(), 1U);
EXPECT_EQ(state->get("entry"), entry);
EXPECT_EQ(entry->get("value"), std::optional<std::string_view>{"original"});
EXPECT_EQ(entry->lastModified(), modified);
EXPECT_FALSE(entry->set("value", "later"));
EXPECT_FALSE(state->insert("later", {}));
EXPECT_FALSE(state->remove("entry"));
EXPECT_EQ(state->pruneOld(0), 0U);
EXPECT_EQ(syncs, failDirectory ? 2 : 1);

CKVStateFile loaded{path};
ASSERT_TRUE(loaded.good());
const auto* persisted = loaded.get("entry");
ASSERT_NE(persisted, nullptr);
EXPECT_EQ(persisted->get("value"), std::optional<std::string_view>{failDirectory ? "replacement" : "original"});
EXPECT_EQ(persisted->lastModified(), failDirectory ? modified + std::chrono::milliseconds{1} : modified);
EXPECT_EQ(directoryEntries(m_directory), (std::set<fs::path>{"state"}));
}

INSTANTIATE_TEST_SUITE_P(KVState, KVStateSyncFailureTest, testing::Bool(), [](const testing::TestParamInfo<bool>& info) { return info.param ? "Directory" : "File"; });

TEST_F(KVStateFileBackendTest, BinaryPayloadSpansReadAndWriteBuffers) {
std::string contents(256 * 1024, '\0');
for (size_t i = 0; i < contents.size(); ++i)
Expand Down
Loading