Debian Codegraph @

src/dir/remove.rs

raw symbols SCIP
1 //!
2 use std::path::{Path, PathBuf};
3
4 /// A special iterator which communicates its operation through results where…
5 ///
6 /// * `Some(Ok(removed_directory))` is yielded once or more success, followed by `None`
7 /// * `Some(Err(std::io::Error))` is yielded exactly once on failure.
8 pub struct Iter<'a> {
9 cursor: Option<&'a Path>,
10 boundary: &'a Path,
11 }
12
13 /// Construction
14 impl<'a> Iter<'a> {
15 /// Create a new instance that deletes `target` but will stop at `boundary`, without deleting the latter.
16 /// Returns an error if `boundary` doesn't contain `target`
17 ///
18 /// **Note** that we don't canonicalize the path for performance reasons.
19 pub fn new(target: &'a Path, boundary: &'a Path) -> std::io::Result<Self> {
20 if !target.starts_with(boundary) {
21 return Err(std::io::Error::new(
22 std::io::ErrorKind::InvalidInput,
23 format!(
24 "Removal target '{target}' must be contained in boundary '{boundary}'",
25 target = target.display(),
26 boundary = boundary.display()
27 ),
28 ));
29 }
30 let cursor = if target == boundary {
31 None
32 } else if target.exists() {
33 Some(target)
34 } else {
35 None
36 };
37 Ok(Iter { cursor, boundary })
38 }
39 }
40
41 impl<'a> Iterator for Iter<'a> {
42 type Item = std::io::Result<&'a Path>;
43
44 fn next(&mut self) -> Option<Self::Item> {
45 match self.cursor.take() {
46 Some(dir) => {
47 let next = match std::fs::remove_dir(dir) {
48 Ok(()) => Some(Ok(dir)),
49 Err(err) => match err.kind() {
50 std::io::ErrorKind::NotFound => Some(Ok(dir)),
51 _other_error_kind => return Some(Err(err)),
52 },
53 };
54 self.cursor = match dir.parent() {
55 Some(parent) => (parent != self.boundary).then_some(parent),
56 None => {
57 unreachable!("directory {:?} ran out of parents, this really shouldn't happen before hitting the boundary {:?}", dir, self.boundary)
58 }
59 };
60 next
61 }
62 None => None,
63 }
64 }
65 }
66
67 /// Delete all empty directories from `delete_dir` upward and until (not including) the `boundary_dir`.
68 ///
69 /// Note that `boundary_dir` must contain `delete_dir` or an error is returned, otherwise `delete_dir` is returned on success.
70 pub fn empty_upward_until_boundary<'a>(delete_dir: &'a Path, boundary_dir: &'a Path) -> std::io::Result<&'a Path> {
71 for item in Iter::new(delete_dir, boundary_dir)? {
72 match item {
73 Ok(_dir) => continue,
74 Err(err) => return Err(err),
75 }
76 }
77 Ok(delete_dir)
78 }
79
80 /// Delete all empty directories reachable from `delete_dir` from empty leaves moving upward to and including `delete_dir`.
81 ///
82 /// If any encountered directory contains a file the entire operation is aborted.
83 /// Please note that this is inherently racy and no attempts are made to counter that, which will allow creators to win
84 /// as long as they retry.
85 pub fn empty_depth_first(delete_dir: PathBuf) -> std::io::Result<()> {
86 if let Ok(()) = std::fs::remove_dir(&delete_dir) {
87 return Ok(());
88 }
89
90 let mut stack = vec![delete_dir];
91 let mut next_to_push = Vec::new();
92 while let Some(dir_to_delete) = stack.pop() {
93 let mut num_entries = 0;
94 for entry in std::fs::read_dir(&dir_to_delete)? {
95 num_entries += 1;
96 let entry = entry?;
97 if entry.file_type()?.is_dir() {
98 next_to_push.push(entry.path());
99 } else {
100 return Err(std::io::Error::other("Directory not empty"));
101 }
102 }
103 if num_entries == 0 {
104 std::fs::remove_dir(&dir_to_delete)?;
105 } else {
106 stack.push(dir_to_delete);
107 stack.append(&mut next_to_push);
108 }
109 }
110 Ok(())
111 }