mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Subject: [PATCH 02/18] kbuild: Use ls(1) instead of stat(1) to obtain file size
Date: Mon, 27 May 2019 11:57:28 +0200	[thread overview]
Message-ID: <20190527095744.5923-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20190527095744.5923-1-a.fatoum@pengutronix.de>

From: Michael Forney <forney@google.com>

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>
[afa: imported script and adjusted barebox stat(1) callsites]
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 defaultenv/Makefile  | 3 ++-
 scripts/Makefile.lib | 4 ++--
 scripts/file-size.sh | 4 ++++
 scripts/gen-dtb-s    | 4 ++--
 4 files changed, 10 insertions(+), 5 deletions(-)
 create mode 100755 scripts/file-size.sh

diff --git a/defaultenv/Makefile b/defaultenv/Makefile
index 950ac29a3cee..e030355a4052 100644
--- a/defaultenv/Makefile
+++ b/defaultenv/Makefile
@@ -20,7 +20,8 @@ $(obj)/barebox_default_env: FORCE
 
 quiet_cmd_env_h = ENVH    $@
 cmd_env_h = cat $< | (cd $(obj) && $(objtree)/scripts/bin2c "__aligned(4) default_environment") > $@; \
-	echo "static const int default_environment_uncompress_size=`stat -c%s $(obj)/barebox_default_env`;" >> $@
+	echo "static const int default_environment_uncompress_size=`${CONFIG_SHELL} \"${srctree}/scripts/file-size.sh\" $(obj)/barebox_default_env`;" >> $@
+
 
 $(obj)/barebox_default_env.h: $(obj)/barebox_default_env$(DEFAULT_COMPRESSION_SUFFIX) FORCE
 	$(call if_changed,env_h)
diff --git a/scripts/Makefile.lib b/scripts/Makefile.lib
index 95eaf522abc9..87bff2d296e3 100644
--- a/scripts/Makefile.lib
+++ b/scripts/Makefile.lib
@@ -341,7 +341,7 @@ cmd_env=$(srctree)/scripts/genenv $(srctree) $(objtree) $@ $<
 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 |						\
@@ -446,7 +446,7 @@ quiet_cmd_check_size = CHKSIZE $2
 # Check size of a file
 quiet_cmd_check_file_size = CHKFILESIZE $2
       cmd_check_file_size = set -e;					\
-	size=`stat -c%s $2`;						\
+	size=`${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" $2`;	\
 	max_size=`printf "%d" $3`;					\
 	if [ $$size -gt $$max_size ] ;					\
 	then								\
diff --git a/scripts/file-size.sh b/scripts/file-size.sh
new file mode 100755
index 000000000000..7eb7423416b5
--- /dev/null
+++ b/scripts/file-size.sh
@@ -0,0 +1,4 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+set -- $(ls -dn "$1")
+printf '%s\n' "$5"
diff --git a/scripts/gen-dtb-s b/scripts/gen-dtb-s
index 307b1f68667e..b2dd253c274f 100755
--- a/scripts/gen-dtb-s
+++ b/scripts/gen-dtb-s
@@ -55,8 +55,8 @@ lzop -f -9 $dtb -o $dtb.lzo
 if [ $? != 0 ]; then
 	exit 1
 fi
-compressed=$(stat $dtb.lzo -c "%s")
-uncompressed=$(stat $dtb -c "%s")
+compressed=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" $dtb.lzo)
+uncompressed=$(${CONFIG_SHELL} "${srctree}/scripts/file-size.sh" $dtb)
 
 echo ".section .dtbz.rodata.${name},\"a\""
 echo ".balign STRUCT_ALIGNMENT"
-- 
2.20.1


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2019-05-27  9:57 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-27  9:57 [PATCH 00/18] scripts: enable compilation on macOS Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 01/18] kbuild: suppress warnings from 'getconf LFS_*' Ahmad Fatoum
2019-05-27  9:57 ` Ahmad Fatoum [this message]
2019-05-27  9:57 ` [PATCH 03/18] scripts: extract symbol offsets using target, not host, nm Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 04/18] scripts: includes: restrict strlcpy prototype to glibc Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 05/18] scripts: compiler.h: don't include <stdint.h> twice Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 06/18] scripts: don't depend on system <asm/types.h> Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 07/18] scripts: bareboxcrc32: remove usage of loff_t Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 08/18] scripts: bareboximd: " Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 09/18] scripts: compiler.h: add endianness helpers for macOS Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 10/18] scripts: use "compiler.h" endianness helpers Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 11/18] scripts: mkimage: s/fdatasync/fsync/ on macOS & OpenBSD Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 12/18] scripts: removes uses of <asm*/errno.h> in favor of <errno.h> Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 13/18] scripts: omap3-usb-loader: don't depend on unportable le32toh Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 14/18] scripts: omap3-usb-loader: drop unneeded header Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 15/18] scripts: imx-usb-loader: don't depend on unportable headers Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 16/18] scripts: kwbimage: fix build with non-glibc systems Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 17/18] scripts: compiler.h: use Linux <endian.h> as default Ahmad Fatoum
2019-05-27  9:57 ` [PATCH 18/18] scripts: compiler.h: support BSDs as well Ahmad Fatoum
2019-05-28  8:36 ` [PATCH 00/18] scripts: enable compilation on macOS Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190527095744.5923-3-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox