summaryrefslogtreecommitdiffstats
path: root/RPM4/lib/RPM4/Header.pm
blob: fa7e4e571eb9ae60b69ccb54080ffcf0e12f8ba7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
##- Nanar <nanardon@mandrake.org>
##-
##- This program is free software; you can redistribute it and/or modify
##- it under the terms of the GNU General Public License as published by
##- the Free Software Foundation; either version 2, or (at your option)
##- any later version.
##-
##- This program is distributed in the hope that it will be useful,
##- but WITHOUT ANY WARRANTY; without even the implied warranty of
##- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##- GNU General Public License for more details.
##-
##- You should have received a copy of the GNU General Public License
##- along with this program; if not, write to the Free Software
##- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# $Id$

package RPM4::Header;

use strict;
use warnings;
use vars qw($AUTOLOAD);

use RPM4;
use Digest::SHA1;
use Carp;

sub new {
    my ($class, $arg) = @_;
    
    if ($arg) {
	if (ref $arg eq 'GLOB') {
	    return RPM4::stream2header($arg);
	} elsif (-f $arg) {
	    return RPM4::rpm2header($arg);
	} else {
	    croak("Invalid argument $arg");
	}
    } else {
	return RPM4::headernew();
    }
}

# proxify calls to $header->tag()
sub AUTOLOAD {
    my ($header) = @_;

    my $tag = $AUTOLOAD;
    $tag =~ s/.*:://;
    return $header->tag($tag);
}

sub writesynthesis {
    my ($header, $handle, $filestoprovides) = @_;
    $handle ||= *STDOUT;
   
    my $sinfo = $header->synthesisinfo($filestoprovides);
    
    foreach my $deptag (qw(provide conflict obsolete require)) {
        printf($handle '@%ss@%s'."\n", 
            $deptag, 
            join('@', @{$sinfo->{$deptag}})) if (@{$sinfo->{$deptag} || []});
    }
    
    printf($handle '@summary@%s'. "\n",
        $sinfo->{summary},
    );
    printf($handle '@info@%s@%d@%d@%s'."\n",
        $sinfo->{fullname},
        $sinfo->{epoch},
        $sinfo->{size},
        $sinfo->{group},
    );
    return 1;
}

sub synthesisinfo {
    my ($header, $filestoprovides) = @_;
    my $synthinfo = {
        fullname => scalar($header->fullname()),
        summary => $header->tag(1004),
        epoch => $header->tag(1003) || 0,
        size => $header->tag(1009),
        group => $header->tag(1016),
        os => $header->tag('OS'),
        hdrid => pack("H*",$header->tag('HDRID')),
    };


    my @pkgfiles;
    if (my $files = $header->files()) {
        $files->init();
        while($files->next() >= 0) {
            my $f = $files->filename();
            foreach(@{$filestoprovides}) {
                $_ eq $f and do {
                    push @pkgfiles, "$f";
                    last;
                };
            }
        }
    }
    foreach my $deptag (qw(provide conflict obsolete require)) {
        my @deps;
        $deptag eq 'provide' and push(@deps, @pkgfiles);
        if (my $dep = $header->dep(uc($deptag . "name")) || undef) {
            $dep->init();
            while ($dep->next() >= 0) {
                ($dep->flags() & (1 << 24)) and next;
                my @d = $dep->info();
                #$d[1] =~ /^rpmlib\(\S*\)$/ and next;
                push(@deps, sprintf(
                        "%s%s%s",
                        "$d[1]",
                        ($dep->flags() & RPM4::flagvalue('sense', [ 'PREREQ' ])) ? '[*]' : '',
                        $d[2] ? '[' . ($d[2] eq '=' ? '==' : $d[2]) . " $d[3]\]" : '' ));
             }
        }
        
        { my %uniq; @uniq{@deps} = (); @deps = keys(%uniq); }
        push(@{$synthinfo->{$deptag}}, @deps) if(@deps);
    }

    $synthinfo;
}

# return an array of required files
sub requiredfiles {
    my ($header) = @_;
    grep { m:^/: } $header->tag(1049);
}

