Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kbuild: Use ls(1) instead of stat(1) to obtain file size

stat(1) is not standardized and different implementations have their own
(conflicting) flags for querying the size of a file.

ls(1) provides the same information (value of st.st_size) in the 5th
column, except when the file is a character or block device. This output
is standardized[0]. The -n option turns on -l, which writes lines
formatted like

"%s %u %s %s %u %s %s\n", <file mode>, <number of links>,
<owner name>, <group name>, <size>, <date and time>,
<pathname>

but instead of writing the <owner name> and <group name>, it writes the
numeric owner and group IDs (this avoids /etc/passwd and /etc/group
lookups as well as potential field splitting issues).

The <size> field is specified as "the value that would be returned for
the file in the st_size field of struct stat".

To avoid duplicating logic in several locations in the tree, create
scripts/file-size.sh and update callers to use that instead of stat(1).

[0] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html#tag_20_73_10

Signed-off-by: Michael Forney <forney@google.com>
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>

authored by

Michael Forney and committed by
Masahiro Yamada
a670b0b4 3fdc7d3f

+9 -5
+1 -1
arch/arm/boot/deflate_xip_data.sh
··· 45 45 data_end=$(($_edata_loc - $base_offset)) 46 46 47 47 # Make sure data occupies the last part of the file. 48 - file_end=$(stat -c "%s" "$XIPIMAGE") 48 + file_end=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$XIPIMAGE") 49 49 if [ "$file_end" != "$data_end" ]; then 50 50 printf "end of xipImage doesn't match with _edata_loc (%#x vs %#x)\n" \ 51 51 $(($file_end + $base_offset)) $_edata_loc 2>&1
+1 -1
arch/powerpc/boot/wrapper
··· 355 355 356 356 # Calculate the vmlinux.strip size 357 357 ${CROSS}objcopy $objflags "$kernel" "$vmz.$$" 358 - strip_size=$(stat -c %s $vmz.$$) 358 + strip_size=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$vmz.$$") 359 359 360 360 if [ -z "$cacheit" -o ! -f "$vmz$compression" -o "$vmz$compression" -ot "$kernel" ]; then 361 361 # recompress the image if we need to
+1 -1
scripts/Makefile.lib
··· 329 329 size_append = printf $(shell \ 330 330 dec_size=0; \ 331 331 for F in $1; do \ 332 - fsize=$$(stat -c "%s" $$F); \ 332 + fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F); \ 333 333 dec_size=$$(expr $$dec_size + $$fsize); \ 334 334 done; \ 335 335 printf "%08x\n" $$dec_size | \
+4
scripts/file-size.sh
··· 1 + #!/bin/sh 2 + # SPDX-License-Identifier: GPL-2.0 3 + set -- $(ls -dn "$1") 4 + printf '%s\n' "$5"