diff --git a/crates/notebook/src/provider/tests.rs b/crates/notebook/src/provider/tests.rs index 9324a3e840e92c65aa4fdb6e1a1340ee6332b4c5..3ad693d9a97d3f888a006265ecde4705f668e545 100644 --- a/crates/notebook/src/provider/tests.rs +++ b/crates/notebook/src/provider/tests.rs @@ -1019,6 +1019,44 @@ fn an_upload_the_provider_dropped_merges_back_once_reads_could_have_caught_up() section.close().unwrap(); } +/// Images an earlier version kept in one folder for the whole notebook move beside their +/// replicas when the notebook next opens, so a publication the document lost meanwhile still +/// merges back. +#[test] +fn images_kept_in_the_notebooks_folder_move_beside_their_replicas() { + let fake = Fake::new(Kind::Seekable); + notebook(&fake); + let original = fake.bytes("Notes.one"); + let documents = fake.documents("root"); + let notebook = Notebook::open_provider(Arc::clone(&documents)).unwrap(); + let section = open(¬ebook, &documents, "Notes.one"); + let id = typed(§ion, 0..0, "Alpha "); + published(§ion, id); + section.close().unwrap(); + drop(notebook); + let legacy = crate::location::folder(&documents.cache, &documents.location()).join("published"); + fs::create_dir_all(&legacy).unwrap(); + for store in stores(&fake) { + for image in fs::read_dir(&store).unwrap() { + let image = image.unwrap().path(); + fs::rename(&image, legacy.join(image.file_name().unwrap())).unwrap(); + } + fs::remove_dir(store).unwrap(); + } + // Another device's upload, made on the document as it stood before, lands over this one's. + fs::write(fake.file(&fake.id("Notes.one")), &original).unwrap(); + let notebook = Notebook::open_provider(Arc::clone(&documents)).unwrap(); + assert!(!legacy.exists()); + assert_eq!(kept_images(&fake).len(), 1); + aged(&fake); + let section = open(¬ebook, &documents, "Notes.one"); + until("the publication merged back", || { + section.wake(); + stored(fake.bytes("Notes.one")) == "Alpha Original text" + }); + section.close().unwrap(); +} + #[test] fn a_torn_upload_on_a_provider_writing_in_place_is_put_back() { for at in [2000, 500] { diff --git a/crates/notebook/src/published.rs b/crates/notebook/src/published.rs index 3475c0c0c966c7746feda60a18d862844ae57149..ba511e230f000ac9038e534e5d84ef6e1414e03c 100644 --- a/crates/notebook/src/published.rs +++ b/crates/notebook/src/published.rs @@ -236,6 +236,45 @@ pub(crate) fn keeps(replica: &Path) -> bool { crate::fs::read_dir(folder(replica)).is_ok_and(|mut images| images.next().is_some()) } +/// Moves the images a provider notebook kept in one folder for all its sections (`legacy`, +/// as before they were kept beside each replica) to the replica `replica` names for each +/// image's file identity, once: an image may be a publication the document lost, not yet +/// merged. One whose file the notebook no longer lists stays where it was. +pub(crate) fn adopt( + legacy: &Path, + replica: impl Fn(&[u8; 16]) -> Option, +) -> io::Result<()> { + let Ok(entries) = crate::fs::read_dir(legacy) else { + return Ok(()); + }; + for entry in entries { + let image = entry?.path(); + let file = (image.file_name().and_then(|name| name.to_str())) + .and_then(|name| name.strip_suffix(".one")?.split('-').next()) + .and_then(identity); + let Some(replica) = file.and_then(|file| replica(&file)) else { + continue; + }; + let store = folder(&replica); + crate::fs::create_dir_all(&store)?; + let name = image.file_name().unwrap_or_default(); + if crate::fs::metadata(store.join(name)).is_err() { + crate::fs::rename(&image, store.join(name))?; + } + } + let _ = crate::fs::remove_dir(legacy); + Ok(()) +} + +/// The file identity a kept image's name begins with. +fn identity(hex: &str) -> Option<[u8; 16]> { + let mut identity = [0; 16]; + for (at, byte) in identity.iter_mut().enumerate() { + *byte = u8::from_str_radix(hex.get(at * 2..at * 2 + 2)?, 16).ok()?; + } + (hex.len() == 32).then_some(identity) +} + /// Where a version of `file` that cannot merge is kept beside it, as iCloud Drive keeps a /// conflict version: "Name (Device).one", numbered past any name taken. pub fn beside(file: &Path, device: &str) -> PathBuf { diff --git a/crates/notebook/src/session.rs b/crates/notebook/src/session.rs index 1dba2ee954416c3800e455dbd2acd8996bc04c2d..df0304a9be9e9f7754f13802e71baa44c4cb88de 100644 --- a/crates/notebook/src/session.rs +++ b/crates/notebook/src/session.rs @@ -591,12 +591,22 @@ impl Notebook { /// reached, the notebook opens as its folders were last listed. pub fn open_provider(documents: Arc) -> Result { let cache = documents.cache.clone(); - let listed = listing(&cache, &documents.location()).with_extension("entries.json"); - Self::with( + let location = documents.location(); + let listed = listing(&cache, &location).with_extension("entries.json"); + let notebook = Self::with( Box::new(crate::provider::Tree { documents, listed }), None, cache, - ) + )?; + let legacy = crate::location::folder(¬ebook.cache, &location).join("published"); + crate::published::adopt(&legacy, |file| { + let section = notebook + .catalog + .sections() + .find(|section| section.file_id == *file)?; + notebook.replica_path(§ion.path).ok() + })?; + Ok(notebook) } /// The storage the notebook's files are in, for a Live Share host to serve. @@ -2761,7 +2771,7 @@ impl Section { offline, move || woken(), ); - match opened { + let section = match opened { Err(Error::Io(error)) if error.kind() == io::ErrorKind::NotConnected => { let mut replicas = fs::read_dir(crate::location::folder(cache, &location)) .into_iter() @@ -2781,7 +2791,10 @@ impl Section { } } opened => opened, - } + }?; + let legacy = crate::location::folder(cache, &location).join("published"); + crate::published::adopt(&legacy, |_| section.replica.path())?; + Ok(section) } fn start(