Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

fix(copy): fall back to a byte copy when reflink is rejected with EPERM (#4434)

Worktrunk Bot528fae0c10ae
diff --git a/src/copy.rs b/src/copy.rs
index 1ce782b7c..809134d22 100644
--- a/src/copy.rs
+++ b/src/copy.rs
@@ -39,7 +39,7 @@
//! outputs older than freshly checked-out sources.
use std::fs;
-use std::io::ErrorKind;
+use std::io::{self, ErrorKind};
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -83,6 +83,82 @@ fn classify_copy(fallback: Option<u64>) -> DataCopy {
}
}
+/// `reflink_copy::reflink_or_copy`, also falling back to a byte copy when the
+/// clone is rejected with `PermissionDenied`.
+///
+/// The crate passes `PermissionDenied` straight through, but some filesystems
+/// answer `FICLONE` with `EPERM` rather than `EOPNOTSUPP` (#4433), where
+/// `cp --reflink=auto` copies the file anyway. A genuine permission problem on
+/// the source or destination fails the retry with the same error. The crate
+/// removes a destination it created before returning the error, so the retry
+/// goes through [`copy_to_new_file`] rather than `fs::copy`, which would
+/// overwrite a file another process put there in between.
+fn reflink_or_copy(src: &Path, dest: &Path) -> io::Result<Option<u64>> {
+ fall_back_on_rejected_reflink(reflink_copy::reflink_or_copy(src, dest), || {
+ copy_to_new_file(src, dest)
+ })
+}
+
+/// Byte-copy `src` to `dest` without ever replacing something already there,
+/// failing with `AlreadyExists` if `dest` is occupied, and give it the source's
+/// permissions.
+///
+/// The bytes go to a temp file beside `dest`, which is then persisted with a
+/// no-clobber rename, so `dest` only ever appears complete. A failed copy
+/// cleans up only that temp file — never `dest`, which another process may
+/// have created meanwhile — and a cleanup that fails is reported rather than
+/// leaving a stray file behind silently.
+fn copy_to_new_file(src: &Path, dest: &Path) -> io::Result<u64> {
+ let mut reader = fs::File::open(src)?;
+ let permissions = reader.metadata()?.permissions();
+ // A bare filename's parent is `Some("")`, which no directory call accepts.
+ let dir = dest
+ .parent()
+ .filter(|parent| !parent.as_os_str().is_empty())
+ .unwrap_or_else(|| Path::new("."));
+ let mut temp = tempfile::Builder::new()
+ .prefix(".wt-tmp.")
+ .suffix(".tmp")
+ .tempfile_in(dir)?;
+ let copied = io::copy(&mut reader, &mut temp)
+ .and_then(|bytes| temp.as_file().set_permissions(permissions).map(|()| bytes));
+ match copied {
+ Ok(bytes) => match temp.persist_noclobber(dest) {
+ Ok(_) => Ok(bytes),
+ Err(e) => Err(discard_temp(e.file, e.error)),
+ },
+ Err(e) => Err(discard_temp(temp, e)),
+ }
+}
+
+/// Remove a temp file after `err`, returning `err` — or, if the removal itself
+/// fails, an error naming the file left behind.
+fn discard_temp(temp: tempfile::NamedTempFile, err: io::Error) -> io::Error {
+ let path = temp.path().to_path_buf();
+ match temp.close() {
+ Ok(()) => err,
+ Err(cleanup) => io::Error::other(format!(
+ "{err}; also failed to remove temporary file {}: {cleanup}",
+ path.display()
+ )),
+ }
+}
+
+/// The decision half of [`reflink_or_copy`], split out because no CI
+/// filesystem rejects a clone with `EPERM`.
+fn fall_back_on_rejected_reflink(
+ result: io::Result<Option<u64>>,
+ copy: impl FnOnce() -> io::Result<u64>,
+) -> io::Result<Option<u64>> {
+ match result {
+ Err(e) if e.kind() == ErrorKind::PermissionDenied => {
+ tracing::debug!("reflink rejected ({e}), falling back to a byte copy");
+ copy().map(Some)
+ }
+ result => result,
+ }
+}
+
/// Capped at 4 threads to avoid saturating the CPU — the global rayon pool is
/// much larger (2× CPU cores, tuned for network I/O in `wt list`).
static COPY_POOL: LazyLock<rayon::ThreadPool> = LazyLock::new(|| {
@@ -185,7 +261,7 @@ pub fn copy_leaf(
create_symlink(&target, src, dest)?;
DataCopy::Neither
} else {
- match reflink_copy::reflink_or_copy(src, dest) {
+ match reflink_or_copy(src, dest) {
Ok(fallback) => {
// Preserve file permissions (especially the execute bit) —
// needed on Linux, skipped on macOS.
@@ -551,6 +627,141 @@ mod tests {
assert_eq!(classify_copy(Some(4096)), DataCopy::Written);
}
+ #[test]
+ fn test_reflink_rejected_with_eperm_falls_back_to_copy() {
+ // Some filesystems reject FICLONE with EPERM rather than EOPNOTSUPP
+ // (#4433); `reflink_or_copy` returns that without trying `fs::copy`.
+ let rejected = Err(io::Error::from(ErrorKind::PermissionDenied));
+ let result = fall_back_on_rejected_reflink(rejected, || Ok(42));
+ assert_eq!(result.unwrap(), Some(42));
+
+ // A real permission problem fails the retry too, and is reported.
+ let rejected = Err(io::Error::from(ErrorKind::PermissionDenied));
+ let result = fall_back_on_rejected_reflink(rejected, || {
+ Err(io::Error::from(ErrorKind::PermissionDenied))
+ });
+ assert_eq!(result.unwrap_err().kind(), ErrorKind::PermissionDenied);
+
+ // Other outcomes pass through without a retry.
+ let retry = || -> io::Result<u64> { panic!("retried") };
+ assert_eq!(
+ fall_back_on_rejected_reflink(Ok(None), retry).unwrap(),
+ None
+ );
+ let exists = Err(io::Error::from(ErrorKind::AlreadyExists));
+ assert_eq!(
+ fall_back_on_rejected_reflink(exists, retry)
+ .unwrap_err()
+ .kind(),
+ ErrorKind::AlreadyExists
+ );
+ }
+
+ #[test]
+ fn test_rejected_reflink_fallback_does_not_overwrite_racing_destination() {
+ // The crate removes the destination it created before returning
+ // `PermissionDenied`; a file another process puts there before the
+ // byte copy must survive, and the `AlreadyExists` it causes is what
+ // `copy_leaf` skips on.
+ let temp = tempfile::tempdir().unwrap();
+ let src = temp.path().join("src");
+ let dest = temp.path().join("dest");
+ fs::write(&src, "source").unwrap();
+
+ let rejected = Err(io::Error::from(ErrorKind::PermissionDenied));
+ let result = fall_back_on_rejected_reflink(rejected, || {
+ fs::write(&dest, "racer").unwrap();
+ copy_to_new_file(&src, &dest)
+ });
+ assert_eq!(result.unwrap_err().kind(), ErrorKind::AlreadyExists);
+ assert_eq!(fs::read_to_string(&dest).unwrap(), "racer");
+ // The no-clobber persist removed its temp file.
+ assert_eq!(fs::read_dir(temp.path()).unwrap().count(), 2);
+ }
+
+ #[test]
+ fn test_copy_to_new_file_copies_contents_and_mode() {
+ let temp = tempfile::tempdir().unwrap();
+ let src = temp.path().join("script.sh");
+ let dest = temp.path().join("copy.sh");
+ fs::write(&src, "#!/bin/sh\n").unwrap();
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ fs::set_permissions(&src, fs::Permissions::from_mode(0o751)).unwrap();
+ }
+
+ assert_eq!(copy_to_new_file(&src, &dest).unwrap(), 10);
+ assert_eq!(fs::read_to_string(&dest).unwrap(), "#!/bin/sh\n");
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ let mode = fs::metadata(&dest).unwrap().permissions().mode();
+ assert_eq!(mode & 0o777, 0o751);
+ }
+
+ // A missing source creates nothing.
+ let missing = temp.path().join("missing");
+ let dest = temp.path().join("never");
+ assert_eq!(
+ copy_to_new_file(&missing, &dest).unwrap_err().kind(),
+ ErrorKind::NotFound
+ );
+ assert!(dest.symlink_metadata().is_err());
+
+ // A copy that fails partway leaves nothing at the destination and no
+ // temp file. On Unix a directory opens as a file, so the read is what
+ // fails.
+ #[cfg(unix)]
+ {
+ let src_dir = temp.path().join("dir");
+ fs::create_dir(&src_dir).unwrap();
+ let out = tempfile::tempdir().unwrap();
+ let dest = out.path().join("partial");
+ assert!(copy_to_new_file(&src_dir, &dest).is_err());
+ assert_eq!(fs::read_dir(out.path()).unwrap().count(), 0);
+ }
+ }
+
+ /// Cleanup after a failed copy only touches the temp file it created, so a
+ /// file another process puts at the destination during the copy survives.
+ #[cfg(unix)]
+ #[test]
+ fn test_failed_copy_cleanup_leaves_racing_destination() {
+ let temp = tempfile::tempdir().unwrap();
+ let out = tempfile::tempdir().unwrap();
+ let dest = out.path().join("dest");
+ fs::write(&dest, "racer").unwrap();
+
+ assert!(copy_to_new_file(temp.path(), &dest).is_err());
+ assert_eq!(fs::read_to_string(&dest).unwrap(), "racer");
+ assert_eq!(fs::read_dir(out.path()).unwrap().count(), 1);
+ }
+
+ #[test]
+ fn test_discard_temp_reports_cleanup_failure() {
+ let dir = tempfile::tempdir().unwrap();
+
+ let temp = tempfile::NamedTempFile::new_in(dir.path()).unwrap();
+ let err = discard_temp(temp, io::Error::from(ErrorKind::AlreadyExists));
+ assert_eq!(err.kind(), ErrorKind::AlreadyExists);
+ assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 0);
+
+ // The temp file is already gone, so removing it fails. Unix only:
+ // Windows can't reliably delete a file another handle holds open.
+ #[cfg(unix)]
+ {
+ let temp = tempfile::NamedTempFile::new_in(dir.path()).unwrap();
+ fs::remove_file(temp.path()).unwrap();
+ let err = discard_temp(temp, io::Error::from(ErrorKind::AlreadyExists));
+ assert!(
+ err.to_string()
+ .contains("also failed to remove temporary file"),
+ "{err}"
+ );
+ }
+ }
+
#[test]
fn test_copy_leaf_force_replaces_existing_destination() {
// The counterpart: with `force` the destination is removed and the