From: Silvio Rhatto Date: Sat, 27 Sep 2025 18:25:58 +0000 (-0300) Subject: Feat: docshower: lock file handling X-Git-Url: https://gitweb.fluxo.info/?a=commitdiff_plain;h=8caf497cb6ab0315f97ed01413a05f4785505d50;p=utils-doc.git Feat: docshower: lock file handling --- diff --git a/docshower b/docshower index 102baae..d7a06a5 100755 --- a/docshower +++ b/docshower @@ -16,9 +16,61 @@ ARG="$1" EXTRA_ARG="$2" DATE="`date +%s`" MAX_AGE="86400" +LOCK="$DOCS/.sync-media.lock" + +# Fatal error +# Adapted from borger +function fatal { + info [fatal] $* + exit 1; +} + +# Create lockfile +# Adapted from borger +function __set_lockfile { + if [ ! -z "$LOCKFILE" ]; then + mkdir -p `dirname $LOCKFILE` + + if ( set -o noclobber; echo "$$" > "$LOCKFILE" ) &> /dev/null; then + trap '__unset_lockfile' INT TERM EXIT + else + fatal "Could not create lockfile $LOCKFILE, exiting" + fi + fi +} + +# Remove lockfile +# Adapted from borger +function __unset_lockfile { + if [ ! -z "$LOCKFILE" ]; then + rm -f $LOCKFILE || echo "Could not remove lockfile $LOCKFILE" + fi +} + +# Check lockfile +# Adapted from borger +function __check_lockfile { + local pid process + + if [ ! -z "$LOCKFILE" ] && [ -f "$LOCKFILE" ]; then + pid="`cat $LOCKFILE`" + process="`ps --no-headers -o comm $pid`" + + if [ "$?" == "0" ] && [ "`ps --no-headers -o comm $$`" == "$process" ]; then + fatal "Another program is running for $LOCKFILE, skipping run" + else + echo "Found old lockfile $LOCKFILE, removing it" + __unset_lockfile + fi + fi +} # Update the filelist function __update_filelist { + # Lock file handling + __check_lockfile + __set_lockfile + echo "Generating new filelist..." # Unnanex if it was erroneously annexed @@ -31,6 +83,9 @@ function __update_filelist { # Stage git add $LIST + + # Unlock file handling + __unlock_repo } # Check