gss_ecfs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. #! /bin/sh
  2. # -------------------------------------------------------------
  3. # --- init
  4. # -------------------------------------------------------------
  5. # leave on error
  6. set -e
  7. # set program name and location:
  8. call="$0 $*"
  9. case $0 in
  10. /* ) script=$0 ;;
  11. * ) script="`/bin/pwd`/$0" ;;
  12. esac
  13. bindir=`/usr/bin/dirname ${script}`
  14. prog=`/bin/basename ${script}`
  15. # -------------------------------------------------------------
  16. # --- help
  17. # -------------------------------------------------------------
  18. DisplayUsage ()
  19. {
  20. ${PAGER:-less} << EOF
  21. NAME
  22. $prog - General Storage System : ECMWF tape archive (ECFS)
  23. USAGE
  24. ${prog} ls [-l,--long] <ecfsfilespec>
  25. -l, --long : long listing including modes, times, sizes etc
  26. ${prog} get [-f,--force] [-m,--mkdir] <source-ecfsfilespec> <dest-filespec>
  27. ${prog} put [-f,--force] [-m,--mkdir] <source-ecfsfilespec> <dest-filespec>
  28. -f, --force : overwrite existing destination files if necessary;
  29. by default, an error is produced if a file on ecfs
  30. already exists (ecp -e)
  31. -m, --mkdir : create destination directories if necessary
  32. FILE SPECIFICATION
  33. A file on ecfs is specified by a standard unix file name:
  34. /ab1/TM/meteo # permanent storage 'ec:'
  35. /TMP/ab1/TM/meteo # temporary storage 'ectmp:'
  36. Special options are preceded, seperate with percent signs:
  37. umask=022%/ab1/TM/meteo
  38. FILE ACCESS PERMISSIONS
  39. Default umask: 077 = --- rwx rwx
  40. Thus new direcory : rwx rwx rwx XOR --- rwx rwx = rwx --- ---
  41. and new file : rw- rw- rw- XOR --- rwx rwx = rw- --- ---
  42. Change mask to exclude only write permissions for 'group' and 'other':
  43. umask=022 # --- -w- -w-
  44. GENERAL OPTIONS
  45. -v, --verbose : display info on progress
  46. EOF
  47. exit 0
  48. }
  49. # err 'help text'
  50. err ()
  51. {
  52. echo "$1" 1>&2
  53. }
  54. # errit <exit-status>
  55. errit ()
  56. {
  57. err "$prog - ERROR - from : ${call}"
  58. err "$prog - ERROR - Use '$prog --help' for more info."
  59. exit $1
  60. }
  61. # -------------------------------------------------------------
  62. # --- arguments
  63. # -------------------------------------------------------------
  64. options=''
  65. task=''
  66. filespecs=''
  67. verbose=''
  68. # extract arguments
  69. for arg in "$@" ; do
  70. case ${arg} in
  71. -h | --help ) DisplayUsage ;;
  72. -v | --verbose ) verbose='T' ;;
  73. -* ) options="${options} ${arg}" ;;
  74. * ) # task or (one of the) file(s)
  75. if [ -z "${task}" ]; then
  76. task=${arg}
  77. else
  78. filespec=${arg}
  79. filespecs="${filespecs} ${filespec}"
  80. fi
  81. ;;
  82. esac
  83. done
  84. # not complete ?
  85. if [ -z "${task}" ]; then
  86. err "$prog - no arguments specified"
  87. errit 1
  88. fi
  89. # -------------------------------------------------------------
  90. # --- functions
  91. # -------------------------------------------------------------
  92. #
  93. # EvalSpecials umask=022%group=ksarch%/fa/ks/TM
  94. # filespec=${FS}
  95. #
  96. # Evaluates all preceding parts ending with '%'.
  97. # Return last part in variable 'FS' .
  98. #
  99. EvalSpecials ()
  100. {
  101. # seperation mark:
  102. sep='%'
  103. # loop until all % parts are removed:
  104. FS=$1
  105. while [[ ${FS} = *%* ]]; do
  106. eval "`echo ${FS} | /usr/bin/cut -d ${sep} -f 1`"
  107. FS=`echo ${FS} | /usr/bin/cut -d ${sep} -f 2-`
  108. done
  109. }
  110. # -------------------------------------------------------------
  111. # --- begin
  112. # -------------------------------------------------------------
  113. test ${verbose} && echo "$prog - $0 $*"
  114. # setup script present ?
  115. case `/bin/hostname` in
  116. ecga* )
  117. . '/usr/local/share/ecmwf/share/.kshrc'
  118. ;;
  119. ecgb* )
  120. . "${ECFS_SYS_PATH}/.ecfs_bash_env"
  121. . "${ECFS_SYS_PATH}/.ecfs_bash_functions"
  122. . "${ECFS_SYS_PATH}/.ecfs_bash_aliases"
  123. ;;
  124. c[12]* )
  125. ;;
  126. cc* )
  127. PATH=$PATH:/usr/local/apps/ecfs/2.0.13rc2/bin
  128. ;;
  129. * )
  130. err "$prog - ERROR - ecfs not supported on this host ..."
  131. errit 1 ;;
  132. esac
  133. # test task:
  134. case ${task} in
  135. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  136. # list file or directory
  137. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  138. 'list' )
  139. # extract options
  140. els_options=''
  141. for option in ${options} ; do
  142. case ${option} in
  143. -l | --long ) els_options="${els_options} -l" ;;
  144. -* )
  145. err "$prog - unknown option '${option}' for task '${task}'"
  146. errit 1
  147. ;;
  148. esac
  149. done
  150. # extract source and destination filespec:
  151. filespec=''
  152. for spec in ${filespecs} ; do
  153. EvalSpecials ${spec}
  154. if [ -z "${filespec}" ]; then
  155. filespec=${FS}
  156. else
  157. err "$prog - ERROR - task '${task}' requires single file specification only."
  158. errit 1
  159. fi
  160. done
  161. if [ -z "${filespec}" ]; then
  162. err "$prog - ERROR - tasks '${task}' requires file specification."
  163. errit 1
  164. fi
  165. # list files:
  166. els ${els_options} ${filespec}
  167. ;;
  168. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  169. # copy files
  170. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  171. 'get' )
  172. # extract options
  173. ecp_options=''
  174. do_mkdir=''
  175. for option in ${options} ; do
  176. case ${option} in
  177. -f | --force ) ecp_options="${ecp_options} -o" ;;
  178. -m | --mkdir ) do_mkdir='true' ;;
  179. -* )
  180. err "$prog - unknown option '${option}' for task '${task}'"
  181. errit 1
  182. ;;
  183. esac
  184. done
  185. # extract source and destination filespec:
  186. source_spec=''
  187. dest_spec=''
  188. for spec in ${filespecs} ; do
  189. EvalSpecials ${spec}
  190. if [ -z "${source_spec}" ]; then
  191. source_spec=${FS}
  192. elif [ -z "${dest_spec}" ]; then
  193. dest_spec=${FS}
  194. else
  195. err "$prog - ERROR - task '${task}' requires source and destination file specifications only."
  196. errit 1
  197. fi
  198. done
  199. if [ -z "${dest_spec}" ]; then
  200. err "$prog - ERROR - task '${task}' requires source and destination file specifications."
  201. errit 1
  202. fi
  203. # create destination directory ?
  204. # o destination is an existing file : not necessary
  205. # o destination is an existing directory : not necessary
  206. if [ ${do_mkdir} ] && [ ! -f ${dest_spec} ] && [ ! -d ${dest_spec} ]; then
  207. # destination is not an existing file or directory;
  208. # create the directory part of the destination:
  209. dest_dir=`/usr/bin/dirname ${dest_spec}`
  210. test "${dest_dir}" != "." && /bin/mkdir -p ${dest_dir}
  211. fi
  212. # copy file; suppress error messages:
  213. outfile=".gss.out.$$"
  214. if ! ( ecp ${ecp_options} ec:${source_spec} ${dest_spec} > ${outfile} 2>&1 ) ; then
  215. /bin/cat ${outfile}
  216. /bin/rm -f ${outfile}
  217. err "$prog - ERROR from ecp"
  218. errit 1
  219. fi
  220. /bin/rm -f ${outfile}
  221. # get over the non working umask stuff -> default to 640
  222. chmod 640 ${dest_spec}
  223. ;;
  224. 'put' )
  225. # defaults
  226. umask=022 # no write permissions for group and other
  227. # extract options
  228. ecp_options='-e' # error if target already exists
  229. do_mkdir=''
  230. for option in ${options} ; do
  231. case ${option} in
  232. -f | --force ) ecp_options="-o" ;;
  233. -m | --mkdir ) do_mkdir='true' ;;
  234. -* )
  235. err "$prog - unknown option '${option}' for task '${task}'"
  236. errit 1
  237. ;;
  238. esac
  239. done
  240. # extract source and destination filespec:
  241. source_spec=''
  242. dest_spec=''
  243. for spec in ${filespecs} ; do
  244. EvalSpecials ${spec}
  245. if [ -z "${source_spec}" ]; then
  246. source_spec=${FS}
  247. elif [ -z "${dest_spec}" ]; then
  248. dest_spec=${FS}
  249. else
  250. err "$prog - ERROR - task '${task}' requires source and destination file specifications only."
  251. errit 1
  252. fi
  253. done
  254. if [ -z "${dest_spec}" ]; then
  255. err "$prog - ERROR - task '${task}' requires source and destination file specifications."
  256. errit 1
  257. fi
  258. # set ECFS umask - do not exit if error (cca: not available, once in the queue)
  259. set +e
  260. eumask ${umask}
  261. set -e
  262. # create destination directory ?
  263. if [ ${do_mkdir} ]; then
  264. # o destination is an existing file : not necessary
  265. # o destination is an existing directory : not necessary
  266. #if ! ${script} exist ${dest_spec} ; then
  267. # do not test if directory already exists,
  268. # this takes more time than just creation;
  269. # supress error messages about already existing directories ...
  270. dest_dir=`/usr/bin/dirname ${dest_spec}`
  271. outfile=".gss.out.$$"
  272. if ! ( emkdir -p ec:${dest_dir} > ${outfile} 2>&1 ) ; then
  273. /bin/cat ${outfile}
  274. /bin/rm -f ${outfile}
  275. err "$prog - ERROR from emkdir"
  276. errit 1
  277. fi
  278. /bin/rm -f ${outfile}
  279. #fi
  280. fi
  281. # copy file; suppress error messages:
  282. outfile=".gss.out.$$"
  283. if ! ( ecp ${ecp_options} ${source_spec} ec:${dest_spec} > ${outfile} 2>&1 ) ; then
  284. /bin/cat ${outfile}
  285. /bin/rm -f ${outfile}
  286. err "$prog - ERROR from ecp"
  287. errit 1
  288. fi
  289. /bin/rm -f ${outfile}
  290. ;;
  291. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  292. # task ???
  293. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  294. * )
  295. err "$prog - ERROR - unsupported task '${task}'"
  296. errit 1
  297. ;;
  298. esac # tasks
  299. # -------------------------------------------------------------
  300. # --- end
  301. # -------------------------------------------------------------
  302. test ${verbose} && echo "$prog - end"
  303. # ok
  304. exit 0