#!/bin/bash # CVS INFO # $Header: /cis/home/cvsroot/lddmm/bin/url_io,v 1.9 2004/04/05 16:13:13 anthony Exp $ # $Log: url_io,v $ # Revision 1.9 2004/04/05 16:13:13 anthony # aek - lddmm script will use SRB_HOME to define a nondefault SRB binary path. # # Revision 1.8 2004/03/29 18:59:31 rob # RYII: Uber script now adds metadata when uploading to SRB. url_io calls 'Sget' # through wrapper method # # Revision 1.7 2004/03/26 19:19:11 anthony # aek - added the '-m' to the bulk upload. # # Revision 1.6 2004/03/26 19:06:19 anthony # aek - Now using 'Sput -b' for SRB upload # # # url_io - RYII 11.3.03 # Bash module to process BIRN URL's # Primary functions: 'url_input' or 'url_output' # # SRB Note: # Uses Sget to download, Sput to upload single files, and bulkUpload for directories # Global defines URL_DIR=1 URL_FILE=2 URL_SRB_DIR=3 URL_SRB_FILE=4 URL_ERROR=99 # Global Variables used for passing information # URL_PATH contains the absolute path of the last parsed URL URL_PATH= # If variable URL_VERBOSE is defined (by user), output will be verbose for debugging url_verbose_echo () { if [ -n "$URL_VERBOSE" ] then echo "$1" fi } # Executes S-Command passed as argument # Calls Sinit/Sexit as necessary, and selects proper (GSI/not) command # USE: url_scommand $scommand [$args...] url_scommand () { if [ -z "$X509_USER_PROXY" ] then Sinit $* Sexit else $* fi } # Usage: url_get_automount_host # Prints hostname of CIS file server where directory # is physically located url_get_automount_host () { if [[ -d "/cis" && -n "`which ypmatch`" ]] then local DIR=$1 local NIS_DIR=`echo ${DIR} | cut -d/ -f3` local NIS_SUBDIR=`echo ${DIR} | cut -d/ -f4` ypmatch ${NIS_SUBDIR} auto.${NIS_DIR} | cut -d: -f1 fi } url_print_type () { case $1 in 1 ) echo "TYPE ($1) - URL_DIR" ;; 2 ) echo "TYPE ($1) - URL_FILE" ;; 3 ) echo "TYPE ($1) - URL_SRB_DIR" ;; 4 ) echo "TYPE ($1) - URL_SRB_FILE" ;; 99) echo "TYPE ($1) - URL_ERROR" ;; * ) echo "TYPE ($1) - UNDEFINED" ;; esac } # Determine what type of address we are dealing with # # USE: url_get_type $URL # RETURNS: Enumerated value specifying URL type url_get_type () { local TYPE=`echo ${1} | cut -s -d: -f1` case ${TYPE} in "dir" ) return $URL_DIR ;; "file" ) return $URL_FILE ;; "srbdir" ) return $URL_SRB_DIR ;; "srbfile" ) return $URL_SRB_FILE ;; "" ) if [ -f $1 ] then return $URL_FILE fi if [ -d $1 ] then return $URL_DIR fi return $URL_ERROR ;; * ) return $URL_ERROR ;; esac } # Convert relative path name to absolute path in local file system or SRB # # NOTE: Operates relative to current directory, so DO NOT change # directories between inputting path and calling this function # # USE: url_convert_to_absolute $url # Places absolute path value in global $URL_PATH url_convert_to_absolute () { local START_DIR=`pwd` if [ "${URL_PATH}" = "${URL_PATH#/}" ] then case $1 in $URL_SRB_DIR | $URL_SRB_FILE ) local SRB_HOME=`url_scommand Spwd` URL_PATH=${SRB_HOME}/${URL_PATH} ;; * ) URL_PATH=${START_DIR}/${URL_PATH} ;; esac fi } # USE: url_parse $url # RETURNS: enumerated type of URL # SIDE EFFECT: Places absolute path from URL in global $URL_PATH url_parse () { url_verbose_echo "Parsing $1" url_get_type $1 local TYPE=$? url_verbose_echo "PARSE - TYPE : $TYPE" if [ $TYPE = $URL_ERROR ] then return $URL_ERROR fi URL_PATH=`echo ${1} | cut -d: -f2` URL_PATH=${URL_PATH%/} url_convert_to_absolute $TYPE url_verbose_echo "URL - $1" if [ -n "$URL_VERBOSE" ] then url_print_type $TYPE fi url_verbose_echo "URL_PATH - $URL_PATH" return $TYPE } # USE: url_input $birn_url $local_path # Copies data from location specified in $birn_url to $local_path url_input () { local IN=$1; local OUT=$2; url_verbose_echo "Input : $1 $2" url_parse $IN local IN_TYPE=$? case $IN_TYPE in $URL_DIR ) if [[ ! -d ${URL_PATH} ]] then echo "Error: Directory ${URL_PATH} does not exist" return $URL_ERROR fi cp -r $URL_PATH $OUT ;; $URL_FILE ) if [[ ! -f ${URL_PATH} ]] then echo "Error: File ${URL_PATH} does not exist or is irregular" return $URL_ERROR fi cp $URL_PATH $OUT ;; $URL_SRB_DIR ) url_scommand Sget -r -m $URL_PATH $OUT ;; $URL_SRB_FILE ) url_scommand Sget -m $URL_PATH $OUT ;; $URL_ERROR ) echo "Error: $IN is not a valid BIRN URL" return $URL_ERROR ;; esac return 0 } # USE: url_output $local_path $birn_url # Copy data in $local path to location specified by $birn_url url_output () { local IN=$1; local OUT=$2; IN=${IN%/} url_verbose_echo "Output : $1 $2" # Parse in address url_parse $IN local IN_TYPE=$? local IN_PATH=$URL_PATH url_verbose_echo "Parsed IN address" # Parse out address url_parse $OUT local OUT_TYPE=$? local HOST="" # local START_DIR=`pwd` if [[ "$OUT_TYPE"="$URL_FILE" || "$OUT_TYPE"="$URL_DIR" ]] then # cd $PREFIX ; cd $START_DIR url_verbose_echo "url_get_automount_host $URL_PATH" HOST=`url_get_automount_host $URL_PATH` fi case $OUT_TYPE in $URL_FILE ) if [ -z "$HOST" ] then cp $IN $URL_PATH else scp $IN $HOST:$URL_PATH > /dev/null fi ;; $URL_DIR ) if [ -z "$INTERNAL_COPY" ] then if [ -z "$HOST" ] then cp -r $IN $URL_PATH else scp -r $IN $HOST:$URL_PATH > /dev/null fi else if [ -z "$HOST" ] then cp -r "$IN/*" $URL_PATH else scp -r "$IN/*" $HOST:$URL_PATH fi fi ;; $URL_SRB_FILE ) url_scommand Sput $IN_PATH $URL_PATH ;; $URL_SRB_DIR ) #AEK if [ "$IN_TYPE" = "$URL_FILE" ] #AEK then url_scommand Sput -b -m $IN_PATH $URL_PATH #AEK else #AEK url_scommand Smkdir $URL_PATH/${IN##*/} #AEK url_scommand Sput -b -m $IN $URL_PATH/${IN##*/} #AEK fi ;; esac }