commit:     b381d3533901b03ca50329d020c51c82757cdb5e
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sat Jul  5 22:23:07 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sun Jul 13 04:19:04 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=b381d353

estrip: avoid repeated PATH searches for getfattr(1) and stat(1)

Presently, both get_node_number() and dump_xattrs() are implemented as
self-redeclaring functions, with the former testing whether the value of
the 'USERLAND' portage variable is "BSD", and the latter testing whether
getfattr(1) exists as a command.

However, given that both of these functions are called from within
command substitutions, they will perform their respective tests - and
redeclare themselves - on each and every occasion that they are called.
Further, repeated PATH searches will be performed for getfattr(1) and
stat(1); partly because the cache employed for Command Search and
Execution ends up being populated only after bash has forked, but also
because the cache can only contain positive entries (found paths by
utility name), not negative entries (unfound paths by utility name).

Though a minor issue, address it by declaring both functions only once,
at the point that the estrip script is initially parsed. Likewise for
the restore_attrs() function, so as to maintain a consistent style.

Fixes: e7c98eccdac4dc1e5a7de9e4048a37ea3756327d
Signed-off-by: Kerin Millar <kfm <AT> plushkava.net>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 bin/estrip | 74 ++++++++++++++++++++++++++------------------------------------
 1 file changed, 31 insertions(+), 43 deletions(-)

diff --git a/bin/estrip b/bin/estrip
index 67ceb4974f..3b2e4dfc62 100755
--- a/bin/estrip
+++ b/bin/estrip
@@ -277,51 +277,39 @@ process_ar() {
        fi
 }
 
-get_inode_number() {
-       if  [[ ${USERLAND} == BSD ]]; then
-               get_inode_number() {
-                       stat -f '%i' "$1"
-               }
-       else
-               get_inode_number() {
-                       stat -c '%i' "$1"
-               }
-       fi
-
-       get_inode_number "$@"
-}
-
-dump_xattrs() {
-       if hash getfattr 2>/dev/null; then
-               dump_xattrs() {
-                       getfattr -d -m - --absolute-names "$1"
-               }
-       else
-               dump_xattrs() {
-                       PYTHONPATH=${PORTAGE_PYTHONPATH:-${PORTAGE_PYM_PATH}} \
-                       "${PORTAGE_PYTHON:-/usr/bin/python}" \
-                       "${PORTAGE_BIN_PATH}/xattr-helper.py" --dump < <(echo 
-n "$1")
-               }
-       fi
-
-       dump_xattrs "$@"
-}
+if  [[ ${USERLAND} == BSD ]]; then
+       get_inode_number() {
+               stat -f '%i' "$1"
+       }
+else
+       get_inode_number() {
+               stat -c '%i' "$1"
+       }
+fi
 
-restore_xattrs() {
-       if hash setfattr 2>/dev/null; then
-               restore_xattrs() {
-                       setfattr --restore=-
-               }
-       else
-               restore_xattrs() {
-                       PYTHONPATH=${PORTAGE_PYTHONPATH:-${PORTAGE_PYM_PATH}} \
-                       "${PORTAGE_PYTHON:-/usr/bin/python}" \
-                       "${PORTAGE_BIN_PATH}/xattr-helper.py" --restore
-               }
-       fi
+if hash getfattr 2>/dev/null; then
+       dump_xattrs() {
+               getfattr -d -m - --absolute-names "$1"
+       }
+else
+       dump_xattrs() {
+               PYTHONPATH=${PORTAGE_PYTHONPATH:-${PORTAGE_PYM_PATH}} \
+               "${PORTAGE_PYTHON:-/usr/bin/python}" \
+               "${PORTAGE_BIN_PATH}/xattr-helper.py" --dump < <(echo -n "$1")
+       }
+fi
 
-       restore_xattrs "$@"
-}
+if hash setfattr 2>/dev/null; then
+       restore_xattrs() {
+               setfattr --restore=-
+       }
+else
+       restore_xattrs() {
+               PYTHONPATH=${PORTAGE_PYTHONPATH:-${PORTAGE_PYM_PATH}} \
+               "${PORTAGE_PYTHON:-/usr/bin/python}" \
+               "${PORTAGE_BIN_PATH}/xattr-helper.py" --restore
+       }
+fi
 
 do_ignore() {
        local -a skip_dirs

Reply via email to