💄 Reduce nesting in Snapshot::remove_path

This commit is contained in:
Max Brunsfeld 2021-04-19 12:15:24 -07:00
parent 3e93fb9459
commit 0fd3f55730

View file

@ -258,51 +258,50 @@ impl Snapshot {
} }
fn remove_path(&mut self, path: &Path) { fn remove_path(&mut self, path: &Path) {
if let Some(mut parent_entry) = path.parent().and_then(|p| self.entry_for_path(p).cloned()) let mut parent_entry = match path.parent().and_then(|p| self.entry_for_path(p).cloned()) {
{ Some(e) => e,
let mut edits = Vec::new(); None => return,
};
let parent_inode = parent_entry.inode(); let mut edits = Vec::new();
let mut entry_inode = None; let parent_inode = parent_entry.inode();
if let Entry::Dir { children, .. } = &mut parent_entry { let mut entry_inode = None;
let mut new_children = Vec::new(); if let Entry::Dir { children, .. } = &mut parent_entry {
for (child_inode, child_name) in children.as_ref() { let mut new_children = Vec::new();
if Some(child_name.as_ref()) == path.file_name() { for (child_inode, child_name) in children.as_ref() {
entry_inode = Some(*child_inode); if Some(child_name.as_ref()) == path.file_name() {
} else { entry_inode = Some(*child_inode);
new_children.push((*child_inode, child_name.clone())); } else {
} new_children.push((*child_inode, child_name.clone()));
} }
if new_children.iter().any(|c| Some(c.0) == entry_inode) {
entry_inode = None;
}
*children = new_children.into();
edits.push(Edit::Insert(parent_entry));
} else {
unreachable!();
} }
if let Some(entry_inode) = entry_inode { if new_children.iter().any(|c| Some(c.0) == entry_inode) {
// Recursively remove the orphaned nodes' descendants. entry_inode = None;
let mut descendant_stack = Vec::new(); }
descendant_stack.push((entry_inode, parent_inode));
while let Some((inode, parent_inode)) = descendant_stack.pop() { *children = new_children.into();
if let Some(entry) = self.entries.get(&inode) { edits.push(Edit::Insert(parent_entry));
if entry.parent() == Some(parent_inode) { } else {
edits.push(Edit::Remove(inode)); unreachable!();
if let Entry::Dir { children, .. } = entry { }
descendant_stack
.extend(children.iter().map(|c| (c.0, entry.inode()))); if let Some(entry_inode) = entry_inode {
} let mut descendant_stack = Vec::new();
descendant_stack.push((entry_inode, parent_inode));
while let Some((inode, parent_inode)) = descendant_stack.pop() {
if let Some(entry) = self.entries.get(&inode) {
if entry.parent() == Some(parent_inode) {
edits.push(Edit::Remove(inode));
if let Entry::Dir { children, .. } = entry {
descendant_stack.extend(children.iter().map(|c| (c.0, entry.inode())));
} }
} }
} }
} }
self.entries.edit(edits);
} }
self.entries.edit(edits);
} }
fn fmt_entry( fn fmt_entry(
@ -372,19 +371,19 @@ impl FileHandle {
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Entry { pub enum Entry {
Dir { Dir {
parent: Option<u64>,
inode: u64, inode: u64,
parent: Option<u64>,
is_symlink: bool, is_symlink: bool,
is_ignored: bool, is_ignored: bool,
children: Arc<[(u64, Arc<OsStr>)]>, children: Arc<[(u64, Arc<OsStr>)]>,
pending: bool, pending: bool,
}, },
File { File {
parent: Option<u64>,
path: PathEntry,
inode: u64, inode: u64,
parent: Option<u64>,
is_symlink: bool, is_symlink: bool,
is_ignored: bool, is_ignored: bool,
path: PathEntry,
}, },
} }