use std::{
ffi::OsStr,
io,
os::unix::ffi::OsStrExt,
path::{Component, Path, MAIN_SEPARATOR as SEP},
};
pub(crate) fn subvolume_parent_and_name(path: &Path) -> io::Result<(&OsStr, &str)> {
if let Some(path) = path.to_str() {
let path = path.trim_end_matches(SEP);
if let Some(n) = path.rfind(SEP) {
if n == 0 {
Ok((Component::RootDir.as_os_str(), path.trim_start_matches(SEP)))
} else {
let (dir, base) = path.split_at(n);
Ok((
OsStr::from_bytes(dir.as_bytes()),
base.trim_start_matches(SEP),
))
}
} else {
Ok((Component::CurDir.as_os_str(), path))
}
} else {
error!(InvalidData; "Invalid UTF-8")
}
}