commit:     7f66a7b26c9caae12aaf2cc2bbe13a180a803fb9
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Thu Aug 11 00:09:52 2022 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sat Jun  7 22:54:09 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=7f66a7b2

ecompress: improve the guess_suffix() function

This commit makes some changes to the guest_suffix() function, which are
described herewith.

Localise its variables. Also, localise the '-' parameter so that the
enablement of the "errexit" shell option is scoped to the function.

Don't make use of brace expansion to print a range of numbers. Doing so
requires for the entire range be expanded as a series of words
beforehand. Even for a narrow range of 0..1000, there is a measurable
impact on memory usage. Instead, use a C-style for loop.

Dispense with the clumsy combination of command substitution, echo
invocation and pathname expansion. Instead, iterate over compress?*,
assigning the first existing match to the 'suffix' variable.

Prior to returning, don't print the suffix unless one was discovered,
and do so using printf rather than echo. Further, ensure that the return
value is something other than 0 in the case that no suffix is found.
Note that the caller is now able to invoke die if something goes wrong.

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

 bin/ecompress | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/bin/ecompress b/bin/ecompress
index 389af4be7d..ec74bdfc25 100755
--- a/bin/ecompress
+++ b/bin/ecompress
@@ -90,6 +90,8 @@ do_queue() {
 }
 
 guess_suffix() {
+       local - f i suffix
+
        set -e
 
        tmpdir="${T}"/.ecompress$$.${RANDOM}
@@ -99,19 +101,25 @@ guess_suffix() {
        # We have to fill the file enough so that there is something
        # to compress as some programs will refuse to do compression
        # if it cannot actually compress the file
-       echo {0..1000} > compressme
+       for (( i = 0; i <= 1000; i++ )); do
+               printf '%s ' "${i}"
+       done > compressme
        ${PORTAGE_COMPRESS} ${PORTAGE_COMPRESS_FLAGS} compressme > /dev/null
 
        # If PORTAGE_COMPRESS_FLAGS contains -k then we need to avoid
        # having our glob match the uncompressed file here.
-       suffix=$(echo compressme.*)
-       [[ -z ${suffix} || "${suffix}" == "compressme.*" ]] && \
-               suffix=$(echo compressme*)
-       suffix=${suffix#compressme}
+       for f in compressme?*; do
+               if [[ -e ${f} ]]; then
+                       suffix=${f#compressme}
+                       break
+               fi
+       done
 
        cd /
        rm -rf -- "${tmpdir}"
-       echo "${suffix}"
+
+       set +e
+       [[ ${suffix} ]] && printf '%s\n' "${suffix}"
 }
 
 fix_symlinks() {

Reply via email to