functions 36.2 KB
Newer Older
1 2 3
############################################################### smallutils

smallyes() {
4
	YES="${1-y}"
5
	while echo "$YES" 2>/dev/null ; do : ; done
6 7
}

8
in_path () {
Joey Hess's avatar
Joey Hess committed
9 10
	local OLD_IFS="$IFS"
	IFS=":"
11 12
	for dir in $PATH; do
		if [ -x "$dir/$1" ]; then
Joey Hess's avatar
Joey Hess committed
13
			IFS="$OLD_IFS"
14 15 16
			return 0
		fi
	done
Joey Hess's avatar
Joey Hess committed
17
	IFS="$OLD_IFS"
18 19 20
	return 1
}

21 22 23
############################################################### interaction

error () {
24 25 26 27 28 29 30 31 32 33 34 35 36
	# <error code> <name> <string> <args>
	local err="$1"
	local name="$2"
	local fmt="$3"
	shift; shift; shift
	if [ "$USE_DEBIANINSTALLER_INTERACTION" ]; then
		(echo "E: $name"
		for x in "$@"; do echo "EA: $x"; done
		echo "EF: $fmt") >&4
	else
		(printf "E: $fmt\n" "$@") >&4
	fi
	exit $err
37 38 39
}

warning () {
40 41 42 43 44 45 46 47 48 49 50
	# <name> <string> <args>
	local name="$1"
	local fmt="$2"
	shift; shift
	if [ "$USE_DEBIANINSTALLER_INTERACTION" ]; then
		(echo "W: $name"
		for x in "$@"; do echo "WA: $x"; done
		echo "WF: $fmt") >&4
	else
		printf "W: $fmt\n" "$@" >&4
	fi
51 52 53
}

info () {
54 55 56 57 58 59 60 61 62 63 64
	# <name> <string> <args>
	local name="$1"
	local fmt="$2"
	shift; shift
	if [ "$USE_DEBIANINSTALLER_INTERACTION" ]; then
		(echo "I: $name"
		for x in "$@"; do echo "IA: $x"; done
		echo "IF: $fmt") >&4
	else
		printf "I: $fmt\n" "$@" >&4
	fi
65 66 67 68 69 70 71 72
}

PROGRESS_NOW=0
PROGRESS_END=0
PROGRESS_NEXT=""
PROGRESS_WHAT=""

progress_next () {
73
	PROGRESS_NEXT="$1"
74 75 76
}

wgetprogress () {
77
	[ ! "$VERBOSE" ] && QSWITCH="-q"
78 79 80 81 82
	local ret=0
	if [ "$USE_DEBIANINSTALLER_INTERACTION" ] && [ "$PROGRESS_NEXT" ]; then
		wget "$@" 2>&1 >/dev/null | $PKGDETAILS "WGET%" $PROGRESS_NOW $PROGRESS_NEXT $PROGRESS_END >&3
		ret=$?
	else
83
		wget $QSWITCH "$@" 
84 85 86
		ret=$?
	fi
	return $ret
87 88 89
}

progress () {
90 91 92 93 94 95 96 97 98 99 100 101 102 103
	# <now> <end> <name> <string> <args>
	local now="$1"
	local end="$2"
	local name="$3"
	local fmt="$4"
	shift; shift; shift; shift
	if [ "$USE_DEBIANINSTALLER_INTERACTION" ]; then
		PROGRESS_NOW="$now"
		PROGRESS_END="$end"
		PROGRESS_NEXT=""
		(echo "P: $now $end $name"
		for x in "$@"; do echo "PA: $x"; done
		echo "PF: $fmt") >&3
	fi
104 105 106
}

dpkg_progress () {
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
	# <now> <end> <name> <desc> UNPACKING|CONFIGURING
	local now="$1"
	local end="$2"
	local name="$3"
	local desc="$4"
	local action="$5"
	local expect=

	if [ "$action" = UNPACKING ]; then
		expect=half-installed
	elif [ "$action" = CONFIGURING ]; then
		expect=half-configured
	fi

	dp () {
		now="$(($now + ${1:-1}))"
	}

	exitcode=0
	while read status pkg qstate; do
		if [ "$status" = "EXITCODE" ]; then
			exitcode="$pkg"
			continue
		fi
		[ "$qstate" = "$expect" ] || continue
		case $qstate in
		    half-installed)
			dp; progress "$now" "$end" "$name" "$desc"
			info "$action" "Unpacking %s..." "${pkg%:}"
			expect=unpacked
			;;
		    unpacked)
			expect=half-installed
			;;
		    half-configured)
			dp; progress "$now" "$end" "$name" "$desc"
			info "$action" "Configuring %s..." "${pkg%:}"
			expect=installed
			;;
		    installed)
			expect=half-configured
			;;
		esac
	done
	return $exitcode
152 153 154 155
}

############################################################# set variables

156
default_mirror () {
157
	DEF_MIRROR="$1"
158 159
}

160 161
FINDDEBS_NEEDS_INDICES=false
finddebs_style () {
162 163 164 165 166 167 168 169 170 171
	case "$1" in
	    hardcoded)
		;;
	    from-indices)
		FINDDEBS_NEEDS_INDICES=true
		;;
	    *)
		error 1 BADFINDDEBS "unknown finddebs style"
		;;
	 esac
172 173 174
}

mk_download_dirs () {
175 176 177 178
	if [ $DLDEST = "apt_dest" ]; then
		mkdir -p "$TARGET/$APTSTATE/lists/partial"
		mkdir -p "$TARGET/var/cache/apt/archives/partial"
	fi
179 180 181
}

download_style () {
182 183 184 185 186 187 188 189 190 191 192 193 194 195
	case "$1" in
	    apt)
		if [ "$2" = "var-state" ]; then
			APTSTATE=var/state/apt
		else
			APTSTATE=var/lib/apt
		fi
		DLDEST=apt_dest
		export APTSTATE DLDEST DEBFOR
		;;
	    *)
		error 1 BADDLOAD "unknown download style"
		;;
	esac
196 197
}

198
keyring () {
199 200 201 202 203 204
	if [ -z "$KEYRING" ]; then
		if [ -e "$1" ]; then
			KEYRING="$1"
		elif [ -z "$DISABLE_KEYRING" ]; then
			if [ -n "$DEF_HTTPS_MIRROR" ] && [ -z "$USER_MIRROR" ] ; then
				info KEYRING "Keyring file not available at %s; switching to https mirror %s" "$1" "$DEF_HTTPS_MIRROR"
205
				USER_MIRROR="$DEF_HTTPS_MIRROR"
206 207 208 209
			else
				warning KEYRING "Cannot check Release signature; keyring file not available %s" "$1"
			fi
		fi
210 211 212
	fi
}

213 214 215
########################################################## variant handling

doing_variant () {
216 217 218
	if [ "$1" = "$VARIANT" ]; then return 0; fi
	if [ "$1" = "-" ] && [ "$VARIANT" = "" ]; then return 0; fi
	return 1
219 220 221 222
}

SUPPORTED_VARIANTS="-"
variants () {
223 224 225 226 227
	SUPPORTED_VARIANTS="$*"
	for v in $*; do
		if doing_variant "$v"; then return 0; fi
	done
	error 1 UNSUPPVARIANT "unsupported variant"
228 229 230 231 232
}

################################################# work out names for things

