]> git.deb.at Git - deb/packages.git/blob - bin/parse-contents
contents search: Give result in correct case
[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                 open CONT, "zcat $filename|$what" if eof(CONT);
87                 while (<CONT>) {
88                     my $data = "";
89                     my %data = ();
90                     chomp;
91                     print "Doing line ".($./1000)."k (out of approx 1.5M)\n" if $. % 250000 == 0;
92                     /^(.+?)\s+(\S+)$/o;
93                     my ($file, $value) = ($1, $2);
94                     $value =~ s#[^,/]+/##og;
95                     my @packages = split /,/, $value;
96                     for (@packages) {
97                         $packages_contents_nr{$_}++;
98                         my $lw = $packages_contents_lastword{$_} || "\0";
99                         my $i=0;
100                         while (substr($file,$i,1) eq substr($lw,$i++,1)) {}
101                         $i--;
102                         $i = 255 if $i > 255;
103                         $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i));
104                         $packages_contents_lastword{$_} = "$file\0";
105                     }
106                     # Searches are case-insensitive
107                     (my $nocase = $file) =~ tr [A-Z] [a-z];
108                     my $case = ($nocase eq $file) ? '-' : $file;
109
110                     print REVERSED (reverse $nocase)."\0".$case."\0".
111                         (join ":$arch\0", @packages).":$arch\n";
112                 }
113                 close CONT;
114                 
115             }
116             close REVERSED;
117             
118             print "Sorting reverse list if needed\n";
119             system("cd $DBDIR && sort reverse.tmp > reverse.sorted && mv reverse.{sorted,tmp}") == 0
120                 or die "Failed to sort reverse"
121                 unless $SORT_REVERSE_CONCURRENTLY;
122             
123             print "Writing filelist db\n";
124             tie my %packages_contents_db, "DB_File", "$filelist_db.new",
125             O_RDWR|O_CREAT, 0666, $DB_BTREE
126                 or die "Error creating DB: $!";
127             while (my ($k, $v) = each(%packages_contents)) {
128                 $packages_contents_db{$k} = (pack "L", $packages_contents_nr{$k})
129                     . $v;
130             }
131             untie %packages_contents_db;
132         
133             rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt");
134         
135             rename("$filelist_db.new", $filelist_db);
136             system("ln", "-sf", $filelist_db, "$DBDIR/filelists_${suite}_all.db") == 0
137                 or die "Oops";
138         }
139     }
140                           
141     my $go = 0;
142     my $suite_mtime = (stat "$DBDIR/reverse_$suite.db")[9];
143     for my $file (glob "$DBDIR/reverse_${suite}_*.txt") {
144         $go = 1 if not defined $suite_mtime
145             or $suite_mtime < (stat $file)[9];
146     }
147     next unless $go;
148
149     print "Merging reverse path lists for ${suite}...\n";
150
151     open MERGED, "-|", "sort -m $DBDIR/reverse_${suite}_*.txt"
152         or die "Failed to open merged list";
153     open FILENAMES, ">", "$DBDIR/filenames_$suite.txt.new"
154         or die "Failed to open filenames list";
155     tie my %reverse_path_db, "DB_File", "$DBDIR/reverse_${suite}.db.new",
156     O_RDWR|O_CREAT, 0666, $DB_BTREE
157         or die "Error creating DB: $!";
158
159     my $lastpath = my $lastcasepath = my $lastfile = "";
160     my %matches = ();
161     while (<MERGED>) {
162         print "Doing line ".($./1000000)."M (out of approx. 16M)\n"
163             if $. % 1000000 == 0;
164         chomp;
165         my @line = split /\0/o, $_;
166         my $revpath = shift @line;
167         my $casepath = shift @line;
168         if ($revpath ne $lastpath) {
169             # Wrap: Do useful stuff with this ($lastpath, @matches)
170             if ($lastpath ne "") {
171                 my @matches;
172                 while (my ($k, $v) = each %matches) {
173                     push @matches, join("\0", $k, @$v);
174                 }
175                 $reverse_path_db{$lastpath} = join "\1", @matches;
176                 %matches = ();
177             }
178             $lastpath =~ s,/.*,,o;
179             if ($lastfile ne $lastpath) {
180                 $lastfile = $lastpath;
181                 print FILENAMES (reverse $lastfile)."\n";
182             }
183             #
184             $lastpath = $revpath;
185             $lastcasepath = $casepath;
186             $matches{$casepath} = \@line;
187             next;
188 #       } elsif ($lastcasepath ne "" and $casepath ne $lastcasepath) {
189 #           warn reverse($revpath)." has more than one casepath: $casepath $lastcasepath\n";
190         }
191         push @{$matches{$casepath}}, @line;
192     }
193     # Note: do useful stuff here too, for out last entry. Maybe prevent this by
194     # adding a fake ultimate entry?
195     {
196         my @matches;
197         while (my ($k, $v) = each %matches) {
198             push @matches, join("\0", $k, @$v);
199         }
200         $reverse_path_db{$lastpath} = join "\1", @matches;
201     }
202
203     untie %reverse_path_db;
204     close FILENAMES;
205     close MERGED;
206     
207     rename "$DBDIR/filenames_$suite.txt.new", "$DBDIR/filenames_$suite.txt";
208     rename "$DBDIR/reverse_$suite.db.new", "$DBDIR/reverse_$suite.db";
209 }
210
211 # vim: set ts=4