diff --git a/backend/cli/src/site/init.rs b/backend/cli/src/site/init.rs index bc2783c4..8464f44d 100644 --- a/backend/cli/src/site/init.rs +++ b/backend/cli/src/site/init.rs @@ -336,9 +336,10 @@ mod tests { } #[test] - fn fake_driven_execution_creates_and_preserves_matching_site_inputs() { + fn fake_driven_execution_creates_site_inputs_in_an_empty_folder_and_preserves_them() { let fixture = SiteRoot::new(); let site_dir = fixture.root.join("site"); + fs::create_dir(&site_dir).expect("empty site folder"); let arguments = init_arguments(ApplicationsChoice::Docker); let prober = InitialSiteProber::new( Box::new(FakeDocker), diff --git a/backend/site/src/initial/input_store/mod.rs b/backend/site/src/initial/input_store/mod.rs index 8885c1d7..2f3fe1aa 100644 --- a/backend/site/src/initial/input_store/mod.rs +++ b/backend/site/src/initial/input_store/mod.rs @@ -252,13 +252,9 @@ impl InitialSiteInputWriter { site_root: &Path, ) -> Result { self.reject_recoverable_backup(site_root)?; - match fs::symlink_metadata(site_root) { - Ok(metadata) if metadata.file_type().is_symlink() => { - Err(InitialSiteWriteError::Symlink { - path: site_root.to_owned(), - }) - } - Ok(_) => { + match inspect_site_target(site_root)? { + SiteTarget::Absent => Ok(InitialSitePresence::Missing), + SiteTarget::Occupied => { self.reject_input_symlinks(site_root)?; ensure_existing_site_lock_available(site_root)?; self.validator @@ -266,10 +262,6 @@ impl InitialSiteInputWriter { .map_err(InitialSiteWriteError::ExistingInputsInvalid)?; Ok(InitialSitePresence::Initialized) } - Err(error) if error.kind() == io::ErrorKind::NotFound => { - Ok(InitialSitePresence::Missing) - } - Err(error) => Err(storage_error("inspect site target", site_root, error)), } } @@ -285,13 +277,12 @@ impl InitialSiteInputWriter { ) -> Result { let _lock = acquire_exclusive_site_lock(site_root)?; self.reject_recoverable_backup(site_root)?; - match fs::symlink_metadata(site_root) { - Ok(metadata) if metadata.file_type().is_symlink() => { - Err(InitialSiteWriteError::Symlink { - path: site_root.to_owned(), - }) + match inspect_site_target(site_root)? { + SiteTarget::Absent => { + self.create_locked(site_root, inputs)?; + Ok(InitialSiteWriteOutcome::Published) } - Ok(_) => { + SiteTarget::Occupied => { self.reject_input_symlinks(site_root)?; let has_declarations = existing_runtime_declarations(site_root)?; if has_declarations && !replace { @@ -332,11 +323,6 @@ impl InitialSiteInputWriter { self.replace_locked(site_root, inputs)?; Ok(InitialSiteWriteOutcome::Published) } - Err(error) if error.kind() == io::ErrorKind::NotFound => { - self.create_locked(site_root, inputs)?; - Ok(InitialSiteWriteOutcome::Published) - } - Err(error) => Err(storage_error("inspect site target", site_root, error)), } } @@ -365,17 +351,10 @@ impl InitialSiteInputWriter { inputs: &InitialSiteInputs, ) -> Result<(), InitialSiteWriteError> { self.reject_recoverable_backup(site_root)?; - match fs::symlink_metadata(site_root) { - Ok(metadata) if metadata.file_type().is_symlink() => { - return Err(InitialSiteWriteError::Symlink { - path: site_root.to_owned(), - }); - } - Ok(_) => return Err(InitialSiteWriteError::TargetExists), - Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} - Err(error) => return Err(storage_error("inspect site target", site_root, error)), + match inspect_site_target(site_root)? { + SiteTarget::Absent => self.create_locked(site_root, inputs), + SiteTarget::Occupied => Err(InitialSiteWriteError::TargetExists), } - self.create_locked(site_root, inputs) } fn create_locked( @@ -384,6 +363,8 @@ impl InitialSiteInputWriter { inputs: &InitialSiteInputs, ) -> Result<(), InitialSiteWriteError> { let staging = self.stage_inputs(site_root, inputs, true)?; + // Renaming a directory replaces an empty one at the target, so an absent target and an + // empty folder publish the same way; a folder that gained an entry makes the rename fail. if let Err(error) = self.filesystem.rename(&staging, site_root) { let _ = self.filesystem.remove_directory_all(&staging); return Err(storage_error( @@ -846,7 +827,7 @@ pub enum InitialSiteWriteError { /// Result of a read-only initialization-if-missing inspection. #[derive(Debug)] pub enum InitialSitePresence { - /// The site target does not exist and can enter guided initialization. + /// The site target is missing or an empty folder and can enter guided initialization. Missing, /// The site contains a complete valid operational input bundle. @@ -874,11 +855,15 @@ fn prepare_lock_parent(site_root: &Path, create_missing: bool) -> anyhow::Result for component in parent.components() { current.push(component.as_os_str()); match fs::symlink_metadata(¤t) { + // Folders above the site may be links, such as a linked home or the system's + // temporary folder; only the site folder and its own files must not be. Ok(metadata) if metadata.file_type().is_symlink() => { - return Err(anyhow!( - "Site input lock ancestor {} must not be a symbolic link", - current.display() - )); + if !fs::metadata(¤t).is_ok_and(|target| target.is_dir()) { + return Err(anyhow!( + "Site input lock ancestor {} must be a symbolic link to a directory", + current.display() + )); + } } Ok(metadata) if metadata.is_dir() => {} Ok(_) => { @@ -931,6 +916,39 @@ fn prepare_lock_parent(site_root: &Path, create_missing: bool) -> anyhow::Result Ok(()) } +/// What a site target path holds: nothing to keep, or something that must be validated or kept. +enum SiteTarget { + /// The path is missing or an empty folder, so a new site can be created there. + Absent, + + /// The path holds a file or a folder with at least one entry. + Occupied, +} + +/// Classifies the site target, refusing a symbolic link to it. +fn inspect_site_target(site_root: &Path) -> Result { + let metadata = match fs::symlink_metadata(site_root) { + Ok(metadata) => metadata, + Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(SiteTarget::Absent), + Err(error) => return Err(storage_error("inspect site target", site_root, error)), + }; + if metadata.file_type().is_symlink() { + return Err(InitialSiteWriteError::Symlink { + path: site_root.to_owned(), + }); + } + if !metadata.is_dir() { + return Ok(SiteTarget::Occupied); + } + let mut entries = fs::read_dir(site_root) + .map_err(|error| storage_error("inspect site target", site_root, error))?; + Ok(if entries.next().is_none() { + SiteTarget::Absent + } else { + SiteTarget::Occupied + }) +} + /// Reports whether a backend run left state beside the site inputs: owner state, the node store, /// application data, logs or telemetry. Runtime resources only exist after such a run. fn has_retained_state(site_root: &Path) -> bool { diff --git a/backend/site/src/initial/input_store/tests.rs b/backend/site/src/initial/input_store/tests.rs index af8bcdb4..901ca828 100644 --- a/backend/site/src/initial/input_store/tests.rs +++ b/backend/site/src/initial/input_store/tests.rs @@ -154,7 +154,77 @@ fn if_missing_inspection_is_read_only_and_rejects_invalid_or_running_sites() { } #[test] -fn clean_create_durably_creates_a_missing_parent_and_rejects_symlink_paths() { +fn an_empty_site_folder_is_initialized_as_a_new_site() { + let fixture = SiteRoot::new(); + let writer = InitialSiteInputWriter::new(SiteInputBundleValidator::for_test()); + let request = InitialSiteRequest::new( + [InitialInferenceRuntime::Simulated], + [InitialApplicationRuntime::Simulated], + ); + + let inspected = fixture.root.join("inspected"); + fs::create_dir(&inspected).expect("empty inspected folder"); + assert!(matches!( + writer + .inspect_if_missing(&inspected) + .expect("inspect empty folder"), + InitialSitePresence::Missing + )); + assert_eq!(directory_entries(&inspected), Vec::::new()); + + let initialized = fixture.root.join("initialized"); + fs::create_dir(&initialized).expect("empty initialized folder"); + assert!(matches!( + writer + .initialize(&initialized, &initial_site_inputs(), &request, false) + .expect("initialize empty folder"), + InitialSiteWriteOutcome::Published + )); + assert_eq!( + directory_entries(&initialized), + ["node.yaml", "site.yaml", "trust"] + ); + + let created = fixture.root.join("created"); + fs::create_dir(&created).expect("empty created folder"); + writer + .create(&created, &initial_site_inputs()) + .expect("create in empty folder"); + assert_eq!( + directory_entries(&created), + ["node.yaml", "site.yaml", "trust"] + ); +} + +#[test] +fn a_site_folder_with_any_entry_is_not_treated_as_empty() { + let fixture = SiteRoot::new(); + let writer = InitialSiteInputWriter::new(SiteInputBundleValidator::for_test()); + let root = fixture.root.join("site"); + fs::create_dir(&root).expect("site folder"); + fs::write(root.join(".hidden"), "kept").expect("hidden entry"); + + assert!(matches!( + writer + .inspect_if_missing(&root) + .expect_err("non-empty inspection"), + InitialSiteWriteError::ExistingInputsInvalid(_) + )); + assert!(matches!( + writer + .create(&root, &initial_site_inputs()) + .expect_err("non-empty creation"), + InitialSiteWriteError::TargetExists + )); + assert_eq!(directory_entries(&root), [".hidden"]); + assert_eq!( + fs::read_to_string(root.join(".hidden")).expect("hidden entry"), + "kept" + ); +} + +#[test] +fn clean_create_creates_missing_parents_through_symlinked_ancestors_and_rejects_symlink_locks() { let fixture = SiteRoot::new(); let root = fixture.root.join("missing-parent/site"); InitialSiteInputWriter::new(SiteInputBundleValidator::for_test()) @@ -166,11 +236,34 @@ fn clean_create_durably_creates_a_missing_parent_and_rejects_symlink_paths() { fs::create_dir(&external).expect("external directory"); let linked_parent = fixture.root.join("linked-parent"); symlink(&external, &linked_parent).expect("symlink ancestor"); - let ancestor_error = InitialSiteInputWriter::new(SiteInputBundleValidator::for_test()) + InitialSiteInputWriter::new(SiteInputBundleValidator::for_test()) .create(&linked_parent.join("site"), &initial_site_inputs()) - .expect_err("symlink ancestor refusal"); - assert!(matches!(ancestor_error, InitialSiteWriteError::Lock(_))); - assert!(!external.join("site").exists()); + .expect("create through symlinked ancestor"); + assert!(external.join("site").join(SITE_FILE).is_file()); + + InitialSiteInputWriter::new(SiteInputBundleValidator::for_test()) + .create(&linked_parent.join("missing/site"), &initial_site_inputs()) + .expect("create missing folders below a symlinked ancestor"); + assert!(external.join("missing/site").join(SITE_FILE).is_file()); + + let shared_root = linked_parent.join("shared/site"); + drop( + acquire_shared_site_lock(&shared_root, Duration::ZERO) + .expect_err("shared lock never creates a missing parent"), + ); + fs::create_dir(external.join("shared")).expect("shared parent"); + drop( + acquire_shared_site_lock(&shared_root, Duration::ZERO) + .expect("shared lock through symlinked ancestor"), + ); + + let dangling_parent = fixture.root.join("dangling-parent"); + symlink(fixture.root.join("nowhere"), &dangling_parent).expect("dangling symlink ancestor"); + let dangling_error = InitialSiteInputWriter::new(SiteInputBundleValidator::for_test()) + .create(&dangling_parent.join("site"), &initial_site_inputs()) + .expect_err("dangling symlink ancestor refusal"); + assert!(matches!(dangling_error, InitialSiteWriteError::Lock(_))); + assert!(!fixture.root.join("nowhere").exists()); let lock_target = fixture.root.join("lock-target"); fs::write(&lock_target, "unchanged").expect("lock target");