Crate cp_r

source · []
Expand description

Copy a directory tree, including mtimes and permissions.

To copy a tree, first configure parameters on CopyOptions and then call CopyOptions::copy_tree.

Features

  • Minimal dependencies: currently just filetime to support copying mtimes.
  • Returns CopyStats describing how much data and how many files were copied.
  • Tested on Linux, macOS and Windows.
  • Copies mtimes and permissions.
  • Takes an optional callback to decide which entries are copied or skipped, CopyOptions::filter.
  • Takes an optional callback to show progress or record which files are copied, CopyOptions::after_entry_copied.

Missing features that could be added

  • Options to not copy mtimes or permissions.
  • A callback that can decide whether to continue after an error.
  • Overwrite existing directories or files.
  • Copy single files: don’t assume the source path is a directory.
  • A dry-run mode.

Example

use std::path::Path;
use cp_r::{CopyOptions, CopyStats};
use tempfile;

// Copy this crate's `src` directory.
let dest = tempfile::tempdir().unwrap();
let stats = CopyOptions::new().copy_tree(Path::new("src"), dest.path()).unwrap();
assert_eq!(stats.files, 1, "only one file in src");
assert_eq!(stats.dirs, 0, "no children");
assert_eq!(stats.symlinks, 0, "no symlinks");

Release history

0.5.1

Released 2022-03-24.

  • Change: Ignore errors when trying to set the mtime on copied files. This can happen on Windows if the file is read-only.

0.5.0

Released 2022-02-15

API changes

0.4.0

Released 2021-11-30

API changes

  • Remove copy_buffer_size, file_buffers_copied: these are too niche to have in the public API, and anyhow become meaningless when we use std::fs::copy.

  • New ErrorKind::DestinationDoesNotExist.

  • Error::io_error returns Option<io::Error> (previously just an io::Error): errors from this crate may not have a direct io::Error source.

  • CopyOptions::copy_tree arguments are relaxed to AsRef<Path> so that they will accept &str, PathBuf, tempfile::TempDir, etc.

Improvements

  • Use std::fs::copy, which is more efficient, and makes this crate simpler.

0.3.1

Released 2021-11-07

API changes

  • CopyOptions::copy_tree consumes self (rather than taking &mut self), which reduces lifetime issues in accessing values owned by callbacks.

New features

0.3.0

Released 2021-11-06

API changes

  • CopyOptions builder functions now return self rather than &mut self.
  • The actual copy operation is run by calling CopyOptions::copy_tree, rather than passing the options as a parameter to copy_tree.
  • Rename with_copy_buffer_size to copy_buffer_size.

New features

  • A new option to provide a filter on which entries should be copied, through CopyOptions::filter.

0.2.0

  • copy_tree will create the immediate destination directory by default, but this can be controlled by CopyOptions::create_destination. The destination, if created, is counted in CopyStats::dirs and inherits its permissions from the source.

0.1.1

0.1.0

  • Initial release.

Structs

Options for copying file trees.

Counters of how many things were copied.

An error from copying a tree.

Enums

Various kinds of errors that can occur while copying a tree.

Type Definitions

A std::result::Result possibly containing a cp_r Error.