-my %packages_contents = ();
-my %packages_contents_nr = ();
-my %packages_contents_lastword = ();
-my %file_reverse = ();
-
-my @archives =( 'us'); #@ARCHIVES
-my @suites = ('stable');#@SUITES
-
-for my $archive (@archives) { for my $suite (@suites) {
-
- print "Reading $archive/$suite/i386...\n";
- open CONT, "zcat /org/ftp.debian.org/ftp/dists/stable/Contents-i386.gz|$what";
- while (1) {$_ = <CONT>;last if /^FILE/mo;}
- while (<CONT>) {
- my $data = "";
- my %data = ();
- chomp;
- print "Doing line $.\n" if $. % 10000 == 0;
- /^(\S+)\s+(\S+)/;
- my ($file, $value) = ($1, $2);
- $value =~ s#[^,/]+/##g;
- my @packages = split /,/, $value;
- for (@packages) {
+
+my @archives = @ARCHIVES;
+my @suites = @SUITES;
+my @archs = @ARCHITECTURES;
+
+$DBDIR .= "/contents";
+mkdirp( $DBDIR );
+
+for my $suite (@suites) {
+ for my $arch (@archs) {
+
+ my $filelist_db = "$DBDIR/filelists_${suite}_${arch}.db";
+ my $dbtime = (stat $filelist_db)[9];
+ my %packages_contents = ();
+ my %packages_contents_nr = ();
+ my %packages_contents_lastword = ();
+
+ my $extra = "";
+ $extra = "|sort" if $SORT_REVERSE_CONCURRENTLY;
+
+ open REVERSED, "$extra>$DBDIR/reverse.tmp"
+ or die "Failed to open output reverse file: $!";
+
+ my $changed = 0;
+ for my $archive (@archives) {
+
+ my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
+ next unless -f $filename;
+ # Note: ctime, because mtime is set back via rsync
+ my $ftime = (stat $filename)[10];
+ next if defined $dbtime and $dbtime > $ftime;
+ print "$archive/$suite/$arch needs update\n";
+ $changed++;
+ }
+ if ($changed) {
+ for my $archive (@archives) {
+
+ my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
+ next unless -f $filename;
+ print "Reading $archive/$suite/$arch...\n";
+
+ open CONT, "zcat $filename|$what"
+ or die $!;
+ while (<CONT>) { last if /^FILE/mo; }
+ if (eof(CONT)) { # no header found
+ close CONT; # explicit close to reset $.
+ open CONT, "zcat $filename|$what";
+ }
+ while (<CONT>) {
+ my $data = "";
+ my %data = ();
+ chomp;
+ print "Doing line ".($NR/1000)."k (out of approx 2.0M)\n"
+ if $NR % 250000 == 0;
+ /^(.+?)\s+(\S+)$/o;
+ my ($file, $value) = ($1, $2);
+ $value =~ s#[^,/]+/##og;
+ my @packages = split m/,/, $value;
+ for (@packages) {