summaryrefslogtreecommitdiff
blob: 5a8ce30436b238fad1fc9c9abfa3739509f23c16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
--- 1/getdelta.sh
+++ 1/getdelta.sh
@@ -25,6 +25,17 @@
 VERSION="0.7"
 
 # Changelog
+# Changes since 2008 are by Martin V\"ath <martin@mvath.de>
+# version 0.7.12  2010/09/27
+#		- Improve treatment of fetch errors
+# version 0.7.11  2010/09/14
+#		- Support alternative location of make.global and
+#		  both locations of make.conf
+# version 0.7.10  2010/08/27
+#		- Use alternative to "deltup-queued" file
+# version 0.7.9   2008/10/08
+#		- add second parameter for destination name (EAPI=2)
+#		- quote more carefully
 # version 0.7.8   2007/07/13
 #		- added option -O to call of emerge when finding mirrors
 #		- replaced some calls of coreutils with bash-internals
@@ -292,6 +303,12 @@
 	echo $vser
 }
 
+GotFile() {
+	[ -n "${1}" ] && test -s "${1}" && {
+		[ "$(head -c5 -- "${1}")" = "sorry" ] || \
+		[ "$(file -bi -- "${1}" | cut -d"/" -f1)" = "application" ]
+	}
+}
 
 # some colors for colored output
 output() {
@@ -326,13 +343,13 @@
 
 remove() {
 	output "${GREEN}You have chosen to remove ${CYAN}$1\n"
-	pushd ${DISTDIR} >/dev/null 2>&1
+	pushd -- "${DISTDIR}" >/dev/null 2>&1
 	removeme=true
 	for n in $(grep -v "^#" ${DO_NOT_REMOVE})
 	do
        		grep -q $n <<< "$1"  && removeme=false && output "${CYAN}${1}${RED} is not deleted, since it matches ${n} in ${DO_NOT_REMOVE}"
 	done
-	$removeme && rm -f $1
+	$removeme && rm -f -- "$1"
 	popd >/dev/null 2>&1
 }
 
@@ -349,7 +366,7 @@
 			-e "s/src4/srcDDD/g" \
 			-e "s/src5/srcEEE/g" \
 			-e "s/src6/srcFFF/g" \
-			-e "s/src7/srcGGG/g" <<< $MASK_FILENAME)
+			-e "s/src7/srcGGG/g" <<< "$MASK_FILENAME")
 	fi
 	
 	# ignore PR for src-files of firefox
@@ -375,7 +392,7 @@
 		   -e "s/rc//g" \
 		   -e "s/[\._-]//g" \
 		   -e "s/\+//g" \
-		   -e "s/ //g" <<< $MASK_FILENAME)
+		   -e "s/ //g" <<< "$MASK_FILENAME")
 }
 
 # create or update a config-file
@@ -384,6 +401,7 @@
 add_to_configfile LOCAL_MIRROR "" "set this to one or more (space separated) URI ending with '/' if you want to check one or more local mirror(s) first\n# most people just leave it empty."
 add_to_configfile DELTUP_SERVER "http://linux01.gwdg.de/~nlissne/deltup.php" "deltup-server to use"
 add_to_configfile FETCH "/usr/bin/wget -t 1 --passive-ftp" "command to use for downloading"
+add_to_configfile FETCHNAME "\$FETCH -O" "command to use for downloading; first parameter is the filename, second the url"
 add_to_configfile QUEUERETRY 15 "number of seconds to wait before a queued request is retried"
 add_to_configfile MAXIMUM_ACCEPTABLE_QUEUEPOS "15" "the maximum queuepos you would accept (if higher download full archive instead)"
 add_to_configfile QUEUETIMEOUT 900 "when a dtu-request is queued - how long should we wait max. before downloading the original archive instead (in seconds)"
@@ -424,21 +442,26 @@
 add_to_donotremove "^rp-pppoe"
 
 
-if [ -z $1 ]
+if [ -z "$1" ]
 then
 	COLOR=true
 	echo -e "${YELLOW}getdelta.sh version ${VERSION}"
 	echo "This script has to be called like this:"
 	echo -e "${CYAN}$0 <URI>"
 	echo -e "\n${YELLOW}To use it, you should just put the following line into your /etc/make.conf"
