cap_primitives/fs/manually/canonical_path.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
use std::ffi::OsStr;
use std::path::{Component, PathBuf};
/// Utility for collecting the canonical path components.
pub(super) struct CanonicalPath<'path_buf> {
/// If the user requested a canonical path, a reference to the `PathBuf` to
/// write it to.
path: Option<&'path_buf mut PathBuf>,
/// Our own private copy of the canonical path, for assertion checking.
#[cfg(racy_asserts)]
pub(super) debug: PathBuf,
}
impl<'path_buf> CanonicalPath<'path_buf> {
pub(super) fn new(path: Option<&'path_buf mut PathBuf>) -> Self {
Self {
#[cfg(racy_asserts)]
debug: PathBuf::new(),
path,
}
}
pub(super) fn push(&mut self, one: &OsStr) {
#[cfg(racy_asserts)]
self.debug.push(one);
if let Some(path) = &mut self.path {
path.push(one)
}
}
pub(super) fn pop(&mut self) -> bool {
#[cfg(racy_asserts)]
self.debug.pop();
if let Some(path) = &mut self.path {
path.pop()
} else {
true
}
}
/// The complete canonical path has been scanned. Set `path` to `None`
/// so that it isn't cleared when `self` is dropped.
pub(super) fn complete(&mut self) {
// Replace "" with ".", since "" as a relative path is interpreted as
// an error.
if let Some(path) = &mut self.path {
if path.as_os_str().is_empty() {
path.push(Component::CurDir);
}
self.path = None;
}
}
}
impl<'path_buf> Drop for CanonicalPath<'path_buf> {
fn drop(&mut self) {
// If `self.path` is still `Some` here, it means that we haven't called
// `complete()` yet, meaning the `CanonicalPath` is being dropped
// before the complete path has been processed. In that case, clear
// `path` to indicate that we weren't able to obtain a complete path.
if let Some(path) = &mut self.path {
path.clear();
self.path = None;
}
}
}