fs_more/
lib.rs

1//! Convenient file and directory operations built on top of [`std::fs`] with improved error handling and in-depth configuration.
2//! Includes copying or moving files and directories with progress reporting.
3//!
4//!
5//! # Main features
6//! - copy and move files or directories with:
7//!     - in-depth configuration options (existing destination file behaviour, copying depth, IO buffer sizes, etc.), and
8//!     - **progress reporting**, if wanted,
9//! - scan directories (with options such as scan depth and symlink behaviour), and
10//! - calculate file or directory sizes.
11//!
12//! <br>
13//!
14//! Visit the **[`directory`]** and **[`file`][mod@file]** modules
15//! for a deeper overview of the available features.
16//! <br> Additionally, you can find the error types in the [`error`] module.
17//!
18//!
19//! <br>
20//!
21//! # Feature flags
22//! <table>
23//!  <thead style="background-color: rgba(0, 0, 0, 0.18)">
24//!   <tr>
25//!    <th style="text-align:left">
26//!
27//! **`dunce`**
28//! <span style="font-weight: normal">&nbsp;(<i>enabled</i> by default)</span>
29//!    </th>
30//!   </tr>
31//!  </thead>
32//!  <tbody>
33//!   <tr>
34//!    <td>
35//!
36//! Enables the optional support for [`dunce`](https://docs.rs/dunce) which automatically strips Windows' UNC paths
37//! if they can be represented as non-UNC paths (e.g., `\\?\C:\foo` as `C:\foo`). This is done both
38//! internally and in external results from e.g., [`DirectoryScanner`].
39//!
40//! This feature is enabled by default — and highly recommended — because path canonicalization on Windows very commonly returns UNC paths.
41//! `dunce` only has an effect when compiling for Windows targets.
42//!    </td>
43//!   </tr>
44//!  </tbody>
45//! </table>
46//!    
47//!
48//! <table>
49//!  <thead style="background-color: rgba(0, 0, 0, 0.18)">
50//!   <tr>
51//!    <th style="text-align:left">
52//!
53//! **`fs-err`**
54//! <span style="font-weight: normal">&nbsp;(disabled by default)</span>
55//!    </th>
56//!   </tr>
57//!  </thead>
58//!  <tbody>
59//!   <tr>
60//!    <td>
61//!
62//! Enables the optional support for [`fs-err`](https://docs.rs/fs-err) which provides more helpful
63//! error messages for underlying IO errors. It should be noted that `fs-more` does already provide plenty
64//! of context on errors by itself, which is why this is disabled by default.
65//!    </td>
66//!   </tr>
67//!  </tbody>
68//! </table>
69//!
70//!
71//! <br>
72//!
73//! # Examples
74//!
75//! Copying a file and getting updates on the progress:
76//! ```no_run
77//! # use std::path::Path;
78//! # use fs_more::error::FileError;
79//! # use fs_more::file::FileCopyWithProgressOptions;
80//! # use fs_more::file::FileCopyFinished;
81//! # use fs_more::file::CollidingFileBehaviour;
82//! # fn main() -> Result<(), FileError> {
83//! let source_path = Path::new("./source-file.txt");
84//! let destination_path = Path::new("./target-file.txt");
85//!
86//! let finished_copy = fs_more::file::copy_file_with_progress(
87//!     source_path,
88//!     destination_path,
89//!     FileCopyWithProgressOptions {
90//!         colliding_file_behaviour: CollidingFileBehaviour::Abort,
91//!         ..Default::default()
92//!     },
93//!     |progress| {
94//!         let percent_copied =
95//!             (progress.bytes_finished as f64) / (progress.bytes_total as f64)
96//!             * 100.0;
97//!
98//!         println!("Copied {:.2}% of the file!", percent_copied);
99//!     }
100//! )?;
101//!
102//! match finished_copy {
103//!     FileCopyFinished::Created { bytes_copied } => {
104//!         println!("Copied {bytes_copied} bytes!");
105//!     }
106//!     FileCopyFinished::Overwritten { bytes_copied } => {
107//!         println!("Copied {bytes_copied} bytes over an existing file!");
108//!     }
109//!     // ...
110//!     _ => {}
111//! };
112//!
113//! # Ok(())
114//! # }
115//! ```
116//!
117//! <br>
118//!
119//! Moving a directory and getting updates on the progress:
120//! ```no_run
121//! # use std::path::Path;
122//! # use fs_more::error::MoveDirectoryError;
123//! # use fs_more::directory::DirectoryMoveWithProgressOptions;
124//! # use fs_more::directory::DestinationDirectoryRule;
125//! # fn main() -> Result<(), MoveDirectoryError> {
126//! let source_path = Path::new("./source-directory");
127//! let destination_path = Path::new("./target-directory");
128//!
129//! let moved = fs_more::directory::move_directory_with_progress(
130//!     source_path,
131//!     destination_path,
132//!     DirectoryMoveWithProgressOptions {
133//!         destination_directory_rule: DestinationDirectoryRule::AllowEmpty,
134//!         ..Default::default()
135//!     },
136//!     |progress| {
137//!         let percent_moved =
138//!             (progress.bytes_finished as f64) / (progress.bytes_total as f64)
139//!             * 100.0;
140//!
141//!         println!(
142//!             "Moved {:.2}% of the directory ({} files and {} directories so far).",
143//!             percent_moved,
144//!             progress.files_moved,
145//!             progress.directories_created
146//!         );
147//!     }
148//! )?;
149//!
150//! println!(
151//!     "Moved {} bytes ({} files, {} directories)! Underlying strategy: {:?}.",
152//!     moved.total_bytes_moved,
153//!     moved.files_moved,
154//!     moved.directories_moved,
155//!     moved.strategy_used
156//! );
157//! # Ok(())
158//! # }
159//! ```
160//!
161//! [`DirectoryScanner`]: crate::directory::DirectoryScanner
162
163#![warn(missing_docs)]
164
165
166/// This brings in the README.md's doctests (and is present only when testing).
167#[doc = include_str!("../README.md")]
168#[cfg(doctest)]
169pub struct ReadmeDoctests;
170
171/// This brings in the CONTRIBUTING.md's doctests (and is present only when testing).
172#[doc = include_str!("../CONTRIBUTING.md")]
173#[cfg(doctest)]
174pub struct ContributionGuideDoctests;
175
176
177
178/// Default file read buffer size, used as a default in progress tracking functions.
179/// Currently equals 64 KiB.
180///
181/// See also:
182/// - [`FileCopyWithProgressOptions`][crate::file::FileCopyWithProgressOptions]
183/// - [`FileMoveWithProgressOptions`][crate::file::FileMoveWithProgressOptions]
184/// - [`DirectoryCopyWithProgressOptions`][crate::directory::DirectoryCopyWithProgressOptions]
185/// - [`DirectoryMoveWithProgressOptions`][crate::directory::DirectoryMoveWithProgressOptions]
186const DEFAULT_READ_BUFFER_SIZE: usize = 1024 * 64;
187
188
189/// Default file write buffer size, used as a default in progress tracking functions.
190/// Currently equals 64 KiB.
191///
192/// See also:
193/// - [`FileCopyWithProgressOptions`][crate::file::FileCopyWithProgressOptions]
194/// - [`FileMoveWithProgressOptions`][crate::file::FileMoveWithProgressOptions]
195/// - [`DirectoryCopyWithProgressOptions`][crate::directory::DirectoryCopyWithProgressOptions]
196/// - [`DirectoryMoveWithProgressOptions`][crate::directory::DirectoryMoveWithProgressOptions]
197const DEFAULT_WRITE_BUFFER_SIZE: usize = 1024 * 64;
198
199
200/// Default progress reporting interval, used as a default in progress tracking functions.
201/// Currently equals 512 KiB.
202///
203/// See also:
204/// - [`FileCopyWithProgressOptions`][crate::file::FileCopyWithProgressOptions]
205/// - [`FileMoveWithProgressOptions`][crate::file::FileMoveWithProgressOptions]
206/// - [`DirectoryCopyWithProgressOptions`][crate::directory::DirectoryCopyWithProgressOptions]
207/// - [`DirectoryMoveWithProgressOptions`][crate::directory::DirectoryMoveWithProgressOptions]
208const DEFAULT_PROGRESS_UPDATE_BYTE_INTERVAL: u64 = 1024 * 512;
209
210
211#[macro_use]
212mod macros;
213
214
215pub mod directory;
216pub mod error;
217pub mod file;