How to implement copy with progress

I'm trying to create a tool which copies a file tree from A to B as fast as possible. It already works very well, but what is still missing is a more fine grained progress information for big files.
Currently I'm using fs::copy, but it doesn't expose the progress callback feature which is exposed by the system function it calls.
I've tried it with io::copy, but unfortunately this function is several times slower than fs::copy. At least on Windows.
I thought to reimplement fs::copy, but this would be a huge effort. At least when it should support several platforms.

The performance differences you see are because std::io::copy() will read the file into memory and write it to the destination chunk-by-chunk and operates entirely in user space, whereas std::fs::copy() is usually a call into the operating system and avoids a lot of overhead (no copying between user space and kernel space, the OS can talk to disk drivers directly, etc.). The OS may also use tricks like copy-on-write so data doesn't actually get copied until parts of the file are changed.

I imagine the OS will provide some way to hook into copy progress, but it'll be highly OS-specific and I don't know of any cross-platform crate that handles this for you.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.