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.
25 # Important, we want sorting and such to happen like in the C locale: binary,
26 # without any fancy collation. FIXME: is this actually adequate?
29 my $what = $ARGV[0] ? "head -10000|" : "";
31 # More RAM vs more disk I/O tradeoff parameters, does not change
32 # functionality. True will always use more RAM at the benefit of less
33 # temporary files, and is adviced when possible
34 my $SORT_REVERSE_CONCURRENTLY = 1;
39 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES @ARCHITECTURES );
40 &Packages::Config::init( './' );
42 my @archives = @ARCHIVES;
44 my @archs = @ARCHITECTURES;
46 $DBDIR .= "/contents";
47 -d $DBDIR || mkpath( $DBDIR );
49 for my $suite (@suites) {
50 for my $arch (@archs) {
52 my $filelist_db = "$DBDIR/filelists_${suite}_${arch}.db";
53 my $dbtime = (stat $filelist_db)[9];
54 my %packages_contents = ();
55 my %packages_contents_nr = ();
56 my %packages_contents_lastword = ();
59 $extra = "|sort" if $SORT_REVERSE_CONCURRENTLY;
61 open REVERSED, "$extra>$DBDIR/reverse.tmp"
62 or die "Failed to open output reverse file: $!";
65 for my $archive (@archives) {
67 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
68 next unless -f $filename;
69 # Note: ctime, because mtime is set back via rsync
70 my $ftime = (stat $filename)[10];
71 next if defined $dbtime and $dbtime > $ftime;
72 print "$archive/$suite/$arch needs update\n";
76 for my $archive (@archives) {
78 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
79 next unless -f $filename;
80 print "Reading $archive/$suite/$arch...\n";
82 open CONT, "zcat $filename|$what"
84 while (<CONT>) {last if /^FILE/mo;}
85 open CONT, "zcat $filename|$what" if eof(CONT);
90 print "Doing line ".($./1000)."k (out of approx 1.5M)\n" if $. % 250000 == 0;
92 my ($file, $value) = ($1, $2);
93 $value =~ s#[^,/]+/##og;
94 my @packages = split /,/, $value;
96 $packages_contents_nr{$_}++;
97 my $lw = $packages_contents_lastword{$_} || "\0";
99 while (substr($file,$i,1) eq substr($lw,$i++,1)) {}
101 $i = 255 if $i > 255;
102 $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i));
103 $packages_contents_lastword{$_} = "$file\0";
105 # Searches are case-insensitive
106 $file =~ tr [A-Z] [a-z];
108 print REVERSED (reverse $file)."\0".(join ":$arch\0", @packages).":$arch\n";
115 print "Sorting reverse list if needed\n";
116 system("cd $DBDIR && sort reverse.tmp > reverse.sorted && mv reverse.{sorted,tmp}") == 0
117 or die "Failed to sort reverse"
118 unless $SORT_REVERSE_CONCURRENTLY;
120 print "Writing filelist db\n";
121 tie my %packages_contents_db, "DB_File", "$filelist_db.new",
122 O_RDWR|O_CREAT, 0666, $DB_BTREE
123 or die "Error creating DB: $!";
124 while (my ($k, $v) = each(%packages_contents)) {
125 $packages_contents_db{$k} = (pack "L", $packages_contents_nr{$k})
128 untie %packages_contents_db;
130 rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt");
132 rename("$filelist_db.new", $filelist_db);
133 system("ln -sf $filelist_db $DBDIR/filelists_${suite}_all.db") == 0
139 my $suite_mtime = (stat "$DBDIR/reverse_$suite.db")[9];
140 for my $file (glob "$DBDIR/reverse_${suite}_*.txt") {
141 $go = 1 if not defined $suite_mtime
142 or $suite_mtime < (stat $file)[9];
146 print "Merging reverse path lists for ${suite}...\n";
148 open MERGED, "sort -m $DBDIR/reverse_${suite}_*.txt |"
149 or die "Failed to open merged list";
150 open FILENAMES, "> $DBDIR/filenames_$suite.txt.new"
151 or die "Failed to open filenames list";
152 tie my %reverse_path_db, "DB_File", "$DBDIR/reverse_${suite}.db.new",
153 O_RDWR|O_CREAT, 0666, $DB_BTREE
154 or die "Error creating DB: $!";
160 print "Doing line ".($./1000000)."M (out of approx. 16M)\n"
161 if $. % 1000000 == 0;
163 my @line = split /\0/o, $_;
164 my $revpath = shift @line;
165 if ($revpath ne $lastpath) {
166 # Wrap: Do useful stuff with this ($lastpath, @matches)
167 $reverse_path_db{$lastpath} = join "\0", @matches if $lastpath ne "";
168 $lastpath =~ s,/.*,,o;
169 if ($lastfile ne $lastpath) {
170 $lastfile = $lastpath;
171 print FILENAMES (reverse $lastfile)."\n";
174 $lastpath = $revpath;
180 # Note: do useful stuff here too, for out last entry. Maybe prevent this by
181 # adding a fake ultimate entry?
182 $reverse_path_db{$lastpath} = join "\0", @matches;
184 untie %reverse_path_db;
188 rename "$DBDIR/filenames_$suite.txt.new", "$DBDIR/filenames_$suite.txt";
189 rename "$DBDIR/reverse_$suite.db.new", "$DBDIR/reverse_$suite.db";