-	echo -e "${GREEN}FETCHCOMMAND=\"$0 \\\${URI}\""
+	echo -e "${GREEN}FETCHCOMMAND=\"$0 \\\"\\\${URI}\\\" \\\"\\\${FILE}\\\"\""
 	echo -e "\n${YELLOW}There is a config-file ${CYAN}${GETDELTA_CONFIGFILE}${YELLOW} with some variables to control the behaviour of this script."
 	echo -e "Edit it to your needs.${NORMAL}"
 	exit 1
 fi 
 # include variables from gentoo make.globals and make.conf
-source /etc/make.globals
-source /etc/make.conf
+if test -f /etc/make.globals
+then	source /etc/make.globals
+elif test -f /usr/share/portage/config/make.globals
+then	source /usr/share/portage/config/make.globals
+fi
+test -f /etc/make.conf && source /etc/make.conf
+test -f /etc/portage/make.conf && source /etc/portage/make.conf
 
 
 if ${COLOR} 
@@ -464,9 +487,10 @@
 	output "use getdelta.sh for your FETCHCOMMAND, only." &&
 	sleep 5 && exit 1
 
-pushd $DISTDIR >/dev/null 2>/dev/null
+pushd -- "$DISTDIR" >/dev/null 2>/dev/null
 ORIG_URI=$1
