#!/bin/sh # # arch - shell script to compress patient cases on disk # # Written by: # Eugene Mah # MSc Graduate Student # Department of Radiology & Diagnostic Imaging # University of Alberta Hospitals # E-mail: eugene@raddi.uah.ualberta.ca # November, 1994 # # This is version 2.3 of arch # # This shell script takes a list of directories or files and compresses # all the raw data, transverse recons, oblique recons and lightbox # files in each directory using the standard UNIX compress utility. # # Wildcards are accepted in the list of directories. To compress a list # of files arch is called with the -f flag followed by the list of files # to be compressed. The list of directories or files *must* follow # the -f options. If the -f flag is not specified, then the script # assumes that what follows is a list of directories containing files # to be compressed. # # Compressed files are created with a fake IIB obtained from the file # being compressed. The words "arch-compressed" are appended to the IIB # and is used to verify that the file contains compressed data before # uncompressing the file. # # When arch is called with the -U flag, it checks to see that this file # exists before trying to uncompress anything. If the file does not exist # a message is printed out and the script proceeds to the next directory. # # Usage: # arch [-CUH] [-f] [list] # -C compress files in directories specified by list # -U uncompress files in directories specified by list # -H display the command usage # -f a list of files rather than directories follows # directory/file list may be specified using wildcards # # Legalese: # This is released into the public domain for the benefit of # Picker PRISM users everywhere. :) # If you're going to modify or enhance it, at least give me some small # mention somewhere. And to Wes Wooten for contributing the key element # which makes this script useful. # # Disclaimer: # Unless somebody turns the computer off/reboots/indiscriminately kills # processes/smashes the computer/tries to use files being compressed/ # unmounts the disk/and other potentially nasty things # while this program is running nothing bad should happen to your data # or your computer. # In the event that something tragic happens to the script as it's running # and data gets trashed, I will disavow any knowledge of the existence # of you or this program. :) # # Revision History: # 1.0 4/20/94 Eugene Mah # - initial working version completed # 1.1 4/21/94 Eugene Mah # - modified to create a file called 'compressed' in the # directory where files have been compressed. # 5/2/94 Eugene Mah # - check for existence of 'compressed' file and skip directory # if it exists. Was going to add this before, but I forgot. # 2.0 5/5/94 Eugene Mah/Wes Wooten # - contributed by W Wooten (wooten@medisun.mep.csufresno.edu) # create a compressed file with a fake IIB header which allows # cibuild and patient transfer utilities to function properly. # - no longer use the presence of the 'compressed' file to test # if files are compressed. The file is kept around as a # visual indication to the user that there are compressed # files in this directory. # - contributed by J Brack (brack@nm.picker.com) # use dd to cut out the fake header and fgrep that rather than # fgrep the entire compressed file. Should be faster. # - got rid of the 'compressed' file # 2.1 10/31/94 Eugene Mah # - added capability to compress/uncompress selected files # within the directory (-f flag) # - cleaned up some obsolete comments in the documentation # # 2.2 06/08/95 Eugene Mah # - when processing files, change the directory containing # the file. If the script crashes, this should make # data recovery easier. # - when compressing files, copy the temporary file # to the original file name and then remove the temp file. # Should reduce the chances of data loss in case something # interrupts the script # - echo "done" to indicate completion # # 2.3 06/22/95 Wes Wooten # - change fgrep to egrep in four locations. (With this change # script works on both TitanOs and OSF/1) # - User may want to allow compression on optical disks only. # Script is set up to block compression on non-existant disks # /img97, /img98, and /img99 # User can easily change /img97 to /img0 if he wants to block # compression on his hard disk /img0. # # Things To Do # Add compression using gzip. Simple enough, but decreases portability # of compressed files to other systems since they need to have gzip # to uncompress files. # Add more flexible command line argument handling. Currently # command line arguments must be specified in a certain order, # [CU] flags first, then [f] flag next. Should be able to do this # with getopts(1). # Make a proper nroff man page. # Make it so directories and files can be handled at the same time. # # # EDIT HERE # (USER MAY WANT TO BLOCK COMPRESSION ON FIXED DISKS) # TO BLOCK COMPRESSION ON /img0 # CHANGE: blockdsk1="/img97" TO blockdsk1="/img0" # (etc.) blockdsk1="/img97" blockdsk2="/img98" blockdsk3="/img99" # # Process the command line # case "$1" in -C) shift comp=1 # compress files if [ $1 = "-f" ] then shift list="$list $@" # file list fd=f else list="$list $@" fd=d fi ;; -U) shift comp=0 # uncompress files if [ $1 = "-f" ] then shift list="$list $@" # file list fd=f else list="$list $@" fd=d fi ;; -H|*) echo "Usage: `basename $0` [-CUH] [-f] list" echo "\twhere list is a list of directories" echo "\t-C\t compress files in list" echo "\t-U\t uncompress files in list" echo "\t-H\t display this screen" echo "\t-f\t list is a list of files rather than directories" echo "\tlist may be specified using wildcards" echo "\tThe -f option should follow either one of the -CUH options" ;; esac # # Process the list # case "$fd" in f) for file in $list # file list do if [ $comp -eq 1 ] # compress files then if [ -f $file ] # make sure the file exists then if [ `echo $file | grep $blockdsk1` ] then echo "`dirname $file` compression blocked" elif [ `echo $file | grep $blockdsk2` ] then echo "`dirname $file` compression blocked" elif [ `echo $file | grep $blockdsk3` ] then echo "`dirname $file` compression blocked" else echo "`basename $0`: Compressing $file" # Change directories cd "`dirname $file`" # Get name of the file fname="`basename $file`" # Check to see that the file isn't already compressed # For some reason, piping the result of dd into egrep # for the if check doesn't work properly. dd if=$fname of=temp ibs=2064 count=1 if egrep -l 'arch-compressed' temp then echo "$fname already compressed. Skipping" else # copy the 2048 byte IIB header from $file dd if=$fname of=header ibs=2048 count=1 # append compression flag to header echo arch-compressed >> header # compess the file and append it to 'header' compress -c $fname >> header # replace $fname with 'header' cp header $fname rm header fi rm temp fi else echo "`basename $0`: $file does not exist" fi else # uncompress files if [ -f $file ] then echo "`basename $0`: Uncompressing $file" # Change directories cd "`dirname $file`" # Get name of the file fname="`basename $file`" dd if=$fname of=temp ibs=2064 count=1 if egrep -l 'arch-compressed' temp then # strip the fake header form $file dd if=$fname of=$fname.Z ibs=2064 skip=1 # uncompress the file rm $fname uncompress -v $fname.Z else echo "$file is not compressed. Skipping" fi rm temp fi fi done ;; d) for dir in $list # directory list do if [ $comp -eq 1 ] # compress files then if [ -d $dir ] # make sure directory exists then if [ `echo $dir | grep $blockdsk1` ] then echo "$dir compression blocked" elif [ `echo $dir | grep $blockdsk2` ] then echo "$dir compression blocked" elif [ `echo $dir | grep $blockdsk3` ] then echo "$dir compression blocked" else cd $dir echo "`basename $0`: Compressing files in $dir" # R - raw data # T - transverse recons # O - oblique recons # L - lightbox files for file in R* T* O* L* do if [ -f $file ] then dd if=$file of=temp ibs=2064 count=1 if egrep -l 'arch-compressed' temp then echo "$file already compressed. Skipping" else # copy the 2048 byte IIb header from $file dd if=$file of=header ibs=2048 count=1 # append compression flag to 'header' echo arch-compressed >> header # compress the file and append it to 'header' compress -c $file >> header # replace $file with 'header' cp header $file rm header fi rm temp fi done fi cd .. else echo "`basename $0`: $dir is not a directory or does not exist" fi else # uncompress files if [ -d $dir ] # make sure directory exists then cd $dir echo "`basename $0`: Uncompressing files in $dir" if [ -f *.Z ] # maintain compatibility with other version then for file in *.Z do uncompress $file done else for file in R* T* O* L* do if [ -f $file ] then dd if=$file of=temp ibs=2064 count=1 if egrep -l 'arch-compressed' temp then # strip the fake header from $file dd if=$file of=$file.Z ibs=2064 skip=1 # uncompress $file rm $file uncompress -v $file.Z else echo "$file not compressed" fi rm temp fi done fi cd .. else echo "`basename $0`: $dir is not a directory or does not exist" fi fi done ;; esac echo "`basename $0`: Done"