Skip to content

Commit

Permalink
Merge pull request #5865 from sylvestre/md5sum-escape
Browse files Browse the repository at this point in the history
hashsum: when the filename contains some special chars, escape them
  • Loading branch information
cakebaker authored Jan 21, 2024
2 parents cfb8e59 + 5e29c60 commit f124910
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/uu/hashsum/src/hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,9 @@ where
} else if options.zero {
print!("{} {}{}\0", sum, binary_marker, filename.display());
} else {
println!("{} {}{}", sum, binary_marker, filename.display());
let (filename, has_prefix) = escape_filename(filename);
let prefix = if has_prefix { "\\" } else { "" };
println!("{}{} {}{}", prefix, sum, binary_marker, filename);
}
}
}
Expand All @@ -781,6 +783,16 @@ where
Ok(())
}

fn escape_filename(filename: &Path) -> (String, bool) {
let original = filename.as_os_str().to_string_lossy();
let escaped = original
.replace('\\', "\\\\")
.replace('\n', "\\n")
.replace('\r', "\\r");
let has_changed = escaped != original;
(escaped, has_changed)
}

fn digest_reader<T: Read>(
digest: &mut Box<dyn Digest>,
reader: &mut BufReader<T>,
Expand Down
15 changes: 15 additions & 0 deletions tests/by-util/test_hashsum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,18 @@ fn test_tag() {
"SHA256 (foobar) = 1f2ec52b774368781bed1d1fb140a92e0eb6348090619c9291f9a5a3c8e8d151\n",
);
}

#[test]
#[cfg(not(windows))]
fn test_with_escape_filename() {
let scene = TestScenario::new(util_name!());

let at = &scene.fixtures;
let filename = "a\nb";
at.touch(filename);
let result = scene.ccmd("md5sum").arg("--text").arg(filename).succeeds();
let stdout = result.stdout_str();
println!("stdout {}", stdout);
assert!(stdout.starts_with('\\'));
assert!(stdout.trim().ends_with("a\\nb"));
}

0 comments on commit f124910

Please sign in to comment.