mirror_style () {
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
	case "$1" in
	    release)
		DOWNLOAD_INDICES=download_release_indices
		DOWNLOAD_DEBS=download_release
		;;
	    main)
		DOWNLOAD_INDICES=download_main_indices
		DOWNLOAD_DEBS=download_main
		;;
	    *)
		error 1 BADMIRROR "unknown mirror style"
		;;
	esac
	export DOWNLOAD_INDICES
	export DOWNLOAD_DEBS
248 249
}

250 251 252 253 254
force_md5 () {
	DEBOOTSTRAP_CHECKSUM_FIELD=MD5SUM
	export DEBOOTSTRAP_CHECKSUM_FIELD
}

Joey Hess's avatar
Joey Hess committed
255 256 257
verify_checksum () {
	# args: dest checksum size
	local expchecksum="$2"
258
	local expsize="$3"
259
	if [ "$DEBOOTSTRAP_CHECKSUM_FIELD" = "MD5SUM" ]; then
260 261 262 263 264 265 266
		if in_path md5sum; then
			relchecksum=`md5sum < "$1" | sed 's/ .*$//'`
		elif in_path md5; then
			relchecksum=`md5 < "$1"`
		else
			error 1 SIGCHECK "Cannot check md5sum"
		fi
267
	else
268 269 270 271 272 273 274
		if in_path "sha${SHA_SIZE}sum"; then
			relchecksum=`sha${SHA_SIZE}sum < "$1" | sed 's/ .*$//'`
		elif in_path "sha${SHA_SIZE}"; then
			relchecksum=`sha${SHA_SIZE} < "$1"`
		else
			error 1 SIGCHECK "Cannot check sha${SHA_SIZE}sum"
		fi
275
	fi
276
	relsize=`wc -c < "$1"`
Joey Hess's avatar
Joey Hess committed
277
	if [ "$expsize" -ne "$relsize" ] || [ "$expchecksum" != "$relchecksum" ]; then
278 279 280
		return 1
	fi
	return 0
281 282 283
}

get () {
284
	# args: from dest 'nocache'
Joey Hess's avatar
Joey Hess committed
285
	# args: from dest [checksum size] [alt {checksum size type}]
286
	local displayname
287
	local versionname
288 289
	if [ "${2%.deb}" != "$2" ]; then
		displayname="$(echo "$2" | sed 's,^.*/,,;s,_.*$,,')"
290
		versionname="$(echo "$2" | sed 's,^.*/,,' | cut -d_ -f2)"
291 292 293 294 295
	else
		displayname="$(echo "$1" | sed 's,^.*/,,')"
	fi

	if [ -e "$2" ]; then
296
		if [ -z "$3" ]; then
297
			return 0
298
		elif [ "$3" = nocache ]; then
299
			rm -f "$2"
300
		else
301
			info VALIDATING "Validating %s %s" "$displayname" "$versionname"
Joey Hess's avatar
Joey Hess committed
302
			if verify_checksum "$2" "$3" "$4"; then
303 304 305 306
				return 0
			else
				rm -f "$2"
			fi
307 308
		fi
	fi
309 310 311 312 313
	# Drop 'nocache' option
	if [ "$3" = nocache ]; then
		set "$1" "$2"
	fi

314 315 316 317 318 319 320 321 322
	if [ "$#" -gt 5 ]; then
		local st=3
		if [ "$5" = "-" ]; then st=6; fi
		local order="$(a=$st; while [ "$a" -le $# ]; do eval echo \"\${$(($a+1))}\" $a;
		a=$(($a + 3)); done | sort -n | sed 's/.* //')"
	else
		local order=3
	fi
	for a in $order; do
Joey Hess's avatar
Joey Hess committed
323
		local checksum="$(eval echo \${$a})"
324 325 326 327
		local siz="$(eval echo \${$(( $a+1 ))})"
		local typ="$(eval echo \${$(( $a+2 ))})"
		local from
		local dest
328
		local iters=0
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

		case "$typ" in
		    bz2) from="$1.bz2"; dest="$2.bz2" ;;
		    gz)  from="$1.gz"; dest="$2.gz" ;;
		    *)   from="$1"; dest="$2" ;;
		esac

		if [ "${dest#/}" = "$dest" ]; then
			dest="./$dest"
		fi
		local dest2="$dest"
		if [ -d "${dest2%/*}/partial" ]; then
			dest2="${dest2%/*}/partial/${dest2##*/}"
		fi

344
		while [ "$iters" -lt 10 ]; do
345
			info RETRIEVING "Retrieving %s %s" "$displayname" "$versionname"
346 347
			if ! just_get "$from" "$dest2"; then continue 2; fi
			if [ "$checksum" != "" ]; then
348
				info VALIDATING "Validating %s %s" "$displayname" "$versionname"
349 350 351
				if verify_checksum "$dest2" "$checksum" "$siz"; then
					checksum=""
				fi
352
			fi
353 354 355 356 357 358 359 360 361 362 363 364 365 366
			if [ -z "$checksum" ]; then
				[ "$dest2" = "$dest" ] || mv "$dest2" "$dest"
				case "$typ" in
				    gz)  gunzip "$dest" ;;
				    bz2) bunzip2 "$dest" ;;
				esac
				return 0
			else
				rm -f "$dest2"
				warning RETRYING "Retrying failed download of %s" "$from"
				iters="$(($iters + 1))"
			fi
		done
		warning CORRUPTFILE "%s was corrupt" "$from"
367 368
	done
	return 1
369 370 371
}

just_get () {
372 373 374 375 376 377 378 379 380 381
	# args: from dest
	local from="$1"
	local dest="$2"
	mkdir -p "${dest%/*}"
	if [ "${from#null:}" != "$from" ]; then
		error 1 NOTPREDL "%s was not pre-downloaded" "${from#null:}"
	elif [ "${from#http://}" != "$from" ] || [ "${from#ftp://}" != "$from" ]; then
		# http/ftp mirror
		if wgetprogress -O "$dest" "$from"; then
			return 0
382 383 384 385 386 387 388 389
		else
			rm -f "$dest"
			return 1
		fi
	elif [ "${from#https://}" != "$from" ] ; then
		# http/ftp mirror
		if wgetprogress $CHECKCERTIF $CERTIFICATE $PRIVATEKEY -O "$dest" "$from"; then
			return 0
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
		else
			rm -f "$dest"
			return 1
		fi
	elif [ "${from#file:}" != "$from" ]; then
		local base="${from#file:}"
		if [ "${base#//}" != "$base" ]; then
			base="/${from#file://*/}"
		fi
		if [ -e "$base" ]; then
			cp "$base" "$dest"
			return 0
		else
			return 1
		fi
	elif [ "${from#ssh:}" != "$from" ]; then
		local ssh_dest="$(echo $from | sed -e 's#ssh://##' -e 's#/#:/#')"
		if [ -n "$ssh_dest" ]; then
			scp "$ssh_dest" "$dest"
			return 0
		else
			return 1
		fi
	else
		error 1 UNKNOWNLOC "unknown location %s" "$from"
	fi
416 417 418
}

download () {
419 420
	mk_download_dirs
	"$DOWNLOAD_DEBS" $(echo "$@" | tr ' ' '\n' | sort)
421 422 423
}

download_indices () {
424 425
	mk_download_dirs
	"$DOWNLOAD_INDICES" $(echo "$@" | tr ' ' '\n' | sort)
426 427 428
}

