aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSilvio Rhatto <rhatto@riseup.net>2025-09-27 15:25:58 -0300
committerSilvio Rhatto <rhatto@riseup.net>2025-09-27 15:25:58 -0300
commit8caf497cb6ab0315f97ed01413a05f4785505d50 (patch)
treeef883154fcd9541b58cb92aaf4c03ce862373d0c
parente1e30b6623d02f2e59e38aae5e616a13c44c82f0 (diff)
downloadutils-doc-main.tar.gz
utils-doc-main.tar.bz2
Feat: docshower: lock file handlingmain
-rwxr-xr-xdocshower55
1 files changed, 55 insertions, 0 deletions
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