fs-more 0.8.1

Convenient file and directory operations with progress reporting built on top of std::fs.
Documentation
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
use std::path::PathBuf;

use thiserror::Error;

use super::FileError;
use crate::directory::DestinationDirectoryRule;


/// Source directory path validation error.
#[derive(Error, Debug)]
pub enum SourceDirectoryPathValidationError {
    /// The source directory (path to the directory you want to copy)
    /// does not exist.
    #[error(
        "source directory path does not exist: {}",
        .directory_path.display()
    )]
    NotFound {
        /// Source directory path.
        directory_path: PathBuf,
    },

    /// The source path (path to the directory you want to copy)
    /// exists, but does not point to a directory.
    #[error(
        "source path exists, but is not a directory: {}",
         .path.display()
    )]
    NotADirectory {
        /// The base source path that was supposed to be a directory.
        path: PathBuf,
    },

    /// The source directory could not be read, or its path could not be canonicalized.
    ///
    /// Among other things, this can happen due to missing read permissions.
    ///
    /// The inner [`std::io::Error`] will likely describe a more precise cause of this error.
    #[error("unable to access source directory: {}", .directory_path.display())]
    UnableToAccess {
        /// The exact path we are unable to access.
        directory_path: PathBuf,

        /// IO error describing why the source directory could not be accessed.
        #[source]
        error: std::io::Error,
    },
}



/// Destination directory path validation error.
#[derive(Error, Debug)]
pub enum DestinationDirectoryPathValidationError {
    /// The base source path (path to the directory you want to copy)
    /// exists, but does not point to a directory.
    #[error(
        "destination path exists, but is not a directory: {}",
         .directory_path.display()
    )]
    NotADirectory {
        /// Destination directory path.
        directory_path: PathBuf,
    },

    /// The destination directory could not be read, or its path could not be canonicalized.
    ///
    /// Among other things, this can happen due to missing read permissions.
    ///
    /// The inner [`std::io::Error`] will likely describe a more precise cause of this error.
    #[error("unable to access destination directory: {}", .directory_path.display())]
    UnableToAccess {
        /// The exact path we were unable to access.
        directory_path: PathBuf,

        /// IO error describing why the source directory could not be accessed.
        #[source]
        error: std::io::Error,
    },

    /// A destination directory or a file inside it already exists,
    /// which is against the provided [`DestinationDirectoryRule`].
    #[error(
        "destination path already exists, which is against \
        the configured destination directory rule ({:?}): {}",
        .destination_directory_rule,
        .path.display()
    )]
    AlreadyExists {
        /// Path to the file or directory that should not exist based on the provided rules.
        path: PathBuf,

        /// Destination directory rule that made the existing destination
        /// directory invalid (see [`DestinationDirectoryRule::DisallowExisting`]).
        destination_directory_rule: DestinationDirectoryRule,
    },

    /// A destination directory or a file inside it exists and is not empty,
    /// which is against the provided [`DestinationDirectoryRule`].
    #[error(
        "destination directory exists and is not empty, which is against \
        the configured destination directory rule ({:?}): {}",
        .destination_directory_rule,
        .directory_path.display(),
    )]
    NotEmpty {
        /// Path to the destination directory that should be empty based on the provided rules.
        directory_path: PathBuf,

        /// Destination directory rule that made the existing destination
        /// directory invalid (see [`DestinationDirectoryRule::AllowEmpty`]).
        destination_directory_rule: DestinationDirectoryRule,
    },

    /// The destination directory path equals or points inside the source directory,
    /// which is very problematic for copies or moves.
    #[error(
        "destination directory path equals or points inside the source directory, \
        which is invalid: {} (but source path is {})",
        .destination_directory_path.display(),
        .source_directory_path.display()
    )]
    DescendantOfSourceDirectory {
        /// Invalid destination directory path.
        destination_directory_path: PathBuf,

        /// Corresponding source directory path.
        source_directory_path: PathBuf,
    },
}



/// Directory copy or move planning error.
#[derive(Error, Debug)]
pub enum DirectoryExecutionPlanError {
    /// A source or destination directory, one of its sub-directories or a file
    /// in it (or its metadata) cannot be read.
    ///
    /// For example, this can happen due to missing read permissions.
    ///
    /// The inner [`std::io::Error`] will likely describe a more precise cause of this error.
    #[error("unable to access path: {}", .path.display())]
    UnableToAccess {
        /// The path we were unable to access.
        path: PathBuf,

        /// IO error describing why the path could not be accessed.
        #[source]
        error: std::io::Error,
    },

