Use reflink_copy::reflink_or_copy in fs::atomic_install* (#1197)

to speedup copy operation `atomic_install*`.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2023-07-17 11:56:10 +10:00 committed by GitHub
parent b3b682a1af
commit 963e9e97ad
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 5 deletions

View file

@ -29,6 +29,7 @@ maybe-owned = "0.3.4"
miette = "5.9.0"
normalize-path = { version = "0.2.1", path = "../normalize-path" }
once_cell = "1.18.0"
reflink-copy = "0.1.5"
semver = { version = "1.0.17", features = ["serde"] }
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.99"

View file

@ -1,24 +1,27 @@
use std::{fs, io, path::Path};
use reflink_copy::reflink_or_copy;
use tempfile::{NamedTempFile, TempPath};
use tracing::{debug, warn};
fn copy_to_tempfile(src: &Path, dst: &Path) -> io::Result<NamedTempFile> {
let mut src_file = fs::File::open(src)?;
let parent = dst.parent().unwrap();
debug!("Creating named tempfile at '{}'", parent.display());
let mut tempfile = NamedTempFile::new_in(parent)?;
let tempfile = NamedTempFile::new_in(parent)?;
debug!(
"Copying from '{}' to '{}'",
src.display(),
tempfile.path().display()
);
io::copy(&mut src_file, tempfile.as_file_mut())?;
// src and dst is likely to be on the same filesystem.
// Uses reflink if the fs support it, or fallback to
// `fs::copy` if it doesn't support it or it is not on the
// same filesystem.
reflink_or_copy(src, tempfile.path())?;
debug!("Retrieving permissions of '{}'", src.display());
let permissions = src_file.metadata()?.permissions();
let permissions = src.metadata()?.permissions();
debug!(
"Setting permissions of '{}' to '{permissions:#?}'",