use std::path::Path;
use crate::ArchiveError;
use crate::ExistingFiles;
use crate::OccupiedBy;
use crate::Result;
use crate::Violation;
use crate::security::fs_view::Occupant;
use crate::security::fs_view::normalized_display;
use crate::selection::EntryShape;
#[derive(Debug)]
pub(crate) struct ReplacePermit(());
impl ReplacePermit {
#[cfg(test)]
pub(crate) const fn for_test() -> Self {
Self(())
}
}
#[derive(Debug)]
pub(crate) enum OccupantAction {
Skip,
Replace(ReplacePermit),
}
pub(crate) const fn is_run_duplicate(arriving: EntryShape, occupant: Occupant) -> bool {
matches!(
(arriving, occupant),
(
EntryShape::File | EntryShape::Link(_),
Occupant::File | Occupant::Symlink
)
)
}
impl ExistingFiles {
pub(crate) fn on_occupant(
self,
arriving: EntryShape,
occupant: impl FnOnce() -> std::io::Result<Option<Occupant>>,
created_here: impl FnOnce() -> bool,
refusal: impl FnOnce() -> ArchiveError,
) -> Result<OccupantAction> {
match self {
Self::Skip => Ok(OccupantAction::Skip),
Self::Refuse => refuse_unless_duplicate(arriving, occupant()?, created_here, refusal)
.map(|RunDuplicate(())| OccupantAction::Skip),
Self::Overwrite => Ok(OccupantAction::Replace(ReplacePermit(()))),
}
}
}
#[derive(Debug)]
pub(crate) struct RunDuplicate(());
pub(crate) fn occupied_by(
found: Option<Occupant>,
created_here: impl FnOnce() -> bool,
) -> OccupiedBy {
if found.is_some() && created_here() {
OccupiedBy::Archive
} else {
OccupiedBy::Existing
}
}
pub(crate) fn refuse_unless_duplicate(
arriving: EntryShape,
occupant: Option<Occupant>,
created_here: impl FnOnce() -> bool,
refusal: impl FnOnce() -> ArchiveError,
) -> Result<RunDuplicate> {
let by = occupied_by(occupant, created_here);
match occupant {
Some(found) if by == OccupiedBy::Archive && is_run_duplicate(arriving, found) => {
Ok(RunDuplicate(()))
}
_ => Err(refusal().occupied_by(by)),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum KeptOccupant {
Directory,
Symlink,
}
impl KeptOccupant {
pub(crate) const fn of(occupant: Occupant) -> Option<Self> {
match occupant {
Occupant::Directory => Some(Self::Directory),
Occupant::Symlink => Some(Self::Symlink),
Occupant::File => None,
}
}
}
pub(crate) fn replaces_symlink(rel: &Path) -> ArchiveError {
Violation::ReplacesSymlink {
rel: normalized_display(rel).into(),
}
.into()
}
pub(crate) fn kept_occupant_refusal(
kept: KeptOccupant,
rel: &Path,
created_here: impl FnOnce() -> bool,
occupied: impl FnOnce() -> ArchiveError,
) -> ArchiveError {
match kept {
KeptOccupant::Symlink => replaces_symlink(rel),
KeptOccupant::Directory => {
occupied().occupied_by(occupied_by(Some(Occupant::Directory), created_here))
}
}
}
pub(crate) fn refuse_hardlink_replace(
_permit: ReplacePermit,
found: Option<Occupant>,
rel: &Path,
created_here: impl FnOnce() -> bool,
occupied: impl FnOnce() -> ArchiveError,
) -> ArchiveError {
match found.and_then(KeptOccupant::of) {
Some(kept) => kept_occupant_refusal(kept, rel, created_here, occupied),
None => occupied().occupied_by(occupied_by(found, created_here)),
}
}
#[cfg(test)]
mod tests {
use std::assert_matches;
use std::cell::Cell;
use std::path::Path;
use super::KeptOccupant;
use super::OccupantAction;
use super::ReplacePermit;
use super::is_run_duplicate;
use super::kept_occupant_refusal;
use super::refuse_hardlink_replace;
use crate::ArchiveError;
use crate::ExistingFiles;
use crate::OccupiedBy;
use crate::security::fs_view::LinkKind;
use crate::security::fs_view::Occupant;
use crate::selection::EntryShape;
fn refusal() -> ArchiveError {
ArchiveError::Cancelled
}
const FILE: EntryShape = EntryShape::File;
#[test]
fn skip_and_overwrite_never_consult_the_occupant_or_the_run_record() {
let asked = Cell::new(false);
let occupant = || {
asked.set(true);
Ok(Some(Occupant::File))
};
let created_here = || {
asked.set(true);
true
};
let skip = ExistingFiles::Skip.on_occupant(FILE, occupant, created_here, refusal);
let overwrite = ExistingFiles::Overwrite.on_occupant(FILE, occupant, created_here, refusal);
assert_matches!(skip, Ok(OccupantAction::Skip));
assert_matches!(overwrite, Ok(OccupantAction::Replace(_)));
assert!(!asked.get());
}
#[test]
fn refuse_skips_a_duplicate_of_the_run_and_refuses_a_foreign_occupant() {
let found = || Ok(Some(Occupant::File));
let duplicate = ExistingFiles::Refuse.on_occupant(FILE, found, || true, refusal);
let foreign = ExistingFiles::Refuse.on_occupant(FILE, found, || false, refusal);
assert_matches!(duplicate, Ok(OccupantAction::Skip));
assert_matches!(foreign, Err(ArchiveError::Cancelled));
}
#[test]
fn refuse_refuses_a_run_directory_whatever_the_record_says() {
for made_here in [true, false] {
let outcome = ExistingFiles::Refuse.on_occupant(
FILE,
|| Ok(Some(Occupant::Directory)),
|| made_here,
refusal,
);
assert_matches!(outcome, Err(ArchiveError::Cancelled));
}
}
#[test]
fn refuse_refuses_a_vanished_occupant_and_propagates_a_probe_error() {
let vanished = ExistingFiles::Refuse.on_occupant(FILE, || Ok(None), || true, refusal);
let failed = ExistingFiles::Refuse.on_occupant(
FILE,
|| Err(std::io::Error::from(std::io::ErrorKind::PermissionDenied)),
|| true,
refusal,
);
assert_matches!(vanished, Err(ArchiveError::Cancelled));
assert_matches!(failed, Err(ArchiveError::Io(_)));
}
#[test]
fn a_refusal_says_whether_the_run_made_the_occupant() {
let by = |outcome: Result<OccupantAction, ArchiveError>| {
let Err(ArchiveError::DestinationOccupied { by, .. }) = outcome else {
panic!("expected a refusal: {outcome:?}");
};
by
};
let occupied =
|| crate::formats::common::destination_occupied(Path::new("d"), Path::new("x"));
let dir = || Ok(Some(Occupant::Directory));
let made = ExistingFiles::Refuse.on_occupant(FILE, dir, || true, occupied);
let foreign = ExistingFiles::Refuse.on_occupant(FILE, dir, || false, occupied);
let vanished = ExistingFiles::Refuse.on_occupant(FILE, || Ok(None), || true, occupied);
assert_eq!(by(made), OccupiedBy::Archive);
assert_eq!(by(foreign), OccupiedBy::Existing);
assert_eq!(by(vanished), OccupiedBy::Existing);
}
#[test]
fn a_kept_directory_is_a_typed_conflict_and_a_kept_symlink_a_violation() {
let occupied = || crate::formats::common::occupied_without_destination(Path::new("d"));
let made =
kept_occupant_refusal(KeptOccupant::Directory, Path::new("d"), || true, occupied);
let foreign =
kept_occupant_refusal(KeptOccupant::Directory, Path::new("d"), || false, occupied);
let link =
kept_occupant_refusal(KeptOccupant::Symlink, Path::new("a//l"), || true, occupied);
assert_matches!(
made,
ArchiveError::DestinationOccupied {
by: OccupiedBy::Archive,
..
}
);
assert_matches!(
foreign,
ArchiveError::DestinationOccupied {
by: OccupiedBy::Existing,
..
}
);
assert_matches!(
link,
ArchiveError::SecurityViolation {
kind: crate::Violation::ReplacesSymlink { rel },
..
} if &*rel == "a/l"
);
}
#[test]
fn a_hardlink_over_a_kept_occupant_gets_the_kept_refusal() {
let refusal = |found| {
refuse_hardlink_replace(
ReplacePermit::for_test(),
found,
Path::new("h"),
|| true,
|| crate::formats::common::occupied_without_destination(Path::new("h")),
)
};
assert_matches!(
refusal(Some(Occupant::Directory)),
ArchiveError::DestinationOccupied {
by: OccupiedBy::Archive,
..
}
);
assert_matches!(
refusal(Some(Occupant::Symlink)),
ArchiveError::SecurityViolation { .. }
);
assert_matches!(
refusal(Some(Occupant::File)),
ArchiveError::DestinationOccupied {
by: OccupiedBy::Archive,
..
}
);
assert_matches!(
refusal(None),
ArchiveError::DestinationOccupied {
by: OccupiedBy::Existing,
..
}
);
}
#[test]
fn only_a_file_or_link_over_a_file_or_symlink_is_a_duplicate() {
let shapes = [
(EntryShape::Directory, false, false),
(EntryShape::File, true, true),
(EntryShape::Link(LinkKind::Symlink), true, true),
(EntryShape::Link(LinkKind::Hardlink), true, true),
];
for (shape, over_file, over_symlink) in shapes {
assert_eq!(
is_run_duplicate(shape, Occupant::File),
over_file,
"{shape:?}"
);
assert_eq!(
is_run_duplicate(shape, Occupant::Symlink),
over_symlink,
"{shape:?}"
);
assert!(!is_run_duplicate(shape, Occupant::Directory), "{shape:?}");
}
}
}