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.
This commit is contained in:
Jorijn van der Graaf 2026-11-01 18:11:01 +01:00
commit 53bc4797bc
Signed by: jorijnvdgraaf
GPG key ID: 2937E59CDCC1BCFB
5 changed files with 242 additions and 23 deletions

View file

@ -29,6 +29,13 @@ void Check(bool ok, std::string_view what) {
}
bool FileHolds(const fs::path& path, std::string_view needle) {
std::ifstream in(path, std::ios::binary);
Check(in.is_open(), "the file opens for the byte check");
const std::string bytes((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
return bytes.find(needle) != std::string::npos;
}
Message Make(std::string_view conversation, Direction direction, std::int64_t sortTime, std::string_view imsdId, std::string_view text) {
Message m;
m.conversation = std::string(conversation);
@ -107,10 +114,29 @@ int main() {
{
MessageStore store(dir);
Check(store.Size() == 1, "the row survives a reopen");
Check(store.ErasedImsdIds() == std::unordered_set<std::string>{ "ims-sms-in-1-1", "ims-sms-in-1-2" }, "the deleted received texts' imsd ids are remembered across a reopen");
Check(store.ImsdIds().contains("ims-sms-in-1-1") && !store.ErasedImsdIds().contains("ims-sms-8"), "and count as known; a deleted sent text's id is not remembered");
store.ForgetErased({ "ims-sms-in-1-2" }, store.ErasedImsdIds());
Check(store.ErasedImsdIds() == std::unordered_set<std::string>{ "ims-sms-in-1-2" }, "the ids imsd no longer lists are forgotten");
Check(FileHolds(dir / "erased.cdb", "ims-sms-in-1-1"), "the forgotten id's bytes are still in the second file");
Check((fs::status(dir / "erased.cdb").permissions() & (fs::perms::group_all | fs::perms::others_all)) == fs::perms::none, "the second table's file is private");
const std::optional<Message> kept = store.Get(1);
Check(kept && kept->state == MessageState::Sent && kept->imsdId == "ims-sms-7" && kept->text == "ours", "the updates survive a reopen");
// The deleted texts' bytes are in the file until a Compact rewrites it.
Check(store.HasDeleted(), "the erased rows are still in the file");
Check(FileHolds(dir / "messages.cdb", "newer") && FileHolds(dir / "messages.cdb", "older"), "their texts are still in the file");
store.Compact();
Check(store.Size() == 1 && store.Get(0) && store.Get(0)->text == "ours", "Compact renumbers the surviving row");
Check(!store.HasDeleted(), "nothing deleted is left after it");
}
{
MessageStore store(dir);
Check(!FileHolds(dir / "erased.cdb", "ims-sms-in-1-1") && FileHolds(dir / "erased.cdb", "ims-sms-in-1-2"), "the forgotten id's bytes are gone from the second file after the Compact, the remembered one's kept");
Check(!FileHolds(dir / "messages.cdb", "newer") && !FileHolds(dir / "messages.cdb", "older") && FileHolds(dir / "messages.cdb", "ours"), "the deleted texts' bytes are gone after the Compact and a reopen; the kept text's stay");
store.ForgetErased({}, store.ErasedImsdIds());
Check(store.HasDeleted(), "a forgotten id alone is something a Compact drops");
store.Compact();
Check(!store.HasDeleted() && store.ErasedImsdIds().empty(), "and the second file is rewritten with the first");
}
// A process that ends without the destructor's flush, as a crash does:
// what Add returned is on disk.