forked from flavienbwk/apt-mirror-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apt-mirror
1154 lines (986 loc) · 36.4 KB
/
apt-mirror
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
#!/usr/bin/perl
=pod
=head1 NAME
apt-mirror - apt sources mirroring tool
=head1 SYNOPSIS
apt-mirror [configfile]
=head1 DESCRIPTION
A small and efficient tool that lets you mirror a part of or
the whole Debian GNU/Linux distribution or any other apt sources.
Main features:
* It uses a config similar to APT's F<sources.list>
* It's fully pool compliant
* It supports multithreaded downloading
* It supports multiple architectures at the same time
* It can automatically remove unneeded files
* It works well on an overloaded Internet connection
* It never produces an inconsistent mirror including while mirroring
* It works on all POSIX compliant systems with Perl and wget
=head1 COMMENTS
apt-mirror uses F</etc/apt/mirror.list> as a configuration file.
By default it is tuned to official Debian or Ubuntu mirrors. Change
it for your needs.
After you setup the configuration file you may run as root:
# su - apt-mirror -c apt-mirror
Or uncomment the line in F</etc/cron.d/apt-mirror> to enable daily mirror updates.
=head1 FILES
F</etc/apt/mirror.list>
Main configuration file
F</etc/cron.d/apt-mirror>
Cron configuration template
F</var/spool/apt-mirror/mirror>
Mirror places here
F</var/spool/apt-mirror/skel>
Place for temporarily downloaded indexes
F</var/spool/apt-mirror/var>
Log files placed here. URLs and MD5 checksums also here.
=head1 CONFIGURATION EXAMPLES
The mirror.list configuration supports many options, the file is well commented explaining each option.
Here are some sample mirror configuration lines showing the various supported ways:
Normal:
deb http://example.com/debian stable main contrib non-free
Arch Specific: (many other architectures are supported)
deb-powerpc http://example.com/debian stable main contrib non-free
HTTP and FTP Auth or non-standard port:
deb http://user:[email protected]:8080/debian stable main contrib non-free
HTTPS with sending Basic HTTP authentication information (plaintext username and password) for all requests:
(this was default behaviour of Wget 1.10.2 and prior and is needed for some servers with new version of Wget)
set auth_no_challenge 1
deb https://user:[email protected]:443/debian stable main contrib non-free
HTTPS without checking certificate:
set no_check_certificate 1
deb https://example.com:443/debian stable main contrib non-free
Source Mirroring:
deb-src http://example.com/debian stable main contrib non-free
=head1 AUTHORS
Dmitry N. Hramtsov E<lt>[email protected]<gt>
Brandon Holtsclaw E<lt>[email protected]<gt>
=cut
use warnings;
use strict;
use File::Copy;
use File::Compare;
use File::Path qw(make_path);
use File::Basename;
use Fcntl qw(:flock);
my $config_file;
my %config_variables = (
"defaultarch" => `dpkg --print-architecture 2>/dev/null` || 'i386',
"nthreads" => 20,
"base_path" => '/var/spool/apt-mirror',
"mirror_path" => '$base_path/mirror',
"skel_path" => '$base_path/skel',
"var_path" => '$base_path/var',
"cleanscript" => '$var_path/clean.sh',
"_contents" => 1,
"_autoclean" => 0,
"_tilde" => 0,
"limit_rate" => '100m',
"run_postmirror" => 1,
"auth_no_challenge" => 0,
"no_check_certificate" => 0,
"unlink" => 0,
"postmirror_script" => '$var_path/postmirror.sh',
"use_proxy" => 'off',
"http_proxy" => '',
"https_proxy" => '',
"proxy_user" => '',
"proxy_password" => '',
"certificate" => '',
"private_key" => '',
"ca_certificate" => '',
);
my @config_binaries = ();
my @config_sources = ();
my @release_urls;
my @index_urls;
my @childrens = ();
my %skipclean = ();
my %clean_directory = ();
my @hash_strength = qw(SHA512 SHA256 SHA1 MD5Sum);
# Mapping of files downloaded from a by-hash directory to their canonical locations.
my %hashsum_to_files = ();
# Mapping of all the checksums for a given canonical filename.
my %file_to_hashsums;
######################################################################################
## Setting up $config_file variable
$config_file = "/etc/apt/mirror.list"; # Default value
if ( $_ = shift )
{
die("apt-mirror: invalid config file specified") unless -e $_;
$config_file = $_;
}
chomp $config_variables{"defaultarch"};
######################################################################################
##################################################
## Make sure we have all the compression commands
my @compression_extensions = qw(xz gz bz2);
my %compression_commands = (
xz => "xz",
gz => "gzip",
bz2 => "bzip2",
);
sub check_compression_commands
{
my $success = 1;
foreach (@compression_extensions)
{
unless("dummy" eq `echo -n dummy | ${compression_commands{$_}} 2>/dev/null | ${compression_commands{$_}} -d 2>/dev/null`)
{
warn("apt-mirror: You must have a working ${compression_commands{$_}} in your \$PATH to decompress .${_} files.\n");
$success = 0;
}
}
return $success;
}
die("apt-mirror: Failing due to missing compression commands.\n") unless check_compression_commands;
## Common subroutines
sub round_number
{
my $n = shift;
my $minus = $n < 0 ? '-' : '';
$n = abs($n);
$n = int( ( $n + .05 ) * 10 ) / 10;
$n .= '.0' unless $n =~ /\./;
$n .= '0' if substr( $n, ( length($n) - 1 ), 1 ) eq '.';
chop $n if $n =~ /\.\d\d0$/;
return "$minus$n";
}
sub format_bytes
{
my $bytes = shift;
my $bytes_out = '0';
my $size_name = 'bytes';
my $KiB = 1024;
my $MiB = 1024 * 1024;
my $GiB = 1024 * 1024 * 1024;
if ( $bytes >= $KiB )
{
$bytes_out = $bytes / $KiB;
$size_name = 'KiB';
if ( $bytes >= $MiB )
{
$bytes_out = $bytes / $MiB;
$size_name = 'MiB';
if ( $bytes >= $GiB )
{
$bytes_out = $bytes / $GiB;
$size_name = 'GiB';
}
}
$bytes_out = round_number($bytes_out);
}
else
{
$bytes_out = $bytes;
$size_name = 'bytes';
}
return "$bytes_out $size_name";
}
sub get_variable
{
my $value = $config_variables{ shift @_ };
my $count = 16;
while ( $value =~ s/\$(\w+)/$config_variables{$1}/xg )
{
die("apt-mirror: too many substitution while evaluating variable") if ( $count-- ) < 0;
}
return $value;
}
sub quoted_path
{
my $path = shift;
$path =~ s/'/'\\''/g;
return "'" . $path . "'";
}
sub lock_aptmirror
{
open( LOCK_FILE, '>', get_variable("var_path") . "/apt-mirror.lock" );
my $lock = flock( LOCK_FILE, LOCK_EX | LOCK_NB );
if ( !$lock )
{
die("apt-mirror is already running, exiting");
}
}
sub unlock_aptmirror
{
close(LOCK_FILE);
unlink( get_variable("var_path") . "/apt-mirror.lock" );
}
sub download_urls
{
my $stage = shift;
my @urls;
my $i = 0;
my $pid;
my $nthreads = get_variable("nthreads");
my @args = ();
local $| = 1;
@urls = @_;
$nthreads = @urls if @urls < $nthreads;
if ( get_variable("auth_no_challenge") == 1 ) { push( @args, "--auth-no-challenge" ); }
if ( get_variable("no_check_certificate") == 1 ) { push( @args, "--no-check-certificate" ); }
if ( get_variable("unlink") == 1 ) { push( @args, "--unlink" ); }
if ( length( get_variable("use_proxy") ) && ( get_variable("use_proxy") eq 'yes' || get_variable("use_proxy") eq 'on' ) )
{
if ( length( get_variable("http_proxy") ) || length( get_variable("https_proxy") ) ) { push( @args, "-e use_proxy=yes" ); }
if ( length( get_variable("http_proxy") ) ) { push( @args, "-e http_proxy=" . get_variable("http_proxy") ); }
if ( length( get_variable("https_proxy") ) ) { push( @args, "-e https_proxy=" . get_variable("https_proxy") ); }
if ( length( get_variable("proxy_user") ) ) { push( @args, "-e proxy_user=" . get_variable("proxy_user") ); }
if ( length( get_variable("proxy_password") ) ) { push( @args, "-e proxy_password=" . get_variable("proxy_password") ); }
}
if ( length( get_variable("certificate") ) ) { push( @args, "--certificate=" . get_variable("certificate") ); }
if ( length( get_variable("private_key") ) ) { push( @args, "--private-key=" . get_variable("private_key") ); }
if ( length( get_variable("ca_certificate") ) ) { push( @args, "--ca-certificate=" . get_variable("ca_certificate") ); }
push @args, "--no-if-modified-since";
print "Downloading " . scalar(@urls) . " $stage files using $nthreads threads...\n";
while ( scalar @urls )
{
my @part = splice( @urls, 0, int( @urls / $nthreads ) );
open URLS, ">" . get_variable("var_path") . "/$stage-urls.$i" or die("apt-mirror: can't write to intermediate file ($stage-urls.$i)");
foreach (@part) { print URLS "$_\n"; }
close URLS or die("apt-mirror: can't close intermediate file ($stage-urls.$i)");
$pid = fork();
die("apt-mirror: can't do fork in download_urls") if !defined($pid);
if ( $pid == 0 )
{
exec 'wget', '--no-cache', '--limit-rate=' . get_variable("limit_rate"), '-t', '5', '-r', '-N', '-l', 'inf', '-o', get_variable("var_path") . "/$stage-log.$i", '-i', get_variable("var_path") . "/$stage-urls.$i", @args;
# shouldn't reach this unless exec fails
die("\n\nCould not run wget, please make sure its installed and in your path\n\n");
}
push @childrens, $pid;
$i++;
$nthreads--;
}
print "Begin time: " . localtime() . "\n[" . scalar(@childrens) . "]... ";
while ( scalar @childrens )
{
my $dead = wait();
@childrens = grep { $_ != $dead } @childrens;
print "[" . scalar(@childrens) . "]... ";
}
print "\nEnd time: " . localtime() . "\n\n";
if (scalar keys %hashsum_to_files > 0)
{
foreach my $hashsum_filename (keys %hashsum_to_files)
{
foreach my $filename (@{$hashsum_to_files{$hashsum_filename}})
{
copy_file( $hashsum_filename, $filename );
}
}
}
}
## Parse config
sub parse_config_line
{
my $pattern_deb_line = qr/^[\t ]*(?<type>deb-src|deb)(?:-(?<arch>[\w\-]+))?[\t ]+(?:\[(?<options>[^\]]+)\][\t ]+)?(?<uri>[^\s]+)[\t ]+(?<components>.+)$/;
my $line = $_;
my %config;
if ( $line =~ $pattern_deb_line ) {
$config{'type'} = $+{type};
$config{'arch'} = $+{arch};
$config{'options'} = $+{options} ? $+{options} : "";
$config{'uri'} = $+{uri};
$config{'components'} = $+{components};
if ( $config{'options'} =~ /arch=((?<arch>[\w\-]+)[,]*)/g ) {
$config{'arch'} = $+{arch};
}
$config{'components'} = [ split /\s+/, $config{'components'} ];
} elsif ( $line =~ /set[\t ]+(?<key>[^\s]+)[\t ]+(?<value>"[^"]+"|'[^']+'|[^\s]+)/ ) {
$config{'type'} = 'set';
$config{'key'} = $+{key};
$config{'value'} = $+{value};
$config{'value'} =~ s/^'(.*)'$/$1/;
$config{'value'} =~ s/^"(.*)"$/$1/;
} elsif ( $line =~ /(?<type>clean|skip-clean)[\t ]+(?<uri>[^\s]+)/ ) {
$config{'type'} = $+{type};
$config{'uri'} = $+{uri};
}
return %config;
}
open CONFIG, "<$config_file" or die("apt-mirror: can't open config file ($config_file)");
while (<CONFIG>)
{
next if /^\s*#/;
next unless /\S/;
my $line = $_;
my %config_line = parse_config_line;
if ( $config_line{'type'} eq "set" ) {
$config_variables{ $config_line{'key'} } = $config_line{'value'};
next;
} elsif ( $config_line{'type'} eq "deb" ) {
my $arch = $config_line{'arch'};
$arch = get_variable("defaultarch") if ! defined $config_line{'arch'};
push @config_binaries, [ $arch, $config_line{'uri'}, @{$config_line{'components'}} ];
next;
} elsif ( $config_line{'type'} eq "deb-src" ) {
push @config_sources, [ $config_line{'uri'}, @{$config_line{'components'}} ];
next;
} elsif ( $config_line{'type'} =~ /(skip-clean|clean)/ ) {
my $link = $config_line{'uri'};
$link =~ s[^(\w+)://][];
$link =~ s[/$][];
$link =~ s[~][%7E]g if get_variable("_tilde");
if ( $config_line{'type'} eq "skip-clean" ) {
$skipclean{ $link } = 1;
} elsif ( $config_line{'type'} eq "clean" ) {
$clean_directory{ $link } = 1;
}
next;
}
die("apt-mirror: invalid line in config file ($.: $line ...)");
}
close CONFIG;
die("Please explicitly specify 'defaultarch' in mirror.list") unless get_variable("defaultarch");
######################################################################################
## Create the 3 needed directories if they don't exist yet
my @needed_directories = ( get_variable("mirror_path"), get_variable("skel_path"), get_variable("var_path") );
foreach my $needed_directory (@needed_directories)
{
unless ( -d $needed_directory )
{
make_path($needed_directory) or die("apt-mirror: can't create $needed_directory directory");
}
}
#
#######################################################################################
lock_aptmirror();
######################################################################################
## Skel download
my %urls_to_download = ();
my ( $url, $arch );
sub remove_double_slashes
{
local $_ = shift;
while (s[/\./][/]g) { }
while (s[(?<!:)//][/]g) { }
while (s[(?<!:/)/[^/]+/\.\./][/]g) { }
s/~/\%7E/g if get_variable("_tilde");
return $_;
}
sub add_url_to_download
{
my $url = remove_double_slashes(shift);
my $size = shift;
my $strongest_hash = shift;
my $hash = shift;
my $hashsum = shift;
my $canonical_filename = $url;
$canonical_filename =~ s[^(\w+)://][];
$canonical_filename =~ s[~][%7E]g if get_variable("_tilde");
$skipclean{$canonical_filename} = 1;
if ($hashsum)
{
# If the optional hashsum was passed as an argument
# - download the strongest hash only
# - make a copy to the canonical location
# - make a copy for the other known hash versions
$url = dirname($url) . "/by-hash/${hash}/${hashsum}";
my $hashsum_filename = dirname($canonical_filename) . "/by-hash/${hash}/${hashsum}";
$skipclean{$hashsum_filename} = 1;
if ($hash eq $strongest_hash)
{
# This is the strongest hash, which is the one to download.
# Also need to remember to which canonical location it should be linked.
$hashsum_to_files{$hashsum_filename} ||= [];
push @{$hashsum_to_files{$hashsum_filename}}, $canonical_filename;
$urls_to_download{$url} = $size;
} else {
# We are not going to download using this checksum, but we still
# need to know where to put the checksum.
$file_to_hashsums{$canonical_filename} ||= [];
push @{$file_to_hashsums{$canonical_filename}}, $hashsum_filename;
}
} else {
# Not using by-hash, so download the file only.
$urls_to_download{$url} = $size;
}
}
foreach (@config_sources)
{
my ( $uri, $distribution, @components ) = @{$_};
if (@components)
{
$url = $uri . "/dists/" . $distribution . "/";
}
else
{
# https://wiki.debian.org/DebianRepository/Format#Flat_Repository_Format
$url = $uri . "/" . $distribution . "/";
}
add_url_to_download( $url . "InRelease" );
add_url_to_download( $url . "Release" );
add_url_to_download( $url . "Release.gpg" );
}
foreach (@config_binaries)
{
my ( $arch, $uri, $distribution, @components ) = @{$_};
if (@components)
{
$url = $uri . "/dists/" . $distribution . "/";
}
else
{
# https://wiki.debian.org/DebianRepository/Format#Flat_Repository_Format
$url = $uri . "/" . $distribution . "/";
}
add_url_to_download( $url . "InRelease" );
add_url_to_download( $url . "Release" );
add_url_to_download( $url . "Release.gpg" );
}
chdir get_variable("skel_path") or die("apt-mirror: can't chdir to skel");
@release_urls = sort keys %urls_to_download;
download_urls( "release", @release_urls );
######################################################################################
## Download all relevant metadata
%urls_to_download = ();
sub sanitise_uri
{
my $uri = shift;
$uri =~ s[^(\w+)://][];
$uri =~ s/^([^@]+)?@?// if $uri =~ /@/;
$uri =~ s/~/\%7E/g if get_variable("_tilde");
return $uri;
}
sub find_metadata_in_release
{
# Look in the Release file for any files we need to download
my ( $arch, $uri, $distribution, @components ) = @_;
my ( $release_uri, $release_path, $line ) = '';
my $component_regex = undef;
my $arch_regex = "(?:${arch}|all)";
my $compressed_extension_regex = '(?:\.(?:gz|bz2|xz))$';
my $dist_uri;
my $hash_type_regex = "(?:" . join("|", @hash_strength) . ")";
if (@components)
{
$dist_uri = remove_double_slashes($uri . "/dists/" . $distribution . "/");
$component_regex = "(?:" . join("|", @components) . ")";
}
else {
$dist_uri = remove_double_slashes($uri . "/" . $distribution . "/");
}
my $stream;
foreach my $release_filename ("InRelease", "Release")
{
$release_uri = $dist_uri . $release_filename;
$release_path = get_variable("skel_path") . "/" . sanitise_uri($release_uri);
last if ( open $stream, "<", $release_path);
$stream = undef;
}
unless ( $stream )
{
warn( "Failed to find InRelease or Release in " . get_variable("skel_path") . "/" . sanitise_uri($dist_uri) );
return 0;
}
my $hash = undef;
my %avaiable_hashes = ();
my $acquire_by_hash = 0;
my @parts_to_download = ();
while ( $line = <$stream> )
{
chomp $line;
if ($hash)
{
if ( $line =~ /^ +(.*)$/ )
{
my @parts = split( / +/, $1 );
if ( @parts == 3 )
{
my ( $hashsum, $size, $filename ) = @parts;
push @parts, $hash;
if ($arch eq "source")
{
if ($component_regex)
{
# Debian repository format https://wiki.debian.org/DebianRepository/Format#Debian_Repository_Format
if (
(
$filename =~ m{^${component_regex}/source/Sources${compressed_extension_regex}}
) or (
$filename =~ m{^${component_regex}/Contents-source${compressed_extension_regex}}
)
)
{
push @parts_to_download, \@parts;
}
} else {
# Flat repository format https://wiki.debian.org/DebianRepository/Format#Flat_Repository_Format
if ($filename =~ m{^Sources${compressed_extension_regex}}
) {
push @parts_to_download, \@parts;
}
}
} else {
if ($component_regex)
{
# Debian repository format https://wiki.debian.org/DebianRepository/Format#Debian_Repository_Format
if (
(
$filename =~ m{^Contents-${arch_regex}${compressed_extension_regex}}
) or (
$filename =~ m{^${component_regex}/Contents-${arch_regex}${compressed_extension_regex}}
) or (
$filename =~ m{^${component_regex}/binary-${arch_regex}/Packages${compressed_extension_regex}}
) or (
$filename =~ m{^${component_regex}/binary-${arch_regex}/Release$}
) or (
$filename =~ m{^${component_regex}/cnf/Commands-${arch_regex}${compressed_extension_regex}}
) or (
$filename =~ m{^${component_regex}/dep11/Components-${arch_regex}.*${compressed_extension_regex}}
) or (
$filename =~ m{^${component_regex}/dep11/icons-.*${compressed_extension_regex}}
) or (
$filename =~ m{^${component_regex}/i18n/Translation-.*${compressed_extension_regex}}
)
)
{
push @parts_to_download, \@parts;
}
} else {
# Flat repository format https://wiki.debian.org/DebianRepository/Format#Flat_Repository_Format
if ($filename =~ m{^Packages${compressed_extension_regex}})
{
push @parts_to_download, \@parts;
}
}
}
}
else
{
warn("Malformed checksum line \"$1\" in $release_uri");
}
}
else
{
$hash = undef;
}
}
if ( not $hash )
{
if ( $line =~ /^(${hash_type_regex}):$/ )
{
$hash = $1;
$avaiable_hashes{$hash} = 1;
}
elsif ( $line eq "Acquire-By-Hash: yes" )
{
$acquire_by_hash = 1;
}
}
}
close $stream;
my $strongest_hash;
if ($acquire_by_hash)
{
foreach (@hash_strength)
{
if ($avaiable_hashes{$_})
{
$strongest_hash = $_;
last;
}
}
unless ($strongest_hash)
{
warn("Cannot find a supported hash in $release_uri, will download from canonical locations.");
$acquire_by_hash = 0;
}
}
foreach (@parts_to_download)
{
my ( $hashsum, $size, $filename, $hash ) = @{$_};
if ($acquire_by_hash)
{
add_url_to_download( $dist_uri . $filename, $size, $strongest_hash, $hash, $hashsum );
}
else
{
add_url_to_download( $dist_uri . $filename, $size );
}
}
return 1;
}
print "Processing metadata files from releases [";
foreach (@config_binaries)
{
my ( $arch, $uri, $distribution, @components ) = @{$_};
print "M";
unless (find_metadata_in_release( $arch, $uri, $distribution, @components))
{
# Insecure repo with no release file - try to get the well known indices
foreach my $file_extension (".gz", ".xz", ".bz2", "")
{
if (@components)
{
# Debian repo
foreach my $component (@components)
{
foreach my $path (
"/dists/${distribution}/${component}/binary-${arch}/Packages",
"/dists/${distribution}/${component}/binary-all/Packages",
"/dists/${distribution}/${component}/Contents-${arch}",
"/dists/${distribution}/${component}/Contents-all",
"/dists/${distribution}/Contents-${arch}",
"/dists/${distribution}/Contents-all",
)
{
add_url_to_download( "${uri}/${path}${file_extension}" );
}
}
} else {
# Flat repo
foreach my $path (
"${distribution}/Packages",
"${distribution}/Contents-${arch}",
"${distribution}/Contents-all",
)
{
add_url_to_download( "${uri}/${path}${file_extension}" );
}
}
}
}
}
foreach (@config_sources)
{
my ( $uri, $distribution, @components ) = @{$_};
print "M";
unless (find_metadata_in_release( "source", $uri, $distribution, @components))
{
# Insecure repo with no release file - try to get the well known indices
foreach my $file_extension (".gz", ".xz", ".bz2", "")
{
if (@components)
{
# Debian repo
foreach my $path (
"${distribution}/source/Sources",
"${distribution}/Contents-source",
)
{
add_url_to_download( "${uri}/${path}${file_extension}" );
}
} else {
# Flat repo
add_url_to_download( "${uri}/${distribution}/Sources${file_extension}" );
}
}
}
}
print "]\n\n";
@index_urls = sort keys %urls_to_download;
download_urls( "index", @index_urls );
######################################################################################
## Main download preparations
%urls_to_download = ();
open FILES_ALL, ">" . get_variable("var_path") . "/ALL" or die("apt-mirror: can't write to intermediate file (ALL)");
open FILES_NEW, ">" . get_variable("var_path") . "/NEW" or die("apt-mirror: can't write to intermediate file (NEW)");
open FILES_MD5, ">" . get_variable("var_path") . "/MD5" or die("apt-mirror: can't write to intermediate file (MD5)");
open FILES_SHA1, ">" . get_variable("var_path") . "/SHA1" or die("apt-mirror: can't write to intermediate file (SHA1)");
open FILES_SHA256, ">" . get_variable("var_path") . "/SHA256" or die("apt-mirror: can't write to intermediate file (SHA256)");
my %stat_cache = ();
sub _stat
{
my ($filename) = shift;
return @{ $stat_cache{$filename} } if exists $stat_cache{$filename};
my @res = stat($filename);
$stat_cache{$filename} = \@res;
return @res;
}
sub clear_stat_cache
{
%stat_cache = ();
}
sub need_update
{
my $filename = shift;
my $size_on_server = shift;
my ( undef, undef, undef, undef, undef, undef, undef, $size ) = _stat($filename);
return 1 unless ($size);
return 0 if $size_on_server == $size;
# The file is corrupted, throw it away so we can download it again
unlink $filename;
return 1;
}
sub remove_spaces($)
{
my $hashref = shift;
foreach ( keys %{$hashref} )
{
while ( substr( $hashref->{$_}, 0, 1 ) eq ' ' )
{
substr( $hashref->{$_}, 0, 1 ) = '';
}
}
}
sub process_index
{
my $uri = shift;
my $index = shift;
my $optional = shift;
my ( $path, $package, $mirror, $files ) = '';
$path = sanitise_uri($uri);
local $/ = "\n\n";
$mirror = get_variable("mirror_path") . "/" . $path;
foreach my $extension (@compression_extensions)
{
if (-e "$path/$index.$extension" )
{
system("${compression_commands{$extension}} -d < $path/$index.$extension > $path/$index");
last;
}
}
unless ( open STREAM, "<$path/$index" )
{
if ($optional)
{
return;
}
warn("apt-mirror: can't open index $path/$index in process_index");
return;
}
while ( $package = <STREAM> )
{
local $/ = "\n";
chomp $package;
my ( undef, %lines ) = split( /^([\w\-]+:)/m, $package );
$lines{"Directory:"} = "" unless defined $lines{"Directory:"};
chomp(%lines);
remove_spaces( \%lines );
if ( exists $lines{"Filename:"} )
{ # Packages index
$skipclean{ remove_double_slashes( $path . "/" . $lines{"Filename:"} ) } = 1;
print FILES_ALL remove_double_slashes( $path . "/" . $lines{"Filename:"} ) . "\n";
print FILES_MD5 $lines{"MD5sum:"} . " " . remove_double_slashes( $path . "/" . $lines{"Filename:"} ) . "\n" if defined $lines{"MD5sum:"};
print FILES_SHA1 $lines{"SHA1:"} . " " . remove_double_slashes( $path . "/" . $lines{"Filename:"} ) . "\n" if defined $lines{"SHA1:"};
print FILES_SHA256 $lines{"SHA256:"} . " " . remove_double_slashes( $path . "/" . $lines{"Filename:"} ) . "\n" if defined $lines{"SHA256:"};
if ( need_update( $mirror . "/" . $lines{"Filename:"}, $lines{"Size:"} ) )
{
print FILES_NEW remove_double_slashes( $uri . "/" . $lines{"Filename:"} ) . "\n";
add_url_to_download( $uri . "/" . $lines{"Filename:"}, $lines{"Size:"} );
}
}
else
{ # Sources index
foreach ( split( /\n/, $lines{"Files:"} ) )
{
next if $_ eq '';
my @file = split;
die("apt-mirror: invalid Sources format") if @file != 3;
$skipclean{ remove_double_slashes( $path . "/" . $lines{"Directory:"} . "/" . $file[2] ) } = 1;
print FILES_ALL remove_double_slashes( $path . "/" . $lines{"Directory:"} . "/" . $file[2] ) . "\n";
print FILES_MD5 $file[0] . " " . remove_double_slashes( $path . "/" . $lines{"Directory:"} . "/" . $file[2] ) . "\n";
if ( need_update( $mirror . "/" . $lines{"Directory:"} . "/" . $file[2], $file[1] ) )
{
print FILES_NEW remove_double_slashes( $uri . "/" . $lines{"Directory:"} . "/" . $file[2] ) . "\n";
add_url_to_download( $uri . "/" . $lines{"Directory:"} . "/" . $file[2], $file[1] );
}
}
}
}
close STREAM;
}
print "Processing indexes: [";
foreach (@config_sources)
{
my ( $uri, $distribution, @components ) = @{$_};
print "S";
if (@components)
{
my $component;
foreach $component (@components)
{
process_index( $uri, "/dists/$distribution/$component/source/Sources" );
}
}
else
{
process_index( $uri, "/$distribution/Sources" );
}
}
foreach (@config_binaries)
{
my ( $arch, $uri, $distribution, @components ) = @{$_};
print "P";
if (@components)
{
my $component;
foreach $component (@components)
{
process_index( $uri, "/dists/$distribution/$component/binary-$arch/Packages" );
process_index( $uri, "/dists/$distribution/$component/binary-all/Packages", 1 );
}
}
else
{
process_index( $uri, "/$distribution/Packages" );
}
}
clear_stat_cache();
print "]\n\n";
close FILES_ALL;
close FILES_NEW;
close FILES_MD5;
close FILES_SHA1;
close FILES_SHA256;
######################################################################################
## Main download
chdir get_variable("mirror_path") or die("apt-mirror: can't chdir to mirror");
my $need_bytes = 0;
foreach ( values %urls_to_download )
{
$need_bytes += $_;
}
my $size_output = format_bytes($need_bytes);
print "$size_output will be downloaded into archive.\n";
download_urls( "archive", sort keys %urls_to_download );
######################################################################################
## Copy skel to main archive
sub copy_file
{
my ( $from, $to ) = @_;
my $dir = dirname($to);
return unless -f $from;
make_path($dir) unless -d $dir;
if ( get_variable("unlink") == 1 )
{
if ( compare( $from, $to ) != 0 ) { unlink($to); }
}
my @stat_from = stat($from);
if ( -f $to )
{
my @stat_to = stat($to);
return if ("@stat_to" eq "@stat_from");
}
unless ( link( $from, $to ) or copy( $from, $to ) )
{