debfor () {
429 430 431 432 433 434 435
	(while read pkg path; do
		for p in "$@"; do
			[ "$p" = "$pkg" ] || continue;
			echo "$path"
		done
	 done <"$TARGET/debootstrap/debpaths"
	)
436 437 438
}

apt_dest () {
439 440 441 442 443 444
	# args:
	#   deb package version arch mirror path
	#   pkg suite component arch mirror path
	#   rel suite mirror path
	case "$1" in
	    deb)
445
		echo "/var/cache/apt/archives/${2}_${3}_${4}.deb" | sed 's/:/%3a/'
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
		;;
	    pkg)
		local m="$5"
		m="debootstrap.invalid"
		#if [ "${m#http://}" != "$m" ]; then
		#	m="${m#http://}"
		#elif [ "${m#file://}" != "$m" ]; then
		#	m="file_localhost_${m#file://*/}"
		#elif [ "${m#file:/}" != "$m" ]; then
		#	m="file_localhost_${m#file:/}"
		#fi

		printf "%s" "$APTSTATE/lists/"
		echo "${m}_$6" | sed 's/\//_/g'
		;;
	    rel)
		local m="$3"
		m="debootstrap.invalid"
		#if [ "${m#http://}" != "$m" ]; then
		#	m="${m#http://}"
		#elif [ "${m#file://}" != "$m" ]; then
		#	m="file_localhost_${m#file://*/}"
		#elif [ "${m#file:/}" != "$m" ]; then
		#	m="file_localhost_${m#file:/}"
		#fi
		printf "%s" "$APTSTATE/lists/"
		echo "${m}_$4" | sed 's/\//_/g'
		;;
	esac
475 476 477 478
}

################################################################## download

Joey Hess's avatar
Joey Hess committed
479
get_release_checksum () {
480 481
	local reldest="$1"
	local path="$2"
482 483 484 485 486 487
	if [ "$DEBOOTSTRAP_CHECKSUM_FIELD" = MD5SUM ]; then
		local match="^[Mm][Dd]5[Ss][Uu][Mm]"
	else
		local match="^[Ss][Hh][Aa]$SHA_SIZE:"
	fi
	sed -n "/$match/,/^[^ ]/p" < "$reldest" | \
488 489 490
		while read a b c; do
			if [ "$c" = "$path" ]; then echo "$a $b"; fi
		done | head -n 1
491 492
}

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
extract_release_components () {
	local reldest="$1"; shift
	TMPCOMPONENTS="$(sed -n 's/Components: *//p' "$reldest")"
	for c in $TMPCOMPONENTS ; do
		eval "
		case \"\$c\" in
		    $USE_COMPONENTS)
			COMPONENTS=\"\$COMPONENTS \$c\"
			;;
		esac
		"
	done
	COMPONENTS="$(echo $COMPONENTS)"
	if [ -z "$COMPONENTS" ]; then
		mv "$reldest" "$reldest.malformed"
		error 1 INVALIDREL "Invalid Release file, no valid components"
	fi
}

512
download_release_sig () {
513 514
	local m1="$1"
	local reldest="$2"
515
	local relsigdest="$3"
516

517
	if [ -n "$KEYRING" ] && [ -z "$DISABLE_KEYRING" ]; then
518 519 520 521 522 523
		progress 0 100 DOWNRELSIG "Downloading Release file signature"
		progress_next 50
		get "$m1/dists/$SUITE/Release.gpg" "$relsigdest" nocache ||
			error 1 NOGETRELSIG "Failed getting release signature file %s" \
			"$m1/dists/$SUITE/Release.gpg"
		progress 50 100 DOWNRELSIG "Downloading Release file signature"
524 525

		info RELEASESIG "Checking Release signature"
Joey Hess's avatar
Joey Hess committed
526 527
		# Don't worry about the exit status from gpgv; parsing the output will
		# take care of that.
528 529
		(gpgv --status-fd 1 --keyring "$KEYRING" --ignore-time-conflict \
		 "$relsigdest" "$reldest" || true) | read_gpg_status
530 531
		progress 100 100 DOWNRELSIG "Downloading Release file signature"
	fi
532 533 534
}

download_release_indices () {
535 536
	local m1="${MIRRORS%% *}"
	local reldest="$TARGET/$($DLDEST rel "$SUITE" "$m1" "dists/$SUITE/Release")"
537
	local relsigdest
538 539
	progress 0 100 DOWNREL "Downloading Release file"
	progress_next 100
540 541 542
	get "$m1/dists/$SUITE/Release" "$reldest" nocache ||
		error 1 NOGETREL "Failed getting release file %s" "$m1/dists/$SUITE/Release"
	relsigdest="$TARGET/$($DLDEST rel "$SUITE" "$m1" "dists/$SUITE/Release.gpg")"
543 544
	progress 100 100 DOWNREL "Downloading Release file"

545
	download_release_sig "$m1" "$reldest" "$relsigdest"
546

547 548
	extract_release_components $reldest

549 550 551
	local totalpkgs=0
	for c in $COMPONENTS; do
		local subpath="$c/binary-$ARCH/Packages"
Joey Hess's avatar
Joey Hess committed
552 553 554 555 556 557
		local bz2i="`get_release_checksum "$reldest" "$subpath.bz2"`"
		local gzi="`get_release_checksum "$reldest" "$subpath.gz"`"
		local normi="`get_release_checksum "$reldest" "$subpath"`"
		local i=
		if [ "$normi" != "" ]; then
			i="$normi"
558
		elif in_path bunzip2 && [ "$bz2i" != "" ]; then
Joey Hess's avatar
Joey Hess committed
559
			i="$bz2i"
560
		elif in_path gunzip && [ "$gzi" != "" ]; then
Joey Hess's avatar
Joey Hess committed
561
			i="$gzi"
562
		fi
Joey Hess's avatar
Joey Hess committed
563 564
		if [ "$i" != "" ]; then
			totalpkgs="$(( $totalpkgs + ${i#* } ))"
565 566 567 568 569 570 571
		else
			mv "$reldest" "$reldest.malformed"
			error 1 MISSINGRELENTRY "Invalid Release file, no entry for %s" "$subpath"
		fi
	done

	local donepkgs=0
572
	local pkgdest
573 574 575 576
	progress 0 $totalpkgs DOWNPKGS "Downloading Packages files"
	for c in $COMPONENTS; do
		local subpath="$c/binary-$ARCH/Packages"
		local path="dists/$SUITE/$subpath"
Joey Hess's avatar
Joey Hess committed
577 578 579
		local bz2i="`get_release_checksum "$reldest" "$subpath.bz2"`"
		local gzi="`get_release_checksum "$reldest" "$subpath.gz"`"
		local normi="`get_release_checksum "$reldest" "$subpath"`"
580
		local ext=
Joey Hess's avatar
Joey Hess committed
581 582 583 584
		local i=
		if [ "$normi" != "" ]; then
			ext="$ext $normi ."
			i="$normi"
585
		fi
586
		if in_path bunzip2 && [ "$bz2i" != "" ]; then
Joey Hess's avatar
Joey Hess committed
587 588
			ext="$ext $bz2i bz2"
			i="${i:-$bz2i}"
589
		fi
590
		if in_path gunzip && [ "$gzi" != "" ]; then
Joey Hess's avatar
Joey Hess committed
591 592
			ext="$ext $gzi gz"
			i="${i:-$gzi}"
593
		fi
Joey Hess's avatar
Joey Hess committed
594
		progress_next "$(($donepkgs + ${i#* }))"
595
		for m in $MIRRORS; do
596
			pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m" "$path")"
597 598
			if get "$m/$path" "$pkgdest" $ext; then break; fi
		done
599 600 601
		if [ ! -f "$pkgdest" ]; then
			error 1 COULDNTDL "Couldn't download %s" "$path"
		fi
Joey Hess's avatar
Joey Hess committed
602
		donepkgs="$(($donepkgs + ${i#* }))"
603 604
		progress $donepkgs $totalpkgs DOWNPKGS "Downloading Packages files"
	done
605 606 607
}

