Skip to content

Commit

Permalink
fix(ci): fixing package cleanup to work with toybox df.
Browse files Browse the repository at this point in the history
  • Loading branch information
twaik committed Mar 4, 2025
1 parent 8c25273 commit ff5c2d6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
19 changes: 11 additions & 8 deletions scripts/build/termux_step_cleanup_packages.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ termux_step_cleanup_packages() {
[[ "${TERMUX_CLEANUP_BUILT_PACKAGES_ON_LOW_DISK_SPACE:=false}" == "true" ]] || return 0
[[ -d "$TERMUX_TOPDIR" ]] || return 0

# How much space is considered to be enough
local CLEANUP_THRESHOLD="$(( 5 * 1024 ** 3 ))" # 5 GiB
local AVAILABLE TERMUX_PACKAGES_DIRECTORIES PKGS PKG_REGEX

# Extract available disk space in bytes
local AVAILABLE="$(df "$TERMUX_TOPDIR" | awk 'NR==2 {print $4 * 1024}')"
AVAILABLE="$(df "$TERMUX_TOPDIR" | awk 'NR==2 {print $4 * 1024}')"

# No need to cleanup if there is enough disk space
(( AVAILABLE <= CLEANUP_THRESHOLD )) || return 0
(( AVAILABLE <= TERMUX_CLEANUP_BUILT_PACKAGES_THRESHOLD )) || return 0

local TERMUX_PACKAGES_DIRECTORIES="$(jq --raw-output 'del(.pkg_format) | keys | .[]' "${TERMUX_SCRIPTDIR}"/repo.json)"
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 "{}" +)
}
1 change: 1 addition & 0 deletions scripts/properties.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions scripts/updates/utils/termux_pkg_upgrade_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ termux_pkg_upgrade_version() {
done

local force_cleanup="false"
local space_available

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
space_available="$(df "/var/lib/docker" | awk 'NR==2 { print $4 * 1024 }')"

if (( space_available <= cleanup_threshold )); then
if (( space_available <= TERMUX_CLEANUP_BUILT_PACKAGES_THRESHOLD )); then
force_cleanup="true"
fi
fi
Expand Down

0 comments on commit ff5c2d6

Please sign in to comment.