Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions crates/disktree-core/src/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ pub fn device_in(table: &str, path: &Path) -> Option<String> {
.lines()
.filter_map(|line| {
let mut fields = line.split_whitespace();
let device = fields.next()?;
// Spaces in mount points are escaped as \040.
let mount = fields.next()?.replace("\\040", " ");
path.starts_with(&mount)
.then(|| (mount.len(), device.to_string()))
let device = unescape_octal(fields.next()?);
let mount = unescape_octal(fields.next()?);
path.starts_with(&mount).then_some((mount.len(), device))
})
.max_by_key(|(length, _)| *length)
.map(|(_, device)| device)
Expand All @@ -122,9 +120,9 @@ pub fn parse_mounts(table: &str) -> Vec<Mount> {
.lines()
.filter_map(|line| {
let mut fields = line.split_whitespace();
let source = fields.next()?.to_string();
// Spaces in mount points are escaped as \040.
let point = PathBuf::from(fields.next()?.replace("\\040", " "));
// Escaped as in mountinfo: space, tab, newline and backslash.
let source = unescape_octal(fields.next()?);
let point = PathBuf::from(unescape_octal(fields.next()?));
let fstype = fields.next()?.to_string();
let options = fields.next().unwrap_or_default().to_string();
Some(Mount {
Expand Down Expand Up @@ -996,6 +994,25 @@ portal /run/user/1000/doc fuse.portal rw 0 0
assert_eq!(mounts[0].fstype, "ext4");
}

#[test]
fn mounts_escapes_are_decoded_once() {
let table = "/dev/sda1 /media/My\\040Disk\\134040 ext4 rw 0 0\n\
//nas/My\\040Share /mnt/tab\\011name cifs rw 0 0\n";
let mounts = parse_mounts(table);
assert_eq!(mounts.len(), 2);
assert_eq!(mounts[0].point, Path::new("/media/My Disk\\040"));
assert_eq!(mounts[1].point, Path::new("/mnt/tab\tname"));
assert_eq!(mounts[1].source, "//nas/My Share");
assert_eq!(
device_in(table, Path::new("/media/My Disk\\040/a")).as_deref(),
Some("/dev/sda1")
);
assert_eq!(
device_in(table, Path::new("/mnt/tab\tname/a")).as_deref(),
Some("//nas/My Share")
);
}

#[test]
fn volumes_with_least_space_come_first_and_unknowns_last() {
let volume = |name: &str, available: Option<u64>| Volume {
Expand Down
Loading