authoravatarclover caruso<git@paperclover.net>2026-10-19 22:43:29-07:00
committeravatarclover caruso<git@paperclover.net>2026-10-19 23:39:58-07:00
log36498b522852632e131ef7260a39d49cb9f34668
treefe98b17423c831bf943b974aa2f680d5c592a380
parent00efb7f2a201741ccea64df3120cf80bb4d0f6cd
signature Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU

fix(notebook): move the images Android kept for a notebook beside their replicas

Before the images a section's publications keep moved beside its replica, an Android provider notebook kept them in one folder for the whole notebook. One of them can be a publication the document lost and the phone has not yet merged back, so leaving the folder behind would lose it. Opening a provider notebook, or a lone provider document, now moves each such image beside the replica of the section whose file identity it names, once, and removes the folder; an image whose section is gone stays where it was. A test kept its image the old way, lost the upload, and reopens; it fails without the move. Assisted-by: claude-opus-5.5

3 files changed, 95 insertions(+), 5 deletions(-)

crates/notebook/src/provider/tests.rs+38
...@@ -1019,6 +1019,44 @@ fn an_upload_the_provider_dropped_merges_back_once_reads_could_have_caught_up()...@@ -1019,6 +1019,44 @@ fn an_upload_the_provider_dropped_merges_back_once_reads_could_have_caught_up()
1019 section.close().unwrap();1019 section.close().unwrap();
1020}1020}
10211021
1022/// Images an earlier version kept in one folder for the whole notebook move beside their
1023/// replicas when the notebook next opens, so a publication the document lost meanwhile still
1024/// merges back.
1025#[test]
1026fn images_kept_in_the_notebooks_folder_move_beside_their_replicas() {
1027 let fake = Fake::new(Kind::Seekable);
1028 notebook(&fake);
1029 let original = fake.bytes("Notes.one");
1030 let documents = fake.documents("root");
1031 let notebook = Notebook::open_provider(Arc::clone(&documents)).unwrap();
1032 let section = open(&notebook, &documents, "Notes.one");
1033 let id = typed(&section, 0..0, "Alpha ");
1034 published(&section, id);
1035 section.close().unwrap();
1036 drop(notebook);
1037 let legacy = crate::location::folder(&documents.cache, &documents.location()).join("published");
1038 fs::create_dir_all(&legacy).unwrap();
1039 for store in stores(&fake) {
1040 for image in fs::read_dir(&store).unwrap() {
1041 let image = image.unwrap().path();
1042 fs::rename(&image, legacy.join(image.file_name().unwrap())).unwrap();
1043 }
1044 fs::remove_dir(store).unwrap();
1045 }
1046 // Another device's upload, made on the document as it stood before, lands over this one's.
1047 fs::write(fake.file(&fake.id("Notes.one")), &original).unwrap();
1048 let notebook = Notebook::open_provider(Arc::clone(&documents)).unwrap();
1049 assert!(!legacy.exists());
1050 assert_eq!(kept_images(&fake).len(), 1);
1051 aged(&fake);
1052 let section = open(&notebook, &documents, "Notes.one");
1053 until("the publication merged back", || {
1054 section.wake();
1055 stored(fake.bytes("Notes.one")) == "Alpha Original text"
1056 });
1057 section.close().unwrap();
1058}
1059
1022#[test]1060#[test]
1023fn a_torn_upload_on_a_provider_writing_in_place_is_put_back() {1061fn a_torn_upload_on_a_provider_writing_in_place_is_put_back() {
1024 for at in [2000, 500] {1062 for at in [2000, 500] {
crates/notebook/src/published.rs+39
...@@ -236,6 +236,45 @@ pub(crate) fn keeps(replica: &Path) -> bool {...@@ -236,6 +236,45 @@ pub(crate) fn keeps(replica: &Path) -> bool {
236 crate::fs::read_dir(folder(replica)).is_ok_and(|mut images| images.next().is_some())236 crate::fs::read_dir(folder(replica)).is_ok_and(|mut images| images.next().is_some())
237}237}
238238
239/// Moves the images a provider notebook kept in one folder for all its sections (`legacy`,
240/// as before they were kept beside each replica) to the replica `replica` names for each
241/// image's file identity, once: an image may be a publication the document lost, not yet
242/// merged. One whose file the notebook no longer lists stays where it was.
243pub(crate) fn adopt(
244 legacy: &Path,
245 replica: impl Fn(&[u8; 16]) -> Option<PathBuf>,
246) -> io::Result<()> {
247 let Ok(entries) = crate::fs::read_dir(legacy) else {
248 return Ok(());
249 };
250 for entry in entries {
251 let image = entry?.path();
252 let file = (image.file_name().and_then(|name| name.to_str()))
253 .and_then(|name| name.strip_suffix(".one")?.split('-').next())
254 .and_then(identity);
255 let Some(replica) = file.and_then(|file| replica(&file)) else {
256 continue;
257 };
258 let store = folder(&replica);
259 crate::fs::create_dir_all(&store)?;
260 let name = image.file_name().unwrap_or_default();
261 if crate::fs::metadata(store.join(name)).is_err() {
262 crate::fs::rename(&image, store.join(name))?;
263 }
264 }
265 let _ = crate::fs::remove_dir(legacy);
266 Ok(())
267}
268
269/// The file identity a kept image's name begins with.
270fn identity(hex: &str) -> Option<[u8; 16]> {
271 let mut identity = [0; 16];
272 for (at, byte) in identity.iter_mut().enumerate() {
273 *byte = u8::from_str_radix(hex.get(at * 2..at * 2 + 2)?, 16).ok()?;
274 }
275 (hex.len() == 32).then_some(identity)
276}
277
239/// Where a version of `file` that cannot merge is kept beside it, as iCloud Drive keeps a278/// Where a version of `file` that cannot merge is kept beside it, as iCloud Drive keeps a
240/// conflict version: "Name (Device).one", numbered past any name taken.279/// conflict version: "Name (Device).one", numbered past any name taken.
241pub fn beside(file: &Path, device: &str) -> PathBuf {280pub fn beside(file: &Path, device: &str) -> PathBuf {
crates/notebook/src/session.rs+18-5
...@@ -591,12 +591,22 @@ impl Notebook {...@@ -591,12 +591,22 @@ impl Notebook {
591 /// reached, the notebook opens as its folders were last listed.591 /// reached, the notebook opens as its folders were last listed.
592 pub fn open_provider(documents: Arc<crate::provider::Documents>) -> Result<Self> {592 pub fn open_provider(documents: Arc<crate::provider::Documents>) -> Result<Self> {
593 let cache = documents.cache.clone();593 let cache = documents.cache.clone();
594 let listed = listing(&cache, &documents.location()).with_extension("entries.json");594 let location = documents.location();
595 Self::with(595 let listed = listing(&cache, &location).with_extension("entries.json");
596 let notebook = Self::with(
596 Box::new(crate::provider::Tree { documents, listed }),597 Box::new(crate::provider::Tree { documents, listed }),
597 None,598 None,
598 cache,599 cache,
599 )600 )?;
601 let legacy = crate::location::folder(&notebook.cache, &location).join("published");
602 crate::published::adopt(&legacy, |file| {
603 let section = notebook
604 .catalog
605 .sections()
606 .find(|section| section.file_id == *file)?;
607 notebook.replica_path(&section.path).ok()
608 })?;
609 Ok(notebook)
600 }610 }
601611
602 /// The storage the notebook's files are in, for a Live Share host to serve.612 /// The storage the notebook's files are in, for a Live Share host to serve.
...@@ -2761,7 +2771,7 @@ impl Section {...@@ -2761,7 +2771,7 @@ impl Section {
2761 offline,2771 offline,
2762 move || woken(),2772 move || woken(),
2763 );2773 );
2764 match opened {2774 let section = match opened {
2765 Err(Error::Io(error)) if error.kind() == io::ErrorKind::NotConnected => {2775 Err(Error::Io(error)) if error.kind() == io::ErrorKind::NotConnected => {
2766 let mut replicas = fs::read_dir(crate::location::folder(cache, &location))2776 let mut replicas = fs::read_dir(crate::location::folder(cache, &location))
2767 .into_iter()2777 .into_iter()
...@@ -2781,7 +2791,10 @@ impl Section {...@@ -2781,7 +2791,10 @@ impl Section {
2781 }2791 }
2782 }2792 }
2783 opened => opened,2793 opened => opened,
2784 }2794 }?;
2795 let legacy = crate::location::folder(cache, &location).join("published");
2796 crate::published::adopt(&legacy, |_| section.replica.path())?;
2797 Ok(section)
2785 }2798 }
27862799
2787 fn start<R: Remote + 'static>(2800 fn start<R: Remote + 'static>(