mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

811 lines
22 KiB

  1. #!/bin/bash
  2. #
  3. # --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  4. #
  5. # This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  6. # Please add additional copyright information _after_ the line containing
  7. # the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  8. # the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  9. #
  10. # ROCK Linux: rock-src/scripts/Download
  11. # ROCK Linux is Copyright (C) 1998 - 2003 Clifford Wolf
  12. #
  13. # This program is free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation; either version 2 of the License, or
  16. # (at your option) any later version. A copy of the GNU General Public
  17. # License can be found at Documentation/COPYING.
  18. #
  19. # Many people helped and are helping developing ROCK Linux. Please
  20. # have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  21. # file for details.
  22. #
  23. # --- ROCK-COPYRIGHT-NOTE-END ---
  24. #
  25. # Run this command from the ROCK directory as ./scripts/Download [ options ]
  26. #
  27. # It enables you to download source files as described in the package
  28. # definitions (optionally using a mirroring 'cache' server).
  29. #
  30. # This script also allows for checksum display/validation.
  31. #
  32. # Key to understanding this script is that all file lists get broken
  33. # down to a single file download using ./scripts/Download filename.
  34. umask 022
  35. if [ "$1" = '--help' ] ; then
  36. { echo
  37. echo "Usage:"
  38. echo
  39. echo " ./scripts/Download [options] [ Filename(s) ]"
  40. echo " ./scripts/Download [options] -pattern Pattern(s)"
  41. echo " ./scripts/Download [options] -package Package(s)"
  42. echo " ./scripts/Download [options] -repository Repositories"
  43. echo " ./scripts/Download [options] { -all | -required }"
  44. echo
  45. echo " Where [options] is an alias for:"
  46. echo " [ -cfg <config> ] [ -nock ] [ -alt-dir <AlternativeDirectory> ]"
  47. echo " [ -mirror <URL> | -check ] [ -try-questionable ] [ -notimeout ]"
  48. echo " [ -longtimeout ] [ -curl-opt <curl-option>[:<curl-option>[:..]] ]"
  49. echo " [ -proxy <server>[:<port>] ] [ -proxy-auth <username>[:<password>] ]"
  50. echo
  51. echo " On default, this script auto-detects the best ROCK Linux mirror."
  52. echo
  53. echo " Mirrors can also be a local directories in the form of 'file:///<dir>'."
  54. echo
  55. echo " ./scripts/Download -mk-cksum Filename(s)"
  56. echo " ./scripts/Download [ -list | -list-unknown | -list-missing | -list-cksums ]"
  57. echo
  58. echo "See '-mirror none' output for help on bypassing the official mirrors."
  59. echo ; } >&2
  60. exit 1
  61. fi
  62. # -mk-cksum mode (display ROCK type package checksum): it
  63. # displays the checksum ROCK validates against.
  64. #
  65. # Currently bz2, tbz2, gz, tgz, Z are unpacked
  66. #
  67. if [ "$1" = -mk-cksum ] ; then
  68. shift
  69. for x ; do
  70. echo -n "$x: "
  71. if [ ! -f "$x" ] ; then
  72. echo "No such file."
  73. elif [ "${x%.bz2}" != "$x" -o "${x%.tbz2}" != "$x" ] ; then
  74. bunzip2 < "$x" | cksum | cut -f1 -d' '
  75. elif [ "${x%.gz}" != "$x" -o "${x%.tgz}" != "$x" ] ; then
  76. gunzip < "$x" | cksum | cut -f1 -d' '
  77. elif [ "${x%.Z}" != "$x" ] ; then
  78. uncompress < "$x" | cksum | cut -f1 -d' '
  79. else
  80. cksum < "$x" | cut -f1 -d' '
  81. fi
  82. done
  83. exit 1
  84. fi
  85. # Handle options passed on the command line
  86. #
  87. mkdir -p src/ download/ ; config=default
  88. this_is_the_2nd_run=0
  89. mirror='' ; checkonly=0 ; altdir='' ; loop=1
  90. tryques=0 ; nocheck=0 ; options='-this_is_the_2nd_run '
  91. notimeout=0 ; curl_options='--disable-epsv'
  92. #
  93. while [ $loop -eq 1 ] ; do
  94. case "$1" in
  95. -this_is_the_2nd_run)
  96. this_is_the_2nd_run=1
  97. shift ;;
  98. -cfg)
  99. options="$options -cfg $2"
  100. config="$2" ; shift ; shift ;;
  101. -nock)
  102. # -nock skips checksum checking (don't use lightly)
  103. options="$options -nock"
  104. nocheck=1 ; shift ;;
  105. -mirror)
  106. # -mirror uses a mirror for finding source files
  107. if [ "$2" = none ]; then
  108. echo
  109. echo "The option '-mirror none' is not supported anymore!"
  110. echo
  111. echo "You may 'echo none > download/Mirror' if you really"
  112. echo "want to use the original download resources. However, this"
  113. echo "is not supported and if such a download fails, this is not"
  114. echo "a bug in ROCK Linux and doesn't neccessarily needs fixing."
  115. echo
  116. exit 1;
  117. else
  118. mkdir -p download
  119. echo "$2" > download/Mirror
  120. options="$options -mirror $2"
  121. mirror="$2"
  122. fi
  123. shift ; shift ;;
  124. -check)
  125. # -check just validates the file using the checksum
  126. options="$options -check"
  127. checkonly=1 ; shift ;;
  128. -notimeout)
  129. # don't add timeout curl options
  130. options="$options -notimeout"
  131. notimeout=2 ; shift ;;
  132. -longtimeout)
  133. # don't add timeout curl options
  134. options="$options -longtimeout"
  135. notimeout=1 ; shift ;;
  136. -curl-opt)
  137. # additional curl options
  138. options="$options -curl-opt $2"
  139. curl_options="$curl_options `echo $2 | tr : ' '`"
  140. shift ; shift ;;
  141. -proxy)
  142. # proxy option for curl
  143. mkdir -p download
  144. echo -n "$2" > download/Proxy
  145. options="$options -proxy $2"
  146. shift ; shift ;;
  147. -proxy-auth)
  148. # proxy authentication for curl - can be seen with ps!
  149. mkdir -p download
  150. echo -n "$2" > download/Proxy-auth
  151. chmod 600 download/Proxy-auth
  152. options="$options -proxy-auth $2"
  153. shift ; shift ;;
  154. -alt-dir)
  155. # check for an alternative directory where to search for
  156. # package source tarballs
  157. options="$options -alt-dir $2"
  158. altdir=$2 ; shift ; shift ;;
  159. -try-questionable)
  160. # also try to download questionable URLs
  161. options="$options -try-questionable"
  162. tryques=1 ; shift ;;
  163. *)
  164. loop=0 ;;
  165. esac
  166. done
  167. if [ $notimeout -eq 0 ] ; then
  168. curl_options="$curl_options -y 10 -Y 10 --connect-timeout 60"
  169. fi
  170. if [ $notimeout -eq 1 ] ; then
  171. curl_options="$curl_options -y 60 -Y 1 --connect-timeout 300"
  172. fi
  173. if [ -f download/Proxy ]; then
  174. proxy="`cat download/Proxy`"
  175. if [ "$proxy" ]; then
  176. test $this_is_the_2nd_run -eq 0 && \
  177. echo "INFO: Setting proxy to $proxy."
  178. curl_options="$curl_options --proxy $proxy"
  179. else
  180. echo "INFO: Empty proxy definition... removing download/Proxy."
  181. rm download/Proxy
  182. fi
  183. fi
  184. if [ -f download/Proxy-auth ]; then
  185. proxyauth="`cat download/Proxy-auth`"
  186. if [ "$proxyauth" ]; then
  187. test $this_is_the_2nd_run -eq 0 && \
  188. echo "INFO: Setting proxy authentication information."
  189. curl_options="$curl_options --proxy-user $proxyauth"
  190. else
  191. echo "INFO: No proxy-auth information... removing download/Proxy-auth."
  192. rm download/Proxy-auth
  193. fi
  194. fi
  195. # cksum_chk filename cksum origfile
  196. #
  197. # This function verifies the checksum. If it fails it renames the file
  198. # to file.chksum-err and returns failure.
  199. #
  200. # It seams like the [ ] command has problems with comparing high numbers.
  201. # That's why I'm using a text comparison here.
  202. #
  203. # Not doing anything if checksum is '0' or a text of 'X'.
  204. #
  205. cksum_chk() {
  206. y="`echo $2 | sed 's,^0*,,;'`"
  207. [ $nocheck = 1 -o -z "$y" -o -z "${2//X/}" ] && return 0
  208. x="`cksum "$1" | cut -f1 -d' ' | sed 's,^0*,,;'`"
  209. if [ "$x" != "$y" ] ; then
  210. # Add .cksum-err extension to filename:
  211. echo "Cksum ERROR: $3.cksum-err ($x)"
  212. mv "$3" "$3.cksum-err" ; return 1
  213. fi
  214. return 0
  215. }
  216. # Autodetect best Mirror and safe url in $mirror
  217. #
  218. detect_mirror() {
  219. if [ -f download/Mirror ] ; then
  220. mirror="`cat download/Mirror`"
  221. if [ -z "$mirror" -o "$mirror" = "none" ] ; then
  222. echo "INFO: Found download/Mirror: none" \
  223. "(use the original download locations)"
  224. else
  225. echo "INFO: Found cached mirror URL in download/Mirror:"
  226. echo "INFO: $mirror"
  227. fi
  228. echo "INFO: To force a new mirror auto-detection, remove download/Mirror."
  229. else
  230. echo "INFO: Auto-detecting best mirror ..."
  231. eval "$(egrep '^(rockver)=' scripts/parse-config)"
  232. echo "INFO: Downloading mirror-list from www.rocklinux.net."
  233. curl -s -S $curl_options -o src/Download-Mirror-List \
  234. "http://www.rocklinux.net/mirrors.cgi?$rockver"
  235. bestval=0 ; result='No Mirror Found!'
  236. while read mirror_name ; do
  237. if [ "${mirror_name#=}" != "$mirror_name" ] ; then
  238. mirror_name="${mirror_name#= }"
  239. mirror_name="${mirror_name% =}"
  240. read mirror_url
  241. echo -n "INFO: Testing <$mirror_name> ..."
  242. val="$(curl -s $curl_options -m 20 "${mirror_url%/}/DOWNTEST" \
  243. -w "ok %{speed_download}" -o /dev/null)"
  244. if [ "$val" = "${val#ok }" -o "$val" = "ok 0.000" ] ; then
  245. echo " error"
  246. else
  247. xval=`echo ${val#ok } | tr -d .` ; echo " $val"
  248. if [ "$xval" -gt "$bestval" ] ; then
  249. bestval=$xval ; mirror="${mirror_url%/}"
  250. result="Using mirror <$mirror>."
  251. fi
  252. fi
  253. fi
  254. done < src/Download-Mirror-List
  255. echo $mirror > download/Mirror
  256. echo "INFO: $result"
  257. fi
  258. }
  259. # download_file local-filename download-location cksum
  260. #
  261. # This function decides if download directly or from a mirror,
  262. # validates checksum, etc.
  263. # Calls download_file_now to do the actual download.
  264. #
  265. download_file() {
  266. # Init
  267. #
  268. gzfile="$1" ; location="$2" ; cksum="$3"
  269. # Make src directory for creating tar balls
  270. mkdir -p src/
  271. # Tar ball file name:
  272. bzfile="`echo "$gzfile" | sed 's,\.\(t\?\)\(gz\|Z\)$,.\1bz2,'`"
  273. # Lock file name:
  274. lkfile="src/down.lockfile.`echo $bzfile | tr / -`"
  275. # Check if it's already there
  276. #
  277. [ -s "$bzfile" -a $checkonly != 1 ] && return 0
  278. # Make locking
  279. #
  280. if [ -s "$lkfile" ]; then
  281. echo "Found $lkfile -> skip download."
  282. return 0
  283. fi
  284. trap 'rm -f "$lkfile"' INT
  285. echo $$ > "$lkfile"
  286. # Check if we only like to test the cksum(s)
  287. #
  288. if [ $checkonly = 1 ] ; then
  289. gzfile="$bzfile"
  290. if [ ! -f "$bzfile" ] ; then
  291. echo "File missing: $bzfile"
  292. rm -f "$lkfile" ; trap INT ; return 1
  293. fi
  294. if [ -z "${cksum##X*}" ] ; then
  295. echo "No checksum (ignore): $bzfile"
  296. rm -f "$lkfile" ; trap INT ; return 1
  297. fi
  298. if [ "$cksum" -eq 0 ] ; then
  299. echo "No checksum (missing): $bzfile"
  300. rm -f "$lkfile" ; trap INT ; return 1
  301. fi
  302. elif [ -s "$gzfile" ] ; then
  303. echo ; echo "Already downloaded $gzfile ..."
  304. else
  305. echo ; echo "Downloading $gzfile ..."
  306. # Existing *.cksum-err
  307. #
  308. if [ -s "$gzfile.cksum-err" ] ; then
  309. # cksum-err file alread exists:
  310. echo "ERROR: Found $gzfile.cksum-err."
  311. echo "ERROR: That means that we downloaded the" \
  312. "file already and it had an"
  313. echo "ERROR: incorrect checksum. Remove the" \
  314. "*.cksum-err file to force a"
  315. echo "ERROR: new download of that file."
  316. rm -f "$lkfile" ; trap INT ; return 1
  317. fi
  318. # Existing *.extck-err
  319. #
  320. if [ -s "$gzfile.extck-err" ] ; then
  321. # extck-err file alread exists:
  322. echo "ERROR: Found $gzfile.extck-err."
  323. echo "ERROR: That means that we downloaded the" \
  324. "file already and it's content"
  325. echo "ERROR: did not match it's filename extension." \
  326. "Remove the *.extck-err file"
  327. echo "ERROR: to force a new download of that file."
  328. rm -f "$lkfile" ; trap INT ; return 1
  329. fi
  330. # Questionable URL
  331. #
  332. if [ "$location" != "${location#\?}" ] ; then
  333. if [ "$tryques" = 0 ] ; then
  334. echo "ERROR: URL is marked as questionable." \
  335. "Not downloading this file."
  336. rm -f "$lkfile" ; trap INT ; return 1
  337. else
  338. echo "WARNING: URL is marked as questionable." \
  339. "Downloading it anyways."
  340. location="${location#\?}"
  341. fi
  342. fi
  343. # Make directory (if required)
  344. #
  345. if [ ! -d `dirname "$bzfile"` ] ; then
  346. mkdir -p `dirname "$bzfile"`
  347. fi
  348. # Alternative Directory
  349. #
  350. if [ "$altdir" ] ; then
  351. altfile=$(find $altdir/ -name `basename $bzfile` | head -1)
  352. else
  353. altfile=""
  354. fi
  355. if [ "$altfile" ] ; then
  356. echo "Found `basename $bzfile` as $altfile."
  357. cp -lv $altfile $bzfile ; gzfile="$bzfile"
  358. else
  359. # Mirroring
  360. #
  361. mirror="$( cat download/Mirror )"
  362. if [ "$mirror" -a "$mirror" != "none" ] ; then
  363. # try to use mirror
  364. if ! download_file_now "!$mirror/${bzfile#download/}" $bzfile $bzfile; then
  365. # oops... so try direct (original URL)
  366. echo "INFO: download from mirror failed, trying original URL."
  367. download_file_now $location $gzfile $bzfile
  368. else
  369. gzfile="$bzfile"
  370. fi
  371. else
  372. # don't want to use mirror
  373. download_file_now $location $gzfile $bzfile
  374. fi
  375. fi
  376. if [ ! -s "$gzfile" ]; then
  377. rm -f "$lkfile" ; trap INT ; return 1
  378. fi
  379. fi
  380. # Convert a .gz to .bz2 and test checksum
  381. #
  382. if [ "$gzfile" != "$bzfile" ] ; then
  383. echo "bzip'ing + cksum-test: $gzfile"
  384. gunzip < "$gzfile" > src/down.$$.dat
  385. if cksum_chk src/down.$$.dat $cksum "$gzfile" ; then
  386. bzip2 < src/down.$$.dat > "$bzfile" ; rm -f "$gzfile"
  387. fi
  388. rm -f src/down.$$.dat
  389. # Execute a cksum test on a bzip2 file
  390. #
  391. elif [ "${gzfile%.bz2}" != "$gzfile" -o \
  392. "${gzfile%.tbz2}" != "$gzfile" ]
  393. then
  394. echo "cksum-test (bzip2): $bzfile"
  395. if [ $nocheck = 0 ] ; then
  396. bunzip2 < "$bzfile" > src/down.$$.dat
  397. cksum_chk src/down.$$.dat $cksum "$bzfile"
  398. fi
  399. rm -f src/down.$$.dat
  400. # Execute a cksum test on a raw data file
  401. #
  402. else
  403. echo "cksum-test (raw): $gzfile"
  404. cksum_chk "$gzfile" $cksum "$gzfile"
  405. fi
  406. # Free Lock and finish
  407. #
  408. rm -f "$lkfile" ; trap INT ; return 0
  409. }
  410. # download_file_now location remote_filename local_filename
  411. #
  412. # This function executes the actual download using curl.
  413. #
  414. download_file_now() {
  415. local location="$1" gzfile="$2" bzfile="$3"
  416. # Create URL
  417. #
  418. if [ "${location#!}" != "$location" ] ; then
  419. url="`echo "$location" | sed 's,!,,'`"
  420. else
  421. url="`echo "$location" | \
  422. sed 's,/[^/]*$,,'`/`echo $gzfile | sed 's,.*/,,'`"
  423. fi
  424. # Check for existing Error Log
  425. #
  426. if test -s src/Download-Errors &&
  427. grep -q " $url\$" src/Download-Errors ; then
  428. echo "ERROR: According to src/Download-Errors" \
  429. "we had already an error for that URL."
  430. echo "ERROR: So I'm not trying to download" \
  431. "it again (remove src/Download-Errors"
  432. echo "ERROR: if you want to force a retry)."
  433. rm -f "$lkfile" ; trap INT ; return 1
  434. fi
  435. # Download
  436. #
  437. if [[ $url = cvs://* ]] ; then
  438. # cvs://mode:[login[:password]@]server[:port]:/path::module!revision/
  439. # btw, at least current cvs supports password at CVSROOT.
  440. url="${url#cvs://}"; url="${url%/*}"
  441. # cvs://mode:loc::module!date/
  442. #
  443. mode="${url%%:*}"; loc="${url#*:}"
  444. module="${loc##*::}"; loc="${loc%%::*}"
  445. revision="${module#*!}"; module="${module%%!*}"
  446. [[ $loc != *@* ]] && loc="anonymous@$loc"
  447. # everything after the first 'bang' (!) is analysed here
  448. # someday we could add more cvs options.
  449. #
  450. dat="$( echo $revision | \
  451. sed -n -e 's,\([0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}\),-D \1,p' )"
  452. cvsdir="src/down.cvsdir.`echo $bzfile | tr / -`"
  453. saved_pwd=$PWD ; mkdir -p $cvsdir ; cd $cvsdir
  454. echo CVS $mode $loc $dat $module
  455. { [ $mode = ssh ] && export CVS_RSH=ssh
  456. [ $mode = pserver ] && loc=":pserver:$loc"
  457. # for ssh we need some way to quitely accept
  458. # the key ...
  459. echo cvs -z9 -Q -d $loc checkout $dat -P $module
  460. if ! cvs -z9 -Q -d $loc checkout $dat -P $module
  461. then touch .cvs_error ; fi
  462. } &> .cvs_output &
  463. while fuser .cvs_output &> /dev/null ; do
  464. echo -ne `nice du -sh 2> /dev/null | cut -f1` 'downloaded from' \
  465. 'CVS archive so far...\r'
  466. sleep 3
  467. done
  468. echo `du -sh 2> /dev/null | cut -f1` 'downloaded from' \
  469. 'CVS archive (download finished).'
  470. if [ ! -f .cvs_error ] ; then
  471. cd `dirname $module`
  472. dir="`echo "$bzfile" | sed s/\.tar\.bz2$//`"
  473. dir="`basename $dir`"
  474. mv `basename $module` $dir
  475. tar --owner root --group root \
  476. --use-compress-program=bzip2 \
  477. -cf $dir.tar.bz2 $dir
  478. mv $dir.tar.bz2 $saved_pwd/$bzfile
  479. cd $saved_pwd ; rm -rf $cvsdir
  480. else
  481. cat .cvs_output
  482. cd $saved_pwd ; rm -rf $cvsdir
  483. echo ERROR: CVS $dat $loc $module \
  484. returned an error.
  485. echo "0 $gzfile $url" >> src/Download-Errors
  486. fi
  487. else
  488. if [ -s "$gzfile.incomplete" ] ; then
  489. echo "INFO: Trying to resume previous download .."
  490. resume="-C -"
  491. else
  492. resume=""
  493. fi
  494. curl -w '\rFinished downloading %{size_download} bytes in %{time_total} seconds (%{speed_download} bytes/sec). \n' -f --progress-bar $resume $curl_options "$url" -o "$gzfile.incomplete"
  495. curlret="$?"
  496. if [ "$resume" ] && \
  497. [ $curlret -eq 33 -o $curlret -eq 36 ] ; then
  498. echo "INFO: Resuming download not possible. ->" \
  499. "Overwriting old file."
  500. rm -f "$gzfile.incomplete"
  501. curl -w '\rFinished downloading %{size_download} bytes in %{time_total} seconds (%{speed_download} bytes/sec). \n' -f --progress-bar $curl_options "$url" -o "$gzfile.incomplete"
  502. curlret="$?"
  503. fi
  504. if [ $curlret -ne 0 ] ; then
  505. rm -f "$lkfile" ; trap INT
  506. case "$curlret" in
  507. 18)
  508. echo "WARNING: Got only some of the" \
  509. "file. A re-run of $0"
  510. echo "WARNING: is required to complete" \
  511. "the download." ;;
  512. 130)
  513. echo -e '\rWARNING: CURL got a SIGINT' \
  514. "(someone pressed Ctrl-C). A re-run of"
  515. echo "WARNING: $0 is required to complete" \
  516. "the download." ; sleep 1 ;;
  517. *)
  518. echo "$curlret $gzfile $url" \
  519. >> src/Download-Errors
  520. echo -e '\rERROR: CURL Returned Error' \
  521. "$curlret. Please read" \
  522. "the curl manpage." ;;
  523. esac
  524. return 1
  525. elif [ ! -s "$gzfile.incomplete" ] ; then
  526. echo "0 $gzfile $url" >> src/Download-Errors
  527. echo "ERROR: CURL returned success but" \
  528. "we have no data!"
  529. curlret=1
  530. else
  531. case "$gzfile" in
  532. *.gz|*.tgz)
  533. typeexpr="gzip compressed data" ;;
  534. *.bz2|*.tbz2)
  535. typeexpr="bzip2 compressed data" ;;
  536. *.Z|*.tZ)
  537. typeexpr="compress'd data" ;;
  538. *.zip|*.jar)
  539. typeexpr="Zip archive data" ;;
  540. *.tar)
  541. typeexpr="tar archive" ;;
  542. *)
  543. echo "WARNING: Unkown file extension: $gzfile"
  544. typeexpr="." ;;
  545. esac
  546. if file "$gzfile.incomplete" | grep -v "$typeexpr"
  547. then
  548. echo "ERROR: File type does not match" \
  549. "filename ($typeexpr)!"
  550. mv "$gzfile.incomplete" "$gzfile.extck-err"
  551. else
  552. mv "$gzfile.incomplete" "$gzfile"
  553. fi
  554. fi
  555. fi
  556. }
  557. # handle_file filename
  558. #
  559. # This function fetches the checksum and download information
  560. # from the desc files and calls the download_file function.
  561. #
  562. handle_file() {
  563. # Handle pkg file (package/.../*.desc)
  564. #
  565. tree="`echo "$1" | cut -f2 -d/`"
  566. pkg="` echo "$1" | cut -f3 -d/`"
  567. file="`echo "$1" | cut -f4 -d/`"
  568. if grep -q "^\[D\].* $file " package/$tree/$pkg/$pkg.desc 2> /dev/null
  569. then
  570. grep "^\[D\].* $file " package/$tree/$pkg/$pkg.desc |
  571. while read tag cksum file url ; do
  572. download_file "$1" "$url" "$cksum"
  573. done
  574. return 0
  575. fi
  576. # Handle target file (target/.../download.txt)
  577. #
  578. target="`echo "$1" | cut -f2 -d/`"
  579. file="`echo "$1" | cut -f3- -d/`"
  580. if grep -q " $file " target/$target/download.txt 2> /dev/null
  581. then
  582. grep " $file " target/$target/download.txt |
  583. while read cksum file url ; do
  584. download_file "$1" "$url" "$cksum"
  585. done
  586. return 0
  587. fi
  588. # Handle misc file (scripts/miscdown.txt)
  589. #
  590. if [ "${1#download/misc/}" != "$1" ] ; then
  591. file="`echo "$1" | cut -f3- -d/`"
  592. if grep -q " $file " scripts/miscdown.txt
  593. then
  594. grep " $file " scripts/miscdown.txt |
  595. while read cksum file url ; do
  596. download_file "$1" "$url" "$cksum"
  597. done
  598. return 0
  599. fi
  600. fi
  601. # Can't handle file / file not found
  602. #
  603. return 1
  604. }
  605. list_cksums() {
  606. trap '' INT
  607. { grep -H '^\[D\] ' package/*/*/*.desc | tr '\t' ' ' | tr -s ' ' |
  608. sed -e 's,^package/,download/,;'
  609. grep -H '^[X0-9]' target/*/download.txt | tr '\t' ' ' | tr -s ' ' |
  610. sed -e 's,^target/,download/,; s,:,:[D] ,;'
  611. grep -H '^[X0-9]' scripts/miscdown.txt | tr '\t' ' ' | tr -s ' ' |
  612. sed 's,^scripts/,download/misc/,; s,:,:[D] ,;'
  613. } | sed 's,^\(.*/\)[^/:]*:[^ ]* \([X0-9]*\) ,\2 \1,;' | cut -f1,2 -d' '
  614. trap INT
  615. }
  616. list() {
  617. trap '' INT
  618. list_cksums | cut -f2- -d' '
  619. trap INT
  620. }
  621. list_unknown() {
  622. trap '' INT
  623. mkdir -p src/ ; list | sed 's,\.\(t\?\)\(gz\|Z\)$,.\1bz2,' > src/down.$$.lst
  624. ls download/{INDEX,README,DOWNTEST,LAST-UPDATE} \
  625. >> src/down.$$.lst 2> /dev/null
  626. find download/* -type f -o -type l | grep -v -e download/Mirror \
  627. -e download/Proxy -e download/Proxy-auth | \
  628. while read fn ; do
  629. grep -qx "$fn" src/down.$$.lst || echo "Unknown file: $fn"
  630. done
  631. rm -f src/down.$$.lst
  632. trap INT
  633. }
  634. list_missing() {
  635. trap '' INT
  636. list | sed 's,\.\(t\?\)\(gz\|Z\)$,.\1bz2,' | \
  637. while read fn ; do
  638. [ -f "$fn" ] || echo "$fn"
  639. done
  640. trap INT
  641. }
  642. pattern() {
  643. for pattern ; do
  644. echo "Processing pattern $pattern ..."
  645. list | while read fn ; do
  646. if [ "${fn##$pattern}" = '' ] ; then
  647. echo "Matched: $fn"
  648. $0 $options "$fn"
  649. fi
  650. done
  651. done
  652. }
  653. package() {
  654. for package ; do
  655. pattern "download/*/$package/*"
  656. done
  657. }
  658. repository() {
  659. for repository ; do
  660. pattern "download/$repository/*"
  661. done
  662. }
  663. required() {
  664. # Choosen config must exist
  665. #
  666. if [ ! -f config/$config/packages ]; then
  667. echo "ERROR: Config $config doesn't exist."
  668. echo "ERROR: try ./scripts/Config -cfg $config first."
  669. exit 1
  670. fi
  671. while read on a b tree pkg c ; do
  672. if [ "$on" = "X" ] ; then
  673. grep -H '^\[D\] ' package/$tree/$pkg/$pkg.desc > src/down.$$.lst
  674. while read tag cksum file url ; do
  675. download_file "download/$tree/$pkg/$file" "$url" "$cksum"
  676. done < src/down.$$.lst ; rm -f src/down.$$.lst
  677. fi
  678. done < config/$config/packages
  679. target=`grep '^export ROCKCFG_TARGET=' config/$config/config | \
  680. cut -f2 -d= | tr -d "'"`
  681. if [ -f target/$target/download.txt ] ; then
  682. while read cksum file url ; do
  683. download_file "download/$target/$file" "$url" "$cksum"
  684. done < target/$target/download.txt
  685. fi
  686. }
  687. single_files() {
  688. for file_name ; do
  689. file_name_gz="`echo "$file_name" | sed 's,\.\(t\?\)bz2$,.\1gz,'`"
  690. file_name_Z="`echo "$file_name" | sed 's,\.\(t\?\)bz2$,.\1Z,'`"
  691. if ! handle_file "$file_name" && \
  692. ! handle_file "$file_name_gz" && \
  693. ! handle_file "$file_name_Z"
  694. then
  695. echo "ERROR: Unknown file: $file_name."
  696. fi
  697. done
  698. }
  699. all() {
  700. if [ $checkonly = 1 ] ; then list
  701. else list_missing ; fi > src/down.$$.lst
  702. while read fn ; do single_files $fn ; done < src/down.$$.lst
  703. rm -f src/down.$$.lst
  704. }
  705. # Do mirror detection only once
  706. #
  707. if [ $this_is_the_2nd_run = 0 -a "${1:0:5}" != "-list" -a $checkonly = 0 ]; then
  708. detect_mirror
  709. fi
  710. case "$1" in
  711. -list) list ;;
  712. -list-unknown) list_unknown ;;
  713. -list-missing) list_missing ;;
  714. -list-cksums) list_cksums ;;
  715. -pattern) shift ; pattern "$@" ;;
  716. -package) shift ; package "$@" ;;
  717. -repository) shift ; repository "$@" ;;
  718. -required) required ;;
  719. -all) all ;;
  720. -*|"") exec $0 --help ;;
  721. *) single_files "$@" ;;
  722. esac
  723. exit 0