source: server/trunk/web/lib/Sophie/Base/Header.pm @ 114

Last change on this file since 114 was 42, checked in by nanardon, 14 years ago
  • add dump/load distrib config using Yaml
  • separate DB connection for session
File size: 3.0 KB
Line 
1package Sophie::Base::Header;
2
3use strict;
4use warnings;
5use base qw(Sophie::Base);
6use File::Temp;
7use File::Copy;
8use Archive::Cpio;
9use Encode::Guess;
10use Encode;
11
12sub new {
13    my ($class, $pkgid) = @_;
14
15    bless({ key => $pkgid }, $class);
16}
17
18sub key { $_[0]->{key} }
19
20sub rpm_path {
21    my ($self) = @_;
22
23    my $listrpm = $self->db->prepare_cached(
24        q{
25        select * from rpmfile join rpmspath
26        on rpmfile.path = rpmspath.path
27        where pkgid = ?
28        }
29    );
30    $listrpm->execute($self->key);
31
32    return $listrpm->fetchall_hashref({});
33}
34
35sub addfiles_content {
36    my ($self, $rpm) = @_;
37
38    my $tmp = File::Temp->new( UNLINK => 1, SUFFIX => '.cpio' );
39    unlink($tmp->filename);
40    my $ok = 0;
41    foreach ($rpm ? ($rpm) : (@{ $self->rpm_path })) {
42        if (open(my $cpioh, "rpm2cpio " . quotemeta($_->{path} . '/' .
43                    $_->{filename}) . " |")) {
44        File::Copy::copy($cpioh, $tmp);
45        close($cpioh);
46        $ok = 1;
47        last;
48        }
49    }
50    $ok or return;
51
52    my $list_file = $self->db->prepare_cached(q{
53        select (rpmqueryfiles(header)).* from rpms where pkgid = ?
54    });
55    $list_file->execute($self->key);
56
57    my $files = $list_file->fetchall_hashref([ 'dirname', 'basename' ]);
58
59    my $add_content = $self->db->prepare_cached(
60        q{
61        UPDATE allfiles set contents = ? where pkgid = ? and count = ?
62        }
63    );
64    seek($tmp, 0, 0);
65    my $cpio = Archive::Cpio->new();
66    eval {
67        $cpio->read_with_handler(
68            $tmp,
69            sub {
70                my ($file) = @_;
71                my ($dirname, $basename) = $file->name =~ /^(?:\.(.*\/))?([^\/]+)$/;
72                $dirname ||= '';
73                my $entry = $files->{$dirname}{$basename};
74                for (1) {
75                    $entry->{size} > 1024 * 1024 and return 1;
76                    # Spec files and patch
77                    $entry->{flags} == 32 and last;
78                    $dirname eq '' and last;
79                    # Doc files
80                    $entry->{flags} & (1 << 1) || $entry->{flags} & (1 << 8)
81                        and last;
82                    # Config file
83                    $entry->{flags}  & (1 << 0) and last;
84                    $basename =~ /\.h$/ and last;
85
86                    return 1;
87                }
88                my $content = $file->get_content;
89                my $enc = guess_encoding($content, qw/latin1/);
90                if ($enc && ref $enc) {
91                    $content = $enc->decode($content);
92
93                    $self->db->pg_savepoint('FILECONTENT');
94                    $add_content->execute(
95                        $enc && ref $enc ? encode('utf8', $enc->decode($content)) : $content,
96                        $self->key,
97                        $entry->{count}) or do {
98                        $self->db->pg_rollback_to('FILECONTENT');
99                    };
100                } else {
101                }
102                1;
103            }
104        );
105    };
106
107    $tmp = undef;
108    return 1;
109}
110
1111;
Note: See TracBrowser for help on using the repository browser.