Skip to content

Commit

Permalink
kbuild: Use ls(1) instead of stat(1) to obtain file size
Browse files Browse the repository at this point in the history
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 <[email protected]>
Signed-off-by: Masahiro Yamada <[email protected]>
  • Loading branch information
Michael Forney authored and masahir0y committed Mar 25, 2018
1 parent 3fdc7d3 commit a670b0b
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 5 deletions.
2 changes: 1 addition & 1 deletion arch/arm/boot/deflate_xip_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ data_start=$(($__data_loc - $base_offset))
data_end=$(($_edata_loc - $base_offset))

# Make sure data occupies the last part of the file.
file_end=$(stat -c "%s" "$XIPIMAGE")
file_end=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$XIPIMAGE")
if [ "$file_end" != "$data_end" ]; then
printf "end of xipImage doesn't match with _edata_loc (%#x vs %#x)\n" \
$(($file_end + $base_offset)) $_edata_loc 2>&1
Expand Down
2 changes: 1 addition & 1 deletion arch/powerpc/boot/wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ vmz="$tmpdir/`basename \"$kernel\"`.$ext"

# Calculate the vmlinux.strip size
${CROSS}objcopy $objflags "$kernel" "$vmz.$$"
strip_size=$(stat -c %s $vmz.$$)
strip_size=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$vmz.$$")

if [ -z "$cacheit" -o ! -f "$vmz$compression" -o "$vmz$compression" -ot "$kernel" ]; then
# recompress the image if we need to
Expand Down
2 changes: 1 addition & 1 deletion scripts/Makefile.lib
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ dtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp)
size_append = printf $(shell \
dec_size=0; \
for F in $1; do \
fsize=$$(stat -c "%s" $$F); \
fsize=$$($(CONFIG_SHELL) $(srctree)/scripts/file-size.sh $$F); \
dec_size=$$(expr $$dec_size + $$fsize); \
done; \
printf "%08x\n" $$dec_size | \
Expand Down
4 changes: 4 additions & 0 deletions scripts/file-size.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
set -- $(ls -dn "$1")
printf '%s\n' "$5"
4 changes: 2 additions & 2 deletions scripts/link-vmlinux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ if [ -n "${CONFIG_KALLSYMS}" ]; then
kallsyms .tmp_vmlinux2 .tmp_kallsyms2.o

# step 3
size1=$(stat -c "%s" .tmp_kallsyms1.o)
size2=$(stat -c "%s" .tmp_kallsyms2.o)
size1=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms1.o)
size2=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" .tmp_kallsyms2.o)

if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
kallsymso=.tmp_kallsyms3.o
Expand Down

0 comments on commit a670b0b

Please sign in to comment.