home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume39 / trm105 / part01 < prev    next >
Encoding:
Text File  |  1993-09-07  |  13.9 KB  |  464 lines

  1. Newsgroups: comp.sources.misc
  2. From: tpaquette@ita.lgc.com (Trevor Paquette)
  3. Subject: v39i074:  trm105 - rm replacement, does remove and recover, Part01/01
  4. Message-ID: <1993Sep7.132715.3184@sparky.sterling.com>
  5. X-Md4-Signature: bae600b25630dcc25fab6f04e82301e8
  6. Sender: kent@sparky.sterling.com (Kent Landfield)
  7. Reply-To: tpaquette@ita.lgc.com
  8. Organization: Landmark/ITA
  9. Date: Tue, 7 Sep 1993 13:27:15 GMT
  10. Approved: kent@sparky.sterling.com
  11.  
  12. Submitted-by: tpaquette@ita.lgc.com (Trevor Paquette)
  13. Posting-number: Volume 39, Issue 74
  14. Archive-name: trm105/part01
  15. Environment: UNIX
  16. Supersedes: trm: Volume 39, Issue 59
  17.  
  18. This fixes a bug removing files not located in the current working directory.
  19.  
  20. #! /bin/sh
  21. # This is a shell archive.  Remove anything before this line, then unpack
  22. # it by saving it into a file and typing "sh file".  To overwrite existing
  23. # files, type "sh file -c".  You can also feed this as standard input via
  24. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  25. # will see the following message at the end:
  26. #        "End of shell archive."
  27. # Contents:  trm
  28. # Wrapped by tpaquett@ita.lgc.com on Tue Aug 31 08:33:00 1993
  29. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  30. if test -f 'trm' -a "${1}" != "-c" ; then 
  31.   echo shar: Will not clobber existing file \"'trm'\"
  32. else
  33. echo shar: Extracting \"'trm'\" \(11934 characters\)
  34. sed "s/^X//" >'trm' <<'END_OF_FILE'
  35. X#!/bin/sh
  36. X#
  37. X# Trevor's rm. Version 1.05
  38. X# 
  39. X#  It's 'rm' but more... It can handle removeing directories.. recovering
  40. X#  'removed' files and undoing the last 'remove'.. read on..
  41. X#
  42. X#  dont't forget to chmod 755 this file after installing it
  43. X#
  44. X#  trm file [file..]
  45. X#    will MOVE the named files to the $TRMTMP directory
  46. X#    If the file being moved already has a version under $TRMTMP then
  47. X#    the version number is incremented and the file is moved.
  48. X#        EX: trm gm.c      gm.c is currently not in TRMTMP)
  49. X#                          TRMTMP would now contail a file called gm.c,01
  50. X#                           This is the first version of gm.c under TRMTMP
  51. X#              
  52. X#            trm gm.c      there already is a 'gm.c' under TRMTMP. This
  53. X#               file would be moved to gm.c,02. It is the second
  54. X#                          version of gm.c
  55. X#
  56. X#  trm  -list | -l  [file..]
  57. X#    will list the named files that are in the $TRMTMP directory
  58. X#      This will list all the versions of the named files.
  59. X#      If no file is given then the entire contents of TRMTMP are listed
  60. X#
  61. X#  trm  -recover | -rc  file [file..]
  62. X#    will recover the biggest version of the named files from the
  63. X#    $TRMTMP directory to `pwd`
  64. X#        EX: trm -rc gm.c     There is are two versions of gm.c in TRMTMP.
  65. X#                  gm.c,01 and gm.c,02. trm will grab gm.c,02
  66. X#                  and recover it as gm.c
  67. X#
  68. X#  trm  -undo | -un
  69. X#    will undo the last rm
  70. X#     meaning you accidently removed a bunch of files and instead of
  71. X#        listing them one by one and recovering each.. this will do all of
  72. X#        them for you in one shot..
  73. X#        NOTE: THIS WILL NOT AUTOMAGICALLY PUT THE FILES BACK FROM WHENCE
  74. X#              THEY CAME. IT WILL PUT THE FILES TO `pwd`
  75. X#
  76. X#  trm  -listundo | -lun
  77. X#    will list the undo files
  78. X#
  79. X#  trm  -purge | -p  [file..]
  80. X#    will REMOVE ALL VERSIONS of the named files that are in the
  81. X#      $TRMTMP directory
  82. X#    [no files named will remove the $TRMTMP directory completly and then
  83. X#     remake it]
  84. X#
  85. X# ---------------------------------------------------------------------------
  86. X# Version 1.05  Aug 31 1993
  87. X#
  88. X# Bug Fix. grab the last part of the file to remove. ie:basename $file
  89. X#
  90. X# Version 1.04  Aug 26 1993
  91. X#
  92. X# Bug Fix. when removeing a dir that already exists it would try to use the last
  93. X#   entry in the dir as the version number.. bad. Use ls -1d, not ls -1
  94. X#
  95. X# Version 1.03  Aug  25 1993
  96. X#
  97. X#  Add version information.
  98. X#    Thanx to montanaro@ausable.crd.ge.com (Skip Montanaro) for this.
  99. X#    Removing a dir/file that already exists in $TRMTMP, will cause it
  100. X#    to be set to the next 'version' of that file. Recovering a file/dir
  101. X#    will grab the biggest version.
  102. X#  Add basename $0 for getting the program name
  103. X#
  104. X# Version 1.02  Aug  25 1993
  105. X#
  106. X#  Add short form options
  107. X#  Bug Fix. enclose TRMDEBUG in quotes wherever needed
  108. X#
  109. X# Version 1.01  Aug  23 1993
  110. X#
  111. X#   - add TRMDEBUG to set -x and -v for debugging
  112. X#   - add -purge option
  113. X#   - add check for moving a file ontop of existing one (ONLY if moved failed)
  114. X#
  115. X# Version 1.00  July 22 1993
  116. X#
  117. X#  Initial release
  118. X#
  119. X# ===========================================================================
  120. X#  How to Setup
  121. X# ===========================================================================
  122. X# I put the following in my .cshrc to create the trmlast and trmtmp
  123. X# variables for each shell, only if interactive
  124. X
  125. X# if ( $?prompt ) then 
  126. X#   # create trm variables
  127. X#   # NOTE: TRMLOCK is created in the .login
  128. X#   setenv TRMLAST $HOME/.trmtmp/.trmlast.`hostname`.$$
  129. X#   setenv TRMTMP  $HOME/.trmtmp
  130. X# fi
  131. X
  132. X# You may also want to put the following remove, recover and listing
  133. X# aliases in your .cshrc
  134. X
  135. X# alias    rm    'echo "Use trm instead"'
  136. X# alias    rc    'trm -recover \!*'
  137. X# alias    prc    'trm -purge \!*'
  138. X# alias    lsrc    'trm -list \!* | more'
  139. X# alias rcu    'trm -undo'
  140. X# alias    lsrcu    'trm -listundo | more'
  141. X
  142. X# ---------------------------------------------------------------------------
  143. X# I also put the following in my .login
  144. X
  145. X# # create recover directory.
  146. X# # Only the process on the machine that created the directory should be able
  147. X# # to remove it
  148. X# # NOTE: $TRMTMP and $TRMLAST are created in the .cshrc
  149. X# setenv TRMLOCK $HOME/.trmlock.`hostname`.$$
  150. X
  151. X# if !( -d $TRMTMP ) then
  152. X#    mkdir $TRMTMP
  153. X#    chmod 700 $TRMTMP
  154. X#    echo "$TRMTMP created at `date`. Lockfile is $TRMLOCK" > $TRMLOCK
  155. X# fi
  156. X
  157. X# ---------------------------------------------------------------------------
  158. X# I also put the following in my .logout
  159. X
  160. X# if ( -f $TRMLOCK ) then
  161. X#   /bin/rm -rf $TRMTMP
  162. X#   /bin/rm -f $TRMLOCK
  163. X# fi
  164. X
  165. X# ===========================================================================
  166. X# start of trm
  167. X# ===========================================================================
  168. X
  169. X# get program name
  170. Xprog=`basename $0`
  171. X
  172. X# setenv TRMDEBUG to 1 to display verbose debugging info
  173. Xif [ "$TRMDEBUG" = "1" ]; then
  174. X   set -x
  175. X   set -v
  176. Xfi
  177. X
  178. X# if $TRMTMP is not set then look under $home/.trmtmp
  179. Xif [ "$TRMTMP" = "" ]; then
  180. X   TRMTMP=$HOME/.trmtmp
  181. Xfi
  182. X
  183. X# make sure $TRMTMP is a directory
  184. Xif [ ! -d $TRMTMP ]; then
  185. X   echo $prog: ERROR: $TRMTMP is not a directory or does not exist
  186. X   exit 1
  187. Xfi
  188. X
  189. X# if no arguments passed then return
  190. Xif [ $# -eq 0 ]; then
  191. X  exit 0
  192. Xfi
  193. X
  194. X# ---------------------------------------------------------------------------
  195. X# handle listing
  196. X# ---------------------------------------------------------------------------
  197. Xif [ "$1" = "-list" -o "$1" = "-l" ]; then
  198. X
  199. X   # shift out -list from the arg list
  200. X   shift
  201. X
  202. X   # if no futher args then list the directory
  203. X   if [ $# -eq 0 ]; then
  204. X     ls -laug $TRMTMP
  205. X     exit 0
  206. X   fi
  207. X
  208. X   # cd to the directory and list each argument individually
  209. X   cd $TRMTMP
  210. X   for i in $*; do
  211. X     ls -laug $i,*
  212. X   done
  213. X
  214. X   exit 0
  215. Xfi
  216. X
  217. X# ---------------------------------------------------------------------------
  218. X# handle recover
  219. X# ---------------------------------------------------------------------------
  220. Xif [ "$1" = "-recover"  -o "$1" = "-rc" ]; then
  221. X
  222. X   # shift out -recover from the arg list
  223. X   shift
  224. X
  225. X   # if no futher args then exit
  226. X   if [ $# -eq 0 ]; then
  227. X     exit 0
  228. X   fi
  229. X
  230. X   # the destination directory is where you are now
  231. X   destdir=`pwd`
  232. X
  233. X   # cd to the TRMTMP directory
  234. X   cd $TRMTMP
  235. X
  236. X   # for each arg passed try to 'mv' it to the destination dir
  237. X   for i in $*; do
  238. X
  239. X     # get back the biggest version
  240. X     rev=`(ls -1d $TRMTMP/$i,* 2>/dev/null) |\
  241. X        sort |\
  242. X        tail -1 |\
  243. X        sed -e 's/.*,\([0-9]*\)/\1/'`
  244. X     
  245. X     /bin/mv -f $i,$rev $destdir/$i > /dev/null 2>&1
  246. X
  247. X     if [ $? != 0 ]; then
  248. X       # the move failed... 
  249. X
  250. X      # probably tried to remove a file that already exists in $TRMTMP
  251. X      if [ -d $destdir/$i ]; then
  252. X    echo $prog: ${i} already exists in $destdir as a dir, move if needed.
  253. X      elif [ -f $destdir/$i ]; then
  254. X        echo $prog: ${i} already exists in $destdir as a file, move if needed.
  255. X      elif [ -d $i ]; then
  256. X         # tried to move a directory.. most likely failure is trying to move
  257. X         # a directory across filesystems.. sooo lets tar up the whole thing
  258. X         # and then untar it in the destination directory
  259. X         tar -cBf - $i,$rev | ( cd $destdir ; tar -xBpf - )
  260. X     mv $destdir/$i,$rev $destdir/$i
  261. X
  262. X     # now remove the directory from $TRMTMP
  263. X         /bin/rm -rf $i,$rev
  264. X
  265. X       fi
  266. X     fi
  267. X   done
  268. X   exit 0
  269. Xfi
  270. X
  271. X# ---------------------------------------------------------------------------
  272. X# handle undo
  273. X# ---------------------------------------------------------------------------
  274. Xif [ "$1" = "-undo" -o "$1" = "-un" ]; then
  275. X
  276. X   # the destination directory is where you are now
  277. X   destdir=`pwd`
  278. X
  279. X   # cd to the TRMTMP directory
  280. X   cd $TRMTMP
  281. X
  282. X   # if $TRMLAST is not set then we have nothing to do
  283. X   if [ "$TRMLAST" = "" ]; then
  284. X     exit 0
  285. X   fi
  286. X
  287. X   # if $TRMLAST does not exist then we have nothing to do
  288. X   if [ ! -f $TRMLAST ]; then
  289. X     exit 0
  290. X   fi
  291. X
  292. X   # foreach file in the $TRMLAST list try to 'mv' it to the destination dir
  293. X   for i in `cat $TRMLAST`; do
  294. X
  295. X     # get back the biggest version
  296. X     rev=`(ls -1d $TRMTMP/$i,* 2>/dev/null) |\
  297. X        sort |\
  298. X        tail -1 |\
  299. X        sed -e 's/.*,\([0-9]*\)/\1/'`
  300. X     
  301. X     /bin/mv -f $i,$rev $destdir/$i > /dev/null 2>&1
  302. X
  303. X     if [ $? != 0 ]; then
  304. X       # the move failed... 
  305. X
  306. X      # probably tried to remove a file that already exists in $TRMTMP
  307. X      if [ -d $destdir/$i ]; then
  308. X    echo $prog: ${i} already exists in $destdir as a dir, move if needed.
  309. X      elif [ -f $destdir/$i ]; then
  310. X        echo $prog: ${i} already exists in $destdir as a file, move if needed.
  311. X      elif [ -d $i ]; then
  312. X         # tried to move a directory.. most likely failure is trying to move
  313. X         # a directory across filesystems.. sooo lets tar up the whole thing
  314. X         # and then untar it in the destination directory
  315. X         tar -cBf - $i,$rev | ( cd $destdir ; tar -xBpf - )
  316. X     mv $destdir/$i,$rev $destdir/$i
  317. X
  318. X     # now remove the directory from $TRMTMP
  319. X         /bin/rm -rf $i,$rev
  320. X
  321. X       fi
  322. X     fi
  323. X   done
  324. X
  325. X   # at this point we want to reset the $TRMLAST file list
  326. X   /bin/rm -f $TRMLAST
  327. X   echo > $TRMLAST
  328. X
  329. X   exit 0
  330. Xfi
  331. X
  332. X# ---------------------------------------------------------------------------
  333. X# handle listundo
  334. X# ---------------------------------------------------------------------------
  335. Xif [ "$1" = "-listundo" -o "$1" = "-lun" ]; then
  336. X
  337. X   # if $TRMLAST is not set then we have nothing to do
  338. X   if [ "$TRMLAST" = "" ]; then
  339. X     exit 0
  340. X   fi
  341. X
  342. X   # if $TRMLAST does not exist then we have nothing to do
  343. X   if [ ! -f $TRMLAST ]; then
  344. X     exit 0
  345. X   fi
  346. X
  347. X   # cat the $TRMLAST file
  348. X   cat $TRMLAST
  349. X
  350. X   exit 0
  351. Xfi
  352. X
  353. X# ---------------------------------------------------------------------------
  354. X# handle purge
  355. X# ---------------------------------------------------------------------------
  356. Xif [ "$1" = "-purge" -o "$1" = "-p" ]; then
  357. X
  358. X   # shift out -purge from the arg list
  359. X   shift
  360. X
  361. X   # if no futher args then remove $TRMTMP and remake it
  362. X   if [ $# -eq 0 ]; then
  363. X     /bin/rm -rf $TRMTMP
  364. X     mkdir $TRMTMP
  365. X     exit 0
  366. X   fi
  367. X
  368. X   # cd to the directory and remove each argument individually
  369. X   cd $TRMTMP
  370. X   for i in $*; do
  371. X     /bin/rm -rf $i,*
  372. X   done
  373. X
  374. X   exit 0
  375. Xfi
  376. X
  377. X# ---------------------------------------------------------------------------
  378. X# handle remove
  379. X# ---------------------------------------------------------------------------
  380. X
  381. X# at this point we want to reset the old $TRMLAST file list
  382. X if [ "$TRMLAST" != "" ]; then
  383. X   /bin/rm -f $TRMLAST
  384. X fi
  385. X
  386. Xfor i in $*; do
  387. X
  388. X  base=`basename $i`
  389. X  dir=`dirname $i`
  390. X
  391. X  # if the filename passed does not exist then echo what 'rm' would  
  392. X  # for now only deal with files and dirs..
  393. X  if [  ! -f $i  -a  ! -d $i  ]; then
  394. X     echo $prog: ${i}: No such file or directory
  395. X  else
  396. X    # figure out a version number to move the file to
  397. X    rev=`(ls -1d $TRMTMP/$base,* 2>/dev/null) |\
  398. X        sort |\
  399. X        tail -1 |\
  400. X        sed -e 's/.*,\([0-9]*\)/\1/'`
  401. X    if [ "$rev" = "" ] ; then
  402. X       rev=1;
  403. X    else
  404. X       rev=`expr $rev + 1`
  405. X    fi
  406. X    if [ "$rev" = "" ]; then
  407. X      rev=01
  408. X    fi
  409. X    rev=`echo $rev | awk '{if(length($1)==1){print 0$1}else{print $1}}'`
  410. X    if [ $rev -gt 99 ]; then
  411. X      echo $prog: ${i} already has 99 versions, staying at 99
  412. X      rev=99
  413. X    fi
  414. X
  415. X    # try to move the file to $TRMTMP
  416. X    /bin/mv -f $i $TRMTMP/$base,$rev > /dev/null 2>&1
  417. X
  418. X    if [ $? != 0 ]; then
  419. X      # the move failed
  420. X
  421. X      # probably tried to remove a file that already exists in $TRMTMP
  422. X      if [ -d $i ]; then
  423. X        # tried to move a directory.. most likely failure is trying to move
  424. X        # a directory across filesystems.. sooo lets tar up the whole thing
  425. X        # and then untar it in the $TRMTMP directory
  426. X    pd=`pwd`
  427. X    cd $dir
  428. X        tar -cBf - $base | ( cd $TRMTMP ; tar -xBf - ) > /dev/null 2>&1
  429. X    mv $TRMTMP/$base $TRMTMP/$base,$rev
  430. X
  431. X        # now remove the directory
  432. X        /bin/rm -rf $base
  433. X    cd $pd
  434. X      fi
  435. X
  436. X    fi
  437. X
  438. X    # at this point the 'remove' was succesfull. Add the filename to the
  439. X    # $TRMLAST file for possible undoing.
  440. X    if [ "$TRMLAST" != "" ]; then
  441. X      echo $i >> $TRMLAST
  442. X    fi
  443. X
  444. X  fi
  445. X
  446. Xdone
  447. X
  448. END_OF_FILE
  449. if test 11934 -ne `wc -c <'trm'`; then
  450.     echo shar: \"'trm'\" unpacked with wrong size!
  451. fi
  452. chmod +x 'trm'
  453. # end of 'trm'
  454. fi
  455. echo shar: End of shell archive.
  456. exit 0
  457. --
  458. Name : Trevor Paquette       | Landmark/ITA             |  _\___ Fahrvergnuegen
  459. Email: tpaquette@ita.lgc.com | Calgary, Alberta, Canada | /     \____
  460. Visitor from CyberSpace      | (403) 269-4669           |/ G60       \
  461. Renegade of Virtual Reality  | #include <disclaimer.h>  |\-O------O--/
  462.  
  463. exit 0 # Just in case...
  464.