Skip to content

Commit

Permalink
Don't prepend current path to symlink targets
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Kevin Ballard authored and lilyball committed Apr 29, 2017
1 parent 0c69eec commit f8624ed
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
22 changes: 13 additions & 9 deletions src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,28 +169,32 @@ impl<'dir> File<'dir> {
Err(e) => return FileTarget::Err(e),
};

let target_path = match self.dir {
Some(dir) => dir.join(&*path),
None => path
let (metadata, ext) = {
let target_path_ = match self.dir {
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(),
None => String::new(),
};

// Use plain `metadata` instead of `symlink_metadata` - we *want* to follow links.
if let Ok(metadata) = fs::metadata(&target_path) {
if let Ok(metadata) = metadata {
FileTarget::Ok(File {
path: target_path.to_path_buf(),
path: path,
dir: self.dir,
metadata: metadata,
ext: ext(&target_path),
ext: ext,
name: filename,
})
}
else {
FileTarget::Broken(target_path)
FileTarget::Broken(path)
}
}

Expand Down
2 changes: 1 addition & 1 deletion xtests/links_1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
broken -> /testcases/links/nowhere
broken -> nowhere
forbidden -> /proc/1/root
root -> /
usr -> /usr
2 changes: 1 addition & 1 deletion xtests/links_T
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/testcases/links
├── broken -> /testcases/links/nowhere
├── broken -> nowhere
│ └── <No such file or directory (os error 2)>
├── forbidden -> /proc/1/root
│ └── <Permission denied (os error 13)>
Expand Down

0 comments on commit f8624ed

Please sign in to comment.