π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Use ordinary filesystem writes for export output
Manav Rathidb50ac03b166
diff --git a/rust/apps/cli-next/src/auth/output.rs b/rust/apps/cli-next/src/auth/output.rsindex 4cb807e36c..59a7226411 100644--- a/rust/apps/cli-next/src/auth/output.rs+++ b/rust/apps/cli-next/src/auth/output.rs@@ -46,17 +46,6 @@ impl Destination {Ok(())}Self::Directory { path, .. } => {- let mut missing = Vec::new();- for directory in path.ancestors() {- if directory.as_os_str().is_empty()- || directory.try_exists().with_context(|| {- format!("cannot inspect export directory {}", directory.display())- })?- {- break;- }- missing.push(directory);- }let mut builder = fs::DirBuilder::new();builder.recursive(true);#[cfg(unix)]@@ -67,14 +56,6 @@ impl Destination {builder.create(path).with_context(|| {format!("cannot create export directory {}", path.display())})?;- for directory in missing.into_iter().rev() {- sync_directory(parent(directory)).with_context(|| {- format!(- "cannot sync parent of export directory {}",- directory.display()- )- })?;- }Ok(())}}@@ -94,12 +75,6 @@ impl Destination {.persist_noclobber(path).map_err(|error| error.error).with_context(|| format!("cannot publish {}", path.display()))?;- sync_directory(parent(path)).with_context(|| {- format!(- "{} was published, but cannot sync its directory",- path.display()- )- })?;Ok(Some(path.clone()))}Self::Directory { path, extension } => {@@ -120,16 +95,9 @@ fn parent(path: &Path) -> &Path {fn stage(directory: &Path, bytes: &[u8]) -> Result<tempfile::NamedTempFile> {let mut temporary = tempfile::NamedTempFile::new_in(directory)?;temporary.write_all(bytes)?;- temporary.as_file().sync_all()?;Ok(temporary)}-fn sync_directory(_directory: &Path) -> io::Result<()> {- #[cfg(unix)]- fs::File::open(_directory)?.sync_all()?;- Ok(())-}-fn backup_sequence(name: &str, stem: &str, extension: &str) -> Option<u64> {let suffix = name.strip_prefix(stem)?.strip_suffix(extension)?;if suffix.is_empty() {@@ -179,7 +147,6 @@ fn publish_directory(Err(error) => return Err(error.error).context("cannot publish Auth backup"),}};- sync_directory(directory).context("backup published, but cannot sync its directory")?;Ok(path)}diff --git a/rust/apps/cli-next/src/photos/mod.rs b/rust/apps/cli-next/src/photos/mod.rsindex 0b361ba81d..fe09d4f5bf 100644--- a/rust/apps/cli-next/src/photos/mod.rs+++ b/rust/apps/cli-next/src/photos/mod.rs@@ -219,10 +219,6 @@ async fn file(}).await.with_context(|| format!("cannot download file {}", file.id))?;- temporary- .as_file()- .sync_all()- .with_context(|| format!("cannot sync download to {}", output.display()))?;let bytes = temporary.as_file().metadata()?.len();temporary.persist_noclobber(&output)diff --git a/rust/crates/photos-export/src/fs.rs b/rust/crates/photos-export/src/fs.rsindex 899a069be0..c27cd6395a 100644--- a/rust/crates/photos-export/src/fs.rs+++ b/rust/crates/photos-export/src/fs.rs@@ -32,9 +32,8 @@ impl HashWriter {})}- pub fn finish(self) -> Result<(File, u64, String)> {- self.file.sync_all()?;- Ok((self.file, self.size, b64::encode(&self.digest.finalize())))+ pub fn finish(self) -> (File, u64, String) {+ (self.file, self.size, b64::encode(&self.digest.finalize()))}}@@ -77,27 +76,12 @@ pub fn directory(root: &Path, relative: &str) -> Result<()> {meta.is_dir(),super::Conflict(format!("expected directory at {}", path.display()))),- Err(error) if error.kind() == io::ErrorKind::NotFound => create_directory(&path)?,+ Err(error) if error.kind() == io::ErrorKind::NotFound => fs::create_dir_all(&path)?,Err(error) => return Err(error.into()),}Ok(())}-pub fn create_directory(path: &Path) -> Result<()> {- let mut missing = Vec::new();- for directory in path.ancestors() {- if directory.try_exists()? {- break;- }- missing.push(directory);- }- fs::create_dir_all(path)?;- for directory in missing.into_iter().rev() {- sync_parent(directory)?;- }- Ok(())-}-pub fn set_time(path: &Path, micros: i64) -> Result<Properties> {let properties = Properties::read(path)?;if properties.modified == i128::from(micros) * 1000 {@@ -114,24 +98,6 @@ pub fn set_time(path: &Path, micros: i64) -> Result<Properties> {Properties::read(path)}-pub fn sync_parent(_path: &Path) -> Result<()> {- #[cfg(unix)]- if let Some(parent) = _path.parent() {- File::open(parent)?.sync_all()?;- }- Ok(())-}--pub fn sync_move(source: &Path, destination: &Path) -> Result<()> {- if destination.parent().is_some_and(Path::exists) {- sync_parent(destination)?;- }- if source.parent() != destination.parent() && source.parent().is_some_and(Path::exists) {- sync_parent(source)?;- }- Ok(())-}-pub fn free_rename(source: &Path, destination: &Path) -> Result<()> {ensure!(!destination.try_exists()? || names::same_path(source, destination),@@ -145,9 +111,9 @@ pub fn free_rename(source: &Path, destination: &Path) -> Result<()> {pub fn move_file(source: &Path, destination: &Path) -> Result<()> {free_rename(source, destination)?;- create_directory(destination.parent().context("move has no parent")?)?;+ fs::create_dir_all(destination.parent().context("move has no parent")?)?;fs::rename(source, destination)?;- sync_move(source, destination)+ Ok(())}pub struct Json {@@ -176,8 +142,5 @@ pub fn remove(path: &Path) -> Result<()> {Err(error) if error.kind() == io::ErrorKind::NotFound => {}Err(error) => return Err(error.into()),}- if path.parent().is_some_and(Path::exists) {- sync_parent(path)?;- }Ok(())}diff --git a/rust/crates/photos-export/src/legacy/mod.rs b/rust/crates/photos-export/src/legacy/mod.rsindex 0735a35d54..3c5ea2164b 100644--- a/rust/crates/photos-export/src/legacy/mod.rs+++ b/rust/crates/photos-export/src/legacy/mod.rs@@ -288,7 +288,6 @@ pub fn move_leftovers(root: &Path, index: &Index, cancel: &watch::Receiver<bool>}fs::free_rename(&source, &destination)?;disk::rename(&source, &destination)?;- fs::sync_move(&source, &destination)?;record.album.path = name.clone();adopt::save_album(index, &record)?;break;@@ -385,7 +384,7 @@ fn move_one(let source = names::check(root, source)?;let parent = names::check(root, directory)?;if Properties::optional(&source)?.is_none() {- return fs::sync_move(&source, &parent.join(name));+ return Ok(());}for suffix in 0.. {ensure!(!*cancel.borrow(), crate::Cancelled);@@ -459,7 +458,6 @@ pub fn finish(root: &Path, store: &Store, cancel: &watch::Receiver<bool>) -> Res}if disk::read_dir(&meta)?.next().is_none() {disk::remove_dir(&meta)?;- fs::sync_parent(&meta)?;}}let absent: bool = store.db.query_row(@@ -472,7 +470,6 @@ pub fn finish(root: &Path, store: &Store, cancel: &watch::Receiver<bool>) -> Reslet path = names::check(root, &relative)?;if path.is_dir() && disk::read_dir(&path)?.next().is_none() {disk::remove_dir(&path)?;- fs::sync_parent(&path)?;}}}diff --git a/rust/crates/photos-export/src/lib.rs b/rust/crates/photos-export/src/lib.rsindex 4b79cc744c..63ba875557 100644--- a/rust/crates/photos-export/src/lib.rs+++ b/rust/crates/photos-export/src/lib.rs@@ -416,7 +416,7 @@ fn open_root(destination: &Path, expected: &Root) -> Result<Option<RootLock>> {}fn initialize(destination: &Path, root: &Root, legacy: bool) -> Result<RootLock> {- fs::create_directory(destination)?;+ std::fs::create_dir_all(destination)?;let path = names::check(destination, "export.json")?;let file = OpenOptions::new().read(true)@@ -434,8 +434,6 @@ fn initialize(destination: &Path, root: &Root, legacy: bool) -> Result<RootLock>);serde_json::to_writer_pretty(&mut lock.0, root)?;lock.0.write_all(b"\n")?;- lock.0.sync_all()?;- fs::sync_parent(&path)?;Ok(lock)}diff --git a/rust/crates/photos-export/src/reconcile.rs b/rust/crates/photos-export/src/reconcile.rsindex d3ae735467..a48780a3eb 100644--- a/rust/crates/photos-export/src/reconcile.rs+++ b/rust/crates/photos-export/src/reconcile.rs@@ -379,11 +379,10 @@ impl Run<'_> {!unrepairable,"missing album contains originals that cannot be repaired");- fs::create_directory(&destination)?;+ disk::create_dir_all(&destination)?;}- fs::sync_move(&source, &destination)?;} else {- fs::create_directory(&destination)?;+ disk::create_dir_all(&destination)?;ensure!(disk::read_dir(&destination)?.next().is_none(),super::Conflict(format!(@@ -391,14 +390,6 @@ impl Run<'_> {destination.display())));- fs::sync_parent(&destination)?;- if target.retained {- fs::sync_parent(- destination- .parent()- .context("retained album has no parent")?,- )?;- }}let store = store::lock(self.store)?;let transaction = store.db.unchecked_transaction()?;@@ -430,7 +421,7 @@ impl Run<'_> {Properties::optional(&path)?.is_none() || owned,super::Conflict(format!("at {}: unrelated occupant", path.display())));- fs::create_directory(path.parent().context("publication has no parent")?)?;+ disk::create_dir_all(path.parent().context("publication has no parent")?)?;disk::rename(&source, &path)?;} else {let (size, hash) = output.identity();@@ -447,7 +438,6 @@ impl Run<'_> {return Ok(());}}- fs::sync_move(&source, &path)?;self.acknowledge(target, &destination, &output, value)?;}Action::Move {@@ -467,7 +457,6 @@ impl Run<'_> {|| fs::matches(&to, size, &hash, self.cancel)?,super::Conflict(format!("at {}: moved bytes differ", to.display())));- fs::sync_move(&from, &to)?;} else {ensure!(!target.retained || current_media,@@ -592,7 +581,6 @@ impl Run<'_> {) -> Result<()> {let (temporary, mut file) = self.temporary(&location.folder, target.album, target.file)?;file.write_all(&json.bytes)?;- file.sync_all()?;drop(file);let output = Output::Json {role: role.clone(),@@ -1285,7 +1273,7 @@ impl Run<'_> {let (temporary, file) = self.temporary(&album.key, album_id, Some(file.id))?;let mut writer = fs::HashWriter::new(file, self.cancel)?;std::io::copy(&mut File::open(self.path(&source.location)?)?, &mut writer)?;- let (_, size, hash) = writer.finish()?;+ let (_, size, hash) = writer.finish();ensure!(size == source.size && hash == source.hash,"local original changed while copying"@@ -1653,7 +1641,6 @@ impl Run<'_> {let path = names::check(self.root, &relative)?;if directory_exists(&path)? && disk::read_dir(&path)?.next().is_none() {disk::remove_dir(&path)?;- fs::sync_parent(&path)?;}}let store = store::lock(self.store)?;diff --git a/rust/crates/photos-export/src/transfer.rs b/rust/crates/photos-export/src/transfer.rsindex e8c1c62373..fba537d72b 100644--- a/rust/crates/photos-export/src/transfer.rs+++ b/rust/crates/photos-export/src/transfer.rs@@ -279,7 +279,7 @@ pub async fn prepare(run: &Run<'_>, session: &Session, file: &FileSnapshot) -> RHashWriter::new(output, run.cancel).map_err(std::io::Error::other)});let writer = tokio::select! { result=download => result?, _=cancel.changed() => return Err(super::Cancelled.into()) };- let (archive, size, hash) = writer.finish()?;+ let (archive, size, hash) = writer.finish();let components = if file.kind == "livephoto" {let mut temporaries = Vec::new();let extracted = crate::live_photo::extract(archive, |role, extension| {@@ -292,7 +292,7 @@ pub async fn prepare(run: &Run<'_>, session: &Session, file: &FileSnapshot) -> Rlet mut components = Vec::with_capacity(2);for ((role, extension, writer), (_, _, temporary)) in extracted.into_iter().zip(temporaries){- let (_, size, hash) = writer.finish()?;+ let (_, size, hash) = writer.finish();if let Some(expected) = expected_hash(file, &role) {ensure!(hash == expected,