gix_fs/stack.rs
1use std::path::{Component, Path, PathBuf};
2
3use crate::Stack;
4
5/// Access
6impl Stack {
7 /// Returns the top-level path of the stack.
8 pub fn root(&self) -> &Path {
9 &self.root
10 }
11
12 /// Returns the absolute path the currently set path.
13 pub fn current(&self) -> &Path {
14 &self.current
15 }
16
17 /// Returns the currently set path relative to the [`root()`][Stack::root()].
18 pub fn current_relative(&self) -> &Path {
19 &self.current_relative
20 }
21}
22
23/// A delegate for use in a [`Stack`].
24pub trait Delegate {
25 /// Called whenever we push a directory on top of the stack, and after the respective call to [`push()`](Self::push).
26 ///
27 /// It is only called if the currently acted on path is a directory in itself, which is determined by knowing
28 /// that it's not the last component of the path.
29 /// Use [`Stack::current()`] to see the directory.
30 fn push_directory(&mut self, stack: &Stack) -> std::io::Result<()>;
31
32 /// Called after any component was pushed, with the path available at [`Stack::current()`].
33 ///
34 /// `is_last_component` is `true` if the path is completely built, which typically means it's not a directory.
35 fn push(&mut self, is_last_component: bool, stack: &Stack) -> std::io::Result<()>;
36
37 /// Called right after a directory-component was popped off the stack.
38 ///
39 /// Use it to pop information off internal data structures. Note that no equivalent call exists for popping
40 /// the file-component.
41 fn pop_directory(&mut self);
42}
43
44impl Stack {
45 /// Create a new instance with `root` being the base for all future paths we handle, assuming it to be valid which includes
46 /// symbolic links to be included in it as well.
47 pub fn new(root: PathBuf) -> Self {
48 Stack {
49 current: root.clone(),
50 current_relative: PathBuf::with_capacity(128),
51 valid_components: 0,
52 root,
53 current_is_directory: true,
54 }
55 }
56
57 /// Set the current stack to point to the `relative` path and call `push_comp()` each time a new path component is popped
58 /// along with the stacks state for inspection to perform an operation that produces some data.
59 ///
60 /// The full path to `relative` will be returned along with the data returned by `push_comp`.
61 /// Note that this only works correctly for the delegate's `push_directory()` and `pop_directory()` methods if
62 /// `relative` paths are terminal, so point to their designated file or directory.
63 /// The path is also expected to be normalized, and should not contain extra separators, and must not contain `..`
64 /// or have leading or trailing slashes (or additionally backslashes on Windows).
65 pub fn make_relative_path_current(&mut self, relative: &Path, delegate: &mut dyn Delegate) -> std::io::Result<()> {
66 if self.valid_components != 0 && relative.as_os_str().is_empty() {
67 return Err(std::io::Error::new(
68 std::io::ErrorKind::Other,
69 "empty inputs are not allowed",
70 ));
71 }
72 if self.valid_components == 0 {
73 delegate.push_directory(self)?;
74 }
75
76 let mut components = relative.components().peekable();
77 let mut existing_components = self.current_relative.components();
78 let mut matching_components = 0;
79 while let (Some(existing_comp), Some(new_comp)) = (existing_components.next(), components.peek()) {
80 if existing_comp == *new_comp {
81 components.next();
82 matching_components += 1;
83 } else {
84 break;
85 }
86 }
87
88 for _ in 0..self.valid_components - matching_components {
89 self.current.pop();
90 self.current_relative.pop();
91 if self.current_is_directory {
92 delegate.pop_directory();
93 }
94 self.current_is_directory = true;
95 }
96 self.valid_components = matching_components;
97
98 if !self.current_is_directory && components.peek().is_some() {
99 delegate.push_directory(self)?;
100 }
101
102 while let Some(comp) = components.next() {
103 if !matches!(comp, Component::Normal(_)) {
104 return Err(std::io::Error::new(
105 std::io::ErrorKind::Other,
106 format!(
107 "Input path \"{}\" contains relative or absolute components",
108 relative.display()
109 ),
110 ));
111 }
112 let is_last_component = components.peek().is_none();
113 self.current_is_directory = !is_last_component;
114 self.current.push(comp);
115 self.current_relative.push(comp);
116 self.valid_components += 1;
117 let res = delegate.push(is_last_component, self);
118 if self.current_is_directory {
119 delegate.push_directory(self)?;
120 }
121
122 if let Err(err) = res {
123 self.current.pop();
124 self.current_relative.pop();
125 self.valid_components -= 1;
126 return Err(err);
127 }
128 }
129 Ok(())
130 }
131}