Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processes
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 ea21df3 · Browse files · 4 files·+40 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+40 added−7 removed
- other:+40 added−7 removed
Provenance
- Commit
6c9e743- Tree
7727911- Author
sharkdp <davidpeter@web.de>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
ea21df3
4 files changed+40 added−7 removed
modified:src/filesystem.rs-4 removed
86 unmodified lines87 pub fn strip_current_dir(path: &Path) -> &Path {88 path.strip_prefix(".").unwrap_or(path)89 }90 91-pub fn replace_path_separator<'a>(path: &str, new_path_separator: &str) -> String {92- path.replace(std::path::MAIN_SEPARATOR, &new_path_separator)93-}94-91 #[cfg(test)]92 mod tests {93 use super::strip_current_dir;94 use std::path::Path;15 unmodified linesmodified:src/main.rs+3 added-1 removed
135 unmodified lines136 // if the pattern has an uppercase character (smart case).137 let case_sensitive = !matches.is_present("ignore-case")138 && (matches.is_present("case-sensitive") || pattern_has_uppercase_char(&pattern_regex));139 140+ let interactive_terminal = atty::is(Stream::Stdout);141 let colored_output = match matches.value_of("color") {142 Some("always") => true,143 Some("never") => false,143- _ => env::var_os("NO_COLOR").is_none() && atty::is(Stream::Stdout),144+ _ => env::var_os("NO_COLOR").is_none() && interactive_terminal,145 };146 147 let path_separator = matches.value_of("path-separator").map(|str| str.to_owned());148 91 unmodified lines240 .value_of("max-buffer-time")241 .and_then(|n| u64::from_str_radix(n, 10).ok())242 .map(time::Duration::from_millis),243 ls_colors,244+ interactive_terminal,245 file_types: matches.values_of("file-type").map(|values| {246 let mut file_types = FileTypes::default();247 for value in values {248 match value {80 unmodified linesmodified:src/options.rs+3 added
50 unmodified lines51 /// `None` if the output should not be colorized. Otherwise, a `LsColors` instance that defines52 /// how to style different filetypes.53 pub ls_colors: Option<LsColors>,54 55+ /// Whether or not we are writing to an interactive terminal56+ pub interactive_terminal: bool,57+58 /// The type of file to search for. If set to `None`, all file types are displayed. If59 /// set to `Some(..)`, only the types that are specified are shown.60 pub file_types: Option<FileTypes>,61 29 unmodified linesmodified:src/output.rs+34 added-2 removed
6 unmodified lines7 use ansi_term;8 use lscolors::{LsColors, Style};9 10 use crate::exit_codes::ExitCode;11-use crate::filesystem::{replace_path_separator, strip_current_dir};11+use crate::filesystem::strip_current_dir;12 use crate::options::Options;13 14+pub fn replace_path_separator<'a>(path: &str, new_path_separator: &str) -> String {15+ path.replace(std::path::MAIN_SEPARATOR, &new_path_separator)16+}17+18 // TODO: this function is performance critical and can probably be optimized19 pub fn print_entry(20 stdout: &mut StdoutLock,21 entry: &PathBuf,54 unmodified lines76 }77 }78 79 // TODO: this function is performance critical and can probably be optimized76-fn print_entry_uncolorized(80+fn print_entry_uncolorized_base(81 stdout: &mut StdoutLock,82 path: &Path,83 config: &Options,84 ) -> io::Result<()> {4 unmodified lines89 *path_string.to_mut() = replace_path_separator(&path_string, &separator);90 }91 write!(stdout, "{}{}", path_string, separator)92 }93+94+#[cfg(not(unix))]95+fn print_entry_uncolorized(96+ stdout: &mut StdoutLock,97+ path: &Path,98+ config: &Options,99+) -> io::Result<()> {100+ print_entry_uncolorized_base(stdout, path, config)101+}102+103+#[cfg(unix)]104+fn print_entry_uncolorized(105+ stdout: &mut StdoutLock,106+ path: &Path,107+ config: &Options,108+) -> io::Result<()> {109+ use std::os::unix::ffi::OsStrExt;110+111+ if config.interactive_terminal || config.path_separator.is_some() {112+ // Fall back to the base implementation113+ print_entry_uncolorized_base(stdout, path, config)114+ } else {115+ // Print path as raw bytes, allowing invalid UTF-8 filenames to be passed to other processes116+ let separator = if config.null_separator { b"\0" } else { b"\n" };117+ stdout.write_all(path.as_os_str().as_bytes())?;118+ stdout.write_all(separator)119+ }120+}