Skip to content

Commit 05c4ce0

Browse files
authored
Merge pull request #7151 from jfinkels/df-mountinfo-special-chars
df: fix display of special characters
2 parents df77904 + 96c1731 commit 05c4ce0

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

src/uucore/src/lib/features/fsext.rs

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ pub struct MountInfo {
137137
pub dummy: bool,
138138
}
139139

140+
#[cfg(any(target_os = "linux", target_os = "android"))]
141+
fn replace_special_chars(s: String) -> String {
142+
// Replace
143+
//
144+
// * ASCII space with a regular space character,
145+
// * \011 ASCII horizontal tab with a tab character,
146+
// * ASCII backslash with an actual backslash character.
147+
//
148+
s.replace(r#"\040"#, " ")
149+
.replace(r#"\011"#, " ")
150+
.replace(r#"\134"#, r#"\"#)
151+
}
152+
140153
impl MountInfo {
141154
#[cfg(any(target_os = "linux", target_os = "android"))]
142155
fn new(file_name: &str, raw: &[&str]) -> Option<Self> {
@@ -158,14 +171,14 @@ impl MountInfo {
158171
dev_name = raw[after_fields + 1].to_string();
159172
fs_type = raw[after_fields].to_string();
160173
mount_root = raw[3].to_string();
161-
mount_dir = raw[4].to_string();
174+
mount_dir = replace_special_chars(raw[4].to_string());
162175
mount_option = raw[5].to_string();
163176
}
164177
LINUX_MTAB => {
165178
dev_name = raw[0].to_string();
166179
fs_type = raw[2].to_string();
167180
mount_root = String::new();
168-
mount_dir = raw[1].to_string();
181+
mount_dir = replace_special_chars(raw[1].to_string());
169182
mount_option = raw[3].to_string();
170183
}
171184
_ => return None,
@@ -1081,4 +1094,28 @@ mod tests {
10811094
assert_eq!(info.fs_type, "xfs");
10821095
assert_eq!(info.dev_name, "/dev/fs0");
10831096
}
1097+
1098+
#[test]
1099+
#[cfg(any(target_os = "linux", target_os = "android"))]
1100+
fn test_mountinfo_dir_special_chars() {
1101+
let info = MountInfo::new(
1102+
LINUX_MOUNTINFO,
1103+
&r#"317 61 7:0 / /mnt/f\134\040\011oo rw,relatime shared:641 - ext4 /dev/loop0 rw"#
1104+
.split_ascii_whitespace()
1105+
.collect::<Vec<_>>(),
1106+
)
1107+
.unwrap();
1108+
1109+
assert_eq!(info.mount_dir, r#"/mnt/f\ oo"#);
1110+
1111+
let info = MountInfo::new(
1112+
LINUX_MTAB,
1113+
&r#"/dev/loop0 /mnt/f\134\040\011oo ext4 rw,relatime 0 0"#
1114+
.split_ascii_whitespace()
1115+
.collect::<Vec<_>>(),
1116+
)
1117+
.unwrap();
1118+
1119+
assert_eq!(info.mount_dir, r#"/mnt/f\ oo"#);
1120+
}
10841121
}

0 commit comments

Comments
 (0)