Skip to contentSkip to conversation

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

PATCH Superseded Michael Forney 2 participants 11 messages Jan 28, 2018 – Mar 21, 2018

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

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

From: Michael Forney <[email protected]>

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.

---
 scripts/Makefile.lib    | 3 ++-
 scripts/link-vmlinux.sh | 6 ++++--
 2 files changed, 6 insertions(+), 3 deletions(-)
scripts/Makefile.lib +2 -1
index 7dee1da83e2a..24566d1c10d2 100644
@@ -319,7 +319,8 @@ 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);					\
+	set -- $$(ls -dn $$F);						\
+	fsize=$$5;							\
 	dec_size=$$(expr $$dec_size + $$fsize);				\
 done;									\
 printf "%08x\n" $$dec_size |						\
scripts/link-vmlinux.sh +4 -2
index c0d129d7f430..4a5737647a79 100755
@@ -296,8 +296,10 @@ 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)
+	set -- $(ls -dn .tmp_kallsyms1.o)
+	size1=$5
+	set -- $(ls -dn .tmp_kallsyms2.o)
+	size2=$5
 
 	if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
 		kallsymso=.tmp_kallsyms3.o
Signature · 1 line
2.15.1
#1 lore raw
Quoted 8 lines · Michael Forney

From: Michael Forney <[email protected]>

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.

Forgot to add

Quoted 40 lines
---
 scripts/Makefile.lib    | 3 ++-
 scripts/link-vmlinux.sh | 6 ++++--
 2 files changed, 6 insertions(+), 3 deletions(-)
scripts/Makefile.lib +2 -1
index 7dee1da83e2a..24566d1c10d2 100644
@@ -319,7 +319,8 @@ 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);					\
+	set -- $$(ls -dn $$F);						\
+	fsize=$$5;							\
 	dec_size=$$(expr $$dec_size + $$fsize);				\
 done;									\
 printf "%08x\n" $$dec_size |						\
scripts/link-vmlinux.sh +4 -3
index c0d129d7f430..4a5737647a79 100755
@@ -296,8 +296,10 @@ 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)
+	set -- $(ls -dn .tmp_kallsyms1.o)
+	size1=$5
+	set -- $(ls -dn .tmp_kallsyms2.o)
+	size2=$5
 
 	if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
 		kallsymso=.tmp_kallsyms3.o
--

2.15.1

#2 lore raw

2018-01-30 17:01 GMT+09:00 Michael Forney <[email protected]>:

Quoted 19 lines · Michael Forney
Quoted 8 lines

From: Michael Forney <[email protected]>

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.

Forgot to add

Quoted 5 lines
---
 scripts/Makefile.lib    | 3 ++-
 scripts/link-vmlinux.sh | 6 ++++--
 2 files changed, 6 insertions(+), 3 deletions(-)
Quoted 14 lines
Quoted 5 lines
scripts/Makefile.lib +0 -0
index 7dee1da83e2a..24566d1c10d2 100644

scripts/Makefile.lib dtc-tmp = $(subst $(comma),_,$(dot-target).dts.tmp) · line 326

Quoted 9 lines
scripts/Makefile.lib +2 -1
@@ -319,7 +319,8 @@ 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);                                     \
+     set -- $$(ls -dn $$F);                                          \
+     fsize=$$5;                                                      \
      dec_size=$$(expr $$dec_size + $$fsize);                         \
 done;                                                                        \
 printf "%08x\n" $$dec_size |                                         \

I sometimes see patches that address portability.
But, the resulted code is generally ugly.
(I would not say this is too ugly in this case.)

Also, I see some more instances.
If 'stat' is a problem,
do you have an idea for alternative of "stat -c %Y"?

$ git grep 'stat -c'
Documentation/acpi/ssdt-overlays.txt:dd if=$tmp
of="$EFIVARFS/$name-$guid" bs=$(stat -c %s $tmp)
arch/arm/boot/deflate_xip_data.sh:file_end=$(stat -c "%s" "$XIPIMAGE")
arch/blackfin/boot/install.sh:  local stamp=$(stat -c %Y ${file} 2>/dev/null)
arch/powerpc/boot/wrapper:strip_size=$(stat -c %s $vmz.$$)
tools/testing/selftests/efivarfs/efivarfs.sh:   if [ $(stat -c %s
$file) -ne 5 ]; then
Quoted 21 lines
Quoted 21 lines
scripts/link-vmlinux.sh +4 -3
index c0d129d7f430..4a5737647a79 100755
@@ -296,8 +296,10 @@ 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)
+     set -- $(ls -dn .tmp_kallsyms1.o)
+     size1=$5
+     set -- $(ls -dn .tmp_kallsyms2.o)
+     size2=$5
 
      if [ $size1 -ne $size2 ] || [ -n "${KALLSYMS_EXTRA_PASS}" ]; then
              kallsymso=.tmp_kallsyms3.o
--

2.15.1

Signature · 2 lines
Best Regards
Masahiro Yamada
#3 lore raw
Quoted 8 lines · Masahiro Yamada

I sometimes see patches that address portability.
But, the resulted code is generally ugly.
(I would not say this is too ugly in this case.)

