Reduce duplication.

This commit is contained in:
R-VdP 2023-03-16 12:05:13 +01:00
parent f569c59eee
commit bacaae7878
No known key found for this signature in database

View file

@ -85,16 +85,7 @@ pub fn activate(store_path: &StorePath, ephemeral: bool) -> Result<()> {
status?; status?;
let new_state = create_etc_links(config.entries.values(), &etc_dir, state, &old_state) let new_state = create_etc_links(config.entries.values(), &etc_dir, state, &old_state)
.update_state(old_state, &|path, status| { .update_state(old_state, &try_delete_path);
log::debug!("Deactivating: {}", path.display());
try_delete_path(path, status)
.map_err(|e| {
log::error!("Error deleting path: {}", path.display());
log::error!("{e}");
e
})
.is_ok()
});
serialise_state(new_state)?; serialise_state(new_state)?;
@ -106,43 +97,45 @@ pub fn deactivate() -> Result<()> {
let state = read_created_files()?; let state = read_created_files()?;
log::debug!("{:?}", state); log::debug!("{:?}", state);
serialise_state(state.deactivate(&|path, status| { serialise_state(state.deactivate(&try_delete_path))?;
log::debug!("Deactivating: {}", path.display());
try_delete_path(path, status)
.map_err(|e| {
log::error!("Error deleting path: {}", path.display());
log::error!("{e}");
e
})
.is_ok()
}))?;
log::info!("Done"); log::info!("Done");
Ok(()) Ok(())
} }
fn try_delete_path(path: &Path, status: &EtcFileStatus) -> Result<()> { fn try_delete_path(path: &Path, status: &EtcFileStatus) -> bool {
// exists() returns false for broken symlinks fn do_try_delete(path: &Path, status: &EtcFileStatus) -> Result<()> {
if path.exists() || path.is_symlink() { // exists() returns false for broken symlinks
if path.is_symlink() { if path.exists() || path.is_symlink() {
remove_link(path) if path.is_symlink() {
} else if path.is_file() { remove_link(path)
remove_file(path) } else if path.is_file() {
} else if path.is_dir() { remove_file(path)
if path.read_dir()?.next().is_none() { } else if path.is_dir() {
remove_dir(path) if path.read_dir()?.next().is_none() {
} else { remove_dir(path)
if let EtcFileStatus::Managed = status { } else {
log::warn!("Managed directory not empty, ignoring: {}", path.display()); if let EtcFileStatus::Managed = status {
log::warn!("Managed directory not empty, ignoring: {}", path.display());
}
Ok(())
} }
Ok(()) } else {
anyhow::bail!("Unsupported file type! {}", path.display())
} }
} else { } else {
Err(anyhow!("Unsupported file type! {}", path.display())) Ok(())
} }
} else {
Ok(())
} }
log::debug!("Deactivating: {}", path.display());
do_try_delete(path, status)
.map_err(|e| {
log::error!("Error deleting path: {}", path.display());
log::error!("{e}");
e
})
.is_ok()
} }
fn create_etc_links<'a, E>( fn create_etc_links<'a, E>(