Skip to content
Myr

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

2 files+21 added−13 removed

modified:src/main.rs+16 added-13 removed
6 unmodified lines
78pub mod lscolors;9pub mod fshelper;1011use std::borrow::Cow;12use std::env;13use std::error::Error;13use std::ffi::OsStr;14use std::fs;15use std::io::Write;16use std::ops::Deref;17#[cfg(target_family = "unix")]18use std::os::unix::fs::PermissionsExt;19use std::path::{Path, Component};20use std::process;
49 unmodified lines
7071/// Root directory72static ROOT_DIR : &'static str = "/";7374/// Parent directory75static PARENT_DIR : &'static str = "..";7677/// Print a search result to the console.78fn print_entry(base: &Path, entry: &Path, config: &FdOptions) {79 let path_full = base.join(entry);8077 let path_str = match entry.to_str() {78 Some(p) => p,79 None => return80 };81 let path_str = entry.to_string_lossy();8283 #[cfg(target_family = "unix")]84 let is_executable = |p: &std::path::PathBuf| {85 p.metadata()
16 unmodified lines
102103 // 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 };110110 component_path.push(Path::new(comp_str));111 component_path.push(Path::new(comp_str.deref()));112113 let style =114 if component_path.symlink_metadata()115 .map(|md| md.file_type().is_symlink())
65 unmodified lines
181 None => error("Could not get relative path for directory entry.")182 };183 let path_rel = path_rel_buf.as_path();184184 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 };192193 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}199200/// Print error message to stderr and exit with status `1`.
118 unmodified lines
modified:tests/test.sh+5 added
196 unmodified lines
197$root/one/two/C.Foo198$root/one/two/three/d.foo199$root/one/two/three/directory_foo" --absolute-path foo200201202suite "Invalid UTF-8"203touch "$(printf 'test-invalid-utf8-\xc3.txt')"204expect "$(printf 'test-invalid-utf8-\ufffd.txt')" test-invalid-utf8205206# All done207echo