feat: while copying, preserve files' modified and accessed at timestamps

This commit is contained in:
rolv 2024-04-17 20:21:25 +01:00 committed by sxyazi
parent a0b4ee6e6e
commit 791b801aed
No known key found for this signature in database
3 changed files with 14 additions and 2 deletions

1
Cargo.lock generated
View file

@ -2910,6 +2910,7 @@ dependencies = [
"bitflags 2.5.0",
"crossterm",
"dirs",
"filetime",
"futures",
"libc",
"parking_lot",

View file

@ -13,6 +13,7 @@ anyhow = "1.0.82"
bitflags = "2.5.0"
crossterm = "0.27.0"
dirs = "5.0.1"
filetime = "0.2.23"
futures = "0.3.30"
parking_lot = "0.12.1"
percent-encoding = "2.3.1"

View file

@ -1,6 +1,7 @@
use std::{collections::VecDeque, path::{Path, PathBuf}};
use anyhow::Result;
use filetime::{set_file_times, FileTime};
use tokio::{fs, io, select, sync::{mpsc, oneshot}, time};
pub async fn accessible(path: &Path) -> bool {
@ -42,8 +43,17 @@ pub fn copy_with_progress(from: &Path, to: &Path) -> mpsc::Receiver<Result<u64,
let (from, to) = (from.to_path_buf(), to.to_path_buf());
async move {
_ = match fs::copy(from, to).await {
Ok(len) => tick_tx.send(Ok(len)),
_ = match fs::copy(&from, &to).await {
Ok(len) => {
// Attempt to preserve file's accessed at and modified at timestamps
if let Ok(metadata) = fs::metadata(from).await {
let mtime = FileTime::from_last_modification_time(&metadata);
let atime = FileTime::from_last_access_time(&metadata);
_ = set_file_times(to, atime, mtime);
};
tick_tx.send(Ok(len))
}
Err(e) => tick_tx.send(Err(e)),
};
}