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.

Windows: wait for a file the virus scanner holds before renaming it

Yashwanth Kamireddi2207622cc88c
diff --git a/crates/quince-core/src/files.rs b/crates/quince-core/src/files.rs
new file mode 100644
index 0000000..9b91828
--- /dev/null
+++ b/crates/quince-core/src/files.rs
@@ -0,0 +1,161 @@
+//! Moving a file into place, made to hold on Windows.
+//!
+//! A file just written is opened straight after by the virus scanner (Defender scans each new
+//! file as it is closed), the search indexer, or Explorer making a thumbnail. While one of them
+//! has it open, Windows refuses to rename it, or the folder it is in, with "access is denied"
+//! (ERROR_ACCESS_DENIED) or "being used by another process" (ERROR_SHARING_VIOLATION,
+//! ERROR_LOCK_VIOLATION) — for a moment, until they let go. A rename that failed on that at
+//! once failed an export at its very last step, and a download that had already arrived. Here
+//! the same rename is tried again for a while before it is given up on; anything else is said
+//! at once, and elsewhere nothing changes.
+
+use std::io;
+use std::path::Path;
+use std::time::Duration;
+
+/// How long a rename waits for Windows to let go of a file by default: a scan of a large
+/// photo or video takes a few seconds.
+pub const HELD_FOR: Duration = Duration::from_secs(5);
+
+/// The pause between two tries.
+const STEP: Duration = Duration::from_millis(50);
+
+/// Whether a Windows error code says the file is held by someone else for now: access denied
+/// (5), a sharing violation (32), a lock violation (33).
+fn held_code(code: Option<i32>) -> bool {
+ matches!(code, Some(5 | 32 | 33))
+}
+
+/// Whether `e` is Windows saying a file is held by someone else for now. Never elsewhere: the
+/// same numbers mean other things there (5 is EIO on Linux).
+pub fn held_elsewhere(e: &io::Error) -> bool {
+ cfg!(windows) && held_code(e.raw_os_error())
+}
+
+/// `op`, tried again while it fails with an error `held` says will pass, for up to `within`
+/// (`wait` is the pause between tries). Any other answer is returned as it is.
+pub fn patiently<T>(
+ within: Duration,
+ held: impl Fn(&io::Error) -> bool,
+ mut wait: impl FnMut(Duration),
+ mut op: impl FnMut() -> io::Result<T>,
+) -> io::Result<T> {
+ let mut waited = Duration::ZERO;
+ loop {
+ match op() {
+ Err(e) if held(&e) && waited < within => {
+ wait(STEP);
+ waited += STEP;
+ }
+ done => return done,
+ }
+ }
+}
+
+/// [`std::fs::rename`], waiting up to [`HELD_FOR`] for a file Windows says is held.
+pub fn rename(from: &Path, to: &Path) -> io::Result<()> {
+ rename_within(from, to, HELD_FOR)
+}
+
+/// [`std::fs::rename`], waiting up to `within` for a file Windows says is held.
+pub fn rename_within(from: &Path, to: &Path, within: Duration) -> io::Result<()> {
+ patiently(within, held_elsewhere, std::thread::sleep, || {
+ std::fs::rename(from, to)
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use std::cell::Cell;
+
+ fn sharing_violation() -> io::Error {
+ io::Error::from_raw_os_error(32)
+ }
+
+ /// Held for a moment, as Defender holds a file it is scanning: tried again until it goes.
+ #[test]
+ fn a_file_held_for_a_moment_is_moved_once_it_is_let_go() {
+ let tries = Cell::new(0);
+ let waits = Cell::new(0);
+ let done = patiently(
+ Duration::from_secs(5),
+ |e| held_code(e.raw_os_error()),
+ |_| waits.set(waits.get() + 1),
+ || {
+ tries.set(tries.get() + 1);
+ if tries.get() < 4 {
+ Err(sharing_violation())
+ } else {
+ Ok("moved")
+ }
+ },
+ );
+ assert_eq!(done.unwrap(), "moved");
+ assert_eq!(tries.get(), 4);
+ assert_eq!(waits.get(), 3);
+ }
+
+ /// Held for good: given up on once the time is spent, with Windows' own error.
+ #[test]
+ fn a_file_held_for_good_is_given_up_on_in_time() {
+ let tries = Cell::new(0);
+ let done: io::Result<()> = patiently(
+ Duration::from_millis(500),
+ |e| held_code(e.raw_os_error()),
+ |_| {},
+ || {
+ tries.set(tries.get() + 1);
+ Err(sharing_violation())
+ },
+ );
+ assert_eq!(done.unwrap_err().raw_os_error(), Some(32));
+ assert_eq!(
+ tries.get(),
+ 11,
+ "ten waits of 50 ms, and the try after them"
+ );
+ }
+
+ /// Any other failure — the file gone, a full disk — is said at once.
+ #[test]
+ fn any_other_failure_is_said_at_once() {
+ let tries = Cell::new(0);
+ let done: io::Result<()> = patiently(
+ Duration::from_secs(5),
+ |e| held_code(e.raw_os_error()),
+ |_| panic!("no wait for an error that will not pass"),
+ || {
+ tries.set(tries.get() + 1);
+ Err(io::Error::from(io::ErrorKind::NotFound))
+ },
+ );
+ assert_eq!(done.unwrap_err().kind(), io::ErrorKind::NotFound);
+ assert_eq!(tries.get(), 1);
+ }
+
+ #[test]
+ fn only_windows_holding_a_file_counts() {
+ assert!(held_code(Some(5)) && held_code(Some(32)) && held_code(Some(33)));
+ assert!(!held_code(Some(2)) && !held_code(Some(112)) && !held_code(None));
+ // Elsewhere the same numbers are other errors, never waited on.
+ assert_eq!(
+ held_elsewhere(&io::Error::from_raw_os_error(5)),
+ cfg!(windows)
+ );
+ }
+
+ #[test]
+ fn a_rename_moves_the_file() {
+ let dir = tempfile::tempdir().unwrap();
+ let (from, to) = (dir.path().join("a.part"), dir.path().join("a.pdf"));
+ std::fs::write(&from, b"x").unwrap();
+ rename(&from, &to).unwrap();
+ assert!(!from.exists());
+ assert_eq!(std::fs::read(&to).unwrap(), b"x");
+ assert_eq!(
+ rename(&from, &to).unwrap_err().kind(),
+ io::ErrorKind::NotFound
+ );
+ }
+}
diff --git a/crates/quince-core/src/lib.rs b/crates/quince-core/src/lib.rs
index 82c2bdd..bc8022d 100644
--- a/crates/quince-core/src/lib.rs
+++ b/crates/quince-core/src/lib.rs
@@ -10,6 +10,7 @@ pub mod changes;
pub mod contacts;
pub mod drive;
pub mod export;
+pub mod files;
pub mod fold;
pub mod health;
pub mod notes;
diff --git a/crates/quince-icloud/src/drive.rs b/crates/quince-icloud/src/drive.rs
index 4262091..b7c95b9 100644
--- a/crates/quince-icloud/src/drive.rs
+++ b/crates/quince-icloud/src/drive.rs
@@ -312,8 +312,15 @@ pub async fn download_to(
Ok(())
}
// A filesystem without links — a FAT or exFAT drive — takes a rename, checked first.
+ // So does a file Windows holds for a moment (the virus scanner, looking at it as it was
+ // closed): the rename waits for it to be let go (quince_core::files).
Err(e) if e.kind() != std::io::ErrorKind::AlreadyExists && !destination.exists() => {
- tokio::fs::rename(&partial, destination).await.map_err(|e| {
+ let (from, to) = (partial.clone(), destination.to_path_buf());
+ let moved = tokio::task::spawn_blocking(move || quince_core::files::rename(&from, &to))
+ .await
+ .map_err(std::io::Error::other)
+ .and_then(|r| r);
+ moved.map_err(|e| {
let _ = std::fs::remove_file(&partial);
e.into()
})
diff --git a/crates/quince-icloud/src/photos.rs b/crates/quince-icloud/src/photos.rs
index 578228e..ba6fa94 100644
--- a/crates/quince-icloud/src/photos.rs
+++ b/crates/quince-icloud/src/photos.rs
@@ -1098,8 +1098,11 @@ pub async fn fetch_to_file(
"the picture broke off: {e}"
)));
}
- tokio::fs::rename(&partial, path)
+ // Waiting while Windows holds the file just written (quince_core::files).
+ let (from, to) = (partial.clone(), path.to_path_buf());
+ tokio::task::spawn_blocking(move || quince_core::files::rename(&from, &to))
.await
+ .map_err(|e| crate::Error::Protocol(e.to_string()))?
.map_err(|e| crate::Error::Protocol(e.to_string()))?;
Ok(true)
}
diff --git a/src-tauri/src/export.rs b/src-tauri/src/export.rs
index db5fc29..5a55b25 100644
--- a/src-tauri/src/export.rs
+++ b/src-tauri/src/export.rs
@@ -37,6 +37,11 @@ pub const CANCELLED: &str = "Export cancelled.";
/// and may clear it — and nothing else.
const MARKER: &str = ".skykeep-export";
+/// How long the finished export's folder waits to be renamed into place while Windows holds a
+/// file in it: a scan of the last large videos written can take a while, and the alternative
+/// is an export of hours thrown away at its very last step.
+const EXPORT_HELD_FOR: std::time::Duration = std::time::Duration::from_secs(30);
+
static RUNNING: AtomicBool = AtomicBool::new(false);
static CANCEL: AtomicBool = AtomicBool::new(false);
/// The last finished export, the one folder Show in Folder may show.
@@ -744,7 +749,10 @@ pub(crate) fn run(
if cancel.load(Ordering::Relaxed) {
return Err(Stop::Cancelled);
}
- std::fs::rename(&partial, &fin)?;
+ // On Windows a folder cannot be renamed while any file in it is open, and the files
+ // just written are being scanned (Defender), indexed, or made thumbnails of: the
+ // rename is tried again until they let go, rather than the whole export thrown away.
+ quince_core::files::rename_within(&partial, &fin, EXPORT_HELD_FOR)?;
summary.path = fin.to_string_lossy().into_owned();
Ok(summary)
})();
diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs
index d9a8fb1..c933c6f 100644
--- a/src-tauri/src/main.rs
+++ b/src-tauri/src/main.rs
@@ -792,7 +792,7 @@ async fn note_attachment_open(
.await
.map_err(|e| e.to_string())?;
}
- tokio::fs::rename(&partial, &path)
+ move_into_place(&partial, &path)
.await
.map_err(|e| e.to_string())?;
saving::mark_from_internet(&path);
@@ -5411,6 +5411,19 @@ fn photo_path(dir: &std::path::Path, guid: &str, signature: &str) -> PathBuf {
dir.join("faces").join(format!("{guid_safe}-{safe}"))
}
+/// A file just written renamed to its own name, waiting while Windows holds it — the virus
+/// scanner looks at each new file as it is closed, and a rename meanwhile is refused
+/// (quince_core::files). Off the async threads: the wait is a sleep.
+pub(crate) async fn move_into_place(
+ from: &std::path::Path,
+ to: &std::path::Path,
+) -> std::io::Result<()> {
+ let (from, to) = (from.to_path_buf(), to.to_path_buf());
+ tokio::task::spawn_blocking(move || quince_core::files::rename(&from, &to))
+ .await
+ .map_err(std::io::Error::other)?
+}
+
/// Write `bytes` to `path` whole or not at all: to a file of its own beside it, then renamed
/// over it. Written in place, a crash or a full disk part way left a cut-off picture that was
/// served from then on — the read side only asks whether the file is there.
diff --git a/src-tauri/src/photos.rs b/src-tauri/src/photos.rs
index c544b93..ed99c78 100644
--- a/src-tauri/src/photos.rs
+++ b/src-tauri/src/photos.rs
@@ -406,7 +406,7 @@ async fn heic_full(app: &AppHandle, id: &str, path: &std::path::Path) -> Result<
tokio::fs::write(&part, &jpeg)
.await
.map_err(|e| e.to_string())?;
- tokio::fs::rename(&part, path)
+ crate::move_into_place(&part, path)
.await
.map_err(|e| e.to_string())?;
crate::photo_cache::hold(path);
@@ -2334,7 +2334,7 @@ pub async fn photo_download(
return Err(e);
}
drop(out);
- tokio::fs::rename(&part, &path)
+ crate::move_into_place(&part, &path)
.await
.map_err(|e| e.to_string())?;
crate::saving::mark_from_internet(&path);