893 lines
29 KiB
Rust
893 lines
29 KiB
Rust
use std::collections::BTreeMap;
|
|
use std::ffi::OsString;
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::{Arc, Mutex};
|
|
use std::time::{Duration, UNIX_EPOCH};
|
|
|
|
use async_trait::async_trait;
|
|
use pretty_assertions::assert_eq;
|
|
use rcom::domain::{Entry, EntryKind, HostAlias, Location, SourceId};
|
|
use rcom::fs::{
|
|
BackendHandle, FileOperations, FsError, PartFile, PartWriter, ReadFileSystem, ReadPrefix,
|
|
ReadStream, StagingOperations, TransferCapabilities,
|
|
};
|
|
use rcom::operations::planner::PathValidationError;
|
|
use rcom::operations::{
|
|
CollisionDecision, CollisionPolicy, OperationKind, OperationPlanner, OperationRequest,
|
|
PlanGeneration, PlanId, PreflightError,
|
|
};
|
|
use rcom::test_support::{MemoryFs, MemoryTransferFs, TransferOperation};
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
struct WriteJournal {
|
|
calls: Arc<Mutex<Vec<&'static str>>>,
|
|
}
|
|
|
|
impl WriteJournal {
|
|
fn record(&self, call: &'static str) -> FsError {
|
|
self.calls
|
|
.lock()
|
|
.unwrap_or_else(|error| error.into_inner())
|
|
.push(call);
|
|
FsError::Unsupported("read-only test double".into())
|
|
}
|
|
|
|
fn assert_empty(&self) {
|
|
assert_eq!(
|
|
self.calls
|
|
.lock()
|
|
.unwrap_or_else(|error| error.into_inner())
|
|
.as_slice(),
|
|
&[] as &[&'static str]
|
|
);
|
|
}
|
|
}
|
|
|
|
/// Minimal FileOperations implementation: reads delegate to ReadFileSystem and every mutation is
|
|
/// journalled and rejected. It deliberately does not depend on the parallel LocalFileOperations work.
|
|
struct ReadOnlyTransfer {
|
|
source: SourceId,
|
|
read: Arc<dyn ReadFileSystem>,
|
|
writes: WriteJournal,
|
|
}
|
|
|
|
#[async_trait]
|
|
impl StagingOperations for ReadOnlyTransfer {}
|
|
|
|
#[async_trait]
|
|
impl FileOperations for ReadOnlyTransfer {
|
|
fn source_id(&self) -> &SourceId {
|
|
&self.source
|
|
}
|
|
|
|
fn capabilities(&self) -> TransferCapabilities {
|
|
TransferCapabilities {
|
|
canonicalize: true,
|
|
regular_files: true,
|
|
..TransferCapabilities::default()
|
|
}
|
|
}
|
|
|
|
async fn canonicalize(&self, location: &Location) -> Result<Location, FsError> {
|
|
self.read.canonicalize(location).await
|
|
}
|
|
|
|
async fn metadata(&self, location: &Location, follow_symlinks: bool) -> Result<Entry, FsError> {
|
|
assert!(
|
|
!follow_symlinks,
|
|
"planner must always request no-follow metadata"
|
|
);
|
|
self.read.metadata(location).await
|
|
}
|
|
|
|
async fn open_read(&self, _location: &Location) -> Result<ReadStream, FsError> {
|
|
Err(FsError::Unsupported(
|
|
"preflight must not open file contents".into(),
|
|
))
|
|
}
|
|
|
|
async fn open_part(&self, _destination: &Location) -> Result<PartWriter, FsError> {
|
|
Err(self.writes.record("open_part"))
|
|
}
|
|
|
|
async fn write_part(&self, _part: &mut PartWriter, _chunk: &[u8]) -> Result<(), FsError> {
|
|
Err(self.writes.record("write_part"))
|
|
}
|
|
|
|
async fn flush_and_close(&self, _part: PartWriter) -> Result<PartFile, FsError> {
|
|
Err(self.writes.record("flush_and_close"))
|
|
}
|
|
|
|
async fn commit_part(&self, _part: PartFile) -> Result<(), FsError> {
|
|
Err(self.writes.record("commit_part"))
|
|
}
|
|
|
|
async fn discard_part(&self, _part: PartFile) -> Result<(), FsError> {
|
|
Err(self.writes.record("discard_part"))
|
|
}
|
|
|
|
async fn rename(&self, _from: &Location, _to: &Location) -> Result<(), FsError> {
|
|
Err(self.writes.record("rename"))
|
|
}
|
|
|
|
async fn remove_file(&self, _location: &Location) -> Result<(), FsError> {
|
|
Err(self.writes.record("remove_file"))
|
|
}
|
|
|
|
async fn remove_dir(&self, _location: &Location) -> Result<(), FsError> {
|
|
Err(self.writes.record("remove_dir"))
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug)]
|
|
struct FakeFs {
|
|
source: SourceId,
|
|
nodes: BTreeMap<PathBuf, Entry>,
|
|
canonical_paths: BTreeMap<PathBuf, PathBuf>,
|
|
listings: BTreeMap<PathBuf, Vec<Entry>>,
|
|
metadata_errors: BTreeMap<PathBuf, FsError>,
|
|
}
|
|
|
|
impl FakeFs {
|
|
fn local() -> Self {
|
|
Self::new(SourceId::Local)
|
|
}
|
|
|
|
fn new(source: SourceId) -> Self {
|
|
let mut fs = Self {
|
|
source,
|
|
nodes: BTreeMap::new(),
|
|
canonical_paths: BTreeMap::new(),
|
|
listings: BTreeMap::new(),
|
|
metadata_errors: BTreeMap::new(),
|
|
};
|
|
fs.insert_entry(PathBuf::from("/"), EntryKind::Directory, None);
|
|
fs
|
|
}
|
|
|
|
fn with_dir(self, path: impl Into<PathBuf>) -> Self {
|
|
self.with_kind(path, EntryKind::Directory)
|
|
}
|
|
|
|
fn with_file(self, path: impl Into<PathBuf>, size: Option<u64>) -> Self {
|
|
let path = path.into();
|
|
self.with_kind(path.clone(), EntryKind::File)
|
|
.with_size(path, size)
|
|
}
|
|
|
|
fn with_kind(mut self, path: impl Into<PathBuf>, kind: EntryKind) -> Self {
|
|
let path = path.into();
|
|
self.insert_entry(path, kind, None);
|
|
self
|
|
}
|
|
|
|
fn with_size(mut self, path: impl Into<PathBuf>, size: Option<u64>) -> Self {
|
|
let path = path.into();
|
|
let entry = self.nodes.get_mut(&path).expect("file was inserted");
|
|
entry.size = size;
|
|
self
|
|
}
|
|
|
|
fn with_metadata_snapshot(
|
|
mut self,
|
|
path: impl Into<PathBuf>,
|
|
modified: std::time::SystemTime,
|
|
permissions: u32,
|
|
) -> Self {
|
|
let path = path.into();
|
|
let entry = self.nodes.get_mut(&path).expect("entry was inserted");
|
|
entry.modified = Some(modified);
|
|
entry.permissions = Some(permissions);
|
|
self
|
|
}
|
|
|
|
fn with_canonical_path(mut self, from: impl Into<PathBuf>, to: impl Into<PathBuf>) -> Self {
|
|
self.canonical_paths.insert(from.into(), to.into());
|
|
self
|
|
}
|
|
|
|
fn with_listing(mut self, directory: impl Into<PathBuf>, entries: Vec<Entry>) -> Self {
|
|
self.listings.insert(directory.into(), entries);
|
|
self
|
|
}
|
|
|
|
fn with_metadata_error(mut self, location: impl Into<PathBuf>, error: FsError) -> Self {
|
|
self.metadata_errors.insert(location.into(), error);
|
|
self
|
|
}
|
|
|
|
fn insert_entry(&mut self, path: PathBuf, kind: EntryKind, size: Option<u64>) {
|
|
let parents: Vec<_> = path.ancestors().skip(1).map(Path::to_path_buf).collect();
|
|
for parent in parents.into_iter().rev() {
|
|
self.nodes
|
|
.entry(parent.clone())
|
|
.or_insert_with(|| Entry::new(entry_name(&parent), EntryKind::Directory));
|
|
}
|
|
let name = entry_name(&path);
|
|
self.nodes
|
|
.insert(path, Entry::new(name, kind).with_metadata(size, None, None));
|
|
}
|
|
|
|
fn location(&self, path: impl Into<PathBuf>) -> Location {
|
|
Location {
|
|
source: self.source.clone(),
|
|
path: path.into(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl ReadFileSystem for FakeFs {
|
|
async fn list(&self, location: &Location) -> Result<Vec<Entry>, FsError> {
|
|
self.check_location(location)?;
|
|
if self.nodes.get(&location.path).map(|entry| entry.kind) != Some(EntryKind::Directory) {
|
|
return Err(FsError::NotADirectory);
|
|
}
|
|
if let Some(entries) = self.listings.get(&location.path) {
|
|
return Ok(entries.clone());
|
|
}
|
|
Ok(self
|
|
.nodes
|
|
.iter()
|
|
.filter(|(path, _)| path.parent() == Some(&location.path))
|
|
.map(|(_, entry)| entry.clone())
|
|
.collect())
|
|
}
|
|
|
|
async fn canonicalize(&self, location: &Location) -> Result<Location, FsError> {
|
|
self.check_location(location)?;
|
|
if !self.nodes.contains_key(&location.path) {
|
|
return Err(FsError::NotFound);
|
|
}
|
|
let path = self
|
|
.canonical_paths
|
|
.get(&location.path)
|
|
.cloned()
|
|
.unwrap_or_else(|| location.path.clone());
|
|
if !self.nodes.contains_key(&path) {
|
|
return Err(FsError::NotFound);
|
|
}
|
|
Ok(Location {
|
|
source: self.source.clone(),
|
|
path,
|
|
})
|
|
}
|
|
|
|
async fn metadata(&self, location: &Location) -> Result<Entry, FsError> {
|
|
self.check_location(location)?;
|
|
if let Some(error) = self.metadata_errors.get(&location.path) {
|
|
return Err(error.clone());
|
|
}
|
|
self.nodes
|
|
.get(&location.path)
|
|
.cloned()
|
|
.ok_or(FsError::NotFound)
|
|
}
|
|
|
|
async fn read_prefix(
|
|
&self,
|
|
_location: &Location,
|
|
_limit: usize,
|
|
) -> Result<ReadPrefix, FsError> {
|
|
Err(FsError::Unsupported("not needed by preflight".into()))
|
|
}
|
|
}
|
|
|
|
impl FakeFs {
|
|
fn check_location(&self, location: &Location) -> Result<(), FsError> {
|
|
if location.source != self.source {
|
|
return Err(FsError::Other("wrong fake endpoint".into()));
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
fn entry_name(path: &Path) -> OsString {
|
|
path.file_name().unwrap_or(path.as_os_str()).to_os_string()
|
|
}
|
|
|
|
fn fake_backend(fs: FakeFs) -> (Arc<BackendHandle>, WriteJournal) {
|
|
let source = fs.source.clone();
|
|
let read: Arc<dyn ReadFileSystem> = Arc::new(fs);
|
|
let writes = WriteJournal::default();
|
|
let transfer: Arc<dyn FileOperations> = Arc::new(ReadOnlyTransfer {
|
|
source: source.clone(),
|
|
read: read.clone(),
|
|
writes: writes.clone(),
|
|
});
|
|
let backend = BackendHandle::new(source, read, transfer).expect("matching fake endpoint");
|
|
(Arc::new(backend), writes)
|
|
}
|
|
|
|
fn request(sources: &[&str], destination: &str, policy: CollisionPolicy) -> OperationRequest {
|
|
OperationRequest::new(
|
|
PlanId::new(17),
|
|
PlanGeneration::new(4),
|
|
OperationKind::Copy,
|
|
sources.iter().map(|path| Location::local(*path)).collect(),
|
|
Location::local(destination),
|
|
policy,
|
|
BTreeMap::new(),
|
|
)
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn nested_sources_are_deduplicated_in_any_order_without_writes_or_prefix_confusion() {
|
|
let source_read = MemoryFs::new()
|
|
.with_file(Location::local("/a/b/file"), "abc")
|
|
.with_file(Location::local("/a/bc"), "xy");
|
|
let source_transfer = MemoryTransferFs::local()
|
|
.with_file(Location::local("/a/b/file"), b"abc")
|
|
.with_file(Location::local("/a/bc"), b"xy");
|
|
let destination_read = MemoryFs::new().with_dir(Location::local("/dest"));
|
|
let destination_transfer = MemoryTransferFs::local().with_dir(Location::local("/dest"));
|
|
|
|
let source = Arc::new(
|
|
BackendHandle::new(
|
|
SourceId::Local,
|
|
Arc::new(source_read),
|
|
Arc::new(source_transfer.clone()),
|
|
)
|
|
.unwrap(),
|
|
);
|
|
let destination = Arc::new(
|
|
BackendHandle::new(
|
|
SourceId::Local,
|
|
Arc::new(destination_read),
|
|
Arc::new(destination_transfer.clone()),
|
|
)
|
|
.unwrap(),
|
|
);
|
|
|
|
let prepared = OperationPlanner::new(source, destination)
|
|
.prepare(request(
|
|
&["/a/b/file", "/a/bc", "/a/b"],
|
|
"/dest",
|
|
CollisionPolicy::Ask,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(prepared.plan().expected_items(), Some(2));
|
|
assert_eq!(prepared.plan().expected_bytes(), Some(5));
|
|
assert_eq!(prepared.items().len(), 3);
|
|
assert_eq!(
|
|
prepared
|
|
.items()
|
|
.iter()
|
|
.filter(|item| item.source() == &Location::local("/a/b/file"))
|
|
.count(),
|
|
1
|
|
);
|
|
assert!(prepared.items().iter().any(|item| {
|
|
item.source() == &Location::local("/a/bc")
|
|
&& item.destination() == &Location::local("/dest/bc")
|
|
}));
|
|
assert_eq!(
|
|
prepared
|
|
.plan()
|
|
.sources()
|
|
.iter()
|
|
.map(|source| source.location().clone())
|
|
.collect::<Vec<_>>(),
|
|
vec![Location::local("/a/bc"), Location::local("/a/b")]
|
|
);
|
|
|
|
for transfer in [&source_transfer, &destination_transfer] {
|
|
assert!(transfer.part_locations().is_empty());
|
|
assert!(transfer.operations().iter().all(|entry| matches!(
|
|
entry.operation,
|
|
TransferOperation::Canonicalize | TransferOperation::Metadata
|
|
)));
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn canonical_directory_descendant_and_same_file_aliases_are_rejected() {
|
|
let fs = FakeFs::local()
|
|
.with_dir("/real/sub")
|
|
.with_dir("/alias")
|
|
.with_canonical_path("/alias", "/real/sub");
|
|
let (source, source_writes) = fake_backend(fs.clone());
|
|
let (destination, destination_writes) = fake_backend(fs);
|
|
|
|
let error = OperationPlanner::new(source, destination)
|
|
.prepare(request(&["/real"], "/alias", CollisionPolicy::Ask))
|
|
.await
|
|
.unwrap_err();
|
|
assert_eq!(
|
|
error,
|
|
PreflightError::Path(PathValidationError::DirectoryIntoItself)
|
|
);
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
|
|
let fs = FakeFs::local()
|
|
.with_file("/real/file", Some(3))
|
|
.with_file("/dest/file", Some(3))
|
|
.with_canonical_path("/dest/file", "/real/file");
|
|
let (source, source_writes) = fake_backend(fs.clone());
|
|
let (destination, destination_writes) = fake_backend(fs);
|
|
let error = OperationPlanner::new(source, destination)
|
|
.prepare(request(
|
|
&["/real/file"],
|
|
"/dest",
|
|
CollisionPolicy::Overwrite,
|
|
))
|
|
.await
|
|
.unwrap_err();
|
|
assert_eq!(error, PreflightError::Path(PathValidationError::SameFile));
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn direct_source_and_final_destination_identity_is_rejected_before_collision_policy() {
|
|
let fs = FakeFs::local().with_file("/dest/file", Some(1));
|
|
let (source, source_writes) = fake_backend(fs.clone());
|
|
let (destination, destination_writes) = fake_backend(fs);
|
|
|
|
let error = OperationPlanner::new(source, destination)
|
|
.prepare(request(
|
|
&["/dest/file"],
|
|
"/dest",
|
|
CollisionPolicy::Overwrite,
|
|
))
|
|
.await
|
|
.unwrap_err();
|
|
|
|
assert_eq!(error, PreflightError::Path(PathValidationError::SameFile));
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn symlink_and_special_sources_are_rejected_without_following_or_writing() {
|
|
for kind in [EntryKind::Symlink, EntryKind::Other] {
|
|
let fs = FakeFs::local().with_kind("/source/item", kind);
|
|
let destination = FakeFs::local().with_dir("/dest");
|
|
let (source, source_writes) = fake_backend(fs);
|
|
let (destination, destination_writes) = fake_backend(destination);
|
|
|
|
let error = OperationPlanner::new(source, destination)
|
|
.prepare(request(
|
|
&["/source/item"],
|
|
"/dest",
|
|
CollisionPolicy::Overwrite,
|
|
))
|
|
.await
|
|
.unwrap_err();
|
|
|
|
assert_eq!(
|
|
error,
|
|
PreflightError::UnsupportedSource {
|
|
location: Location::local("/source/item"),
|
|
kind,
|
|
}
|
|
);
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
}
|
|
|
|
let fs = FakeFs::local()
|
|
.with_dir("/dir")
|
|
.with_kind("/dir/link", EntryKind::Symlink)
|
|
.with_dir("/outside")
|
|
.with_file("/outside/secret", Some(6))
|
|
.with_canonical_path("/dir/link", "/outside");
|
|
let destination = FakeFs::local().with_dir("/dest");
|
|
let (source, source_writes) = fake_backend(fs);
|
|
let (destination, destination_writes) = fake_backend(destination);
|
|
let error = OperationPlanner::new(source, destination)
|
|
.prepare(request(&["/dir"], "/dest", CollisionPolicy::Skip))
|
|
.await
|
|
.unwrap_err();
|
|
assert_eq!(
|
|
error,
|
|
PreflightError::UnsupportedSource {
|
|
location: Location::local("/dir/link"),
|
|
kind: EntryKind::Symlink,
|
|
}
|
|
);
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn symlink_destination_directory_is_not_followed() {
|
|
let source = FakeFs::local().with_file("/source/file", Some(1));
|
|
let destination = FakeFs::local()
|
|
.with_kind("/dest", EntryKind::Symlink)
|
|
.with_dir("/real")
|
|
.with_canonical_path("/dest", "/real");
|
|
let (source, _) = fake_backend(source);
|
|
let (destination, writes) = fake_backend(destination);
|
|
|
|
let error = OperationPlanner::new(source, destination)
|
|
.prepare(request(
|
|
&["/source/file"],
|
|
"/dest",
|
|
CollisionPolicy::Overwrite,
|
|
))
|
|
.await
|
|
.unwrap_err();
|
|
|
|
assert_eq!(
|
|
error,
|
|
PreflightError::DestinationNotDirectory {
|
|
location: Location::local("/dest"),
|
|
kind: EntryKind::Symlink,
|
|
}
|
|
);
|
|
writes.assert_empty();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn ask_skip_and_overwrite_collisions_carry_the_destination_fingerprint() {
|
|
let modified = UNIX_EPOCH + Duration::from_secs(1234);
|
|
let fs = FakeFs::local()
|
|
.with_file("/source/file", Some(4))
|
|
.with_file("/dest/file", Some(9))
|
|
.with_metadata_snapshot("/dest/file", modified, 0o640);
|
|
let (source, source_writes) = fake_backend(fs.clone());
|
|
let (destination, destination_writes) = fake_backend(fs);
|
|
let planner = OperationPlanner::new(source, destination);
|
|
|
|
let error = planner
|
|
.prepare(request(&["/source/file"], "/dest", CollisionPolicy::Ask))
|
|
.await
|
|
.unwrap_err();
|
|
let PreflightError::CollisionRequired(collision) = error else {
|
|
panic!("ask must return a collision request");
|
|
};
|
|
assert_eq!(collision.source(), &Location::local("/source/file"));
|
|
assert_eq!(collision.destination(), &Location::local("/dest/file"));
|
|
assert_eq!(collision.destination_fingerprint().size(), Some(9));
|
|
assert_eq!(
|
|
collision.destination_fingerprint().modified(),
|
|
Some(modified)
|
|
);
|
|
assert_eq!(
|
|
collision.destination_fingerprint().permissions(),
|
|
Some(0o640)
|
|
);
|
|
|
|
for (policy, is_skip) in [
|
|
(CollisionPolicy::Skip, true),
|
|
(CollisionPolicy::Overwrite, false),
|
|
] {
|
|
let prepared = planner
|
|
.prepare(request(&["/source/file"], "/dest", policy))
|
|
.await
|
|
.unwrap();
|
|
let collision = prepared.items()[0].collision();
|
|
match collision {
|
|
CollisionDecision::Skip { destination } if is_skip => {
|
|
assert_eq!(destination.size(), Some(9));
|
|
assert_eq!(destination.modified(), Some(modified));
|
|
}
|
|
CollisionDecision::Overwrite { destination } if !is_skip => {
|
|
assert_eq!(destination.size(), Some(9));
|
|
assert_eq!(destination.permissions(), Some(0o640));
|
|
}
|
|
other => panic!("unexpected collision decision: {other:?}"),
|
|
}
|
|
}
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn mixed_ask_resolutions_advance_and_preserve_distinct_root_policies() {
|
|
let first = Location::local("/source/first");
|
|
let second = Location::local("/source/second");
|
|
let fs = FakeFs::local()
|
|
.with_file(&first.path, Some(1))
|
|
.with_file(&second.path, Some(2))
|
|
.with_file("/dest/first", Some(3))
|
|
.with_file("/dest/second", Some(4));
|
|
let (source, source_writes) = fake_backend(fs.clone());
|
|
let (destination, destination_writes) = fake_backend(fs);
|
|
let planner = OperationPlanner::new(source, destination);
|
|
let base_request = || {
|
|
request(
|
|
&["/source/first", "/source/second"],
|
|
"/dest",
|
|
CollisionPolicy::Ask,
|
|
)
|
|
};
|
|
|
|
assert!(base_request().resolutions.is_empty());
|
|
|
|
// An unresolved Ask stops at the first collision and remains completely read-only.
|
|
let error = planner.prepare(base_request()).await.unwrap_err();
|
|
assert!(matches!(
|
|
error,
|
|
PreflightError::CollisionRequired(collision)
|
|
if collision.source() == &first
|
|
));
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
|
|
// Resolving the first item must expose the second collision rather than looping on the first.
|
|
let error = planner
|
|
.prepare(base_request().with_resolutions(BTreeMap::from([(
|
|
first.clone(),
|
|
CollisionPolicy::Overwrite,
|
|
)])))
|
|
.await
|
|
.unwrap_err();
|
|
assert!(matches!(
|
|
error,
|
|
PreflightError::CollisionRequired(collision)
|
|
if collision.source() == &second
|
|
));
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
|
|
let prepared = planner
|
|
.prepare(base_request().with_resolutions(BTreeMap::from([
|
|
(first.clone(), CollisionPolicy::Overwrite),
|
|
(second.clone(), CollisionPolicy::Skip),
|
|
])))
|
|
.await
|
|
.unwrap();
|
|
assert!(matches!(
|
|
prepared.items()[0].collision(),
|
|
CollisionDecision::Overwrite { .. }
|
|
));
|
|
assert!(matches!(
|
|
prepared.items()[1].collision(),
|
|
CollisionDecision::Skip { .. }
|
|
));
|
|
|
|
let first_id = prepared.plan().sources()[0].id();
|
|
let second_id = prepared.plan().sources()[1].id();
|
|
assert_eq!(prepared.plan().sources()[0].location(), &first);
|
|
assert_eq!(prepared.plan().sources()[1].location(), &second);
|
|
assert_eq!(
|
|
prepared.plan().collision_policy_for(first_id),
|
|
CollisionPolicy::Overwrite
|
|
);
|
|
assert_eq!(
|
|
prepared.plan().collision_policy_for(second_id),
|
|
CollisionPolicy::Skip
|
|
);
|
|
assert_eq!(prepared.plan().collision_policies().len(), 2);
|
|
assert!(prepared.plan().collision_policies().keys().all(|id| {
|
|
prepared
|
|
.plan()
|
|
.sources()
|
|
.iter()
|
|
.any(|source| source.id() == *id)
|
|
}));
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn overwrite_replaces_a_symlink_snapshot_not_its_canonical_target() {
|
|
let fs = FakeFs::local()
|
|
.with_file("/source/file", Some(4))
|
|
.with_kind("/dest/file", EntryKind::Symlink)
|
|
.with_canonical_path("/dest/file", "/source/file");
|
|
let (source, source_writes) = fake_backend(fs.clone());
|
|
let (destination, destination_writes) = fake_backend(fs);
|
|
|
|
let prepared = OperationPlanner::new(source, destination)
|
|
.prepare(request(
|
|
&["/source/file"],
|
|
"/dest",
|
|
CollisionPolicy::Overwrite,
|
|
))
|
|
.await
|
|
.unwrap();
|
|
|
|
let CollisionDecision::Overwrite { destination } = prepared.items()[0].collision() else {
|
|
panic!("explicit overwrite should be retained");
|
|
};
|
|
assert_eq!(destination.kind(), EntryKind::Symlink);
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn existing_directory_and_directory_merge_collisions_are_always_rejected() {
|
|
for policy in [
|
|
CollisionPolicy::Ask,
|
|
CollisionPolicy::Skip,
|
|
CollisionPolicy::Overwrite,
|
|
] {
|
|
let fs = FakeFs::local()
|
|
.with_file("/source/file", Some(4))
|
|
.with_dir("/dest/file");
|
|
let (source, source_writes) = fake_backend(fs.clone());
|
|
let (destination, destination_writes) = fake_backend(fs);
|
|
let error = OperationPlanner::new(source, destination)
|
|
.prepare(request(&["/source/file"], "/dest", policy))
|
|
.await
|
|
.unwrap_err();
|
|
assert_eq!(
|
|
error,
|
|
PreflightError::DestinationDirectoryConflict {
|
|
location: Location::local("/dest/file"),
|
|
}
|
|
);
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
}
|
|
|
|
let fs = FakeFs::local()
|
|
.with_dir("/source/dir")
|
|
.with_file("/dest/dir", Some(4));
|
|
let (source, _) = fake_backend(fs.clone());
|
|
let (destination, writes) = fake_backend(fs);
|
|
let error = OperationPlanner::new(source, destination)
|
|
.prepare(request(
|
|
&["/source/dir"],
|
|
"/dest",
|
|
CollisionPolicy::Overwrite,
|
|
))
|
|
.await
|
|
.unwrap_err();
|
|
assert_eq!(
|
|
error,
|
|
PreflightError::DirectoryCollisionUnsupported {
|
|
location: Location::local("/dest/dir"),
|
|
}
|
|
);
|
|
writes.assert_empty();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn special_destination_and_duplicate_final_paths_are_rejected() {
|
|
let fs = FakeFs::local()
|
|
.with_file("/source/file", Some(4))
|
|
.with_kind("/dest/file", EntryKind::Other);
|
|
let (source, _) = fake_backend(fs.clone());
|
|
let (destination, writes) = fake_backend(fs);
|
|
let error = OperationPlanner::new(source, destination)
|
|
.prepare(request(
|
|
&["/source/file"],
|
|
"/dest",
|
|
CollisionPolicy::Overwrite,
|
|
))
|
|
.await
|
|
.unwrap_err();
|
|
assert_eq!(
|
|
error,
|
|
PreflightError::UnsupportedDestination {
|
|
location: Location::local("/dest/file"),
|
|
kind: EntryKind::Other,
|
|
}
|
|
);
|
|
writes.assert_empty();
|
|
|
|
let fs = FakeFs::local()
|
|
.with_file("/one/shared", Some(1))
|
|
.with_file("/two/shared", Some(2))
|
|
.with_dir("/dest");
|
|
let (source, _) = fake_backend(fs.clone());
|
|
let (destination, writes) = fake_backend(fs);
|
|
let error = OperationPlanner::new(source, destination)
|
|
.prepare(request(
|
|
&["/one/shared", "/two/shared"],
|
|
"/dest",
|
|
CollisionPolicy::Skip,
|
|
))
|
|
.await
|
|
.unwrap_err();
|
|
assert_eq!(
|
|
error,
|
|
PreflightError::DuplicateDestination {
|
|
location: Location::local("/dest/shared"),
|
|
}
|
|
);
|
|
writes.assert_empty();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn source_disappearing_between_listing_and_metadata_is_an_error_without_writes() {
|
|
let fs = FakeFs::local()
|
|
.with_dir("/dir")
|
|
.with_file("/dir/file", Some(1))
|
|
.with_metadata_error("/dir/file", FsError::NotFound);
|
|
let destination = FakeFs::local().with_dir("/dest");
|
|
let (source, source_writes) = fake_backend(fs);
|
|
let (destination, destination_writes) = fake_backend(destination);
|
|
|
|
let error = OperationPlanner::new(source, destination)
|
|
.prepare(request(&["/dir"], "/dest", CollisionPolicy::Skip))
|
|
.await
|
|
.unwrap_err();
|
|
|
|
assert_eq!(
|
|
error,
|
|
PreflightError::SourceBackend {
|
|
location: Location::local("/dir/file"),
|
|
error: FsError::NotFound,
|
|
}
|
|
);
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn unsafe_listed_names_cannot_escape_the_typed_destination() {
|
|
let fs = FakeFs::local().with_dir("/dir").with_listing(
|
|
"/dir",
|
|
vec![Entry::new("../outside", EntryKind::File).with_metadata(Some(3), None, None)],
|
|
);
|
|
let destination = FakeFs::local().with_dir("/dest");
|
|
let (source, source_writes) = fake_backend(fs);
|
|
let (destination, destination_writes) = fake_backend(destination);
|
|
|
|
let error = OperationPlanner::new(source, destination)
|
|
.prepare(request(&["/dir"], "/dest", CollisionPolicy::Skip))
|
|
.await
|
|
.unwrap_err();
|
|
|
|
assert_eq!(
|
|
error,
|
|
PreflightError::UnsafeEntryName {
|
|
directory: Location::local("/dir"),
|
|
}
|
|
);
|
|
source_writes.assert_empty();
|
|
destination_writes.assert_empty();
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn unknown_file_size_makes_only_the_byte_total_unknown() {
|
|
let fs = FakeFs::local().with_file("/source/file", None);
|
|
let destination = FakeFs::local().with_dir("/dest");
|
|
let (source, _) = fake_backend(fs);
|
|
let (destination, _) = fake_backend(destination);
|
|
|
|
let prepared = OperationPlanner::new(source, destination)
|
|
.prepare(request(&["/source/file"], "/dest", CollisionPolicy::Ask))
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(prepared.plan().expected_items(), Some(1));
|
|
assert_eq!(prepared.plan().expected_bytes(), None);
|
|
assert_eq!(
|
|
prepared.destination_fingerprint().kind(),
|
|
EntryKind::Directory
|
|
);
|
|
assert_eq!(prepared.items()[0].size(), None);
|
|
assert_eq!(prepared.items()[0].collision(), &CollisionDecision::Create);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn plan_identity_kind_policy_and_remote_epochs_are_preserved() {
|
|
let source_id = SourceId::Ssh(HostAlias::new("source"));
|
|
let destination_id = SourceId::Ssh(HostAlias::new("destination"));
|
|
let source = FakeFs::new(source_id.clone()).with_file("/file", Some(7));
|
|
let destination = FakeFs::new(destination_id.clone()).with_dir("/dest");
|
|
let source_location = source.location("/file");
|
|
let destination_location = destination.location("/dest");
|
|
let (source, _) = fake_backend(source);
|
|
let (destination, _) = fake_backend(destination);
|
|
let epochs = BTreeMap::from([(source_id.clone(), 11), (destination_id.clone(), 12)]);
|
|
|
|
let prepared = OperationPlanner::new(source, destination)
|
|
.prepare(OperationRequest::new(
|
|
PlanId::new(91),
|
|
PlanGeneration::new(8),
|
|
OperationKind::Move,
|
|
vec![source_location],
|
|
destination_location,
|
|
CollisionPolicy::Skip,
|
|
epochs.clone(),
|
|
))
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(prepared.plan().id(), PlanId::new(91));
|
|
assert_eq!(prepared.plan().generation(), PlanGeneration::new(8));
|
|
assert_eq!(prepared.plan().kind(), OperationKind::Move);
|
|
assert_eq!(prepared.plan().policy(), CollisionPolicy::Skip);
|
|
assert_eq!(prepared.plan().endpoint_epochs(), &epochs);
|
|
assert_eq!(prepared.plan().endpoint_epoch(&source_id), Some(11));
|
|
}
|