Repository: termux/termux-packages
Pull request #23530: fix(ci): fixing package cleanup to work with toybox df.
Author: login: twaik
Created: 2025-02-27T16:42:34Z | Merged: 2025-03-04T05:10:03Z

Fixes on-device builds.


Diff:
diff --git a/.github/workflows/packages.yml b/.github/workflows/packages.yml
index 2617e8ce645..7520dcf60ab 100644
--- a/.github/workflows/packages.yml
+++ b/.github/workflows/packages.yml
@@ -228,9 +228,9 @@ jobs:
         done
 
         if [ "$DOCKER_BUILD" == 'false' ]; then
-          NDK=$ANDROID_NDK ANDROID_HOME=$ANDROID_SDK_ROOT ./build-package.sh -I -a ${{ matrix.target_arch }} $packages
+          NDK=$ANDROID_NDK ANDROID_HOME=$ANDROID_SDK_ROOT ./build-package.sh -I -a -C ${{ matrix.target_arch }} $packages
         elif [ -n "$packages" ]; then
-          ./scripts/run-docker.sh ./build-package.sh -I -a ${{ matrix.target_arch }} $packages
+          ./scripts/run-docker.sh ./build-package.sh -I -a -C ${{ matrix.target_arch }} $packages
         fi
 
     - name: Generate build artifacts
diff --git a/build-package.sh b/build-package.sh
index 3e619817f41..987e819ba7a 100755
--- a/build-package.sh
+++ b/build-package.sh
@@ -423,6 +423,8 @@ _show_usage() {
 	echo "Available options:"
 	[ "$TERMUX_ON_DEVICE_BUILD" = "false" ] && echo "  -a The architecture to build for: aarch64(default), arm, i686, x86_64 or all."
 	echo "  -d Build with debug symbols."
+	echo "  -c Continue building."
+	echo "  -C Cleanup already built packages on low disk space."
 	echo "  -D Build a disabled package in disabled-packages/."
 	echo "  -f Force build even if package has already been built."
 	echo "  -F Force build even if package and its dependencies have already been built."
@@ -521,6 +523,7 @@ while (($# >= 1)); do
 			fi
 			;;
 		-c) TERMUX_CONTINUE_BUILD=true;;
+		-C) TERMUX_CLEANUP_BUILT_PACKAGES_ON_LOW_DISK_SPACE=true;;
 		-*) termux_error_exit "./build-package.sh: illegal option '$1'";;
 		*) PACKAGE_LIST+=("$1");;
 	esac