get_package_sizes () {
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
	# mirror pkgdest debs..
	local m="$1"; shift
	local pkgdest="$1"; shift
	$PKGDETAILS PKGS "$m" "$pkgdest" "$@" | (
		newleft=""
		totaldebs=0
		countdebs=0
		while read p details; do
			if [ "$details" = "-" ]; then
				newleft="$newleft $p"
			else
				size="${details##* }";
				totaldebs="$(($totaldebs + $size))"
				countdebs="$(($countdebs + 1))"
			fi
		done
		echo "$countdebs $totaldebs$newleft"
	)
626 627 628 629
}

# note, leftovers come back on fd5 !!
download_debs () {
630 631 632 633 634 635
	local m="$1"
	local pkgdest="$2"
	shift; shift

	$PKGDETAILS PKGS "$m" "$pkgdest" "$@" | (
		leftover=""
Joey Hess's avatar
Joey Hess committed
636
		while read p ver arc mdup fil checksum size; do
637 638 639 640 641
			if [ "$ver" = "-" ]; then
				leftover="$leftover $p"
			else
				progress_next "$(($dloaddebs + $size))"
				local debdest="$($DLDEST deb "$p" "$ver" "$arc" "$m" "$fil")"
Joey Hess's avatar
Joey Hess committed
642
				if get "$m/$fil" "$TARGET/$debdest" "$checksum" "$size"; then
643 644 645
					dloaddebs="$(($dloaddebs + $size))"
					echo >>$TARGET/debootstrap/debpaths "$p $debdest"
				else
646
					warning COULDNTDL "Couldn't download package %s (ver %s arch %s)" "$p" "$ver" "$arc"
647
					leftover="$leftover $p"
648 649 650 651 652
				fi
			fi
		done
		echo >&5 ${leftover# }
	)
653 654 655
}

download_release () {
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	local m1="${MIRRORS%% *}"

	local numdebs="$#"

	local countdebs=0
	progress $countdebs $numdebs SIZEDEBS "Finding package sizes"

	local totaldebs=0
	local leftoverdebs="$*"
	for c in $COMPONENTS; do
		if [ "$countdebs" -ge "$numdebs" ]; then break; fi

		local path="dists/$SUITE/$c/binary-$ARCH/Packages"
		local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m1" "$path")"
		if [ ! -e "$pkgdest" ]; then continue; fi

		info CHECKINGSIZES "Checking component %s on %s..." "$c" "$m1"

		leftoverdebs="$(get_package_sizes "$m1" "$pkgdest" $leftoverdebs)"

		countdebs=$(($countdebs + ${leftoverdebs%% *}))
		leftoverdebs=${leftoverdebs#* }

		totaldebs=${leftoverdebs%% *}
		leftoverdebs=${leftoverdebs#* }

		progress $countdebs $numdebs SIZEDEBS "Finding package sizes"
	done

	if [ "$countdebs" -ne "$numdebs" ]; then
		error 1 LEFTOVERDEBS "Couldn't find these debs: %s" "$leftoverdebs"
	fi

	local dloaddebs=0

	progress $dloaddebs $totaldebs DOWNDEBS "Downloading packages"
	:>$TARGET/debootstrap/debpaths

	pkgs_to_get="$*"
	for c in $COMPONENTS; do
	    local path="dists/$SUITE/$c/binary-$ARCH/Packages"
	    for m in $MIRRORS; do
		local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m" "$path")"
		if [ ! -e "$pkgdest" ]; then continue; fi
		pkgs_to_get="$(download_debs "$m" "$pkgdest" $pkgs_to_get 5>&1 1>&6)"
701
		if [ -z "$pkgs_to_get" ]; then break; fi
702
	    done 6>&1
703
	    if [ -z "$pkgs_to_get" ]; then break; fi
704 705 706 707 708
	done
	progress $dloaddebs $totaldebs DOWNDEBS "Downloading packages"
	if [ "$pkgs_to_get" != "" ]; then
		error 1 COULDNTDLPKGS "Couldn't download packages: %s" "$pkgs_to_get"
	fi
709 710 711
}

download_main_indices () {
712
	local m1="${MIRRORS%% *}"
713
	local comp="${USE_COMPONENTS}"
714 715
	progress 0 100 DOWNMAINPKGS "Downloading Packages file"
	progress_next 100
716 717 718 719

	if [ -z "$comp" ]; then comp=main; fi
	COMPONENTS="$(echo $comp | tr '|' ' ')"

720 721 722 723 724
	export COMPONENTS
	for m in $MIRRORS; do
	    for c in $COMPONENTS; do
		local path="dists/$SUITE/$c/binary-$ARCH/Packages"
		local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m" "$path")"
725
		if in_path gunzip && get "$m/${path}.gz" "${pkgdest}.gz"; then
726 727 728 729 730 731 732 733
			rm -f "$pkgdest"
			gunzip "$pkgdest.gz"
		elif get "$m/$path" "$pkgdest"; then
			true
		fi
	    done
	done
	progress 100 100 DOWNMAINPKGS "Downloading Packages file"
734 735 736
}

download_main () {
737 738 739 740 741 742 743 744 745 746 747
	local m1="${MIRRORS%% *}"

	:>$TARGET/debootstrap/debpaths
	for p in "$@"; do
	    for c in $COMPONENTS; do
		local details=""
		for m in $MIRRORS; do
			local path="dists/$SUITE/$c/binary-$ARCH/Packages"
			local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m" "$path")"
			if [ ! -e "$pkgdest" ]; then continue; fi
			details="$($PKGDETAILS PKGS "$m" "$pkgdest" "$p")"
748 749 750 751
			if [ "$details" = "$p -" ]; then
				details=""
				continue
			fi
752
			size="${details##* }"; details="${details% *}"
Joey Hess's avatar
Joey Hess committed
753
			checksum="${details##* }"; details="${details% *}"
754
			local debdest="$($DLDEST deb $details)"
Joey Hess's avatar
Joey Hess committed
755
			if get "$m/${details##* }" "$TARGET/$debdest" "$checksum" "$size"; then
756 757 758 759 760 761 762 763 764 765 766 767 768
				echo >>$TARGET/debootstrap/debpaths "$p $debdest"
				details="done"
				break
			fi
		done
		if [ "$details" != "" ]; then
			break
		fi
	    done
	    if [ "$details" != "done" ]; then
		error 1 COULDNTDL "Couldn't download %s" "$p"
	    fi
	done
769 770 771 772 773
}

###################################################### deb choosing support

get_debs () {
774 775 776 777 778 779 780 781 782 783
	local field="$1"
	shift
	local m1 c
	for m1 in $MIRRORS; do
		for c in $COMPONENTS; do
			local path="dists/$SUITE/$c/binary-$ARCH/Packages"
			local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m1" "$path")"
			echo $("$PKGDETAILS" FIELD "$field" "$m1" "$pkgdest" "$@" | sed 's/ .*//')
		done
	done
784 785 786 787
}

################################################################ extraction

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
EXTRACTORS_SUPPORTED="dpkg-deb ar"

# Native dpkg-deb based extractors
extract_dpkg_deb_field () {
	local pkg="$1"
	local field="$2"

	dpkg-deb -f "$pkg" "$field"
}

extract_dpkg_deb_data () {
	local pkg="$1"

	dpkg-deb --fsys-tarfile "$pkg" | tar -xf -
}

# Raw .deb extractors
extract_ar_deb_field () {
	local pkg="$1"
	local field="$2"
808
	local tarball=$(ar -t "$pkg" | grep "^control\.tar")
809

810 811 812 813 814 815 816 817 818 819 820 821 822 823
	case "$tarball" in
		control.tar.gz) cat_cmd=zcat ;;
		control.tar.xz) cat_cmd=xzcat ;;
		control.tar) cat_cmd=cat ;;
		*) error 1 UNKNOWNCONTROLCOMP "Unknown compression type for %s in %s" "$tarball" "$pkg" ;;
	esac

	if type $cat_cmd >/dev/null 2>&1; then
		ar -p "$pkg" "$tarball" | $cat_cmd |
		    tar -O -xf - control ./control 2>/dev/null |
		    grep -i "^$field:" | sed -e 's/[^:]*: *//' | head -n 1
	else
		error 1 UNPACKCMDUNVL "Extracting %s requires the %s command, which is not available" "$pkg" "$cat_cmd"
	fi
