Io::copy works, but fs::copy fails

I've run into somewhat unexpected behavior and would like to know if someone already knows what this means.

In my program, at some point, I want to copy existing file into freshly created directory. When I use std::fs::copy, like this:

std::fs::copy(&source, &target)?;

it fails with "Operation not permitted (os error 1)". However, when I open two files and copy between them, like this:

let mut source = std::fs::File::open(&source)?;
let mut target = std::fs::File::create(&target)?;
std::io::copy(&mut source, &mut target)?;

it works. What might be the cause for this difference?

The fs::copy operation will also copy various metadata of the file, so it may be failing to copy that.

This make sense, since both the source and destination are on the NTFS disk mounted to the Linux system. However, wouldn't the standard cp program fail in this case too?

The cp commandline tool might know about NTFS or otherwise know how to better handle copies of metadata. The fs::copy function is not implemented as a call to cp, after all.

Running the program under the strace command should help determine exactly what operation is failing.

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.