use std::path::Path;
use crate::ArchiveError;
use crate::ExistingFiles;
use crate::ManifestEntryType;
use crate::OccupiedBy;
use crate::RepeatedPaths;
use crate::Result;
use crate::SinkOptions;
use crate::Violation;
use crate::formats::pinned_dir::PinnedRoot;
use crate::formats::run_record::Maker;
use crate::formats::run_record::RecordBudget;
use crate::security::fs_view::LinkKind;
use crate::security::fs_view::Occupant;
use crate::security::fs_view::normalized_display;
use crate::selection::EntryShape;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct OccupantPolicy {
existing: ExistingFiles,
repeats: RepeatedPaths,
}
impl OccupantPolicy {
pub(crate) const PRE_EXISTING: Self = Self::new(ExistingFiles::Skip, RepeatedPaths::KeepFirst);
pub(crate) const fn new(existing: ExistingFiles, repeats: RepeatedPaths) -> Self {
Self { existing, repeats }
}
pub(crate) const fn of(options: SinkOptions) -> Self {
Self::new(options.existing_files, options.repeats)
}
pub(crate) const fn repeats(self) -> RepeatedPaths {
self.repeats
}
pub(crate) const fn record_budget(self) -> RecordBudget {
match self.repeats {
RepeatedPaths::Refuse => RecordBudget::Full,
RepeatedPaths::KeepFirst => self.existing.record_budget(),
}
}
pub(crate) fn on_occupant(
self,
arriving: EntryShape,
rel: &Path,
occupant: impl FnOnce() -> std::io::Result<Option<Occupant>>,
maker: impl FnOnce() -> Maker,
site: OccupiedSite<'_>,
) -> Result<OccupantAction> {
if self.repeats == RepeatedPaths::KeepFirst {
return self.existing.on_occupant(
arriving,
rel,
occupant,
|| maker() == Maker::Run,
site,
);
}
let found = occupant()?;
if let Some(found) = found {
self.repeats
.refuse_repeat(arriving, rel, found, maker, site)?;
}
self.existing
.on_occupant(arriving, rel, || Ok(found), || false, site)
}
}
impl RepeatedPaths {
pub(crate) fn refuse_repeat(
self,
arriving: EntryShape,
rel: &Path,
found: Occupant,
maker: impl FnOnce() -> Maker,
site: OccupiedSite<'_>,
) -> Result<()> {
if self == Self::KeepFirst {
return Ok(());
}
match maker() {
Maker::Run if arriving == EntryShape::Directory && found == Occupant::Directory => {
Ok(())
}
Maker::Run => Err(repeated_path(rel, found, arriving)),
Maker::Unknown => Err(site.refuse(rel, Some(found), OccupiedBy::Existing)),
Maker::Foreign => Ok(()),
}
}
}
const fn occupant_type(occupant: Occupant) -> ManifestEntryType {
match occupant {
Occupant::File => ManifestEntryType::File,
Occupant::Directory => ManifestEntryType::Directory,
Occupant::Symlink => ManifestEntryType::Symlink,
}
}
pub(crate) const fn shape_type(shape: EntryShape) -> ManifestEntryType {
match shape {
EntryShape::Directory => ManifestEntryType::Directory,
EntryShape::File => ManifestEntryType::File,
EntryShape::Link(LinkKind::Symlink) => ManifestEntryType::Symlink,
EntryShape::Link(LinkKind::Hardlink) => ManifestEntryType::Hardlink,
}
}
pub(crate) fn repeated_path(rel: &Path, earlier: Occupant, later: EntryShape) -> ArchiveError {
repeated_path_between(rel, occupant_type(earlier), shape_type(later))
}
pub(crate) fn repeated_path_between(
rel: &Path,
earlier: ManifestEntryType,
later: ManifestEntryType,
) -> ArchiveError {
Violation::RepeatedPath {
path: normalized_display(rel).into(),
earlier,
later,
}
.into()
}
#[derive(Debug, Clone, Copy)]
pub(crate) enum OccupiedSite<'a> {
Host(&'a PinnedRoot),
Detached,
}
impl OccupiedSite<'_> {
pub(crate) fn refuse(
self,
rel: &Path,
occupant: Option<Occupant>,
by: OccupiedBy,
) -> ArchiveError {
ArchiveError::DestinationOccupied {
destination: match self {
Self::Host(root) => Some(root.destination().to_path_buf()),
Self::Detached => None,
},
conflicts: crate::OccupiedPaths::one(crate::OccupiedPath::at(rel, occupant)),
by,
}
}
}
#[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 {
fn on_occupant(
self,
arriving: EntryShape,
rel: &Path,
occupant: impl FnOnce() -> std::io::Result<Option<Occupant>>,
created_here: impl FnOnce() -> bool,
site: OccupiedSite<'_>,
) -> Result<OccupantAction> {
match self {
Self::Skip => Ok(OccupantAction::Skip),
Self::Refuse => refuse_unless_duplicate(arriving, rel, occupant()?, created_here, site)
.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,
rel: &Path,
occupant: Option<Occupant>,
created_here: impl FnOnce() -> bool,
site: OccupiedSite<'_>,
) -> 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(site.refuse(rel, occupant, by)),
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum KeptOccupant {
Directory,
Symlink,
}
impl KeptOccupant {
pub(crate) const fn occupant(self) -> Occupant {
match self {
Self::Directory => Occupant::Directory,
Self::Symlink => Occupant::Symlink,
}
}
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, by: OccupiedBy) -> ArchiveError {
Violation::ReplacesSymlink {
rel: normalized_display(rel).into(),
by,
}
.into()
}
pub(crate) fn kept_occupant_refusal(
kept: KeptOccupant,
rel: &Path,
created_here: impl FnOnce() -> bool,
site: OccupiedSite<'_>,
) -> ArchiveError {
let occupant = kept.occupant();
let by = occupied_by(Some(occupant), created_here);
match kept {
KeptOccupant::Symlink => replaces_symlink(rel, by),
KeptOccupant::Directory => site.refuse(rel, Some(occupant), by),
}
}
pub(crate) fn tree_occupant_refusal(
arriving: EntryShape,
occupant: Occupant,
rel: &Path,
) -> Option<ArchiveError> {
let by = OccupiedBy::Archive;
let arriving_directory = arriving == EntryShape::Directory;
match occupant {
Occupant::Symlink if arriving_directory => Some(replaces_symlink(rel, by)),
Occupant::File if arriving_directory => {
Some(OccupiedSite::Detached.refuse(rel, Some(occupant), by))
}
Occupant::Directory if !arriving_directory => {
Some(OccupiedSite::Detached.refuse(rel, Some(occupant), by))
}
Occupant::File | Occupant::Symlink | Occupant::Directory => None,
}
}
pub(crate) fn refuse_hardlink_replace(
_permit: ReplacePermit,
found: Option<Occupant>,
rel: &Path,
created_here: impl FnOnce() -> bool,
site: OccupiedSite<'_>,
) -> ArchiveError {
match found.and_then(KeptOccupant::of) {
Some(kept) => kept_occupant_refusal(kept, rel, created_here, site),
None => site.refuse(rel, found, 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::OccupantPolicy;
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::RepeatedPaths;
use crate::formats::run_record::RecordBudget;
use crate::security::fs_view::LinkKind;
use crate::security::fs_view::Occupant;
use crate::selection::EntryShape;
#[test]
fn refuse_repeats_keep_the_full_record_under_every_existing_files_mode() {
for existing in [
ExistingFiles::Skip,
ExistingFiles::Refuse,
ExistingFiles::Overwrite,
] {
assert_eq!(
OccupantPolicy::new(existing, RepeatedPaths::Refuse).record_budget(),
RecordBudget::Full,
"{existing:?}"
);
assert_eq!(
OccupantPolicy::new(existing, RepeatedPaths::KeepFirst).record_budget(),
existing.record_budget(),
"{existing:?}"
);
}
}
use super::OccupiedSite;
use crate::Violation;
use crate::formats::run_record::Maker;
const MODES: [ExistingFiles; 3] = [
ExistingFiles::Skip,
ExistingFiles::Refuse,
ExistingFiles::Overwrite,
];
fn decide(
existing: ExistingFiles,
repeats: RepeatedPaths,
arriving: EntryShape,
found: Occupant,
maker: Maker,
) -> Result<OccupantAction, ArchiveError> {
OccupantPolicy::new(existing, repeats).on_occupant(
arriving,
Path::new("x"),
|| Ok(Some(found)),
|| maker,
OccupiedSite::Detached,
)
}
#[test]
fn refused_repeats_refuse_an_occupant_of_the_run_under_every_mode() {
for existing in MODES {
for found in [Occupant::File, Occupant::Symlink, Occupant::Directory] {
let outcome = decide(existing, RepeatedPaths::Refuse, FILE, found, Maker::Run);
assert_matches!(
outcome,
Err(ArchiveError::SecurityViolation {
kind: Violation::RepeatedPath { .. },
..
}),
"{existing:?} {found:?}"
);
}
}
}
#[test]
fn refused_repeats_fail_closed_when_the_maker_is_unknown_under_every_mode() {
for existing in MODES {
let outcome = decide(
existing,
RepeatedPaths::Refuse,
FILE,
Occupant::File,
Maker::Unknown,
);
assert_matches!(
outcome,
Err(ArchiveError::DestinationOccupied {
by: OccupiedBy::Existing,
..
}),
"{existing:?}"
);
}
}
#[test]
fn refused_repeats_leave_a_foreign_occupant_to_the_existing_files_policy() {
let foreign = |existing| {
decide(
existing,
RepeatedPaths::Refuse,
FILE,
Occupant::File,
Maker::Foreign,
)
};
assert_matches!(foreign(ExistingFiles::Skip), Ok(OccupantAction::Skip));
assert_matches!(
foreign(ExistingFiles::Overwrite),
Ok(OccupantAction::Replace(_))
);
assert_matches!(
foreign(ExistingFiles::Refuse),
Err(ArchiveError::DestinationOccupied {
by: OccupiedBy::Existing,
..
})
);
}
#[test]
fn a_directory_over_a_directory_of_the_run_is_no_repeat() {
let policy = OccupantPolicy::new(ExistingFiles::Skip, RepeatedPaths::Refuse);
let check = |arriving, found| {
policy
.repeats()
.refuse_repeat(arriving, x(), found, || Maker::Run, DETACHED)
};
assert!(check(EntryShape::Directory, Occupant::Directory).is_ok());
assert!(check(EntryShape::Directory, Occupant::File).is_err());
assert!(check(EntryShape::Directory, Occupant::Symlink).is_err());
assert!(check(FILE, Occupant::Directory).is_err());
}
#[test]
fn kept_repeats_ignore_the_maker_and_decide_as_existing_files_does() {
for existing in MODES {
for maker in [Maker::Run, Maker::Foreign, Maker::Unknown] {
let kept = decide(
existing,
RepeatedPaths::KeepFirst,
FILE,
Occupant::File,
maker,
);
let direct = existing.on_occupant(
FILE,
x(),
|| Ok(Some(Occupant::File)),
|| maker == Maker::Run,
DETACHED,
);
assert_eq!(
std::mem::discriminant(&kept.map_err(|e| e.to_string())),
std::mem::discriminant(&direct.map_err(|e| e.to_string())),
"{existing:?} {maker:?}"
);
}
}
}
#[test]
fn refused_repeats_name_both_kinds_and_the_path() {
let outcome = decide(
ExistingFiles::Skip,
RepeatedPaths::Refuse,
EntryShape::Link(LinkKind::Hardlink),
Occupant::Symlink,
Maker::Run,
);
assert_matches!(
outcome,
Err(ArchiveError::SecurityViolation {
kind: Violation::RepeatedPath { path, earlier, later },
..
}) if &*path == "x"
&& earlier == crate::ManifestEntryType::Symlink
&& later == crate::ManifestEntryType::Hardlink
);
}
const FILE: EntryShape = EntryShape::File;
const DETACHED: OccupiedSite<'static> = OccupiedSite::Detached;
fn x() -> &'static Path {
Path::new("x")
}
#[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, x(), occupant, created_here, DETACHED);
let overwrite =
ExistingFiles::Overwrite.on_occupant(FILE, x(), occupant, created_here, DETACHED);
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, x(), found, || true, DETACHED);
let foreign = ExistingFiles::Refuse.on_occupant(FILE, x(), found, || false, DETACHED);
assert_matches!(duplicate, Ok(OccupantAction::Skip));
assert_matches!(foreign, Err(ArchiveError::DestinationOccupied { .. }));
}
#[test]
fn refuse_refuses_a_run_directory_whatever_the_record_says() {
for made_here in [true, false] {
let outcome = ExistingFiles::Refuse.on_occupant(
FILE,
x(),
|| Ok(Some(Occupant::Directory)),
|| made_here,
DETACHED,
);
assert_matches!(outcome, Err(ArchiveError::DestinationOccupied { .. }));
}
}
#[test]
fn refuse_refuses_a_vanished_occupant_and_propagates_a_probe_error() {
let vanished = ExistingFiles::Refuse.on_occupant(FILE, x(), || Ok(None), || true, DETACHED);
let failed = ExistingFiles::Refuse.on_occupant(
FILE,
x(),
|| Err(std::io::Error::from(std::io::ErrorKind::PermissionDenied)),
|| true,
DETACHED,
);
assert_matches!(vanished, Err(ArchiveError::DestinationOccupied { .. }));
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 dir = || Ok(Some(Occupant::Directory));
let made = ExistingFiles::Refuse.on_occupant(FILE, x(), dir, || true, DETACHED);
let foreign = ExistingFiles::Refuse.on_occupant(FILE, x(), dir, || false, DETACHED);
let vanished = ExistingFiles::Refuse.on_occupant(FILE, x(), || Ok(None), || true, DETACHED);
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 made =
kept_occupant_refusal(KeptOccupant::Directory, Path::new("d"), || true, DETACHED);
let foreign =
kept_occupant_refusal(KeptOccupant::Directory, Path::new("d"), || false, DETACHED);
let link =
kept_occupant_refusal(KeptOccupant::Symlink, Path::new("a//l"), || true, DETACHED);
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,
DETACHED,
)
};
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 the_tree_refuses_a_directory_over_a_file_or_symlink_and_a_file_over_a_directory() {
use super::tree_occupant_refusal;
let refuse = |arriving, occupant| tree_occupant_refusal(arriving, occupant, Path::new("p"));
assert_matches!(
refuse(EntryShape::Directory, Occupant::File),
Some(ArchiveError::DestinationOccupied { by: OccupiedBy::Archive, conflicts, .. })
if conflicts.iter().map(|c| c.occupant).eq([Some(Occupant::File)])
);
assert_matches!(
refuse(FILE, Occupant::Directory),
Some(ArchiveError::DestinationOccupied { by: OccupiedBy::Archive, conflicts, .. })
if conflicts.iter().map(|c| c.occupant).eq([Some(Occupant::Directory)])
);
assert_matches!(
refuse(EntryShape::Directory, Occupant::Symlink),
Some(ArchiveError::SecurityViolation {
kind: crate::Violation::ReplacesSymlink {
by: OccupiedBy::Archive,
..
}
})
);
assert_matches!(refuse(EntryShape::Directory, Occupant::Directory), None);
assert_matches!(refuse(FILE, Occupant::File), None);
}
#[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:?}");
}
}
}