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:
1 parent
c47144a401
commit
2275c34e86
5 files changed
+115
-10
No files matched your search
@@ -1039,3 +1039,25 @@ fn shell_join(words: &[String]) -> String {
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ")
|
||||
}
|
||||
|
||||
/// With nothing running, `books list` reads the folder and says which
|
||||
/// companies wait for a format upgrade, from what their journals declare
|
||||
/// (no lock, nothing written): it used to say so only through a server.
|
||||
#[test]
|
||||
fn a_list_with_nothing_running_says_which_books_wait_for_an_upgrade() {
|
||||
let home = Home::new("list-upgrade");
|
||||
let dir = old_books(&home, "acme");
|
||||
let before = journal(&dir);
|
||||
let out = home.books(&["list"]);
|
||||
let said = String::from_utf8_lossy(&out.stdout).into_owned();
|
||||
assert!(out.status.success(), "{out:?}");
|
||||
assert!(
|
||||
said.contains("acme not open: Wanderbooks isn't running")
|
||||
&& said.contains("waiting for a format upgrade (`wanderbooks books upgrade acme`)"),
|
||||
"{said}"
|
||||
);
|
||||
// Reading it changed nothing and left nothing running.
|
||||
assert_eq!(journal(&dir), before);
|
||||
assert!(!ledger_host::opener::server_lock_held(&home.run_dir()));
|
||||
assert!(!ledger_host::opener::host_lock_held(&home.run_dir()));
|
||||
}
|
||||
@@ -829,13 +829,13 @@ pub fn books(
|
||||
common.ask_upgrades = !matches!(words.as_slice(), ["upgrade", ..]);
|
||||
let answer = match words.as_slice() {
|
||||
["list"] => match listed_while_closed(&common) {
|
||||
Ok(Some(ids)) => {
|
||||
if ids.is_empty() {
|
||||
Ok(Some(listed)) => {
|
||||
if listed.is_empty() {
|
||||
println!("No books yet: `wanderbooks books create ID` starts some.");
|
||||
}
|
||||
for id in ids {
|
||||
for (id, upgrade) in listed {
|
||||
println!(
|
||||
"{id} not open: Wanderbooks isn't running (start it, or run `wanderbooks serve`, to open it)"
|
||||
"{id} not open: Wanderbooks isn't running (start it, or run `wanderbooks serve`, to open it){upgrade}"
|
||||
);
|
||||
}
|
||||
return Ok(());
|
||||
@@ -1032,8 +1032,13 @@ pub fn books(
|
||||
/// The books of the root by name, read from the folder and no more, when
|
||||
/// nothing runs: no host and no server holds the root, so there is no open
|
||||
/// or closed to tell, and nothing is changed or read of the books, which
|
||||
/// is why it takes no lock (WB-app-5). `None` when something runs: ask it.
|
||||
fn listed_while_closed(common: &Common) -> Result<Option<Vec<String>>, String> {
|
||||
/// is why it takes no lock (WB-app-5), with what each waits for in words:
|
||||
/// a format upgrade is read from what the journal declares
|
||||
/// (`ledger_store::declared_format`: no lock, nothing written, the chain
|
||||
/// not verified, so it says what the books declare and no more), and a
|
||||
/// journal that can't be read says so and is not skipped. `None` when
|
||||
/// something runs: ask it.
|
||||
fn listed_while_closed(common: &Common) -> Result<Option<Vec<(String, String)>>, String> {
|
||||
let found =
|
||||
ledger_store::root::Root::find(common.root.as_deref()).map_err(|e| e.to_string())?;
|
||||
let run = found.run_dir().map_err(|e| e.to_string())?;
|
||||
@@ -1041,7 +1046,27 @@ fn listed_while_closed(common: &Common) -> Result<Option<Vec<String>>, String> {
|
||||
return Ok(None);
|
||||
}
|
||||
let ids = found.list_books().map_err(|e| e.to_string())?;
|
||||
Ok(Some(ids.iter().map(|id| id.to_string()).collect()))
|
||||
Ok(Some(
|
||||
ids.iter()
|
||||
.filter_map(|id| {
|
||||
let waits = match ledger_store::declared_format(&found.book_dir(id)) {
|
||||
Ok(declared) if declared.upgrade().is_some() => {
|
||||
format!("; waiting for a format upgrade (`wanderbooks books upgrade {id}`)")
|
||||
}
|
||||
Ok(_) => String::new(),
|
||||
// A book that vanished since the list is not one of the
|
||||
// books; one that can't be read says why.
|
||||
Err(ledger_store::StoreError::Io(e))
|
||||
if e.kind() == std::io::ErrorKind::NotFound =>
|
||||
{
|
||||
return None;
|
||||
}
|
||||
Err(e) => format!("; its format isn't known: {e}"),
|
||||
};
|
||||
Some((id.to_string(), waits))
|
||||
})
|
||||
.collect(),
|
||||
))
|
||||
}
|
||||
|
||||
fn book_line(b: &admin::BookStatus) -> String {
|
||||
|
||||
@@ -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};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user