/var/lib/sorcery/modules/dl_handlers/dl_rsync
1 #!/bin/bash
2 #---------------------------------------------------------------------
3 ##
4 ##=head1 SYNOPSIS
5 ##
6 ## Url handler functions for downloading rsync urls.
7 ##
8 ##=head1 DESCRIPTION
9 ##
10 ## This file contains functions for I<downloading> files through rsync.
11 ##
12 ## In order for rsync urls to be downloaded, the I<rsync> spell must have been
13 ## cast. This script first determines if rsync has been installed before
14 ## attempting to download a rsync url.
15 ##
16 ##=head1 RSYNC URL Format
17 ##
18 ##
19 ## rsync://SERVER::MODULE_NAME
20 ##
21 ## The above url will download the latest version of the specified
22 ## module.
23 ##
24 ##=head1 EXAMPLES
25 ##
26 ## Suppose we want to download the latest version of the sorcery
27 ## stable grimoire via rsync. We'd use the following url:
28 ##
29 ## rsync://codex.sourcemage.org::stable
30 ##
31 ##=head1 IMPLEMENTATION NOTE
32 ##
33 ## Downloading is supported but rsync url verification is not
34 ## currently supported.
35 ##
36 ##=head1 COPYRIGHT
37 ##
38 ## Copyright 2003 by the Source Mage Team
39 ##
40 ##=head1 FUNCTIONS
41 ##
42 ##=over 4
43 ##
44 #---------------------------------------------------------------------
45
46 #---------------------------------------------------------------------
47 ##=item dl_rsync_download <url>
48 ##
49 ## Fetch the specified rsync url.
50 ##
51 ## This handler supports both files and trees.
52 ##
53 #---------------------------------------------------------------------
54 function dl_rsync_get() {
55 dl_command_check rsync || return 254
56
57 local target=$1
58 local url_list=$2
59 local hints=$3
60 local dl_target=$4
61 local dl_type=$5
62 local url rc=1
63
64 [[ $target ]] &&
65 dl_connect || return 255
66
67 local dl_file=0
68 if list_find "$hints" file; then
69 dl_file=1
70 fi
71
72 for url in $url_list; do
73 url=`url_rsync_crack "$url"`
74 dl_rsync_run_rsync "$target" "$url" "$dl_target" "$dl_type"
75 rc=$?
76 [[ $rc == 0 ]] && break
77 done
78 dl_disconnect
79
80 return $rc
81 }
82
83 #---------------------------------------------------------------------
84 ##=item dl_rsync_run_rsync <url> <to>
85 ##
86 ## Private function. Calls rsync and beautifies output
87 ##
88 #---------------------------------------------------------------------
89 function dl_rsync_run_rsync() {
90 local target=$1
91 local url=$2
92 local dl_target=$3
93 local dl_type=$4
94
95 local TOTAL line retcode
96 local COUNT=0
97 local position="a"
98 local use_spinner
99
100 if [ "$dl_file" == 1 ]; then
101 TOTAL=0 # downloading a single file, not a source tree
102 else
103 let TOTAL=$(find $1 -type f 2>/dev/null | wc -l)
104 if [ "$TOTAL" -lt 2 ] ; then
105 let TOTAL=100
106 use_spinner=yes
107 else
108 let TOTAL+=10
109 fi
110 fi
111
112 message "${MESSAGE_COLOR}Running rsync...${DEFAULT_COLOR}"
113 if [ $TOTAL -lt 10 ] ; then
114 echo rsync -rz --delete --stats --progress "$url" "$target"
115 rsync -rz --delete --stats --progress "$url" "$target" #few files - big files
116 retcode=$?
117 else
118 echo rsync -rz --delete --stats -vv "$url" "$target"
119 {
120 rsync -rz --delete --stats -vv "$url" "$target"
121 echo $? > $TMP_DIR/rsync.rc
122 } | tee $TMP_DIR/rsyncout$$ |
123 while read line ; do
124 if [ "$position" == "c" ] ;then
125 #make the progress bar - as quick as possible
126 let COUNT=($COUNT+1)%$TOTAL
127 if [[ $use_spinner ]] ; then
128 progress_spinner
129 else
130 progress_bar $COUNT $TOTAL 50
131 fi
132 elif [ "$position" == "a" ] ;then
133 echo "$line" | grep -q 'receiving file list ...' && position="b"
134 echo "$line" #print welcome message until filelist almost starts
135 elif [ "$position" == "b" ] ;then
136 echo "$line" | grep -q 'done' && position="c"
137 echo "$line" #only now look for done = only filenames following
138 fi
139 done
140 retcode=$(<$TMP_DIR/rsync.rc)
141 [[ $retcode == 0 ]] &&
142 [[ $use_spinner ]] ||
143 progress_bar $TOTAL $TOTAL 50 # make the progress bar show 100, on success
144
145 echo
146 tail -n 25 $TMP_DIR/rsyncout$$ | head -n 12 #stats w/o useless stack stats
147 echo
148 rm -f $TMP_DIR/rsyncout$$
149 fi
150 if test -d $target; then
151 eval "$dl_type=\"tree\""
152 else
153 eval "$dl_type=\"file\""
154 fi
155 eval "$dl_target=\"$target\""
156 return $retcode
157 }
158
159
160
161 #---------------------------------------------------------------------
162 ##=back
163 ##
164 ##=head1 LICENSE
165 ##
166 ## This software is free software; you can redistribute it and/or modify
167 ## it under the terms of the GNU General Public License as published by
168 ## the Free Software Foundation; either version 2 of the License, or
169 ## (at your option) any later version.
170 ##
171 ## This software is distributed in the hope that it will be useful,
172 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
173 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
174 ## GNU General Public License for more details.
175 ##
176 ## You should have received a copy of the GNU General Public License
177 ## along with this software; if not, write to the Free Software
178 ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
179 ##
180 #---------------------------------------------------------------------