@@ -588,7 +591,7 @@ for ((i=0; i<${#PACKAGE_LIST[@]}; i++)); do
 					${TERMUX_FORCE_BUILD+-f} ${TERMUX_INSTALL_DEPS+-i} ${TERMUX_IS_DISABLED+-D} \
 					${TERMUX_DEBUG_BUILD+-d} ${TERMUX_OUTPUT_DIR+-o $TERMUX_OUTPUT_DIR} \
 					${TERMUX_FORCE_BUILD_DEPENDENCIES+-F} ${TERMUX_GLOBAL_LIBRARY+-L} \
-					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} \
+					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} ${TERMUX_CLEANUP_BUILT_PACKAGES_ON_LOW_DISK_SPACE+-C} \
 					--format ${TERMUX_PACKAGE_FORMAT:=debian} \
 					--library ${TERMUX_PACKAGE_LIBRARY:=bionic} "${PACKAGE_LIST[i]}"
 			done
diff --git a/scripts/build/termux_step_cleanup_packages.sh b/scripts/build/termux_step_cleanup_packages.sh
index d17aedbf53e..cf93d1aac28 100644
--- a/scripts/build/termux_step_cleanup_packages.sh
+++ b/scripts/build/termux_step_cleanup_packages.sh
@@ -1,21 +1,28 @@
 termux_step_cleanup_packages() {
+	[[ "${TERMUX_CLEANUP_BUILT_PACKAGES_ON_LOW_DISK_SPACE:=false}" == "true" ]] || return 0
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	local AVAILABLE TERMUX_PACKAGES_DIRECTORIES PKGS PKG_REGEX
 
-	# No need to cleanup if there is enough disk space (more than 5 GB)
-	[ "$AVAILABLE" -lt 5 ] || return 0
+	# Extract available disk space in bytes
+	AVAILABLE="$(df "$TERMUX_TOPDIR" | awk 'NR==2 {print $4 * 1024}')"
 
-	local TERMUX_PACKAGES_DIRECTORIES="$(jq --raw-output 'del(.pkg_format) | keys | .[]' "${TERMUX_SCRIPTDIR}"/repo.json)"
+	# No need to cleanup if there is enough disk space
+	(( AVAILABLE <= TERMUX_CLEANUP_BUILT_PACKAGES_THRESHOLD )) || return 0
+
+	TERMUX_PACKAGES_DIRECTORIES="$(jq --raw-output 'del(.pkg_format) | keys | .[]' "${TERMUX_SCRIPTDIR}"/repo.json)"
 
 	# Build package name regex to be used with `find`, avoiding loops.
-	local PKG_REGEX="$(find ${TERMUX_PACKAGES_DIRECTORIES} -mindepth 1 -maxdepth 1 -type d -printf '%f|')^$"
+	PKGS="$(find ${TERMUX_PACKAGES_DIRECTORIES} -mindepth 1 -maxdepth 1 -type d -printf '%f\n')"
+	[[ -z "$PKGS" ]] && return 0
 
 	# Exclude current package from the list.
-	PKG_REGEX="${PKG_REGEX//$TERMUX_PKG_NAME|/}"
+	PKGS="$(printf "%s" "$PKGS" | grep -Fxv "$TERMUX_PKG_NAME")"
+	[[ -z "$PKGS" ]] && return 0
+
+	PKG_REGEX="$(printf "%s" "$PKGS" | sed -zE 's/[][\.|$(){}?+*^]/\\&/g' | sed -E 's/(.*)/(\1)/g' | sed -zE -e 's/[\n]+/|/g' -e 's/(.*)/(\1)/g')"
 
 	echo "INFO: cleaning up some disk space for building \"${TERMUX_PKG_NAME}\"."
 
-	find "$TERMUX_TOPDIR" -mindepth 1 -maxdepth 1 -type d -regextype posix-extended -regex ".*/($PKG_REGEX)$" -exec rm -rf {} +
+	(cd "$TERMUX_TOPDIR" && find . -mindepth 1 -maxdepth 1 -type d -regextype posix-extended -regex "^\./$PKG_REGEX$" -exec rm -rf "{}" +)
 }
diff --git a/scripts/properties.sh b/scripts/properties.sh
index 69e27486c2f..f7fea5ebca4 100644
--- a/scripts/properties.sh
+++ b/scripts/properties.sh
@@ -41,6 +41,7 @@ TERMUX_ETC_PREFIX_DIR_PATH="${TERMUX_PREFIX}/etc"
 TERMUX_PROFILE_D_PREFIX_DIR_PATH="${TERMUX_ETC_PREFIX_DIR_PATH}/profile.d"
 TERMUX_CONFIG_PREFIX_DIR_PATH="${TERMUX_ETC_PREFIX_DIR_PATH}/termux"
 TERMUX_BOOTSTRAP_CONFIG_DIR_PATH="${TERMUX_CONFIG_PREFIX_DIR_PATH}/bootstrap"
+TERMUX_CLEANUP_BUILT_PACKAGES_THRESHOLD="$(( 5 * 1024 ** 3 ))" # 5 GiB
 
 # Path to CGCT tools
 CGCT_DEFAULT_PREFIX="/data/data/com.termux/files/usr/glibc"
diff --git a/scripts/updates/utils/termux_pkg_upgrade_version.sh b/scripts/updates/utils/termux_pkg_upgrade_version.sh
index 6a02acf2c73..d7f532a8d72 100755
--- a/scripts/updates/utils/termux_pkg_upgrade_version.sh
+++ b/scripts/updates/utils/termux_pkg_upgrade_version.sh
@@ -96,8 +96,18 @@ termux_pkg_upgrade_version() {
 		fi
 	done
 
-	# Get available space (df outputs sizes in 1024-byte blocks)
-	local space_available="$(df -P "/var/lib/docker" | awk 'NR==2 {print $4}')"
+	local force_cleanup="false"
+	local space_available
+
+	if [[ -d "/var/lib/docker" ]]; then
+		# Get available space in bytes
+		space_available="$(df "/var/lib/docker" | awk 'NR==2 { print $4 * 1024 }')"
+
+		if (( space_available <= TERMUX_CLEANUP_BUILT_PACKAGES_THRESHOLD )); then
+			force_cleanup="true"
+		fi
+	fi
+
 
 	local big_package=false
 	while IFS= read -r p; do
@@ -111,7 +121,7 @@ termux_pkg_upgrade_version() {
 		"${TERMUX_SCRIPTDIR}/scripts/run-docker.sh" ./clean.sh
 	fi
 
-	if ! "${TERMUX_SCRIPTDIR}/scripts/run-docker.sh" ./build-package.sh -a "${TERMUX_ARCH}" -i "${TERMUX_PKG_NAME}"; then
+	if ! "${TERMUX_SCRIPTDIR}/scripts/run-docker.sh" ./build-package.sh -C -a "${TERMUX_ARCH}" -i "${TERMUX_PKG_NAME}"; then
 		if [[ "${big_package}" == "true" ]]; then
 			"${TERMUX_SCRIPTDIR}/scripts/run-docker.sh" ./clean.sh
 		fi
@@ -119,7 +129,7 @@ termux_pkg_upgrade_version() {
 		termux_error_exit "ERROR: failed to build."
 	fi
 
-	if [[ "${big_package}" == "true" ]] || (( space_available <= 2 * 1024 ** 2 )); then
+	if [[ "${big_package}" == "true" ]] || [[ "${force_cleanup}" == "true" ]]; then
 		"${TERMUX_SCRIPTDIR}/scripts/run-docker.sh" ./clean.sh
 	fi
 


Reviews:
user: login: robertkirkman
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:08:21Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:10:11Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:17:37Z
user: login: robertkirkman
body: Ok now it is working on my device as expected.
state: APPROVED
author_association: MEMBER
submitted_at: 2025-02-27T17:19:14Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:19:42Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:20:44Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:27:42Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:40:51Z
user: login: robertkirkman
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:41:08Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:42:15Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:42:46Z
user: login: TomJo2000
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:42:58Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:43:45Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T17:45:08Z
user: login: robertkirkman
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T18:33:19Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T18:41:39Z
user: login: robertkirkman
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T18:41:40Z
user: login: robertkirkman
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T18:54:56Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T18:59:52Z
user: login: robertkirkman
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T19:10:23Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T19:18:49Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T19:21:47Z
user: login: robertkirkman
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T19:31:04Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-27T19:31:55Z
user: login: truboxl
state: CHANGES_REQUESTED
author_association: MEMBER
submitted_at: 2025-02-28T00:18:04Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-28T00:45:51Z
user: login: agnostic-apollo
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-02-28T20:18:32Z
user: login: truboxl
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-03-03T05:04:52Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-03-03T05:18:44Z
user: login: truboxl
state: APPROVED
author_association: MEMBER
submitted_at: 2025-03-03T10:18:08Z
user: login: TomJo2000
body: LGTM, I'd just like the space threshold to be standardized.
state: APPROVED
author_association: MEMBER
submitted_at: 2025-03-03T15:00:42Z
user: login: twaik
state: COMMENTED
author_association: MEMBER
submitted_at: 2025-03-03T15:11:26Z

Review comments:
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 2 ))" # 5 GB
 
-	# No need to cleanup if there is enough disk space (more than 5 GB)
-	[ "$AVAILABLE" -lt 5 ] || return 0
+	# Extract available disk space in KB
+	local AVAILABLE=`df -P "$TERMUX_TOPDIR" | awk 'NR==2 {print $4}'`
path: scripts/build/termux_step_cleanup_packages.sh
user: login: robertkirkman
body: Are you sure that you wanted to use the `-P` argument here?

`-P` makes the report in 512 byte blocks instead of 1024 byte blocks

so, when I test on my device, it looks like the amount of space available is reported as larger compared to `$CLEANUP_THRESHOLD` than expected.

```
~ $ CLEANUP_THRESHOLD="$(( 5 * 1024 ** 2 ))"
~ $ AVAILABLE=`df -P "$TERMUX_TOPDIR" | awk 'NR==2 {print $4}'`
~ $ echo $CLEANUP_THRESHOLD
5242880
~ $ echo $AVAILABLE
15270800
~ $ df -h .
Filesystem      Size  Used Avail Use% Mounted on
/dev/block/dm-3  52G   45G  7.2G  87% /data
~ $
```
created_at: 2025-02-27T17:08:17Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 2 ))" # 5 GB
 
