summaryrefslogtreecommitdiffstats
path: root/lib/Devscripts/Compression.pm
blob: c1389481b0c6e1d7ce96b5a3f7a60522af68dff6 (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
# Copyright James McCoy <jamessan@debian.org> 2013.
# Modifications copyright 2002 Julian Gilbey <jdg@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 <https://www.gnu.org/licenses/>.

package Devscripts::Compression;

use Dpkg::Compression qw(!compression_get_property);
use Dpkg::IPC;
use Exporter qw(import);

our @EXPORT = (
    @Dpkg::Compression::EXPORT,
    qw(compression_get_file_extension_regex compression_guess_from_file),
);

eval {
    Dpkg::Compression->VERSION(1.02);
    1;
} or do {
    # Ensure we have compression_get_file_extension_regex, regardless of the
    # version of Dpkg::Compression to ease backporting.
    *{'Devscripts::Compression::compression_get_file_extension_regex'} = sub {
        return $compression_re_file_ext;
    };
};

# This can potentially be moved to Dpkg::Compression

my %mime2comp = (
    "application/x-gzip"       => "gzip",
    "application/gzip"         => "gzip",
    "application/x-bzip2"      => "bzip2",
    "application/bzip2  "      => "bzip2",
    "application/x-xz"         => "xz",
    "application/xz"           => "xz",
    "application/zip"          => "zip",
    "application/x-compress"   => "compress",
    "application/java-archive" => "zip",
    "application/x-tar"        => "tar",
    "application/zstd"         => "zst",
    "application/x-zstd"       => "zst",
);

sub compression_guess_from_file {
    my $filename = shift;
    my $mimetype;
    spawn(
        exec => ['file', '--dereference', '--brief', '--mime-type', $filename],
        to_string  => \$mimetype,
        wait_child => 1
    );
    chomp($mimetype);
    if (exists $mime2comp{$mimetype}) {
        return $mime2comp{$mimetype};
    } else {
        return;
    }
}

# comp_prog and default_level aren't provided because a) they aren't needed in
# devscripts and b) the Dpkg::Compression API isn't rich enough to support
# these as compressors
my %comp_properties = (
    compress => {
        file_ext    => 'Z',
        decomp_prog => ['uncompress'],
    },
    zip => {
        file_ext    => 'zip',
        decomp_prog => ['unzip'],
    },
    zst => {
        file_ext => 'zst',
        #comp_prog     => ['zstd'],
        decomp_prog   => ['unzstd'],
        default_level => 3,
    });

sub compression_get_property {
    my ($compression, $property) = @_;
    if (!exists $comp_properties{$compression}) {
        return Dpkg::Compression::compression_get_property($compression,
            $property);
    }

    if (exists $comp_properties{$compression}{$property}) {
        return $comp_properties{$compression}{$property};
    }
    return;
}

1;