    /// An item inside the source directory "escaped" outside of
    /// the base source directory.
    ///
    /// # Implementation detail
    /// This is an extremely unlikely error, because its requirement
    /// is that [`std::fs::read_dir`]'s iterator returns a directory entry
    /// outside the provided directory path.
    ///
    /// Even though this seems extremely unlikely, a `panic!` would be
    /// an extreme measure due to the many types of filesystems that exist.
    /// Instead, treat this as a truly fatal error.
    #[error(
        "a directory entry inside the source directory escaped out of it: {}",
        .path.display()
    )]
    EntryEscapesSourceDirectory {
        /// The path that "escaped" the source directory.
        path: PathBuf,
    },

    /// A destination directory or a file inside it already exists,
    /// which is against the configured [`DestinationDirectoryRule`].
    ///
    /// This can also happen when we intended to copy a file to the destination,
    /// but a directory with the same name appeared mid-copy
    /// (an unavoidable [time-of-check time-of-use](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use) bug).
    ///
    /// The `path` field contains the path that already existed, causing this error.
    #[error("destination directory or file already exists: {}", .path.display())]
    DestinationItemAlreadyExists {
        /// Path of the target directory or file that already exists.
        path: PathBuf,
    },

    /// A broken symbolic link has been encountered inside the source directory.
    ///
    /// This error can occur only when `broken_symlink_behaviour` is set to
    /// [`BrokenSymlinkBehaviour::Abort`].
    ///
    ///
    /// [`BrokenSymlinkBehaviour::Abort`]: crate::directory::BrokenSymlinkBehaviour::Abort
    #[error(
        "symbolic link inside source directory is broken, \
        and the behaviour is set to abort"
    )]
    SymbolicLinkIsBroken {
        /// Path of the broken symbolic link.
        path: PathBuf,
    },
}



/// An item inside the source directory "escaped" outside of
/// the base source directory.
///
/// # Implementation detail
/// This is an extremely unlikely error, because its requirement
/// is that [`fs::read_dir`]'s iterator returns a directory entry
/// outside the provided directory path.
///
/// Even though this seems extremely unlikely, a `panic!` would be
/// an extreme measure due to the many types of filesystems that exist.
/// Instead, treat this as a truly fatal error.
#[derive(Error, Debug)]
#[error(
    "a directory entry inside the source directory escaped out of it: {}",
    .path.display()
)]
pub(crate) struct SourceSubPathNotUnderBaseSourceDirectory {
    /// The path that "escaped" the source directory.
    pub(crate) path: PathBuf,
}



/// Directory copy preparation error.
#[derive(Error, Debug)]
pub enum CopyDirectoryPreparationError {
    /// A source directory validation error.
    #[error(transparent)]
    SourceDirectoryValidationError(#[from] SourceDirectoryPathValidationError),

    /// A destination directory validation error.
    #[error(transparent)]
    DestinationDirectoryValidationError(#[from] DestinationDirectoryPathValidationError),

    /// A directory copy planning error.
    #[error(transparent)]
    CopyPlanningError(#[from] DirectoryExecutionPlanError),
}



/// Directory copy execution error.
#[derive(Error, Debug)]
pub enum CopyDirectoryExecutionError {
    /// Failed to create a directory inside the destination folder.
    ///
    /// For example, this can happen due to missing write permissions.
    ///
    /// The inner [`std::io::Error`] will likely describe a more precise cause of this error.
    #[error("unable to create directory: {}", .directory_path.display())]
    UnableToCreateDirectory {
        /// Directory we were unable to create.
        directory_path: PathBuf,

        /// IO error describing why the directory could not be created.
        #[source]
        error: std::io::Error,
    },

    /// A file or directory inside the destination directory could not be accessed.
    #[error("unable to access destination path: {}", .path.display())]
    UnableToAccessDestination {
        /// The path we were unable to access.
        path: PathBuf,

        /// IO error describing why the directory could not be created.
        #[source]
        error: std::io::Error,
    },

    /// An error occurred while trying to copy a file to the destination.
    #[error(
        "an error occurred while copying a file to the destination: {}",
        .file_path.display(),
    )]
    FileCopyError {
        /// The file path that could not be copied.
        file_path: PathBuf,

        /// The underlying file copying error.
        #[source]
        error: FileError,
    },

    /// An error occurred while trying to create a symlink at the destination.
    #[error(
        "failed while creating a symlink at {}",
        .symlink_path.display()
    )]
    SymlinkCreationError {
        /// The path to the symbolic link that could not be created.
        symlink_path: PathBuf,

        /// The underlying symlink creation error.
        #[source]
        error: std::io::Error,
    },

    /// A destination directory, a file, or a sub-directory inside it
    /// has changed since the preparation phase of the directory copy.
    ///
    /// We can't guarantee that all destination directory changes
    /// will trigger this, but some more obvious problematic ones, like
    /// a file appearing in one of the destinations we wanted to copy to, will.
    ///
    /// This is essentially an unavoidable
    /// [time-of-check time-of-use](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use)
    /// bug, but we try to catch it if possible.
    ///
    /// The `path` field contains the path that already existed, causing this error.
    #[error("destination directory or file has been created externally mid-execution: {}", .path.display())]
    DestinationEntryUnexpected {
        /// The path of the target directory or file that already exists.
        path: PathBuf,
    },
}



