fix(mcp,store): a close stops every call and job that can run long, and says so; an unrecorded refusal never ran

A close waited, unbounded, for a running books.back_up_now (git only Backups::shut
could cancel), for a restore, create or adopt (they hold the one lifecycle lock,
and at the server's close admin_jobs.wait() waited for them before anything was
released), and, through the store lock, for a verification or a package reading
every stored file. The bound's T_calls term was not finite.

Now a close calls hurry at its start, and the stoppable work looks at it:
books.back_up_now and timer-asked backups (git stopped, a stopped run records
'stopped' and no result of its own), books.verify (per journal line and file; a
heal of a drifted database is begun only if no stop came before it: stopped half
way it would leave the live ledger part replayed), books.export_cpa_package (per
stored file). Such a call answers that it was stopped because the books are
closing, and its access record says refused: stopped_by_close. On the admin
socket restore, adopt, create and open take a JobStop: asked at each phase and
file copied, they remove what they wrote and leave the root byte for byte as it
was; the renames that put the books in place are the point of no return, after
which they finish (two renames and two syncs). A close of any book, and the
server's own, raises the flag before it waits for the lifecycle lock.

A close whose last backup had to be killed in the owner's folder now says which
file was left, and the target's backup is marked failed by shut when the run's
future was dropped at its bound. INVARIANT 10 is rewritten so each term of the
bound is enforced by code, and lists the calls that can run long (and the one
known exception, books.import_bank_csv); INVARIANTS 8 and 9 say exactly which
refusals are recorded and why an unrecorded one never ran; the format upgrade's
wording says a stop is honoured up to its final look.

Tests: the close is held to the bound while a backup call, a verification, a
package and each admin job is running and would never end; the stopped job leaves
the root byte for byte as it was; a lock left in a backup folder is reported and
never removed. Sleeps that stood for 'the other side got there' now wait on the
condition (Expect: 100-continue for a half-sent body, a writable named pipe for a
fetch that is reading).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Sonnet 5.5 committed 2026-11-20 17:15:36 -07:00
1 parent 6f174f3f87
commit aa64df9a25
18 files changed
+1889 -248

No files matched your search

