fix(store,host): books list with nothing running says which books wait for a format upgrade

Plan P15 unit A5, fix round. With nothing running, `books list` reads the folder
and takes no lock (nothing is opened), so it stopped showing which companies wait
for a format upgrade, which a server showed. It now reads what each journal
declares (`ledger_store::declared_format`: no lock, nothing written, the hash
chain not verified, so it says what the books declare and no more) and adds
"; waiting for a format upgrade (`wanderbooks books upgrade ID`)"; a journal that
cannot be read says why on its line. The listing no longer fails when a company's
folder vanishes while it is made (a book put aside meanwhile): a vanished entry is
skipped, not an error (`list_books_inner` used `entry?`).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 12:19:58 -07:00
1 parent c47144a401
commit 2275c34e86
5 files changed
+115 -10

No files matched your search

+54
View File
@@ -447,6 +447,35 @@ impl JournalFormat {
}
}
/// The format a book's journal declares, read without opening the book:
/// no lock is taken and nothing is written, and the hash chain is not
/// verified (this is what the journal says, for a program that only lists
/// the books while none is open: `wanderbooks books list`). It is the
/// last `journal.format_set` line, or format 1 for books that never
/// declared one; a line that can't be read is an `Err`, and so is a
/// format this version doesn't read.
pub fn declared_format(book_dir: &std::path::Path) -> Result<JournalFormat, StoreError> {
use std::io::BufRead;
let file = std::fs::File::open(book_dir.join(crate::store::JOURNAL_FILE))?;
let mut format = JournalFormat::default();
for raw in std::io::BufReader::new(file).split(b'\n') {
let raw = raw?;
// Only the declaration lines are read in full.
if !raw
.windows(FORMAT_SET.len())
.any(|window| window == FORMAT_SET.as_bytes())
{
continue;
}
if let Ok(line) = JournalLine::parse(&raw)
&& line.line_type == FORMAT_SET
{
format = declared(&line)?;
}
}
Ok(format)
}
/// The journal format this version writes: the newest format it reads,
/// with every feature it knows, sorted. New books declare it as they are
/// created ([`crate::Store::create`]), and books in an older format move
@@ -711,6 +740,31 @@ pub(crate) fn settle(refusal: StoreError, hash: &str, next: Option<&[u8]>) -> St
#[cfg(test)]
mod tests {
/// `declared_format` reads what a journal declares with no store open:
/// new books declare this version's, a journal that never declared one
/// is format 1 (and waits for an upgrade), and the file is untouched.
#[test]
fn a_format_is_read_from_the_journal_without_opening_it() {
let dir = tempfile::tempdir().unwrap();
let book = dir.path().join("acme");
drop(crate::Store::create(&book, ledger_core::Currency::USD).unwrap());
let journal = book.join(crate::store::JOURNAL_FILE);
let before = std::fs::read(&journal).unwrap();
let declared = super::declared_format(&book).unwrap();
assert_eq!(declared, super::current());
assert!(declared.upgrade().is_none());
assert_eq!(std::fs::read(&journal).unwrap(), before, "nothing written");
// Books written before the gate declared nothing: format 1.
let old = dir.path().join("old");
std::fs::create_dir(&old).unwrap();
std::fs::write(old.join(crate::store::JOURNAL_FILE), b"").unwrap();
let declared = super::declared_format(&old).unwrap();
assert_eq!(declared, super::JournalFormat::default());
assert!(declared.upgrade().is_some());
// No journal at all is an error, not format 1.
assert!(super::declared_format(&dir.path().join("nope")).is_err());
}
use super::*;
use crate::store::{Store, apply_line_to_sqlite, replay_line_into_ledger};
use ledger_core::{Currency, Ledger};
+1 -1
View File
@@ -49,7 +49,7 @@ pub mod winsec;
pub use blob::BlobStore;
pub use error::StoreError;
pub use format::{FormatUpgrade, JournalFormat, SUPPORTED_JOURNAL_FORMAT};
pub use format::{FormatUpgrade, JournalFormat, SUPPORTED_JOURNAL_FORMAT, declared_format};
pub use journal::{GENESIS_PREV, JournalLine, JournalWriter};
pub use snapshot::{DiskCheck, Snapshot};
pub use store::audit;
+6 -2
View File
@@ -809,14 +809,18 @@ impl Root {
let (books, _) = self.private_subdir(BOOKS_DIR, false)?;
let mut ids = Vec::new();
for entry in std::fs::read_dir(books)? {
let entry = entry?;
// A folder that vanishes while the list is made (a book put
// aside or removed meanwhile) is not one of the books.
let Ok(entry) = entry else { continue };
let Some(name) = entry.file_name().to_str().map(str::to_string) else {
continue;
};
let Ok(id) = BookId::parse(&name) else {
continue;
};
if entry.file_type()?.is_dir() && entry.path().join(JOURNAL_FILE).is_file() {
if entry.file_type().is_ok_and(|t| t.is_dir())
&& entry.path().join(JOURNAL_FILE).is_file()
{
ids.push(id);
}
}