π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Support hard links in Photos exports
Manav Rathied4240d2a09f
diff --git a/rust/Cargo.lock b/rust/Cargo.lockindex 85212e12fd..f78b001d63 100644--- a/rust/Cargo.lock+++ b/rust/Cargo.lock@@ -2551,6 +2551,7 @@ dependencies = ["mockito","reqwest 0.12.28","rusqlite",+ "same-file","serde","serde_json","tempfile",@@ -2895,6 +2896,7 @@ dependencies = ["ente-photos","icu_casemap 2.2.0","rusqlite",+ "same-file","serde","serde_json","tempfile",diff --git a/rust/Cargo.toml b/rust/Cargo.tomlindex a2d3802a65..b7e7c781dc 100644--- a/rust/Cargo.toml+++ b/rust/Cargo.toml@@ -102,6 +102,7 @@ quick-xml = "0.42.0"regex = "1.13.1"reqwest = { version = "0.12.28", default-features = false }rusqlite = { version = "0.40.2", default-features = false, features = ["bundled"] }+same-file = "1.0.6"serde = { version = "1.0.229", features = ["derive"] }serde-wasm-bindgen = "0.6.5"serde_json = "1.0.151"diff --git a/rust/apps/cli-next/Cargo.toml b/rust/apps/cli-next/Cargo.tomlindex 4867a4920a..5ff362fea9 100644--- a/rust/apps/cli-next/Cargo.toml+++ b/rust/apps/cli-next/Cargo.toml@@ -38,6 +38,7 @@ rusqlite = { workspace = true, features = ["bundled-sqlcipher-vendored-openssl"]ente-test-support.workspace = truefutures-util.workspace = truemockito.workspace = true+same-file.workspace = truezip.workspace = true[lints]diff --git a/rust/apps/cli-next/src/args.rs b/rust/apps/cli-next/src/args.rsindex ad04c57fb1..2408d3079f 100644--- a/rust/apps/cli-next/src/args.rs+++ b/rust/apps/cli-next/src/args.rs@@ -212,6 +212,12 @@ pub struct ExportArgs {help = "Take over an old CLI/Desktop export or reconnect an existing one")]pub adopt: bool,+ #[arg(+ long,+ help = "Share originals across albums using hard links",+ long_help = "Share originals across albums using hard links, including verified copies already in selected albums. Edits and file attributes are shared. Omitting this flag later does not undo existing links. The destination must support hard links."+ )]+ pub hardlinks: bool,#[arg(long,short = 'j',diff --git a/rust/apps/cli-next/src/export/mod.rs b/rust/apps/cli-next/src/export/mod.rsindex 974ecf3724..f76b4278b1 100644--- a/rust/apps/cli-next/src/export/mod.rs+++ b/rust/apps/cli-next/src/export/mod.rs@@ -42,6 +42,7 @@ async fn run_locked(args: ExportArgs, selected: Option<&str>, as_json: bool) ->albums: args.album,exclude_albums: args.exclude_album,adopt: args.adopt,+ hardlinks: args.hardlinks,jobs: args.jobs,},)diff --git a/rust/apps/cli-next/tests/cli.rs b/rust/apps/cli-next/tests/cli.rsindex 9f338aed3a..e9a487def9 100644--- a/rust/apps/cli-next/tests/cli.rs+++ b/rust/apps/cli-next/tests/cli.rs@@ -35,6 +35,8 @@ fn reads_do_not_initialize_storage() {assert!(help.contains("Usage: ente-cli-next [OPTIONS] <COMMAND>"));assert!(help.contains(&format!("=== {binary} photos file download ===")));assert!(help.contains(&format!("=== {binary} vault key generate ===")));+ assert!(help.contains("--hardlinks"));+ assert!(help.contains("Edits and file attributes are shared"));let output = home.command(&["accounts", "list"]).env("ENTE_CLI_HOME", &path)diff --git a/rust/apps/cli-next/tests/support/export.rs b/rust/apps/cli-next/tests/support/export.rsindex 6c7e286c18..99aef6f422 100644--- a/rust/apps/cli-next/tests/support/export.rs+++ b/rust/apps/cli-next/tests/support/export.rs@@ -69,6 +69,16 @@ fn export_selection_reuses_independent_album_copies() -> TestResult {fs::metadata(root.join("Family/A.jpg"))?.ino());}+ export(+ &home,+ &root,+ &["--album", "Family", "--album", "Travel", "--hardlinks"],+ );+ assert!(same_file::is_same_file(+ root.join("Travel/A.jpg"),+ root.join("Family/A.jpg")+ )?);+ assert_eq!(objects.reads(), initial_reads + 1);let travel_time = fs::metadata(root.join("Travel/A.jpg"))?.modified()?;export(&home, &root, &["--album", "Family"]);assert_eq!(diff --git a/rust/apps/cli-next/tests/support/export_adoption.rs b/rust/apps/cli-next/tests/support/export_adoption.rsindex 4792a99834..2dba95f730 100644--- a/rust/apps/cli-next/tests/support/export_adoption.rs+++ b/rust/apps/cli-next/tests/support/export_adoption.rs@@ -192,6 +192,7 @@ fn adoption_reuses_captured_producer_shapes_and_converts_current_names() {for producer in ["go", "desktop"] {let mut server = mockito::Server::new();let (mut engine, downloads) = new_engine(&server, producer);+ engine.hardlinks = true;let requests: Vec<_> = downloads.iter().map(|(id, bytes)| download(&mut server, *id, bytes, 0))@@ -261,6 +262,13 @@ fn adoption_reuses_captured_producer_shapes_and_converts_current_names() {}assert!(!root.join("Family").exists());assert!(root.join("Renamed/Portrait.jpg").is_file());+ assert!(+ same_file::is_same_file(+ root.join("Renamed/Pair.jpg"),+ root.join("Live Pair/Pair.jpg")+ )+ .unwrap()+ );assert!(root.join("Renamed/Live-1.jpg").is_file());assert!(root.join("Renamed/Live.mov").is_file());assert_eq!(diff --git a/rust/apps/cli-next/tests/support/export_engine.rs b/rust/apps/cli-next/tests/support/export_engine.rsindex 80cd583b4a..042dd36f5b 100644--- a/rust/apps/cli-next/tests/support/export_engine.rs+++ b/rust/apps/cli-next/tests/support/export_engine.rs@@ -91,6 +91,7 @@ pub(super) struct Engine {session: Session,pub catalog: Catalog,pub database: String,+ pub hardlinks: bool,}impl Engine {@@ -107,6 +108,7 @@ impl Engine {.unwrap(),catalog,database: "export.db".into(),+ hardlinks: false,}}@@ -123,6 +125,7 @@ impl Engine {albums: albums.iter().map(|s| (*s).into()).collect(),exclude_albums: Vec::new(),adopt,+ hardlinks: self.hardlinks,jobs: std::num::NonZeroUsize::new(1).unwrap(),}}@@ -146,11 +149,18 @@ impl Engine {Ok(export)}- pub fn execute(&self, mut export: Export) -> Result<Summary> {+ pub fn execute(&self, export: Export) -> Result<Summary> {+ self.execute_with_report(export, &|line| eprintln!("{line}"))+ }++ pub fn execute_with_report(+ &self,+ mut export: Export,+ report: &(dyn Fn(&str) + Sync),+ ) -> Result<Summary> {let (send, cancel) = watch::channel(false);- tokio::runtime::Runtime::new()?.block_on(async {- export.execute(&self.session, &send, &cancel, &|line| eprintln!("{line}"))- })+ tokio::runtime::Runtime::new()?+ .block_on(async { export.execute(&self.session, &send, &cancel, report) })}pub fn run(&mut self, adopt: bool, albums: &[&str]) -> Result<Summary> {diff --git a/rust/apps/cli-next/tests/support/export_media.rs b/rust/apps/cli-next/tests/support/export_media.rsindex dd3723567a..4c5adf60f2 100644--- a/rust/apps/cli-next/tests/support/export_media.rs+++ b/rust/apps/cli-next/tests/support/export_media.rs@@ -414,3 +414,240 @@ fn engine_recovery_preserves_edits_at_either_move_endpoint() {fetched.assert();}}++fn linked(root: &Path, first: &str, second: &str) -> bool {+ same_file::is_same_file(root.join(first), root.join(second)).unwrap()+}++#[test]+fn engine_consolidates_copies_and_keeps_linked_history() {+ let mut server = mockito::Server::new();+ let key = Key::generate();+ let (record, bytes) = original_fixture(&key, b"old", false, true, 10);+ let fetched = download(&mut server, 10, &bytes, 1);+ let mut other = public(record.clone(), &key, json!({"editedName":"Other.jpg"}), 20);+ other["id"] = json!(11);+ let separate = download(&mut server, 11, &bytes, 1);+ let mut engine = new_engine(&server, key, record.clone());+ engine.catalog.files.insert((2, 11), other);+ engine.catalog.albums.insert(3, "Excluded".into());+ engine.catalog.files.insert((3, 10), record.clone());+ run(&mut engine, true);+ let root = engine.root();+ let first = root.join("First/Photo.jpg");+ let second = root.join("Second/Photo.jpg");+ assert!(!same_file::is_same_file(&first, &second).unwrap());+ engine.hardlinks = true;+ assert_eq!(run(&mut engine, true)["changes"]["exported"], 2);+ assert!(same_file::is_same_file(&first, &second).unwrap());+ assert!(!same_file::is_same_file(&first, root.join("Second/Other.jpg")).unwrap());+ assert_eq!(run(&mut engine, true)["changes"]["exported"], 0);+ assert_eq!(count(&engine.db(), "SELECT count(*) FROM temporaries"), 0);+ for album in ["First", "Second"] {+ for entry in fs::read_dir(root.join(album)).unwrap() {+ let name = entry.unwrap().file_name();+ assert!(!name.to_string_lossy().ends_with(".part"));+ }+ }+ engine.catalog.files.remove(&(1, 10));+ run(&mut engine, true);+ let retained = root.join("Trash/First/Photo.jpg");+ assert!(same_file::is_same_file(&retained, &second).unwrap());+ let history_path = root.join("Trash/First/metadata/Photo.jpg.json");+ let history = fs::read(&history_path).unwrap();+ let before = fs::metadata(&retained).unwrap().modified().unwrap();+ engine.hardlinks = false;+ let changed = public(+ record,+ &engine.catalog.key,+ json!({"editedTime":1_700_000_100_000_000i64}),+ 30,+ );+ engine.catalog.files.insert((2, 10), changed);+ assert!(engine.run(false, &["Second"]).unwrap().complete);+ assert_ne!(fs::metadata(&retained).unwrap().modified().unwrap(), before);+ assert!(same_file::is_same_file(&retained, &second).unwrap());+ let (new, bytes) = original_fixture(&engine.catalog.key, b"new", false, true, 40);+ engine.catalog.files.insert((2, 10), new.clone());+ let replacement = download(&mut server, 10, &bytes, 1);+ assert!(engine.run(false, &["Second"]).unwrap().complete);+ assert_eq!(fs::read(&retained).unwrap(), b"old");+ assert_eq!(fs::read(&history_path).unwrap(), history);+ assert_eq!(fs::read(root.join("Excluded/Photo.jpg")).unwrap(), b"old");+ engine.catalog.files.insert((1, 10), new);+ assert!(engine.run(false, &["First", "Second"]).unwrap().complete);+ assert_component(&root, "First", "Photo.jpg", b"new");+ assert!(!same_file::is_same_file(&first, &second).unwrap());+ fetched.assert();+ separate.assert();+ replacement.assert();+}++#[test]+fn engine_links_live_components_and_preserves_edits() {+ let mut server = mockito::Server::new();+ let key = Key::generate();+ let (record, bytes) = original_fixture(&key, b"original", true, true, 10);+ let fetched = download(&mut server, 10, &bytes, 1);+ let mut engine = new_engine(&server, key, record);+ engine.hardlinks = true;+ run(&mut engine, true);+ let root = engine.root();+ for name in ["Motion.heic", "Motion.mov"] {+ assert!(linked(+ &root,+ &format!("First/{name}"),+ &format!("Second/{name}")+ ));+ assert!(!linked(+ &root,+ &format!("First/metadata/{name}.json"),+ &format!("Second/metadata/{name}.json")+ ));+ assert_component(&root, "Second", name, b"original");+ }+ assert!(!linked(&root, "First/Motion.heic", "First/Motion.mov"));+ let first = root.join("First/Motion.heic");+ let second = root.join("Second/Motion.heic");+ fs::remove_file(&first).unwrap();+ assert_eq!(fs::read(&second).unwrap(), b"original");+ run(&mut engine, true);+ assert!(same_file::is_same_file(&first, &second).unwrap());+ fs::write(&first, b"edited in place").unwrap();+ let (other, bytes) = source(11, &engine.catalog.key, b"independent", "Other.jpg");+ engine.catalog.files.insert((2, 11), other);+ let independent = download(&mut server, 11, &bytes, 1);+ let result = run(&mut engine, false);+ assert_eq!(result["conflicts"], 2);+ assert_eq!(result["copies"]["completed"], 1);+ assert_component(&root, "Second", "Other.jpg", b"independent");+ assert_eq!(fs::read(&second).unwrap(), b"edited in place");+ fs::remove_file(&first).unwrap();+ fs::write(&first, b"independent local edit").unwrap();+ assert_eq!(run(&mut engine, false)["conflicts"], 2);+ assert_eq!(fs::read(&first).unwrap(), b"independent local edit");+ independent.assert();+ fetched.assert();+}++#[test]+fn engine_recovers_interrupted_link_publication() {+ for boundary in [+ "BEFORE INSERT ON pending WHEN new.album=2 AND json_type(new.action,'$.Publish.output.Media') IS NOT NULL",+ "BEFORE UPDATE ON pending WHEN old.album=2 AND json_type(old.action,'$.Publish.output.Media') IS NOT NULL AND new.action='null'",+ ] {+ let mut server = mockito::Server::new();+ let key = Key::generate();+ let (record, bytes) = original_fixture(&key, b"original", false, true, 10);+ let fetched = download(&mut server, 10, &bytes, 1);+ let mut engine = new_engine(&server, key, record);+ run(&mut engine, true);+ engine.hardlinks = true;+ let db = engine.db();+ db.execute_batch(&format!("CREATE TRIGGER stop_link {boundary} BEGIN SELECT RAISE(ABORT,'link publication interrupted'); END;")).unwrap();+ run(&mut engine, false);+ let root = engine.root();+ assert_eq!(+ fs::read(root.join("Second/Photo.jpg")).unwrap(),+ b"original"+ );+ db.execute_batch("DROP TRIGGER stop_link").unwrap();+ run(&mut engine, true);+ assert!(linked(&root, "First/Photo.jpg", "Second/Photo.jpg"));+ assert_eq!(count(&db, "SELECT count(*) FROM temporaries"), 0);+ fetched.assert();+ }+}++#[test]+#[cfg(unix)]+fn engine_link_failure_preserves_existing_copy() {+ use std::os::unix::fs::PermissionsExt;+ let mut server = mockito::Server::new();+ let key = Key::generate();+ let (record, bytes) = original_fixture(&key, b"original", false, true, 10);+ let fetched = download(&mut server, 10, &bytes, 1);+ let mut engine = new_engine(&server, key, record);+ run(&mut engine, true);+ engine.hardlinks = true;+ let directory = engine.root().join("Second");+ let permissions = fs::metadata(&directory).unwrap().permissions();+ fs::set_permissions(&directory, fs::Permissions::from_mode(0o500)).unwrap();+ let result = engine.run(false, &[]);+ fs::set_permissions(&directory, permissions).unwrap();+ let result = result.unwrap();+ assert!(!result.complete);+ assert_eq!(result.json["copies"]["completed"], 1);+ assert_eq!(fs::read(directory.join("Photo.jpg")).unwrap(), b"original");+ assert!(!linked(+ &engine.root(),+ "First/Photo.jpg",+ "Second/Photo.jpg"+ ));+ run(&mut engine, true);+ assert!(linked(+ &engine.root(),+ "First/Photo.jpg",+ "Second/Photo.jpg"+ ));+ fetched.assert();+}++#[test]+#[cfg(unix)]+fn engine_rechecks_link_source_after_an_edit_and_timestamp_reset() {+ use std::os::unix::fs::PermissionsExt;+ use std::time::{Duration, UNIX_EPOCH};+ let mut server = mockito::Server::new();+ let key = Key::generate();+ let (record, bytes) = original_fixture(&key, b"original", false, true, 10);+ let fetched = download(&mut server, 10, &bytes, 1);+ let mut engine = new_engine(&server, key, record.clone());+ for (id, name) in [(3, "Third"), (4, "Fourth"), (5, "Fifth")] {+ engine.catalog.albums.insert(id, name.into());+ engine.catalog.files.insert((id, 10), record.clone());+ }+ run(&mut engine, true);+ let root = engine.root();+ let first = root.join("First/Photo.jpg");+ fs::remove_file(root.join("Fourth/Photo.jpg")).unwrap();+ fs::hard_link(&first, root.join("Fourth/Photo.jpg")).unwrap();+ let changed = public(+ record,+ &engine.catalog.key,+ json!({"editedTime":1_700_000_100_000_000i64}),+ 30,+ );+ for record in engine.catalog.files.values_mut() {+ *record = changed.clone();+ }+ let modified = UNIX_EPOCH + Duration::from_secs(1_700_000_100);+ engine.hardlinks = true;+ let export = engine.prepare(engine.options(false, &[])).unwrap();+ let directory = root.join("Third");+ let permissions = fs::metadata(&directory).unwrap().permissions();+ fs::set_permissions(&directory, fs::Permissions::from_mode(0o500)).unwrap();+ let result = engine.execute_with_report(export, &|line| {+ eprintln!("{line}");+ if line.starts_with("file:3:10:") && line.contains("cannot link") {+ assert!(linked(&root, "First/Photo.jpg", "Second/Photo.jpg"));+ fs::write(&first, b"modified").unwrap();+ fs::File::options()+ .write(true)+ .open(&first)+ .unwrap()+ .set_modified(modified + Duration::from_secs(1))+ .unwrap();+ }+ });+ fs::set_permissions(&directory, permissions).unwrap();+ let result = result.unwrap();+ assert!(!result.complete);+ assert_eq!(result.json["conflicts"], 1);+ assert_eq!(result.json["copies"]["completed"], 3);+ assert_eq!(fs::read(&first).unwrap(), b"modified");+ assert_eq!(fs::metadata(&first).unwrap().modified().unwrap(), modified);+ assert_component(&root, "Fifth", "Photo.jpg", b"original");+ assert!(!linked(&root, "First/Photo.jpg", "Fifth/Photo.jpg"));+ fetched.assert();+}diff --git a/rust/crates/photos-export/Cargo.toml b/rust/crates/photos-export/Cargo.tomlindex f0ea90eefe..aa64271357 100644--- a/rust/crates/photos-export/Cargo.toml+++ b/rust/crates/photos-export/Cargo.toml@@ -11,6 +11,7 @@ ente-core.workspace = trueente-photos.workspace = trueicu_casemap.workspace = truerusqlite.workspace = true+same-file.workspace = trueserde.workspace = trueserde_json.workspace = truetempfile.workspace = truediff --git a/rust/crates/photos-export/src/lib.rs b/rust/crates/photos-export/src/lib.rsindex bbcd815bbb..14abeccff0 100644--- a/rust/crates/photos-export/src/lib.rs+++ b/rust/crates/photos-export/src/lib.rs@@ -43,6 +43,7 @@ pub struct Options {pub albums: Vec<String>,pub exclude_albums: Vec<String>,pub adopt: bool,+ pub hardlinks: bool,pub jobs: NonZeroUsize,}@@ -293,20 +294,13 @@ impl Export {store: &shared,cancel,report,+ hardlinks: self.options.hardlinks,};let result = (|| {refreshed?;run.recover()?;run.albums()?;- transfers(- &self.destination,- &shared,- session,- cancel_send,- cancel,- self.options.jobs.get(),- report,- )?;+ transfers(&run, session, cancel_send, self.options.jobs.get())?;run.check_cancel()?;run.retain_removed()})();@@ -642,14 +636,12 @@ impl Drop for CancelWorkers<'_> {}fn transfers(- root: &Path,- store: &Mutex<Store>,+ run: &reconcile::Run<'_>,session: &ente_core::Session,cancel_send: &watch::Sender<bool>,- cancel: &watch::Receiver<bool>,jobs: usize,- report: &(dyn Fn(&str) + Sync),) -> Result<()> {+ let (store, cancel, report) = (run.store, run.cancel, run.report);let (expected, files): (i64, i64) = store::lock(store)?.db.query_row("SELECT count(*),count(DISTINCT file) FROM desired_files WHERE failure IS NULL",[],@@ -665,7 +657,6 @@ fn transfers(let last_report = &last_report;workers.push(scope.spawn(move || {let _cancel_on_panic = CancelWorkers(cancel_send);- let run = reconcile::Run { root, store, cancel, report };let result = (|| {loop {if *cancel.borrow() {diff --git a/rust/crates/photos-export/src/reconcile.rs b/rust/crates/photos-export/src/reconcile.rsindex 1e1ea244c6..0cea882a3e 100644--- a/rust/crates/photos-export/src/reconcile.rs+++ b/rust/crates/photos-export/src/reconcile.rs@@ -24,6 +24,7 @@ use super::{pub struct Run<'a> {pub root: &'a Path,+ pub hardlinks: bool,pub store: &'a Mutex<Store>,pub cancel: &'a watch::Receiver<bool>,pub report: &'a (dyn Fn(&str) + Sync),@@ -52,12 +53,7 @@ impl Run<'_> {Ok(())}- pub fn temporary(- &self,- folder: &str,- album: i64,- file: Option<i64>,- ) -> Result<(Location, File)> {+ fn temporary_name(&self, folder: &str, album: i64, file: Option<i64>) -> Result<Location> {self.check_cancel()?;let temporary = Temporary {location: Location {@@ -68,13 +64,23 @@ impl Run<'_> {file,};store::lock(self.store)?.temporary(&temporary)?;- let path = self.path(&temporary.location)?;+ Ok(temporary.location)+ }++ pub fn temporary(+ &self,+ folder: &str,+ album: i64,+ file: Option<i64>,+ ) -> Result<(Location, File)> {+ let temporary = self.temporary_name(folder, album, file)?;+ let path = self.path(&temporary)?;let file = OpenOptions::new().write(true).read(true).create_new(true).open(&path)?;- Ok((temporary.location, file))+ Ok((temporary, file))}pub fn discard(&self, location: &Location) -> Result<()> {@@ -1255,7 +1261,29 @@ impl Run<'_> {}}}- if !valid {+ let path = self.path(&destination)?;+ let same_source = self.hardlinks+ && valid+ && same_file::is_same_file(+ self.path(+ &prepared+ .components+ .iter()+ .find(|source| source.role == component.role)+ .context("no original is available for linking")?+ .location,+ )?,+ &path,+ )?;+ let consolidate = self.hardlinks && valid && !same_source;+ if consolidate {+ let previous = previous.context("missing completed original")?;+ ensure!(+ fs::matches(&path, previous.size, &previous.hash, self.cancel)?,+ super::Conflict(format!("at {}: locally changed media", path.display()))+ );+ }+ if !valid || consolidate {let source = prepared.components.iter_mut()@@ -1263,6 +1291,25 @@ impl Run<'_> {.context("no verified original is available")?;let temporary = if source.temporary {source.location.clone()+ } else if self.hardlinks {+ let temporary = self.temporary_name(&album.key, album_id, Some(file.id))?;+ let from = self.path(&source.location)?;+ let to = self.path(&temporary)?;+ disk::hard_link(&from, &to).with_context(|| {+ format!("cannot link {} to {}", from.display(), path.display())+ })?;+ let properties = Properties::read(&to)?;+ if source.verified_properties.as_ref() != Some(&properties) {+ ensure!(+ fs::matches(&to, source.size, &source.hash, self.cancel)?,+ super::Conflict(format!(+ "at {}: locally changed media",+ from.display()+ ))+ );+ source.verified_properties = Some(properties);+ }+ temporary} else {let (temporary, file) = self.temporary(&album.key, album_id, Some(file.id))?;let mut writer = fs::HashWriter::new(file, self.cancel)?;@@ -1304,8 +1351,30 @@ impl Run<'_> {|| observed != published.properties.as_ref()|| renamed{- published.properties =- Some(fs::set_time(&self.path(&destination)?, intended_time)?);+ let verified = if self.hardlinks {+ let properties = Properties::read(&path)?;+ prepared+ .components+ .iter_mut()+ .find(|source| {+ source.role == component.role+ && (source.location == destination || same_source)+ })+ .and_then(|source| {+ source.verified_properties = source+ .verified_properties+ .take()+ .filter(|verified| *verified == properties);+ source.verified_properties.as_mut()+ })+ } else {+ None+ };+ let updated = fs::set_time(&path, intended_time)?;+ if let Some(verified) = verified {+ verified.modified = updated.modified;+ }+ published.properties = Some(updated);}published.intended_time = Some(intended_time);published.signature = Some(prepared.signature.clone());diff --git a/rust/crates/photos-export/src/transfer.rs b/rust/crates/photos-export/src/transfer.rsindex d633b28bf6..3b687ec1f4 100644--- a/rust/crates/photos-export/src/transfer.rs+++ b/rust/crates/photos-export/src/transfer.rs@@ -23,6 +23,21 @@ pub struct Source {pub size: u64,pub hash: String,pub temporary: bool,+ pub verified_properties: Option<Properties>,+}++impl Source {+ fn existing(component: &Component, verified_properties: Option<Properties>) -> Self {+ Self {+ role: component.role.clone(),+ extension: names::split(&component.location.name, false).1.into(),+ location: component.location.clone(),+ size: component.size,+ hash: component.hash.clone(),+ temporary: false,+ verified_properties,+ }+ }}pub struct Prepared {@@ -49,24 +64,30 @@ fn source_changed(component: &Component, file: &FileSnapshot, signature: &str) -.is_some_and(|original| original != signature))}-fn verify(run: &Run<'_>, component: &Component, replacing: bool) -> Result<Option<Properties>> {+fn verify(+ run: &Run<'_>,+ component: &Component,+ replacing: bool,+) -> Result<Option<(Properties, bool)>> {let path = run.path(&component.location)?;let Some(properties) = Properties::optional(&path)? else {return Ok(None);};- if replacing || component.properties.as_ref() != Some(&properties) {+ let hashed = replacing || component.properties.as_ref() != Some(&properties);+ if hashed {ensure!(fs::matches(&path, component.size, &component.hash, run.cancel)?,super::Conflict(format!("at {}: locally changed media", path.display())));}- Ok(Some(properties))+ Ok(Some((properties, hashed)))}pub async fn prepare(run: &Run<'_>, session: &Session, file: &FileSnapshot) -> Result<Prepared> {let signature = signature(file)?;let roles = roles(file);let mut observed = Vec::new();+ let mut reusable = Vec::new();let mut needs_bytes = false;let mut after = 0;loop {@@ -105,9 +126,19 @@ pub async fn prepare(run: &Run<'_>, session: &Session, file: &FileSnapshot) -> Rif retained_folder.as_ref() == Some(&component.location.folder) {continue;}- if let Some(properties) = verify(run, component, replacing)?+ if let Some((properties, hashed)) = verify(run, component, replacing)?&& !source_changed(component, file, &signature){+ if run.hardlinks+ && !reusable+ .iter()+ .any(|source: &Source| source.role == component.role)+ {+ reusable.push(Source::existing(+ component,+ hashed.then(|| properties.clone()),+ ));+ }verified.push((component.role.clone(), properties));}}@@ -124,11 +155,10 @@ pub async fn prepare(run: &Run<'_>, session: &Session, file: &FileSnapshot) -> Rif !needs_bytes {return Ok(Prepared {signature,- components: Vec::new(),+ components: reusable,observed,});}- let mut reusable = Vec::new();let mut before = i64::MAX;loop {run.check_cancel()?;@@ -163,20 +193,10 @@ pub async fn prepare(run: &Run<'_>, session: &Session, file: &FileSnapshot) -> R{continue;}- let valid = (|| {- let path = run.path(&component.location)?;- Properties::read(&path)?;- fs::matches(&path, component.size, &component.hash, run.cancel)- })();- match valid {- Ok(true) => reusable.push(Source {- role: component.role,- extension: names::split(&component.location.name, false).1.into(),- location: component.location,- size: component.size,- hash: component.hash,- temporary: false,- }),+ match verify(run, &component, true) {+ Ok(Some((properties, _))) => {+ reusable.push(Source::existing(&component, Some(properties)))+ }Err(error) if super::fatal(&error) => return Err(error),_ => {}}@@ -254,6 +274,10 @@ pub async fn prepare(run: &Run<'_>, session: &Session, file: &FileSnapshot) -> Rcomponents.push(Source {role,extension,+ verified_properties: run+ .hardlinks+ .then(|| Properties::read(&run.path(&temporary)?))+ .transpose()?,location: temporary,size,hash,@@ -274,6 +298,7 @@ pub async fn prepare(run: &Run<'_>, session: &Session, file: &FileSnapshot) -> Rvec![Source {role: Role::Original,extension: names::split(&file.name, false).1.into(),+ verified_properties: run.hardlinks.then(|| Properties::read(&path)).transpose()?,location: temporary,size,hash,