1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
//! 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.
//!
//! <br>
//!
//! Visit the **[`directory`]** and **[`file`][mod@file]** modules
//! for a deeper overview of the available features.
//! <br> Additionally, you can find the error types in the [`error`] module.
//!
//!
//! <br>
//!
//! # Feature flags
//! <table>
//! <thead style="background-color: rgba(0, 0, 0, 0.18)">
//! <tr>
//! <th style="text-align:left">
//!
//! **`dunce`**
//! <span style="font-weight: normal"> (<i>enabled</i> by default)</span>
//! </th>
//! </tr>
//! </thead>
//! <tbody>
//! <tr>
//! <td>
//!
//! Enables the optional support for [`dunce`](https://docs.rs/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.
//! </td>
//! </tr>
//! </tbody>
//! </table>
//!
//!
//! <table>
//! <thead style="background-color: rgba(0, 0, 0, 0.18)">
//! <tr>
//! <th style="text-align:left">
//!
//! **`fs-err`**
//! <span style="font-weight: normal"> (disabled by default)</span>
//! </th>
//! </tr>
//! </thead>
//! <tbody>
//! <tr>
//! <td>
//!
//! Enables the optional support for [`fs-err`](https://docs.rs/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.
//! </td>
//! </tr>
//! </tbody>
//! </table>
//!
//!
//! <br>
//!
//! # Examples
//!
//! Copying a file and getting updates on the progress:
//! ```no_run
//! # use std::path::Path;
//! # use fs_more::error::FileError;
//! # use fs_more::file::FileCopyWithProgressOptions;
//! # use fs_more::file::FileCopyFinished;
//! # use fs_more::file::CollidingFileBehaviour;
//! # fn main() -> Result<(), FileError> {
//! 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!");
//! }
//! // ...
//! _ => {}
//! };
//!
//! # Ok(())
//! # }
//! ```
//!
//! <br>
//!
//! Moving a directory and getting updates on the progress:
//! ```no_run
//! # use std::path::Path;
//! # use fs_more::error::MoveDirectoryError;
//! # use fs_more::directory::DirectoryMoveWithProgressOptions;
//! # use fs_more::directory::DestinationDirectoryRule;
//! # fn main() -> Result<(), MoveDirectoryError> {
//! 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
//! );
//! # Ok(())
//! # }
//! ```
//!
//! [`DirectoryScanner`]: crate::directory::DirectoryScanner
#![warn(missing_docs)]
/// This brings in the README.md's doctests (and is present only when testing).
#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;
/// This brings in the CONTRIBUTING.md's doctests (and is present only when testing).
#[doc = include_str!("../CONTRIBUTING.md")]
#[cfg(doctest)]
pub struct ContributionGuideDoctests;
/// Default file read buffer size, used as a default in progress tracking functions.
/// Currently equals 64 KiB.
///
/// See also:
/// - [`FileCopyWithProgressOptions`][crate::file::FileCopyWithProgressOptions]
/// - [`FileMoveWithProgressOptions`][crate::file::FileMoveWithProgressOptions]
/// - [`DirectoryCopyWithProgressOptions`][crate::directory::DirectoryCopyWithProgressOptions]
/// - [`DirectoryMoveWithProgressOptions`][crate::directory::DirectoryMoveWithProgressOptions]
const DEFAULT_READ_BUFFER_SIZE: usize = 1024 * 64;
/// Default file write buffer size, used as a default in progress tracking functions.
/// Currently equals 64 KiB.
///
/// See also:
/// - [`FileCopyWithProgressOptions`][crate::file::FileCopyWithProgressOptions]
/// - [`FileMoveWithProgressOptions`][crate::file::FileMoveWithProgressOptions]
/// - [`DirectoryCopyWithProgressOptions`][crate::directory::DirectoryCopyWithProgressOptions]
/// - [`DirectoryMoveWithProgressOptions`][crate::directory::DirectoryMoveWithProgressOptions]
const DEFAULT_WRITE_BUFFER_SIZE: usize = 1024 * 64;
/// Default progress reporting interval, used as a default in progress tracking functions.
/// Currently equals 512 KiB.
///
/// See also:
/// - [`FileCopyWithProgressOptions`][crate::file::FileCopyWithProgressOptions]
/// - [`FileMoveWithProgressOptions`][crate::file::FileMoveWithProgressOptions]
/// - [`DirectoryCopyWithProgressOptions`][crate::directory::DirectoryCopyWithProgressOptions]
/// - [`DirectoryMoveWithProgressOptions`][crate::directory::DirectoryMoveWithProgressOptions]
const DEFAULT_PROGRESS_UPDATE_BYTE_INTERVAL: u64 = 1024 * 512;
#[macro_use]
mod macros;
pub mod directory;
pub mod error;
pub mod file;