Subversion Repositories

?revision_form?Rev ?revision_input??revision_submit??revision_endform?

Rev 1 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 magnus 1
package DebPool::DB;
2
 
3
###
4
#
5
# DebPool::DB - Module for managing data hashes via tied NDBM files
6
#
7
# Copyright 2003-2004 Joel Aelwyn. All rights reserved.
8
# 
9
# Redistribution and use in source and binary forms, with or without
10
# modification, are permitted provided that the following conditions
11
# are met:
12
# 1. Redistributions of source code must retain the above copyright
13
#    notice, this list of conditions and the following disclaimer.
14
# 2. Redistributions in binary form must reproduce the above copyright
15
#    notice, this list of conditions and the following disclaimer in the
16
#    documentation and/or other materials provided with the distribution.
17
# 3. Neither the name of the Author nor the names of any contributors
18
#    may be used to endorse or promote products derived from this software
19
#    without specific prior written permission.
20
# 
21
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
# SUCH DAMAGE.
32
#
33
# $Id: DB.pm 62 2005-02-23 18:02:38Z joel $
34
#
35
###
36
 
37
# We use 'our', so we must have at least Perl 5.6
38
 
39
require 5.006_000;
40
 
41
# Always good ideas.
42
 
43
use strict;
44
use warnings;
45
 
46
# This module mostly wraps calls to tied NDBM hashes, so we need these.
47
 
48
use Fcntl;
49
use NDBM_File;
50
 
51
### Module setup
52
 
53
BEGIN {
54
    use Exporter ();
55
    our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
56
 
57
    # Version checking
58
    $VERSION = '0.1.5';
59
 
60
    @ISA = qw(Exporter);
61
 
62
    @EXPORT = qw(
63
    );
64
 
65
    @EXPORT_OK = qw(
66
        %ComponentDB
67
        &Open_Databases
68
        &Close_Databases
69
        &Get_Version
70
        &Set_Versions
71
    );
72
 
73
    %EXPORT_TAGS = (
74
        'functions' => [qw(&Open_Databases &Close_Databases &Get_Version
75
                           &Set_Versions)],
76
        'vars' => [qw(%ComponentDB)],
77
    );
78
}
79
 
80
### Exported package globals
81
 
82
# I'd love to be able to do this as a hash of hashes of hashrefs, but the
83
# database layer can't handle it. So we have multiple DBs.
84
 
85
# VersionDB - hash of tied hashes, keyed on Distribution (then Source
86
# package). Keeps track of all versions. Prior to 0.2.2, the value pointed
87
# to was a scalar representing the version of the source package; as of
88
# 0.2.2 and later, updated records are hashrefs pointing to hashes that
89
# have package -> version mappings, with 'source' being the key for source
90
# package version.
91
 
92
our(%VersionDB);
93
 
94
# ComponentDB - hash of tied hashes, keyed on Distribution (then Source
95
# package). Stores the component data for the given package.
96
 
97
our(%ComponentDB);
98
 
99
### Non-exported package globals
100
 
101
# Thread-safe? What's that? Package global error value. We don't export
102
# this directly, because it would conflict with other modules.
103
 
104
our($Error);
105
 
106
### File lexicals
107
 
108
# None
109
 
110
### Constant functions
111
 
112
# None
113
 
114
### Meaningful functions
115
 
116
# Open_Databases()
117
#
118
# Open all tied NDBM hashes for each real distribution. Returns 0 in the
119
# case of errors opening hashes, 1 otherwise.
120
 
121
sub Open_Databases {
122
    use DebPool::Config qw(:vars);
123
 
124
    my($db_dir) = $Options{'db_dir'};
125
    my($db_file_mode) = $Options{'db_file_mode'};
126
    my($dist);
127
 
128
    foreach $dist (@{$Options{'realdists'}}) {
129
        my(%tiedhash);
130
        my($tie_result) = tie(%tiedhash, 'NDBM_File',
131
                              "$db_dir/${dist}_version",
132
                              O_RDWR|O_CREAT, $db_file_mode);
133
        if (!defined($tie_result)) {
134
            return 0;
135
        };
136
 
137
        $VersionDB{$dist} = \%tiedhash;
138
    }
139
 
140
    foreach $dist (@{$Options{'realdists'}}) {
141
        my(%tiedhash);
142
        my($tie_result) = tie(%tiedhash, 'NDBM_File',
143
                              "$db_dir/${dist}_component",
144
                              O_RDWR|O_CREAT, $db_file_mode);
145
        if (!defined($tie_result)) {
146
            return 0;
147
        };
148
 
149
        $ComponentDB{$dist} = \%tiedhash;
150
    }
151
 
152
    return 1;
153
}
154
 
