summaryrefslogtreecommitdiffstats
path: root/RPM4/lib/RPM4/Index.pm
blob: d804e8bf712d9b963c5dda54941042e2ecad6cc8 (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
##- 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::Index;

use strict;
use warnings;

use RPM4;
use RPM4::Header;
use MDV::Packdrakeng;

use File::Temp qw(tempfile);

sub buildindex {
    my (%options) = @_;
    
    my ($pid, $pack, $h_synthesis, $h_list);
    if ($options{synthesis}) {
        $pid = open($h_synthesis, "| gzip --best > '$options{synthesis}'") or return 0;
    }
    if ($options{hdlist}) {
        $pack = MDV::Packdrakeng->new(
            archive => $options{hdlist},
            comp_level => $options{complevel},
        ) or return 0;
    }
    if ($options{list}) {
        open($h_list, ">", $options{list}) or return 0;
    }

    RPM4::parserpms(
        rpms => $options{rpms},
        callback => sub {
            my (%res) = @_;
            if(defined($options{callback})) {
                $options{callback}->(%res) or return;
            }
            defined($res{header}) or return;
            
            if ($options{synthesis}) {
                $res{header}->writesynthesis($h_synthesis, $options{filestoprovides});
            }

            if ($options{hdlist}) {
                $res{header} or return;
                # Hacking perl-URPM
                my $h = $res{header}->copy(); # Get a copy to not alter original header
                $h->addtag(1000000, 6, scalar($res{header}->fullname()) . '.rpm');
                $h->addtag(1000001, 4, (stat("$res{dir}$res{rpm}"))[7]);
                my $fh = new File::Temp( UNLINK => 1, SUFFIX => '.header');
                $h->write($fh);
                sysseek($fh, 0, 0);
                $pack->add_virtual('f', scalar($res{header}->fullname()), $fh);
                close($fh);
            }
            
            if ($options{list}) {
                print $h_list "$res{rpm}\n";
            }
            
        },
        checkrpms => $options{checkrpms},
        path => $options{path},

    );

    if ($options{synthesis}) {
        close($h_synthesis);
        waitpid $pid, 0;
    }

    if($options{list}) {
        close($h_list);
    }
    1;
}

sub buildsynthesis {
    my (%options) = @_;
    buildindex(%options);
}

# Build only an hdlist file
sub buildhdlist {
    my (%options) = @_;
    buildindex(%options);
}

sub parsehdlist {
    my (%options) = @_;
    my $pack = MDV::Packdrakeng->open(archive => $options{hdlist}) or return 0;

    my (undef, $files, undef) = $pack->getcontent();
    pipe(my $in, my $out);
    if (my $pid = fork()) {
        close($out);
        stream2header($in, 0, sub {
            #printf STDERR $header->fullname ."\n";
            $options{callback}->(
                header => $_[0],
            );
        });
        close($in);
        waitpid($pid, 0);
    } else {
        close($in);
        foreach my $h (@{$options{files} || $files || []}) {
            $pack->extract_virtual($out, $h) >= 0 or die;
        }
        close($out);
        exit;
    }
    1;
}

sub parsesynthesis {
    my (%options) = @_;

    open(my $h, "cat '$options{synthesis}' | gunzip |") or return 0;

    my %hinfo = ();
    while (my $line = <$h>) {
        chomp($line);
        my (undef, $type, $info) = split('@', $line, 3);
        my @infos = split('@', $info);
        if ($type =~ m/^(provides|requires|conflict|obsoletes)$/) {
            @{$hinfo{$type}} = @infos;
        } elsif ($type eq 'summary') {
            $hinfo{summary} = $info;
        } elsif ($type eq 'info') {
            @hinfo{qw(fullname epoch size group)} = @infos;

            my $header = RPM4::headernew();
            $header->buildlight(\%hinfo);
            $options{callback}->(
                header => $header,
            );

            %hinfo = ();
        } else {
        }
    }
    close($h);
    1;
}

1;