-NEW_FILE=$(basename $ORIG_URI)
+NEW_FILE=$2
+[ -z "${NEW_FILE}" ] && NEW_FILE=${ORIG_URI##*/}
 
 # repoman downloads metadata.dtd with FETCHCOMMAND
 # this should not be done with getdelta - so just fetch the file and exit
@@ -475,7 +499,7 @@
 # dtu files.
 if [ "${NEW_FILE}" = "metadata.dtd" ] || [ "$GETDELTA" = "0" ]
 then
-	$FETCH $@
+	$FETCHNAME "$NEW_FILE" "$ORIG_URI"
 	exit $?
 fi
 
@@ -516,7 +540,7 @@
 #
 output "${GREEN}Searching for a previously downloaded file in ${YELLOW}${DISTDIR}\n"
 
-first_chars=$(sed 's/[[:digit:]][[:print:]]*$//' <<< $NEW_FILE)
+first_chars=$(sed 's/[[:digit:]][[:print:]]*$//' <<< "$NEW_FILE")
 length_first_chars=${#first_chars}
 [ $length_first_chars -lt 3 ] && first_chars="${NEW_FILE:0:2}"
 
@@ -527,7 +551,7 @@
 let len1=${#mask}
 filelist=""
 
-for name in $( ls ${first_chars}* 2>/dev/null )
+for name in $( ls -- "${first_chars}"* 2>/dev/null )
 do
 	mask2=$(mask_name "${name}")
 	# add any file, that results in the same mask or differ not more than two letters
@@ -582,7 +606,7 @@
 
 	# find matching part of filename - first: frontmatch
 	x=0;
-	a=($NEW_FILE $filelist)
+	a=("$NEW_FILE" $filelist)
 	match=""
 	while [ -z "$match" ]
 	do
@@ -657,7 +681,7 @@
 	FILE_IS_CORRUPT=false
 	if $CHECK_OLD_FILE 
 	then
-		file_digest=$(grep -h ${best_candidate} ${FILESDIR}/digest-* | sed -n 1p)
+		file_digest=$(grep -h ${best_candidate} "${FILESDIR}"/digest-* | sed -n 1p)
 		if [ "$file_digest" ]
 		then
 			file_md5=$(cut -d ' ' -f2 <<< "$file_digest") 
@@ -669,7 +693,7 @@
 				FILE_IS_CORRUPT=true
 			fi
 		else
-			if [ $(rev <<< ${best_candidate} | cut -d. -f2 | rev) = "tar" ]
+			if [ $(rev <<< "${best_candidate}" | cut -d. -f2 | rev) = "tar" ]
 			then
 				output "${YELLOW}Could not find a digest-file for ${CYAN}${best_candidate}${YELLOW}. Testing file integrity with tar.\n"
 				if ! tar -tf ${best_candidate} >/dev/null
@@ -694,21 +718,23 @@
 			    sed -e "s/ /\\n/g" | egrep "(http|ftp)://" | 
 			    grep "${NEW_FILE}" | tail -n 1)
 		query="?have=${best_candidate}&want=${NEW_FILE}&url=${QUERY_URL}&version=${VERSION}&time=$(date +%s)"
-		output "${GREEN}Trying to download ${YELLOW}${best_candidate}-${NEW_FILE}.dtu\n"
+		dtubase="${best_candidate}-${NEW_FILE}"
+		dtufile="${dtubase}.dtu"
+		output "${GREEN}Trying to download ${YELLOW}${dtufile}\n"
 
 		# Remember where we are, and go to a new dir there we can work
 		tmp_dwn_dest="${DISTDIR}/.getdelta-`date +%N`-tmp"
-		mkdir ${tmp_dwn_dest}
+		mkdir -- "${tmp_dwn_dest}"
 		# If user abort Ctrl+C (signal 2), remove tmp-dir; enabable trap again and send it again to stop wget
-		trap "rm -r ${tmp_dwn_dest}; trap 2; kill -2 $$" 2
-		pushd ${tmp_dwn_dest} >/dev/null 2>&1
+		trap 'rm -r -- "${tmp_dwn_dest}"; trap 2; kill -2 '$$ 2
+		pushd -- "${tmp_dwn_dest}" >/dev/null 2>&1
 
 		# thanks to MATSUI Fe2+ Tetsushi for idea and patch
 		FILESIZE=$(stat -c %s "${DISTDIR}/${best_candidate}")
 		let TIMELIMIT=${FILESIZE}/${BANDWIDTH}
 		[[ $TIMELIMIT -lt $QUEUETIMEOUT ]] && QUEUETIMEOUT=$TIMELIMIT
 			
-		if $FETCH "${DELTUP_SERVER}${query}"
+		if $FETCHNAME "${dtufile}" "${DELTUP_SERVER}${query}"
 		then
 			# thanks to deelkar for this much more elegant solution to the "broken pipe" problem with "head -n1"
 			GOTFILE=$(ls -c | sed -n 1p) 
@@ -718,14 +744,14 @@
 			# may have sento to us.
 			
 			# first: the request have been queued
-			if [ "${GOTFILE}" = "deltup-queued" ]
+			if [ "${GOTFILE}" = "deltup-queued" ] || ! GotFile "${GOTFILE}"
 			then 
 				let QTMOUT=$(date +%s)+QUEUETIMEOUT
-				while [ -f deltup-queued ]
+				while [ -f deltup-queued ] || ! GotFile "${GOTFILE}"
 				do
 					output "${GREEN}destination file: ${CYAN}${NEW_FILE}\n"
-					output "${YELLOW}$(cat deltup-queued)"
-					QUEUEPOS=$(grep "has been queued" deltup-queued | cut -d. -f2 | cut -d")" -f1)
+					output "${YELLOW}$(cat -- "${GOTFILE}")"
+					QUEUEPOS=$(grep "has been queued" -- "${GOTFILE}" | cut -d. -f2 | cut -d")" -f1)
 					rm -f deltup-queued
 					TSTAMP=$(date +%s)
 					if ((TSTAMP<QTMOUT)) && ((QUEUEPOS<=MAXIMUM_ACCEPTABLE_QUEUEPOS))
@@ -741,18 +767,18 @@
 							sleep 1
 						done
 						echo -n -e "${NORMAL}"
-						$FETCH "${DELTUP_SERVER}${query}"
+						$FETCHNAME "${dtufile}" "${DELTUP_SERVER}${query}"
 						GOTFILE=$(ls -c | sed -n 1p)
 					else
 						if ((TSTAMP>=QTMOUT))
 						then 
-							GOTFILE="timeout"
+							GOTFILE=''
 							output "\n${RED}TIMEOUT exceeded.\n"
 							break
 						fi
 						if ((QUEUEPOS>MAXIMUM_ACCEPTABLE_QUEUEPOS))
 						then
-							GOTFILE="unacceptable"
+							GOTFILE=''
 							output "\n${RED}You have configured getdelta.sh not to accept this queue-position.\n"
 							output "${YELLOW}We are going to download the ${RED}full archive${YELLOW} instead.\n"
 							break
@@ -761,21 +787,31 @@
 				done
 			fi
 			
-			if [ -f ${best_candidate}-${NEW_FILE}.failed ]
+			[ -n "${GOTFILE}" ] && test -f "${GOTFILE}" || GOTFILE=""
+			newfile_failed="${dtubase}.failed"
+			test -f "${newfile_failed}" || newfile_failed=""
+			if [ -n "${newfile_failed}" ] || {
+				[ -n "${GOTFILE}" ] && [ "$(head -c5 -- "${GOTFILE}")" = "sorry" ]
+			}
 			then
 				output "\n${RED}The server could not build the dtu-file for ${NEW_FILE}\n" 
-				output "${YELLOW}reason:\n${RED}$(cat ${best_candidate}-${NEW_FILE}.failed)\n" 
-				rm -rf ${best_candidate}-${NEW_FILE}.failed
+				output "${YELLOW}reason:\n${RED}$(
+					[ -n "${newfile_failed}" ] && cat -- "${newfile_failed}"
+					[ -n "${GOTFILE}" ] && cat -- "${GOTFILE}")\n"
+				rm -f -- "${newfile_failed}" "${GOTFILE}"
 			fi
 
-			if [ -f ${best_candidate}-${NEW_FILE}.dtu ]
+			[ -n "${dtufile}" ] && test -f "${dtufile}" || dtufile=""
+			if test -f "${dtufile}"
 			then
 				output "${GREEN}Successfully fetched the dtu-file - let's build ${NEW_FILE}...\n" 
-				downloadsize=$(stat -c %s  ${best_candidate}-${NEW_FILE}.dtu)
-				if deltup -p -v -D ${DISTDIR} ${best_candidate}-${NEW_FILE}.dtu 
+				downloadsize=$(stat -c %s -- "${dtufile}")
+				if deltup -p -v -D "${DISTDIR}" "${dtufile}"
 				then 
-					newsize=$(stat -c %s ${NEW_FILE})
+					newsize=$(stat -c %s -- "${NEW_FILE}")
+					: ${newsize:=0}
 					let savedsize=${newsize}-${downloadsize}
+					[ ${newsize} -eq 0 ] && percent=-100 || \
 					let percent=${savedsize}*100/${newsize}
 					unit="bytes"
 					[ $savedsize -gt 1024 ] && let savedsize=$savedsize/1024 && unit="kB"
@@ -791,8 +827,8 @@
 					esac
 					output "${YELLOW}This dtu-file saved ${UCOLOR}${savedsize} ${unit} (${percent}%)${YELLOW} download size.\n"
 				fi
-				mv -f ${NEW_FILE} ${DISTDIR} &&
-				${REMOVE_OLD}  && remove "${best_candidate}"
+				mv -f -- "${NEW_FILE}" "${DISTDIR}" &&
+				${REMOVE_OLD}  && remove ${best_candidate}
 			fi
 
 		fi # if $FETCH "${DELTUP_SERVER}${query}"
@@ -812,7 +848,7 @@
 			read
 		fi
 		popd >/dev/null 2>&1
-		rm -rf ${tmp_dwn_dest}
+		rm -rf -- "${tmp_dwn_dest}"
 		#stop respond to trap2
 		trap 2
 	fi # if ! FILE_IS_CORRUPT
@@ -824,10 +860,10 @@
 	
 # Ok, once we are here, we should have got the delta (and used it)
 # or we still have to download the full file
-if ! [ -f ${DISTDIR}/${NEW_FILE} ] 
+if ! [ -f "${DISTDIR}/${NEW_FILE}" ] 
 then
 	output "${RED}The dtu could not be fetched,${YELLOW} downloading full file from original URL\n"
-	$FETCH $ORIG_URI
+	$FETCHNAME "$NEW_FILE" "$ORIG_URI"
 # remember we had a fallback to use correct exitcode for portage
 	FALLBACK=$?
 fi