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;
41 use Packages::CommonCode qw(:all);
42 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES @ARCHITECTURES );
43 &Packages::Config::init( './' );
45 my @archives = @ARCHIVES;
47 my @archs = @ARCHITECTURES;
49 $DBDIR .= "/contents";
52 for my $suite (@suites) {
53 for my $arch (@archs) {
55 my $filelist_db = "$DBDIR/filelists_${suite}_${arch}.db";
56 my $dbtime = (stat $filelist_db)[9];
57 my %packages_contents = ();
58 my %packages_contents_nr = ();
59 my %packages_contents_lastword = ();
62 $extra = "|sort" if $SORT_REVERSE_CONCURRENTLY;
64 open REVERSED, "$extra>$DBDIR/reverse.tmp"
65 or die "Failed to open output reverse file: $!";
68 for my $archive (@archives) {
70 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
71 next unless -f $filename;
72 # Note: ctime, because mtime is set back via rsync
73 my $ftime = (stat $filename)[10];
74 next if defined $dbtime and $dbtime > $ftime;
75 print "$archive/$suite/$arch needs update\n";
79 for my $archive (@archives) {
81 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
82 next unless -f $filename;
83 print "Reading $archive/$suite/$arch...\n";
85 open CONT, "zcat $filename|$what"
87 while (<CONT>) { last if /^FILE/mo; }
88 if (eof(CONT)) { # no header found
89 close CONT; # explicit close to reset $.
90 open CONT, "zcat $filename|$what";
96 print "Doing line ".($NR/1000)."k (out of approx 2.0M)\n"
99 my ($file, $value) = ($1, $2);
100 $value =~ s#[^,/]+/##og;
101 my @packages = split m/,/, $value;
103 $packages_contents_nr{$_}++;
104 my $lw = $packages_contents_lastword{$_} || "\0";
106 while (substr($file,$i,1) eq substr($lw,$i++,1)) {}
108 $i = 255 if $i > 255;
109 $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i));
110 $packages_contents_lastword{$_} = "$file\0";
112 # Searches are case-insensitive
113 (my $nocase = $file) =~ tr [A-Z] [a-z];
114 my $case = ($nocase eq $file) ? '-' : $file;
116 print REVERSED (reverse $nocase)."\0".$case."\0".
117 (join ":$arch\0", @packages).":$arch\n";
124 print "Sorting reverse list if needed\n";
125 system("cd $DBDIR && sort reverse.tmp > reverse.sorted &&".
126 " mv reverse.{sorted,tmp}") == 0
127 or die "Failed to sort reverse"
128 unless $SORT_REVERSE_CONCURRENTLY;
130 print "Writing filelist db\n";
131 tie my %packages_contents_db, "DB_File", "$filelist_db.new",
132 O_RDWR|O_CREAT, 0666, $DB_BTREE
133 or die "Error creating DB: $!";
134 while (my ($k, $v) = each(%packages_contents)) {
135 $packages_contents_db{$k} = (pack "L", $packages_contents_nr{$k})
138 untie %packages_contents_db;
140 rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt");
142 activate($filelist_db);
143 #FIXME: hardcoded archs. (debports has no contrib/non-free)
144 if ($arch !~ m/^kfreebsd-.*$/) {
145 system("ln", "-sf", $filelist_db,
146 "$DBDIR/filelists_${suite}_all.db") == 0
153 my $suite_mtime = (stat "$DBDIR/reverse_$suite.db")[9];
154 for my $file (glob "$DBDIR/reverse_${suite}_*.txt") {
155 $go = 1 if not defined $suite_mtime
156 or $suite_mtime < (stat $file)[9];
160 print "Merging reverse path lists for ${suite}...\n";
162 open MERGED, "-|", "sort -m $DBDIR/reverse_${suite}_*.txt"
163 or die "Failed to open merged list";
164 open FILENAMES, ">", "$DBDIR/filenames_$suite.txt.new"
165 or die "Failed to open filenames list";
166 tie my %reverse_path_db, "DB_File", "$DBDIR/reverse_${suite}.db.new",
167 O_RDWR|O_CREAT, 0666, $DB_BTREE
168 or die "Error creating DB: $!";
170 my $lastpath = my $lastcasepath = my $lastfile = "";
173 print "Doing line ".($NR/1000000)."M (out of approx. 20M)\n"
174 if $NR % 1000000 == 0;
176 my @line = split m/\0/o, $_;
177 my $revpath = shift @line;
178 my $casepath = shift @line;
179 if ($revpath ne $lastpath) {
180 # Wrap: Do useful stuff with this ($lastpath, @matches)
181 if ($lastpath ne "") {
183 while (my ($k, $v) = each %matches) {
184 push @matches, join("\0", $k, @$v);
186 $reverse_path_db{$lastpath} = join "\1", @matches;
189 $lastpath =~ s,/.*,,o;
190 if ($lastfile ne $lastpath) {
191 $lastfile = $lastpath;
192 print FILENAMES (reverse $lastfile)."\n";
195 $lastpath = $revpath;
196 $lastcasepath = $casepath;
197 $matches{$casepath} = \@line;
199 # } elsif ($lastcasepath ne "" and $casepath ne $lastcasepath) {
200 # warn reverse($revpath)." has more than one casepath: $casepath $lastcasepath\n";
202 push @{$matches{$casepath}}, @line;
204 # Note: do useful stuff here too, for out last entry. Maybe prevent this by
205 # adding a fake ultimate entry?
208 while (my ($k, $v) = each %matches) {
209 push @matches, join("\0", $k, @$v);
211 $reverse_path_db{$lastpath} = join "\1", @matches;
214 untie %reverse_path_db;
218 activate("$DBDIR/filenames_$suite.txt");
219 activate("$DBDIR/reverse_$suite.db");