Crate fs_more

source ·
Expand description

Convenient file and directory operations built on top of std::fs with improved error handling and in-depth configuration. Includes copying or moving files and directories with progress reporting.

§Main features

  • copy and move files or directories with:
    • in-depth configuration options (existing destination file behaviour, copying depth, IO buffer sizes, etc.), and
    • progress reporting, if wanted,
  • scan directories (with options such as scan depth and symlink behaviour), and
  • calculate file or directory sizes.

Visit the directory and file modules for a deeper overview of the available features.
Additionally, you can find the error types in the error module.


§Feature flags

dunce  (enabled by default)

Enables the optional support for dunce which automatically strips Windows’ UNC paths if they can be represented as non-UNC paths (e.g., \\?\C:\foo as C:\foo). This is done both internally and in external results from e.g., DirectoryScanner.

This feature is enabled by default — and highly recommended — because path canonicalization on Windows very commonly returns UNC paths. dunce only has an effect when compiling for Windows targets.

fs-err  (disabled by default)

Enables the optional support for fs-err which provides more helpful error messages for underlying IO errors. It should be noted that fs-more does already provide plenty of context on errors by itself, which is why this is disabled by default.


§Examples

Copying a file and getting updates on the progress:

let source_path = Path::new("./source-file.txt");
let destination_path = Path::new("./target-file.txt");

let finished_copy = fs_more::file::copy_file_with_progress(
    source_path,
    destination_path,
    FileCopyWithProgressOptions {
        colliding_file_behaviour: CollidingFileBehaviour::Abort,
        ..Default::default()
    },
    |progress| {
        let percent_copied =
            (progress.bytes_finished as f64) / (progress.bytes_total as f64)
            * 100.0;

        println!("Copied {:.2}% of the file!", percent_copied);
    }
)?;

match finished_copy {
    FileCopyFinished::Created { bytes_copied } => {
        println!("Copied {bytes_copied} bytes!");
    }
    FileCopyFinished::Overwritten { bytes_copied } => {
        println!("Copied {bytes_copied} bytes over an existing file!");
    }
    // ...
    _ => {}
};

Moving a directory and getting updates on the progress:

let source_path = Path::new("./source-directory");
let destination_path = Path::new("./target-directory");

let moved = fs_more::directory::move_directory_with_progress(
    source_path,
    destination_path,
    DirectoryMoveWithProgressOptions {
        destination_directory_rule: DestinationDirectoryRule::AllowEmpty,
        ..Default::default()
    },
    |progress| {
        let percent_moved =
            (progress.bytes_finished as f64) / (progress.bytes_total as f64)
            * 100.0;

        println!(
            "Moved {:.2}% of the directory ({} files and {} directories so far).",
            percent_moved,
            progress.files_moved,
            progress.directories_created
        );
    }
)?;

println!(
    "Moved {} bytes ({} files, {} directories)! Underlying strategy: {:?}.",
    moved.total_bytes_moved,
    moved.files_moved,
    moved.directories_moved,
    moved.strategy_used
);

Modules§

  • Directory copying, moving, scanning and sizing operations. Includes progress monitoring variants.
  • Error types provided by this library.
  • File copying, moving, sizing and removal operations. Includes progress monitoring variants.