Fix grant ledger ownership and truncated-record recovery

This commit is contained in:
dingo
2026-10-02 06:24:56 -04:00
parent c588be237c
commit 7ac587743c
6 changed files with 180 additions and 9 deletions
+57 -4
View File
@@ -1,4 +1,5 @@
#include "evo/network/grant_ledger.hpp"
#include <cerrno>
#include <charconv>
#include <fstream>
#include <string>
@@ -7,8 +8,13 @@
#ifdef _WIN32
#include <io.h>
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#else
#include <fcntl.h>
#include <sys/file.h>
#include <unistd.h>
#endif
@@ -50,7 +56,7 @@ std::string line(ConsumedGrant g) {
out[static_cast<std::size_t>(i)] = digits[g.grant_id & 0x0FU];
return out + ' ' + std::to_string(g.expires_at) + '\n';
}
bool parse(const std::string& text, ConsumedGrant& out) { // One getline() line.
bool parse(const std::string& text, ConsumedGrant& out) { // One complete bounded line.
if (text.size() < 18 || text[16] != ' ')
return false;
const auto [a, e1] = std::from_chars(text.data(), text.data() + 16, out.grant_id, 16);
@@ -61,8 +67,44 @@ bool parse(const std::string& text, ConsumedGrant& out) { // One getline() line.
}
} // namespace
// A separate, stable file: locking the ledger inode would not survive compaction.
struct GrantLedger::Lock {
#ifdef _WIN32
HANDLE handle{INVALID_HANDLE_VALUE};
~Lock() {
if (handle != INVALID_HANDLE_VALUE)
CloseHandle(handle);
}
#else
int descriptor{-1};
~Lock() {
if (descriptor >= 0)
::close(descriptor);
}
#endif
};
GrantLedger::GrantLedger(std::FILE* file, std::vector<ConsumedGrant> loaded,
std::unique_ptr<Lock> lock)
: lock_{std::move(lock)}, file_{file}, loaded_{std::move(loaded)}, entries_{loaded_.size()} {}
std::expected<std::unique_ptr<GrantLedger>, LedgerError>
GrantLedger::open(const std::filesystem::path& path, std::uint64_t now_seconds) {
auto lock = std::make_unique<Lock>();
const auto lock_path = std::filesystem::path{path}.concat(".lock");
#ifdef _WIN32
lock->handle = CreateFileW(lock_path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, nullptr,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
if (lock->handle == INVALID_HANDLE_VALUE)
return std::unexpected(GetLastError() == ERROR_SHARING_VIOLATION ? LedgerError::busy
: LedgerError::io);
#else
lock->descriptor = ::open(lock_path.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0600);
if (lock->descriptor < 0)
return std::unexpected(LedgerError::io);
if (::flock(lock->descriptor, LOCK_EX | LOCK_NB) != 0)
return std::unexpected(errno == EWOULDBLOCK ? LedgerError::busy : LedgerError::io);
#endif
std::vector<ConsumedGrant> live;
std::error_code error;
if (std::filesystem::exists(path, error)) {
@@ -71,7 +113,15 @@ GrantLedger::open(const std::filesystem::path& path, std::uint64_t now_seconds)
return std::unexpected(LedgerError::io);
std::string text;
std::size_t total = 0;
while (std::getline(in, text)) {
constexpr std::size_t max_line_bytes = 16 + 1 + 20; // hex id, space, u64 expiry.
char byte{};
while (in.get(byte)) {
if (byte != '\n') {
if (text.size() == max_line_bytes)
return std::unexpected(LedgerError::corrupt);
text.push_back(byte);
continue;
}
ConsumedGrant g;
if (!parse(text, g))
return std::unexpected(LedgerError::corrupt); // Never guess: fail closed.
@@ -79,9 +129,12 @@ GrantLedger::open(const std::filesystem::path& path, std::uint64_t now_seconds)
return std::unexpected(LedgerError::full);
if (g.expires_at > now_seconds)
live.push_back(g);
text.clear();
}
if (!in.eof())
if (in.bad() || !in.eof())
return std::unexpected(LedgerError::io);
if (!text.empty())
return std::unexpected(LedgerError::corrupt); // Unterminated record: never compact.
} else if (error) {
return std::unexpected(LedgerError::io);
}
@@ -104,7 +157,7 @@ GrantLedger::open(const std::filesystem::path& path, std::uint64_t now_seconds)
std::FILE* file = open_file(path, "ab");
if (file == nullptr)
return std::unexpected(LedgerError::io);
return std::unique_ptr<GrantLedger>(new GrantLedger(file, std::move(live)));
return std::unique_ptr<GrantLedger>(new GrantLedger(file, std::move(live), std::move(lock)));
}
GrantLedger::~GrantLedger() {