Skip to content
Myr

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

2 files+17 added−7 removed

modified:src/main.rs+11 added-7 removed
9 unmodified lines
1011use std::borrow::Cow;12use std::env;13use std::error::Error;14use std::fs;14use std::io::Write;15use std::ops::Deref;16#[cfg(target_family = "unix")]17use std::os::unix::fs::PermissionsExt;
85 unmodified lines
103 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 };109110 component_path.push(Path::new(comp_str.deref()));111
143 unmodified lines
255256 // 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();262263 // 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);267268 root_dir_is_absolute = path.is_absolute();269266 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 };276277 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 }280281 let root_dir = root_dir_buf.as_path();282
24 unmodified lines
307 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 lines
modified:tests/test.sh+6 added
195 unmodified lines
196$root/one/two/c.foo197$root/one/two/C.Foo198$root/one/two/three/d.foo199$root/one/two/three/directory_foo" --absolute-path foo200expect "$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"206207208suite "Invalid UTF-8"209touch "$(printf 'test-invalid-utf8-\xc3.txt')"
4 unmodified lines