summaryrefslogtreecommitdiff
blob: e014c54acb4442a34a089df5f667cb34b92b6000 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#!/bin/bash
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$
# Author: Karl Trygve Kalleberg <karltk@gentoo.org>
# Rewritten from the old, Perl-based emerge-webrsync script
# Author: Alon Bar-Lev <alon.barlev@gmail.com>
# Major rewrite from Karl's scripts.

#
# gpg key import
# KEY_ID=0x7DDAD20D
# gpg --homedir /etc/portage/gnupg --keyserver subkeys.pgp.net --recv-keys $KEY_ID
# gpg --homedir /etc/portage/gnupg --edit-key $KEY_ID trust
#

type portageq > /dev/null || exit $?
eval $(portageq envvar -v FEATURES FETCHCOMMAND GENTOO_MIRRORS \
	PORTAGE_BIN_PATH PORTAGE_GPG_DIR PORTAGE_INST_UID PORTAGE_INST_GID \
	PORTAGE_NICENESS PORTAGE_RSYNC_EXTRA_OPTS PORTAGE_TMPDIR PORTDIR \
	http_proxy ftp_proxy)
DISTDIR="${PORTAGE_TMPDIR}/emerge-webrsync"
export http_proxy ftp_proxy

# If PORTAGE_NICENESS is overriden via the env then it will
# still pass through the portageq call and override properly.
if [ -n "${PORTAGE_NICENESS}" ]; then
	renice $PORTAGE_NICENESS $$ > /dev/null
fi

source "${PORTAGE_BIN_PATH}"/isolated-functions.sh || exit 1

do_verbose=0

if hasq webrsync-gpg ${FEATURES} ; then
	WEBSYNC_VERIFY_SIGNATURE=1
else
	WEBSYNC_VERIFY_SIGNATURE=0
fi
if [ ${WEBSYNC_VERIFY_SIGNATURE} != 0 -a -z "${PORTAGE_GPG_DIR}" ]; then
	echo "Error: Please set PORTAGE_GPG_DIR in make.conf"
	exit 1
fi

get_utc_date_in_seconds() {
	date -u +"%s"
}

get_date_part() {
	local utc_time_in_secs="$1"
	local part="$2"

	if	[[ ${USERLAND} == BSD ]] ; then
		date -r ${utc_time_in_secs} -u +"${part}"
	else
		date -d @${utc_time_in_secs} -u +"${part}"
	fi
}

get_utc_second_from_string() {
	local s="$1"
	date -d "${s:0:4}-${s:4:2}-${s:6:2}" -u +"%s"
}

get_portage_timestamp() {
	local portage_current_timestamp=0

	if [ -f "${PORTDIR}/metadata/timestamp.x" ]; then
		portage_current_timestamp=$(cut -f 1 -d " " "${PORTDIR}/metadata/timestamp.x" )
	fi

	echo "${portage_current_timestamp}"
}

fetch_file() {
	local URI="$1"
	local FILE="$2"
	local opts

	if [ "${FETCHCOMMAND/wget/}" != "${FETCHCOMMAND}" ]; then
		opts="--continue"

		[ "${do_verbose}" == 0 ] && opts="$opts -q" 
	elif [ "${FETCHCOMMAND/curl/}" != "${FETCHCOMMAND}" ]; then
		opts="--continue-at -"

		[ "${do_verbose}" == 0 ] && opts="$opts -s -f" 
	else
		rm -f "${FILE}"
	fi

	echo "Fetching file ${FILE}..."

	#already set DISTDIR=
	if [ "${do_verbose}" == 0 ] ; then
		eval "${FETCHCOMMAND}" ${opts} > /dev/null && [ -s "${FILE}" ]
	else
		eval "${FETCHCOMMAND}" ${opts} && [ -s "${FILE}" ]
	fi
}

check_file_digest() {
	local digest="$1"
	local file="$2"
	local r=1

	echo "Checking digest..."

	if type -P md5sum > /dev/null; then
		md5sum -c $digest && r=0
	elif type -P md5 > /dev/null; then
		[ "$(md5 -q $file)" == "$(cut -d \ -f 1 \"$digest\")" ] && r=0
	else
		echo "Error: Cannot check digest"
		echo "No suitable md5/md5sum binaries found"
	fi

	return "${r}"
}

check_file_signature() {
	local signature="$1"
	local file="$2"
	local r=1

	if [ ${WEBSYNC_VERIFY_SIGNATURE} != 0 ]; then

		echo "Checking signature..."

		if type -p gpg > /dev/null; then
			gpg --homedir "${PORTAGE_GPG_DIR}" --verify "$signature" "$file" && r=0
		else
			echo "Error: Cannot check signature"
			echo "gpg binary not found"
		fi
	else
		r=0
	fi

	return "${r}"
}

get_snapshot_timestamp() {
	local file="$1"

	tar --to-stdout -xf "${file}" portage/metadata/timestamp.x | cut -f 1 -d " "
}