Also, I see some more instances.
If 'stat' is a problem,
do you have an idea for alternative of "stat -c %Y"?

I'm not aware of a portable alternative for that. However, it is only used in a single place for backing up uImage and System.map. If the stat -c %Y command produces no stdout, it uses the .old extension instead which seems like reasonable fallback behavior.

Quoted 9 lines
$ git grep 'stat -c'
Documentation/acpi/ssdt-overlays.txt:dd if=$tmp
of="$EFIVARFS/$name-$guid" bs=$(stat -c %s $tmp)
arch/arm/boot/deflate_xip_data.sh:file_end=$(stat -c "%s" "$XIPIMAGE")
arch/blackfin/boot/install.sh:  local stamp=$(stat -c %Y ${file}
2>/dev/null)
arch/powerpc/boot/wrapper:strip_size=$(stat -c %s $vmz.$$)
tools/testing/selftests/efivarfs/efivarfs.sh:   if [ $(stat -c %s
$file) -ne 5 ]; then

Would you like me to include those other instances of stat -c %s in this patch? I left them out for now because I wasn't sure if they needed to be sent to various other maintainers as separate patches.

#4 lore raw

2018-02-06 18:13 GMT+09:00 Michael Forney <[email protected]>:

Quoted 6 lines · Masahiro Yamada
Quoted 5 lines

I sometimes see patches that address portability.
But, the resulted code is generally ugly.
(I would not say this is too ugly in this case.)

Quoted 8 lines
Quoted 3 lines

Also, I see some more instances.
If 'stat' is a problem,
do you have an idea for alternative of "stat -c %Y"?

I'm not aware of a portable alternative for that. However, it is only used in a single place for backing up uImage and System.map. If the stat -c %Y command produces no stdout, it uses the .old extension instead which seems like reasonable fallback behavior.

Ah, right.

Quoted 10 lines
Quoted 9 lines
$ git grep 'stat -c'
Documentation/acpi/ssdt-overlays.txt:dd if=$tmp
of="$EFIVARFS/$name-$guid" bs=$(stat -c %s $tmp)
arch/arm/boot/deflate_xip_data.sh:file_end=$(stat -c "%s" "$XIPIMAGE")
arch/blackfin/boot/install.sh:  local stamp=$(stat -c %Y ${file}
2>/dev/null)
arch/powerpc/boot/wrapper:strip_size=$(stat -c %s $vmz.$$)
tools/testing/selftests/efivarfs/efivarfs.sh:   if [ $(stat -c %s
$file) -ne 5 ]; then
Quoted 3 lines

Would you like me to include those other instances of stat -c %s in this patch? I left them out for now because I wasn't sure if they needed to be sent to various other maintainers as separate patches.

I want all instances written in the same way
once we make a decision.

BTW, is the output format of ls(1) standardized?
I am not familiar with this area.

Also, I am not a big fan of the mysterious two lines code. (We will have some more if we fix the rest of the instances)

Perhaps, put
set -- $(ls -dn $1)
echo $5

into "scripts/file-size.sh" (any other suitable file name is OK) then, call it like this?

   size1=$($CONFIG_SHELL $srctree/scripts/file-size.sh .tmp_kallsyms1.o)
   size2=$($CONFIG_SHELL $srctree/scripts/file-size.sh .tmp_kallsyms2.o)

Is this less uglier?

Signature · 2 lines
Best Regards
Masahiro Yamada
#5 lore raw
Quoted 10 lines · Masahiro Yamada

2018-02-06 18:13 GMT+09:00 Michael Forney <[email protected]>:

Quoted 3 lines

Would you like me to include those other instances of stat -c %s in this patch? I left them out for now because I wasn't sure if they needed to be sent to various other maintainers as separate patches.

I want all instances written in the same way
once we make a decision.

BTW, is the output format of ls(1) standardized?
I am not familiar with this area.

Yes, it is[0]. The -d 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".

Quoted 8 lines

Also, I am not a big fan of the mysterious two lines code. (We will have some more if we fix the rest of the instances)

Perhaps, put
set -- $(ls -dn $1)
echo $5

Quoted 8 lines

into "scripts/file-size.sh" (any other suitable file name is OK) then, call it like this?

   size1=$($CONFIG_SHELL $srctree/scripts/file-size.sh .tmp_kallsyms1.o)
   size2=$($CONFIG_SHELL $srctree/scripts/file-size.sh .tmp_kallsyms2.o)

Is this less uglier?

This sounds reasonable to me. I will make this change.

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

#6 lore raw

2018-02-06 19:29 GMT+09:00 Michael Forney <[email protected]>:

Quoted 9 lines · Masahiro Yamada
Quoted 8 lines

2018-02-06 18:13 GMT+09:00 Michael Forney <[email protected]>:

Quoted 3 lines

Would you like me to include those other instances of stat -c %s in this patch? I left them out for now because I wasn't sure if they needed to be sent to various other maintainers as separate patches.

I want all instances written in the same way
once we make a decision.

Quoted 8 lines
Quoted 2 lines

BTW, is the output format of ls(1) standardized?
I am not familiar with this area.

