Skip to content

Native iCloud apps for Windows and Linux, built in Rust. Notes, Reminders, Calendar, Contacts, Drive and Photos, offline and in sync with your iPhone.

Folders: names that stay put and never hide each other

Yashwanth Kamireddi7c1e4f436fab
diff --git a/crates/quince-vfs/src/lib.rs b/crates/quince-vfs/src/lib.rs
index 468af7a..d84346b 100644
--- a/crates/quince-vfs/src/lib.rs
+++ b/crates/quince-vfs/src/lib.rs
@@ -233,9 +233,7 @@ impl Inodes {
/// path cannot hold) are told apart, never one hiding the other.
#[must_use]
pub fn named(items: &[DriveItem]) -> Vec<(String, DriveItem)> {
- let mut taken: HashMap<String, usize> = HashMap::new();
- let mut out = Vec::with_capacity(items.len());
- for item in items {
+ let base_of = |item: &DriveItem| {
// "/" cannot be in a name on Linux, and NUL nowhere: Apple allows the first.
let base: String = item
.display_name()
@@ -246,22 +244,38 @@ pub fn named(items: &[DriveItem]) -> Vec<(String, DriveItem)> {
c => c,
})
.collect();
- let base = if base == "." || base == ".." || base.is_empty() {
+ if base == "." || base == ".." || base.is_empty() {
format!("{base}_")
} else {
base
- };
+ }
+ };
+ // Every name the folder holds as it is, so a number given to tell two apart never lands on
+ // a third that is really called that ("Report (2).pdf" beside two "Report.pdf"s).
+ let real: std::collections::HashSet<String> = items.iter().map(base_of).collect();
+ let mut given: std::collections::HashSet<String> = std::collections::HashSet::new();
+ let mut taken: HashMap<String, usize> = HashMap::new();
+ let mut out = Vec::with_capacity(items.len());
+ for item in items {
+ let base = base_of(item);
let n = taken.entry(base.clone()).or_insert(0);
*n += 1;
- let name = if *n == 1 {
+ let name = if *n == 1 && given.insert(base.clone()) {
base
} else {
// "Report (2).pdf": the second of two with the same name.
- match base.rsplit_once('.') {
- Some((stem, ext)) if !item.is_container() && !stem.is_empty() => {
- format!("{stem} ({n}).{ext}")
+ loop {
+ *n = (*n).max(2);
+ let candidate = match base.rsplit_once('.') {
+ Some((stem, ext)) if !item.is_container() && !stem.is_empty() => {
+ format!("{stem} ({n}).{ext}")
+ }
+ _ => format!("{base} ({n})"),
+ };
+ if !real.contains(&candidate) && given.insert(candidate.clone()) {
+ break candidate;
}
- _ => format!("{base} ({n})"),
+ *n += 1;
}
};
out.push((name, item.clone()));
@@ -791,6 +805,18 @@ mod tests {
assert_eq!(names, ["Report.pdf", "Report (2).pdf", "a∕b", ".._"]);
}
+ /// A number given to tell two apart never takes the name of a third that really has it.
+ #[test]
+ fn a_numbered_name_never_hides_a_real_one() {
+ let items = [
+ item("a", "Report", Some("pdf"), "FILE", 1, "e"),
+ item("b", "Report", Some("pdf"), "FILE", 1, "e"),
+ item("c", "Report (2)", Some("pdf"), "FILE", 1, "e"),
+ ];
+ let names: Vec<String> = named(&items).into_iter().map(|(n, _)| n).collect();
+ assert_eq!(names, ["Report.pdf", "Report (3).pdf", "Report (2).pdf"]);
+ }
+
#[test]
fn inodes_are_stable() {
let mut inodes = Inodes::new();
diff --git a/src-tauri/src/mount_photos.rs b/src-tauri/src/mount_photos.rs
index 32ead08..e55c0f0 100644
--- a/src-tauri/src/mount_photos.rs
+++ b/src-tauri/src/mount_photos.rs
@@ -135,7 +135,11 @@ fn children(app: &AppHandle, id: &str) -> Option<Vec<DriveItem>> {
}
fn children_in(library: &quince_core::Library, id: &str) -> Option<Vec<DriveItem>> {
- let photos = |view: View| library.photos(view).unwrap_or_default();
+ // Oldest first, so that of two photos with the same name (an iPhone's IMG_ numbers come
+ // round again every ten thousand) the older keeps the plain name and the newer is "(2)".
+ // Newest first, each new favourite or video with a name already there renamed the older
+ // one, and backups and open paths saw a file move.
+ let photos = |view: View| oldest_first(library.photos(view).unwrap_or_default());
Some(match id {
ROOT => vec![
folder(LIBRARY.into(), ROOT, "Library".into()),
@@ -198,9 +202,7 @@ fn children_in(library: &quince_core::Library, id: &str) -> Option<Vec<DriveItem
if is_folder {
albums_in(library, Some(album), id)
} else {
- library
- .album_photos(album)
- .ok()?
+ oldest_first(library.album_photos(album).ok()?)
.iter()
.filter(|p| !p.hidden && !p.deleted)
.flat_map(|p| files(p, id))
@@ -211,6 +213,12 @@ fn children_in(library: &quince_core::Library, id: &str) -> Option<Vec<DriveItem
})
}
+/// Photos in the order their names are given out: taken first, named first.
+fn oldest_first(mut photos: Vec<Photo>) -> Vec<Photo> {
+ photos.sort_by(|a, b| a.taken_at.cmp(&b.taken_at).then_with(|| a.id.cmp(&b.id)));
+ photos
+}
+
/// The albums (and album folders) directly inside `parent`, in Photos' own order.
fn albums_in(library: &quince_core::Library, parent: Option<&str>, at: &str) -> Vec<DriveItem> {
let mut albums: Vec<_> = library
@@ -400,6 +408,40 @@ impl Remote for PhotosRemote {
mod tests {
use super::*;
+ /// Two photos with the same name keep their names as new ones arrive: the older the plain
+ /// one, the newer "(2)" — a newer favourite of the same name used to take the plain name
+ /// and rename the older file under its owner.
+ #[test]
+ fn same_named_photos_keep_their_names_as_new_ones_arrive() {
+ let shot = |id: &str, at: i64| -> Photo {
+ serde_json::from_value(serde_json::json!({
+ "id": id, "master": format!("M{id}"), "takenAt": at, "kind": "image",
+ "width": 4032, "height": 3024, "filename": "IMG_0001.JPG", "size": 1000,
+ "favorite": true, "hidden": false, "deleted": false, "subtype": 0
+ }))
+ .unwrap()
+ };
+ let mut lib = quince_core::Library::in_memory().unwrap();
+ lib.put_photos(&[shot("old", 1_600_000_000)]).unwrap();
+ let names = |lib: &quince_core::Library| -> Vec<(String, String)> {
+ quince_vfs::named(&children_in(lib, FAVORITES).unwrap())
+ .into_iter()
+ .map(|(n, i)| (n, i.drivewsid))
+ .collect()
+ };
+ let before = names(&lib);
+ lib.put_photos(&[shot("new", 1_700_000_000)]).unwrap();
+ let after = names(&lib);
+ let of = |all: &[(String, String)], id: &str| {
+ all.iter()
+ .find(|(_, d)| d.ends_with(id))
+ .map(|(n, _)| n.clone())
+ };
+ assert_eq!(of(&before, "old"), Some("IMG_0001.JPG".into()));
+ assert_eq!(of(&after, "old"), Some("IMG_0001.JPG".into()), "{after:?}");
+ assert_eq!(of(&after, "new"), Some("IMG_0001 (2).JPG".into()));
+ }
+
/// A Live Photo is two files under one name — the still and its .MOV, at the moving part's
/// own size — and each knows which picture it is.
#[test]