final touchups
[tor.git] / contrib / directory-archive / fetch-all-functions
bloba9335bda7b6a1315409a744ec8457c013e876432
1 #!/bin/bash
3 # function used by fetch-all* to download server descriptors and
4 # extra info documents
6 # Copyright (c) 2005, 2006, 2007, 2008 Peter Palfrader
8 # Permission is hereby granted, free of charge, to any person obtaining a copy
9 # of this software and associated documentation files (the "Software"), to deal
10 # in the Software without restriction, including without limitation the rights
11 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 # copies of the Software, and to permit persons to whom the Software is
13 # furnished to do so, subject to the following conditions:
15 # The above copyright notice and this permission notice shall be included in
16 # all copies or substantial portions of the Software.
18 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 # SOFTWARE.
27 fetch_digest() {
28 local digest
29 local objecttype
30 local urlpart
31 local pathpart
32 local target
33 local targetdir
34 local dirserver
35 local ei
37 digest="$1"
38 objecttype="$2"
39 if [ "$objecttype" = "server-descriptor" ] ; then
40 urlpart="server"
41 pathpart="server-descriptor"
42 elif [ "$objecttype" = "extra-info" ] ; then
43 urlpart="extra"
44 pathpart="extra-info"
45 else
46 echo "Called fetch_digest with illegal objecttype '$objecttype'" >&2
47 exit 1
49 target=$( echo $digest | sed -e 's#^\(.\)\(.\)#'"$pathpart"'/\1/\2/\1\2#' )
50 targetdir=$( dirname $target )
51 [ -d "$targetdir" ] || mkdir -p "$targetdir"
52 if ! [ -e "$target" ]; then
53 for dirserver in $DIRSERVERS; do
54 wget -q -O "$target" http://$dirserver/tor/$urlpart/d/"$digest" || rm -f "$target"
55 if [ -s "$target" ]; then
56 if egrep '^opt extra-info-digest ' "$target" > /dev/null; then
57 ei=$( egrep '^opt extra-info-digest ' "$target" | awk '{print $3}' | tr 'A-F' 'a-f' )
58 fetch_digest "$ei" "extra-info"
59 elif egrep '^extra-info-digest ' "$target" > /dev/null; then
60 ei=$( egrep '^extra-info-digest ' "$target" | awk '{print $2}' | tr 'A-F' 'a-f' )
61 fetch_digest "$ei" "extra-info"
63 break
64 else
65 rm -f "$target"
67 done
69 #if ! [ -e "$target" ]; then
70 # echo "$objecttype $digest" >> failed
71 #fi
74 if [ -x /usr/bin/base64 ] ; then
75 base64-decode() {
76 /usr/bin/base64 -d
78 else
79 base64-decode() {
80 perl -MMIME::Base64 -e 'print decode_base64(<>)'