summaryrefslogtreecommitdiff
path: root/scripts/Dpkg/Compression/Process.pm
blob: 7eb5653c761c23dcfa6aea89d1610222f84ed78d (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
# Copyright © 2008-2010 Raphaël Hertzog <hertzog@debian.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 of the License, 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, see <http://www.gnu.org/licenses/>.

package Dpkg::Compression::Process;

use strict;
use warnings;

use Dpkg::Compression;
use Dpkg::Gettext;
use Dpkg::IPC;
use Dpkg::ErrorHandling;

use POSIX;

# Object methods
sub new {
    my ($this, %args) = @_;
    my $class = ref($this) || $this;
    my $self = {};
    bless $self, $class;
    $self->set_compression($args{"compression"} || compression_get_default());
    $self->set_compression_level($args{"compression_level"} ||
	    compression_get_default_level());
    return $self;
}

sub set_compression {
    my ($self, $method) = @_;
    error(_g("%s is not a supported compression method"), $method)
	    unless compression_is_supported($method);
    $self->{"compression"} = $method;
}

sub set_compression_level {
    my ($self, $level) = @_;
    error(_g("%s is not a compression level"), $level)
	    unless compression_is_valid_level($level);
    $self->{"compression_level"} = $level;
}

sub get_compress_cmdline {
    my ($self) = @_;
    my @prog = (compression_get_property($self->{"compression"}, "comp_prog"));
    my $level = "-" . $self->{"compression_level"};
    $level = "--" . $self->{"compression_level"}
	    if $self->{"compression_level"} =~ m/best|fast/;
    push @prog, $level;
    return @prog;
}

sub get_uncompress_cmdline {
    my ($self) = @_;
    return (compression_get_property($self->{"compression"}, "decomp_prog"));
}

sub _sanity_check {
    my ($self, %opts) = @_;
    # Check for proper cleaning before new start
    error(_g("Dpkg::Compression::Process can only start one subprocess at a time"))
	    if $self->{"pid"};
    # Check options
    my $to = my $from = 0;
    foreach (qw(file handle string pipe)) {
        $to++ if $opts{"to_$_"};
        $from++ if $opts{"from_$_"};
    }
    internerr("exactly one to_* parameter is needed") if $to != 1;
    internerr("exactly one from_* parameter is needed") if $from != 1;
    return %opts;
}

sub compress {
    my $self = shift;
    my %opts = $self->_sanity_check(@_);
    my @prog = $self->get_compress_cmdline();
    $opts{"exec"} = \@prog;
    $self->{"cmdline"} = "@prog";
    $self->{"pid"} = fork_and_exec(%opts);
    delete $self->{"pid"} if $opts{"to_string"}; # wait_child already done
}

sub uncompress {
    my $self = shift;
    my %opts = $self->_sanity_check(@_);
    my @prog = $self->get_uncompress_cmdline();
    $opts{"exec"} = \@prog;
    $self->{"cmdline"} = "@prog";
    $self->{"pid"} = fork_and_exec(%opts);
    delete $self->{"pid"} if $opts{"to_string"}; # wait_child already done
}

sub wait_end_process {
    my ($self, %opts) = @_;
    $opts{"cmdline"} ||= $self->{"cmdline"};
    wait_child($self->{"pid"}, %opts) if $self->{'pid'};
    delete $self->{"pid"};
    delete $self->{"cmdline"};
}

1;