summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Palfrader <peter@palfrader.org>2012-12-15 16:24:41 +0000
committerweasel <weasel@bc3d92e2-beff-0310-a7cd-cc87d7ac0ede>2012-12-15 16:24:41 +0000
commita91e9967df7530c37ce16494c01201f5c858044f (patch)
treeb58a31ba141b41f022e81e6b137ce895c545d77c
parent87f92dbf2105d8d068e9f5956931968b7cf33c47 (diff)
Merge branch 'shuber'
* shuber: publish-purge: a cleanup script for publish's dir Conflicts: publish-purge git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@565 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede
-rwxr-xr-xpublish-purge74
1 files changed, 38 insertions, 36 deletions
diff --git a/publish-purge b/publish-purge
index 8443cf0..8c464ef 100755
--- a/publish-purge
+++ b/publish-purge
@@ -5,14 +5,14 @@ set -e
test_if_ancient()
{
- d=$1
- ttl=$2
+ local d="$1"
+ local ttl="$2"
- mtime=$(stat --format "%Y" "$d")
- tod=$(($mtime + 3600*24*$ttl))
- now=$(date +%s)
+ mtime="$(stat --format "%Y" "$d")"
+ tod="$(($mtime + 3600*24*$ttl))"
+ now="$(date +%s)"
- [ "$optVerbose" -ge "2" ] && echo "Consider $d: ttl=$ttl, mod time=$mtime, time of death=$tod, now=$now"
+ [ "$optVerbose" -ge "2" ] && echo " Considering $d: ttl=$ttl, mod time=$mtime, time of death=$tod, now=$now"
[ "$tod" -lt "$now" ] && return 0
return 1
@@ -21,13 +21,16 @@ test_if_ancient()
cleanit()
{
- d=$1
- [ -d "$d" ] || exit 0
- [ -e $d/.publish.ttl ] && ttl=`cat $d/.publish.ttl`
+ local d="$1"
+ local ttl="$2"
- if test_if_ancient $d $ttl; then
+ if [ -e "$d/.publish.ttl" ]; then
+ ttl="$(cat "$d/.publish.ttl")"
+ fi
+
+ if test_if_ancient "$d" "$ttl"; then
[ "$optVerbose" -ge "1" ] && echo "Purge $d"
- rm -rf "$d"
+ [ "$optDonot" = "1" ] || rm -rf "$d"
fi
return 0
}
@@ -39,57 +42,56 @@ usage: $0 [OPTIONS]
OPTIONS:
-h Show this message.
+ -n Do not acutally purge directories, but show what would be done.
-v Give verbose output. Be more verbose with -vv.
EOF
}
optVerbose=0
-
-while getopts "vh" OPTION; do
- case $OPTION in
- h)
- usage
- ;;
- v)
- optVerbose=$(($optVerbose + 1))
- ;;
- esac
+optDonot=0
+
+while getopts "hnv" OPTION; do
+ case $OPTION in
+ h)
+ usage
+ ;;
+ n)
+ optDonot=1
+ ;;
+ v)
+ optVerbose=$(($optVerbose + 1))
+ ;;
+ esac
done
ttl=14
+base=""
[ -e ~/.publish-purge.cfg ] && . ~/.publish-purge.cfg
-# We would not like that cleanit removes files in directories that mallory set
-# as $base
-if ! stat --format=%a ~/.publish-purge.cfg | egrep -q ".00"; then
- echo "For security reasons ~/.publish-purge.cfg should have no perms for group and others."
-fi
-
if [ -z "$base" ]; then
- echo "Please setup \$base in ~/.publish-purge.cfg"
+ echo >&2 "%0: Please setup \$base in ~/.publish-purge.cfg"
exit 1
fi
if ! [ -e "$base" ]; then
- echo "Unknown base directory $base"
+ echo >&2 "%0: $base does not exist or is not a directory"
exit 1
fi
-cd $base
+cd "$base"
# We would not like to accidentily delete files in the wrong directory, hence
# the paranoia check
if ! [ -e .publish-purge.token ]; then
- echo "Place a .publish-purge.token in $base"
+ echo >&2 "$0: Place a .publish-purge.token in $base"
exit 1
fi
for d in *; do
- pushd . > /dev/null
- # we launch a subshell so that setting $ttl within the subshell
- # is not visible outside the subshell
- ( cleanit $d )
- popd > /dev/null
+ cleanit "$d" "$ttl"
done
+# vim:set et:
+# vim:set ts=4:
+# vim:set shiftwidth=4: