Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

fix(core): a write to a phone folder on another volume can't destroy the file it replaces

Bee5f857e4f5f24
diff --git a/core/nectarlink-core/src/storage.rs b/core/nectarlink-core/src/storage.rs
index b9a011d..a9895be 100644
--- a/core/nectarlink-core/src/storage.rs
+++ b/core/nectarlink-core/src/storage.rs
@@ -1098,7 +1098,15 @@ impl FolderStorage {
self.ensure_inside(&canon_root, &canon_parent)?;
}
if std::fs::rename(staged, &dest).is_err() {
- std::fs::copy(staged, &dest).map_err(|e| StorageError::Failed(e.to_string()))?;
+ // Another volume: copy beside the file, then swap it in, so a
+ // failed copy leaves the file that was there untouched.
+ let name = dest.file_name().unwrap_or_default().to_string_lossy();
+ let temp = dest.with_file_name(format!(".{name}.nectarlink-part"));
+ let swapped = std::fs::copy(staged, &temp).and_then(|_| std::fs::rename(&temp, &dest));
+ if let Err(e) = swapped {
+ let _ = std::fs::remove_file(&temp);
+ return Err(StorageError::Failed(e.to_string()));
+ }
}
if let Some(ms) = modified.filter(|m| *m >= 0)
&& let Some(st) = UNIX_EPOCH.checked_add(Duration::from_millis(ms as u64))