Merge pull request #169 from kballard/symlink_leading_path

Don't prepend current path to symlink targets
This commit is contained in:
Benjamin Sago 2017-04-30 11:13:00 +01:00 committed by GitHub
commit 91ad09e188
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)>