824 825 826 827
}

extract_ar_deb_data () {
	local pkg="$1"
828
	local tarball=$(ar -t "$pkg" | grep "^data.tar")
829 830 831 832 833

	case "$tarball" in
		data.tar.gz) cat_cmd=zcat ;;
		data.tar.bz2) cat_cmd=bzcat ;;
		data.tar.xz) cat_cmd=xzcat ;;
834
		data.tar) cat_cmd=cat ;;
835 836 837 838
		*) error 1 UNKNOWNDATACOMP "Unknown compression type for %s in %s" "$tarball" "$pkg" ;;
	esac

	if type $cat_cmd >/dev/null 2>&1; then
elmig's avatar
elmig committed
839
		ar -p "$pkg" "$tarball" | $cat_cmd | tar -xf -
840
	else
841
		error 1 UNPACKCMDUNVL "Extracting %s requires the %s command, which is not available" "$pkg" "$cat_cmd"
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
	fi
}

valid_extractor () {
	local extractor="$1"

	for E in $EXTRACTORS_SUPPORTED; do
		if [ "$extractor" = "$E" ]; then
			return 0
		fi
	done

	return 1
}

choose_extractor () {
	local extractor

	if [ -n "$EXTRACTOR_OVERRIDE" ]; then
		extractor="$EXTRACTOR_OVERRIDE"
	elif type dpkg-deb >/dev/null 2>&1; then
		extractor="dpkg-deb"
	else
		extractor="ar"
	fi

	info CHOSENEXTRACTOR "Chosen extractor for .deb packages: %s" "$extractor"
	case "$extractor" in
	dpkg-deb)
		extract_deb_field () { extract_dpkg_deb_field "$@"; }
		extract_deb_data () { extract_dpkg_deb_data "$@"; }
		;;
	ar)
		extract_deb_field () { extract_ar_deb_field "$@"; }
		extract_deb_data () { extract_ar_deb_data "$@"; }
		;;
	esac
}

881
extract () { (
882
	cd "$TARGET"
883
	local p=0 cat_cmd
884 885 886 887 888
	for pkg in $(debfor "$@"); do
		p="$(($p + 1))"
		progress "$p" "$#" EXTRACTPKGS "Extracting packages"
		packagename="$(echo "$pkg" | sed 's,^.*/,,;s,_.*$,,')"
		info EXTRACTING "Extracting %s..." "$packagename"
889
		extract_deb_data "./$pkg"
890
	done
891 892 893
); }

in_target_nofail () {
894
	if ! $CHROOT_CMD "$@" 2>/dev/null; then
895 896 897
		true
	fi
	return 0
898 899 900
}

in_target_failmsg () {
901 902 903 904
	local code="$1"
	local msg="$2"
	local arg="$3"
	shift; shift; shift
905
	if ! $CHROOT_CMD "$@"; then
906 907 908 909 910 911 912 913 914 915 916 917
		warning "$code" "$msg" "$arg"
		# Try to point user at actual failing package.
		msg="See %s for details"
		if [ -e "$TARGET/debootstrap/debootstrap.log" ]; then
			arg="$TARGET/debootstrap/debootstrap.log"
			local pkg="$(grep '^dpkg: error processing ' "$TARGET/debootstrap/debootstrap.log" | head -n 1 | cut -d ' ' -f 4)"
			if [ -n "$pkg" ]; then
				msg="$msg (possibly the package $pkg is at fault)"
			fi
		else
			arg="the log"
		fi
918 919 920 921
		warning "$code" "$msg" "$arg"
		return 1
	fi
	return 0
922 923 924
}

in_target () {
925
	in_target_failmsg IN_TARGET_FAIL "Failure trying to run: %s" "$CHROOT_CMD $*" "$@"
926 927 928 929 930
}

###################################################### standard setup stuff

conditional_cp () {
931
	if [ ! -e "$2/$1" ]; then
932
		if [ -L "$1" ] && [ -e "$1" ]; then
933 934 935 936 937
			cat "$1" >"$2/$1"
		elif [ -e "$1" ]; then
			cp -a "$1" "$2/$1"
		fi
	fi
938 939 940
}

