From 87f92dbf2105d8d068e9f5956931968b7cf33c47 Mon Sep 17 00:00:00 2001 From: Peter Palfrader Date: Sat, 15 Dec 2012 15:33:43 +0000 Subject: publish-purge: a cleanup script for publish's dir publish-purge considers for each directory in pubish's $base directory a TTL (time to live) that is configured by ~/.publish-purge.cfg and is overriden by a local .publish.ttl file. A new option -t in publish lets publish include a local .publish.ttl file if we wish to set a specific TTL. Signed-off-by: Peter Palfrader git-svn-id: svn+ssh://asteria.noreply.org/svn/weaselutils/trunk@564 bc3d92e2-beff-0310-a7cd-cc87d7ac0ede --- publish | 12 +++++++- publish-purge | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100755 publish-purge diff --git a/publish b/publish index f76e645..0623e2a 100755 --- a/publish +++ b/publish @@ -77,6 +77,7 @@ OPTIONS: -q Produce a QR code. -r Add --relative option to rsync so that path names of the given files are preserved at the remote host. + -t days time to live in days -x Publish the contents of the xclipboard. EOF } @@ -117,7 +118,7 @@ make_qrcode=0 add_default_cs=0 relative="" -while getopts "rhnq8HSxs:" OPTION +while getopts "rhnq8HSxs:t:" OPTION do case $OPTION in h) @@ -154,6 +155,9 @@ do r) relative="--relative" ;; + t) + ttl="$OPTARG" + ;; *) usage >&2 exit 1 @@ -250,6 +254,12 @@ if command -v xclip >/dev/null 2>&1; then fi +if [ -n "${ttl}" ]; then + echo "${ttl}" >> $tmpdir/.publish.ttl + set dummy "$tmpdir/.publish.ttl" "$@" + shift +fi + while [ "$#" -gt 0 ]; do file="$1" shift diff --git a/publish-purge b/publish-purge new file mode 100755 index 0000000..8443cf0 --- /dev/null +++ b/publish-purge @@ -0,0 +1,95 @@ +#!/bin/bash + +set -e + + +test_if_ancient() +{ + d=$1 + ttl=$2 + + 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" + + [ "$tod" -lt "$now" ] && return 0 + return 1 +} + + +cleanit() +{ + d=$1 + [ -d "$d" ] || exit 0 + [ -e $d/.publish.ttl ] && ttl=`cat $d/.publish.ttl` + + if test_if_ancient $d $ttl; then + [ "$optVerbose" -ge "1" ] && echo "Purge $d" + rm -rf "$d" + fi + return 0 +} + +usage() +{ + cat << EOF +usage: $0 [OPTIONS] + +OPTIONS: + -h Show this message. + -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 +done + + +ttl=14 +[ -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" + exit 1 +fi + +if ! [ -e "$base" ]; then + echo "Unknown base directory $base" + exit 1 +fi + +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" + 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 +done + + -- cgit v1.2.3