Просмотр исходного кода

Merge pull request #539 from eza-community/fix-symlink-size

fix(symlink): don’t display target’s size if we’re not dereferencing
Christina Sørensen 2 лет назад
Родитель
Сommit
cd1d7d9e82

+ 16 - 16
src/fs/file.rs

@@ -432,12 +432,18 @@ impl<'dir> File<'dir> {
     /// This actual size the file takes up on disk, in bytes.
     #[cfg(unix)]
     pub fn blocksize(&self) -> f::Blocksize {
-        if self.is_file() || self.is_link() {
+        if self.deref_links && self.is_link() {
+            match self.link_target() {
+                FileTarget::Ok(f) => f.blocksize(),
+                _ => f::Blocksize::None,
+            }
+        } else if self.is_file() {
             // Note that metadata.blocks returns the number of blocks
             // for 512 byte blocks according to the POSIX standard
             // even though the physical block size may be different.
             f::Blocksize::Some(self.metadata.blocks() * 512)
         } else {
+            // directory or symlinks
             f::Blocksize::None
         }
     }
@@ -481,14 +487,11 @@ impl<'dir> File<'dir> {
     /// itself.
     #[cfg(unix)]
     pub fn size(&self) -> f::Size {
-        if self.is_link() {
-            let target = self.link_target();
-            if let FileTarget::Ok(target) = target {
-                return target.size();
+        if self.deref_links && self.is_link() {
+            match self.link_target() {
+                FileTarget::Ok(f) => f.size(),
+                _ => f::Size::None,
             }
-        }
-        if self.is_directory() {
-            f::Size::None
         } else if self.is_char_device() || self.is_block_device() {
             let device_id = self.metadata.rdev();
 
@@ -497,20 +500,17 @@ impl<'dir> File<'dir> {
             // the "as u32" cast are not needed.  We turn off the warning to
             // allow it to compile cleanly on Linux.
             #[allow(trivial_numeric_casts)]
-            #[allow(clippy::unnecessary_cast)]
-            #[allow(clippy::useless_conversion)]
+            #[allow(clippy::unnecessary_cast, clippy::useless_conversion)]
             f::Size::DeviceIDs(f::DeviceIDs {
                 // SAFETY: Calling libc function to decompose the device_id
                 major: unsafe { libc::major(device_id.try_into().unwrap()) } as u32,
                 minor: unsafe { libc::minor(device_id.try_into().unwrap()) } as u32,
             })
-        } else if self.is_link() && self.deref_links {
-            match self.link_target() {
-                FileTarget::Ok(f) => f.size(),
-                _ => f::Size::None,
-            }
-        } else {
+        } else if self.is_file() {
             f::Size::Some(self.metadata.len())
+        } else {
+            // directory and symlink
+            f::Size::None
         }
     }
 

+ 2 - 0
tests/cmd/long_blocksize_dereference_symlink.toml

@@ -0,0 +1,2 @@
+bin.name = "eza"
+args = "tests/itest/vagrant/debug --long --blocksize --dereference"

+ 0 - 0
tests/cmd/long_blocksize_dereference_symlink_nix.stderr


+ 3 - 0
tests/cmd/long_blocksize_dereference_symlink_nix.stdout

@@ -0,0 +1,3 @@
+.rw-r--r-- 0 ariasuni 20 oct.  15:38 a
+lrw-r--r-- 0 ariasuni 20 oct.  15:38 symlink -> a
+---------- - -        -              symlink-broken -> ./b

+ 2 - 0
tests/cmd/long_blocksize_symlink.toml

@@ -0,0 +1,2 @@
+bin.name = "eza"
+args = "tests/itest/vagrant/debug --long --blocksize"

+ 0 - 0
tests/cmd/long_blocksize_symlink_nix.stderr


+ 3 - 0
tests/cmd/long_blocksize_symlink_nix.stdout

@@ -0,0 +1,3 @@
+.rw-r--r-- 0 ariasuni 20 oct.  15:38 a
+lrwxrwxrwx - ariasuni 17 oct.  18:35 symlink -> a
+lrwxrwxrwx - ariasuni 20 oct.  15:38 symlink-broken -> ./b

+ 3 - 1
tests/cmd/recursive_long_unix.stdout

@@ -33,7 +33,9 @@ dev
 log
 
 tests/itest/vagrant/debug:
-symlinking -> a
+a
+symlink -> a
+symlink-broken -> ./b
 
 tests/itest/vagrant/dev:
 main.bf

+ 3 - 1
tests/cmd/recursive_unix.stdout

@@ -33,7 +33,9 @@ dev
 log
 
 tests/itest/vagrant/debug:
-symlinking -> a
+a
+symlink -> a
+symlink-broken -> ./b
 
 tests/itest/vagrant/dev:
 main.bf

+ 3 - 1
tests/cmd/tree_long_unix.stdout

@@ -24,7 +24,9 @@ tests/itest
 ├── q
 └── vagrant
    ├── debug
-   │  └── symlinking -> a
+   │  ├── a
+   │  ├── symlink -> a
+   │  └── symlink-broken -> ./b
    ├── dev
    │  └── main.bf
    └── log

+ 3 - 1
tests/cmd/tree_unix.stdout

@@ -24,7 +24,9 @@ tests/itest
 ├── q
 └── vagrant
    ├── debug
-   │  └── symlinking -> a
+   │  ├── a
+   │  ├── symlink -> a
+   │  └── symlink-broken -> ./b
    ├── dev
    │  └── main.bf
    └── log

+ 0 - 0
tests/itest/vagrant/debug/a


+ 0 - 0
tests/itest/vagrant/debug/symlinking → tests/itest/vagrant/debug/symlink


+ 1 - 0
tests/itest/vagrant/debug/symlink-broken

@@ -0,0 +1 @@
+./b