lsd icon indicating copy to clipboard operation
lsd copied to clipboard

Shows size power 2 (in MiB), but prints as if it is power 10 (MB)

Open ValdikSS opened this issue 2 years ago • 2 comments

  • os: Fedora 37
  • lsd --version: lsd 0.22.0

lsd shows file size in non-si mebibytes/gibibytes (power of 2), while suggests it is megabytes/gigabytes (SI, power of 10). In other words, the program should print 10 MiB instead of 10 MB.

Expected behavior

$ lsd -la openwrt-22.03.3-x86-generic-generic-squashfs-combined-efi.img
.rw-r--r--. valdikss valdikss 120 M**i**B Wed Jan 18 19:17:34 2023 openwrt-22.03.3-x86-generic-generic-squashfs-combined-efi.img

Actual behavior

$ lsd -la --size=bytes openwrt-22.03.3-x86-generic-generic-squashfs-combined-efi.img
.rw-r--r--. valdikss valdikss 126123520  Wed Jan 18 19:17:34 2023 openwrt-22.03.3-x86-generic-generic-squashfs-combined-efi.img

$ lsd -la openwrt-22.03.3-x86-generic-generic-squashfs-combined-efi.img
.rw-r--r--. valdikss valdikss 120 **MB** Wed Jan 18 19:17:34 2023 openwrt-22.03.3-x86-generic-generic-squashfs-combined-efi.img

126123520 bytes is 126 MB or 120 MiB.

ValdikSS avatar Jan 18 '23 21:01 ValdikSS

Looking at gnu ls, it looks like they also use 1024 by default and uses 1000 when --si flag is provided. That said, the way they round it off is different.

$ truncate -s 126123520 foo

$ lsd -l foo
.rw-rw-r-- 120M now foo

$ ls -lh foo
-rw-rw-r-- 1 meain meain 121M Jan 19 10:29 foo

$ ls -lh --si foo
-rw-rw-r-- 1 meain meain 127M Jan 19 10:29 foo

$ cargo run -- -l foo  # div by 1000
   Compiling lsd v0.23.1 (/home/meain/dev/src/lsd)
    Finished dev [unoptimized + debuginfo] target(s) in 2.27s
     Running `target/debug/lsd -l foo`
.rw-rw-r-- 126M 14 seconds ago foo

From the man page:

       -h, --human-readable
              with -l and -s, print sizes like 1K 234M 2G etc.

       --si   likewise, but use powers of 1000 not 1024

We should also consider supporting the --si flag from gnu ls when we change this behavior.

Adding some references:

  • https://github.com/coreutils/coreutils/blob/d5868df0d0a6bd09387ece41b62b873fd7c201f9/src/ls.c#L3093
  • https://github.com/coreutils/gnulib/blob/356a414e8c15ef3f8cc7b7157427c8ce9a9f7c1b/lib/human.c#L155

meain avatar Jan 19 '23 05:01 meain

I saw a request #826.

frankebel avatar Aug 28 '23 11:08 frankebel