Yes, it is[0]. The -d 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>

OK. Please add this evidence with the link
into the commit log.

Quoted 31 lines

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".

Quoted 8 lines

Also, I am not a big fan of the mysterious two lines code. (We will have some more if we fix the rest of the instances)

Perhaps, put
set -- $(ls -dn $1)
echo $5

Quoted 8 lines

into "scripts/file-size.sh" (any other suitable file name is OK) then, call it like this?

   size1=$($CONFIG_SHELL $srctree/scripts/file-size.sh .tmp_kallsyms1.o)
   size2=$($CONFIG_SHELL $srctree/scripts/file-size.sh .tmp_kallsyms2.o)

Is this less uglier?

This sounds reasonable to me. I will make this change.

[0] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/ls.html#tag_20_73_10
--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Signature · 2 lines
Best Regards
Masahiro Yamada
#7 lore raw

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

In reply to Michael Forney

From: Michael Forney <[email protected]>

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

---
Changes in v2:
* Use a wrapper script instead of calling ls(1) directly.
* Also replace a couple of other instances of stat -c '%s'.
* Update description with justification of ls(1) usage.

 arch/arm/boot/deflate_xip_data.sh            | 2 +-
 arch/powerpc/boot/wrapper                    | 2 +-
 scripts/Makefile.lib                         | 2 +-
 scripts/file-size.sh                         | 4 ++++
 scripts/link-vmlinux.sh                      | 4 ++--
 tools/testing/selftests/efivarfs/efivarfs.sh | 2 +-
 6 files changed, 10 insertions(+), 6 deletions(-)
 create mode 100755 scripts/file-size.sh
arch/arm/boot/deflate_xip_data.sh +1 -1
index 1189598a25eb..07adfc734fa3 100755
@@ -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
arch/powerpc/boot/wrapper +1 -1
index 76fe3ccfd381..f9141eaec6ff 100755
@@ -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
scripts/Makefile.lib +1 -1
index 7dee1da83e2a..5f74eafa2daa 100644
@@ -319,7 +319,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 |						\
scripts/file-size.sh new file +4 -0
new file mode 100755
index 000000000000..7eb7423416b5
@@ -0,0 +1,4 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+set -- $(ls -dn "$1")
+printf '%s\n' "$5"
scripts/link-vmlinux.sh +2 -2
index c0d129d7f430..573d2ea88842 100755
@@ -296,8 +296,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
tools/testing/selftests/efivarfs/efivarfs.sh +1 -1
index c6d5790575ae..0bcec427edba 100755
@@ -53,7 +53,7 @@ test_create()
 		exit 1
 	fi
 
-	if [ $(stat -c %s $file) -ne 5 ]; then
+	if [ "$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" "$file")" -ne 5 ]; then
 		echo "$file has invalid size" >&2
 		exit 1
 	fi
Signature · 1 line
2.16.2
#8 lore raw

2018-03-19 9:54 GMT+09:00 Michael Forney <[email protected]>:

Quoted 24 lines

From: Michael Forney <[email protected]>

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).

Quoted 9 lines

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

---
Changes in v2:
* Use a wrapper script instead of calling ls(1) directly.
* Also replace a couple of other instances of stat -c '%s'.
* Update description with justification of ls(1) usage.

Applied to linux-kbuild. Thanks!

Signature · 2 lines
Best Regards
Masahiro Yamada
#9 lore raw
Quoted 36 lines · Masahiro Yamada

2018-03-19 9:54 GMT+09:00 Michael Forney <[email protected]>:

Quoted 27 lines

From: Michael Forney <[email protected]>

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

Quoted 7 lines

---
Changes in v2:
* Use a wrapper script instead of calling ls(1) directly.
* Also replace a couple of other instances of stat -c '%s'.
* Update description with justification of ls(1) usage.

Quoted 1 line

Applied to linux-kbuild. Thanks!

Thanks, Masahiro!

However, I just realized that we probably need to revert the change to tools/testing/selftests/efivarfs/efivarfs.sh since it is called through a separate Makefile system that does not have $CONFIG_SHELL and $srctree defined.

Sorry for the mistake.

#10 lore raw

2018-03-21 15:03 GMT+09:00 Michael Forney <[email protected]>:

Quoted 39 lines · Masahiro Yamada
Quoted 36 lines

2018-03-19 9:54 GMT+09:00 Michael Forney <[email protected]>:

Quoted 27 lines

From: Michael Forney <[email protected]>

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

Quoted 7 lines

---
Changes in v2:
* Use a wrapper script instead of calling ls(1) directly.
* Also replace a couple of other instances of stat -c '%s'.
* Update description with justification of ls(1) usage.

Quoted 1 line

Applied to linux-kbuild. Thanks!

Quoted 8 lines

Thanks, Masahiro!

However, I just realized that we probably need to revert the change to tools/testing/selftests/efivarfs/efivarfs.sh since it is called through a separate Makefile system that does not have $CONFIG_SHELL and $srctree defined.

Sorry for the mistake.

OK, I reverted the offending part.

Signature · 2 lines
Best Regards
Masahiro Yamada