mv_invalid_to () {
941 942 943 944 945 946 947
	local m="$1"
	m="$(echo "${m#http://}" | tr '/' '_' | sed 's/_*//')"
	(cd "$TARGET/$APTSTATE/lists"
	 for a in debootstrap.invalid_*; do
		 mv "$a" "${m}_${a#*_}"
	 done
	)
948 949 950
}

setup_apt_sources () {
951 952 953 954 955 956 957 958 959 960
	mkdir -p "$TARGET/etc/apt"
	for m in "$@"; do
		local cs=""
		for c in $COMPONENTS; do
			local path="dists/$SUITE/$c/binary-$ARCH/Packages"
			local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m" "$path")"
			if [ -e "$pkgdest" ]; then cs="$cs $c"; fi
		done
		if [ "$cs" != "" ]; then echo "deb $m $SUITE$cs"; fi
	done > "$TARGET/etc/apt/sources.list"
961 962 963
}

setup_etc () {
964 965 966 967 968 969 970 971
	mkdir -p "$TARGET/etc"

	conditional_cp /etc/resolv.conf "$TARGET"
	conditional_cp /etc/hostname "$TARGET"

	if [ "$DLDEST" = apt_dest ] && [ ! -e "$TARGET/etc/apt/sources.list" ]; then
		setup_apt_sources "http://debootstrap.invalid/"
	fi
972 973
}

974 975 976
UMOUNT_DIRS=

umount_exit_function () {
977
	local realdir
978
	for dir in $UMOUNT_DIRS; do
979 980 981
		realdir="$(in_target_nofail readlink -f "$dir")"
		[ "$realdir" ] || continue
		( cd / ; umount "$TARGET/${realdir#/}" ) || true
982
	done
983 984 985
}

umount_on_exit () {
986 987 988 989 990 991
	if [ "$UMOUNT_DIRS" ]; then
		UMOUNT_DIRS="$UMOUNT_DIRS $1"
	else
		UMOUNT_DIRS="$1"
		on_exit umount_exit_function
	fi
992 993
}

994 995 996 997 998 999
clear_mtab () {
	if [ -f "$TARGET/etc/mtab" ] && [ ! -h "$TARGET/etc/mtab" ]; then
		rm -f "$TARGET/etc/mtab"
	fi
}

1000
setup_proc () {
1001
	case "$HOST_OS" in
1002
	    *freebsd*)
1003 1004 1005
		umount_on_exit /dev
		umount_on_exit /proc
		umount "$TARGET/proc" 2>/dev/null || true
1006 1007 1008 1009 1010
		if [ "$HOST_OS" = kfreebsd ]; then
			in_target mount -t linprocfs proc /proc
		else
			mount -t linprocfs proc $TARGET/proc
		fi
1011
		;;
1012
	    hurd*)
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
		;;
	    *)
		umount_on_exit /dev/pts
		umount_on_exit /dev/shm
		umount_on_exit /proc/bus/usb
		umount_on_exit /proc
		umount "$TARGET/proc" 2>/dev/null || true
		in_target mount -t proc proc /proc
		if [ -d "$TARGET/sys" ] && \
		   grep -q '[[:space:]]sysfs' /proc/filesystems 2>/dev/null; then
			umount_on_exit /sys
			umount "$TARGET/sys" 2>/dev/null || true
			in_target mount -t sysfs sysfs /sys
		fi
1027
		on_exit clear_mtab
1028 1029 1030
		;;
	esac
	umount_on_exit /lib/init/rw
1031 1032 1033
}

setup_proc_fakechroot () {
1034 1035
	rm -rf "$TARGET/proc"
	ln -s /proc "$TARGET"
1036 1037 1038
}

setup_devices () {
1039 1040 1041 1042 1043
	if doing_variant fakechroot; then
		setup_devices_fakechroot
		return 0
	fi

1044
	case "$HOST_OS" in
1045
	    kfreebsd*)
1046
		in_target mount -t devfs devfs /dev ;;
1047 1048
	    freebsd)
		mount -t devfs devfs $TARGET/dev ;;
1049
	    hurd*)
1050 1051 1052
		setup_devices_hurd ;;
	    *)
		if [ -e "$DEVICES_TARGZ" ]; then
1053
			zcat "$DEVICES_TARGZ" | (cd "$TARGET"; tar -xf -)
1054 1055 1056 1057 1058 1059 1060 1061 1062
		else
			if [ -e /dev/.devfsd ] ; then
				in_target mount -t devfs devfs /dev
			else
				error 1 NODEVTGZ "no %s. cannot create devices" "$DEVICES_TARGZ"
			fi
		fi
		;;
	esac
1063 1064
}

1065
setup_devices_hurd () {
Jeremie Koenig's avatar
Jeremie Koenig committed
1066 1067 1068 1069 1070
	# Use the setup-translators of the hurd package, and firmlink
	# $TARGET/{dev,servers} to the system ones.
	in_target /usr/lib/hurd/setup-translators -k
	settrans -a $TARGET/dev /hurd/firmlink /dev
	settrans -a $TARGET/servers /hurd/firmlink /servers
1071 1072
}

1073
setup_devices_fakechroot () {
1074 1075
	rm -rf "$TARGET/dev"
	ln -s /dev "$TARGET"
1076 1077 1078
}

setup_dselect_method () {
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
	case "$1" in
	    apt)
		mkdir -p "$TARGET/var/lib/dpkg"
		echo "apt apt" > "$TARGET/var/lib/dpkg/cmethopt"
		chmod 644 "$TARGET/var/lib/dpkg/cmethopt"
		;;
	    *)
		error 1 UNKNOWNDSELECT "unknown dselect method"
		;;
	esac
1089 1090 1091 1092
}

################################################################ pkgdetails

1093 1094 1095 1096
# NOTE
# For the debootstrap udeb, pkgdetails is provided by the bootstrap-base
# udeb, so the pkgdetails API needs to be kept in sync with that.

1097
if in_path perl; then
1098
	PKGDETAILS=pkgdetails_perl
1099

