Fix copy operation progress

This commit is contained in:
Jeremy Soller 2024-07-11 14:38:51 -06:00
parent 5c82e72c80
commit 6c2ca8acf8
No known key found for this signature in database
GPG key ID: D02FD439211AF56F

View file

@ -250,38 +250,34 @@ impl Operation {
let msg_tx = msg_tx.clone(); let msg_tx = msg_tx.clone();
tokio::task::spawn_blocking(move || -> fs_extra::error::Result<()> { tokio::task::spawn_blocking(move || -> fs_extra::error::Result<()> {
log::info!("Copy {:?} to {:?}", paths, to); log::info!("Copy {:?} to {:?}", paths, to);
let copied_bytes = AtomicU64::default(); let total_paths = paths.len();
let total_bytes = paths for (path_i, (from, mut to)) in
.iter() paths.into_iter().zip(to.into_iter()).enumerate()
.map(fs_extra::dir::get_size) {
.sum::<Result<u64, _>>()?; let handler = |copied_bytes, total_bytes| {
let handler = || { let item_progress = copied_bytes as f32 / total_bytes as f32;
executor::block_on(async { let total_progress =
let _ = msg_tx (item_progress + path_i as f32) / total_paths as f32;
.lock() executor::block_on(async {
.await let _ = msg_tx
.send(Message::PendingProgress( .lock()
id, .await
100.0 * copied_bytes.load(atomic::Ordering::Relaxed) as f32 .send(Message::PendingProgress(id, 100.0 * total_progress))
/ total_bytes as f32, .await;
)) })
.await; };
})
};
// Files and directory progress are handled separately
let file_handler = |progress: fs_extra::file::TransitProcess| {
copied_bytes.fetch_add(progress.copied_bytes, atomic::Ordering::Relaxed);
handler();
};
let dir_handler = |progress: fs_extra::TransitProcess| {
copied_bytes.fetch_add(progress.copied_bytes, atomic::Ordering::Relaxed);
handler();
handle_progress_state(&msg_tx, &progress)
};
for (from, mut to) in paths.into_iter().zip(to.into_iter()) {
if from.is_dir() { if from.is_dir() {
let options = fs_extra::dir::CopyOptions::default().copy_inside(true); let options = fs_extra::dir::CopyOptions::default().copy_inside(true);
fs_extra::copy_items_with_progress(&[from], to, &options, dir_handler)?; fs_extra::copy_items_with_progress(
&[from],
to,
&options,
|progress: fs_extra::TransitProcess| {
handler(progress.copied_bytes, progress.total_bytes);
handle_progress_state(&msg_tx, &progress)
},
)?;
} else { } else {
let mut options = fs_extra::file::CopyOptions::default(); let mut options = fs_extra::file::CopyOptions::default();
if to.exists() { if to.exists() {
@ -309,7 +305,14 @@ impl Operation {
} }
} }
} }
fs_extra::file::copy_with_progress(from, to, &options, file_handler)?; fs_extra::file::copy_with_progress(
from,
to,
&options,
|progress: fs_extra::file::TransitProcess| {
handler(progress.copied_bytes, progress.total_bytes);
},
)?;
} }
} }
Ok(()) Ok(())