Skip to content
Myr

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

4 files+40 added−7 removed

modified:src/filesystem.rs-4 removed
86 unmodified lines
87pub fn strip_current_dir(path: &Path) -> &Path {88 path.strip_prefix(".").unwrap_or(path)89}9091pub fn replace_path_separator<'a>(path: &str, new_path_separator: &str) -> String {92 path.replace(std::path::MAIN_SEPARATOR, &new_path_separator)93}9491#[cfg(test)]92mod tests {93 use super::strip_current_dir;94 use std::path::Path;
15 unmodified lines
modified:src/main.rs+3 added-1 removed
135 unmodified lines
136 // 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));139140 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 };146147 let path_separator = matches.value_of("path-separator").map(|str| str.to_owned());148
91 unmodified lines
240 .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 lines
modified:src/options.rs+3 added
50 unmodified lines
51 /// `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>,5455 /// Whether or not we are writing to an interactive terminal56 pub interactive_terminal: bool,5758 /// 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 lines
modified:src/output.rs+34 added-2 removed
6 unmodified lines
7use ansi_term;8use lscolors::{LsColors, Style};910use crate::exit_codes::ExitCode;11use crate::filesystem::{replace_path_separator, strip_current_dir};11use crate::filesystem::strip_current_dir;12use crate::options::Options;1314pub fn replace_path_separator<'a>(path: &str, new_path_separator: &str) -> String {15 path.replace(std::path::MAIN_SEPARATOR, &new_path_separator)16}1718// TODO: this function is performance critical and can probably be optimized19pub fn print_entry(20 stdout: &mut StdoutLock,21 entry: &PathBuf,
54 unmodified lines
76 }77}7879// TODO: this function is performance critical and can probably be optimized76fn print_entry_uncolorized(80fn print_entry_uncolorized_base(81 stdout: &mut StdoutLock,82 path: &Path,83 config: &Options,84) -> io::Result<()> {
4 unmodified lines
89 *path_string.to_mut() = replace_path_separator(&path_string, &separator);90 }91 write!(stdout, "{}{}", path_string, separator)92}9394#[cfg(not(unix))]95fn 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}102103#[cfg(unix)]104fn print_entry_uncolorized(105 stdout: &mut StdoutLock,106 path: &Path,107 config: &Options,108) -> io::Result<()> {109 use std::os::unix::ffi::OsStrExt;110111 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}