libbtrfs/util/
mod.rs

1/*
2 * utility functions 
3 */
4
5mod convert;
6pub(crate) use convert::*;
7
8mod subvolume;
9pub(crate) use subvolume::*;
10
11mod fd;
12pub(crate) use fd::*;
13
14mod ioctl;
15pub(crate) use ioctl::*;
16
17mod path;
18pub(crate) use path::*;
19
20
21
22
23
24// rust impl of basename from the standard C library
25pub(crate) fn basename(path: &str) -> &str {
26    let path = path.trim_end_matches('/').split('/').last().unwrap();
27
28    if path.is_empty() {
29        "/"
30    } else {
31        path
32    }
33}
34
35// rust impl of dirname from the standard C library
36pub(crate) fn dirname(path: &str) -> &str {
37    let suffix = basename(path);
38
39    if !path.trim_end_matches('/').contains('/') {
40        "."
41    } else {
42        let path = path.trim_end_matches('/').strip_suffix(suffix).expect("ends with the suffix");
43
44        if path.trim_end_matches('/').is_empty() {
45            "/"
46        } else {
47            path.trim_end_matches('/')
48        }
49    }
50}