sync_local() {
	local file="$1"

	echo "Syncing local tree..."

	if type -P tarsync &> /dev/null; then
		if [ "${do_verbose}" != 0 ] ; then
			tarsync_verbose=-v
		fi
		if ! tarsync $tarsync_verbose -s 1 -o portage -g portage -e /distfiles -e /packages -e /local "${file}" "${PORTDIR}"; then
			echo "Error: tarsync failed; tarball is corrupt? (${file})"
			return 1
		fi
	else
		echo "Note: tarsync was not found, you may consider emerge it..."

		if ! tar jxf "${file}"; then
			echo "Error: tar failed to extract the image. tarball is corrupt? (${file})"
			rm -fr portage
			return 1
		fi

		# Free disk space
		rm -f "${file}"

		# Make sure user and group file ownership is ${PORTAGE_INST_UID}:${PORTAGE_INST_GID}
		chown -R ${PORTAGE_INST_UID:-0}:${PORTAGE_INST_GID:-0} portage
		cd portage
		rsync -av --progress --stats --delete --delete-after \
			--exclude='/distfiles' --exclude='/packages' \
			--exclude='/local' ${PORTAGE_RSYNC_EXTRA_OPTS} . "${PORTDIR%%/}"
		cd ..

		echo "Cleaning up..."
		rm -fr portage 
	fi

	if hasq metadata-transfer ${FEATURES} ; then
		echo "Updating cache..."
		emerge --metadata
	fi
	[ -x /etc/portage/bin/post_sync ] && /etc/portage/bin/post_sync
	return 0
}

do_snapshot() {
	local ignore_timestamp="$1"
	local date="$2"

	local r=1

	local file="portage-${date}.tar.bz2"
	local digest="${file}.md5sum"
	local signature="${file}.gpgsig"

	local have_files=0
	local mirror

	echo "Trying to retrieve ${date} snapshot..."

	for mirror in ${GENTOO_MIRRORS} ; do 

		[ -s "${file}" -a -s "${digest}" -a -s "${signature}" ] && \
			check_file_digest "${digest}" "${file}" && \
			check_file_signature "${signature}" "${file}" && \
			have_files=1

		[ ${have_files} == 0 ] && \
			fetch_file "${mirror}/snapshots/${digest}" "${digest}" && \
			fetch_file "${mirror}/snapshots/${signature}" "${signature}" && \
			fetch_file "${mirror}/snapshots/${file}" "${file}" && \
			check_file_digest "${digest}" "${file}" && \
			check_file_signature "${signature}" "${file}" && \
			have_files=1

		#
		# If timestamp is invalid
		# we want to try and retieve
		# from a different mirror
		#
		if [ ${have_files} != 0 ]; then

			echo "Getting snapshot timetasmp..."
			local snapshot_timestamp=$(get_snapshot_timestamp "${file}")

			if [ ${ignore_timestamp} == 0 ]; then
				if [ ${snapshot_timestamp} -lt $(get_portage_timestamp) ]; then
					echo "Warning: Portage is newer than snapshot"
					have_files=0
				fi
			else
				local utc_seconds=$(get_utc_second_from_string "${date}")

				#
				# Check that this snapshot
				# is what it claims to be...
				#
				if [ ${snapshot_timestamp} -lt ${utc_seconds} ] || \
					[ ${snapshot_timestamp} -gt $((${utc_seconds}+ 2*86400)) ]; then

					echo "Warning: Snapshot timestamp is not in acceptable period."
					have_files=0
				fi
			fi
		fi

		if [ ${have_files} != 0 ]; then
			break;
		else
			#
			# Remove files and use
			# a diffeernt mirror
			#
			rm -f "${file}" "${digest}" "${signature}"
		fi
	done

	if [ ${have_files} != 0 ]; then
		sync_local "${file}" && r=0
	else
		echo "Warning: ${date} snapshot was not found."
	fi
	
	rm -f "${file}" "${digest}" "${signature}"
	return "${r}"
}

do_latest_snapshot() {
	local attempts=-1
	local r=1

	echo "Fetching most recent snapshot..."

	while (( ${attempts} <  40 )) ; do
		local day
		local month
		local year
		local seconds

		attempts=$(( ${attempts} + 1 ))

		utc_attempt=$(expr $(get_utc_date_in_seconds) - 86400 \* ${attempts})

		day=$(get_date_part ${utc_attempt} "%d")
		month=$(get_date_part ${utc_attempt} "%m")
		year=$(get_date_part ${utc_attempt} "%Y")
		utc_midnight=$(get_date_part $(expr ${utc_attempt} - ${utc_attempt} % 86400) "%s")

		if [ ${utc_midnight} -lt $(($(get_portage_timestamp)-86400)) ]; then
			echo "Note: Portage content is newer than available snapshots"
			echo "use --revert option to overide."
			r=0
			break;
		fi

		if do_snapshot 0 "${year}${month}${day}"; then
			r=0
			break;
		fi
	done

	return "${r}"
}

main() {
	local arg
	local do_revert=0
	local revert_date
	
	[ ! -d "${DISTDIR}" ] && mkdir -p "${DISTDIR}"
	cd "${DISTDIR}"

	for arg in $*; do
		local v=${arg#*=}
		case ${arg} in
			--help)
				echo "usage: $0 [options]"
				echo "        --verbose (-v)    - verbose"
				echo "        --revert=yyyymmdd - revert to snapshot"
				exit 0
			;;
			--verbose|-v)
				do_verbose=1
			;;
			--revert=*)
				do_revert=1
				revert_date=${v}
			;;
			*)
				echo "Error: Invalid arguments"
				exit 1
			;;
		esac
	done

	if [ ${do_revert} != 0 ]; then
		do_snapshot 1 "${revert_date}"
	else
		do_latest_snapshot
	fi
}

main $*