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