Skip to main content

try_reflink

Function try_reflink 

Source
pub fn try_reflink(source: &Path, dest: &Path) -> Result<ReflinkOutcome>
Expand description

Try a filesystem-level reflink (copy-on-write clone) from source to dest. On success the destination has its own inode and shares physical blocks with the source until either side is modified.

On a successful reflink: returns Ok(ReflinkOutcome::Cloned). The destination file has been created with the kernel’s choice of permissions (typically the source’s). Callers should set_permissions afterwards if they need a specific mode.

On a “filesystem doesn’t support reflinks” verdict (EXDEV, EOPNOTSUPP, ENOTSUP, ENOSYS, EINVAL from the ioctl form): returns Ok(ReflinkOutcome::Unsupported). The caller should fall back to fs::copy and may skip future reflink attempts on this filesystem.

When the source is gone (missing at the pre-check, or ENOENT from the syscall in the TOCTOU window after it): returns Ok(ReflinkOutcome::SourceVanished). The caller should fall back to a copy/bytes-write for this blob only and keep reflinks enabled for the rest of the batch — a vanished mirror says nothing about the filesystem’s reflink capability.

On any other I/O error: returns Err.

dest must not already exist on macOS (clonefile requires a nonexistent destination). On Linux FICLONE requires the dest fd be opened for writing on a regular file, which we create with O_CREAT | O_WRONLY | O_TRUNC.