# is this usefull
# @keeptags can/should be reworks
sub buildlight {
    my ($header, $hinfo) = @_;
    
    {
    my @n = $hinfo->{fullname} =~ m/^(.*)-([^-]*)-([^-]*)\.([^.]*)/;

    $header->addtag(1000, 6, $n[0]); # Name
    $header->addtag(1001, 6, $n[1]); # Version
    $header->addtag(1002, 6, $n[2]); # Release
    if ($n[3] eq 'src') {
        $header->addtag(1022, 6, RPM4::getarchname()); # Arch
    } else {
        $header->addtag(1022, 6, $n[3]);
        $header->addtag(1044, 6, "RPM4-Fake-1-1mdk.src.rpm");    
    }
    }
    $header->addtag(1004, 6, $hinfo->{summary});
    $header->addtag(1003, 4, $hinfo->{epoch}) if ($hinfo->{epoch});
    $header->addtag(1009, 4, $hinfo->{size});
    $header->addtag(1016, 6, $hinfo->{group});
    $header->addtag("OS", 6, $hinfo->{os} ? $hinfo->{os} : RPM4::getosname());

    foreach my $dep (qw(provide require conflict obsolete)) {
        my $deptag = $dep; $deptag = uc($deptag);
        foreach my $entry (@{$hinfo->{$dep} || []}) {
            my ($name, $pre, $fl, $version) = $entry =~ m/([^\[]*)(\[\*\])?(?:\[(\S*)(?:\s*(\S*))?\])?/;
            $fl ||= '';
            $dep eq 'provide' && substr($name, 0, 1) eq '/'  and do {
                $header->addtag('OLDFILENAMES', 8, $name);
                next;
            };
            #print "$deptag . 'NAME', 8, $name\n";
            $header->addtag($deptag . 'NAME', 8, $name);
            $header->addtag($deptag . 'FLAGS', 'INT32', RPM4::flagvalue("sense", $fl || "") | ($pre ? RPM4::flagvalue("sense", [ 'PREREQ' ]) : 0));
            $header->addtag($deptag . 'VERSION', 8, $version || "");
        }
    }
   
    if (!$hinfo->{hdrid}) {
        my $sha = Digest::SHA1->new;

        foreach my $tag ($header->listtag()) {
            $sha->add(join('', $header->tag($tag)));
        }

        $hinfo->{hdrid} = $sha->digest;
    }
    
    $header->addtag("HDRID", "BIN", $hinfo->{hdrid});
}

sub getlight {
    my ($header, $reqfiles) = @_;
    my $hi = RPM4::headernew();
    $hi->buildlight($header->synthesisinfo($reqfiles));
    $hi
}

sub osscore {
    my ($header) = @_;
    my $os = $header->tag("OS");
    defined $os ? RPM4::osscore($os) : 1;
}

sub archscore {
    my ($header) = @_;
    $header->issrc and return 0;
    my $arch = $header->tag("ARCH");
    defined($arch) ? RPM4::archscore($arch) : 1;
}
    
sub is_better_than {
    my ($header, $h) = @_;

    if ($header->tag(1000) eq $h->tag(1000)) {
        my $c = $header->compare($h);
        $c != 0 and return $c;
        return 1 if $header->osscore < $h->osscore;
        return 1 if $header->archscore < $h->archscore;
    } elsif (my $obs = $header->dep('OBSOLETENAME')) {
        $obs->init();
        while ($obs->next >= 0) {
            $obs->name eq $h->tag(1000) or next;
            return 1 if ($obs->matchheadername($h));
        }
    }
    0;
}

sub sourcerpmname {
    $_[0]->queryformat('%|SOURCERPM?{%{SOURCERPM}}:{%{NAME}-%{VERSION}-%{RELEASE}.src.rpm}|')
}

1;

__END__

=head1 NAME

RPM4::Header

=head1 DESCRIPTION

The header contains informations about a rpms, this object give methods
to manipulate its.

=head1 METHODS

=head2 RPM4::Header->new($item)

Create a new C<RPM4::Header> instance from:

=over 4

=item a file

if $item is an rpm file, returns the corresponding object.

=item a file handler

if $item is a file handler, returns an object corresponding to the next header there.

=item nothing

if $item is omitted, returns an empty object.

=back

If data are unreadable for whatever reason, returns undef.

=head2 write(*FILE)

Dump header data into file handle.

Warning: Perl modifier (like PerlIO::Gzip) won't works.

=head2 hsize()

Returns the on-disk size of header data, in bytes.

=head2 copy()

Returns a RPM4::Header object copy.

=head2 removetag(tagid)

Remove tag 'tagid' from header.

=head2 addtag(tagid, tagtype, value1, value2...)

Add a tag into the header:
- tagid is the integervalue of tag
- tagtype is an integer, it identify the tag type to add (see rpmlib headers 
files). Other argument are value to put in tag.

=head2 listtag()

Returns a list of tag id present in header.

=head2 hastag(tagid)

Returns true if tag 'tagid' is present in header.

Ex:
    $header->hastag(1000); # Returns true if tag 'NAME' is present.

=head2 tagtype(tagid)

Returns the tagtype value of tagid. Returns 0 if tagid is not found.

=head2 tag(tagid)

Returns array of tag value for tag 'tagid'.

    $header->tag(1000); # return the name of rpm header.

=head2 queryformat($query)

Make a formated query on the header, macros in I<$query> are evaluated.
This function works like C<rpm --queryformat ...>

    $header->queryformat("%{NAME}-%{VERSION}-%{RELEASE}");

=head2 fullname

In scalar context return the "name-version-version.arch" of package.
In array context return (name, version, release, arch) of package.

=head2 issrc()

Returns true if package is a source package.

=head2 compare(header)

Compare the header to another, return 1 if the object is higher, -1 if
header passed as argument is better, 0 if update is not possible.

=head1 SEE ALSO

L<RPM4>