Subject: bug#48960: stat v8.30 - device number in decimal shown as 16bit number instead of to converted 8bit

From: Pádraig Brady <[email-masked]>
Date: Sat, 12 Jun 2021 16:19:28 +0100

On 11/06/2021 18:11, Paul Eggert wrote:
> The stat output is confusing in other ways. For example:
> 
> 507-day $ ls -ld .; stat . | grep Device
> drwxr-xr-x 4 eggert eggert 12288 May  5 14:48 .
> Device: 10300h/66304d	Inode: 70388429    Links: 4
> 508-day $ ls -ld /dev/ptp0; stat /dev/ptp0 | grep Device
> crw------- 1 root root 246, 0 Jun  3 13:09 /dev/ptp0
> Device: 5h/5d	Inode: 321         Links: 1     Device type: f6,0
> 
> As you write, that "66304d" is useless on my platform, and the "5h/5d"
> uses a completely different notation from the "f6,0".
> 
> I suggest that we change the behavior of both "Device:" and "Device
> type:" to be consistent with that of 'ls', so that the output becomes:
> 
> 507-day $ ls -ld .; stat . | grep Device
> drwxr-xr-x 4 eggert eggert 12288 May  5 14:48 .
> Device: 259, 0	Inode: 70388429    Links: 4
> 508-day $ ls -ld /dev/ptp0; stat /dev/ptp0 | grep Device
> crw------- 1 root root 246, 0 Jun  3 13:09 /dev/ptp0
> Device: 0, 5	Inode: 321         Links: 1     Device type: 246, 0

Yes I agree with this.
I.e. it's most useful to present decomposed decimal IDs.

What we have now in format specifiers is also inconsistent/incomplete:

  %d     (composed) device number in decimal (st_dev)
  %D     (composed) device number in hex (st_dev)
  %t     major device type in hex, for device special files (st_rdev)
  %T     minor device type in hex, for device special files (st_rdev)

I.e. no decomposed st_dev, or no decimal st_rdev.
I think it's more important to provide decomposed numbers,
rather than hex representations (especially decomposed hex).
It's probably best not to change any of the above format specifiers though
for compat reasons. %d while not that useful in isolation, may be used
to distinguish files or devices, which might be persisted in manifests etc.

We could take the FreeBSD approach which is:

   %Hd  major device number in decimal (st_dev)
   %Ld  minor device number in decimal (st_dev)
   %Hr  major device type in decimal (st_rdev)
   %Lr  minor device type in decimal (st_rdev)

Note I'd be inclined to not have a space between major,minor in default stat output,
as there should be no ambiguity with locale formatted numbers, which could be
the case with ls. For example, my ls alias uses thousands grouping with:
alias ls="BLOCK_SIZE=\'1 ls --color=auto".
I.e. the default format would from stat would be
Device: %Hd,%Ld  ..... Device type: %Hr,%Lr

For consistency if we provided the above we should also probably provide:

  %r     (composed) device type in decimal (st_rdev)
  %R     (composed) device type in hex (st_rdev)

cheers,
Pádraig