1100 1101 1102
	pkgdetails_field () {
		# uniq field mirror Packages values...
		perl -le '
1103 1104
$unique = shift @ARGV; $field = lc(shift @ARGV); $mirror = shift @ARGV;
%fields = map { $_, 0 } @ARGV;
1105
$prevpkg = "";
1106
while (<STDIN>) {
1107 1108 1109 1110
	chomp;
	next if (/^ /);
	if (/^([^:]*:)\s*(.*)$/) {
		$f = lc($1); $v = $2;
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
		if ($f eq "package:") {
			$last = 0;
			$pkg = $v;
			if ($pkg ne $prevpkg) {
				print $output if defined $output;
				if ($unique && defined $output_val) {
					delete $fields{$output_val};
					$last = 1 unless keys %fields;
				}
				$prevpkg = $pkg;
			}
			undef $output;
			undef $output_val;
			last if $last;
		}
1126 1127 1128
		$ver = $v if ($f eq "version:");
		$arc = $v if ($f eq "architecture:");
		$fil = $v if ($f eq "filename:");
1129
		$chk = $v if (lc $f eq lc($ENV{DEBOOTSTRAP_CHECKSUM_FIELD}).":");
1130 1131 1132 1133
		$siz = $v if ($f eq "size:");
		$val = $v if ($f eq $field);
	} elsif (/^$/) {
		if (defined $val && defined $fields{$val}) {
1134
			$output = sprintf "%s %s %s %s %s %s %s",
Joey Hess's avatar
Joey Hess committed
1135
			 $pkg, $ver, $arc, $mirror, $fil, $chk, $siz;
1136
			$output_val = $val;
1137 1138 1139
		}
		undef $val;
	}
1140
}
1141 1142
print $output if defined $output;
delete $fields{$output_val} if $unique && defined $output_val;
1143
for $v (keys %fields) {
1144
	printf ("%s -\n", $v) if ($unique);
1145 1146
}
' "$@"
1147 1148 1149 1150 1151
	}

	pkgdetails_perl () {
		if [ "$1" = "WGET%" ]; then
			shift;
1152 1153
			perl -e '
$v = 0;
1154
$allow_percentage = 0;
1155
while (read STDIN, $x, 1) {
1156 1157 1158
	if ($x =~ m/\s/) {
		$allow_percentage = 1;
	} elsif ($allow_percentage and $x =~ m/\d/) {
1159 1160
		$v *= 10;
		$v += $x;
1161
	} elsif ($allow_percentage and $x eq "%") {
1162 1163 1164 1165
		printf "P: %d %d%s\n", int($v / 100.0 * ($ARGV[1] - $ARGV[0]) + $ARGV[0]), $ARGV[2], ($#ARGV == 3 ? " $ARGV[3]" : "");
		$v = 0;
	} else {
		$v = 0;
1166
		$allow_percentage = 0;
1167 1168
	}
}' "$@"
1169 1170
		elif [ "$1" = "GETDEPS" ]; then
			local pkgdest="$2"; shift; shift
1171
			perl -e '
1172 1173
$prevpkg = "";
@d = ();
1174 1175
while (<STDIN>) {
	chomp;
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	if (/^Package: (.*)$/) {
		$pkg = $1;
		if ($pkg ne $prevpkg) {
			for my $d (@d) {
				print "$d\n";
			}
		}
		$prevpkg = $1;
		@d = ();
	}
	$in = 1 if (grep {$_ eq $pkg} @ARGV);
1187 1188 1189 1190 1191
	$in = 0 if (/^$/);
	if ($in and (/^Depends: (.*)$/ or /^Pre-Depends: (.*)$/)) {
		for $d (split /\s*,\s*/, $1) {
			$d =~ s/\s*[|].*$//;
			$d =~ s/\s*[(].*[)]\s*//;
1192
			push @d, $d;
1193 1194
		}
	}
1195 1196 1197
}
for my $d (@d) {
	print "$d\n";
1198
}' <"$pkgdest" "$@" | sort | uniq
1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209
		elif [ "$1" = "PKGS" ]; then
			local m="$2"
			local p="$3"
			shift; shift; shift
			pkgdetails_field 1 Package: "$m" "$@" < "$p"
		elif [ "$1" = "FIELD" ]; then
			local f="$2"
			local m="$3"
			local p="$4"
			shift; shift; shift; shift
			pkgdetails_field 0 "$f" "$m" "$@" < "$p"
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227
		elif [ "$1" = "STANZAS" ]; then
			local pkgdest="$2"; shift; shift
			perl -e '
my $accum = "";
while (<STDIN>) {
	$accum .= $_;
	$in = 1 if (/^Package: (.*)$/ && grep {$_ eq $1} @ARGV);
	if ($in and /^$/) {
		print $accum;
		if (substr($accum, -1) != "\n") {
			print "\n\n";
		} elsif (substr($accum, -2, 1) != "\n") {
			print "\n";
		}
		$in = 0;
	}
	$accum = "" if /^$/;
}' <"$pkgdest" "$@"
1228 1229
		fi
	}
1230 1231
elif [ -e "/usr/lib/debootstrap/pkgdetails" ]; then
	PKGDETAILS="/usr/lib/debootstrap/pkgdetails"
1232
elif [ -e "$DEBOOTSTRAP_DIR/pkgdetails" ]; then
1233
	PKGDETAILS="$DEBOOTSTRAP_DIR/pkgdetails"
1234
else
1235
	PKGDETAILS=""
1236 1237 1238 1239 1240
fi

##################################################### dependency resolution

resolve_deps () {
1241 1242 1243 1244 1245 1246
	local m1="${MIRRORS%% *}"

	local PKGS="$*"
	local ALLPKGS="$PKGS";
	local ALLPKGS2="";
	while [ "$PKGS" != "" ]; do
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
		local NEWPKGS=""
		for c in $COMPONENTS; do
			local path="dists/$SUITE/$c/binary-$ARCH/Packages"
			local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m1" "$path")"
			NEWPKGS="$NEWPKGS $("$PKGDETAILS" GETDEPS "$pkgdest" $PKGS)"
		done
		PKGS=$(echo "$PKGS $NEWPKGS" | tr ' ' '\n' | sort | uniq)
		local REALPKGS=""
		for c in $COMPONENTS; do
			local path="dists/$SUITE/$c/binary-$ARCH/Packages"
			local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m1" "$path")"
			REALPKGS="$REALPKGS $("$PKGDETAILS" PKGS REAL "$pkgdest" $PKGS | sed -n 's/ .*REAL.*$//p')"
		done
		PKGS="$REALPKGS"
1261 1262 1263 1264 1265
		ALLPKGS2=$(echo "$PKGS $ALLPKGS" | tr ' ' '\n' | sort | uniq)
		PKGS=$(without "$ALLPKGS2" "$ALLPKGS")
		ALLPKGS="$ALLPKGS2"
	done
	echo $ALLPKGS
1266 1267
}

1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279
setup_available () {
	local m1="${MIRRORS%% *}"

	for c in $COMPONENTS; do
		local path="dists/$SUITE/$c/binary-$ARCH/Packages"
		local pkgdest="$TARGET/$($DLDEST pkg "$SUITE" "$c" "$ARCH" "$m1" "$path")"
		# XXX: What if a package is in more than one component?
		# -- cjwatson 2009-07-29
		"$PKGDETAILS" STANZAS "$pkgdest" "$@"
	done >"$TARGET/var/lib/dpkg/available"

	for pkg; do
1280 1281
		echo "$pkg install"
	done | in_target dpkg --set-selections
1282 1283 1284
}

get_next_predep () {
1285
	local stanza="$(in_target_nofail dpkg --predep-package)"
1286 1287 1288 1289
	[ "$stanza" ] || return 1
	echo "$stanza" | grep '^Package:' | sed 's/^Package://; s/^ *//'
}

1290 1291
################################################################### helpers

1292 1293 1294 1295
# Return zero if it is possible to create devices and execute programs in
# this directory. (Both may be forbidden by mount options, e.g. nodev and
# noexec respectively.)
check_sane_mount () {
1296 1297
	mkdir -p "$1"

1298
	case "$HOST_OS" in
1299
	    *freebsd*|hurd*)
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
		;;
	    *)
		mknod "$1/test-dev-null" c 1 3 || return 1
		if ! echo test > "$1/test-dev-null"; then
			rm -f "$1/test-dev-null"
			return 1
		fi
		rm -f "$1/test-dev-null"
		;;
	esac

1311 1312 1313
	SH=/bin/sh
	[ -x $SH ] || SH=`which sh`

1314
	cat > "$1/test-exec" <<EOF
1315
#! $SH
1316 1317
:
EOF
1318 1319 1320 1321 1322 1323 1324 1325
	chmod +x "$1/test-exec"
	if ! "$1/test-exec"; then
		rm -f "$1/test-exec"
		return 1
	fi
	rm -f "$1/test-exec"

	return 0
1326 1327
}

1328
read_gpg_status () {
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348
	badsig=
	unkkey=
	validsig=
	while read prefix keyword keyid rest; do
		[ "$prefix" = '[GNUPG:]' ] || continue
		case $keyword in
		    BADSIG)	badsig="$keyid" ;;
		    NO_PUBKEY)	unkkey="$keyid" ;;
		    VALIDSIG)	validsig="$keyid" ;;
		esac
	done
	if [ "$validsig" ]; then
		info VALIDRELSIG "Valid Release signature (key id %s)" "$validsig"
	elif [ "$badsig" ]; then
		error 1 BADRELSIG "Invalid Release signature (key id %s)" "$badsig"
	elif [ "$unkkey" ]; then
		error 1 UNKNOWNRELSIG "Release signed by unknown key (key id %s)" "$unkkey"
	else
		error 1 SIGCHECK "Error executing gpgv to check Release signature"
	fi
1349 1350 1351
}

without () {
1352 1353 1354 1355
	# usage:  without "a b c" "a d" -> "b" "c"
	(echo $1 | tr ' ' '\n' | sort | uniq;
	 echo $2 $2 | tr ' ' '\n') | sort | uniq -u | tr '\n' ' '
	echo
1356 1357
}

