Enhance OSDK performance by using hard link instead of copy

This commit is contained in:
Ruize Tang
2024-12-09 17:03:58 +08:00
committed by Tate, Hongliang Tian
parent 7601509e6e
commit 9d82ac8958
6 changed files with 37 additions and 19 deletions

View File

@ -3,7 +3,7 @@
use std::{
ffi::OsStr,
fs::{self, File},
io::{BufRead, BufReader, Write},
io::{BufRead, BufReader, Result, Write},
path::{Path, PathBuf},
process::Command,
};
@ -290,3 +290,24 @@ impl Drop for DirGuard {
std::env::set_current_dir(&self.0).unwrap();
}
}
/// Attempts to create a hard link from `from` to `to`.
/// If the hard link operation fails (e.g., due to crossing file systems),
/// it falls back to performing a file copy.
///
/// # Arguments
/// - `from`: The source file path.
/// - `to`: The destination file path.
///
/// # Returns
/// - `Ok(0)` if the hard link is successfully created (no data was copied).
/// - `Ok(size)` where `size` is the number of bytes copied if the hard link failed and a copy was performed.
/// - `Err(error)` if an error occurred during the copy operation.
pub fn hard_link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<u64> {
if fs::hard_link(&from, &to).is_err() {
info!("Copying {:?} -> {:?}", from.as_ref(), to.as_ref());
return fs::copy(from, to);
}
info!("Linking {:?} -> {:?}", from.as_ref(), to.as_ref());
Ok(0)
}