commit:     508a79cedd09079174299239afb4e12e5470b129
Author:     Kerin Millar <kfm <AT> plushkava <DOT> net>
AuthorDate: Sat Jun  7 11:49:56 2025 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Sat Jun  7 22:54:12 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=508a79ce

ecompress: have guess_suffix() use a RETURN trap to purge tmpdir

Now that the guess_suffix() function refrains from enabling the errexit
option, it is possible to reliably employ a RETURN trap. Do so, thereby
guaranteeing the destruction of the temporary directory upon returning.

Also, localise the 'tmpdir' variable and refrain from needlessly
switching to the / directory. That the current working directory changes
is unimportant because the function is only ever called from a subshell.

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

 bin/ecompress | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/bin/ecompress b/bin/ecompress
index 8577c27168..2b7bca1629 100755
--- a/bin/ecompress
+++ b/bin/ecompress
@@ -102,7 +102,9 @@ do_queue() {
 }
 
 guess_suffix() {
-       local f i suffix
+       local f i suffix tmpdir
+
+       trap 'rm -rf -- "${tmpdir}"' RETURN
 
        tmpdir=${T}/.ecompress$$.${RANDOM}
        mkdir -- "${tmpdir}" \
@@ -127,10 +129,6 @@ guess_suffix() {
                fi
        done
 
-       cd / \
-       && rm -rf -- "${tmpdir}" \
-       || return
-
        [[ ${suffix} ]] && printf '%s\n' "${suffix}"
 }
 

Reply via email to