-	# No need to cleanup if there is enough disk space (more than 5 GB)
-	[ "$AVAILABLE" -lt 5 ] || return 0
+	# Extract available disk space in KB
+	local AVAILABLE=`df -P "$TERMUX_TOPDIR" | awk 'NR==2 {print $4}'`
path: scripts/build/termux_step_cleanup_packages.sh
user: login: twaik
body: Oh, ok. Probably I will drop `-P` in `termux_pkg_upgrade_version.sh` too.
created_at: 2025-02-27T17:10:11Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 2 ))" # 5 GB
 
-	# No need to cleanup if there is enough disk space (more than 5 GB)
-	[ "$AVAILABLE" -lt 5 ] || return 0
+	# Extract available disk space in KB
+	local AVAILABLE=`df -P "$TERMUX_TOPDIR" | awk 'NR==2 {print $4}'`
path: scripts/build/termux_step_cleanup_packages.sh
user: login: TomJo2000
body: We might even wanna convert to bytes instead of blocks.
created_at: 2025-02-27T17:17:37Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 2 ))" # 5 GB
 
-	# No need to cleanup if there is enough disk space (more than 5 GB)
-	[ "$AVAILABLE" -lt 5 ] || return 0
+	# Extract available disk space in KB
+	local AVAILABLE=`df -P "$TERMUX_TOPDIR" | awk 'NR==2 {print $4}'`
path: scripts/build/termux_step_cleanup_packages.sh
user: login: twaik
body: You mean for readability?
created_at: 2025-02-27T17:19:42Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 2 ))" # 5 GB
 
-	# No need to cleanup if there is enough disk space (more than 5 GB)
-	[ "$AVAILABLE" -lt 5 ] || return 0
+	# Extract available disk space in KB
+	local AVAILABLE=`df -P "$TERMUX_TOPDIR" | awk 'NR==2 {print $4}'`
path: scripts/build/termux_step_cleanup_packages.sh
user: login: TomJo2000
body: For example.
I mean, I forgot about the blocksize twice while referencing this.
So just having it in bytes would probably alleviate that.
created_at: 2025-02-27T17:20:44Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 2 ))" # 5 GB
 
-	# No need to cleanup if there is enough disk space (more than 5 GB)
-	[ "$AVAILABLE" -lt 5 ] || return 0
+	# Extract available disk space in KB
+	local AVAILABLE=`df -P "$TERMUX_TOPDIR" | awk 'NR==2 {print $4}'`
path: scripts/build/termux_step_cleanup_packages.sh
user: login: twaik
body: Done.
created_at: 2025-02-27T17:27:42Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 3 ))" # 5 GB
 