/// Directory copying error (see [`copy_directory`] / [`copy_directory_with_progress`]).
///
///
/// [`copy_directory`]: crate::directory::copy_directory
/// [`copy_directory_with_progress`]: crate::directory::copy_directory_with_progress
#[derive(Error, Debug)]
pub enum CopyDirectoryError {
    /// Directory copy preparation error.
    #[error(transparent)]
    PreparationError(#[from] CopyDirectoryPreparationError),

    /// Directory copy execution error.
    #[error(transparent)]
    ExecutionError(#[from] CopyDirectoryExecutionError),
}



/// Directory move preparation error.
#[derive(Error, Debug)]
pub enum MoveDirectoryPreparationError {
    /// Source directory validation error.
    #[error(transparent)]
    SourceDirectoryValidationError(#[from] SourceDirectoryPathValidationError),

    /// Destination directory validation error.
    #[error(transparent)]
    DestinationDirectoryValidationError(#[from] DestinationDirectoryPathValidationError),

    /// Source directory entry scanning error.
    #[error(transparent)]
    DirectoryScanError(#[from] DirectoryScanError),

    /// Directory copy planning error. These errors can happen
    /// when a move-by-rename fails and a copy-and-delete is attempted instead.
    #[error(transparent)]
    CopyPlanningError(#[from] DirectoryExecutionPlanError),
}



/// Directory move execution error.
#[derive(Error, Debug)]
pub enum MoveDirectoryExecutionError {
    /// A file or directory inside the source directory could not be accessed.
    #[error("unable to access source path: {}", .path.display())]
    UnableToAccessSource {
        /// The path we were unable to access.
        path: PathBuf,

        /// IO error describing why the directory could not be created.
        #[source]
        error: std::io::Error,
    },

    /// An item inside the source directory "escaped" outside of
    /// the base source directory.
    ///
    /// # Implementation detail
    /// This is an extremely unlikely error, because its requirement
    /// is that [`std::fs::read_dir`]'s iterator returns a directory entry
    /// outside the provided directory path.
    ///
    /// Even though this seems extremely unlikely, a `panic!` would be
    /// an extreme measure due to the many types of filesystems that exist.
    /// Instead, treat this as a truly fatal error.
    #[error(
        "a directory entry inside the source directory escaped out of it: {}",
        .path.display()
    )]
    EntryEscapesSourceDirectory {
        /// The path that "escaped" the source directory.
        path: PathBuf,
    },

    /// A destination directory, a file, or a sub-directory inside it
    /// has changed since the preparation phase of the directory move call.
    ///
    /// We can't guarantee that all destination directory changes
    /// will trigger this, but some more obvious problematic ones, like
    /// a file appearing in one of the destinations we wanted to copy to, will.
    ///
    /// This is essentially an unavoidable
    /// [time-of-check time-of-use](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use)
    /// bug, but we try to catch it if possible.
    ///
    /// The `path` field contains the path that already existed, causing this error.
    #[error("destination directory or file has been created externally mid-execution: {}", .path.display())]
    DestinationEntryUnexpected {
        /// The path of the target directory or file that already exists.
        path: PathBuf,
    },

    /// Directory copy execution error.
    ///
    /// These errors can happen when a move-by-rename fails
    /// and a copy-and-delete is performed instead.
    #[error(transparent)]
    CopyDirectoryError(#[from] CopyDirectoryExecutionError),

    /// Occurs when renaming is the only enabled directory move strategy,
    /// but it fails.
    ///
    /// This commonly indicates that the source and destination directory are
    /// on different mount points, which would require copy-and-delete, and sometimes
    /// even following (instead of preserving) symbolic links.
    #[error(
        "only rename strategy is enabled (with no copy-and-delete \
        fallback strategy), but we were unable to rename the directory"
    )]
    RenameFailedAndNoFallbackStrategy,

    /// An uncategorized unrecoverable IO error.
    /// See `error` field for more information.
    #[error("uncategorized std::io::Error")]
    OtherIoError {
        /// IO error describing the cause of the outer error.
        #[source]
        error: std::io::Error,
    },
}



/// Directory moving error (see [`move_directory`] / [`move_directory_with_progress`]).
///
///
/// [`move_directory`]: crate::directory::move_directory
/// [`move_directory_with_progress`]: crate::directory::move_directory_with_progress
#[derive(Error, Debug)]
pub enum MoveDirectoryError {
    /// Directory move preparation error.
    #[error(transparent)]
    PreparationError(#[from] MoveDirectoryPreparationError),

    /// Directory move execution error.
    #[error(transparent)]
    ExecutionError(#[from] MoveDirectoryExecutionError),
}



/// An error that can occur when querying the size of a directory
/// (see [`directory_size_in_bytes`]).
///
///
/// [`directory_size_in_bytes`]: crate::directory::directory_size_in_bytes
#[derive(Error, Debug)]
pub enum DirectorySizeScanError {
    /// An error occurred while scanning the directory.
    #[error("failed while scanning directory: {}", .directory_path.display())]
    ScanError {
        /// The scanning error.
        #[source]
        error: DirectoryScanError,

        /// Base directory path for the scan.
        directory_path: PathBuf,
    },
}



/// An error that can occur when checking whether a directory is empty
/// (see [`is_directory_empty`]).
///
///
/// [`is_directory_empty`]: crate::directory::is_directory_empty
#[derive(Error, Debug)]
pub enum DirectoryEmptinessScanError {
    /// The provided directory path to scan doesn't exist.
    #[error("path doesn't exist: {}", .path.display())]
    NotFound {
        /// The directory path that couldn't be scanned.
        path: PathBuf,
    },

    /// The provided directory path exists, but is not a directory.
    #[error(
        "path exists, but is not a directory nor a symlink to one: {}",
        .path.display()
    )]
    NotADirectory {
        /// The directory path that couldn't be scanned.
        path: PathBuf,
    },

    /// The provided directory path is a directory,
    /// but could not be read due to an IO error.
    ///
    /// The inner [`std::io::Error`] will likely describe a more precise cause of this error.
    #[error("unable to read directory: {}", .directory_path.display())]
    UnableToReadDirectory {
        /// The directory path that could not be read.
        directory_path: PathBuf,

        /// IO error describing why the given root directory could not be read.
        #[source]
        error: std::io::Error,
    },

    /// A directory contains an entry (i.e. directory or file)
    /// that could not be read due to an IO error.
    ///
    /// The inner [`std::io::Error`] will likely describe a more precise cause of this error.
    #[error("unable to read directory entry for {}", .directory_path.display())]
    UnableToReadDirectoryEntry {
        /// The directory path whose entries could not be read.
        directory_path: PathBuf,

        /// IO error describing why the given file or directory could not be read.
        #[source]
        error: std::io::Error,
    },
}




/// An error that can occur when scanning a directory.
#[derive(Error, Debug)]
pub enum DirectoryScanError {
    /// The provided directory path to scan doesn't exist.
    #[error("path doesn't exist: {}", .path.display())]
    NotFound {
        /// The directory path that couldn't be scanned.
        path: PathBuf,
    },

    /// The provided directory path exists, but is not a directory.
    #[error(
        "path exists, but is not a directory nor a symlink to one: {}",
        .path.display()
    )]
    NotADirectory {
        /// The directory path that couldn't be scanned.
        path: PathBuf,
    },

    /// The provided directory path is a directory,
    /// but could not be read due to an IO error.
    ///
    /// The inner [`std::io::Error`] will likely describe a more precise cause of this error.
    #[error("unable to read directory: {}", .directory_path.display())]
    UnableToReadDirectory {
        /// The drectory path that could not be read.
        directory_path: PathBuf,

        /// IO error describing why the given root directory could not be read.
        #[source]
        error: std::io::Error,
    },

    /// A directory contains an entry (i.e. directory or file)
    /// that could not be read due to an IO error.
    ///
    /// The inner [`std::io::Error`] will likely describe a more precise cause of this error.
    #[error("unable to read directory entry for {}", .directory_path.display())]
    UnableToReadDirectoryEntry {
        /// The directory path whose entries could not be read.
        directory_path: PathBuf,

        /// IO error describing why the given file or directory could not be read.
        #[source]
        error: std::io::Error,
    },

    /// A symlink inside the scan tree is cyclical.
    #[error("encountered a directory symlink cycle at {}", .directory_path.display())]
    SymlinkCycleEncountered {
        /// The directory path at which the cycle loops around (i.e. where the cycle was detected).
        directory_path: PathBuf,
    },
}