소스 검색

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.
Kevin Ballard 8 년 전
부모
커밋
f8624ed308
3개의 변경된 파일15개의 추가작업 그리고 11개의 파일을 삭제
  1. 13 9
      src/fs/file.rs
  2. 1 1
      xtests/links_1
  3. 1 1
      xtests/links_T

+ 13 - 9
src/fs/file.rs

@@ -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)
         }
     }
 

+ 1 - 1
xtests/links_1

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

+ 1 - 1
xtests/links_T

@@ -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)>