#! /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] -l, --long : long listing including modes, times, sizes etc ${prog} get [-f,--force] [-m,--mkdir] ${prog} put [-f,--force] [-m,--mkdir] -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 |create a new volume | | | | |cart_dir |show all the defined volumes from the user | |cart_dir |show the files stored on the volume | | | | |cart_put |save in the volume | | | | |cart_get |retrieve the from the volume | | | | |cart_del |delete the from the volume | |cart_del |delete the volume (the volume is empty)| |------------------------------------------------------------------------------| EOF exit 0 } # err 'help text' err () { echo "$1" 1>&2 } # errit 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 [/] ." 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 / ." 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 / ." 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