diff --git a/include/hyprutils/state/KVStateFile.hpp b/include/hyprutils/state/KVStateFile.hpp index 199e44a..251a40d 100644 --- a/include/hyprutils/state/KVStateFile.hpp +++ b/include/hyprutils/state/KVStateFile.hpp @@ -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 { diff --git a/src/state/KVStateFile.hpp b/src/state/KVStateFile.hpp index d53b235..da180d3 100644 --- a/src/state/KVStateFile.hpp +++ b/src/state/KVStateFile.hpp @@ -13,13 +13,16 @@ namespace Hyprutils::State { // nullopt means the file does not exist, not a read error. virtual std::expected, 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 write(std::string_view contents) = 0; }; - using KVStateClock = std::function; + using KVStateClock = std::function; + using KVStateFileSync = std::function; - Memory::CUniquePointer makeKVStateFileBackend(std::filesystem::path path); + // The optional sync callback replaces fsync for tests and follows its return/errno convention. + Memory::CUniquePointer makeKVStateFileBackend(std::filesystem::path path, KVStateFileSync sync = {}); struct SKVStateFileAccess { static Memory::CUniquePointer create(Memory::CUniquePointer backend, KVStateClock clock); diff --git a/src/state/KVStateFileBackend.cpp b/src/state/KVStateFileBackend.cpp index 9792892..79e57bd 100644 --- a/src/state/KVStateFileBackend.cpp +++ b/src/state/KVStateFileBackend.cpp @@ -1,16 +1,20 @@ #include "KVStateFile.hpp" #include +#include #include #include +#include #include #include #include #include +#include using namespace Hyprutils::State; using namespace Hyprutils::Memory; +using namespace Hyprutils::OS; using namespace Hyprutils::Utils; static std::unexpected ioError() { @@ -31,7 +35,8 @@ static std::expected 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::error_code> read() override { if (m_pathError) @@ -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"; @@ -104,6 +113,12 @@ 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(); @@ -111,15 +126,25 @@ namespace { 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 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 Hyprutils::State::makeKVStateFileBackend(std::filesystem::path path) { - return makeUnique(std::move(path)); +CUniquePointer Hyprutils::State::makeKVStateFileBackend(std::filesystem::path path, KVStateFileSync sync) { + return makeUnique(std::move(path), std::move(sync)); } diff --git a/tests/state/KVStateFileBackend.cpp b/tests/state/KVStateFileBackend.cpp index b6bd629..8268ead 100644 --- a/tests/state/KVStateFileBackend.cpp +++ b/tests/state/KVStateFileBackend.cpp @@ -47,6 +47,8 @@ namespace { fs::path m_directory; }; + + class KVStateSyncFailureTest : public KVStateFileBackendTest, public testing::WithParamInterface {}; } TEST_F(KVStateFileBackendTest, MissingReadDoesNotCreateFiles) { @@ -135,6 +137,163 @@ TEST_F(KVStateFileBackendTest, ReplacementIsAtomicAndPrivate) { EXPECT_EQ(directoryEntries(m_directory), (std::set{"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{"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{contents}, std::istreambuf_iterator{}), "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{"replacement"}); + } + } + return ::fsync(fd); + }); + + ASSERT_TRUE(backend->write("replacement")); + EXPECT_EQ(syncs, 2); + EXPECT_EQ(directoryEntries(m_directory), (std::set{"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{"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(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{"replacement"}); + else if (existing) + EXPECT_EQ(*visible, std::optional{"original"}); + else + EXPECT_FALSE(visible->has_value()); + EXPECT_EQ(directoryEntries(m_directory), (existing || failDirectory ? std::set{"state"} : std::set{})); + } +} + +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(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{"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{failDirectory ? "replacement" : "original"}); + EXPECT_EQ(persisted->lastModified(), failDirectory ? modified + std::chrono::milliseconds{1} : modified); + EXPECT_EQ(directoryEntries(m_directory), (std::set{"state"})); +} + +INSTANTIATE_TEST_SUITE_P(KVState, KVStateSyncFailureTest, testing::Bool(), [](const testing::TestParamInfo& 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)