#!/bin/sh if [ ! -d stores ]; then echo "No stores there!" exit 1 fi # small hack, try to find a decent version of sed # like GNUsed, usually installed in /usr/local/bin if [ "$SED" = "" ]; then if [ -f /usr/local/bin/sed ]; then SED=/usr/local/bin/sed elif [ -f /usr/bin/sed ]; then SED=/usr/bin/sed else SED=sed fi fi echo \*\*\* Store cleaning started at `date` echo \*\*\* Cleaning backup stores find stores -name \*.bak -exec rm '{}' \; echo \*\*\* Removing unused stores indexFile=`ls stores/*-index.xml | sed 's,.*/,,'` storeList=`grep repository stores/$indexFile | sed 's/.*>\(.*\)<.*/\1/'` # try to find any duplicated root resource rootKey="" dupKey=0 echo $storeList | sed 's/root\.xml//' | grep root.xml > /dev/null if [ $? -eq 0 ]; then echo \*\*\* Duplicated entry for root resource dupKey=1 fi state=0 for i in `cat stores/root.xml | sed '/^\ \ root\< > /dev/null if [ $? -eq 0 ]; then if [ $state -eq 1 ]; then echo WARNING two root entries in root.xml. Inspect manually exit 1 fi state=1 fi echo $i | grep name=\'key\' > /dev/null if [ $? -eq 0 ]; then if [ $state -eq 1 ]; then rootKey=`echo $i | sed 's/.*>\(.*\)<.*/\1/'` state=0 fi fi done echo \*\*\* RootKey is $rootKey # remove all the stores in X/st-Y and find all the stores that are only # in the index for i in `find stores -name st-\[0-9\]* -print | grep -v bak | grep -v backup | sed 's,stores/,,'` ; do isThere=0 for j in $storeList ; do if [ "$j" = "$i" ]; then isThere=1 indexStoreList=`echo $indexStoreList $i` break fi done if [ $isThere -eq 0 ]; then echo $i is NOT there... removing it! rm -f stores/$i fi done # find all the stores in the index that are not in the store directory for i in $storeList ; do if [ "$i" = "root.xml" ]; then continue fi isThere=0 for j in $indexStoreList ; do if [ "$j" = "$i" ]; then isThere=1 break fi done if [ $isThere -eq 0 ]; then unexistentStoreList=`echo $unexistentStoreList $i` fi done echo \*\*\* Rebuilding cleanly the index file $indexFile # rebuild the index file (usually http-server-index.xml) remove all # the @@NULL@@ stores, the duplicate root resource (if there) and # all the entries linked to repository that are not there state=0 echo "" > stores/$indexFile.new echo "" >> stores/$indexFile.new for i in `cat stores/$indexFile | sed -e 's/.*repository.*>\(.*\)<.*/repository=\1/' -e 's/.*key.*>\(.*\)<.*/key=\1/' -e 's//'` ; do if [ "$i" = "" ]; then if [ $state -eq 0 ]; then echo Unstarted resource, aborting break fi if [ "$currentStore" = "@@NULL@@" ]; then echo Removing @@NULL@@ entry state=0 currentStore="" nullKeys=`echo $nullKeys $currentKey` currentKey="" continue fi if [ "$currentStore" = "root.xml" ]; then if [ "$currentKey" != "$rootKey" ]; then if [ $dupKey -eq 1 ]; then echo Removing duplicate root entry $currentKey state=0 currentStore="" nullKeys=`echo $nullKeys $currentKey` currentKey="" continue fi echo Fixing wrong reference for root key currentKey=$rootKey fi fi for j in $unexistentStoreList ; do if [ "$j" = "$currentStore" ]; then echo Removing non-existent store $j state=0 currentStore="" nullKeys=`echo $nullKeys $currentKey` currentKey="" continue fi done if [ $state -eq 0 ]; then continue fi echo " " >> stores/$indexFile.new echo " "$currentStore"" >> stores/$indexFile.new echo " "$currentKey"" >> stores/$indexFile.new echo " " >> stores/$indexFile.new continue fi echo $i | grep key > /dev/null if [ $? -eq 0 ]; then currentKey=`echo $i | sed 's/.*=//'` state=1 continue fi echo $i | grep repository > /dev/null if [ $? -eq 0 ]; then currentStore=`echo $i | sed 's/.*=//'` state=1 continue fi done echo "" >> stores/$indexFile.new mv stores/$indexFile stores/$indexFile.old mv stores/$indexFile.new stores/$indexFile echo \*\*\* Creating the sed script to clean all stores with unreferenced keys # we are now creating a sed script that will clean every store # it accepts all the known keys and destroy all container with an unknown key keyList=`grep key stores/$indexFile | sed 's/.*>\(.*\)<.*/\1/'` cat > resource.sed << EOF /^ / { w resource.rej d } /\n <\/resource>$/ { w resource.rej d } N b Bogus :Res /^ <\/resource>$/ { p d } /\n <\/resource>$/ { p d } /'key'/ { b Key } N b Res :Key EOF for i in $keyList ; do echo "/ "$i"<\/attribute>$/ {" >> resource.sed cat >> resource.sed << EOF p d } EOF done cat >> resource.sed << EOF / /dev/null > /dev/null # if the sed implementation is not good enough to handle more than 256 # commands, ask people to manually inspect the stores if [ $? -eq 0 ]; then echo \*\*\* Purging all stores for i in stores/root.xml `find stores -name st-\[0-9\]* -print | grep -v backup` ; do $SED -n -f resource.sed $i > $i.new if [ $? -eq 0 ]; then mv $i.new $i else echo store `echo $i | sed 's,stores/,,'` was not processed due to errors fi if [ -s resource.rej ]; then mv resource.rej $i.rej fi done else echo WARNING your sed implementation does not allow enough commands echo \*\*\* Checking for null repositories linked $nullKeys for i in `find stores -name st-\[0-9\]* -print | grep -v backup` ; do for j in `echo $nullKeys` ; do grep \>$j\< $i > /dev/null if [ $? -eq 0 ]; then echo \*\*\* Store `echo $i | sed 's,stores/,,'` needs inspection for key $j fi done done fi # do some cleanup rm -f resource.sed resource.rej 2>/dev/null > /dev/null echo \*\*\* Store cleaning finished at `date`