Don't prepend current path to symlink targets

It's confusing, and `ls` doesn't do this either. We're not prepending
the current path to all of the directory entries, and the user is going
to interpret the symlink target as relative to the directory containing
the symlink.
This commit is contained in:
Kevin Ballard 2017-04-25 16:06:26 -07:00 committed by Kevin Ballard
parent 0c69eeca07
commit f8624ed308
3 changed files with 15 additions and 11 deletions

View File

@ -169,28 +169,32 @@ impl<'dir> File<'dir> {
Err(e) => return FileTarget::Err(e), Err(e) => return FileTarget::Err(e),
}; };
let target_path = match self.dir { let (metadata, ext) = {
Some(dir) => dir.join(&*path), let target_path_ = match self.dir {
None => path Some(dir) if dir.path != Path::new(".") => Some(dir.join(&*path)),
_ => None
};
let target_path = target_path_.as_ref().unwrap_or(&path);
// Use plain `metadata` instead of `symlink_metadata` - we *want* to follow links.
(fs::metadata(&target_path), ext(&target_path))
}; };
let filename = match target_path.components().next_back() { let filename = match path.components().next_back() {
Some(comp) => comp.as_os_str().to_string_lossy().to_string(), Some(comp) => comp.as_os_str().to_string_lossy().to_string(),
None => String::new(), None => String::new(),
}; };
// Use plain `metadata` instead of `symlink_metadata` - we *want* to follow links. if let Ok(metadata) = metadata {
if let Ok(metadata) = fs::metadata(&target_path) {
FileTarget::Ok(File { FileTarget::Ok(File {
path: target_path.to_path_buf(), path: path,
dir: self.dir, dir: self.dir,
metadata: metadata, metadata: metadata,
ext: ext(&target_path), ext: ext,
name: filename, name: filename,
}) })
} }
else { else {
FileTarget::Broken(target_path) FileTarget::Broken(path)
} }
} }

View File

@ -1,4 +1,4 @@
broken -> /testcases/links/nowhere broken -> nowhere
forbidden -> /proc/1/root forbidden -> /proc/1/root
root -> / root -> /
usr -> /usr usr -> /usr

View File

@ -1,5 +1,5 @@
/testcases/links /testcases/links
├── broken -> /testcases/links/nowhere ├── broken -> nowhere
│ └── <No such file or directory (os error 2)> │ └── <No such file or directory (os error 2)>
├── forbidden -> /proc/1/root ├── forbidden -> /proc/1/root
│ └── <Permission denied (os error 13)> │ └── <Permission denied (os error 13)>