2 # Convert Contents.gz files into Sleepycat db files for efficient usage of
5 # Copyright (C) 2006 Jeroen van Wolffelaar <jeroen@wolffelaar.nl>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 # Important, we want sorting and such to happen like in the C locale: binary,
27 # without any fancy collation. FIXME: is this actually adequate?
30 my $what = $ARGV[0] ? "head -10000|" : "";
32 # More RAM vs more disk I/O tradeoff parameters, does not change
33 # functionality. True will always use more RAM at the benefit of less
34 # temporary files, and is adviced when possible
35 my $SORT_REVERSE_CONCURRENTLY = 1;
42 use Packages::CommonCode qw(:all);
43 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES @ARCHITECTURES );
44 &Packages::Config::init( './' );
46 my @archives = @ARCHIVES;
48 my @archs = @ARCHITECTURES;
50 @deborts_hash{qw( avr32 m68k powerpcspe sh4 sparc64 )} = ();
52 $DBDIR .= "/contents";
55 for my $suite (@suites) {
56 for my $arch (@archs) {
58 my $filelist_db = "$DBDIR/filelists_${suite}_${arch}.db";
59 my $dbtime = (stat $filelist_db)[9];
60 my %packages_contents = ();
61 my %packages_contents_nr = ();
62 my %packages_contents_lastword = ();
65 $extra = "|sort" if $SORT_REVERSE_CONCURRENTLY;
67 open REVERSED, "$extra>$DBDIR/reverse.tmp"
68 or die "Failed to open output reverse file: $!";
71 for my $archive (@archives) {
73 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
74 next unless -f $filename;
75 # Note: ctime, because mtime is set back via rsync
76 my $ftime = (stat $filename)[10];
77 next if defined $dbtime and $dbtime > $ftime;
78 print "$archive/$suite/$arch needs update\n";
82 for my $archive (@archives) {
84 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
85 next unless -f $filename;
86 print "Reading $archive/$suite/$arch...\n";
88 open CONT, "zcat $filename|$what"
91 while (<CONT>) { /^FILE/mo && do { $header_found = 1; last };}
92 if (eof(CONT)) { # no header found or only header found
93 close CONT; # explicit close to reset $.
94 next if $header_found;
95 open CONT, "zcat $filename|$what";
101 print "Doing line ".($NR/1000)."k (out of approx 2.0M)\n"
102 if $NR % 250000 == 0;
104 my ($file, $value) = ($1, $2);
105 $value =~ s#[^,/]+/##og;
106 my @packages = split m/,/, $value;
108 $packages_contents_nr{$_}++;
109 my $lw = $packages_contents_lastword{$_} || "\0";
111 while (substr($file,$i,1) eq substr($lw,$i++,1)) {}
113 $i = 255 if $i > 255;
114 $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i));
115 $packages_contents_lastword{$_} = "$file\0";
117 # Searches are case-insensitive
118 (my $nocase = $file) =~ tr [A-Z] [a-z];
119 my $case = ($nocase eq $file) ? '-' : $file;
121 print REVERSED (reverse $nocase)."\0".$case."\0".
122 (join ":$arch\0", @packages).":$arch\n";
129 print "Sorting reverse list if needed\n";
130 system("cd $DBDIR && sort reverse.tmp > reverse.sorted &&".
131 " mv reverse.{sorted,tmp}") == 0
132 or die "Failed to sort reverse"
133 unless $SORT_REVERSE_CONCURRENTLY;
135 print "Writing filelist db\n";
136 tie my %packages_contents_db, "DB_File", "$filelist_db.new",
137 O_RDWR|O_CREAT, 0666, $DB_BTREE
138 or die "Error creating DB: $!";
139 while (my ($k, $v) = each(%packages_contents)) {
140 $packages_contents_db{$k} = (pack "L", $packages_contents_nr{$k})
143 untie %packages_contents_db;
145 rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt");
147 activate($filelist_db);
148 #FIXME: hardcoded archs. (debports has no contrib/non-free)
149 if (not exists $deborts_hash{$arch}) {
150 system("ln", "-sf", basename($filelist_db),
151 "$DBDIR/filelists_${suite}_all.db") == 0
158 my $suite_mtime = (stat "$DBDIR/reverse_$suite.db")[9];
159 for my $file (glob "$DBDIR/reverse_${suite}_*.txt") {
160 $go = 1 if not defined $suite_mtime
161 or $suite_mtime < (stat $file)[9];
165 print "Merging reverse path lists for ${suite}...\n";
167 open MERGED, "-|", "sort -m $DBDIR/reverse_${suite}_*.txt"
168 or die "Failed to open merged list";
169 open FILENAMES, ">", "$DBDIR/filenames_$suite.txt.new"
170 or die "Failed to open filenames list";
171 tie my %reverse_path_db, "DB_File", "$DBDIR/reverse_${suite}.db.new",
172 O_RDWR|O_CREAT, 0666, $DB_BTREE
173 or die "Error creating DB: $!";
175 my $lastpath = my $lastcasepath = my $lastfile = "";
178 print "Doing line ".($NR/1000000)."M (out of approx. 20M)\n"
179 if $NR % 1000000 == 0;
181 my @line = split m/\0/o, $_;
182 my $revpath = shift @line;
183 my $casepath = shift @line;
184 if ($revpath ne $lastpath) {
185 # Wrap: Do useful stuff with this ($lastpath, @matches)
186 if ($lastpath ne "") {
188 while (my ($k, $v) = each %matches) {
189 push @matches, join("\0", $k, @$v);
191 $reverse_path_db{$lastpath} = join "\1", @matches;
194 $lastpath =~ s,/.*,,o;
195 if ($lastfile ne $lastpath) {
196 $lastfile = $lastpath;
197 print FILENAMES (reverse $lastfile)."\n";
200 $lastpath = $revpath;
201 $lastcasepath = $casepath;
202 $matches{$casepath} = \@line;
204 # } elsif ($lastcasepath ne "" and $casepath ne $lastcasepath) {
205 # warn reverse($revpath)." has more than one casepath: $casepath $lastcasepath\n";
207 push @{$matches{$casepath}}, @line;
209 # Note: do useful stuff here too, for out last entry. Maybe prevent this by
210 # adding a fake ultimate entry?
213 while (my ($k, $v) = each %matches) {
214 push @matches, join("\0", $k, @$v);
216 $reverse_path_db{$lastpath} = join "\1", @matches;
219 untie %reverse_path_db;
223 activate("$DBDIR/filenames_$suite.txt");
224 activate("$DBDIR/reverse_$suite.db");