~ubuntu-branches/ubuntu/trusty/dlocate/trusty

« back to all changes in this revision

Viewing changes to update-dlocatedb

  • Committer: Bazaar Package Importer
  • Author(s): Craig Sanders
  • Date: 2009-06-03 11:24:02 UTC
  • mfrom: (3.1.1 karmic)
  • Revision ID: james.westby@ubuntu.com-20090603112402-glc1211vgmee8yel
Tags: 1.02
* fixed example in dlocate.1 man page again (Closes: #466662)
* added '-k', and '-K' commands to list kernels and related packages
* added optional support for gzip compression of /var/lib/dlocate/dlocatedb.txt
* added support output filters (--package-only, --filename-only) (Closes: #531641)

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
# caching of .list timestamps and re-use of old locatedb data added by 
9
9
# Pawel Chmielowski <prefiks@prefiks.org>, see Bug #457572
10
10
 
 
11
# 2009-05-30   Craig Sanders <cas@taz.net.au>
 
12
# - rewrote script to output a plain text listing rather than use frcode
 
13
#
 
14
# 2009-06-03 # Craig Sanders <cas@taz.net.au>
 
15
# - added optional support for compressing /var/lib/dlocatedb
 
16
 
11
17
use strict;
12
18
use File::Basename;
13
19
 
14
20
my $program = basename($0);
15
21
 
16
22
my $dbfile='/var/lib/dlocate/dlocatedb';
17
 
my $stampsfile="$dbfile.stamps";
18
 
my $frcode='/usr/lib/locate/frcode';
 
23
my $stampsfile='/var/lib/dlocate/dlocatedb.stamps';
 
24
#my $dbfile='/tmp/dlocate';
 
25
#my $stampsfile='/tmp/dlocate.stamps';
19
26
my $infodir='/var/lib/dpkg/info';
20
27
 
 
28
my $compress=0;
 
29
my $defaults='/etc/default/dlocate';
 
30
if (open(DEFAULTS,'<', $defaults)) {
 
31
    while (<DEFAULTS>) {
 
32
        chomp;
 
33
        s/#.*|^\s*|\s*$//g;
 
34
        next if (/^$/);
 
35
        s/\s|"//g;
 
36
        my ($key,$val) = split /=/;
 
37
        if ($key eq 'COMPRESS_DLOCATE') {
 
38
            $compress = $val;
 
39
        };
 
40
    };
 
41
    close(DEFAULTS);
 
42
};
 
43
 
21
44
my (%old_stamps, %stamps);
22
45
 
23
46
if (open(STAMPS, '<', $stampsfile)) {
29
52
    close(STAMPS);
30
53
}
31
54
 
32
 
open(FRCODE,"|$frcode >$dbfile.new") or die "$program: couldn't open pipe to $frcode: $!\n";
 
55
open(DBFILE,'>', "$dbfile.new") or die "$program: couldn't open $dbfile.new for write: $!\n";
33
56
 
34
57
opendir(DIR, $infodir) or die "$program: can't open directory $infodir: $!\n";
35
58
while (defined(my $pkg = readdir(DIR))) {
40
63
 
41
64
my @new_pkgs;
42
65
my %processed;
43
 
my $locate = '/usr/bin/locate.findutils';
44
 
 
45
 
if (not -x $locate) {
46
 
    # slocate or mlocate diverts locate
47
 
    $locate = `/usr/sbin/dpkg-divert --truename /usr/bin/locate`;
48
 
    chomp $locate;
49
 
}
50
66
 
51
67
chdir $infodir;
52
 
if (%old_stamps and open(DB, "$locate -d $dbfile '' 2>/dev/null |")) {
 
68
if (%old_stamps and open(DB, '<', $dbfile)) {
53
69
    while (<DB>) {
54
70
        my ($pkg) = /^(\S+?):/;
55
71
        if (not exists $stamps{$pkg}) {
56
 
            # skip packages which are not longer installed
 
72
            # skip packages which are no longer installed
57
73
        } elsif (exists $old_stamps{$pkg} and $stamps{$pkg} == $old_stamps{$pkg}) {
58
 
            print FRCODE $_;
 
74
            print DBFILE $_;
59
75
        } elsif (not exists $processed{$pkg}) {
60
76
            open(FILE, "$pkg.list") or die "$program: can't open file $pkg.list: $!\n";
61
77
            foreach (<FILE>) {
62
 
                print FRCODE "$pkg: $_";
 
78
                print DBFILE "$pkg: $_";
63
79
            }
64
80
            close FILE;
65
81
        }
75
91
}
76
92
 
77
93
foreach my $pkg (@new_pkgs) {
78
 
    open(FILE, '<', "$pkg.list") or die "$program: can't open file $pkg: $!\n";
 
94
    open(FILE, '<', "$pkg.list") or die "$program: can't open $pkg for read: $!\n";
79
95
    foreach (<FILE>) {
80
 
        print FRCODE $pkg, ': ', $_;
 
96
        print DBFILE $pkg, ': ', $_;
81
97
    }
82
98
    close FILE;
83
99
}
84
 
close FRCODE;
 
100
 
 
101
# append diversions info to dlocatedb
 
102
my $divfile = '/var/lib/dpkg/diversions';
 
103
 
 
104
open(DIVERSIONS,"<",$divfile) or die "$program: can't open $divfile for read: $!\n";
 
105
while (my $from = <DIVERSIONS>) {
 
106
  my $to = <DIVERSIONS>;
 
107
  my $pkg = <DIVERSIONS>;
 
108
  chomp($from, $to, $pkg);
 
109
  print DBFILE "diversion by $pkg from: $from\n";
 
110
  print DBFILE "diversion by $pkg to: $to\n";
 
111
}
 
112
close(DIVERSIONS);
 
113
 
 
114
 
 
115
close DBFILE;
85
116
 
86
117
# Create a backup to the database before replacing it with the new database.
87
118
# This is effectively two rename's done atomically.
92
123
 
93
124
rename("$dbfile.new", $dbfile);
94
125
 
 
126
# optionally compress dlocatedb
 
127
if ($compress eq "1") {
 
128
    system('gzip','--quiet','--force',$dbfile);
 
129
    rename("$dbfile.gz", $dbfile);
 
130
};
 
131
 
95
132
open(STAMPS, '>', "$stampsfile.new") or die "$program: can't create stamps file $stampsfile.new: $!\n";
96
133
print STAMPS "$stamps{$_}:$_\n" for keys %stamps;
97
134
close STAMPS;
98
135
 
99
 
unlink($stampsfile);
 
136
if (-e $stampsfile) {
 
137
  unlink($stampsfile);
 
138
};
100
139
rename("$stampsfile.new", $stampsfile);
101
140