123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479 |
- #! /bin/sh
- # -------------------------------------------------------------
- # --- init
- # -------------------------------------------------------------
- # leave on error
- set -e
- # set program name and location:
- call="$0 $*"
- case $0 in
- /* ) script=$0 ;;
- * ) script="`/bin/pwd`/$0" ;;
- esac
- bindir=`/usr/bin/dirname ${script}`
- prog=`/bin/basename ${script}`
- # -------------------------------------------------------------
- # --- help
- # -------------------------------------------------------------
- DisplayUsage ()
- {
- ${PAGER:-less} << EOF
- NAME
- $prog - General Storage System : CINECA cartridge archive (cart)
-
- USAGE
- ${prog} ls [-l,--long] <ecfsfilespec>
-
- -l, --long : long listing including modes, times, sizes etc
- ${prog} get [-f,--force] [-m,--mkdir] <source-ecfsfilespec> <dest-filespec>
- ${prog} put [-f,--force] [-m,--mkdir] <source-ecfsfilespec> <dest-filespec>
-
- -f, --force : overwrite existing destination files if necessary;
- by default, an error is produced if a file on the cartridge
- already exists
- -m, --mkdir : create destination directories if necessary
-
- FILE SPECIFICATION
- A file on the cartridge is specified by a volume and a file name.
- Specify them as if the volume is the (only) directory in a path name:
- myvolume/myfile.txt
-
- GENERAL OPTIONS
- -v, --verbose : display info on progress
- ORIGINAL COMMANDS
- From www.cineca.it > Docs > HPC UserGuide 2010 > 1.5 Production Environment and Tools
- |------------------------------------------------------------------------------|
- |Command |Action |
- |------------------------------------------------------------------------------|
- |cart_new <vol_name> |create a new volume <vol_name> |
- | | |
- |cart_dir |show all the defined volumes from the user |
- |cart_dir <vol_name> |show the files stored on the volume <vol_name> |
- | | |
- |cart_put <vol_name> <file> |save <file> in the volume <vol_name> |
- | | |
- |cart_get <vol_name> <file> |retrieve the <file> from the volume <vol_name> |
- | | |
- |cart_del <vol_name> <file> |delete the <file> from the volume <vol_name> |
- |cart_del <vol_name> |delete the volume <vol_name> (the volume is empty)|
- |------------------------------------------------------------------------------|
- EOF
- exit 0
- }
- # err 'help text'
- err ()
- {
- echo "$1" 1>&2
- }
- # errit <exit-status>
- errit ()
- {
- err "$prog - ERROR - from : ${call}"
- err "$prog - ERROR - Use '$prog --help' for more info."
- exit $1
- }
- # -------------------------------------------------------------
- # --- arguments
- # -------------------------------------------------------------
- options=''
- task=''
- filespecs=''
- verbose=''
- # extract arguments
- for arg in "$@" ; do
- case ${arg} in
- -h | --help ) DisplayUsage ;;
- -v | --verbose ) verbose='T' ;;
- -* ) options="${options} ${arg}" ;;
- * ) # task or (one of the) file(s)
- if [ -z "${task}" ]; then
- task=${arg}
- else
- filespec=${arg}
- filespecs="${filespecs} ${filespec}"
- fi
- ;;
- esac
- done
- # not complete ?
- if [ -z "${task}" ]; then
- err "$prog - no arguments specified"
- errit 1
- fi
- # -------------------------------------------------------------
- # --- begin
- # -------------------------------------------------------------
- test ${verbose} && echo "$prog - $0 $*"
- # test task:
- case ${task} in
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # list file or directory
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 'list' )
- # extract options
- list_long='no'
- for option in ${options} ; do
- case ${option} in
- -l | --long ) list_long='yes' ;;
- -* )
- err "$prog - unknown option '${option}' for task '${task}'"
- errit 1
- ;;
- esac
- done
-
- # extract source and destination filespec:
- filespec=''
- for spec in ${filespecs} ; do
- if [ -z "${filespec}" ]; then
- filespec=${spec}
- else
- err "$prog - ERROR - task '${task}' requires single file specification only."
- errit 1
- fi
- done
- if [ -z "${filespec}" ]; then
- err "$prog - ERROR - tasks '${task}' requires file specification."
- errit 1
- fi
-
- # split at '/' , count words:
- nword=`echo ${filespec} | tr '/' ' ' | wc -w`
- if [ ${nword} -eq 0 ]; then
- filevolume=''
- filename=''
- elif [ ${nword} -eq 1 ]; then
- filevolume=${filespec}
- filename=''
- elif [ ${nword} -eq 2 ]; then
- filevolume=`echo ${filespec} | cut -d '/' -f 1`
- filename=`echo ${filespec} | cut -d '/' -f 2`
- else
- err "$prog - ERROR - file specification should be [<volume>/]<name> ."
- errit 1
- fi
-
- # list files:
- if [ "${list_long}" = "yes" ]; then
- # single file only ?
- if [ -n "${filename}" ]; then
- # list volume, skip total number of files, extract line with requested file:
- cart_dir ${filevolume} | grep -v 'total:' | grep " ${filename} "
- else
- # list volume, skip total number of files:
- cart_dir ${filevolume} | grep -v 'total:'
- fi
- else
- # single file only ?
- if [ -n "${filename}" ]; then
- # list volume, skip total number of files, only the file name, extract line with requested file:
- cart_dir ${filevolume} | grep -v 'total:' | cut -d ' ' -f 2 | grep "^${filename}$"
- else
- # list volume, skip total number of files, only the file name:
- cart_dir ${filevolume} | grep -v 'total:' | cut -d ' ' -f 2
- fi
- fi
-
- ;;
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # copy files
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 'get' )
- # extract options
- #do_force=''
- do_mkdir=''
- for option in ${options} ; do
- case ${option} in
- -f | --force ) ;; # do_force='true' ;; # ignore, cart_get always overwrites
- -m | --mkdir ) do_mkdir='true' ;;
- -* )
- err "$prog - unknown option '${option}' for task '${task}'"
- errit 1
- ;;
- esac
- done
- # extract source and destination filespec:
- source_spec=''
- dest_spec=''
- for spec in ${filespecs} ; do
- if [ -z "${source_spec}" ]; then
- source_spec=${spec}
- elif [ -z "${dest_spec}" ]; then
- dest_spec=${spec}
- else
- err "$prog - ERROR - task '${task}' requires source and destination file specifications only."
- errit 1
- fi
- done
- if [ -z "${dest_spec}" ]; then
- err "$prog - ERROR - task '${task}' requires source and destination file specifications."
- errit 1
- fi
-
- # destination directory:
- dest_dir=`/usr/bin/dirname ${dest_spec}`
- dest_file=`basename ${dest_spec}`
-
- # create destination directory ?
- # o destination is an existing file : not necessary
- # o destination is an existing directory : not necessary
- if [ ${do_mkdir} ] && [ ! -f ${dest_spec} ] && [ ! -d ${dest_spec} ]; then
- # destination is not an existing file or directory;
- # create the directory part of the destination:
- test "${dest_dir}" != "." && /bin/mkdir -p ${dest_dir}
- fi
-
- ## remove existing target ?
- #if [ ${do_force} ] && [ -f ${dest_spec} ]; then
- # # info ...
- # test "${verbose}" && echo "$prog - remove existing ${dest_spec} ..."
- # # remove target:
- # /bin/rm -f ${dest_spec}
- #fi
-
- # split at '/' , count words:
- nword=`echo ${source_spec} | tr '/' ' ' | wc -w`
- if [ ${nword} -eq 2 ]; then
- source_volume=`echo ${source_spec} | cut -d '/' -f 1`
- source_file=`echo ${source_spec} | cut -d '/' -f 2`
- else
- err "$prog - ERROR - file specification should be <volume>/<name> ."
- errit 1
- fi
-
- # save current working dir:
- cwd=`pwd`
- # goto destination directory:
- test "${verbose}" && echo "$prog - goto ${dest_dir} ..."
- cd ${dest_dir}
- # full command:
- cart_cmnd="cart_get ${source_volume} ${source_file}"
-
- # info ..
- test "${verbose}" && echo "$prog - execute : ${cart_cmnd}"
-
- # copy file; suppress error messages:
- outfile=".gss.out.$$"
- if ! ( ${cart_cmnd} > ${outfile} 2>&1 ) ; then
- /bin/cat ${outfile}
- /bin/rm -f ${outfile}
- err "$prog - ERROR from cart_get"
- errit 1
- fi
- /bin/rm -f ${outfile}
-
- # rename ?
- if [ "${source_file}" != "${dest_file}" ]; then
- # info ..
- test "${verbose}" && echo "$prog - rename to : ${dest_file}"
- # rename:
- /bin/mv ${source_file} ${dest_file}
- fi
- # back:
- cd ${cwd}
-
- ;;
- 'put' )
- # extract options
- do_force=''
- do_mkdir=''
- for option in ${options} ; do
- case ${option} in
- -f | --force ) do_force="true" ;;
- -m | --mkdir ) do_mkdir='true' ;;
- -* )
- err "$prog - unknown option '${option}' for task '${task}'"
- errit 1
- ;;
- esac
- done
- # extract source and destination filespec:
- source_spec=''
- dest_spec=''
- for spec in ${filespecs} ; do
- if [ -z "${source_spec}" ]; then
- source_spec=${spec}
- elif [ -z "${dest_spec}" ]; then
- dest_spec=${spec}
- else
- err "$prog - ERROR - task '${task}' requires source and destination file specifications only."
- errit 1
- fi
- done
- if [ -z "${dest_spec}" ]; then
- err "$prog - ERROR - task '${task}' requires source and destination file specifications."
- errit 1
- fi
-
- # split at '/' , count words:
- nword=`echo ${dest_spec} | tr '/' ' ' | wc -w`
- if [ ${nword} -eq 2 ]; then
- dest_volume=`echo ${dest_spec} | cut -d '/' -f 1`
- dest_file=`echo ${dest_spec} | cut -d '/' -f 2`
- else
- err "$prog - ERROR - cart file specification should be <volume>/<name> ."
- errit 1
- fi
-
- # create destination directory ?
- if [ ${do_mkdir} ]; then
- # full command:
- cart_cmnd="cart_new ${dest_volume}"
- # info ...
- test "${verbose}" && echo "$prog - execute : ${cart_cmnd}"
- # execute:
- outfile=".gss.out.$$"
- if ! ( ${cart_cmnd} > ${outfile} 2>&1 ) ; then
- /bin/cat ${outfile}
- /bin/rm -f ${outfile}
- err "$prog - ERROR from: ${cart_cmnd}"
- errit 1
- fi
- /bin/rm -f ${outfile}
- fi
-
- # rename ?
- source_file=`basename ${source_spec}`
- if [ "${dest_file}" != "${source_file}" ]; then
- # check ...
- if [ -f ${dest_file} ]; then
- echo "$prog - ERROR - destination has another name than source, but could not create a temporary symbolic link"
- echo "$prog - ERROR - since file in current directory has the same name already"
- errit 1
- fi
- # info ...
- test "${verbose}" && echo "$prog - create ${dest_file} -> ${source_spec}"
- # create a symbolic link
- ln -s ${source_spec} ${dest_file}
- # new name:
- source_spec="${dest_file}"
- # flag to remove the link afterwards:
- tmplink="${dest_file}"
- else
- # dummy ..
- tmplink=''
- fi
-
- # full command:
- cart_cmnd="cart_put ${dest_volume} ${source_spec}"
- # info ...
- test "${verbose}" && echo "$prog - execute : ${cart_cmnd}"
- # copy file; suppress error messages:
- outfile=".gss.out.$$"
- if ! ( ${cart_cmnd} > ${outfile} 2>&1 ) ; then
- # trap error on existing file ...
- if [ "`head -n 1 ${outfile}`" = "This filename already exist in this volume!" ]; then
- test "${verbose}" && echo "$prog - destination file already exists ..."
- if [ -n "${do_force}" ]; then
- test "${verbose}" && echo "$prog - remove existing destination file ..."
- cart_cmnd_del="cart_del ${dest_volume} ${source_spec}"
- if ! ( ${cart_cmnd_del} > ${outfile} 2>&1 ) ; then
- /bin/cat ${outfile}
- /bin/rm -f ${outfile}
- test -n "${tmplink}" && /bin/rm -f ${tmplink}
- err "$prog - ERROR from : ${cart_cmnd_del}"
- err "$prog - ERROR (delete existing file)"
- errit 1
- fi
- test "${verbose}" && echo "$prog - copy again ..."
- if ! ( ${cart_cmnd} > ${outfile} 2>&1 ) ; then
- /bin/cat ${outfile}
- /bin/rm -f ${outfile}
- test -n "${tmplink}" && /bin/rm -f ${tmplink}
- err "$prog - ERROR from : ${cart_cmnd}"
- err "$prog - ERROR (after deleting existing file)"
- errit 1
- fi
- else
- err "$prog - ERROR - target file exist : ${dest_spec}"
- err "$prog - ERROR - specifiy --force to have existing files removed"
- /bin/rm -f ${outfile}
- test -n "${tmplink}" && /bin/rm -f ${tmplink}
- errit 1
- fi
- else
- /bin/cat ${outfile}
- /bin/rm -f ${outfile}
- test -n "${tmplink}" && /bin/rm -f ${tmplink}
- err "$prog - ERROR from : ${cart_cmnd}"
- errit 1
- fi
- fi
- /bin/rm -f ${outfile}
-
- # cleanup ?
- if [ -n "${tmplink}" ]; then
- # info ...
- test "${verbose}" && echo "$prog - remove temporary link ${tmplink} ..."
- # remove:
- /bin/rm -f ${tmplink}
- fi
-
- ;;
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- # task ???
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- * )
- err "$prog - ERROR - unsupported task '${task}'"
- errit 1
- ;;
-
- esac # tasks
- # -------------------------------------------------------------
- # --- end
- # -------------------------------------------------------------
- test ${verbose} && echo "$prog - end"
- # ok
- exit 0
|