imsd-messages/interfaces/ImsdMessages-Store.cppm

429 lines
19 KiB
Text
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
/*
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
ImsdMessages:Store — the app's message database: a Crafter.DB table of
the messages (messages.cdb) and one of the imsd ids of received texts the
user deleted while imsd may still hold their copies (erased.cdb).
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
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
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
sorted here. One MessageStore per directory: a second one on the same
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
directory, in this process or another, throws Crafter::DB::LockedError —
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
on a lock file beside the tables, held for the store's life, since a
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
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;
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
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-<run>-<start>-<N>` for the texts it
// sends, `ims-sms-in-<run>-<epoch>-<N>` 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<kImsdIdBytes>;
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<std::uint32_t> portDst;
std::optional<std::uint32_t> portSrc;
std::vector<std::uint8_t> 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<MessageRow>::rowSize == 216);
static_assert(Crafter::DB::Layout<MessageRow>::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{};
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
// A received text the user deleted while imsd may still hold its copy
// (a delete it refused or that timed out): its imsd id, so a later List
// does not import it; dropped once imsd no longer lists it.
struct ErasedRow {
ImsdId imsdId;
static constexpr std::uint32_t version = 1;
};
// Held by the build as MessageRow's format is.
static_assert(Crafter::DB::Layout<ErasedRow>::rowSize == 65);
static_assert(Crafter::DB::Layout<ErasedRow>::fingerprint == 0xad3324d2c7d75ee0);
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
// 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<MessageRow> 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 {
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
class MessageStore {
public:
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
// Opens or creates <dir>/messages.cdb and erased.cdb (a directory it
// creates 0700, the files 0600). Throws Crafter::DB::LockedError when another
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
// 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.
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
explicit MessageStore(const std::filesystem::path& dir) : lock(dir), table(dir / "messages.cdb"), erased(dir / "erased.cdb") {}
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
std::function<void(bool refused, std::string_view why)> onWriteState;
bool Refused() const noexcept { return refused; }
std::optional<std::uint64_t> 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<std::uint64_t> 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<Message> 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<std::uint64_t> ids;
for (Crafter::DB::Row<MessageRow> 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<std::uint32_t>(ids.size());
}
bool Erase(std::uint64_t id) {
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
// The id first, the row after: a crash between the two leaves a
// remembered id, never a text that comes back — and a remembering
// the store refuses refuses the delete.
if (const std::optional<Message> m = Get(id); m && m->direction == Direction::Incoming && !m->imsdId.empty() && !RememberErased(m->imsdId)) return false;
bool gone = false;
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
if (!Write([&] {
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
gone = table.Erase(id);
if (gone) table.Flush();
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
}))
return false;
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
return gone;
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
}
std::uint64_t EraseConversation(std::string_view conversation) {
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
// The ids first, the rows after, as Erase does: a crash between
// the two leaves a remembered id, never a text that comes back.
// One append and one flush for all of them.
std::vector<ErasedRow> remembered;
for (Crafter::DB::Row<MessageRow> row : table) {
if (row[&MessageRow::conversation] != conversation) continue;
const ImsdId id = row[&MessageRow::imsdId];
if (row[&MessageRow::direction] == Direction::Incoming && !id.View().empty()) remembered.push_back(ErasedRow{ id });
}
if (!remembered.empty() && !Write([&] {
erased.Append(std::span<const ErasedRow>(remembered));
erased.Flush();
}))
return 0;
std::uint64_t gone = 0;
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
if (!Write([&] {
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
gone = table.EraseIf([&](Crafter::DB::Row<MessageRow> row) { return row[&MessageRow::conversation] == conversation; });
if (gone > 0) table.Flush();
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
}))
return 0;
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
return gone;
}
// The imsd ids of received texts the user deleted, kept until imsd no
// longer lists them: a List does not import them again, and imsd's
// copy is deleted once this session may (at the next List imsd admits
// or after its restart, when polkit refused). False when the store
// refused the write.
bool RememberErased(std::string_view imsdId) {
if (!FitsImsdId(imsdId)) return false; // past imsd's bound: no row carries it
return Write([&] {
erased.Append(ErasedRow{ ImsdId(imsdId) });
erased.Flush();
});
}
std::unordered_set<std::string> ErasedImsdIds() const {
std::unordered_set<std::string> ids;
for (Crafter::DB::Row<ErasedRow> row : erased) ids.emplace(row[&ErasedRow::imsdId].View());
return ids;
}
// Drops the remembered ids a List no longer carries — of those
// remembered before that List was sent (`before`): an id remembered
// while it was in flight belongs to a text its answer could not
// carry, and waits for the next.
void ForgetErased(const std::unordered_set<std::string>& listed, const std::unordered_set<std::string>& before) {
if (before.empty()) return; // nothing to forget: no write, no word on the store's state
Write([&] {
const std::uint64_t gone = erased.EraseIf([&](Crafter::DB::Row<ErasedRow> row) { const std::string id(row[&ErasedRow::imsdId].View()); return before.contains(id) && !listed.contains(id); });
if (gone > 0) erased.Flush();
});
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
}
// The row with that imsd id: unique across imsd's runs (ImsdId), so
// an id names one message.
std::optional<std::uint64_t> 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<MessageRow> row : table.Where(imsdIdColumn == ImsdId(imsdId))) return row.Id();
return std::nullopt;
}
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
// Every imsd id on record: the stored texts' and the deleted ones'
// imsd still holds.
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
std::unordered_set<std::string> ImsdIds() const {
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
std::unordered_set<std::string> ids = ErasedImsdIds();
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
for (Crafter::DB::Row<MessageRow> 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<Message> Conversation(std::string_view conversation) const {
std::vector<Message> out;
for (Crafter::DB::Row<MessageRow> row : table) {
if (row[&MessageRow::conversation] == conversation) out.push_back(FromRow(row));
}
std::ranges::sort(out, Earlier);
return out;
}
std::vector<Message> All() const {
std::vector<Message> out;
for (Crafter::DB::Row<MessageRow> 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 <class F>
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.
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
std::uint64_t Length() const noexcept { return table.Stats().fileLength + erased.Stats().fileLength; }
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
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.
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
void Flush() {
Write([&] {
table.Flush();
erased.Flush();
});
}
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
// Rewrites both files without tombstones; the messages' row ids
// change, so only at start or after a delete with nothing holding
// ids (nothing holds an erased id).
void Compact() {
table.Compact();
erased.Compact();
}
// Deleted rows, or the bytes an update or a delete orphaned, still in
// either file: what a Compact would drop.
bool HasDeleted() const noexcept {
const auto garbage = [](const auto& s) { return s.liveCount < s.slotCount || s.heapGarbage > 0; };
return garbage(table.Stats()) || garbage(erased.Stats());
}
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
private:
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
DirectoryLock lock; // before the tables: taken first, released last
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
Crafter::DB::Table<MessageRow> table;
Store, Sync, main: a deleted text stays deleted imsd keeps a received text until an app deletes it, and a delete can be refused (polkit, for a session that is not the active one) or time out: the List after the next start would carry the text again and the app would import it again (within a run Sync's own record of what it imported keeps it out). And a deleted row is a tombstone whose bytes stay in the file until it is rewritten. The store gains a second table, erased.cdb: the imsd ids of received texts the user deleted while imsd may still hold their copies, its format held by the build as the first table's is. Erase and EraseConversation remember the id before the row goes (a crash between the two leaves a remembered id, never a text that comes back; a remembering the store refuses refuses the delete), ImsdIds folds the remembered ids in, so Sync's `known` keeps a List from importing them, and ForgetErased drops the ids a List no longer carries — of those remembered before that List was sent: an id remembered while it was in flight waits for the next (FetchList's `before` snapshot). Sync's Erase and EraseConversation go to the store; Fail and the Send continuation check that the row still exists, so a text deleted while its Send is in flight takes no late word, and a late answer for a deleted message finds no row and is logged. main compacts both files at start when either holds deleted rows or bytes an update or a delete orphaned (a state reason replaced by another can leave some, so a start after a send may rewrite both with nothing deleted), and opens the store again on what is there if the rewrite failed: a deleted text's bytes leave the files at the next start (a resident instance keeps them until then — nothing holds a row id across the rewrite but the app itself). Tested: tests/Store — the remembered ids across a reopen and as known, a deleted sent text's id not remembered, the ids imsd no longer lists forgotten, the bytes of deleted texts and forgotten ids gone after the Compact and a reopen while the kept ones stay, a forgotten id alone enough for a Compact, the second file private and rewritten with the first; tests/Sync — a message deleted while it is sending (its late answers write nothing and are logged), a received text imported, remembered when deleted and forgotten when no longer listed, a text signalled while a List is in flight kept by the List sent before the delete — which forgets the id remembered before it went out — and forgotten by the next, and a state for another client's text leaving every message alone.
2026-11-01 18:11:01 +01:00
Crafter::DB::Table<ErasedRow> erased;
Store: the message database on Crafter.DB One table without an ordered column: Crafter.DB enforces the order on every write, and a List() import after a restart can carry an older stamp than the last row, so a conversation is sorted on read. A row carries the plan it was written under, the SIM's country code and its operator, a column each. Messages are looked up by imsd id (unique across imsd's runs by its run token: an id names one message, and an empty one — a text imsd has not answered yet — none); read marks and deletes go per message and per conversation; every write but a Compact is caught at this one boundary — a store that refuses one (Crafter.DB's IOError: the disk full, the file system read-only) answers nullopt from Add, false or a count of 0 (a row appended before its Flush failed is tombstoned; an in-place write whose Flush failed stays in the row as the process reads it; SetState's state and imsd's id land in place even when a reason cannot — the id is inline in the row, 64 bytes as imsd bounds it, one past that refused rather than thrown), and tells its onWriteState listener once of the refusal and once of the next write that got room, which is one that grew a file: Crafter.DB allocates its extents, so a write that fit the ones it had (an in-place one, a short row) is no recovery. So a refused write ends no caller's process and the app's banner can say so. A lock file beside the table, held for the store's life, makes a second store on the same directory throw LockedError (the table's own lock sits on an inode a Compact replaces); it is built on Crafter.DB's File and Os — the directory made 0700 at every component, the file 0600 and never a symlink, a lock that fails for any reason but another holder an IOError, not a LockedError. Every change a call answers as made — a row id, true, a count — is committed before the call returns, so a row Add returned is on disk. The file's format is held by the build: static_asserts on the row's size and layout fingerprint (Crafter.DB's Layout) and on the stored values of Direction and MessageState, so a column changed without a version bump fails the build and not a user's open, and an enumerator moved fails it where an open would misread the stored states without a word. Tested: tests/Store in a temp directory: the round trip, the sort of a later-appended older message, the updates, the lookup by imsd id and an erased row's, an id past imsd's bound refused, the lock from a second open, persistence across a reopen, Compact, a row kept by a child process that ends without a flush, the private file modes, and a write refused under the file-size limit (told once with its reason, the rows before it standing, an in-place write going through and telling nothing, a state landing without its refused reason and imsd's id beside it, the next Add after the limit's lifting telling of the recovery once), and a Flush failing after its Append (the descriptor pointed at /dev/null for the one write) tombstoning the row Add refused.
2026-10-17 23:25:48 +02:00
};
}