Pass the status along to the delete_action so that we can act correctly on non-empty dirs.
This commit is contained in:
parent
daae141949
commit
687c533478
2 changed files with 32 additions and 21 deletions
|
|
@ -85,7 +85,7 @@ pub fn activate(store_path: &StorePath, ephemeral: bool) -> Result<()> {
|
|||
status?;
|
||||
|
||||
let new_state = create_etc_links(config.entries.values(), &etc_dir, state, &old_state)
|
||||
.update_state(old_state, &|path| {
|
||||
.update_state(old_state, &|path, status| {
|
||||
log::debug!("Deactivating: {}", path.display());
|
||||
false
|
||||
});
|
||||
|
|
@ -100,8 +100,9 @@ pub fn deactivate() -> Result<()> {
|
|||
let state = read_created_files()?;
|
||||
log::debug!("{:?}", state);
|
||||
|
||||
serialise_state(state.deactivate(&|path| {
|
||||
try_delete_path(path)
|
||||
serialise_state(state.deactivate(&|path, status| {
|
||||
log::debug!("Deactivating: {}", path.display());
|
||||
try_delete_path(path, status)
|
||||
.map_err(|e| {
|
||||
log::error!("Error deleting path: {}", path.display());
|
||||
log::error!("{e}");
|
||||
|
|
@ -114,7 +115,7 @@ pub fn deactivate() -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn try_delete_path(path: &Path) -> Result<()> {
|
||||
fn try_delete_path(path: &Path, status: &EtcFileStatus) -> Result<()> {
|
||||
// exists() returns false for broken symlinks
|
||||
if path.exists() || path.is_symlink() {
|
||||
if path.is_symlink() {
|
||||
|
|
@ -122,7 +123,14 @@ fn try_delete_path(path: &Path) -> Result<()> {
|
|||
} else if path.is_file() {
|
||||
remove_file(path)
|
||||
} else if path.is_dir() {
|
||||
remove_dir(path)
|
||||
if path.read_dir()?.next().is_none() {
|
||||
remove_dir(path)
|
||||
} else {
|
||||
if let EtcFileStatus::Managed = status {
|
||||
log::warn!("Managed directory not empty, ignoring: {}", path.display());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Err(anyhow!("Unsupported file type! {}", path.display()))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -149,7 +149,7 @@ impl EtcTree {
|
|||
|
||||
pub fn deactivate<F>(self, delete_action: &F) -> Option<EtcTree>
|
||||
where
|
||||
F: Fn(&Path) -> bool,
|
||||
F: Fn(&Path, &EtcFileStatus) -> bool,
|
||||
{
|
||||
let new_tree = self.nested.keys().fold(self.clone(), |mut new_tree, name| {
|
||||
new_tree.nested = new_tree.nested.alter(
|
||||
|
|
@ -165,7 +165,7 @@ impl EtcTree {
|
|||
// closure on their paths).
|
||||
if new_tree.nested.is_empty() {
|
||||
if let EtcFileStatus::Managed = new_tree.status {
|
||||
if delete_action(&new_tree.path) {
|
||||
if delete_action(&new_tree.path, &new_tree.status) {
|
||||
None
|
||||
} else {
|
||||
Some(new_tree)
|
||||
|
|
@ -180,7 +180,7 @@ impl EtcTree {
|
|||
|
||||
pub fn update_state<F>(self, other: Self, delete_action: &F) -> Option<Self>
|
||||
where
|
||||
F: Fn(&Path) -> bool,
|
||||
F: Fn(&Path, &EtcFileStatus) -> bool,
|
||||
{
|
||||
let to_deactivate = other
|
||||
.nested
|
||||
|
|
@ -235,7 +235,7 @@ mod tests {
|
|||
impl EtcTree {
|
||||
pub fn deactivate_managed_entry<F>(self, path: &Path, delete_action: &F) -> Self
|
||||
where
|
||||
F: Fn(&Path) -> bool,
|
||||
F: Fn(&Path, &EtcFileStatus) -> bool,
|
||||
{
|
||||
fn go<'a, C, F>(
|
||||
mut tree: EtcTree,
|
||||
|
|
@ -245,7 +245,7 @@ mod tests {
|
|||
) -> EtcTree
|
||||
where
|
||||
C: Iterator<Item = path::Component<'a>>,
|
||||
F: Fn(&Path) -> bool,
|
||||
F: Fn(&Path, &EtcFileStatus) -> bool,
|
||||
{
|
||||
log::debug!("Deactivating {}", path.display());
|
||||
|
||||
|
|
@ -368,22 +368,25 @@ mod tests {
|
|||
.register_managed_entry(&PathBuf::from("/").join("foo5").join("baz").join("bar"));
|
||||
let tree2 = tree1
|
||||
.clone()
|
||||
.deactivate_managed_entry(&PathBuf::from("/").join("foo4"), &|p| {
|
||||
println!("Deactivating: {}", p.display());
|
||||
.deactivate_managed_entry(&PathBuf::from("/").join("foo4"), &|path, _status| {
|
||||
println!("Deactivating: {}", path.display());
|
||||
false
|
||||
})
|
||||
.deactivate_managed_entry(&PathBuf::from("/").join("foo2"), &|p| {
|
||||
println!("Deactivating: {}", p.display());
|
||||
.deactivate_managed_entry(&PathBuf::from("/").join("foo2"), &|path, _status| {
|
||||
println!("Deactivating: {}", path.display());
|
||||
true
|
||||
})
|
||||
.deactivate_managed_entry(&PathBuf::from("/").join("foo3"), &|p| {
|
||||
println!("Deactivating: {}", p.display());
|
||||
.deactivate_managed_entry(&PathBuf::from("/").join("foo3"), &|path, _status| {
|
||||
println!("Deactivating: {}", path.display());
|
||||
true
|
||||
})
|
||||
.deactivate_managed_entry(&PathBuf::from("/").join("foo5").join("baz"), &|p| {
|
||||
println!("Deactivating: {}", p.display());
|
||||
true
|
||||
});
|
||||
.deactivate_managed_entry(
|
||||
&PathBuf::from("/").join("foo5").join("baz"),
|
||||
&|path, _status| {
|
||||
println!("Deactivating: {}", path.display());
|
||||
true
|
||||
},
|
||||
);
|
||||
dbg!(&tree1);
|
||||
assert_eq!(
|
||||
tree2.nested.keys().sorted().collect::<Vec<_>>(),
|
||||
|
|
@ -424,7 +427,7 @@ mod tests {
|
|||
.register_managed_entry(&PathBuf::from("/").join("foo4").join("bar"))
|
||||
.register_managed_entry(&PathBuf::from("/").join("foo5"))
|
||||
.register_managed_entry(&PathBuf::from("/").join("foo5").join("bar"));
|
||||
let new_tree = tree1.update_state(tree2, &|path| {
|
||||
let new_tree = tree1.update_state(tree2, &|path, _status| {
|
||||
println!("Deactivating path: {}", path.display());
|
||||
path != PathBuf::from("/").join("foo5").join("bar").as_path()
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue