]> gitweb.fluxo.info Git - utils-doc.git/commitdiff
Feat: docshower: lock file handling main
authorSilvio Rhatto <rhatto@riseup.net>
Sat, 27 Sep 2025 18:25:58 +0000 (15:25 -0300)
committerSilvio Rhatto <rhatto@riseup.net>
Sat, 27 Sep 2025 18:25:58 +0000 (15:25 -0300)
docshower

index 102baaec141f12de96031702fadb63d20e1ef976..d7a06a5b49abdaee1249f4284cfe7e142cb831f2 100755 (executable)
--- 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