]> git.deb.at Git - deb/packages.git/blob - bin/parse-contents
Replace non working volatile mirror debian.domainmail.org with mirror.csclub.uwaterloo.ca
[deb/packages.git] / bin / parse-contents
1 #!/usr/bin/perl -w
2 # Convert Contents.gz files into Sleepycat db files for efficient usage of
3 # data
4 #
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.
10
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.
15
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.
19
20 use strict;
21 use warnings;
22 use lib './lib';
23
24 $| = 1;
25
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?
28 $ENV{"LC_ALL"} = 'C';
29
30 my $what = $ARGV[0] ? "head -10000|" : "";
31
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;
36
37 use English;
38 use DB_File;
39 use Storable;
40 use File::Path;
41 use File::Basename;
42 use Packages::CommonCode qw(:all);
43 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES @ARCHITECTURES );
44 &Packages::Config::init( './' );
45
46 my @archives = @ARCHIVES;
47 my @suites = @SUITES;
48 my @archs = @ARCHITECTURES;
49
50 $DBDIR .= "/contents";
51 mkdirp( $DBDIR );
52
53 for my $suite (@suites) {
54     for my $arch (@archs) {
55
56         my $filelist_db = "$DBDIR/filelists_${suite}_${arch}.db";
57         my $dbtime = (stat $filelist_db)[9];
58         my %packages_contents = ();
59         my %packages_contents_nr = ();
60         my %packages_contents_lastword = ();
61
62         my $extra = "";
63         $extra = "|sort" if $SORT_REVERSE_CONCURRENTLY;
64
65         open REVERSED, "$extra>$DBDIR/reverse.tmp"
66             or die "Failed to open output reverse file: $!";
67
68         my $changed = 0;
69         for my $archive (@archives) {
70
71             my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
72             next unless -f $filename;
73             # Note: ctime, because mtime is set back via rsync
74             my $ftime = (stat $filename)[10];
75             next if defined $dbtime and $dbtime > $ftime;
76             print "$archive/$suite/$arch needs update\n";
77             $changed++;
78         }
79         if ($changed) {
80             for my $archive (@archives) {
81
82                 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
83                 next unless -f $filename;
84                 print "Reading $archive/$suite/$arch...\n";
85
86                 open CONT, "zcat $filename|$what"
87                     or die $!;
88                 while (<CONT>) { last if /^FILE/mo; }
89                 if (eof(CONT)) { # no header found
90                     close CONT; # explicit close to reset $.
91                     open CONT, "zcat $filename|$what";
92                 }
93                 while (<CONT>) {
94                     my $data = "";
95                     my %data = ();
96                     chomp;
97                     print "Doing line ".($NR/1000)."k (out of approx 2.0M)\n"
98                         if $NR % 250000 == 0;
99                     /^(.+?)\s+(\S+)$/o;
100                     my ($file, $value) = ($1, $2);
101                     $value =~ s#[^,/]+/##og;
102                     my @packages = split m/,/, $value;
103                     for (@packages) {
104                         $packages_contents_nr{$_}++;
105                         my $lw = $packages_contents_lastword{$_} || "\0";
106                         my $i=0;
107                         while (substr($file,$i,1) eq substr($lw,$i++,1)) {}
108                         $i--;
109                         $i = 255 if $i > 255;
110                         $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i));
111                         $packages_contents_lastword{$_} = "$file\0";
112                     }
113                     # Searches are case-insensitive
114                     (my $nocase = $file) =~ tr [A-Z] [a-z];
115                     my $case = ($nocase eq $file) ? '-' : $file;
116
117                     print REVERSED (reverse $nocase)."\0".$case."\0".
118                         (join ":$arch\0", @packages).":$arch\n";
119                 }
120                 close CONT;
121
122             }
123             close REVERSED;
124
125             print "Sorting reverse list if needed\n";
126             system("cd $DBDIR && sort reverse.tmp > reverse.sorted &&".
127                    " mv reverse.{sorted,tmp}") == 0
128                    or die "Failed to sort reverse"
129                    unless $SORT_REVERSE_CONCURRENTLY;
130
131             print "Writing filelist db\n";
132             tie my %packages_contents_db, "DB_File", "$filelist_db.new",
133             O_RDWR|O_CREAT, 0666, $DB_BTREE
134                 or die "Error creating DB: $!";
135             while (my ($k, $v) = each(%packages_contents)) {
136                 $packages_contents_db{$k} = (pack "L", $packages_contents_nr{$k})
137                     . $v;
138             }
139             untie %packages_contents_db;
140
141             rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt");
142
143             activate($filelist_db);
144             #FIXME: hardcoded archs. (debports has no contrib/non-free)
145             if ($arch !~ m/^kfreebsd-.*$/) {
146                 system("ln", "-sf", basename($filelist_db),
147                        "$DBDIR/filelists_${suite}_all.db") == 0
148                            or die "Oops";
149             }
150         }
151     }
152
153     my $go = 0;
154     my $suite_mtime = (stat "$DBDIR/reverse_$suite.db")[9];
155     for my $file (glob "$DBDIR/reverse_${suite}_*.txt") {
156         $go = 1 if not defined $suite_mtime
157             or $suite_mtime < (stat $file)[9];
158     }
159     next unless $go;
160
161     print "Merging reverse path lists for ${suite}...\n";
162
163     open MERGED, "-|", "sort -m $DBDIR/reverse_${suite}_*.txt"
164         or die "Failed to open merged list";
165     open FILENAMES, ">", "$DBDIR/filenames_$suite.txt.new"
166         or die "Failed to open filenames list";
167     tie my %reverse_path_db, "DB_File", "$DBDIR/reverse_${suite}.db.new",
168         O_RDWR|O_CREAT, 0666, $DB_BTREE
169         or die "Error creating DB: $!";
170
171     my $lastpath = my $lastcasepath = my $lastfile = "";
172     my %matches = ();
173     while (<MERGED>) {
174         print "Doing line ".($NR/1000000)."M (out of approx. 20M)\n"
175             if $NR % 1000000 == 0;
176         chomp;
177         my @line = split m/\0/o, $_;
178         my $revpath = shift @line;
179         my $casepath = shift @line;
180         if ($revpath ne $lastpath) {
181             # Wrap: Do useful stuff with this ($lastpath, @matches)
182             if ($lastpath ne "") {
183                 my @matches;
184                 while (my ($k, $v) = each %matches) {
185                     push @matches, join("\0", $k, @$v);
186                 }
187                 $reverse_path_db{$lastpath} = join "\1", @matches;
188                 %matches = ();
189             }
190             $lastpath =~ s,/.*,,o;
191             if ($lastfile ne $lastpath) {
192                 $lastfile = $lastpath;
193                 print FILENAMES (reverse $lastfile)."\n";
194             }
195             #
196             $lastpath = $revpath;
197             $lastcasepath = $casepath;
198             $matches{$casepath} = \@line;
199             next;
200 #       } elsif ($lastcasepath ne "" and $casepath ne $lastcasepath) {
201 #           warn reverse($revpath)." has more than one casepath: $casepath $lastcasepath\n";
202         }
203         push @{$matches{$casepath}}, @line;
204     }
205     # Note: do useful stuff here too, for out last entry. Maybe prevent this by
206     # adding a fake ultimate entry?
207     {
208         my @matches;
209         while (my ($k, $v) = each %matches) {
210             push @matches, join("\0", $k, @$v);
211         }
212         $reverse_path_db{$lastpath} = join "\1", @matches;
213     }
214
215     untie %reverse_path_db;
216     close FILENAMES;
217     close MERGED;
218
219     activate("$DBDIR/filenames_$suite.txt");
220     activate("$DBDIR/reverse_$suite.db");
221 }
222
223 # vim: set ts=4