~ubuntu-branches/ubuntu/edgy/rpm/edgy

« back to all changes in this revision

Viewing changes to Perl-RPM/RPM/Header.pm

  • Committer: Bazaar Package Importer
  • Author(s): Joey Hess
  • Date: 2002-01-22 20:56:57 UTC
  • Revision ID: james.westby@ubuntu.com-20020122205657-l74j50mr9z8ofcl5
Tags: upstream-4.0.3
ImportĀ upstreamĀ versionĀ 4.0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
###############################################################################
 
2
#
 
3
#   (c) Copyright @ 2000, Randy J. Ray <rjray@blackperl.com>
 
4
#               All Rights Reserved
 
5
#
 
6
###############################################################################
 
7
#
 
8
#   $Id: Header.pm,v 1.20 2001/04/27 09:05:21 rjray Exp $
 
9
#
 
10
#   Description:    The RPM::Header class provides access to the RPM Header
 
11
#                   structure as a tied hash, allowing direct access to the
 
12
#                   tags in a header as keys, and the content of said tags
 
13
#                   as the values.
 
14
#
 
15
#   Functions:      new
 
16
#                   AUTOLOAD
 
17
#                   filenames
 
18
#
 
19
#   Libraries:      None.
 
20
#
 
21
#   Global Consts:  None.
 
22
#
 
23
#   Environment:    Just stuff from the RPM config.
 
24
#
 
25
###############################################################################
 
26
 
 
27
package RPM::Header;
 
28
 
 
29
require 5.005;
 
30
 
 
31
use strict;
 
32
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $AUTOLOAD);
 
33
use subs qw(new AUTOLOAD filenames);
 
34
 
 
35
require Exporter;
 
36
 
 
37
use RPM;
 
38
use RPM::Error;
 
39
use RPM::Constants ':rpmerr';
 
