home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
misc
/
volume39
/
trm105
/
part01
/
trm
Wrap
Text File
|
1993-09-07
|
12KB
|
414 lines
#!/bin/sh
#
# Trevor's rm. Version 1.05
#
# It's 'rm' but more... It can handle removeing directories.. recovering
# 'removed' files and undoing the last 'remove'.. read on..
#
# dont't forget to chmod 755 this file after installing it
#
# trm file [file..]
# will MOVE the named files to the $TRMTMP directory
# If the file being moved already has a version under $TRMTMP then
# the version number is incremented and the file is moved.
# EX: trm gm.c gm.c is currently not in TRMTMP)
# TRMTMP would now contail a file called gm.c,01
# This is the first version of gm.c under TRMTMP
#
# trm gm.c there already is a 'gm.c' under TRMTMP. This
# file would be moved to gm.c,02. It is the second
# version of gm.c
#
# trm -list | -l [file..]
# will list the named files that are in the $TRMTMP directory
# This will list all the versions of the named files.
# If no file is given then the entire contents of TRMTMP are listed
#
# trm -recover | -rc file [file..]
# will recover the biggest version of the named files from the
# $TRMTMP directory to `pwd`
# EX: trm -rc gm.c There is are two versions of gm.c in TRMTMP.
# gm.c,01 and gm.c,02. trm will grab gm.c,02
# and recover it as gm.c
#
# trm -undo | -un
# will undo the last rm
# meaning you accidently removed a bunch of files and instead of
# listing them one by one and recovering each.. this will do all of
# them for you in one shot..
# NOTE: THIS WILL NOT AUTOMAGICALLY PUT THE FILES BACK FROM WHENCE
# THEY CAME. IT WILL PUT THE FILES TO `pwd`
#
# trm -listundo | -lun
# will list the undo files
#
# trm -purge | -p [file..]
# will REMOVE ALL VERSIONS of the named files that are in the
# $TRMTMP directory
# [no files named will remove the $TRMTMP directory completly and then
# remake it]
#
# ---------------------------------------------------------------------------
# Version 1.05 Aug 31 1993
#
# Bug Fix. grab the last part of the file to remove. ie:basename $file
#
# Version 1.04 Aug 26 1993
#
# Bug Fix. when removeing a dir that already exists it would try to use the last
# entry in the dir as the version number.. bad. Use ls -1d, not ls -1
#
# Version 1.03 Aug 25 1993
#
# Add version information.
# Thanx to montanaro@ausable.crd.ge.com (Skip Montanaro) for this.
# Removing a dir/file that already exists in $TRMTMP, will cause it
# to be set to the next 'version' of that file. Recovering a file/dir
# will grab the biggest version.
# Add basename $0 for getting the program name
#
# Version 1.02 Aug 25 1993
#
# Add short form options
# Bug Fix. enclose TRMDEBUG in quotes wherever needed
#
# Version 1.01 Aug 23 1993
#
# - add TRMDEBUG to set -x and -v for debugging
# - add -purge option
# - add check for moving a file ontop of existing one (ONLY if moved failed)
#
# Version 1.00 July 22 1993
#
# Initial release
#
# ===========================================================================
# How to Setup
# ===========================================================================
# I put the following in my .cshrc to create the trmlast and trmtmp
# variables for each shell, only if interactive
# if ( $?prompt ) then
# # create trm variables
# # NOTE: TRMLOCK is created in the .login
# setenv TRMLAST $HOME/.trmtmp/.trmlast.`hostname`.$$
# setenv TRMTMP $HOME/.trmtmp
# fi
# You may also want to put the following remove, recover and listing
# aliases in your .cshrc
# alias rm 'echo "Use trm instead"'
# alias rc 'trm -recover \!*'
# alias prc 'trm -purge \!*'
# alias lsrc 'trm -list \!* | more'
# alias rcu 'trm -undo'
# alias lsrcu 'trm -listundo | more'
# ---------------------------------------------------------------------------
# I also put the following in my .login
# # create recover directory.
# # Only the process on the machine that created the directory should be able
# # to remove it
# # NOTE: $TRMTMP and $TRMLAST are created in the .cshrc
# setenv TRMLOCK $HOME/.trmlock.`hostname`.$$
# if !( -d $TRMTMP ) then
# mkdir $TRMTMP
# chmod 700 $TRMTMP
# echo "$TRMTMP created at `date`. Lockfile is $TRMLOCK" > $TRMLOCK
# fi
# ---------------------------------------------------------------------------
# I also put the following in my .logout
# if ( -f $TRMLOCK ) then
# /bin/rm -rf $TRMTMP
# /bin/rm -f $TRMLOCK
# fi
# ===========================================================================
# start of trm
# ===========================================================================
# get program name
prog=`basename $0`
# setenv TRMDEBUG to 1 to display verbose debugging info
if [ "$TRMDEBUG" = "1" ]; then
set -x
set -v
fi
# if $TRMTMP is not set then look under $home/.trmtmp
if [ "$TRMTMP" = "" ]; then
TRMTMP=$HOME/.trmtmp
fi
# make sure $TRMTMP is a directory
if [ ! -d $TRMTMP ]; then
echo $prog: ERROR: $TRMTMP is not a directory or does not exist
exit 1
fi
# if no arguments passed then return
if [ $# -eq 0 ]; then
exit 0
fi
# ---------------------------------------------------------------------------
# handle listing
# ---------------------------------------------------------------------------
if [ "$1" = "-list" -o "$1" = "-l" ]; then
# shift out -list from the arg list
shift
# if no futher args then list the directory
if [ $# -eq 0 ]; then
ls -laug $TRMTMP
exit 0
fi
# cd to the directory and list each argument individually
cd $TRMTMP
for i in $*; do
ls -laug $i,*
done
exit 0
fi
# ---------------------------------------------------------------------------
# handle recover
# ---------------------------------------------------------------------------
if [ "$1" = "-recover" -o "$1" = "-rc" ]; then
# shift out -recover from the arg list
shift
# if no futher args then exit
if [ $# -eq 0 ]; then
exit 0
fi
# the destination directory is where you are now
destdir=`pwd`
# cd to the TRMTMP directory
cd $TRMTMP
# for each arg passed try to 'mv' it to the destination dir
for i in $*; do
# get back the biggest version
rev=`(ls -1d $TRMTMP/$i,* 2>/dev/null) |\
sort |\
tail -1 |\
sed -e 's/.*,\([0-9]*\)/\1/'`
/bin/mv -f $i,$rev $destdir/$i > /dev/null 2>&1
if [ $? != 0 ]; then
# the move failed...
# probably tried to remove a file that already exists in $TRMTMP
if [ -d $destdir/$i ]; then
echo $prog: ${i} already exists in $destdir as a dir, move if needed.
elif [ -f $destdir/$i ]; then
echo $prog: ${i} already exists in $destdir as a file, move if needed.
elif [ -d $i ]; then
# tried to move a directory.. most likely failure is trying to move
# a directory across filesystems.. sooo lets tar up the whole thing
# and then untar it in the destination directory
tar -cBf - $i,$rev | ( cd $destdir ; tar -xBpf - )
mv $destdir/$i,$rev $destdir/$i
# now remove the directory from $TRMTMP
/bin/rm -rf $i,$rev
fi
fi
done
exit 0
fi
# ---------------------------------------------------------------------------
# handle undo
# ---------------------------------------------------------------------------
if [ "$1" = "-undo" -o "$1" = "-un" ]; then
# the destination directory is where you are now
destdir=`pwd`
# cd to the TRMTMP directory
cd $TRMTMP
# if $TRMLAST is not set then we have nothing to do
if [ "$TRMLAST" = "" ]; then
exit 0
fi
# if $TRMLAST does not exist then we have nothing to do
if [ ! -f $TRMLAST ]; then
exit 0
fi
# foreach file in the $TRMLAST list try to 'mv' it to the destination dir
for i in `cat $TRMLAST`; do
# get back the biggest version
rev=`(ls -1d $TRMTMP/$i,* 2>/dev/null) |\
sort |\
tail -1 |\
sed -e 's/.*,\([0-9]*\)/\1/'`
/bin/mv -f $i,$rev $destdir/$i > /dev/null 2>&1
if [ $? != 0 ]; then
# the move failed...
# probably tried to remove a file that already exists in $TRMTMP
if [ -d $destdir/$i ]; then
echo $prog: ${i} already exists in $destdir as a dir, move if needed.
elif [ -f $destdir/$i ]; then
echo $prog: ${i} already exists in $destdir as a file, move if needed.
elif [ -d $i ]; then
# tried to move a directory.. most likely failure is trying to move
# a directory across filesystems.. sooo lets tar up the whole thing
# and then untar it in the destination directory
tar -cBf - $i,$rev | ( cd $destdir ; tar -xBpf - )
mv $destdir/$i,$rev $destdir/$i
# now remove the directory from $TRMTMP
/bin/rm -rf $i,$rev
fi
fi
done
# at this point we want to reset the $TRMLAST file list
/bin/rm -f $TRMLAST
echo > $TRMLAST
exit 0
fi
# ---------------------------------------------------------------------------
# handle listundo
# ---------------------------------------------------------------------------
if [ "$1" = "-listundo" -o "$1" = "-lun" ]; then
# if $TRMLAST is not set then we have nothing to do
if [ "$TRMLAST" = "" ]; then
exit 0
fi
# if $TRMLAST does not exist then we have nothing to do
if [ ! -f $TRMLAST ]; then
exit 0
fi
# cat the $TRMLAST file
cat $TRMLAST
exit 0
fi
# ---------------------------------------------------------------------------
# handle purge
# ---------------------------------------------------------------------------
if [ "$1" = "-purge" -o "$1" = "-p" ]; then
# shift out -purge from the arg list
shift
# if no futher args then remove $TRMTMP and remake it
if [ $# -eq 0 ]; then
/bin/rm -rf $TRMTMP
mkdir $TRMTMP
exit 0
fi
# cd to the directory and remove each argument individually
cd $TRMTMP
for i in $*; do
/bin/rm -rf $i,*
done
exit 0
fi
# ---------------------------------------------------------------------------
# handle remove
# ---------------------------------------------------------------------------
# at this point we want to reset the old $TRMLAST file list
if [ "$TRMLAST" != "" ]; then
/bin/rm -f $TRMLAST
fi
for i in $*; do
base=`basename $i`
dir=`dirname $i`
# if the filename passed does not exist then echo what 'rm' would
# for now only deal with files and dirs..
if [ ! -f $i -a ! -d $i ]; then
echo $prog: ${i}: No such file or directory
else
# figure out a version number to move the file to
rev=`(ls -1d $TRMTMP/$base,* 2>/dev/null) |\
sort |\
tail -1 |\
sed -e 's/.*,\([0-9]*\)/\1/'`
if [ "$rev" = "" ] ; then
rev=1;
else
rev=`expr $rev + 1`
fi
if [ "$rev" = "" ]; then
rev=01
fi
rev=`echo $rev | awk '{if(length($1)==1){print 0$1}else{print $1}}'`
if [ $rev -gt 99 ]; then
echo $prog: ${i} already has 99 versions, staying at 99
rev=99
fi
# try to move the file to $TRMTMP
/bin/mv -f $i $TRMTMP/$base,$rev > /dev/null 2>&1
if [ $? != 0 ]; then
# the move failed
# probably tried to remove a file that already exists in $TRMTMP
if [ -d $i ]; then
# tried to move a directory.. most likely failure is trying to move
# a directory across filesystems.. sooo lets tar up the whole thing
# and then untar it in the $TRMTMP directory
pd=`pwd`
cd $dir
tar -cBf - $base | ( cd $TRMTMP ; tar -xBf - ) > /dev/null 2>&1
mv $TRMTMP/$base $TRMTMP/$base,$rev
# now remove the directory
/bin/rm -rf $base
cd $pd
fi
fi
# at this point the 'remove' was succesfull. Add the filename to the
# $TRMLAST file for possible undoing.
if [ "$TRMLAST" != "" ]; then
echo $i >> $TRMLAST
fi
fi
done