155
# Close_Databases()
156
#
157
# Closes all tied NDBM hashes.
158
#
159
# NOTE: Untie doesn't return anything (?), so we can't really trap errors.
160
 
161
sub Close_Databases {
162
    my($dist);
163
 
164
    foreach $dist (keys(%VersionDB)) {
165
        untie(%{$VersionDB{$dist}});
166
    }
167
 
168
    foreach $dist (keys(%ComponentDB)) {
169
        untie(%{$ComponentDB{$dist}});
170
    }
171
 
172
    return 1;
173
}
174
 
175
# Get_Version($dist, $source, $package)
176
#
177
# Retrieves the version of $package (from source package $source) in
178
# distribution $dist. The package name 'source' retrieves the source
179
# package name, or undef if no information is available.
180
 
181
sub Get_Version {
182
    my($dist, $source, $package) = @_;
183
 
184
    my($temp) = $VersionDB{$dist}->{$source};
185
    if (!defined($temp)) { return undef; }
186
 
187
    # Versions prior to 0.2.2 had only one entry, which is the source
188
    # version; since this is the same as the binary version on the vast
189
    # majority of packages, fake an answer. This works because hash entries
190
    # are guaranteed to be non-empty.
191
 
192
    if ($temp !~ m/\|/) {
193
        return $temp;
194
    }
195
 
196
    if ('meta' eq $package) {
197
        $temp =~ s/\|.*//;
198
        return $temp;
199
    } elsif ('source' eq $package) {
200
        return $VersionDB{$dist}->{"source_${source}"};
201
    } else {
202
        return $VersionDB{$dist}->{"binary_${source}_${package}"};
203
    }
204
}
205
 
206
# Set_Versions($dist, $source, $file_arrayref
207
 
208
sub Set_Versions {
209
    my($dist, $source, $meta_version, $file_arrayref) = @_;
210
 
211
    my($oldbinlist) = $VersionDB{$dist}->{$source};
212
    if (defined($oldbinlist) && ($oldbinlist =~ m/\|/)) { # 0.2.2 or later
213
        $oldbinlist =~ s/.*\|//; # Strip meta version
214
        my(@oldbins) = split(/,/,$oldbinlist);
215
 
216
        my($oldbin);
217
        foreach $oldbin (@oldbins) {
218
            $VersionDB{$dist}->{"binary_${source}_${oldbin}"} = undef;
219
        }
220
 
221
        $VersionDB{$dist}->{"source_${source}"} = undef;
222
        $VersionDB{$dist}->{"${source}"} = undef;
223
    }
224
 
225
    # Walk through each file looking for version data. Note that only the
226
    # .dsc file is guaranteed to be the same for source uploads (it can be
227
    # orig.tar.gz or tar.gz, and diff.gz need not exist), and .deb files
228
    # have binary versions, so that's all we look for.
229
    #
230
    # FIXME: Do udeb files have different versions from deb files?
231
 
232
    my(@files) = @{$file_arrayref};
233
    my(@entries) = ();
234
 
235
    my($hashref);
236
    foreach $hashref (@files) {
237
        my($filename) = $hashref->{'Filename'};
238
 
239
        if ($filename =~ m/^([^_]+)_([^_]+)_.+\.deb/) {
240
            my($package) = $1;
241
            my($version) = $2;
242
 
243
            $VersionDB{$dist}->{"binary_${source}_${package}"} = $version;
244
            push(@entries, $package);
245
        } elsif ($filename =~ m/^[^_]+_([^_]+)\.dsc/) {
246
            my($version) = $1;
247
 
248
            $VersionDB{$dist}->{"source_${source}"} = $version;
249
            push(@entries, 'source');
250
        } # else skip
251
    }
252
 
253
    $VersionDB{$dist}->{$source} = "${meta_version}|" . join(',', @entries);
254
}
255
 
256
END {}
257
 
258
1;
259
 
260
__END__
261
 
262
# vim:set tabstop=4 expandtab: