gss_mos 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #! /bin/sh
  2. # -------------------------------------------------------------
  3. # --- init
  4. # -------------------------------------------------------------
  5. # leave on error
  6. set -e
  7. # set program name and filespec:
  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 : Massa Opslag Systeem (MOS)
  23. USAGE
  24. ${prog} ls [-l,--long] <mosfilespec>
  25. -l, --long : long listing including modes, times, sizes etc
  26. ${prog} get [-f,--force] [-m,--mkdir] <source-mosfilespec> <dest-filespec>
  27. ${prog} put [-f,--force] [-m,--mkdir] <source-mosfilespec> <dest-filespec>
  28. -f, --force : overwrite existing destination files if necessary
  29. -m, --mkdir : create destination directories if necessary
  30. FILE SPECIFICATION
  31. A file on MOS is specified by a standard unix file name:
  32. /fa/ks/TM/meteo
  33. Special options are preceded, seperate with percent signs:
  34. umask=022%/fa/ks/TM/meteo
  35. FILE ACCESS PERMISSIONS
  36. Default umask: 077 = --- rwx rwx
  37. Thus new direcory : rwx rwx rwx XOR --- rwx rwx = rwx --- ---
  38. and new file : rw- rw- rw- XOR --- rwx rwx = rw- --- ---
  39. Change mask to exclude only write permissions for 'group' and 'other':
  40. umask=022 # --- -w- -w-
  41. GROUP NAMES
  42. The following group names are automatically set:
  43. files group
  44. ----------- --------------
  45. /fa/ks/* ksarch
  46. GENERAL OPTIONS
  47. -v, --verbose : display info on progress
  48. EOF
  49. exit 0
  50. }
  51. # err 'help text'
  52. err ()
  53. {
  54. echo "$1" 1>&2
  55. }
  56. # errit <exit-status>
  57. errit ()
  58. {
  59. err "$prog - ERROR - from : ${call}"
  60. err "$prog - ERROR - Use '$prog --help' for more info."
  61. exit $1
  62. }
  63. # -------------------------------------------------------------
  64. # --- arguments
  65. # -------------------------------------------------------------
  66. options=''
  67. task=''
  68. filespecs=''
  69. verbose=''
  70. # extract arguments
  71. for arg in "$@" ; do
  72. case ${arg} in
  73. -h | --help ) DisplayUsage ;;
  74. -v | --verbose ) verbose='T' ;;
  75. -* ) options="${options} ${arg}" ;;
  76. * ) # task or (one of the) file(s)
  77. if [ -z "${task}" ]; then
  78. task=${arg}
  79. else
  80. filespec=${arg}
  81. filespecs="${filespecs} ${filespec}"
  82. fi
  83. ;;
  84. esac
  85. done
  86. # not complete ?
  87. if [ -z "${task}" ]; then
  88. err "$prog - no arguments specified"
  89. errit 1
  90. fi
  91. # -------------------------------------------------------------
  92. # --- functions
  93. # -------------------------------------------------------------
  94. #
  95. # EvalSpecials umask=022%group=ksarch%/fa/ks/TM
  96. # filespec=${FS}
  97. #
  98. # Evaluates all preceding parts ending with '%'.
  99. # Return last part in variable 'FS' .
  100. #
  101. EvalSpecials ()
  102. {
  103. # seperation mark:
  104. sep='%'
  105. # loop until all % parts are removed:
  106. FS=$1
  107. while [[ ${FS} = *%* ]]; do
  108. eval "`echo ${FS} | /usr/bin/cut -d ${sep} -f 1`"
  109. FS=`echo ${FS} | /usr/bin/cut -d ${sep} -f 2-`
  110. done
  111. }
  112. # -------------------------------------------------------------
  113. # --- begin
  114. # -------------------------------------------------------------
  115. test ${verbose} && echo "$prog - $prog $*"
  116. # setup script present ?
  117. setupsc='/usr/local/mos/lib/MOSsetup.sh'
  118. if [ ! -f ${setupsc} ]; then
  119. err "$prog - ERROR - MOS setup file not found; no MOS access ?"
  120. err "$prog - ERROR - ${setupsc}"
  121. errit 1
  122. fi
  123. # setup MOS access:
  124. . ${setupsc}
  125. # test task:
  126. case ${task} in
  127. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  128. # list file or directory
  129. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  130. 'list' )
  131. # extract options
  132. mls_options=''
  133. for option in ${options} ; do
  134. case ${option} in
  135. -l | --long ) mls_options="${mls_options} -l" ;;
  136. -* )
  137. err "$prog - unknown option '${option}' for task '${task}'"
  138. errit 1
  139. ;;
  140. esac
  141. done
  142. # extract source and destination filespec:
  143. filespec=''
  144. for spec in ${filespecs} ; do
  145. EvalSpecials ${spec}
  146. if [ -z "${filespec}" ]; then
  147. filespec=${FS}
  148. else
  149. err "$prog - ERROR - task '${task}' requires single file specification only."
  150. errit 1
  151. fi
  152. done
  153. if [ -z "${filespec}" ]; then
  154. err "$prog - ERROR - tasks '${task}' requires file specification."
  155. errit 1
  156. fi
  157. # list files:
  158. mls ${mls_options} ${filespec}
  159. ;;
  160. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  161. # copy files
  162. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  163. 'get' )
  164. # extract options
  165. mget_options=''
  166. do_mkdir=''
  167. for option in ${options} ; do
  168. case ${option} in
  169. -f | --force ) mget_options="${mget_options} -f" ;;
  170. -m | --mkdir ) do_mkdir='true' ;;
  171. -* )
  172. err "$prog - unknown option '${option}' for task '${task}'"
  173. errit 1
  174. ;;
  175. esac
  176. done
  177. # extract source and destination filespec:
  178. source_spec=''
  179. dest_spec=''
  180. for spec in ${filespecs} ; do
  181. EvalSpecials ${spec}
  182. if [ -z "${source_spec}" ]; then
  183. source_spec=${FS}
  184. elif [ -z "${dest_spec}" ]; then
  185. dest_spec=${FS}
  186. else
  187. err "$prog - ERROR - task '${task}' requires source and destination file specifications only."
  188. errit 1
  189. fi
  190. done
  191. if [ -z "${dest_spec}" ]; then
  192. err "$prog - ERROR - task '${task}' requires source and destination file specifications."
  193. errit 1
  194. fi
  195. # create destination directory ?
  196. # o destination is an existing file : not necessary
  197. # o destination is an existing directory : not necessary
  198. if [ ${do_mkdir} ] && [ ! -f ${dest_spec} ] && [ ! -d ${dest_spec} ]; then
  199. # destination is not an existing file or directory;
  200. # create the directory part of the destination:
  201. dest_dir=`/usr/bin/dirname ${dest_spec}`
  202. test "${dest_dir}" != "." && /bin/mkdir -p ${dest_dir}
  203. fi
  204. # copy file; suppress error messages:
  205. mget ${mget_options} ${source_spec} ${dest_spec} > /dev/null 2>&1
  206. ;;
  207. 'put' )
  208. # extract options
  209. mput_options=''
  210. do_mkdir=''
  211. for option in ${options} ; do
  212. case ${option} in
  213. -f | --force ) mput_options="${mput_options} -f" ;;
  214. -m | --mkdir ) do_mkdir='true' ;;
  215. -* )
  216. err "$prog - unknown option '${option}' for task '${task}'"
  217. errit 1
  218. ;;
  219. esac
  220. done
  221. # extract source and destination filespec:
  222. source_spec=''
  223. dest_spec=''
  224. for spec in ${filespecs} ; do
  225. EvalSpecials ${spec}
  226. if [ -z "${source_spec}" ]; then
  227. source_spec=${FS}
  228. elif [ -z "${dest_spec}" ]; then
  229. dest_spec=${FS}
  230. else
  231. err "$prog - ERROR - task '${task}' requires source and destination file specifications only."
  232. errit 1
  233. fi
  234. done
  235. if [ -z "${dest_spec}" ]; then
  236. err "$prog - ERROR - task '${task}' requires source and destination file specifications."
  237. errit 1
  238. fi
  239. # set umask ?
  240. test ${umask} && mumask ${umask}
  241. # set special group names:
  242. case ${dest_spec} in
  243. /fa/ks/* ) mnewgrp ksarch ;;
  244. esac
  245. # create destination directory ?
  246. if [ ${do_mkdir} ]; then
  247. # o destination is an existing file : not necessary
  248. # o destination is an existing directory : not necessary
  249. #if ! ${script} exist ${dest_spec} ; then
  250. # do not test if directory already exists,
  251. # this takes more time than just creation;
  252. # supress error messages about already existing directories ...
  253. dest_dir=`/usr/bin/dirname ${dest_spec}`
  254. outfile="/tmp/$prog.out"
  255. if ! ( mmkdir -p ${dest_dir} > ${outfile} 2>&1 ) ; then
  256. /bin/cat ${outfile}
  257. /bin/rm -f ${outfile}
  258. err "$prog - ERROR from 'mmkdir -p ${dest_dir}' :"
  259. errit 1
  260. fi
  261. /bin/rm -f ${outfile}
  262. #fi
  263. fi
  264. # copy file; suppress error messages:
  265. outfile="/tmp/$prog.out"
  266. if ! ( mput ${mput_options} ${source_spec} ${dest_spec} > ${outfile} 2>&1 ) ; then
  267. /bin/cat ${outfile}
  268. /bin/rm -f ${outfile}
  269. err "$prog - ERROR from 'mput ${mput_options} ${source_spec} ${dest_spec}' :"
  270. errit 1
  271. fi
  272. /bin/rm -f ${outfile}
  273. ;;
  274. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  275. # task ???
  276. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  277. * )
  278. err "$prog - ERROR - unsupported task '${task}'"
  279. errit 1
  280. ;;
  281. esac # tasks
  282. # -------------------------------------------------------------
  283. # --- end
  284. # -------------------------------------------------------------
  285. test ${verbose} && echo "$prog - end"
  286. # ok
  287. exit 0