Subject: bug#39092: Bug in coreutils "du"? - ignores files when both a file and a hard link is given

From: Bernhard Voelker <[email-masked]>
Date: Sun, 12 Jan 2020 16:11:42 +0100

tag 39092 notabug
close 39092
stop

I had to take out your email address to be able to reply to this issue:

  An error occurred while sending mail. The mail server responded:
  Requested action not taken: mailbox unavailable
  invalid DNS MX or A/AAAA resource record.
  Please check the message recipient "[email-masked]" and try again.

On 2020-01-11 18:23, Alex wrote:
> Hi guys,
> 
> I noticed that when I explicitly specify a file and a hardlink to that 
> file as du's command line arguments it ignores both of them. Here 
> "kernel", "initramfs" and "System.map" are hardlinks to 
> kernel-genkernel-x86_64-5.4.2-gentoo, 
> initramfs-genkernel-x86_64-5.4.2-gentoo and System.map-5.4.10-gentoo-x86_64.
> 
> 
> 19:56:45 [0] ~ >ls -la /boot/
> total 72281
> drwxr-xr-x  5 root root     1024 Jan 11 19:49 .
> drwxr-xr-x 22 root root     4096 Dec  7 21:00 ..
> -rw-r--r--  1 root root        0 Jan  8 23:58 .e.mount
> -rw-r--r--  1 root root        0 Jan  8 23:58 .keep
> -rw-r--r--  2 root root  3456397 Jan  9 21:48 System.map
> -rw-r--r--  1 root root  3474911 Dec  6 20:22 System.map-5.3.5-gentoo-x86_64
> -rw-r--r--  1 root root  3473259 Jan 11 19:49 
> System.map-5.4.10-gentoo-x86_64
> -rw-r--r--  2 root root  3456397 Jan  9 21:48 
> System.map-genkernel-x86_64-5.4.2-gentoo
> drwxr-xr-x  2 root root     1024 Apr 29  2019 efi
> drwxr-xr-x  7 root root     1024 Jan  9 22:50 grub
> -rw-r--r--  2 root root  8196820 Jan  9 22:15 initramfs
> -rw-r--r--  1 root root 16531780 Oct  9 21:35 
> initramfs-5.3.5-gentoo-x86_64.img
> -rw-r--r--  2 root root  8196820 Jan  9 22:15 
> initramfs-genkernel-x86_64-5.4.2-gentoo
> -rw-r--r--  2 root root  6804640 Jan  9 21:48 kernel
> -rw-r--r--  2 root root  6804640 Jan  9 21:48 
> kernel-genkernel-x86_64-5.4.2-gentoo
> drwx------  2 root root    12288 Apr 29  2019 lost+found
> -rw-r--r--  1 root root  6759584 Dec  6 20:22 vmlinuz-5.3.5-gentoo-x86_64
> -rw-r--r--  1 root root  6829216 Jan 11 19:49 vmlinuz-5.4.10-gentoo-x86_64
> 
> 
> As you can see they are missing from du output:
> 
> 19:56:48 [0] ~ >sudo du -hs /boot/System.map 
> /boot/System.map-5.3.5-gentoo-x86_64 
> /boot/System.map-genkernel-x86_64-5.4.2-gentoo /boot/efi /boot/grub 
> /boot/initramfs /boot/initramfs-5.3.5-gentoo-x86_64.img 
> /boot/initramfs-genkernel-x86_64-5.4.2-gentoo /boot/kernel 
> /boot/kernel-genkernel-x86_64-5.4.2-gentoo /boot/lost+found 
> /boot/vmlinuz-5.3.5-gentoo-x86_64
> 
> 3.3M    /boot/System.map
> 3.4M    /boot/System.map-5.3.5-gentoo-x86_64
> 1.0K    /boot/efi
> 9.7M    /boot/grub
> 7.9M    /boot/initramfs
> 16M    /boot/initramfs-5.3.5-gentoo-x86_64.img
> 6.5M    /boot/kernel
> 12K    /boot/lost+found
> 6.5M    /boot/vmlinuz-5.3.5-gentoo-x86_64
> 
> 
> When I remove the hard link from the arguments the corresponding file 
> appears in the output:
> 
> 19:56:51 [0] ~ >sudo du -hs /boot/System.map 
> /boot/System.map-5.3.5-gentoo-x86_64 
> /boot/System.map-genkernel-x86_64-5.4.2-gentoo /boot/efi /boot/grub 
> /boot/initramfs /boot/initramfs-5.3.5-gentoo-x86_64.img 
> /boot/initramfs-genkernel-x86_64-5.4.2-gentoo 
> /boot/kernel-genkernel-x86_64-5.4.2-gentoo /boot/lost+found 
> /boot/vmlinuz-5.3.5-gentoo-x86_64
> 
> 3.3M    /boot/System.map
> 3.4M    /boot/System.map-5.3.5-gentoo-x86_64
> 1.0K    /boot/efi
> 9.7M    /boot/grub
> 7.9M    /boot/initramfs
> 16M    /boot/initramfs-5.3.5-gentoo-x86_64.img
> 6.5M    /boot/kernel-genkernel-x86_64-5.4.2-gentoo
> 12K    /boot/lost+found
> 6.5M    /boot/vmlinuz-5.3.5-gentoo-x86_64
> 19:57:28 [0] ~ >
> 
> 
> Cheers,
> Alex.

du(1) should count the actual disc usage, and hard links do not
add more to that, so that was actually a bug fix back in coreutils-8.6:

  https://git.sv.gnu.org/cgit/coreutils.git/commit/?id=efe53cc72b59

Discussion starts here:
  https://bugs.gnu.org/6557
  https://lists.gnu.org/r/bug-coreutils/2010-07/msg00015.html

This is intended behavior, and also documented in the Texinfo manual:

  https://www.gnu.org/software/coreutils/du

  If two or more hard links point to the same file, only one of the
  hard links is counted. The file argument order affects which links
  are counted, and changing the argument order may change the numbers
  and entries that du outputs.

I suggest to use the following option to make du(1) also count hard
links if needed:

  ‘-l’
  ‘--count-links’
    Count the size of all files, even if they have appeared already (as a hard link).

As such, I'm hereby marking this as "not-a-bug", and close the issue in
out bug tracker.  However, the discussion about this can continue here,
of course.

Have a nice day,
Berny