40
 
 
41
$VERSION = do { my @r=(q$Revision: 1.20 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r };
 
42
 
 
43
@ISA = qw(Exporter);
 
44
@EXPORT = ();
 
45
@EXPORT_OK = qw(RPM_HEADER_MASK RPM_HEADER_READONLY);
 
46
%EXPORT_TAGS = (all => \@EXPORT_OK);
 
47
 
 
48
1;
 
49
 
 
50
sub AUTOLOAD
 
51
{
 
52
    my $constname;
 
53
    ($constname = $AUTOLOAD) =~ s/.*:://;
 
54
    die "& not defined" if $constname eq 'constant';
 
55
    my $val = constant($constname);
 
56
    if ($! != 0)
 
57
    {
 
58
        die "Your vendor has not defined RPM macro $constname";
 
59
    }
 
60
    no strict 'refs';
 
61
    *$AUTOLOAD = sub { $val };
 
62
    goto &$AUTOLOAD;
 
63
}
 
64
 
 
65
sub new
 
66
{
 
67
    my $class = shift;
 
68
    my %hash = ();
 
69
 
 
70
    tie %hash, $class, @_;
 
71
}
 
72
 
 
73
###############################################################################
 
74
#
 
75
#   Sub Name:       filenames
 
76
#
 
77
#   Description:    Glue together the contents of BASENAMES, DIRNAMES and
 
78
#                   DIRINDEXES to create a single list of fully-qualified
 
79
#                   filenames
 
80
#
 
81
#   Arguments:      NAME      IN/OUT  TYPE      DESCRIPTION
 
82
#                   $self     in      ref       Object of this class
 
83
#
 
84
#   Globals:        None.
 
85
#
 
86
#   Environment:    None.
 
87
#
 
88
#   Returns:        Success:    listref
 
89
#                   Failure:    undef
 
90
#
 
91
###############################################################################
 
92
sub filenames
 
93
{
 
94
    my $self = shift;
 
95
 
 
96
    # Each of these three fetches will have already triggered rpm_error, were
 
97
    # there any problems.
 
98
    my $base = $self->{BASENAMES}  || return undef;
 
99
    my $dirs = $self->{DIRNAMES}   || return undef;
 
100
    my $idxs = $self->{DIRINDEXES} || return undef;
 
101
 
 
102
    unless (@$base == @$idxs)
 
103
    {
 
104
        rpm_error(RPMERR_INTERNAL,
 
105
                  "Mis-matched elements lists for BASENAMES and DIRINDEXES");
 
106
        return undef;
 
107
    }
 
108
 
 
109
    # The value from DIRNAMES already has the trailing separator
 
110
    [ map { "$dirs->[$idxs->[$_]]$base->[$_]" } (0 .. $#$base) ];
 
111
}
 
112
 
 
113
 
 
114
__END__
 
115
 
 
116
=head1 NAME
 
117
 
 
118
RPM::Header - Access to RPM package headers
 
119
 
 
120
=head1 SYNOPSIS
 
121
 
 
122
    use RPM::Header;
 
123
 
 
124
    tie %hdr, "RPM::Header", "rpm-3.0.4-0.48.i386.rpm" or die "$RPM::err";
 
125
 
 
126
    for (sort keys %hdr)
 
127
    {
 
128
        ...
 
129
    }
 
130
 
 
131
=head1 DESCRIPTION
 
132
 
 
133
The B<RPM::Header> package permits access to the header of a package (external
 
134
file or currently installed) as either a tied hash or a blessed hash reference.
 
135
The tags that are present in the header are expressed as keys. Retrieving
 
136
them via C<keys> or C<each> returns the tags in the order in which they
 
137
appear in the header. Keys may be requested without regard for letter case,
 
138
but they are always returned as all upper-case.
 
139
 
 
140
The return value corresponding to each key is a list reference or scalar
 
141
(or C<undef> if the key is not valid), depending on the data-type of the
 
142
key. Each of the header tags are noted with one of C<$> or C<@> in the
 
143
B<RPM::Constants> documentation. The C<defined> keyword should be used
 
144
for testing success versus failure, as empty tags are possible. See the
 
145
C<scalar_tag> test, below.
 
146
 
 
147
This is a significant change from versions prior to 0.27, in which the
 
148
return value was always a list reference. This new approach brings
 
149
B<RPM::Header> more in line with other interfaces to B<rpm> header information.
 
150
 
 
151
B<RPM::Header> objects are also the native return value from keys retrieved
 
152
in the B<RPM::Database> class (see L<RPM::Database>). In these cases, the
 
153
header data is marked read-only, and attempts to alter any of the keys will
 
154
generate an error.
 
155
 
 
156
There are also a number of class methods implemented, which are described in
 
157
the next section.
 
158
 
 
159
=head1 USAGE
 
160
 
 
161
=head2 Creating an Object
 
162
 
 
163
An object may be created one of two ways:
 
164
 
 
165
    tie %h, "RPM::Header", "filename";
 
166
 
 
167
    $href = new RPM::Header "filename";
 
168
 
 
169
The latter approach offers more direct access to the class methods, while
 
170
also permitting the usual tied-hash operations such as fetching:
 
171
 
 
172
    $href->{tag}    # Such as "name" or "version"
 
173
 
 
174
=head2 Class Methods
 
175
 
 
176
The following methods are available to objects of this class, in addition to
 
177
the tied-hash suite of operations. If the object is a hash instead of a
 
178
hash reference, it can be used to call these methods via:
 
179
 
 
180
    (tied %hash)->method_name(...)
 
181
 
 
182
=over
 
183
 
 
184
=item filenames
 
185
 
 
186
The B<RPM> system attempts to save space by splitting up the file paths into
 
187
the leafs (stored by the tag C<BASENAMES>), the directories (stored under
 
188
C<DIRNAMES>) and indexes into the list of directories (stored under
 
189
C<DIRINDEXES>). As a convenience, this method re-assembles the list of
 
190
filenames and returns it as a list reference. If an error occurs, C<undef>
 
191
will be returned after C<rpm_error> has been called.
 
192
 
 
193
=item is_source
 
194
 
 
195
Returns true (1) or false (0), depending on whether the package the header
 
196
object is derived from is a source RPM.
 
197
 
 
198
=item size
 
199
 
 
200
Return the size of the header, in bytes, within the disk file containing the
 
201
associated package. The value is also returned for those headers within the
 
202
database.
 
203
 
 
204
=item scalar_tag(TAG)
 
205
 
 
206
Returns a true/false value (1 or 0) based on whether the value returned by
 
207
the specified tag is a scalar or an array reference. Useful in place of
 
208
using C<ref> to test the fetched values. B<TAG> may be either a string (name)
 
209
or a number (imported from the B<RPM::Constants> tag C<:rpmtag>). This
 
210
method may be called as a class (static) method.
 
211
 
 
212
=item tagtype(TAG)
 
213
 
 
214
Given a tag I<TAG>, return the type as a numerical value. The valid types
 
215
can be imported from the B<RPM::Constants> package via the import-tag
 
216
":rpmtype", and are:
 
217
 
 
218
=over
 
219
 
 
220
=item RPM_NULL_TYPE
 
221
 
 
222
Used internally by B<rpm>.
 
223
 
 
224
=item RPM_BIN_TYPE
 
225
 
 
226
The data returned is a single chunk of binary data. It has been converted to
 
227
a single "string" in Perl terms, but may contain nulls within it. The
 
228
B<length()> keyword should return the correct size for the chunk.
 
229
 
 
230
=item RPM_CHAR_TYPE
 
231
 
 
232
All items are single-character in nature. Note that since Perl does not
 
233
distinguish single characters versus strings in the way that C does, these
 
234
are stored as single-character strings. It is a tradeoff of efficiency over
 
235
memory.
 
236
 
 
237
=item RPM_INT8_TYPE
 
238
 
 
239
All items are integers no larger than 8 bits wide.
 
240
 
 
241
=item RPM_INT16_TYPE
 
242
 
 
243
All items are integers no larger than 16 bits wide.
 
244
 
 
245
=item RPM_INT32_TYPE
 
246
 
 
247
All items are integers no larger than 32 bits wide.
 
248
 
 
249
=item RPM_STRING_TYPE
 
250
 
 
251
=item RPM_I18NSTRING_TYPE
 
252
 
 
253
=item RPM_STRING_ARRAY_TYPE
 
254
 
 
255
These three are functionally similar from the Perl perspective. Currently,
 
256
B<RPM> does not export data in an I18N format, it will have been converted to
 
257
an ordinary string before being handed to the caller (in this case, before
 
258
the internal Perl/XS code receives it). The distinction between STRING and
 
259
STRING_ARRAY types is only relevant internally. All of these are sequences of
 
260
one or more text strings, returned in the same internal order as they are
 
261
stored within the header.
 
262
 
 
263
=back
 
264
 
 
265
=item NVR
 
266
 
 
267
The commonly-needed data triple of (B<name>, B<version>, B<release>) may be
 
268
accessed more directly by means of this method. It returns the three values
 
269
on the stack, saving the need for three separate fetches.
 
270
 
 
271
=item cmpver(OTHER)
 
272
 
 
273
Compare the version of the current header against that in the header
 
274
referenced by C<$other>. The argument should be an object reference, not
 
275
a tied-hash representation of a header. Returns -1, 0 or 1, based on the
 
276
established behavior of other comparison operators (C<cmp> and C<E<lt>=E<gt>>);
 
277
-1 indicates that the calling object is considered less, or older, than the
 
278
passed argument. A value of 1 indicates that the calling object is greater,
 
279
or newer, than the argument. A value of 0 indicates that they are equal.
 
280
 
 
281
=item source_name
 
282
 
 
283
If the B<RPM::Header> object is created directly from a file, FTP source
 
284
or HTTP source, then that source is kept for future reference and may be
 
285
retrieved using this accessor. This will be an empty string if the header
 
286
was retrieved from the RPM database, or was built in-memory from data.
 
287
 
 
288
=back
 
289
 
 
290
=head1 DIAGNOSTICS
 
291
 
 
292
Direct binding to the internal error-management of B<rpm> is still under
 
293
development. At present, most operations generate their diagnostics to
 
294
STDERR.
 
295
 
 
296
=head1 CAVEATS
 
297
 
 
298
This is currently regarded as alpha-quality software. The interface is
 
299
subject to change in future releases.
 
300
 
 
301
=head1 SEE ALSO
 
302
 
 
303
L<RPM>, L<RPM::Database>, L<RPM::Constants>, L<perl>, L<rpm>
 
304
 
 
305
=head1 AUTHOR
 
306
 
 
307
Randy J. Ray <rjray@blackperl.com>
 
308
 
 
309
=cut