]> git.deb.at Git - deb/packages.git/blob - bin/parse-contents
Comment clean-up: Remove $Id$, fix FSF address, copyright years
[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 lib './lib';
22
23 $| = 1;
24
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?
27 $ENV{"LC_ALL"} = 'C';
28
29 my $what = $ARGV[0] ? "head -10000|" : "";
30
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;
35
36 use DB_File;
37 use Storable;
38 use File::Path;
39 use Packages::Config qw( $TOPDIR $DBDIR @ARCHIVES @SUITES @ARCHITECTURES );
40 &Packages::Config::init( './' );
41
42 my @archives = @ARCHIVES;
43 my @suites = @SUITES;
44 my @archs = @ARCHITECTURES;
45
46 $DBDIR .= "/contents";
47 -d $DBDIR || mkpath( $DBDIR );
48
49 for my $suite (@suites) {
50     for my $arch (@archs) {
51
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 = ();
57         
58         my $extra = "";
59         $extra = "|sort" if $SORT_REVERSE_CONCURRENTLY;
60         
61         open REVERSED, "$extra>$DBDIR/reverse.tmp"
62             or die "Failed to open output reverse file: $!";
63
64         my $changed = 0;
65         for my $archive (@archives) { 
66
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";
73             $changed++;
74         }
75         if ($changed) {
76             for my $archive (@archives) { 
77
78                 my $filename = "$TOPDIR/archive/$archive/$suite/Contents-$arch.gz";
79                 next unless -f $filename;
80                 print "Reading $archive/$suite/$arch...\n";
81                 
82                 open CONT, "zcat $filename|$what"
83                     or die $!;
84                 while (<CONT>) {last if /^FILE/mo;}
85                 open CONT, "zcat $filename|$what" if eof(CONT);
86                 while (<CONT>) {
87                     my $data = "";
88                     my %data = ();
89                     chomp;
90                     print "Doing line ".($./1000)."k (out of approx 1.5M)\n" if $. % 250000 == 0;
91                     /^(.+?)\s+(\S+)$/o;
92                     my ($file, $value) = ($1, $2);
93                     $value =~ s#[^,/]+/##og;
94                     my @packages = split /,/, $value;
95                     for (@packages) {
96                         $packages_contents_nr{$_}++;
97                         my $lw = $packages_contents_lastword{$_} || "\0";
98                         my $i=0;
99                         while (substr($file,$i,1) eq substr($lw,$i++,1)) {}
100                         $i--;
101                         $i = 255 if $i > 255;
102                         $packages_contents{$_} .= pack "CC/a*", ($i, substr($file, $i));
103                         $packages_contents_lastword{$_} = "$file\0";
104                     }
105                     # Searches are case-insensitive
106                     $file =~ tr [A-Z] [a-z];
107                     
108                     print REVERSED (reverse $file)."\0".(join ":$arch\0", @packages).":$arch\n";
109                 }
110                 close CONT;
111                 
112             }
113             close REVERSED;
114             
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;
119             
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})
126                     . $v;
127             }
128             untie %packages_contents_db;
129         
130             rename("$DBDIR/reverse.tmp", "$DBDIR/reverse_${suite}_${arch}.txt");
131         
132             rename("$filelist_db.new", $filelist_db);
133             system("ln -sf $filelist_db $DBDIR/filelists_${suite}_all.db") == 0
134                 or die "Oops";
135         }
136     }
137                           
138     my $go = 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];
143     }
144     next unless $go;
145
146     print "Merging reverse path lists for ${suite}...\n";
147
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: $!";
155
156     my $lastpath = "";
157     my $lastfile = "";
158     my @matches = ();
159     while (<MERGED>) {
160         print "Doing line ".($./1000000)."M (out of approx. 16M)\n"
161             if $. % 1000000 == 0;
162         chomp;
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";
172             }
173             #
174             $lastpath = $revpath;
175             @matches = @line;
176             next;
177         }
178         push @matches, @line
179         }
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;
183     
184     untie %reverse_path_db;
185     close FILENAMES;
186     close MERGED;
187     
188     rename "$DBDIR/filenames_$suite.txt.new", "$DBDIR/filenames_$suite.txt";
189     rename "$DBDIR/reverse_$suite.db.new", "$DBDIR/reverse_$suite.db";
190 }
191
192 # vim: set ts=4