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 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699
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,
},
}
/// 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),
/// Directory copy or move 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 {
/// 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 {
/// File path that could not be copied.
file_path: PathBuf,
/// The underlying file copying error.
#[source]
error: FileError,
},
/// A destination directory, a file or a sub-directory inside it
/// has changed since the preparation phase of the directory copy 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.
///
/// 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 {
/// Path of the target directory or file that already exists.
path: PathBuf,
},
}
/// Directory copying error, see [`copy_directory`].
///
///
/// [`copy_directory`]: [crate::directory::copy_directory]
#[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),
/// Source directory size scanning error.
#[error(transparent)]
DirectorySizeScanError(#[from] DirectorySizeScanError),
/// 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 {
/// 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.
///
/// 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 {
/// 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),
/// An uncategorized unrecoverable IO error. See `error` 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_with_progress`].
///
/// [`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 copying or moving a directory.
#[derive(Error, Debug)]
pub enum DirectoryError {
/// The base source directory (i.e. the directory you want to copy from) does not exist.
#[error(
"source directory path does not exist: {}",
.directory_path.display()
)]
SourceDirectoryNotFound {
/// Source directory path.
directory_path: PathBuf,
},
/// The base source directory path (i.e. the directory you want to copy from) exists,
/// but does not point to a directory.
#[error(
"source directory path exists, but is not a directory: {}",
.directory_path.display()
)]
SourceDirectoryNotADirectory {
/// Source directory path.
directory_path: PathBuf,
},
/// A base source directory, its sub-directory or a file inside it cannot be read.
///
/// For example, this can happen due to missing permissions,
/// files or directories being removed externally mid-copy or mid-move, etc.
///
/// The inner [`std::io::Error`] will likely describe a more precise cause of this error.
#[error("unable to access source directory or file: {}", .path.display())]
UnableToAccessSource {
/// The path we are unable to access.
path: PathBuf,
/// IO error describing why the source directory could not be accessed.
#[source]
error: std::io::Error,
},
/// A base source directory, its sub-directory or a file inside it
/// no longer exists (since being first scanned when preparing for a copy, move etc.).
///
/// This is basically a [TOCTOU](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use)
/// race condition.
#[error(
"directory or file inside the source directory has been \
unexpectedly removed while processing: {}",
.path.display()
)]
SourceEntryNoLongerExists {
/// The path to a directory or file that is invalid.
path: PathBuf,
},
/// The provided destination directory path points to an invalid location.
///
/// This can occur due to (not an exhaustive list):
/// - source and destination directory are the same,
/// - destination directory is a subdirectory of the source directory, or,
/// - destination path already exists, but is not a directory.
#[error("destination directory path points to an invalid location: {}", .path.display())]
InvalidDestinationDirectoryPath {
/// Invalid destination path.
path: PathBuf,
},
/// The file system state of the destination directory does not match
/// the provided [`DestinationDirectoryRule`].
///
/// For example, this happens when the the destination directory rule is set to
/// [`DestinationDirectoryRule::AllowEmpty`], but the destination directory isn't actually empty.
#[error(
"destination directory is not empty, but configured rules ({:?}) require so: {}",
destination_directory_rule,
.destination_path.display()
)]
DestinationDirectoryNotEmpty {
/// Destination directory path.
destination_path: PathBuf,
/// Requirements for the destination directory
/// (e.g. it should be empty or it should not exist at all).
destination_directory_rule: DestinationDirectoryRule,
},
/// A destination directory or a file inside it cannot be created
/// or written to (e.g. due to missing permissions).
///
/// The inner [`std::io::Error`] will likely describe a more precise cause of this error.
#[error("unable to access destination directory or file: {}", .path.display())]
UnableToAccessDestination {
/// Path that cannot be accessed.
path: PathBuf,
/// IO error describing why the target directory could not be accessed.
#[source]
error: std::io::Error,
},
/// A destination directory or a file inside it already exists.
///
/// 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,
},
/// An item inside the source directory somehow escaped outside
/// the base source directory.
#[error(
"a sub-path inside the source directory escaped out of it: {}",
.path.display()
)]
SourceSubPathEscapesSourceDirectory {
/// The related path that "escaped" the source directory.
path: PathBuf,
},
/// An uncategorized unrecoverable IO error. See `error` for more information.
#[error("uncategorized std::io::Error")]
OtherIoError {
/// IO error describing the cause of the outer error.
#[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 {
/// 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())]
UnableToReadDirectoryItem {
/// 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 querying size of a scanned directory.
#[derive(Error, Debug)]
pub enum DirectorySizeScanError {
/// The provided directory path does not exist.
#[error("the provided scan directory path doesn't exist: {}", .path.display())]
ScanDirectoryNotFound {
/// The directory whose scan was requested.
path: PathBuf,
},
/// The root directory path exists, but is not a directory nor a symbolic link to one.
#[error(
"the provided scan path exists, bus is not a directory \
nor a symbolic link to one: {}", .path.display()
)]
ScanDirectoryNotADirectory {
/// The path that was requested to be scanned.
path: PathBuf,
},
/// A file or directory that was scanned on initialization
/// of [`DirectoryScan`][crate::directory::DirectoryScan] is no longer there or no longer a file.
///
/// This is basically a [TOCTOU](https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use)
///
#[error("a scanned file or directory no longer exists or isn't a file anymore: {path}")]
ScanEntryNoLongerExists {
/// Path of the file or directory that no longer exists.
path: PathBuf,
},
/// A file cannot be accessed (e.g. due to missing permissions).
///
/// The inner [`std::io::Error`] will likely describe a more precise cause of this error.
#[error("unable to access file: {}", .file_path.display())]
UnableToAccessFile {
/// File path that could not be accessed.
file_path: PathBuf,
/// Underlying IO error describing why the file could not be accessed.
#[source]
error: std::io::Error,
},
/// The directory cannot be accessed (e.g. due to missing permissions).
///
/// The inner [`std::io::Error`] will likely describe a more precise cause of this error.
#[error("unable to access directory: {}", .directory_path.display())]
UnableToAccessDirectory {
/// Directory path that could not be accessed.
directory_path: PathBuf,
/// Underlying IO error describing why the directory could not be accessed.
#[source]
error: std::io::Error,
},
/// Some other [`std::io::Error`] was encountered.
#[error("other std::io::Error")]
OtherIoError {
/// IO error describing the cause of the outer error.
#[source]
error: std::io::Error,
},
}
/// An error that can occur when checking whether a directory is empty.
#[derive(Error, Debug)]
pub enum IsDirectoryEmptyError {
/// The provided path doesn't exist.
#[error("given path does not exist: {}", .directory_path.display())]
NotFound {
/// Directory path that does not exist.
directory_path: PathBuf,
},
/// The provided path exists, but is not a directory.
#[error("given path exists, but is not a directory: {}", .path.display())]
NotADirectory {
/// Path that exists, but should have been a directory.
path: PathBuf,
},
/// Could not read the contents of a directory.
///
/// For example, this can happen due to missing permissions.
#[error("unable to read contents of directory: {}", .directory_path.display())]
UnableToReadDirectory {
/// Directory path that could not be read.
directory_path: PathBuf,
/// Underlying IO error describing why the directory could not be read.
#[source]
error: std::io::Error,
},
}