fs_extra icon indicating copy to clipboard operation
fs_extra copied to clipboard

Recursive copy semantics are very unclear

Open dpc opened this issue 6 years ago • 5 comments
trafficstars

is

fs_extra::dir_copy("a/b", "c/d");

goint to create c/d/b, or just c/d? Does it depend on c/d existing or not? The documentation is very unclear.

"Copies the directory contents from one place to another using recursive method. This function will also copy the permission bits of the original files to destionation files (not for directories)."

Explains non of it.

dpc avatar Mar 01 '19 00:03 dpc

Sorry, but fs_extra does not exist method dir_copy. fs_extra have this method fs_extra::dir::copy which contains third argument copy_options, where you can set necessary settings for copy operation.

If you are expected this c/d/b behavior, you should set true for the copy_inside property of copy_option object

webdesus avatar Mar 01 '19 22:03 webdesus

This is all not well explained in documentation. I was able to figure it out myself, but it require some trail and error.

dpc avatar Mar 01 '19 23:03 dpc

Thank you for your feedback, i will improve docs :)

webdesus avatar Mar 02 '19 00:03 webdesus

I have to agree that the semantics are are bit weird:

Taking a look at the copy function:

    if options.copy_inside {
        if to.exists() {
            if let Some(dir_name) = from.components().last() {
                to.push(dir_name.as_os_str());
            } else {
                err!("Invalid folder from", ErrorKind::InvalidFolder);
            }
        }
    } else {
        if let Some(dir_name) = from.components().last() {
            to.push(dir_name.as_os_str());
        } else {
            err!("Invalid folder from", ErrorKind::InvalidFolder);
        }
    }

Depending on the fact that the target directory exists or not, the outcome is different. And there seems to be no way to influence that.

e.g.: copying a/b from a to c, setting copy_inside=true will result in:

  • c/b - if c does not exist
  • or c/a/b - if c did already exist

So the behavior of copy with copy_inside=true is not as cp -r.

Which causes as problem when you want to copy from multiple source directories, to a single target directory with copy_inside=true.

Also is the documentation of https://docs.rs/fs_extra/1.1.0/fs_extra/dir/fn.copy.html wrong IMHO. It refers to a non-existing copy option named mirror_copy.

I guess the right fix would be to simple drop the "if exists" check for copy_inside.

ctron avatar Jan 09 '20 15:01 ctron

Hi @ctron,

is not as cp -r

I checked right now behaviors cp -r and this behavior the same as mine. These operations have a different outcome if the target folder exists or doesn't ...

It refers to a non-existing copy option named mirror_copy.

My bad... i missed this moment, when changed old name(mirror_copy) by new (copy_inside)

webdesus avatar Jan 09 '20 21:01 webdesus