+19 -9
View File
@@ -55,7 +55,7 @@
//! stopped gently: SIGTERM to its whole process group first (git
//! removes the lock files it holds when it is sent one, and a push's
//! receiving end the objects it had not yet kept), up to TERM_GRACE
//! (3 s: git's handler takes milliseconds, so the grace is only for a
//! (5 s: git's handler takes milliseconds, so the grace is only for a
//! machine too loaded to schedule it) for the group to end, and only
//! then SIGKILL. So a lock left behind is the rare case, and there is
//! nothing to clean up in the usual one. (Windows has no signal to
@@ -138,9 +138,11 @@ pub const BACKED_UP: [&str; 5] = [
pub const GIT_TIMEOUT: Duration = Duration::from_secs(300);
/// How long a git that is to be stopped is given, after SIGTERM, to end
/// (and so remove its own lock files) before its whole process group is
/// killed (INVARIANT 5). Git's handler takes milliseconds; this is for a
/// machine so loaded that the handler is slow to be scheduled.
pub const TERM_GRACE: Duration = Duration::from_secs(3);
/// killed (INVARIANT 5). Git's handler takes milliseconds; the grace is for
/// a machine so loaded that the handler is slow to be scheduled. It is
/// generous on purpose: a lock left in the owner's folder costs the owner a
/// step by hand, while a longer grace costs only a git that ignores SIGTERM.
pub const TERM_GRACE: Duration = Duration::from_secs(5);
/// How old a lock in the book's own archive must be, at open and when a
/// backup begins, before it is taken for a dead git's (INVARIANT 5a): a
/// younger one is left, and the next backup tries again.
@@ -749,7 +751,10 @@ impl Watched {
fn open(&self) -> std::io::Result<crate::git_locks::RepoDir> {
if self.is_archive() {
match (self.dir.parent(), self.dir.file_name().and_then(|n| n.to_str())) {
match (
self.dir.parent(),
self.dir.file_name().and_then(|n| n.to_str()),
) {
(Some(book), Some(name)) => crate::git_locks::RepoDir::open_in(book, name),
_ => Err(std::io::ErrorKind::InvalidInput.into()),
}
@@ -2937,7 +2942,10 @@ mod tests {
assert!(Path::new(path).exists(), "{path}");
}
let words = said.to_string();
assert!(words.contains("nothing else is using the backup folder"), "{words}");
assert!(
words.contains("nothing else is using the backup folder"),
"{words}"
);
assert!(!words.contains("another.lock"), "{words}");
// Left where it was, and the one before our push untouched.
assert!(lock.exists(), "a file in the owner's folder was deleted");
@@ -2946,15 +2954,17 @@ mod tests {
assert!(
notes.iter().all(|n| n.repo == "folder"
&& !n.removed
&& n.path.as_deref()
== Some(repo.join(&n.lock).display().to_string().as_str())),
&& n.path.as_deref() == Some(repo.join(&n.lock).display().to_string().as_str())),
"{notes:?}"
);
assert!(
notes.iter().any(|n| n.lock == "refs/heads/main.lock"),
"{notes:?}"
);
assert!(!notes.iter().any(|n| n.lock.contains("another")), "{notes:?}");
assert!(
!notes.iter().any(|n| n.lock.contains("another")),
"{notes:?}"
);
// Still there, a later push fails by git's own words, and the file
// stays.
std::fs::remove_file(hooks.join("slow")).unwrap();
+14
View File
@@ -22,6 +22,10 @@
//! 3. A stop is an error (`StoreError::Cancelled`), never a partial
//! success: what a stopped verification had made is the caller's to
//! remove (the books it was opened on are a scratch folder's).
//! 4. A step that can't be left half-made is shielded (`shielded`): the
//! stop is looked at before it begins, never inside it. The database's
//! rebuild from the journal (`verify_and_heal`, healing a drift) is
//! one: stopped part way it would leave the live ledger part replayed.
//! ```
use crate::error::StoreError;
@@ -49,6 +53,16 @@ pub fn scope(flag: Arc<AtomicBool>) -> Scope {
Scope(FLAG.with(|f| f.borrow_mut().replace(flag)))
}
/// Run `f` with no stop in force on this thread (the request in force
/// before comes back after): for the one step that can't be left half-made,
/// such as rebuilding the database from the journal, which would leave the
/// live ledger part replayed.
pub fn shielded<R>(f: impl FnOnce() -> R) -> R {
let outer = FLAG.with(|flag| flag.borrow_mut().take());
let _restore = Scope(outer);
f()
}
/// Whether a stop was asked for.
pub fn requested() -> bool {
FLAG.with(|f| {
+28 -4
View File
@@ -327,17 +327,41 @@ mod tests {
let (x, id) = (x.clone(), e.id.clone());
std::thread::spawn(move || x.get(&id, "t", NOW))
};
// The fetch holds the lock, blocked opening the pipe for reading
// (no writer yet): give it time to be there, then close.
std::thread::sleep(std::time::Duration::from_millis(300));
// The fetch holds the lock, blocked opening the pipe for reading (no
// writer yet). A writer can be opened without blocking only once a
// reader has the pipe open, so that is the condition waited for (the
// writer, kept, is the one that lets the read end later).
use std::os::unix::fs::OpenOptionsExt;
let by = std::time::Instant::now() + std::time::Duration::from_secs(60);
let writer = loop {
match std::fs::OpenOptions::new()
.write(true)
.custom_flags(libc::O_NONBLOCK)
.open(&bin)
{
Ok(w) => break w,
Err(_) => {
assert!(
std::time::Instant::now() < by,
"the fetch never reached the pipe"
);
std::thread::sleep(std::time::Duration::from_millis(5));
}
}
};
let (begun, started) = std::sync::mpsc::channel();
let (done, closed) = std::sync::mpsc::channel();
let closing = {
let x = x.clone();
std::thread::spawn(move || {
let _ = begun.send(());
x.close();
let _ = done.send(());
})
};
started.recv().unwrap();
// A negative window: it can only fail if the close returned, never
// because the machine was slow.
assert!(
closed
.recv_timeout(std::time::Duration::from_millis(500))
@@ -346,7 +370,7 @@ mod tests {
);
// A writer that sends nothing lets the read end (an empty file,
// not the size kept: the fetch is refused).
drop(std::fs::OpenOptions::new().write(true).open(&bin).unwrap());
drop(writer);
assert!(fetching.join().unwrap().is_err());
closed
.recv_timeout(std::time::Duration::from_secs(5))
+3 -10
View File
@@ -376,9 +376,7 @@ mod imp {
pub fn regular(&self, rel: &str) -> Option<Option<SystemTime>> {
let path = self.at(rel).ok()?;
let meta = std::fs::symlink_metadata(path).ok()?;
meta.file_type()
.is_file()
.then(|| meta.modified().ok())
meta.file_type().is_file().then(|| meta.modified().ok())
}
pub fn refs(&self, out: &mut Vec<Found>, depth: usize) {
@@ -414,9 +412,7 @@ mod imp {
return Ok(false);
};
match std::fs::symlink_metadata(&path) {
Ok(meta) if meta.file_type().is_file() => {
std::fs::remove_file(path).map(|()| true)
}
Ok(meta) if meta.file_type().is_file() => std::fs::remove_file(path).map(|()| true),
_ => Ok(false),
}
}
@@ -548,10 +544,7 @@ mod tests {
b"theirs",
"the link's target was touched"
);
assert_eq!(
std::fs::read(outside.join("HEAD.lock")).unwrap(),
b"theirs"
);
assert_eq!(std::fs::read(outside.join("HEAD.lock")).unwrap(), b"theirs");
// And a repository that is a link when the handle is opened is
// refused, with the archive's own `open_in` too.
assert!(RepoDir::open(&second).is_err());
+262 -51
View File
@@ -116,6 +116,10 @@ pub enum RootError {
/// Adopting was refused, with why.
#[error("{0}")]
Adopt(String),
/// A job was stopped because the books are closing, before its point of
/// no return ([`JobStop`]): nothing it did is left.
#[error("{0}")]
Stopped(String),
/// The store refused (its message with the root's path taken out).
#[error("{0}")]
Store(String, #[source] StoreError),
@@ -146,6 +150,87 @@ impl From<std::io::Error> for RootError {
}
}
/// What a job that changes which books the root holds (create, adopt,
/// restore) is asked, as it goes, whether it must stop (the books server's
/// close): looked at as each phase begins and once per stored file copied;
/// a job stops, removes what it made, and leaves the books exactly as they
/// were. Each job has a point of no return (the rename that puts its books
/// in place), after which nothing is asked and the job finishes: a few
/// renames and directory syncs, so short and bounded.
#[derive(Clone)]
pub struct JobStop {
flag: std::sync::Arc<std::sync::atomic::AtomicBool>,
phase: Option<JobPhaseWatch>,
}
/// What is told the name of each phase of a job as it begins
/// ([`JobStop::with_phase_watch`]).
pub type JobPhaseWatch = std::sync::Arc<dyn Fn(&str) + Send + Sync>;
impl JobStop {
/// Stop when `flag` is set.
pub fn new(flag: std::sync::Arc<std::sync::atomic::AtomicBool>) -> Self {
Self { flag, phase: None }
}
/// Never asked to stop (a command run on its own, not in the server).
pub fn never() -> Self {
Self::new(std::sync::Arc::default())
}
/// Told the name of each phase as it begins, before the stop is looked
/// at (a seam for tests, and the server's test knob that holds a job
/// inside a phase: it may wait, but must give up when the flag is set).
#[must_use]
pub fn with_phase_watch(mut self, watch: JobPhaseWatch) -> Self {
self.phase = Some(watch);
self
}
/// The flag: what git and the store's loops are handed to look at.
pub fn flag(&self) -> std::sync::Arc<std::sync::atomic::AtomicBool> {
self.flag.clone()
}
/// Whether a stop was asked for.
pub fn requested(&self) -> bool {
self.flag.load(std::sync::atomic::Ordering::SeqCst)
}
/// A phase begins, for a caller that decides for itself what to do about
/// a stop: whether one was asked for (the phase watch is told first).
pub fn look(&self, phase: &str) -> bool {
if let Some(watch) = &self.phase {
watch(phase);
}
self.requested()
}
/// A phase begins: Ok, or `Stopped` (naming what was not done).
fn at(&self, phase: &str, undone: &str) -> Result<(), RootError> {
if let Some(watch) = &self.phase {
watch(phase);
}
if self.requested() {
Err(RootError::Stopped(format!(
"stopped because the books are closing: nothing was {undone}"
)))
} else {
Ok(())
}
}
/// A stop that reached the store's own loops or git, said as a job's.
fn said(&self, e: RootError, undone: &str) -> RootError {
match e {
RootError::Store(_, StoreError::Cancelled) => RootError::Stopped(format!(
"stopped because the books are closing: nothing was {undone}"
)),
other => other,
}
}
}
/// A book's id within the root: its folder name under `books/`.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BookId(String);
@@ -748,17 +833,35 @@ impl Root {
/// Start new, empty books called `id` in `base_currency`. Refused if
/// books of that id exist. Returns the open store.
pub fn create_book(&self, id: &BookId, base_currency: Currency) -> Result<Store, RootError> {
self.create_book_inner(id, base_currency)
self.create_book_stoppable(id, base_currency, &JobStop::never())
}
/// [`Self::create_book`], stopped if `stop` asks before its folder is
/// made (its point of no return); after that it makes one empty book,
/// the same few files and syncs whatever else is in the root.
pub fn create_book_stoppable(
&self,
id: &BookId,
base_currency: Currency,
stop: &JobStop,
) -> Result<Store, RootError> {
self.create_book_inner(id, base_currency, stop)
.map_err(|e| self.hide(e))
}
fn create_book_inner(&self, id: &BookId, base_currency: Currency) -> Result<Store, RootError> {
fn create_book_inner(
&self,
id: &BookId,
base_currency: Currency,
stop: &JobStop,
) -> Result<Store, RootError> {
let _lock = self.books_lock()?;
let (books, _) = self.private_subdir(BOOKS_DIR, false)?;
let dir = books.join(id.as_str());
if std::fs::symlink_metadata(&dir).is_ok() {
return Err(RootError::Exists(id.to_string()));
}
stop.at("start", "created")?;
make_private_dir(&dir)?;
match Store::create(&dir, base_currency) {
Ok(store) => {
@@ -793,7 +896,21 @@ impl Root {
id: &BookId,
options: &AdoptOptions,
) -> Result<AdoptReport, RootError> {
self.adopt_inner(source, id, options)
self.adopt_stoppable(source, id, options, &JobStop::never())
}
/// [`Self::adopt`], stopped if `stop` asks at any phase before the
/// rename that puts the books in place (its point of no return): the
/// staging folder is removed and the root is as it was. After the
/// rename it finishes, which is a directory sync.
pub fn adopt_stoppable(
&self,
source: &Path,
id: &BookId,
options: &AdoptOptions,
stop: &JobStop,
) -> Result<AdoptReport, RootError> {
self.adopt_inner(source, id, options, stop)
.map_err(|e| self.hide(e))
}
@@ -802,6 +919,7 @@ impl Root {
source: &Path,
id: &BookId,
options: &AdoptOptions,
stop: &JobStop,
) -> Result<AdoptReport, RootError> {
let _lock = self.books_lock()?;
let (books, _) = self.private_subdir(BOOKS_DIR, false)?;
@@ -809,6 +927,7 @@ impl Root {
if std::fs::symlink_metadata(&dest).is_ok() {
return Err(RootError::Exists(id.to_string()));
}
stop.at("start", "adopted")?;
let source = self.adoptable(source)?;
for (what, path) in [
("vault folder", &options.vault_dir),
@@ -825,8 +944,12 @@ impl Root {
// Step 1: the source's writer lock, held to the end, so nothing
// writes it while it is copied.
let (_source_lock, source_lock_created) = lock_source(&source)?;
// Step 2: verify the chain and record the head.
let (raw, lines) = read_source_journal(&source)?;
// Step 2: verify the chain and record the head. (The chain's check
// looks at the stop once per journal line.)
let scope = crate::cancel::scope(stop.flag());
let (raw, lines) = read_source_journal(&source).map_err(|e| stop.said(e, "adopted"))?;
drop(scope);
stop.at("journal", "adopted")?;
let head = head_of(&lines);
if let Some(expected) = &options.expect_head
&& *expected != head
@@ -859,11 +982,19 @@ impl Root {
}
let staging = books.join(format!("{STAGING_PREFIX}{id}-{}", std::process::id()));
make_private_dir(&staging)?;
// The store's own loops (opening, verifying) look at the stop too.
let scope = crate::cancel::scope(stop.flag());
let built = (|| -> Result<AdoptReport, RootError> {
stop.at("copy", "adopted")?;
// Step 3: the journal as read, then every stored file.
write_private_file(&staging.join(JOURNAL_FILE), &raw)?;
let blob_dir = crate::store::BLOBS_DIR;
let copied = copy_blobs(&source.join(blob_dir), &staging.join(blob_dir))?;
let copied = copy_blobs(
&source.join(blob_dir),
&staging.join(blob_dir),
stop,
"adopted",
)?;
// Step 5: the vault's records and the v1 key.
let vault_files = match &options.vault_dir {
Some(dir) => copy_flat_private(dir, &staging.join(VAULT_DIR))?,
@@ -879,6 +1010,7 @@ impl Root {
}
None => false,
};
stop.at("open", "adopted")?;
let mut store = Store::open(&staging)?;
if store.head() != head {
return Err(RootError::Adopt(format!(
@@ -899,6 +1031,7 @@ impl Root {
.join(", ")
)));
}
stop.at("verify", "adopted")?;
store.verify_and_heal()?;
let blobs_verified = store.verify_blobs()?;
// The copy is new books of this version, in its journal format
@@ -936,11 +1069,16 @@ impl Root {
format_upgraded,
})
})();
let renamed = built.and_then(|report| {
std::fs::rename(&staging, &dest)?;
sync_dir(&books)?;
Ok(report)
});
drop(scope);
let renamed = built
.map_err(|e| stop.said(e, "adopted"))
.and_then(|report| {
// The last look: from the rename on, the job finishes.
stop.at("place", "adopted")?;
std::fs::rename(&staging, &dest)?;
sync_dir(&books)?;
Ok(report)
});
if renamed.is_err() {
let _ = std::fs::remove_dir_all(&staging);
}
@@ -991,7 +1129,11 @@ impl Root {
source: &str,
books: &Path,
id: &BookId,
stop: &JobStop,
) -> Result<(PathBuf, crate::backup::Verified), RootError> {
stop.at("fetch", "restored")?;
// git, and the verification's own phases and loops, look at the stop.
let git = git.clone().with_cancel(stop.flag());
// Anything left from one that never finished goes first: none is
// in progress (each holds the books lock).
for entry in std::fs::read_dir(books)? {
@@ -1005,13 +1147,18 @@ impl Root {
}
}
let scratch = books.join(format!("{RESTORE_PREFIX}{id}-{}", std::process::id()));
match crate::backup::verify(git, source, &scratch) {
match crate::backup::verify(&git, source, &scratch) {
Ok(v) => Ok((scratch, v)),
Err(e) => {
// What the verification wrote goes with it (a stop leaves the
// root as it was).
let _ = std::fs::remove_dir_all(&scratch);
Err(RootError::Adopt(format!(
"that backup can't be restored: {e}"
)))
Err(match e {
crate::backup::BackupError::Cancelled(_) => RootError::Stopped(
"stopped because the books are closing: nothing was restored".into(),
),
e => RootError::Adopt(format!("that backup can't be restored: {e}")),
})
}
}
}
@@ -1024,10 +1171,23 @@ impl Root {
source: &str,
id: &BookId,
) -> Result<RestorePreview, RootError> {
self.restore_preview_stoppable(git, source, id, &JobStop::never())
}
/// [`Self::restore_preview`], stopped if `stop` asks: the clone is
/// removed and nothing is left.
pub fn restore_preview_stoppable(
&self,
git: &crate::backup::Git,
source: &str,
id: &BookId,
stop: &JobStop,
) -> Result<RestorePreview, RootError> {
let _scope = crate::cancel::scope(stop.flag());
(|| {
let _lock = self.books_lock()?;
let (books, _) = self.private_subdir(BOOKS_DIR, false)?;
let (scratch, v) = self.fetch_backup(git, source, &books, id)?;
let (scratch, v) = self.fetch_backup(git, source, &books, id, stop)?;
let vault = scratch.join(VAULT_DIR);
let vault_files = if vault.is_dir() {
std::fs::read_dir(&vault)
@@ -1063,7 +1223,24 @@ impl Root {
expect_head: &(u64, String),
replace: bool,
) -> Result<RestoreReport, RootError> {
self.restore_inner(git, source, id, expect_head, replace)
self.restore_stoppable(git, source, id, expect_head, replace, &JobStop::never())
}
/// [`Self::restore`], stopped if `stop` asks at any phase before the
/// renames that put the books in place (its point of no return): the
/// clone and the staging folder are removed and the root is as it was,
/// books that were to be replaced untouched. From the first rename on it
/// finishes: two renames and two directory syncs.
pub fn restore_stoppable(
&self,
git: &crate::backup::Git,
source: &str,
id: &BookId,
expect_head: &(u64, String),
replace: bool,
stop: &JobStop,
) -> Result<RestoreReport, RootError> {
self.restore_inner(git, source, id, expect_head, replace, stop)
.map_err(|e| match e {
// The copy's checks are the adoption's: said as a restore.
RootError::Adopt(text) => {
@@ -1081,6 +1258,7 @@ impl Root {
id: &BookId,
expect_head: &(u64, String),
replace: bool,
stop: &JobStop,
) -> Result<RestoreReport, RootError> {
let _lock = self.books_lock()?;
let (books, _) = self.private_subdir(BOOKS_DIR, false)?;
@@ -1106,9 +1284,12 @@ impl Root {
} else {
None
};
let (scratch, v) = self.fetch_backup(git, source, &books, id)?;
stop.at("start", "restored")?;
let _scope = crate::cancel::scope(stop.flag());
let (scratch, v) = self.fetch_backup(git, source, &books, id, stop)?;
let staging = books.join(format!("{RESTORE_PREFIX}{id}-{}-book", std::process::id()));
let built = (|| -> Result<RestoreReport, RootError> {
stop.at("copy", "restored")?;
if v.head != *expect_head {
return Err(RootError::Adopt(format!(
"that backup isn't the one previewed (it is now at event {}): nothing was restored; preview it again",
@@ -1124,7 +1305,12 @@ impl Root {
make_private_dir(&staging)?;
write_private_file(&staging.join(JOURNAL_FILE), &raw)?;
let blob_dir = crate::store::BLOBS_DIR;
let copied = copy_blobs(&scratch.join(blob_dir), &staging.join(blob_dir))?;
let copied = copy_blobs(
&scratch.join(blob_dir),
&staging.join(blob_dir),
stop,
"restored",
)?;
let vault_files = if scratch.join(VAULT_DIR).is_dir() {
copy_flat_private(&scratch.join(VAULT_DIR), &staging.join(VAULT_DIR))?
} else {
@@ -1151,6 +1337,7 @@ impl Root {
&crate::ops::ops_dir(&staging).join(RESTORED_FROM),
v.book.as_deref().unwrap_or("").as_bytes(),
)?;
stop.at("open", "restored")?;
let mut store = Store::open(&staging)?;
if store.head() != v.head {
return Err(RootError::Adopt(format!(
@@ -1165,6 +1352,7 @@ impl Root {
"the backup lacks stored files its books name: nothing was restored".into(),
));
}
stop.at("verify", "restored")?;
store.verify_and_heal()?;
let blobs_verified = store.verify_blobs()?;
let head = store.head();
@@ -1180,38 +1368,42 @@ impl Root {
})
})();
let _ = std::fs::remove_dir_all(&scratch);
let placed = built.and_then(|mut report| {
if exists {
let (legacy, _) = self.private_subdir(LEGACY_DIR, false)?;
let stamp = chrono::Utc::now().format("%Y%m%d-%H%M%S");
let mut name = format!("{id}-{stamp}");
let mut n = 1;
while std::fs::symlink_metadata(legacy.join(&name)).is_ok() {
n += 1;
name = format!("{id}-{stamp}-{n}");
}
std::fs::rename(&dest, legacy.join(&name))?;
if let Err(e) = std::fs::rename(&staging, &dest) {
// The books it would replace go back where they were.
if std::fs::rename(legacy.join(&name), &dest).is_err() {
return Err(RootError::Adopt(format!(
"the backup couldn't be put in place ({}), and the books it would \
replace are in the legacy folder as {name}: nothing was restored",
e.kind()
)));
let placed = built
.map_err(|e| stop.said(e, "restored"))
.and_then(|mut report| {
// The last look: from the first rename on, the job finishes.
stop.at("place", "restored")?;
if exists {
let (legacy, _) = self.private_subdir(LEGACY_DIR, false)?;
let stamp = chrono::Utc::now().format("%Y%m%d-%H%M%S");
let mut name = format!("{id}-{stamp}");
let mut n = 1;
while std::fs::symlink_metadata(legacy.join(&name)).is_ok() {
n += 1;
name = format!("{id}-{stamp}-{n}");
}
return Err(e.into());
std::fs::rename(&dest, legacy.join(&name))?;
if let Err(e) = std::fs::rename(&staging, &dest) {
// The books it would replace go back where they were.
if std::fs::rename(legacy.join(&name), &dest).is_err() {
return Err(RootError::Adopt(format!(
"the backup couldn't be put in place ({}), and the books it would \
replace are in the legacy folder as {name}: nothing was restored",
e.kind()
)));
}
return Err(e.into());
}
// In place: a folder that won't sync now is said no more
// than any later write's would be.
let _ = sync_dir(&legacy);
report.replaced = Some(name);
} else {
std::fs::rename(&staging, &dest)?;
}
// In place: a folder that won't sync now is said no more
// than any later write's would be.
let _ = sync_dir(&legacy);
report.replaced = Some(name);
} else {
std::fs::rename(&staging, &dest)?;
}
let _ = sync_dir(&books);
Ok(report)
});
let _ = sync_dir(&books);
Ok(report)
});
if placed.is_err() {
let _ = std::fs::remove_dir_all(&staging);
}
@@ -1242,7 +1434,19 @@ impl Root {
/// checks [`Self::adopt`] makes, writing nothing (not even a lock
/// file). The adoption itself reads it all again under the lock.
pub fn adopt_preview(&self, source: &Path) -> Result<SourcePreview, RootError> {
self.adopt_preview_stoppable(source, &JobStop::never())
}
/// [`Self::adopt_preview`], stopped if `stop` asks (the read of the
/// source's journal looks once per line).
pub fn adopt_preview_stoppable(
&self,
source: &Path,
stop: &JobStop,
) -> Result<SourcePreview, RootError> {
let _scope = crate::cancel::scope(stop.flag());
(|| {
stop.at("preview", "read")?;
let source = self.adoptable(source)?;
// Held for writing now: adoption would be refused, so is this.
if let Ok(lock) = std::fs::File::open(source.join(crate::store::LOCK_FILE))
@@ -1254,7 +1458,7 @@ impl Root {
.into(),
)));
}
let (_, lines) = read_source_journal(&source)?;
let (_, lines) = read_source_journal(&source).map_err(|e| stop.said(e, "read"))?;
Ok(SourcePreview {
head: head_of(&lines),
currency: crate::store::journal_currency(&lines)?,
@@ -2183,7 +2387,12 @@ struct CopiedBlobs {
/// its name; an unfinished write (`<sha>.tmp`, a put that never finished)
/// is left behind and counted; anything else (a link, a stray file) is
/// refused. Folders 0700, files 0600, every folder synced.
fn copy_blobs(from: &Path, to: &Path) -> Result<CopiedBlobs, RootError> {
fn copy_blobs(
from: &Path,
to: &Path,
stop: &JobStop,
undone: &str,
) -> Result<CopiedBlobs, RootError> {
use sha2::{Digest, Sha256};
let mut copied = CopiedBlobs {
shas: BTreeSet::new(),
@@ -2251,6 +2460,8 @@ fn copy_blobs(from: &Path, to: &Path) -> Result<CopiedBlobs, RootError> {
}
return Err(stray(&f.path()));
}
// Once per stored file (one read, one hash, one write).
stop.at("file", undone)?;
let bytes = std::fs::read(f.path())?;
if hex::encode(Sha256::digest(&bytes)) != name.to_lowercase() {
return Err(RootError::Adopt(format!(
+8 -2
View File
@@ -6390,8 +6390,14 @@ impl Store {
"wanderbooks: {why}; kept the old database as {} beside it and rebuilt from the journal",
keep.file_name().unwrap_or_default().to_string_lossy()
);
self.rebuild()?;
self.verify()?;
// A stop is looked at before the rebuild begins, never in it
// (cancel INVARIANT 4): half-rebuilt, the live ledger would
// be part replayed.
crate::cancel::check()?;
crate::cancel::shielded(|| -> Result<(), StoreError> {
self.rebuild()?;
self.verify()
})?;
Some(why)
}
Err(e) => return Err(e),