-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions
1883 lines (1690 loc) · 46.6 KB
/
functions
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
############################################################### smallutils
smallyes() {
YES="${1-y}"
while echo "$YES" 2>/dev/null ; do : ; done
}
in_path () {
local OLD_IFS="$IFS"
IFS=":"
for dir in $PATH; do
if [ -x "$dir/$1" ]; then
IFS="$OLD_IFS"
return 0
fi
done
IFS="$OLD_IFS"
return 1
}
############################################################### interaction
error () {
# <error code> <name> <string> <args>
local err name fmt
err="$1"
name="$2"
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"
}
warning () {
# <name> <string> <args>
local name fmt
name="$1"
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
}
info () {
# <name> <string> <args>
local name fmt
name="$1"
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
}
PROGRESS_NOW=0
PROGRESS_END=0
PROGRESS_NEXT=""
PROGRESS_WHAT=""
progress_next () {
PROGRESS_NEXT="$1"
}
wgetprogress () {
[ ! "$VERBOSE" ] && NVSWITCH="-nv"
local ret=0
if [ "$USE_DEBIANINSTALLER_INTERACTION" ] && [ "$PROGRESS_NEXT" ]; then
# The exit status of a pipeline is that of the last command in
# the pipeline, so wget's exit status must be saved in the
# pipeline's first command. Since commands in a pipeline run in
# subshells, we have to print the exit status (on a file
# descriptor other than standard output, which is used by the
# pipeline itself) and then assign it to $ret outside of the
# pipeline. The "||" is necessary due to "set -e"; otherwise, a
# non-zero exit status would cause the echo command to be
# skipped. If wget succeeds, $ret will be "", so it then has to
# be set to a default value of 0.
ret=$({ { wget $@ 2>&1 >/dev/null || echo $? >&2; } | "$PKGDETAILS" "WGET%" "$PROGRESS_NOW" "$PROGRESS_NEXT" "$PROGRESS_END" >&3; } 2>&1)
: ${ret:=0}
else
wget ${NVSWITCH:+"$NVSWITCH"} "$@"
ret=$?
fi
return $ret
}
progress () {
# <now> <end> <name> <string> <args>
local now end name fmt
now="$1"
end="$2"
name="$3"
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
}
dpkg_progress () {
# <now> <end> <name> <desc> UNPACKING|CONFIGURING
local now end name desc action expect
now="$1"
end="$2"
name="$3"
desc="$4"
action="$5"
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"
}
############################################################# set variables
default_mirror () {
DEF_MIRROR="$1"
}
FINDDEBS_NEEDS_INDICES="false"
finddebs_style () {
case "$1" in
hardcoded)
;;
from-indices)
FINDDEBS_NEEDS_INDICES="true"
;;
*)
error 1 BADFINDDEBS "unknown finddebs style"
;;
esac
}
mk_download_dirs () {
if [ "$DLDEST" = "apt_dest" ]; then
mkdir -p "$TARGET/$APTSTATE/lists/partial"
mkdir -p "$TARGET/var/cache/apt/archives/partial"
fi
}
download_style () {
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
}
keyring () {
# avoid unnecessary warning with --second-stage
if [ -z "$KEYRING" ] && [ "$SECOND_STAGE_ONLY" != true ]; then
if [ -e "$1" ]; then
KEYRING="$1"
elif [ -z "$DISABLE_KEYRING" ]; then
if [ -n "$DEF_HTTPS_MIRROR" ] && [ -z "$USER_MIRROR" ] && [ -z "$FORCE_KEYRING" ]; then
info KEYRING "Keyring file not available at %s; switching to https mirror %s" "$1" "$DEF_HTTPS_MIRROR"
USER_MIRROR="$DEF_HTTPS_MIRROR"
else
warning KEYRING "Cannot check Release signature; keyring file not available %s" "$1"
if [ -n "$FORCE_KEYRING" ]; then
error 1 KEYRING "Keyring-based check was requested; aborting accordingly"
fi
fi
fi
fi
}
detect_container () {
if [ "$container" = lxc ]; then
CONTAINER="lxc"
elif grep -qs container=lxc-libvirt /proc/1/environ; then
CONTAINER="lxc-libvirt"
elif grep -qs ^systemd-nspawn$ /run/systemd/container || grep -qs systemd-nspawn /proc/1/environ || [ "$container" = "systemd-nspawn" ]; then
CONTAINER="systemd-nspawn"
elif grep -qs '[[:space:]]/docker/.*/sys/fs/cgroup' /proc/1/mountinfo; then
CONTAINER="docker"
else
CONTAINER=""
fi
}
########################################################## variant handling
doing_variant () {
if [ "$1" = "$VARIANT" ]; then return 0; fi
if [ "$1" = "-" ] && [ "$VARIANT" = "" ]; then return 0; fi
return 1
}
SUPPORTED_VARIANTS="-"
variants () {
SUPPORTED_VARIANTS="$*"
for v in $*; do
if doing_variant "$v"; then return 0; fi
done
error 1 UNSUPPVARIANT "unsupported variant"
}
########################################################### option handling
check_conflicting_option () {
if ( [ "$set_what_to_do" = --foreign ] && [ "${1%%=*}" = --unpack-tarball ] ) || \
( [ "${set_what_to_do%%=*}" = "--unpack-tarball" ] && [ "$1" = --foreign ] ); then
LOOSEN_CONFLICTING_RESTRICTION="true"
elif [ -n "$set_what_to_do" ]; then
error 1 ARG_CONFLICTING "$set_what_to_do is specified with $1, please use only one of those options."
fi
set_what_to_do="$1"
}
################################################# work out names for things
mirror_style () {
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
}
force_md5 () {
DEBOOTSTRAP_CHECKSUM_FIELD=MD5SUM
export DEBOOTSTRAP_CHECKSUM_FIELD
}
verify_checksum () {
# args: dest checksum size
local expchecksum="$2"
local expsize="$3"
if [ "$DEBOOTSTRAP_CHECKSUM_FIELD" = "MD5SUM" ]; then
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
else
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
fi
relsize="$(wc -c < "$1")"
if [ "$expsize" -ne "$relsize" ] || [ "$expchecksum" != "$relchecksum" ]; then
return 1
fi
return 0
}
get () {
# args: from dest 'nocache'
# args: from dest [checksum size] [alt {checksum size type}]
# args: from dest 'byhash' [checksum size] [alt {checksum size type}]
local displayname
local versionname
local from_base
local dest_base
local nocache=""
local byhash=""
from_base="$1"; shift
dest_base="$1"; shift
if [ "$1" = "nocache" ]; then
nocache="true"; shift
elif [ "$1" = "byhash" ]; then
byhash="true"; shift
fi
if [ "${dest_base%.deb}" != "$dest_base" ]; then
displayname="$(echo "$dest_base" | sed 's,^.*/,,;s,_.*$,,')"
versionname="$(echo "$dest_base" | sed 's,^.*/,,' | cut -d_ -f2 | sed 's/%3a/:/')"
else
displayname="$(echo "$from_base" | sed 's,^.*/,,')"
fi
if [ -e "$dest_base" ]; then
if [ -z "$1" ]; then
return 0
elif [ -n "$nocache" ]; then
rm -f "$dest_base"
else
info VALIDATING "Validating %s %s" "$displayname" "$versionname"
if verify_checksum "$dest_base" "$1" "$2"; then
return 0
else
rm -f "$dest_base"
fi
fi
fi
if [ "$#" -gt 3 ]; then
local st=1
if [ "$3" = "-" ]; then st=4; 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=1
fi
for a in $order; do
local checksum siz typ from dest iters
checksum="$(eval echo \${$a})"
siz="$(eval echo \${$(( $a+1 ))})"
typ="$(eval echo \${$(( $a+2 ))})"
iters="0"
case "$typ" in
xz) from="$from_base.xz"; dest="$dest_base.xz" ;;
bz2) from="$from_base.bz2"; dest="$dest_base.bz2" ;;
gz) from="$from_base.gz"; dest="$dest_base.gz" ;;
*) from="$from_base"; dest="$dest_base" ;;
esac
if [ ! -z "$CACHE_DIR" ]; then
dest="${dest%%*/}"
elif [ "${dest#/}" = "$dest" ]; then
dest="./$dest"
fi
local dest2="$dest"
if [ -d "${dest2%/*}/partial" ]; then
dest2="${dest2%/*}/partial/${dest2##*/}"
fi
while [ "$iters" -lt 10 ]; do
local from2=""
info RETRIEVING "Retrieving %s %s" "$displayname" "$versionname"
if [ "$checksum" != "" ] && [ "$byhash" != "" ]; then
# assume we don't mix acquire-by-hash and md5
from2="$(dirname "$from")/by-hash/SHA${SHA_SIZE}/$checksum"
fi
if [ ! -e "$dest2" ]; then
if [ -z "$from2" ] || ! just_get "$from2" "$dest2"; then
if ! just_get "$from" "$dest2"; then continue 2; fi
fi
fi
if [ "$checksum" != "" ]; then
info VALIDATING "Validating %s %s" "$displayname" "$versionname"
if verify_checksum "$dest2" "$checksum" "$siz"; then
checksum=""
fi
fi
if [ -z "$checksum" ]; then
[ "$dest2" = "$dest" ] || mv "$dest2" "$dest"
case "$typ" in
gz) gunzip "$dest" ;;
bz2) bunzip2 "$dest" ;;
xz) unxz "$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"
done
return 1
}
just_get () {
# 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#https://}" != "$from" ] || [ "${from#ftp://}" != "$from" ]; then
# http/https/ftp mirror
if wgetprogress ${CHECKCERTIF:+"$CHECKCERTIF"} ${CERTIFICATE:+"$CERTIFICATE"} ${PRIVATEKEY:+"$PRIVATEKEY"} -O "$dest" "$from"; then
return 0
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
}
download () {
mk_download_dirs
"$DOWNLOAD_DEBS" "$(echo "$@" | tr ' ' '\n' | sort)"
}
download_indices () {
mk_download_dirs
"$DOWNLOAD_INDICES" "$(echo "$@" | tr ' ' '\n' | sort)"
}
debfor () {
(while read pkg path; do
for p in "$@"; do
[ "$p" = "$pkg" ] || continue;
echo "$path"
done
done <"$TARGET/debootstrap/debpaths"
)
}
apt_dest () {
# args:
# deb package version arch mirror path
# pkg suite component arch mirror path
# rel suite mirror path
case "$1" in
deb)
echo "/var/cache/apt/archives/${2}_${3}_${4}.deb" | sed 's/:/%3a/'
;;
pkg)
local m="$5"
printf "%s" "$APTSTATE/lists/"
echo "${m}_$6" | sed -e 's,^[^:]\+://,,' -e 's/\//_/g'
;;
rel)
local m="$3"
printf "%s" "$APTSTATE/lists/"
echo "${m}_$4" | sed -e 's,^[^:]\+://,,' -e 's/\//_/g'
;;
esac
}
################################################################## download
get_release_checksum () {
local reldest path
reldest="$1"
path="$2"
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" | \
while read a b c; do
if [ "$c" = "$path" ]; then echo "$a $b"; fi
done | head -n 1
}
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
if [ -z "$COMPONENTS" ]; then
mv "$reldest" "$reldest.malformed"
error 1 INVALIDREL "Invalid Release file, no valid components"
fi
}
CODENAME=""
validate_suite () {
local reldest suite
reldest="$1"
CODENAME=$(sed -n "s/^Codename: *//p" "$reldest")
suite=$(sed -n "s/^Suite: *//p" "$reldest")
for s in $SUITE $EXTRA_SUITES; do
if [ "$s" = "$suite" ] || [ "$s" = "$CODENAME" ]; then
return 0
fi
done
if [ "$EXTRA_SUITES" = "" ]; then
error 1 WRONGSUITE "Asked to install suite %s, but got %s (codename: %s) from mirror" "$SUITE" "$suite" "$CODENAME"
else
error 1 WRONGSUITE "Asked to install suites %s %s, but got %s (codename: %s) from mirror" "$SUITE" "$EXTRA_SUITES" "$suite" "$CODENAME"
fi
}
split_inline_sig () {
local inreldest reldest relsigdest
inreldest="$1"
reldest="$2"
relsigdest="$3"
# Note: InRelease files are fun since one needs to remove the
# last newline from the PGP SIGNED MESSAGE part, while keeping
# the PGP SIGNATURE part intact. This shell implementation
# should work on most if not all systems, instead of trying to
# sed/tr/head, etc.
rm -f "$reldest" "$relsigdest"
nl=""
state="pre-begin"
while IFS= read -r line; do
case "${state}" in
pre-begin)
if [ "x${line}" = "x-----BEGIN PGP SIGNED MESSAGE-----" ]; then
state="begin"
fi
;;
begin)
if [ "x${line}" = "x" ]; then
state="data"
fi
;;
data)
if [ "x${line}" = "x-----BEGIN PGP SIGNATURE-----" ]; then
printf "%s\n" "${line}" > "$relsigdest"
state="signature"
else
printf "${nl}%s" "${line}" >> "$reldest"
nl="\n"
fi
;;
signature)
printf "%s\n" "${line}" >> "$relsigdest"
if [ "x${line}" = "x-----END PGP SIGNATURE-----" ]; then
break
fi
esac
done < "$inreldest"
}
download_release_sig () {
local m1 suite inreldest reldest relsigdest
m1="$1"
suite="$2"
inreldest="$3"
reldest="$4"
relsigdest="$5"
progress 0 100 DOWNREL "Downloading Release file"
progress_next 100
if get "$m1/dists/$suite/InRelease" "$inreldest" nocache; then
split_inline_sig "$inreldest" "$reldest" "$relsigdest"
progress 100 100 DOWNREL "Downloading Release file"
else
get "$m1/dists/$suite/Release" "$reldest" nocache ||
error 1 NOGETREL "Failed getting release file %s" "$m1/dists/$suite/Release"
progress 100 100 DOWNREL "Downloading Release file"
fi
if [ -n "$KEYRING" ] && [ -z "$DISABLE_KEYRING" ]; then
progress 0 100 DOWNRELSIG "Downloading Release file signature"
if ! [ -f "$relsigdest" ]; then
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"
fi
info RELEASESIG "Checking Release signature"
# Don't worry about the exit status from gpgv; parsing the output will
# take care of that.
(gpgv --status-fd 1 --keyring "$KEYRING" --ignore-time-conflict \
"$relsigdest" "$reldest" || true) | read_gpg_status
progress 100 100 DOWNRELSIG "Downloading Release file signature"
fi
}
download_release_indices () {
local m1 inreldest reldest relsigdest totalpkgs \
subpath xzi bz2i gzi normi i ext \
donepkgs pkgdest acquirebyhash
m1="${MIRRORS%% *}"
for s in $SUITE $EXTRA_SUITES; do
inreldest="$TARGET/$($DLDEST rel "$s" "$m1" "dists/$s/InRelease")"
reldest="$TARGET/$($DLDEST rel "$s" "$m1" "dists/$s/Release")"
relsigdest="$TARGET/$($DLDEST rel "$s" "$m1" "dists/$s/Release.gpg")"
download_release_sig "$m1" "$s" "$inreldest" "$reldest" "$relsigdest"
validate_suite "$reldest"
extract_release_components "$reldest"
acquirebyhash=$(grep "^Acquire-By-Hash: yes$" "$reldest" || true)
totalpkgs=0
for c in $COMPONENTS; do
subpath="$c/binary-$ARCH/Packages"
xzi="$(get_release_checksum "$reldest" "$subpath.xz")"
bz2i="$(get_release_checksum "$reldest" "$subpath.bz2")"
gzi="$(get_release_checksum "$reldest" "$subpath.gz")"
normi="$(get_release_checksum "$reldest" "$subpath")"
if [ "$normi" != "" ]; then
i="$normi"
elif in_path bunzip2 && [ "$bz2i" != "" ]; then
i="$bz2i"
elif in_path unxz && [ "$xzi" != "" ]; then
i="$xzi"
elif in_path gunzip && [ "$gzi" != "" ]; then
i="$gzi"
fi
if [ "$i" != "" ]; then
totalpkgs=$(( $totalpkgs + ${i#* } ))
else
mv "$reldest" "$reldest.malformed"
error 1 MISSINGRELENTRY "Invalid Release file, no entry for %s" "$subpath"
fi
done
donepkgs=0
progress 0 $totalpkgs DOWNPKGS "Downloading Packages files"
for c in $COMPONENTS; do
subpath="$c/binary-$ARCH/Packages"
path="dists/$s/$subpath"
xzi="$(get_release_checksum "$reldest" "$subpath.xz")"
bz2i="$(get_release_checksum "$reldest" "$subpath.bz2")"
gzi="$(get_release_checksum "$reldest" "$subpath.gz")"
normi="$(get_release_checksum "$reldest" "$subpath")"
ext=""
if [ "$acquirebyhash" != "" ]; then
ext="$ext byhash"
fi
if [ "$normi" != "" ]; then
ext="$ext $normi ."
i="$normi"
fi
if in_path unxz && [ "$xzi" != "" ]; then
ext="$ext $xzi xz"
i="${i:-$xzi}"
fi
if in_path bunzip2 && [ "$bz2i" != "" ]; then
ext="$ext $bz2i bz2"
i="${i:-$bz2i}"
fi
if in_path gunzip && [ "$gzi" != "" ]; then
ext="$ext $gzi gz"
i="${i:-$gzi}"
fi
progress_next $(($donepkgs + ${i#* }))
for m in $MIRRORS; do
pkgdest="$TARGET/$($DLDEST pkg "$s" "$c" "$ARCH" "$m" "$path")"
if get "$m/$path" "$pkgdest" $ext; then break; fi
done
if [ ! -f "$pkgdest" ]; then
error 1 COULDNTDL "Couldn't download %s" "$m/$path"
fi
donepkgs=$(($donepkgs + ${i#* }))
progress $donepkgs $totalpkgs DOWNPKGS "Downloading Packages files"
done
done
}
get_package_sizes () {
# mirror pkgdest debs..
local m pkgdest
m="$1"; shift
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"
)
}
# note, leftovers come back on fd5 !!
download_debs () {
local m pkgdest debdest debcache
m="$1"
pkgdest="$2"
shift; shift
"$PKGDETAILS" PKGS "$m" "$pkgdest" "$@" | (
leftover=""
while read p ver arc mdup fil checksum size; do
if [ "$ver" = "-" ]; then
leftover="$leftover $p"
else
progress_next $(($dloaddebs + $size))
debdest="$($DLDEST deb "$p" "$ver" "$arc" "$m" "$fil")"
debcache="$(echo "$p"_"$ver"_"$arc".deb | sed 's/:/%3a/')"
if [ -z "$CACHE_DIR" ] && get "$m/$fil" "$TARGET/$debdest" "$checksum" "$size"; then
dloaddebs=$(($dloaddebs + $size))
echo >>"$TARGET/debootstrap/deburis" "$p $ver $m/$fil"
echo >>"$TARGET/debootstrap/debpaths" "$p $debdest"
elif [ -d "$CACHE_DIR" ] && get "$m/$fil" "$CACHE_DIR/$debcache" "$checksum" "$size"; then
dloaddebs=$(($dloaddebs + $size))
echo >>"$TARGET/debootstrap/deburis" "$p $ver $m/$fil"
echo >>"$TARGET/debootstrap/debpaths" "$p $debdest"
cp "$CACHE_DIR/$debcache" "$TARGET/$debdest"
else
warning COULDNTDL "Couldn't download package %s (ver %s arch %s) at %s" "$p" "$ver" "$arc" "$m/$fil"
leftover="$leftover $p"
fi
fi
done
echo >&5 ${leftover# }
)
}
download_release () {
local m1 numdebs countdebs totaldebs leftoverdebs path pkgdest dloaddebs
m1="${MIRRORS%% *}"
numdebs="$#"
countdebs=0
progress "$countdebs" "$numdebs" SIZEDEBS "Finding package sizes"
totaldebs=0
leftoverdebs="$*"
# Fix possible duplicate package names, which would screw up counts:
leftoverdebs=$(printf "$leftoverdebs"|tr ' ' '\n'|sort -u|tr '\n' ' ')
numdebs=$(printf "$leftoverdebs"|wc -w)
for s in $SUITE $EXTRA_SUITES; do
for c in $COMPONENTS; do
if [ "$countdebs" -ge "$numdebs" ]; then break; fi
path="dists/$s/$c/binary-$ARCH/Packages"
pkgdest="$TARGET/$($DLDEST pkg "$s" "$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
done
if [ "$countdebs" -ne "$numdebs" ]; then
error 1 LEFTOVERDEBS "Couldn't find these debs: %s" "$leftoverdebs"
fi
dloaddebs=0
progress "$dloaddebs" "$totaldebs" DOWNDEBS "Downloading packages"
:>"$TARGET/debootstrap/debpaths"
pkgs_to_get="$*"
for s in $SUITE $EXTRA_SUITES; do
for c in $COMPONENTS; do
path="dists/$s/$c/binary-$ARCH/Packages"
for m in $MIRRORS; do
pkgdest="$TARGET/$($DLDEST pkg "$s" "$c" "$ARCH" "$m" "$path")"
if [ ! -e "$pkgdest" ]; then continue; fi
pkgs_to_get="$(download_debs "$m" "$pkgdest" $pkgs_to_get 5>&1 1>&6)"
if [ -z "$pkgs_to_get" ]; then break; fi
done 6>&1
if [ -z "$pkgs_to_get" ]; then break; fi
done
if [ -z "$pkgs_to_get" ]; then break; fi
done
progress "$dloaddebs" "$totaldebs" DOWNDEBS "Downloading packages"
if [ "$pkgs_to_get" != "" ]; then
error 1 COULDNTDLPKGS "Couldn't download packages: %s" "$pkgs_to_get"
fi
}
download_main_indices () {
local m1 comp path pkgdest
m1="${MIRRORS%% *}"
comp="${USE_COMPONENTS}"
progress 0 100 DOWNMAINPKGS "Downloading Packages file"
progress_next 100
if [ -z "$comp" ]; then comp=main; fi
COMPONENTS="$(echo $comp | tr '|' ' ')"
export COMPONENTS
for m in $MIRRORS; do
for s in $SUITE $EXTRA_SUITES; do
for c in $COMPONENTS; do
path="dists/$s/$c/binary-$ARCH/Packages"
pkgdest="$TARGET/$($DLDEST pkg "$s" "$c" "$ARCH" "$m" "$path")"
if in_path gunzip && get "$m/${path}.gz" "${pkgdest}.gz"; then
rm -f "$pkgdest"
gunzip "$pkgdest.gz"
elif get "$m/$path" "$pkgdest"; then
true
fi
done
done
done
progress 100 100 DOWNMAINPKGS "Downloading Packages file"
}
download_main () {
local m1 path pkgdest debdest
m1="${MIRRORS%% *}"
:>"$TARGET/debootstrap/debpaths"
for p in "$@"; do
for s in $SUITE $EXTRA_SUITES; do
for c in $COMPONENTS; do
local details=""
for m in $MIRRORS; do
path="dists/$s/$c/binary-$ARCH/Packages"
pkgdest="$TARGET/$($DLDEST pkg "$s" "$c" "$ARCH" "$m" "$path")"
if [ ! -e "$pkgdest" ]; then continue; fi
details="$($PKGDETAILS PKGS "$m" "$pkgdest" "$p")"
if [ "$details" = "$p -" ]; then
details=""
continue
fi
size="${details##* }"; details="${details% *}"
checksum="${details##* }"; details="${details% *}"
debdest="$($DLDEST deb $details)"
if get "$m/${details##* }" "$TARGET/$debdest" "$checksum" "$size"; then
echo >>"$TARGET/debootstrap/debpaths" "$p $debdest"
details="done"
break
fi
done
if [ "$details" != "" ]; then
break
fi
done
if [ "$details" != "" ]; then
break
fi
done
if [ "$details" != "done" ]; then
error 1 COULDNTDL "Couldn't download %s" "$p"
fi
done
}
###################################################### deb choosing support
get_debs () {
local field m1 c path pkgdest
field="$1"
shift
for m1 in $MIRRORS; do
for s in $SUITE $EXTRA_SUITES; do
for c in $COMPONENTS; do
path="dists/$s/$c/binary-$ARCH/Packages"
pkgdest="$TARGET/$($DLDEST pkg "$s" "$c" "$ARCH" "$m1" "$path")"
echo "$("$PKGDETAILS" FIELD "$field" "$m1" "$pkgdest" "$@" | sed 's/ .*//')"
done
done
done
}
################################################################ extraction
EXTRACTORS_SUPPORTED="dpkg-deb ar"
EXTRACT_DEB_TAR_OPTIONS=
# Native dpkg-deb based extractors
extract_dpkg_deb_field () {
local pkg field
pkg="$1"
field="$2"
dpkg-deb -f "$pkg" "$field"
}
extract_dpkg_deb_data () {
local pkg="$1"
dpkg-deb --fsys-tarfile "$pkg" | tar $EXTRACT_DEB_TAR_OPTIONS -xf - || error 1 FILEEXIST "Tried to extract package, but file already exists. Exit..."
}
# Raw .deb extractors
extract_ar_deb_field () {
local pkg field tarball
pkg="$1"
field="$2"
tarball=$(ar -t "$pkg" | grep "^control\.tar")
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 in_path $cat_cmd; 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
}
extract_ar_deb_data () {
local pkg tarball
pkg="$1"
tarball="$(ar -t "$pkg" | grep "^data.tar")"
case "$tarball" in
data.tar.gz) cat_cmd=zcat ;;
data.tar.bz2) cat_cmd=bzcat ;;
data.tar.xz) cat_cmd=xzcat ;;
data.tar) cat_cmd=cat ;;
*) error 1 UNKNOWNDATACOMP "Unknown compression type for %s in %s" "$tarball" "$pkg" ;;