2 # debbugs-rebuild-index.db is part of debbugs, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2012 by Don Armstrong <don@donarmstrong.com>.
11 use Getopt::Long qw(:config no_ignore_case);
16 debbugs-rebuild-index.db -- rebuild Debbug's index.db
20 debbugs-rebuild-index.db [options]
23 --spool-dir debbugs spool directory
24 --debug, -d debugging level (Default 0)
25 --help, -h display this help
26 --man, -m display manual
34 Debbugs spool directory; defaults to the value configured in the
35 debbugs configuration file.
43 Display brief useage information.
53 Rebuild the index.db for db-h.
55 debbugs-rebuild-index.db;
57 Rebuild the index.db for archive
59 debbugs-rebuild-index.db archive;
67 use Debbugs::Common qw(checkpid lockpid get_hashname getparsedaddrs getbugcomponent make_list);
68 use Debbugs::Config qw(:config);
69 use Debbugs::Status qw(read_bug split_status_fields generate_index_db_line);
71 my %options = (debug => 0,
85 'spool_dir|spool-dir=s',
86 'debug|d+','help|h|?','man|m');
88 pod2usage() if $options{help};
89 pod2usage({verbose=>2}) if $options{man};
91 $DEBUG = $options{debug};
94 $options{verbose} = $options{verbose} - $options{quiet};
96 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
98 if (exists $options{spool_dir} and defined $options{spool_dir}) {
99 $config{spool_dir} = $options{spool_dir};
101 chdir($config{spool_dir}) or die "chdir $config{spool_dir} failed: $!";
103 my $verbose = $options{debug};
105 my $initialdir = "db-h";
107 if (defined $ARGV[0] and $ARGV[0] eq "archive") {
108 $initialdir = "archive";
111 if (not lockpid($config{spool_dir}.'/lock/debbugs-rebuild-index.db')) {
112 print STDERR "Another debbugs-rebuild-index.db is running; stopping\n";
116 my $fh_type = $initialdir;
117 # if initaldir is db-h, the file is db.
118 $fh_type = 'db' if $initialdir eq 'db-h';
120 my $file = "index.${fh_type}.realtime";
121 my $idx_rebuild = IO::File->new($file.'.rebuild','w')
122 or die "Couldn't open ${file}.rebuild: $!";
125 my @dirs = ($initialdir);
128 while (my $dir = shift @dirs) {
129 printf "Doing dir %s ...\n", $dir if $verbose;
131 opendir(DIR, "$dir/.") or die "opendir $dir: $!";
132 my @subdirs = readdir(DIR);
135 my @list = map { m/^(\d+)\.summary$/?($1):() } @subdirs;
136 push @dirs, map { m/^(\d+)$/ && -d "$dir/$1"?("$dir/$1"):() } @subdirs;
138 for my $bug (@list) {
139 print "Up to $cnt bugs...\n" if (++$cnt % 100 == 0 && $verbose);
140 my $stat = stat(getbugcomponent($bug,'summary',$initialdir));
141 if (not defined $stat) {
142 print STDERR "Unable to stat $bug $!\n";
145 my $data = read_bug(bug => $bug,
146 location => $initialdir);
147 my $line = generate_index_db_line($data);
151 binmode($idx_rebuild,':raw:encoding(UTF-8)');
152 print {$idx_rebuild} $bugs{$_} foreach sort {$a <=> $b} keys %bugs;
154 rename("$file.rebuild", $file);