Handle invalid UTF-8, fixes #34
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by David Peter · parent 087e709 · Browse files · 2 files·+21 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+21 added−13 removed
- tests:+5 added−0 removed
- other:+16 added−13 removed
Provenance
- Commit
24070b0- Tree
ab65f73- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
087e709
2 files changed+21 added−13 removed
modified:src/main.rs+16 added-13 removed
6 unmodified lines7 8 pub mod lscolors;9 pub mod fshelper;10 11+use std::borrow::Cow;12 use std::env;13 use std::error::Error;13-use std::ffi::OsStr;14 use std::fs;15 use std::io::Write;16+use std::ops::Deref;17 #[cfg(target_family = "unix")]18 use std::os::unix::fs::PermissionsExt;19 use std::path::{Path, Component};20 use std::process;49 unmodified lines70 71 /// Root directory72 static ROOT_DIR : &'static str = "/";73 74+/// Parent directory75+static PARENT_DIR : &'static str = "..";76+77 /// Print a search result to the console.78 fn print_entry(base: &Path, entry: &Path, config: &FdOptions) {79 let path_full = base.join(entry);80 77- let path_str = match entry.to_str() {78- Some(p) => p,79- None => return80- };81+ let path_str = entry.to_string_lossy();82 83 #[cfg(target_family = "unix")]84 let is_executable = |p: &std::path::PathBuf| {85 p.metadata()16 unmodified lines102 103 // Traverse the path and colorize each component104 for component in entry.components() {105 let comp_str = match component {105- Component::Normal(p) => p.to_str().unwrap(),106- Component::ParentDir => "..",106+ Component::Normal(p) => p.to_string_lossy(),107+ Component::ParentDir => Cow::from(PARENT_DIR),108 _ => error("Unexpected path component")109 };110 110- component_path.push(Path::new(comp_str));111+ component_path.push(Path::new(comp_str.deref()));112 113 let style =114 if component_path.symlink_metadata()115 .map(|md| md.file_type().is_symlink())65 unmodified lines181 None => error("Could not get relative path for directory entry.")182 };183 let path_rel = path_rel_buf.as_path();184 184- let search_str =185+ let search_str_o =186 if config.search_full_path {186- path_rel.to_str()187+ Some(path_rel.to_string_lossy())188 } else {189 path_rel.file_name()189- .and_then(OsStr::to_str)190+ .map(|f| f.to_string_lossy())191 };192 193+ if let Some(search_str) = search_str_o {192- search_str.and_then(|s| pattern.find(s))193- .map(|_| print_entry(base, path_rel, config));194+ pattern.find(&*search_str)195+ .map(|_| print_entry(base, path_rel, config));196+ }197 }198 }199 200 /// Print error message to stderr and exit with status `1`.118 unmodified linesmodified:tests/test.sh+5 added
196 unmodified lines197 $root/one/two/C.Foo198 $root/one/two/three/d.foo199 $root/one/two/three/directory_foo" --absolute-path foo200 201+202+suite "Invalid UTF-8"203+touch "$(printf 'test-invalid-utf8-\xc3.txt')"204+expect "$(printf 'test-invalid-utf8-\ufffd.txt')" test-invalid-utf8205+206 # All done207 echo