diff --git a/interfaces/ImsdMessages-Store.cppm b/interfaces/ImsdMessages-Store.cppm index dfb7fc8..96db99b 100644 --- a/interfaces/ImsdMessages-Store.cppm +++ b/interfaces/ImsdMessages-Store.cppm @@ -1,8 +1,347 @@ // SPDX-License-Identifier: GPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® +/* +ImsdMessages:Store — the app's message database, one Crafter.DB table. + +The table has no ordered column on purpose: Crafter.DB enforces the order on +every write, and a List() import after a restart can carry an older stamp +than the last row. SMS volumes make a scan free, so conversations are +derived from a scan (ImsdMessages:Model) and a conversation's messages are +sorted here. One Table per file: a second MessageStore on the same +directory, in this process or another, throws Crafter::DB::LockedError — +on a lock file beside the table, held for the store's life, since the +table's own lock moves with the file a Compact replaces. + +Every change a call answers as made is flushed before the call returns: +the app deletes imsd's copy of a text as soon as Add has returned, and a +crash must not take the only copy with it. +*/ export module ImsdMessages:Store; import std; +import Crafter.DB; +import :Model; + +namespace ImsdMessages { + // imsd's id of a text, bounded as imsd bounds it (its Delete refuses + // one over 64 bytes): `ims-sms---` for the texts it + // sends, `ims-sms-in---` for the received ones, unique + // across its runs by the run's token (§8.4 of its AGENTS.md). Inline + // in the row, so recording one is an in-place write, as the state's. + inline constexpr std::size_t kImsdIdBytes = 64; + using ImsdId = Crafter::DB::FixedString; + constexpr bool FitsImsdId(std::string_view id) noexcept { return id.size() <= kImsdIdBytes; } + + // Message without its id: the row id is the id. ToRow/FromRow keep the + // two in step; a changed column is a version bump by Crafter.DB's rules. + struct MessageRow { + std::string conversation; + std::string peer; + std::string planCode; + std::string planOperator; + Direction direction; + std::string text; + std::int64_t sortTime; + std::int64_t received; + MessageState state; + std::string stateReason; + bool read; + ImsdId imsdId; + std::int32_t segments; + std::int32_t smsClass; + bool messageWaiting; + std::optional portDst; + std::optional portSrc; + std::vector data; + + static constexpr std::uint32_t version = 1; + }; + + // The file's format, held by the build: a failing assert on the layout + // is the SchemaError a user's open would throw, and the answer to it is + // a version bump that keeps this layout as `Previous`, never a new + // number here alone. The layout does not hold the two enums' values, + // which an open would misread without a word, so the last two asserts + // do: a new enumerator is appended. + static_assert(Crafter::DB::Layout::rowSize == 216); + static_assert(Crafter::DB::Layout::fingerprint == 0x390799ffa69ba100); + static_assert(std::to_underlying(Direction::Incoming) == 0 && std::to_underlying(Direction::Outgoing) == 1); + static_assert(std::to_underlying(MessageState::Received) == 0 && std::to_underlying(MessageState::Pending) == 1 && std::to_underlying(MessageState::Sent) == 2 && std::to_underlying(MessageState::Failed) == 3 && std::to_underlying(MessageState::Delivered) == 4 && std::to_underlying(MessageState::DeliveryPending) == 5 && std::to_underlying(MessageState::DeliveryFailed) == 6); + + inline constexpr Crafter::DB::Column<&MessageRow::imsdId> imsdIdColumn{}; + + // One MessageStore per directory, held for its life on a file nothing + // renames: the table's own lock sits on its file's inode, which a + // Compact replaces (a new file renamed over the old), so a second opener + // could lock the unlinked one and write beside this process. Built on + // Crafter.DB's own primitives: the directory 0700 at every component it + // creates, the lock file 0600 and never a symlink, a lock refused by + // another holder told from one that failed. + class DirectoryLock { + public: + explicit DirectoryLock(const std::filesystem::path& dir) { + Crafter::DB::Os::CreateDirectories(dir, std::filesystem::perms::owner_all); + const std::filesystem::path path = dir / "lock"; + try { + file = Crafter::DB::File::CreateExclusive(path, std::filesystem::perms::owner_read | std::filesystem::perms::owner_write); + } catch (const Crafter::DB::IOError& e) { + if (e.code != std::errc::file_exists) throw; + file = Crafter::DB::File::Open(path, true); // an earlier run's + } + try { + file.LockExclusive(); + } catch (const Crafter::DB::LockedError&) { + throw Crafter::DB::LockedError(std::format("{} is held by another imsd-messages", path.string())); + } + } + + private: + Crafter::DB::File file; // closed, the lock with it, when the store goes + }; + + MessageRow ToRow(const Message& m) { + MessageRow r{}; + r.conversation = m.conversation; + r.peer = m.peer; + r.planCode = m.plan.code; + r.planOperator = m.plan.operatorId; + r.direction = m.direction; + r.text = m.text; + r.sortTime = m.sortTime; + r.received = m.received; + r.state = m.state; + r.stateReason = m.stateReason; + r.read = m.read; + r.imsdId = ImsdId(m.imsdId); + r.segments = m.segments; + r.smsClass = m.smsClass; + r.messageWaiting = m.messageWaiting; + r.portDst = m.portDst; + r.portSrc = m.portSrc; + r.data = m.data; + return r; + } + + Message FromRow(Crafter::DB::Row row) { + const MessageRow r = row.Materialize(); + Message m; + m.id = row.Id(); + m.conversation = r.conversation; + m.peer = r.peer; + m.plan = { r.planCode, r.planOperator }; + m.direction = r.direction; + m.text = r.text; + m.sortTime = r.sortTime; + m.received = r.received; + m.state = r.state; + m.stateReason = r.stateReason; + m.read = r.read; + m.imsdId = std::string(r.imsdId.View()); + m.segments = r.segments; + m.smsClass = r.smsClass; + m.messageWaiting = r.messageWaiting; + m.portDst = r.portDst; + m.portSrc = r.portSrc; + m.data = r.data; + return m; + } + + bool Earlier(const Message& a, const Message& b) { + return a.sortTime != b.sortTime ? a.sortTime < b.sortTime : a.id < b.id; + } +} export namespace ImsdMessages { + class MessageStore { + public: + // Opens or creates /messages.cdb (a directory it creates 0700, + // the file 0600). Throws Crafter::DB::LockedError when another + // MessageStore holds it. Every write but a Compact is caught here: one the + // store refuses (Crafter.DB's IOError — the disk full, the file system + // read-only) answers nullopt (Add), false or 0 (a count) — a row Add + // appended before its Flush failed is tombstoned; an in-place write + // whose Flush failed stays in the row as this process reads it; + // SetState's state lands in place even when its reason cannot + // (a `sent` that did not land would be a retry at the next start) — + // and is told once through `onWriteState`, as is the next write + // that got room (grew a file) — so no caller can end the process on + // a full disk, and the app's banner can say so — on a file system that + // rewrites a written page in place (ext4, f2fs: the phone's); a + // copy-on-write one (btrfs) can refuse the rewrite of a mapped page + // with SIGBUS, which no gate sees — Crafter.DB maps its files. + explicit MessageStore(const std::filesystem::path& dir) : lock(dir), table(dir / "messages.cdb") {} + + std::function onWriteState; + + bool Refused() const noexcept { return refused; } + + std::optional Add(const Message& m) { + if (!FitsImsdId(m.imsdId)) return std::nullopt; // past imsd's own bound: refused, not thrown (ToRow's ImsdId would) + std::optional appended; + if (!Write([&] { + appended = table.Append(ToRow(m)); + table.Flush(); + })) { + // Append published the row before the Flush that failed: it + // goes, as the answer says (a tombstone, no I/O; the next + // Flush commits it deleted). + if (appended) table.Erase(*appended); + return std::nullopt; + } + return appended; + } + + std::optional Get(std::uint64_t id) const { + if (!table.Contains(id)) return std::nullopt; + return FromRow(table.At(id)); + } + + // The state first, in place (a uint8: never refused where a page + // rewrites in place) and flushed, then the reason, which may need room: + // a word that lands without its reason beats one lost — imsd's `sent` + // not landing would make the message a retry at the next start — and a + // word that landed is on disk before the reason's attempt, not in the + // mapping alone when that attempt is refused. False when the reason + // was refused. + bool SetState(std::uint64_t id, MessageState state, std::string_view reason) { + return Write([&] { + table.Set<&MessageRow::state>(id, state); + table.Flush(); + table.Set<&MessageRow::stateReason>(id, std::string(reason)); + table.Flush(); + }); + } + + // In place, as the state is: not refused by a full disk. False for an + // id past imsd's own bound (refused, not thrown). + bool SetImsdId(std::uint64_t id, std::string_view imsdId) { + if (!FitsImsdId(imsdId)) return false; + return Write([&] { + table.Set<&MessageRow::imsdId>(id, ImsdId(imsdId)); + table.Flush(); + }); + } + + bool SetRead(std::uint64_t id, bool read) { + return Write([&] { + table.Set<&MessageRow::read>(id, read); + table.Flush(); + }); + } + + // Marks every unread message of a conversation read; returns how many. + std::uint32_t MarkRead(std::string_view conversation) { + std::vector ids; + for (Crafter::DB::Row row : table) { + if (row[&MessageRow::conversation] == conversation && !row[&MessageRow::read]) ids.push_back(row.Id()); + } + if (ids.empty()) return 0; + if (!Write([&] { + for (const std::uint64_t id : ids) table.Set<&MessageRow::read>(id, true); + table.Flush(); + })) + return 0; + return static_cast(ids.size()); + } + + bool Erase(std::uint64_t id) { + bool erased = false; + if (!Write([&] { + erased = table.Erase(id); + if (erased) table.Flush(); + })) + return false; + return erased; + } + + std::uint64_t EraseConversation(std::string_view conversation) { + std::uint64_t erased = 0; + if (!Write([&] { + erased = table.EraseIf([&](Crafter::DB::Row row) { return row[&MessageRow::conversation] == conversation; }); + if (erased > 0) table.Flush(); + })) + return 0; + return erased; + } + + // The row with that imsd id: unique across imsd's runs (ImsdId), so + // an id names one message. + std::optional FindByImsdId(std::string_view imsdId) const { + if (imsdId.empty()) return std::nullopt; // a text imsd has not answered yet, or a retry's dropped attempt: nobody's + if (!FitsImsdId(imsdId)) return std::nullopt; // past imsd's bound: no row carries it + for (const Crafter::DB::Row row : table.Where(imsdIdColumn == ImsdId(imsdId))) return row.Id(); + return std::nullopt; + } + + // Every imsd id on record. + std::unordered_set ImsdIds() const { + std::unordered_set ids; + for (Crafter::DB::Row row : table) { + const ImsdId id = row[&MessageRow::imsdId]; + if (!id.View().empty()) ids.emplace(id.View()); + } + return ids; + } + + // A conversation's messages, oldest first (by sort time, then id). + std::vector Conversation(std::string_view conversation) const { + std::vector out; + for (Crafter::DB::Row row : table) { + if (row[&MessageRow::conversation] == conversation) out.push_back(FromRow(row)); + } + std::ranges::sort(out, Earlier); + return out; + } + + std::vector All() const { + std::vector out; + for (Crafter::DB::Row row : table) out.push_back(FromRow(row)); + return out; + } + + std::uint64_t Size() const noexcept { return table.Size(); } + + private: + // The one gate every write passes: a refusal is caught and told once; + // the recovery is told once, by a write that got room — grew a file. + // Crafter.DB allocates its extents, so a write that fit the ones it + // had (an in-place one, a short row) goes through on a full disk and + // says nothing of it. + template + bool Write(F&& write) { + const std::uint64_t before = Length(); + try { + write(); + } catch (const Crafter::DB::IOError& e) { + if (!refused) { + refused = true; + if (onWriteState) onWriteState(true, e.what()); + } + return false; + } + if (refused && Length() > before) { + refused = false; + if (onWriteState) onWriteState(false, ""); + } + return true; + } + + // The files' length: what a write that needed room grew. + std::uint64_t Length() const noexcept { return table.Stats().fileLength; } + + bool refused = false; // a write was refused and none has grown a file since + + public: + // Through the gate: a flush that fails at the end of a run is told, + // not thrown. + void Flush() { Write([&] { table.Flush(); }); } + + // Rewrites the file without tombstones; row ids change, so only at + // start or after a delete with nothing holding ids. + void Compact() { table.Compact(); } + + private: + DirectoryLock lock; // before the table: taken first, released last + Crafter::DB::Table table; + }; } diff --git a/project.cpp b/project.cpp index 388bcaf..b96a8b7 100644 --- a/project.cpp +++ b/project.cpp @@ -137,6 +137,7 @@ extern "C" Configuration CrafterBuildProject(std::span a cfg.AddTest("Time").Dependencies({ Core.get() }); cfg.AddTest("Wrap").Dependencies({ Core.get() }); cfg.AddTest("Model").Dependencies({ Core.get() }); + cfg.AddTest("Store").Dependencies({ Core.get(), db }); LintRules::AddRules(cfg); return cfg; diff --git a/tests/Store/main.cpp b/tests/Store/main.cpp new file mode 100644 index 0000000..8f697fb --- /dev/null +++ b/tests/Store/main.cpp @@ -0,0 +1,204 @@ +// SPDX-License-Identifier: GPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +// The store: round trips through a Crafter.DB table in a temp directory, +// lookups by imsd id, state and read updates, a conversation sorted by sort +// time even when an older message was appended later (the no-orderedBy +// guarantee), deletes, persistence across a reopen, a row kept when the +// process ends without a flush, the single-writer lock and the private file +// modes. +#include +#include +#include +#include +#include +import std; +import Crafter.DB; +import ImsdMessages; +using namespace ImsdMessages; +namespace fs = std::filesystem; + +namespace { + +std::int32_t failures = 0; + +void Check(bool ok, std::string_view what) { + std::println("{} {}", ok ? "PASS" : "FAIL", what); + if (!ok) ++failures; +} + +} + +Message Make(std::string_view conversation, Direction direction, std::int64_t sortTime, std::string_view imsdId, std::string_view text) { + Message m; + m.conversation = std::string(conversation); + m.peer = std::string(conversation); + m.plan = { "31", "20408" }; + m.direction = direction; + m.sortTime = sortTime; + m.received = sortTime; + m.imsdId = std::string(imsdId); + m.text = std::string(text); + m.state = direction == Direction::Incoming ? MessageState::Received : MessageState::Pending; + return m; +} + +int main() { + const fs::path dir = fs::temp_directory_path() / "imsd-messages-store"; + fs::remove_all(dir); + { + MessageStore store(dir); + Check(fs::exists(dir / "messages.cdb"), "the table file is created"); + Check((fs::status(dir / "messages.cdb").permissions() & (fs::perms::group_all | fs::perms::others_all)) == fs::perms::none, "the file is private"); + Check((fs::status(dir / "lock").permissions() & (fs::perms::group_all | fs::perms::others_all)) == fs::perms::none, "the lock file is private"); + Check((fs::status(dir).permissions() & (fs::perms::group_all | fs::perms::others_all)) == fs::perms::none, "the directory is private"); + + const std::uint64_t id0 = *store.Add(Make("+31611111111", Direction::Incoming, 300, "ims-sms-in-1-1", "newer")); + const std::uint64_t id1 = *store.Add(Make("+31622222222", Direction::Outgoing, 200, "", "ours")); + const std::uint64_t id2 = *store.Add(Make("+31611111111", Direction::Incoming, 100, "ims-sms-in-1-2", "older, appended later")); + Check(id0 == 0 && id1 == 1 && id2 == 2, "row ids are slot indices"); + Check(store.Size() == 3, "three rows"); + + const std::optional got = store.Get(id0); + Check(got && got->id == 0 && got->text == "newer" && got->conversation == "+31611111111" && got->plan == SimPlan{ "31", "20408" } && got->imsdId == "ims-sms-in-1-1", "Get round trip"); + Check(!store.Get(42), "Get of an unknown id is nullopt"); + Check(store.FindByImsdId("ims-sms-in-1-2") == std::optional(2), "FindByImsdId"); + Check(!store.FindByImsdId("ims-sms-in-9-9"), "FindByImsdId of an unknown id"); + Check(!store.FindByImsdId(""), "an empty id finds nothing: a text imsd has not answered has none"); + const std::unordered_set ids = store.ImsdIds(); + Check(ids.size() == 2 && ids.contains("ims-sms-in-1-1") && ids.contains("ims-sms-in-1-2"), "ImsdIds skips the empty one"); + + const std::vector conv = store.Conversation("+31611111111"); + Check(conv.size() == 2 && conv[0].id == 2 && conv[1].id == 0, "a conversation is sorted by sort time, not by append order"); + Check(store.Conversation("+31699999999").empty(), "an unknown conversation is empty"); + + store.SetState(id1, MessageState::Sent, "2026-10-17T00:48:32+02:00"); + store.SetImsdId(id1, "ims-sms-7"); + const std::optional sent = store.Get(id1); + Check(sent && sent->state == MessageState::Sent && sent->stateReason == "2026-10-17T00:48:32+02:00" && sent->imsdId == "ims-sms-7", "SetState and SetImsdId"); + store.SetState(id1, MessageState::Sent, "2026-10-17T00:48:32+02:00"); + Check(store.FindByImsdId("ims-sms-7") == std::optional(1), "the new imsd id is found"); + const std::string tooLong(65, 'a'); // one past imsd's own bound (64 bytes): refused, not thrown + const std::uint64_t rowsBefore = store.Size(); + Check(!store.SetImsdId(id1, tooLong) && store.Get(id1)->imsdId == "ims-sms-7" && !store.FindByImsdId(tooLong) && !store.Add(Make("+31622222222", Direction::Outgoing, 600, tooLong, "no id of imsd's")) && store.Size() == rowsBefore, "an id past imsd's 64-byte bound is refused by SetImsdId, FindByImsdId and Add"); + const std::uint64_t id3 = *store.Add(Make("+31622222222", Direction::Outgoing, 500, "ims-sms-8", "erased below")); + Check(store.FindByImsdId("ims-sms-8") == std::optional(id3), "a later row's imsd id is found"); + Check(store.Erase(id3) && !store.FindByImsdId("ims-sms-8") && store.FindByImsdId("ims-sms-7") == std::optional(1), "an erased row's id is not found; the others still are"); + + Check(store.MarkRead("+31611111111") == 2, "MarkRead marks both unread messages"); + Check(store.MarkRead("+31611111111") == 0, "MarkRead again marks nothing"); + Check(store.Get(id2) && store.Get(id2)->read, "the message is read now"); + store.SetRead(id2, false); + Check(store.Get(id2) && !store.Get(id2)->read, "SetRead"); + + bool locked = false; + try { + MessageStore second(dir); + } catch (const Crafter::DB::LockedError&) { + locked = true; + } + Check(locked, "a second store on the directory throws LockedError"); + + Check(store.EraseConversation("+31611111111") == 2, "EraseConversation removes both"); + Check(store.Size() == 1, "one row left"); + Check(store.All().size() == 1 && store.All()[0].id == 1, "All after the erase"); + store.Flush(); + } + { + MessageStore store(dir); + Check(store.Size() == 1, "the row survives a reopen"); + const std::optional kept = store.Get(1); + Check(kept && kept->state == MessageState::Sent && kept->imsdId == "ims-sms-7" && kept->text == "ours", "the updates survive a reopen"); + store.Compact(); + Check(store.Size() == 1 && store.Get(0) && store.Get(0)->text == "ours", "Compact renumbers the surviving row"); + } + // A process that ends without the destructor's flush, as a crash does: + // what Add returned is on disk. + fs::remove_all(dir); + const pid_t child = fork(); + if (child == 0) { + MessageStore store(dir); + store.Add(Make("+31633333333", Direction::Incoming, 400, "ims-sms-in-3-1", "kept across a crash")); + _exit(0); + } + std::int32_t childStatus = 0; + waitpid(child, &childStatus, 0); + { + MessageStore store(dir); + Check(store.Size() == 1 && store.Get(0) && store.Get(0)->text == "kept across a crash", "a row Add returned survives a process that ends without a flush"); + } + // A store that refuses a write — the file-size limit standing in for a + // full disk: the next extent cannot be allocated — leaves the rows as + // they were, answers nullopt, tells of it once, and tells once of the + // next write that grows a file (one that fits in place goes through + // untold). + { + const fs::path limitDir = fs::temp_directory_path() / "imsd-messages-store-limit"; + fs::remove_all(limitDir); + MessageStore limited(limitDir); + std::vector> told; + limited.onWriteState = [&](bool refused, std::string_view why) { told.emplace_back(refused, std::string(why)); }; + signal(SIGXFSZ, SIG_IGN); // past the limit the kernel sends it; ignored, the allocation fails with EFBIG + rlimit before{}; + getrlimit(RLIMIT_FSIZE, &before); + rlim_t largest = 0; + for (const auto& entry : fs::directory_iterator(limitDir)) if (entry.is_regular_file()) largest = std::max(largest, static_cast(entry.file_size())); + rlimit limit{}; + limit.rlim_cur = largest; + limit.rlim_max = before.rlim_max; + setrlimit(RLIMIT_FSIZE, &limit); + const std::string filler(4096, 'x'); + std::size_t added = 0; + bool refusedOnce = false; + for (std::size_t i = 0; i < 5000 && !refusedOnce; ++i) { + if (limited.Add(Make("+31600000000", Direction::Incoming, 1000 + static_cast(i), std::format("ims-sms-in-9-{}", i), filler))) ++added; + else refusedOnce = true; + } + Check(refusedOnce && limited.Refused() && told.size() == 1 && told[0].first && told[0].second.contains("cannot allocate"), "a write past the limit is refused and told once, with the reason"); + Check(!limited.Add(Make("+31600000000", Direction::Incoming, 9000, "ims-sms-in-9-again", filler)) && told.size() == 1 && limited.Size() == added, "a second refusal is not told again; nothing was added"); + Check(limited.SetState(0, MessageState::Sent, "") && limited.Refused() && told.size() == 1, "an in-place write goes through on the full disk and is no recovery"); + Check(limited.SetImsdId(0, "ims-sms-8a1b2c3d-1793559285-1") && limited.Get(0)->imsdId == "ims-sms-8a1b2c3d-1793559285-1" && limited.FindByImsdId("ims-sms-8a1b2c3d-1793559285-1") == std::optional(0) && limited.Refused() && told.size() == 1, "imsd's id lands in place on the full disk too, inline in the row, and is found"); + const std::string longReason(4096, 'r'); // what the filler's refused text could not fit: the heap's room is less + Check(!limited.SetState(0, MessageState::Failed, longReason) && limited.Get(0)->state == MessageState::Failed && limited.Get(0)->stateReason.empty() && told.size() == 1, "a state lands in place on the full disk; its reason, needing room, does not: false, told already"); + setrlimit(RLIMIT_FSIZE, &before); + Check(limited.Add(Make("+31600000000", Direction::Incoming, 9001, "ims-sms-in-9-room", filler)).has_value() && !limited.Refused() && told.size() == 2 && !told[1].first && limited.Size() == added + 1, "room again: the write goes through and the recovery is told once"); + fs::remove_all(limitDir); + } + // A Flush that fails after its Append went through — the disk failing, + // not full: for the one write the table's descriptor is pointed at + // /dev/null, whose fdatasync is refused — tombstones the row Add says + // it refused. + { + const fs::path flushDir = fs::temp_directory_path() / "imsd-messages-store-flush"; + fs::remove_all(flushDir); + MessageStore store(flushDir); + std::vector> told; + store.onWriteState = [&](bool refused, std::string_view why) { told.emplace_back(refused, std::string(why)); }; + Check(store.Add(Make("+31600000000", Direction::Incoming, 100, "ims-sms-in-f-1", "before")).has_value(), "a row before"); + std::int32_t fd = -1; + const fs::path table = fs::canonical(flushDir / "messages.cdb"); + for (const auto& entry : fs::directory_iterator("/proc/self/fd")) { + std::error_code ec; + if (fs::read_symlink(entry.path(), ec) == table) fd = std::stoi(entry.path().filename().string()); + } + Check(fd >= 0, "the table's descriptor is found"); + const std::int32_t saved = dup(fd); + const std::int32_t null = open("/dev/null", O_RDWR); + dup2(null, fd); + close(null); + Check(!store.Add(Make("+31600000000", Direction::Incoming, 101, "ims-sms-in-f-2", "unflushed")).has_value() && store.Refused() && told.size() == 1 && told[0].first, "an Add whose Flush fails is refused and told"); + Check(store.Size() == 1 && !store.FindByImsdId("ims-sms-in-f-2") && store.All().size() == 1, "the row it appended is gone: not found, not counted"); + dup2(saved, fd); + close(saved); + const std::string big(4096, 'x'); + // Enough Adds to grow the heap whatever the kernel's page size: its + // first extent is 16 KiB on a 4K kernel, 64 KiB on a 64K one (the + // library rounds extents to the page); the loop stops at the recovery. + for (std::size_t i = 0; i < 64 && store.Refused(); ++i) store.Add(Make("+31600000000", Direction::Incoming, 200 + static_cast(i), std::format("ims-sms-in-f-{}", 10 + i), big)); + Check(!store.Refused() && told.size() == 2 && !told[1].first, "the descriptor back, a write that grows the file tells of the recovery"); + fs::remove_all(flushDir); + } + + fs::remove_all(dir); + return failures == 0 ? 0 : 1; +}