repo-add: add --wait-for-lock option

Using the --wait-for-lock option will result in repo-add retrying to acquire the database lock file after 3 seconds. If the option is not set, exit with code 2 on lock failure.
This commit is contained in:
Patrick Northon 2025-05-07 02:23:45 +00:00 committed by Allan McRae
parent 267a0cc912
commit 38394d54f0
2 changed files with 27 additions and 6 deletions

View file

@ -60,6 +60,10 @@ Common Options
*\--nocolor*:: *\--nocolor*::
Remove color from 'repo-add' and 'repo-remove' output. Remove color from 'repo-add' and 'repo-remove' output.
*-w, \--wait-for-lock*::
Wait for the lock file to be acquired. If unset, command will fail with
exit code 2 if acquiring the lock fails. If set, it will retry to acquire
lock until success.
repo-add Options repo-add Options
---------------- ----------------

View file

@ -44,6 +44,7 @@ USE_COLOR='y'
PREVENT_DOWNGRADE=0 PREVENT_DOWNGRADE=0
INCLUDE_SIGS=0 INCLUDE_SIGS=0
DB_MODIFIED=0 DB_MODIFIED=0
WAIT_LOCK=0
# Import libmakepkg # Import libmakepkg
source "$MAKEPKG_LIBRARY"/util/compress.sh source "$MAKEPKG_LIBRARY"/util/compress.sh
@ -86,6 +87,7 @@ packages to remove can be specified on the command line.\n")"
printf -- "$(gettext " -s, --sign sign database with GnuPG after update\n")" printf -- "$(gettext " -s, --sign sign database with GnuPG after update\n")"
printf -- "$(gettext " -k, --key <key> use the specified key to sign the database\n")" printf -- "$(gettext " -k, --key <key> use the specified key to sign the database\n")"
printf -- "$(gettext " -v, --verify verify database's signature before update\n")" printf -- "$(gettext " -v, --verify verify database's signature before update\n")"
printf -- "$(gettext " -w, --wait-for-lock retry to acquire lock file until success\n")"
printf -- "$(gettext "\n\ printf -- "$(gettext "\n\
See %s(8) for more details and descriptions of the available options.\n")" $cmd See %s(8) for more details and descriptions of the available options.\n")" $cmd
printf "\n" printf "\n"
@ -397,13 +399,25 @@ prepare_repo_db() {
exit 1 exit 1
fi fi
try_lock_file() {
(set -o noclobber; echo "$$" > "$LOCKFILE") 2> /dev/null
}
# check lock file # check lock file
if ( set -o noclobber; echo "$$" > "$LOCKFILE") 2> /dev/null; then if (( WAIT_LOCK )); then
while ! try_lock_file; do
warning "$(gettext "Failed to acquire lockfile: %s. Retrying in 3 seconds.")" "$LOCKFILE"
sleep 3s
done
CLEAN_LOCK=1
else
if try_lock_file; then
CLEAN_LOCK=1 CLEAN_LOCK=1
else else
error "$(gettext "Failed to acquire lockfile: %s.")" "$LOCKFILE" error "$(gettext "Failed to acquire lockfile: %s.")" "$LOCKFILE"
[[ -f $LOCKFILE ]] && error "$(gettext "Held by process %s")" "$(cat "$LOCKFILE")" [[ -f $LOCKFILE ]] && error "$(gettext "Held by process %s")" "$(cat "$LOCKFILE")"
exit 1 exit 2
fi
fi fi
for repo in "db" "files"; do for repo in "db" "files"; do
@ -605,9 +619,9 @@ trap 'trap_exit "$(gettext "Aborted by user! Exiting...")"' INT
trap 'trap_exit "$(gettext "An unknown error has occurred. Exiting...")"' ERR trap 'trap_exit "$(gettext "An unknown error has occurred. Exiting...")"' ERR
OPT_SHORT="k:npqRsv" OPT_SHORT="k:npqRsvw"
OPT_LONG=('include-sigs' 'key:' 'new' 'nocolor' 'quiet' 'prevent-downgrade' 'remove' OPT_LONG=('include-sigs' 'key:' 'new' 'nocolor' 'quiet' 'prevent-downgrade' 'remove'
'sign' 'verify') 'sign' 'verify' 'wait-for-lock')
if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then if ! parseopts "$OPT_SHORT" "${OPT_LONG[@]}" -- "$@"; then
exit 1 # E_INVALID_OPTION exit 1 # E_INVALID_OPTION
fi fi
@ -639,6 +653,9 @@ while true; do
--include-sigs) --include-sigs)
INCLUDE_SIGS=1 INCLUDE_SIGS=1
;; ;;
-w|--wait-for-lock)
WAIT_LOCK=1
;;
--) --)
shift shift
break break