libbtrfs 0.0.20

Rust library for working with the btrfs filesystem
Documentation
use std::{
    ffi::OsStr,
    io,
    os::unix::ffi::OsStrExt,
    path::{Component, Path, MAIN_SEPARATOR as SEP},
};

/// Returns the parent dir and name as a tuple for a given path.
/// If the path contains invalid utf-8 then [`InvalidData`] is returned
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")
    }
}