1358 1359
# Formerly called 'repeat', but that's a reserved word in zsh.
repeatn () {
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
	local n="$1"
	shift
	while [ "$n" -gt 0 ]; do
		if "$@"; then
			break
		else
			n="$(( $n - 1 ))"
			sleep 1
		fi
	done
	if [ "$n" -eq 0 ]; then return 1; fi
	return 0
1372 1373 1374 1375
}

N_EXIT_THINGS=0
exit_function () {
1376 1377 1378 1379 1380 1381
	local n=0
	while [ "$n" -lt "$N_EXIT_THINGS" ]; do
		(eval $(eval echo \${EXIT_THING_$n}) 2>/dev/null || true)
		n="$(( $n + 1 ))"
	done
	N_EXIT_THINGS=0
1382 1383 1384 1385 1386 1387 1388 1389 1390
}

trap "exit_function" 0
trap "exit 129" 1
trap "error 130 INTERRUPTED \"Interrupt caught ... exiting\"" 2
trap "exit 131" 3
trap "exit 143" 15

on_exit () {
1391 1392
	eval `echo EXIT_THING_${N_EXIT_THINGS}=\"$1\"`
	N_EXIT_THINGS="$(( $N_EXIT_THINGS + 1 ))"
1393 1394 1395 1396 1397
}

############################################################## fakechroot tools

install_fakechroot_tools () {
1398 1399 1400 1401
	if [ "$VARIANT" = "fakechroot" ]; then
		export PATH=/usr/sbin:/sbin:$PATH
	fi

1402 1403
	mv "$TARGET/sbin/ldconfig" "$TARGET/sbin/ldconfig.REAL"
	echo \
1404 1405 1406
"#!/bin/sh
echo
echo \"Warning: Fake ldconfig called, doing nothing\"" > "$TARGET/sbin/ldconfig"
1407
	chmod 755 "$TARGET/sbin/ldconfig"
1408

1409
	echo \
1410 1411 1412 1413
"/sbin/ldconfig
/sbin/ldconfig.REAL
fakechroot" >> "$TARGET/var/lib/dpkg/diversions"

1414 1415
	mv "$TARGET/usr/bin/ldd" "$TARGET/usr/bin/ldd.REAL"
	cat << 'END' > "$TARGET/usr/bin/ldd"
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435
#!/usr/bin/perl

# fakeldd
#
# Replacement for ldd with usage of objdump
#
# (c) 2003-2005 Piotr Roszatycki <dexter@debian.org>, BSD


my %libs = ();

my $status = 0;
my $dynamic = 0;
my $biarch = 0;

my $ldlinuxsodir = "/lib";
my @ld_library_path = qw(/usr/lib /lib);


sub ldso($) {
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456
	my ($lib) = @_;
	my @files = ();

	if ($lib =~ /^\//) {
	    $libs{$lib} = $lib;
	    push @files, $lib;
	} else {
	    foreach my $ld_path (@ld_library_path) {
		next unless -f "$ld_path/$lib";
		my $badformat = 0;
		open OBJDUMP, "objdump -p $ld_path/$lib 2>/dev/null |";
	 	while (my $line = <OBJDUMP>) {
		    if ($line =~ /file format (\S*)$/) {
				$badformat = 1 unless $format eq $1;
				last;
		    }
		}
		close OBJDUMP;
		next if $badformat;
		$libs{$lib} = "$ld_path/$lib";
		push @files, "$ld_path/$lib";
1457
	    }
1458
	    objdump(@files);
1459 1460 1461 1462 1463
	}
}


sub objdump(@) {
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484
	my (@files) = @_;
	my @libs = ();

	foreach my $file (@files) {
	    open OBJDUMP, "objdump -p $file 2>/dev/null |";
	    while (my $line = <OBJDUMP>) {
		$line =~ s/^\s+//;
		my @f = split (/\s+/, $line);
		if ($line =~ /file format (\S*)$/) {
		    if (not $format) {
			$format = $1;
			if ($unamearch eq "x86_64" and $format eq "elf32-i386") {
			    my $link = readlink "/lib/ld-linux.so.2";
			    if ($link =~ /^\/emul\/ia32-linux\//) {
				$ld_library_path[-2] = "/emul/ia32-linux/usr/lib";
				$ld_library_path[-1] = "/emul/ia32-linux/lib";
			    }
			} elsif ($unamearch =~ /^(sparc|sparc64)$/ and $format eq "elf64-sparc") {
			    $ldlinuxsodir = "/lib64";
			    $ld_library_path[-2] = "/usr/lib64";
			    $ld_library_path[-1] = "/lib64";
1485
			}
1486 1487
		    } else {
			next unless $format eq $1;
1488
		    }
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
		}
		if (not $dynamic and $f[0] eq "Dynamic") {
		    $dynamic = 1;
		}
		next unless $f[0] eq "NEEDED";
		if ($f[1] =~ /^ld-linux(\.|-)/) {
		    $f[1] = "$ldlinuxsodir/" . $f[1];
		}
		if (not defined $libs{$f[1]}) {
		    $libs{$f[1]} = undef;
		    push @libs, $f[1];
		}
1501
	    }
1502
	    close OBJDUMP;
1503 1504
	}

1505 1506 1507
	foreach my $lib (@libs) {
	    ldso($lib);
	}
1508 1509 1510 1511
}


if ($#ARGV < 0) {
1512 1513
	print STDERR "fakeldd: missing file arguments\n";
	exit 1;
1514 1515 1516
}

while ($ARGV[0] =~ /^-/) {
1517 1518 1519
	my $arg = $ARGV[0];
	shift @ARGV;
	last if $arg eq "--";
1520 1521 1522 1523
}

open LD_SO_CONF, "/etc/ld.so.conf";
while ($line = <LD_SO_CONF>) {
1524 1525
	chomp $line;
	unshift @ld_library_path, $line;
1526 1527 1528 1529 1530 1531 1532 1533 1534
}
close LD_SO_CONF;

unshift @ld_library_path, split(/:/, $ENV{LD_LIBRARY_PATH});

$unamearch = `/bin/uname -m`;
chomp $unamearch;

foreach my $file (@ARGV) {
1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559
	my $address;
	%libs = ();
	$dynamic = 0;

	if ($#ARGV > 0) {
		print "$file:\n";
	}

	if (not -f $file) {
		print STDERR "ldd: $file: No such file or directory\n";
		$status = 1;
		next;
	}

	objdump($file);

	if ($dynamic == 0) {
		print "\tnot a dynamic executable\n";
		$status = 1;
	} elsif (scalar %libs eq "0") {
		print "\tstatically linked\n";
	}

	if ($format =~ /^elf64-/) {
		$address = "0x0000000000000000";
1560
	} else {
1561 1562 1563 1564 1565 1566 1567 1568 1569
		$address = "0x00000000";
	}

	foreach $lib (keys %libs) {
		if ($libs{$lib}) {
			printf "\t%s => %s (%s)\n", $lib, $libs{$lib}, $address;
		} else {
			printf "\t%s => not found\n", $lib;
		}
1570 1571 1572 1573 1574
	}
}

exit $status;
END
1575
	chmod 755 "$TARGET/usr/bin/ldd"
1576

1577
	echo \
1578 1579 1580 1581 1582
"/usr/bin/ldd
/usr/bin/ldd.REAL
fakechroot" >> "$TARGET/var/lib/dpkg/diversions"

}