-	# No need to cleanup if there is enough disk space (more than 5 GB)
-	[ "$AVAILABLE" -lt 5 ] || return 0
+	# Extract available disk space in bytes
+	local AVAILABLE=`df "$TERMUX_TOPDIR" | awk 'NR==2 {print $4 * 1024}'`
path: scripts/build/termux_step_cleanup_packages.sh
user: login: TomJo2000
body: ```suggestion
	local AVAILABLE="$(df "$TERMUX_TOPDIR" | awk 'NR==2 {print $4 * 1024}')"
```
created_at: 2025-02-27T17:40:51Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 3 ))" # 5 GB
path: scripts/build/termux_step_cleanup_packages.sh
user: login: robertkirkman
body: I am not sure if it is very important, but currently here the word "threshold" in the variable name is misspelled, and that made it a little bit confusing when I tried to test by copying and pasting some code from here, because I accidentally typed "`CLEANUP_THRESHOLD`", which did not successfully match the variable in the code that I copied and pasted.
created_at: 2025-02-27T17:41:00Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 3 ))" # 5 GB
path: scripts/build/termux_step_cleanup_packages.sh
user: login: TomJo2000
body: This is binary so Gibibytes.
```suggestion
	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 3 ))" # 5 GiB
```
created_at: 2025-02-27T17:41:39Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 3 ))" # 5 GB
path: scripts/build/termux_step_cleanup_packages.sh
user: login: twaik
body: Fixed.
created_at: 2025-02-27T17:42:46Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 3 ))" # 5 GB
path: scripts/build/termux_step_cleanup_packages.sh
user: login: TomJo2000
body: May as well spell it correctly while we're here.
created_at: 2025-02-27T17:42:58Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 3 ))" # 5 GB
path: scripts/build/termux_step_cleanup_packages.sh
user: login: twaik
body: Fixed.
created_at: 2025-02-27T17:43:45Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,14 @@
 termux_step_cleanup_packages() {
 	[[ -d "$TERMUX_TOPDIR" ]] || return 0
 
-	# Extract available disk space in GB
-	local AVAILABLE=`df -B $((1024**3)) --output=avail "$TERMUX_TOPDIR" | tail -1`
+	# How much space is considered to be enough
+	local CLEANUP_TRESHOLD="$(( 5 * 1024 ** 3 ))" # 5 GB
 
-	# No need to cleanup if there is enough disk space (more than 5 GB)
-	[ "$AVAILABLE" -lt 5 ] || return 0
+	# Extract available disk space in bytes
+	local AVAILABLE=`df "$TERMUX_TOPDIR" | awk 'NR==2 {print $4 * 1024}'`
path: scripts/build/termux_step_cleanup_packages.sh
user: login: twaik
body: Done.
created_at: 2025-02-27T17:45:08Z
author_association: MEMBER
diff_hunk: @@ -228,9 +228,9 @@ jobs:
         done
 
         if [ "$DOCKER_BUILD" == 'false' ]; then
-          NDK=$ANDROID_NDK ANDROID_HOME=$ANDROID_SDK_ROOT ./build-package.sh -I -a ${{ matrix.target_arch }} $packages
+          NDK=$ANDROID_NDK ANDROID_HOME=$ANDROID_SDK_ROOT ./build-package.sh -I -a -C ${{ matrix.target_arch }} $packages
         elif [ -n "$packages" ]; then
-          ./scripts/run-docker.sh ./build-package.sh -I -a ${{ matrix.target_arch }} $packages
+          ./scripts/run-docker.sh ./build-package.sh -I -a -C ${{ matrix.target_arch }} $packages
path: .github/workflows/packages.yml
user: login: robertkirkman
body: Should it also be added to here, or is it not necessary?

https://github.com/termux/termux-packages/blob/c24217ae64ea15bc173ea47e433c4d8533ef7c38/scripts/updates/utils/termux_pkg_upgrade_version.sh#L114

I thought space might be helpful there since that is where `big-pkgs.list` is used.
created_at: 2025-02-27T18:31:21Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,15 @@
 termux_step_cleanup_packages() {
+	[[ "${TERMUX_CLEANUP_PACKAGES:=false}" == "true" ]] || return 0
path: scripts/build/termux_step_cleanup_packages.sh
user: login: robertkirkman
body: Maybe it is not very important, but should the variable declaration line be added to `termux_step_setup_variables()` so that it could be consistently available in all places where the other variables declared there are available?

created_at: 2025-02-27T18:33:14Z
author_association: MEMBER
diff_hunk: @@ -1,11 +1,15 @@
 termux_step_cleanup_packages() {
+	[[ "${TERMUX_CLEANUP_PACKAGES:=false}" == "true" ]] || return 0
path: scripts/build/termux_step_cleanup_packages.sh
user: login: twaik
body: `TERMUX_FORCE_BUILD`, `TERMUX_INSTALL_DEPS`, `TERMUX_IS_DISABLED` and other variables set in arg parsing step are not used anywhere else so I do not think we should move the assigning to `termux_step_setup_variables`. Also this variable is set way before `termux_step_setup_variables` is being invoked so I do not think it is right.
Also I intentionally used `${VAR+smth}` and `${VAR:=default}` to avoid throwing unset variable exceptions.
created_at: 2025-02-27T18:41:39Z
author_association: MEMBER
diff_hunk: @@ -228,9 +228,9 @@ jobs:
         done
 
         if [ "$DOCKER_BUILD" == 'false' ]; then
-          NDK=$ANDROID_NDK ANDROID_HOME=$ANDROID_SDK_ROOT ./build-package.sh -I -a ${{ matrix.target_arch }} $packages
+          NDK=$ANDROID_NDK ANDROID_HOME=$ANDROID_SDK_ROOT ./build-package.sh -I -a -C ${{ matrix.target_arch }} $packages
         elif [ -n "$packages" ]; then
-          ./scripts/run-docker.sh ./build-package.sh -I -a ${{ matrix.target_arch }} $packages
+          ./scripts/run-docker.sh ./build-package.sh -I -a -C ${{ matrix.target_arch }} $packages
path: .github/workflows/packages.yml
user: login: robertkirkman
body: Oh never mind, I see that it is, I missed that
created_at: 2025-02-27T18:41:40Z
author_association: MEMBER
diff_hunk: @@ -588,7 +591,7 @@ for ((i=0; i<${#PACKAGE_LIST[@]}; i++)); do
 					${TERMUX_FORCE_BUILD+-f} ${TERMUX_INSTALL_DEPS+-i} ${TERMUX_IS_DISABLED+-D} \
 					${TERMUX_DEBUG_BUILD+-d} ${TERMUX_OUTPUT_DIR+-o $TERMUX_OUTPUT_DIR} \
 					${TERMUX_FORCE_BUILD_DEPENDENCIES+-F} ${TERMUX_GLOBAL_LIBRARY+-L} \
-					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} \
+					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} ${TERMUX_CLEANUP_PACKAGES+-C} \
path: build-package.sh
user: login: robertkirkman
body: Should this part also be copied to here, like `TERMUX_FORCE_BUILD_DEPENDENCIES` is, or would that be incorrect?

https://github.com/termux/termux-packages/blob/c24217ae64ea15bc173ea47e433c4d8533ef7c38/scripts/build/termux_step_get_dependencies.sh#L135-L139
created_at: 2025-02-27T18:54:40Z
author_association: MEMBER
diff_hunk: @@ -588,7 +591,7 @@ for ((i=0; i<${#PACKAGE_LIST[@]}; i++)); do
 					${TERMUX_FORCE_BUILD+-f} ${TERMUX_INSTALL_DEPS+-i} ${TERMUX_IS_DISABLED+-D} \
 					${TERMUX_DEBUG_BUILD+-d} ${TERMUX_OUTPUT_DIR+-o $TERMUX_OUTPUT_DIR} \
 					${TERMUX_FORCE_BUILD_DEPENDENCIES+-F} ${TERMUX_GLOBAL_LIBRARY+-L} \
-					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} \
+					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} ${TERMUX_CLEANUP_PACKAGES+-C} \
path: build-package.sh
user: login: twaik
body: It is already there.
created_at: 2025-02-27T18:59:52Z
author_association: MEMBER
diff_hunk: @@ -588,7 +591,7 @@ for ((i=0; i<${#PACKAGE_LIST[@]}; i++)); do
 					${TERMUX_FORCE_BUILD+-f} ${TERMUX_INSTALL_DEPS+-i} ${TERMUX_IS_DISABLED+-D} \
 					${TERMUX_DEBUG_BUILD+-d} ${TERMUX_OUTPUT_DIR+-o $TERMUX_OUTPUT_DIR} \
 					${TERMUX_FORCE_BUILD_DEPENDENCIES+-F} ${TERMUX_GLOBAL_LIBRARY+-L} \
-					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} \
+					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} ${TERMUX_CLEANUP_PACKAGES+-C} \
path: build-package.sh
user: login: robertkirkman
body: I have looked again and again several times but, on my screen it is not there,
but it is true that a little while ago I couldn't find it in `termux_pkg_upgrade_version.sh` when it was already,
so maybe it is just very hard for me to see it.
It looks like this for me

![image](https://github.com/user-attachments/assets/dc737aba-ef60-4847-8b00-d8da121759ba)

created_at: 2025-02-27T19:10:23Z
author_association: MEMBER
diff_hunk: @@ -588,7 +591,7 @@ for ((i=0; i<${#PACKAGE_LIST[@]}; i++)); do
 					${TERMUX_FORCE_BUILD+-f} ${TERMUX_INSTALL_DEPS+-i} ${TERMUX_IS_DISABLED+-D} \
 					${TERMUX_DEBUG_BUILD+-d} ${TERMUX_OUTPUT_DIR+-o $TERMUX_OUTPUT_DIR} \
 					${TERMUX_FORCE_BUILD_DEPENDENCIES+-F} ${TERMUX_GLOBAL_LIBRARY+-L} \
-					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} \
+					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} ${TERMUX_CLEANUP_PACKAGES+-C} \
path: build-package.sh
user: login: twaik
body: Oh, wait. Wrong file. I am dumb, sorry.
created_at: 2025-02-27T19:18:49Z
author_association: MEMBER
diff_hunk: @@ -588,7 +591,7 @@ for ((i=0; i<${#PACKAGE_LIST[@]}; i++)); do
 					${TERMUX_FORCE_BUILD+-f} ${TERMUX_INSTALL_DEPS+-i} ${TERMUX_IS_DISABLED+-D} \
 					${TERMUX_DEBUG_BUILD+-d} ${TERMUX_OUTPUT_DIR+-o $TERMUX_OUTPUT_DIR} \
 					${TERMUX_FORCE_BUILD_DEPENDENCIES+-F} ${TERMUX_GLOBAL_LIBRARY+-L} \
-					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} \
+					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} ${TERMUX_CLEANUP_PACKAGES+-C} \
path: build-package.sh
user: login: twaik
body: I do not think it should be there. `termux_step_cleanup_packages` cleans up everything but keeps folder specified in `${TERMUX_PKG_NAME}`. So `termux_step_cleanup_packages` invoked in subsequent `./build-package.sh` will cleanup packages of currently built package too and which will be bad in `TERMUX_CONTINUE_BUILD` mode.
created_at: 2025-02-27T19:21:47Z
author_association: MEMBER
diff_hunk: @@ -588,7 +591,7 @@ for ((i=0; i<${#PACKAGE_LIST[@]}; i++)); do
 					${TERMUX_FORCE_BUILD+-f} ${TERMUX_INSTALL_DEPS+-i} ${TERMUX_IS_DISABLED+-D} \
 					${TERMUX_DEBUG_BUILD+-d} ${TERMUX_OUTPUT_DIR+-o $TERMUX_OUTPUT_DIR} \
 					${TERMUX_FORCE_BUILD_DEPENDENCIES+-F} ${TERMUX_GLOBAL_LIBRARY+-L} \
-					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} \
+					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} ${TERMUX_CLEANUP_PACKAGES+-C} \
path: build-package.sh
user: login: robertkirkman
body: Yes you are right, when I think about it, that means that the version currently in the repository is broken in that codepath because of `termux_step_cleanup_packages()` always running, so this PR will fix it by limiting it to the top-level invocations of `build-package.sh` and only when `-C` is used, and not running it in the nested invocations that build dependency packages.
created_at: 2025-02-27T19:31:04Z
author_association: MEMBER
diff_hunk: @@ -588,7 +591,7 @@ for ((i=0; i<${#PACKAGE_LIST[@]}; i++)); do
 					${TERMUX_FORCE_BUILD+-f} ${TERMUX_INSTALL_DEPS+-i} ${TERMUX_IS_DISABLED+-D} \
 					${TERMUX_DEBUG_BUILD+-d} ${TERMUX_OUTPUT_DIR+-o $TERMUX_OUTPUT_DIR} \
 					${TERMUX_FORCE_BUILD_DEPENDENCIES+-F} ${TERMUX_GLOBAL_LIBRARY+-L} \
-					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} \
+					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} ${TERMUX_CLEANUP_PACKAGES+-C} \
path: build-package.sh
user: login: twaik
body: Correct.
created_at: 2025-02-27T19:31:55Z
author_association: MEMBER
diff_hunk: @@ -96,8 +96,8 @@ termux_pkg_upgrade_version() {
 		fi
 	done
 
-	# Get available space (df outputs sizes in 1024-byte blocks)
-	local space_available="$(df -P "/var/lib/docker" | awk 'NR==2 {print $4}')"
+	# Get available space in bytes
+	local space_available="$(df "/var/lib/docker" | awk 'NR==2 {print $4 * 1024}')"
path: scripts/updates/utils/termux_pkg_upgrade_version.sh
user: login: truboxl
body: Add a case if /var/lib/docker not exist or the command fails, fallback to check current location disk space
created_at: 2025-02-28T00:17:18Z
author_association: MEMBER
diff_hunk: @@ -521,6 +523,7 @@ while (($# >= 1)); do
 			fi
 			;;
 		-c) TERMUX_CONTINUE_BUILD=true;;
+		-C) TERMUX_CLEANUP_PACKAGES=true;;
path: build-package.sh
user: login: agnostic-apollo
body: Rename to `TERMUX_CLEANUP_BUILT_PACKAGES_ON_LOW_DISK_SPACE`
created_at: 2025-02-28T00:45:51Z
author_association: MEMBER
diff_hunk: @@ -588,7 +591,7 @@ for ((i=0; i<${#PACKAGE_LIST[@]}; i++)); do
 					${TERMUX_FORCE_BUILD+-f} ${TERMUX_INSTALL_DEPS+-i} ${TERMUX_IS_DISABLED+-D} \
 					${TERMUX_DEBUG_BUILD+-d} ${TERMUX_OUTPUT_DIR+-o $TERMUX_OUTPUT_DIR} \
 					${TERMUX_FORCE_BUILD_DEPENDENCIES+-F} ${TERMUX_GLOBAL_LIBRARY+-L} \
-					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} \
+					${TERMUX_WITHOUT_DEPVERSION_BINDING+-w} ${TERMUX_CLEANUP_PACKAGES+-C} \
path: build-package.sh
user: login: agnostic-apollo
body: If a dependency package instead of top level package requires lot of space, then you will run into same issues you are trying to solve, although CI is less likely to be affected as it uses `-I` and dependencies won't get built, unless there is a dependency version constraint or dependency package isn't available in repo. Been a while since I looked into it, but I am not sure how continuous build will be affected as dependencies would already have been built before top level continuous package gets built. If there is some issue with, then force set `TERMUX_CLEANUP_BUILT_PACKAGES_ON_LOW_DISK_SPACE` to `false` if continuous build is enabled after flag parsing.
created_at: 2025-02-28T20:18:32Z
author_association: MEMBER
diff_hunk: @@ -96,8 +96,10 @@ termux_pkg_upgrade_version() {
 		fi
 	done
 
-	# Get available space (df outputs sizes in 1024-byte blocks)
-	local space_available="$(df -P "/var/lib/docker" | awk 'NR==2 {print $4}')"
+	local cleanup_threshold="$(( 5 * 1024 ** 3 ))" # 3 GiB
+
+	# Get available space in bytes
+	local space_available="$(df "/var/lib/docker" | awk 'NR==2 { val=$4*1024 } END { if(!val) val='$cleanup_threshold'+1; print val }')"
path: scripts/updates/utils/termux_pkg_upgrade_version.sh
user: login: truboxl
body: You cannot expect `/var/lib/docker` to present in all configurations. It can be changed. I strongly oppose fallback to hardcoded threshold+1 value.
created_at: 2025-03-03T05:04:52Z
author_association: MEMBER
diff_hunk: @@ -96,8 +96,10 @@ termux_pkg_upgrade_version() {
 		fi
 	done
 
-	# Get available space (df outputs sizes in 1024-byte blocks)
-	local space_available="$(df -P "/var/lib/docker" | awk 'NR==2 {print $4}')"
+	local cleanup_threshold="$(( 5 * 1024 ** 3 ))" # 3 GiB
+
+	# Get available space in bytes
+	local space_available="$(df "/var/lib/docker" | awk 'NR==2 { val=$4*1024 } END { if(!val) val='$cleanup_threshold'+1; print val }')"
path: scripts/updates/utils/termux_pkg_upgrade_version.sh
user: login: twaik
body: Ok, fixed this.
created_at: 2025-03-03T05:18:44Z
author_association: MEMBER
diff_hunk: @@ -96,8 +96,18 @@ termux_pkg_upgrade_version() {
 		fi
 	done
 
-	# Get available space (df outputs sizes in 1024-byte blocks)
-	local space_available="$(df -P "/var/lib/docker" | awk 'NR==2 {print $4}')"
+	local force_cleanup="false"
+
+	if [[ -d "/var/lib/docker" ]]; then
+		# Get available space in bytes
+		local space_available="$(df "/var/lib/docker" | awk 'NR==2 { print $4 * 1024 }')"
+		local cleanup_threshold="$(( 5 * 1024 ** 3 ))" # 3 GiB
path: scripts/updates/utils/termux_pkg_upgrade_version.sh
user: login: TomJo2000
body: Why is the threshold 3GiB here when it was 5GiB in `termux_step_cleanup_packages.sh`?
We might wanna promote this to a "global" variable and put it in `properties.sh`
https://github.com/termux/termux-packages/blob/master/scripts/properties.sh
created_at: 2025-03-03T14:55:09Z
author_association: MEMBER
diff_hunk: @@ -96,8 +96,18 @@ termux_pkg_upgrade_version() {
 		fi
 	done
 
-	# Get available space (df outputs sizes in 1024-byte blocks)
-	local space_available="$(df -P "/var/lib/docker" | awk 'NR==2 {print $4}')"
+	local force_cleanup="false"
+
+	if [[ -d "/var/lib/docker" ]]; then
+		# Get available space in bytes
+		local space_available="$(df "/var/lib/docker" | awk 'NR==2 { print $4 * 1024 }')"
+		local cleanup_threshold="$(( 5 * 1024 ** 3 ))" # 3 GiB
path: scripts/updates/utils/termux_pkg_upgrade_version.sh
user: login: twaik
body: Done.
created_at: 2025-03-03T15:11:25Z
author_association: MEMBER

PR comments:
user: login: twaik
created_at: 2025-02-27T16:43:33Z
body: Works fine on my device.
author_association: MEMBER
user: login: twaik
created_at: 2025-02-27T20:04:18Z
body: Since this PR makes newly added build step disabled by default it should be harmless.
I will merge it in 12 hours if there are no objections.
author_association: MEMBER
user: login: agnostic-apollo
created_at: 2025-02-28T20:09:25Z
body: https://github.com/termux/termux-packages/blob/833f12b838b46eaecaf3e5d2c1be8bf83cff2f55/scripts/build/termux_step_cleanup_packages.sh#L14-L24

1. Using `local` and sub shell together hides the result value of the command and `set -e` abort won't happen. All local variables must be declared beforehand.
- https://github.com/koalaman/shellcheck/wiki/SC2155#problematic-code-in-the-case-of-local

```shell
$ func() { set -e; local res="$(false)"; echo here1; }
(func;)
func1() { set -e; local res; res="$(false)"; echo here2; }
(func1;)
here1
```

2. Using `-printf '%f|'` will result in last file to also have `|` appended, which will result in a dangling `|` in the regex, which is invalid.
3. Package names are not safe to be parsed as regex, for example, `libc++`,  `libsigc++-3.0`, etc have regex special characters like `.` and `+`, which can have unexpected consequences. Additionally, a double `++` would be invalid too.
4. Instead of using `.*` in `.*/($PKG_REGEX)$"`, `cd` first and use `^\./`.

```shell
local packages packages_regex

packages="$(find ${TERMUX_PACKAGES_DIRECTORIES} -mindepth 1 -maxdepth 1 -type d -printf '%f\n')"
[[ -z "$packages" ]] && return 0

# Exclude current package.
packages="$(printf "%s" "$packages" | grep -Fxv "$TERMUX_PKG_NAME")"
[[ -z "$packages" ]] && return 0

packages_regex="$(printf "%s" "$packages" | sed -zE 's/[][\.|$(){}?+*^]/\\&/g' | sed -E 's/(.*)/(\1)/g' | sed -zE -e 's/[\n]+/|/g' -e 's/(.*)/(\1)/g')"

(cd "$TERMUX_TOPDIR" && find . -mindepth 1 -maxdepth 1 -type d -regextype posix-extended -regex "^\./$packages_regex$" -exec rm -rf "{}" +)
```
author_association: MEMBER
user: login: twaik
created_at: 2025-03-02T05:14:56Z
body: > 1\. Using `local` and sub shell together hides the result value of the command and `set -e` abort won't happen. All local variables must be declared beforehand.

In this case nothing bad will happen, cleanup will not be performed. Seems to be harmless.

> 2\. Using `-printf '%f|'` will result in last file to also have `|` appended, which will result in a dangling `|` in the regex, which is invalid.

Correct, and that is the reason for `^$` dummy token to be added. Empty filenames are not possible in our case so it will have no matches.
Also `|` is not a part of any packagename in termux-packages so IMHO it should not be escaped and can be safely used here.

> 3\. Package names are not safe to be parsed as regex, for example, libc++, libsigc++-3.0, etc have regex special characters like . and +, which can have unexpected consequences. Additionally, a double ++ would be invalid too.

Correct. Did not think about it.

> 4\. Instead of using `.*` in `.*/($PKG_REGEX)$"`, `cd` first and use `^\./`.

`find` appends a string literal passed to commandline as a part of response so it should be safe to use a response without `cd`, isn't it?
author_association: MEMBER
user: login: truboxl
created_at: 2025-03-03T09:18:51Z
body: Please init `TERMUX_CLEANUP_BUILT_PACKAGES_ON_LOW_DISK_SPACE` variable with `false` value in `scripts/build/termux_step_setup_variables.sh` instead

to allow reuse outside `scripts/build/termux_step_cleanup_packages.sh`
author_association: MEMBER
user: login: robertkirkman
created_at: 2025-03-03T09:33:20Z
body: Truboxl, I asked about that but, twaik explained that there is some reason it is better to stay this way [here](https://github.com/termux/termux-packages/pull/23530#discussion_r1974136876)
author_association: MEMBER
user: login: truboxl
created_at: 2025-03-03T14:42:45Z
body: It would be extremely helpful and speed up review process of changing the build scripts by having CI for on device build. Not that we are allowing on device built packages to be uploaded to server yet (with some exceptions like pypy).
author_association: MEMBER
user: login: twaik
created_at: 2025-03-03T14:49:09Z
body: > It would be extremely helpful and speed up review process of changing the build scripts by having CI for on device build. Not that we are allowing on device built packages to be uploaded to server yet (with some exceptions like pypy).

Probably we should create `termux-docker`-based workflow for checking build-system capabilities for all cases (host, docker and termux-docker (aka on-device)).
author_association: MEMBER
user: login: twaik
created_at: 2025-03-03T15:18:46Z
body: Seems to be fine, I'll merge this in 12 hours if no one objects.
author_association: MEMBER
user: login: robertkirkman
created_at: 2025-03-03T20:37:48Z
body: > It would be extremely helpful and speed up review process of changing the build scripts by having CI for on device build. Not that we are allowing on device built packages to be uploaded to server yet (with some exceptions like pypy).

I discussed some information about that [here](https://github.com/termux/termux-docker/pull/72#issuecomment-2630757437), i believe licy183 might try to make that, but I will try to make it also.

I created a `$TERMUX_ON_DEVICE_BUILD=true`-compatible `build.sh` for `pypy` as a test and it works, but I will wait to upload it until after there is a workflow capable of testing it in CI.

There are a lot of packages that currently have various errors when built with `$TERMUX_ON_DEVICE_BUILD=true`, so if such a workflow is enabled, it would fail to build a lot of packages, but maybe it would be good if it helps to fix those errors.

> Probably we should create termux-docker-based workflow for checking build-system capabilities for all cases (host, docker and termux-docker (aka on-device)).

There are some packages that rely on audio or graphics libraries, which I believe termux-docker cannot currently build or test effectively with exactly accurate behavior to what a full AVD would, because of not having, for one example, OpenSL ES libraries or stubs yet,  so any workflow doing this would need to be limited primarily to purely "headless" CLI packages first, but it would still be a good start.
author_association: MEMBER
user: login: agnostic-apollo
created_at: 2025-03-04T02:01:49Z
body: >In this case nothing bad will happen, cleanup will not be performed. Seems to be harmless.

In your old code, you would be getting an empty variable for packages when command fails, and then using it to generate a regex and then using it to remove directories. If something bad doesn't happen, it wouldn't be because its harmless, but because you were lucky.

You still haven't separated the `local` declaration from assignment with commands. Fix it.

>Correct, and that is the reason for ^$ dummy token to be added. Empty filenames are not possible in our case so it will have no matches.

You would still not be creating a valid or at least logical regex. That dummy token will be added to `".*/($PKG_REGEX)$"` and you will get something like  `".*/(foo|bar|^$)$"`, where there are double end anchors `$`.

>find appends a string literal passed to commandline as a part of response so it should be safe to use a response without cd, isn't it?

Yes, it should be fine to do that as well, at the cost of slight cost if there are lot of directories.


author_association: MEMBER
user: login: twaik
created_at: 2025-03-04T04:56:13Z
body: > You still haven't separated the `local` declaration from assignment with commands. Fix it.

You mean declare all local variables on separate line at the start of the function?

> You would still not be creating a valid or at least logical regex. That dummy token will be added to `".*/($PKG_REGEX)$"` and you will get something like `".*/(foo|bar|^$)$"`, where there are double end anchors `$`.

The whole point was to create a token that will not match in any case :) .
author_association: MEMBER
user: login: agnostic-apollo
created_at: 2025-03-04T05:00:01Z
body: Yes, separate line.

https://github.com/koalaman/shellcheck/wiki/SC2155#problematic-code-in-the-case-of-local
author_association: MEMBER
user: login: twaik
created_at: 2025-03-04T05:01:42Z
body: Oh, ok. TIL.
author_association: MEMBER
user: login: twaik
created_at: 2025-03-04T05:06:23Z
body: Done.
author_association: MEMBER
user: login: twaik
created_at: 2025-03-04T05:07:27Z
body: So now it should be fine, right?
author_association: MEMBER
user: login: agnostic-apollo
created_at: 2025-03-04T05:09:36Z
body: Yes, thanks
author_association: MEMBER