source: web/trunk/lib/Sophie/Base/RpmsPath.pm @ 6

Last change on this file since 6 was 6, checked in by nanardon, 14 years ago
  • add base updater daemon
File size: 4.6 KB
Line 
1package Sophie::Base::RpmsPath;
2
3use strict;
4use warnings;
5use base qw(Sophie::Base);
6use Sophie::Base::Header;
7use RPM4;
8use File::Temp;
9use File::Copy;
10use Archive::Cpio;
11use Encode::Guess;
12use Encode;
13
14sub new {
15    my ($class, $pathkey) = @_;
16
17    bless(\$pathkey, $class);
18}
19
20sub path {
21    my ($self) = @_;
22   
23    my $sth = $self->db->prepare_cached(
24        q{select path from d_path where d_path_key = ?}
25    );
26    $sth->execute($$self);
27    my $res = $sth->fetchrow_hashref;
28    $sth->finish;
29    return $res->{path}
30}
31
32sub ls_rpms {
33    my ($self) = @_;
34
35    my $sth = $self->db->prepare_cached(
36        q{select * from rpmfiles where d_path = ?}
37    );
38    $sth->execute($$self);
39    $sth->fetchall_hashref([ 'filename' ]);
40}
41
42sub local_ls_rpms {
43    my ($self) = @_;
44
45    if (opendir(my $dh, $self->path)) {
46        my %list;
47        while (my $entry = readdir($dh)) {
48            $entry eq '.' and next;
49            $entry eq '..' and next;
50            $list{$entry} = 1;
51        }
52        closedir($dh);
53        return \%list;
54    } else {
55        return;
56    }
57}
58
59sub find_delta {
60    my ($self) = @_;
61
62    warn $self->path;
63
64    my $localrpms = $self->local_ls_rpms || {};
65    my $baserpms  = $self->ls_rpms;
66
67    my %list;
68    foreach (keys %{ $localrpms }, keys %{ $baserpms }) {
69        $list{$_} = 1;
70    }
71    my @delta;
72
73    foreach my $rpm (sort { $b cmp $a } keys %list) {
74        if ($localrpms->{$rpm} && $baserpms->{$rpm}) {
75            # nothing to do
76        } elsif ($localrpms->{$rpm}) {
77            push(@delta, { rpm => $rpm, delta => 'A' });
78        } elsif ($baserpms->{$rpm}) {
79            push(@delta, { rpm => $rpm, delta => 'R' });
80        }
81    }
82    @delta;
83}
84sub update_content {
85    my ($self, @delta) = @_;
86    foreach (@delta) {
87        if (!$_->{delta}) {
88        }
89        elsif ($_->{delta} eq 'A') {
90            $self->add_rpm($_->{rpm});
91        }
92        elsif ($_->{delta} eq 'R') {
93            $self->remove_rpm($_->{rpm});
94        }
95    }
96}
97
98sub remove_rpm {
99    my ($self, $rpm) = @_;
100    my $remove = $self->db->prepare_cached(
101        q{
102        DELETE FROM rpmfiles where d_path = ? and filename = ?
103        }
104    );
105    $remove->execute($$self, $rpm);
106    warn "deleting $rpm";
107    $self->db->commit;
108}
109
110sub add_rpm {
111    my ($self, $rpm) = @_;
112
113    if (my $pkgid = $self->_add_header($rpm)) {
114        my $register = $self->db->prepare_cached(
115            q{
116            INSERT INTO rpmfiles (d_path, filename, pkgid)
117            values (?,?,?)
118            }
119        );
120        $register->execute($$self, $rpm, $pkgid);
121
122    } else {
123    }
124    warn "adding $rpm";
125    $self->db->commit;
126}
127
128sub _add_header {
129    my ($self, $rpm) = @_;
130
131    my $header;
132    eval {
133        $header = RPM4::Header->new($self->path . '/' . $rpm) 
134    };
135    $header or do {
136        warn "Cannot read " . $self->path . '/' . $rpm;
137        return;
138    };
139
140    {
141        my $find = $self->db->prepare_cached(q{
142            select pkgid from rpms where pkgid = ?
143        });
144        $find->execute($header->queryformat('%{PKGID}'));
145        my $rows = $find->rows;
146        $find->finish;
147        if ($rows) {
148            warn "Find";
149            return $header->queryformat('%{PKGID}');
150        }
151    }
152    my $tmp = File::Temp->new( UNLINK => 1, SUFFIX => '.hdr' );
153    unlink($tmp->filename);
154    $header->write($tmp, 0);
155    seek($tmp, 0, 0);
156    my $string = '';
157    while (read($tmp, my $str, 1024)) { $string .= $str }
158    $tmp = undef;
159    my $add_header = $self->db->prepare_cached(
160        q{
161        INSERT into rpms (pkgid, header, evr, issrc, description, summary)
162        values (?,rpmheader_in(decode(?, 'hex')::bytea),?,?,?,?)
163        }
164    );
165    my $description = $header->queryformat('%{DESCRIPTION}');
166    {
167        my $enc = guess_encoding($description, qw/latin1/);
168        $description = $enc->decode($description) if ($enc && ref $enc);
169    }
170    my $summary = $header->queryformat('%{SUMMARY}');
171    {
172        my $enc = guess_encoding($summary, qw/latin1/);
173        $summary = $enc->decode($summary) if ($enc && ref $enc);
174    }
175
176    $add_header->execute(
177        $header->queryformat('%{PKGID}'),
178        unpack('H*', $string),
179        $header->queryformat('%|EPOCH?{%{EPOCH}:}:{}|%{VERSION}-%{RELEASE}'),
180        $header->hastag('SOURCERPM') ? 'f' : 't',
181        $description,
182        $summary,
183    );
184    my $index_tag = $self->db->prepare_cached(
185        q{
186        select index_rpms(?);
187        }
188    );
189    $index_tag->execute($header->queryformat('%{PKGID}'));
190    $index_tag->finish;
191    Sophie::Base::Header->new($header->queryformat('%{PKGID}'))
192        ->addfiles_content({ path => $self->path, filename => $rpm});
193
194    $header->queryformat('%{PKGID}');
195}
196
1971;
Note: See TracBrowser for help on using the repository browser.