From 9eb1cd7b9403533c4b60ecfbbbf00a08c211059a Mon Sep 17 00:00:00 2001
From: Pierre Schmitz <pierre@archlinux.de>
Date: Sun, 8 Aug 2010 15:03:27 +0200
Subject: Move common function to db-functions

db-functions now sets an individual $WORKDIR and implements trap functinos that
remove locks on exit or error. There are new functions to lock and unlock the running script.

misc-scripts/ftpdir-cleanup was renamed to ftpdir-cleanup-repo as the cron-job had the same name.
Script names have to be unique when using db-functions.
---
 misc-scripts/ftpdir-cleanup-repo | 207 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 207 insertions(+)
 create mode 100755 misc-scripts/ftpdir-cleanup-repo

(limited to 'misc-scripts/ftpdir-cleanup-repo')

diff --git a/misc-scripts/ftpdir-cleanup-repo b/misc-scripts/ftpdir-cleanup-repo
new file mode 100755
index 0000000..12f726c
--- /dev/null
+++ b/misc-scripts/ftpdir-cleanup-repo
@@ -0,0 +1,207 @@
+#!/bin/bash
+
+if [ $# -ne 1 ]; then
+	echo "usage: $(basename $0) <reponame>"
+	exit 1
+fi
+
+reponame=$1
+
+############################################################
+
+. "$(dirname $0)/../db-functions"
+. "$(dirname $0)/../config"
+
+clean_pkgs () {
+    for pkg in "$@"; do
+        if [ -h "$pkg" ]; then
+            rm -f "$pkg"
+        else
+            mv "$pkg" "$CLEANUP_DESTDIR"
+        fi
+    done
+}
+
+${CLEANUP_DRYRUN} && echo 'dry run mode is active'
+
+ftppath_base="$FTP_BASE/$reponame/os"
+
+for arch in ${ARCHES[@]}; do
+
+  repo_lock $reponame $arch $LOCK_TIMEOUT || continue
+
+  CLEANUP_TMPDIR=$(mktemp -d ${WORKDIR}/cleanup-XXXXXX) || exit 1
+  ftppath="$ftppath_base/$arch"
+  MISSINGFILES=""
+  DELETEFILES=""
+  DELETESYMLINKS=""
+  EXTRAFILES=""
+
+  if [ ! -d "$ftppath" ]; then
+      echo "FTP path '$ftppath' does not exist"
+      exit 1
+  fi
+
+  if ! cd "${CLEANUP_TMPDIR}" ; then
+      echo "Failed to cd to ${CLEANUP_TMPDIR}"
+      exit 1
+  fi
+
+  if [ ! -f "$ftppath/$reponame$DBEXT" ]; then
+    echo ""
+    echo "WARNING: The file \"$ftppath/$reponame$DBEXT\" could not be found, skipping."
+    echo ""
+    repo_unlock $reponame $arch
+    continue
+  fi
+
+  if ! bsdtar xf "$ftppath/$reponame$DBEXT"; then
+    echo ""
+    echo "ERROR: Command failed: bsdtar xf \"$ftppath/$reponame$DBEXT\""
+    exit 1
+  fi
+
+  cd "$ftppath"
+
+  for pkg in $CLEANUP_TMPDIR/*; do
+    [ -f "${pkg}" ] || continue
+    filename=$(grep -A1 '^%FILENAME%$' "${pkg}/desc" | tail -n1)
+
+    if [ ! -e "${filename}" ]; then
+      MISSINGFILES="${MISSINGFILES} ${filename}"
+    else
+      pkgname="$(getpkgname ${filename})"
+      for otherfile in ${pkgname}-*; do
+        if [ "${otherfile}" != "${filename}" -a "${pkgname}" = "$(getpkgname ${otherfile})" ]; then
+          if [ -h "${otherfile}" ]; then
+            DELETESYMLINKS="${DELETESYMLINKS} ${otherfile}"
+          else
+            DELETEFILES="${DELETEFILES} ${otherfile}"
+          fi
+        fi
+      done
+    fi
+  done
+
+  for pkg in *$PKGEXT; do
+      if [ ! -e "$pkg" ]; then
+          continue
+      fi
+      pkgname="$(getpkgname $pkg)"
+      for p in ${CLEANUP_TMPDIR}/${pkgname}-*; do
+          [ ! -d "${p}" ] || continue 2
+          dbpkgname=$(grep -A1 '^%FILENAME%$' "${p}/desc" 2>/dev/null| tail -n1)
+          if [ "${dbpkgname}" = "${pkgname}" ]; then
+              continue 2
+          fi
+      done
+      EXTRAFILES="$EXTRAFILES $pkg"
+  done
+
+  rm -rf ${CLEANUP_TMPDIR}
+
+  # Do a quick check to see if a missing ARCHINDEPFILE is in the any dir
+  # If it is, and the file is MISSING, restore it
+  missfiles="$MISSINGFILES"
+  MISSINGFILES=""
+  for mf in $missfiles; do
+      if [ -e "${ftppath_base}/any/${mf}" ]; then
+          echo "Restoring missing 'any' symlink: ${mf}"
+          ${CLEANUP_DRYRUN} || ln -s "../any/${mf}" "${ftppath}"
+      else
+          MISSINGFILES="${MISSINGFILES} ${mf}"
+      fi
+  done
+
+  repo_unlock $reponame $arch
+
+  #Make sure we've done *something* before outputting anything
+  if [ -z "$DELETEFILES$DELETESYMLINKS$MISSINGFILES$EXTRAFILES" ]; then
+      continue
+  fi
+
+  echo "Scan complete for $reponame ($arch) at ${ftppath}"
+
+  if [ -n "$DELETEFILES" ]; then
+      echo "    The following files are out of date"
+      for f in $DELETEFILES; do
+          echo "        $f"
+      done
+      echo ""
+  fi
+
+  if [ -n "$DELETESYMLINKS" ]; then
+      echo "    The following symlinks are out of date"
+      echo "    They will be deleted"
+      for f in $DELETESYMLINKS; do
+          echo "        $f"
+      done
+      echo ""
+  fi
+
+  if [ -n "$MISSINGFILES" ]; then
+      echo "    The following files are missing in the repo"
+      for f in $MISSINGFILES; do
+          echo "        $f"
+      done
+      echo ""
+  fi
+
+  if [ -n "$EXTRAFILES" ]; then
+      echo "    The following files are in the repo but not the db"
+      for f in $EXTRAFILES; do
+          echo "        $f"
+      done
+  fi
+
+  if [ -n "${DELETEFILES}" ]; then
+      ${CLEANUP_DRYRUN} || clean_pkgs ${DELETEFILES}
+      echo ""
+  fi
+
+  if [ -n "${DELETESYMLINKS}" ]; then
+      ${CLEANUP_DRYRUN} || clean_pkgs ${DELETESYMLINKS}
+      echo ""
+  fi
+
+  if [ -n "${EXTRAFILES}" ]; then
+      ${CLEANUP_DRYRUN} || clean_pkgs ${EXTRAFILES}
+      echo ""
+  fi
+
+done
+
+ARCHINDEPFILES=""
+
+if [ -d "$ftppath_base/any" ]; then
+    cd "$ftppath_base/any"
+    for pkg in *$PKGEXT; do
+        [ -f "$pkg" ] || continue # in case we get a file named "*.pkg.tar.gz"
+        found=0
+        #check for any existing symlinks
+        for arch in ${ARCHES[@]}; do
+            if [ -h "$ftppath_base/$arch/$pkg" ]; then
+                found=1
+                break
+            fi
+        done
+        if [ $found -eq 0 ]; then
+            # We found no symlinks to this, delete it
+            ARCHINDEPFILES="$ARCHINDEPFILES $pkg"
+        fi
+    done
+fi
+
+if [ -n "$ARCHINDEPFILES" ]; then
+  echo "    The following architecture independent packages"
+  echo "    are not symlinked in the architecture repositories."
+    for f in $ARCHINDEPFILES; do
+        echo "        $f"
+    done
+fi
+
+if [ -d "$ftppath_base/any" -a -n "${ARCHINDEPFILES}" ]; then
+  cd "$ftppath_base/any"
+  ${CLEANUP_DRYRUN} || clean_pkgs ${ARCHINDEPFILES}
+  echo ""
+fi
-- 
cgit v1.2.3-2-g168b