Use absolute paths if root dir is an absolute path, fixes #40
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by sharkdp · committed by sharkdp · parent 1b1f644 · Browse files · 2 files·+17 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
2 files+17 added−7 removed
- tests:+6 added−0 removed
- other:+11 added−7 removed
Provenance
- Commit
54f33e5- Tree
24093f9- Author
sharkdp <davidpeter@web.de>- Committer
sharkdp <davidpeter@web.de>- Parent
1b1f644
2 files changed+17 added−7 removed
modified:src/main.rs+11 added-7 removed
9 unmodified lines10 11 use std::borrow::Cow;12 use std::env;13 use std::error::Error;14-use std::fs;14 use std::io::Write;15 use std::ops::Deref;16 #[cfg(target_family = "unix")]17 use std::os::unix::fs::PermissionsExt;85 unmodified lines103 for component in entry.components() {104 let comp_str = match component {105 Component::Normal(p) => p.to_string_lossy(),106 Component::ParentDir => Cow::from(PARENT_DIR),108- _ => error("Error: unexpected path component")107+ _ => error("Error: unexpected path component.")108 };109 110 component_path.push(Path::new(comp_str.deref()));111 143 unmodified lines255 256 // Get the current working directory257 let current_dir_buf = match env::current_dir() {258 Ok(cd) => cd,260- Err(_) => error("Error: could not get current directory!")259+ Err(_) => error("Error: could not get current directory.")260 };261 let current_dir = current_dir_buf.as_path();262 263 // Get the root directory for the search264+ let mut root_dir_is_absolute = false;265 let root_dir_buf = if let Some(rd) = matches.value_of("path") {266+ let path = Path::new(rd);267+268+ root_dir_is_absolute = path.is_absolute();269+266- fs::canonicalize(rd).unwrap_or_else(267- |_| error(&format!("Error: could not find directory '{}'", rd))270+ path.canonicalize().unwrap_or_else(271+ |_| error(&format!("Error: could not find directory '{}'.", rd))272 )273 } else {274 current_dir_buf.clone()275 };276 277 if !root_dir_buf.is_dir() {274- error(&format!("Error: '{}' is not a directory", root_dir_buf.to_string_lossy()));278+ error(&format!("Error: '{}' is not a directory.", root_dir_buf.to_string_lossy()));279 }280 281 let root_dir = root_dir_buf.as_path();282 24 unmodified lines307 read_ignore: !matches.is_present("no-ignore"),308 follow_links: matches.is_present("follow"),309 max_depth: matches.value_of("depth")310 .and_then(|ds| usize::from_str_radix(ds, 10).ok()),307- path_display: if matches.is_present("absolute-path") {311+ path_display: if matches.is_present("absolute-path") || root_dir_is_absolute {312 PathDisplay::Absolute313 } else {314 PathDisplay::Relative315 },16 unmodified linesmodified:tests/test.sh+6 added
195 unmodified lines196 $root/one/two/c.foo197 $root/one/two/C.Foo198 $root/one/two/three/d.foo199 $root/one/two/three/directory_foo" --absolute-path foo200+expect "$root/a.foo201+$root/one/b.foo202+$root/one/two/c.foo203+$root/one/two/C.Foo204+$root/one/two/three/d.foo205+$root/one/two/three/directory_foo" foo "$root"206 207 208 suite "Invalid UTF-8"209 touch "$(printf 'test-invalid-utf8-\xc3.txt')"4 unmodified lines