Function copy_dir::copy_dir [] [src]

pub fn copy_dir<Q: AsRef<Path>, P: AsRef<Path>>(
    from: P,
    to: Q
) -> Result<Vec<Error>>

Copy a directory and its contents

Unlike e.g. the cp -r command, the behavior of this function is simple and easy to understand. The file or directory at the source path is copied to the destination path. If the source path points to a directory, it will be copied recursively with its contents.

Errors

  • It's possible for many errors to occur during the recursive copy operation. These errors are all returned in a Vec. They may or may not be helpful or useful.
  • If the source path does not exist.
  • If the destination path exists.
  • If something goes wrong with copying a regular file, as with std::fs::copy().
  • If something goes wrong creating the new root directory when copying a directory, as with std::fs::create_dir.
  • If you try to copy a directory to a path prefixed by itself i.e. copy_dir(".", "./foo"). See below for more details.

Caveats/Limitations

I would like to add some flexibility around how "edge cases" in the copying operation are handled, but for now there is no flexibility and the following caveats and limitations apply (not by any means an exhaustive list):

  • You cannot currently copy a directory into itself i.e. copy_dir(".", "./foo"). This is because we are recursively walking the directory to be copied while we're copying it, so in this edge case you get an infinite recursion. Fixing this is the top of my list of things to do with this crate.
  • Hard links are not accounted for, i.e. if more than one hard link pointing to the same inode are to be copied, the data will be copied twice.
  • Filesystem boundaries may be crossed.
  • Symbolic links will be copied, not followed.