Dpkg/Compression.pm

filter: Statementstmt 90.9% 80 / 88 Branchbran 52.9% 18 / 34 Conditioncond 53.3% 8 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 95.6% 22 / 23 Podpod 100% 16 / 16 totaltotal 77.8% 144 / 185 CC 24 SCAR 14.2
CC
24
Cov
77%
compression_set_threads
12.0
compression_get_cmdline_compress
9.7
compression_get_level
4.3
CRAP 4.2
« Dpkg/Gettext.pm ↑ Summary Dpkg/Source/Package/V3/Custom.pm »
1 # Copyright © 2007-2022 Guillem Jover <guillem@debian.org>
2 # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <https://www.gnu.org/licenses/>.
16  
17 =encoding utf8
18  
19 =head1 NAME
20  
21 Dpkg::Compression - simple database of available compression methods
22  
23 =head1 DESCRIPTION
24  
25 This modules provides a few public functions and a public regex to
26 interact with the set of supported compression methods.
27  
28 =cut
29  
30 package Dpkg::Compression 3.00;
31  
32 24 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 22 our @EXPORT = qw(
35 compression_is_supported
36 compression_get_list
37 compression_guess_from_filename
38 compression_get_file_extension_regex
39 compression_get_file_extension
40 compression_get_default
41 compression_set_default
42 compression_get_default_level
43 compression_set_default_level
44 compression_get_level
45 compression_set_level
46 compression_is_valid_level
47 compression_get_threads
48 compression_set_threads
49 compression_get_cmdline_compress
50 compression_get_cmdline_decompress
51 );
52  
53 24 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
54 24 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 24 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
56  
57 24 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 24 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 22 my %COMP = (
61 gzip => {
62 file_ext => 'gz',
63 comp_prog => [ 'gzip', '-n' ],
64 decomp_prog => [ 'gunzip' ],
65 default_level => 9,
66 },
67 bzip2 => {
68 file_ext => 'bz2',
69 comp_prog => [ 'bzip2' ],
70 decomp_prog => [ 'bunzip2' ],
71 default_level => 9,
72 },
73 lzma => {
74 file_ext => 'lzma',
75 comp_prog => [ 'xz', '--format=lzma' ],
76 decomp_prog => [ 'unxz', '--format=lzma' ],
77 default_level => 6,
78 },
79 xz => {
80 file_ext => 'xz',
81 comp_prog => [ 'xz' ],
82 decomp_prog => [ 'unxz', '--format=xz' ],
83 default_level => 6,
84 },
85 );
86  
87 22 my $default_compression = 'xz';
88 22 my $default_compression_level = undef;
89 22 my $default_compression_threads = 0;
90  
91 =head1 FUNCTIONS
92  
93 =over 4
94  
95 =item @list = compression_get_list()
96  
97 Returns a list of supported compression methods (sorted alphabetically).
98  
99 =cut
100  
101 221 ▶sub compression_get_list {
Subroutine compression_get_list: called CC=1 SCAR=0.0
Pod documented
102 221 my @list = sort keys %COMP;
103 221 return @list;
104 }
105  
106 =item $bool = compression_is_supported($comp)
107  
108 Returns a boolean indicating whether the give compression method is
109 known and supported.
110  
111 =cut
112  
113 498 ▶sub compression_is_supported {
Subroutine compression_is_supported: called CC=1 SCAR=0.0
Pod documented
114 498 my $comp = shift;
115  
116 498 return exists $COMP{$comp};
117 }
118  
119 =item $comp = compression_guess_from_filename($filename)
120  
121 Returns the compression method that is likely used on the indicated
122 filename based on its file extension.
123  
124 =cut
125  
126 216 ▶sub compression_guess_from_filename {
Subroutine compression_guess_from_filename: called CC=3 SCAR=11.0
Pod documented
127 216 my $filename = shift;
128 216 foreach my $comp (compression_get_list()) {
129 806 my $ext = $COMP{$comp}{file_ext};
130 806 ▶ if ($filename =~ /^(.*)\.\Q$ext\E$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($filename =~ /^(.*)\.\Q$ext\E$/u) 34 772 806
131 34 return $comp;
132 }
133 }
134 182 return;
135 }
136  
137 =item $regex = compression_get_file_extension_regex()
138  
139 Returns a regex that matches a file extension of a file compressed with
140 one of the supported compression methods.
141  
142 =cut
143  
144 188 ▶sub compression_get_file_extension_regex {
Subroutine compression_get_file_extension_regex: called CC=1 SCAR=0.0
Pod documented
145 188 my $regex_file_ext_alts = join '|', map { $_->{file_ext} } values %COMP;
146 188 my $regex_file_ext = qr/(?:$regex_file_ext_alts)/;
147  
148 188 return $regex_file_ext;
149 }
150  
151 =item $ext = compression_get_file_extension($comp)
152  
153 Return the file extension for the compressor $comp.
154  
155 =cut
156  
157 20 ▶sub compression_get_file_extension {
Subroutine compression_get_file_extension: called CC=2 SCAR=7.1
Pod documented
158 20 my $comp = shift;
159  
160 20 ▶ error(g_('%s is not a supported compression'), $comp)
Branch: 1 branch
BranchTrueFalseTotal
unless compression_is_supported($comp) 0 20 20
161 unless compression_is_supported($comp);
162  
163 20 return $COMP{$comp}{file_ext};
164 }
165  
166 =item $comp = compression_get_default()
167  
168 Return the default compression method. It is "xz" unless
169 compression_set_default() has been used to change it.
170  
171 =cut
172  
173 203 ▶sub compression_get_default {
Subroutine compression_get_default: called CC=1 SCAR=0.0
Pod documented
174 203 return $default_compression;
175 }
176  
177 =item compression_set_default($comp)
178  
179 Change the default compression method. Errors out if the
180 given compression method is not supported.
181  
182 =cut
183  
184 15 ▶sub compression_set_default {
Subroutine compression_set_default: called CC=2 SCAR=6.9
Pod documented
185 15 my $method = shift;
186 15 ▶ error(g_('%s is not a supported compression'), $method)
Branch: 1 branch
BranchTrueFalseTotal
unless compression_is_supported($method) 5 10 15
187 unless compression_is_supported($method);
188 10 $default_compression = $method;
189 }
190  
191 =item $level = compression_get_default_level()
192  
193 Return the global default compression level used when compressing data if
194 it has been set, otherwise the default level for the default compressor.
195  
196 It's "9" for "gzip" and "bzip2", "6" for "xz" and "lzma", unless
197 compression_set_default_level() has been used to change it.
198  
199 =cut
200  
201 203 ▶sub compression_get_default_level {
Subroutine compression_get_default_level: called CC=2 SCAR=6.9
Pod documented
202 203 ▶ if (defined $default_compression_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $default_compression_level) { } 10 193 203
203 10 return $default_compression_level;
204 } else {
205 193 return $COMP{$default_compression}{default_level};
206 }
207 }
208  
209 =item compression_set_default_level($level)
210  
211 Change the global default compression level. Passing undef as the level will
212 reset it to the global default compressor specific default, otherwise errors
213 out if the level is not valid (see compression_is_valid_level()).
214  
215 =cut
216  
217 15 ▶sub compression_set_default_level {
Subroutine compression_set_default_level: called CC=3 SCAR=11.4
Pod documented
218 15 my $level = shift;
219 15 ▶ error(g_('%s is not a compression level'), $level)
Branch: 1 branch
BranchTrueFalseTotal
if defined $level and not compression_is_valid_level($level) 0 15 15
Condition66%2/3
defined $level and not compression_is_valid_level($level)
!ll&&!rl&&r
5100
MC/DC0%0/2
defined $level and not compression_is_valid_level($level)
defined $level not compression_is_valid_level($level)
Truth table66%2/3
A and not(B)
A= defined $levelB= not compression_is_valid_level($level)
ABresult
0X0
100
111
220 if defined($level) and not compression_is_valid_level($level);
221 15 $default_compression_level = $level;
222 }
223  
224 =item $level = compression_get_level($comp)
225  
226 Return the compression level used when compressing data with a specific
227 compressor. The value returned is the specific compression level if it has
228 been set, otherwise the global default compression level if it has been set,
229 falling back to the specific default compression level.
230  
231 =cut
232  
233 39 ▶sub compression_get_level {
Subroutine compression_get_level: called CC=4 SCAR=14.6
Pod documented
234 39 my $comp = shift;
235  
236 39 ▶ error(g_('%s is not a supported compression'), $comp)
Branch: 1 branch
BranchTrueFalseTotal
unless compression_is_supported($comp) 0 39 39
237 unless compression_is_supported($comp);
238  
239 return $COMP{$comp}{level} //
240 $default_compression_level //
241 39 ▶ $COMP{$comp}{default_level};
Condition66%4/6
($COMP{$comp}{"level"} // $default_compression_level) // $COMP{$comp}{"default_level"}
l!l&&r!l&&!r
5340
$COMP{$comp}{"level"} // $default_compression_level
l!l&&r!l&&!r
5034
MC/DC0%0/3
($COMP{$comp}{"level"} // $default_compression_level) // $COMP{$comp}{"default_level"}
$COMP{$comp}{"level"} $default_compression_level $COMP{$comp}{"default_level"}
Truth table50%2/4
A // B // C
A= $COMP{$comp}{"level"}B= $default_compression_levelC= $COMP{$comp}{"default_level"}
ABCresult
1XX1
01X1
0011
0000
242 }
243  
244 =item compression_set_level($comp, $level)
245  
246 Change the compression level for a specific compressor. Passing undef as
247 the level will reset it to the specific default compressor level, otherwise
248 errors out if the level is not valid (see compression_is_valid_level()).
249  
250 =cut
251  
252 193 ▶sub compression_set_level {
Subroutine compression_set_level: called CC=4 SCAR=14.6
Pod documented
253 193 my ($comp, $level) = @_;
254  
255 193 ▶ error(g_('%s is not a supported compression'), $comp)
Branch: 1 branch
BranchTrueFalseTotal
unless compression_is_supported($comp) 0 193 193
256 unless compression_is_supported($comp);
257 193 ▶ error(g_('%s is not a compression level'), $level)
Branch: 1 branch
BranchTrueFalseTotal
if defined $level and not compression_is_valid_level($level) 0 193 193
Condition66%2/3
defined $level and not compression_is_valid_level($level)
!ll&&!rl&&r
51880
MC/DC0%0/2
defined $level and not compression_is_valid_level($level)
defined $level not compression_is_valid_level($level)
Truth table66%2/3
A and not(B)
A= defined $levelB= not compression_is_valid_level($level)
ABresult
0X0
100
111
258 if defined $level && ! compression_is_valid_level($level);
259  
260 193 $COMP{$comp}{level} = $level;
261 }
262  
263 =item $bool = compression_is_valid_level($level)
264  
265 Returns a boolean indicating whether $level is a valid compression level
266 (it must be either a number between 1 and 9 or "fast" or "best")
267  
268 =cut
269  
270 228 ▶sub compression_is_valid_level {
Subroutine compression_is_valid_level: called CC=1 SCAR=0.0
Pod documented
271 228 my $level = shift;
272 228 return $level =~ /^([1-9]|fast|best)$/;
273 }
274  
275 =item $threads = compression_get_threads()
276  
277 Return the number of threads to use for compression and decompression.
278  
279 =cut
280  
281 14 ▶sub compression_get_threads {
Subroutine compression_get_threads: called CC=1 SCAR=0.0
Pod documented
282 14 return $default_compression_threads;
283 }
284  
285 =item compression_set_threads($threads)
286  
287 Change the threads to use for compression and decompression. Passing C<undef>
288 or B<0> requests to use automatic mode, based on the current CPU cores on
289 the system.
290  
291 =cut
292  
293 0 ▶sub compression_set_threads {
Subroutine compression_set_threads: not called CC=3 SCAR=24.8
Pod documented
294 0 my $threads = shift;
295  
296 0 ▶ error(g_('compression threads %s is not a number'), $threads)
Branch: 1 branch
BranchTrueFalseTotal
if defined $threads and not $threads =~ /^\d+$/u 0 0 0
Condition0%0/3
defined $threads and not $threads =~ /^\d+$/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $threads and not $threads =~ /^\d+$/u
defined $threads not $threads =~ /^\d+$/u
Truth table0%0/3
A and not(B)
A= defined $threadsB= not $threads =~ /^\d+$/u
ABresult
0X0
100
111
297 if defined $threads && $threads !~ m/^\d+$/;
298 0 $default_compression_threads = $threads;
299 }
300  
301 =item @exec = compression_get_cmdline_compress($comp)
302  
303 Returns a list ready to be passed to exec(), its first element is the
304 program name for compression and the following elements are parameters
305 for the program.
306  
307 When executed the program will act as a filter between its standard input
308 and its standard output.
309  
310 =cut
311  
312 9 ▶sub compression_get_cmdline_compress {
Subroutine compression_get_cmdline_compress: called CC=7 SCAR=22.7
Pod documented
313 9 my $comp = shift;
314  
315 9 ▶ error(g_('%s is not a supported compression'), $comp)
Branch: 1 branch
BranchTrueFalseTotal
unless compression_is_supported($comp) 0 9 9
316 unless compression_is_supported($comp);
317  
318 9 my @prog = @{$COMP{$comp}{comp_prog}};
319 9 my $level = compression_get_level($comp);
320 9 ▶ if ($level =~ m/^[1-9]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level =~ /^[1-9]$/u) { } 9 0 9
321 9 push @prog, "-$level";
322 } else {
323 0 push @prog, "--$level";
324 }
325 9 my $threads = compression_get_threads();
326 9 ▶ if ($comp eq 'xz') {
Branch: 2 branches
BranchTrueFalseTotal
if ($comp eq 'xz') { } 0 9 9
elsif ($comp eq 'gzip') { } 9 0 9
327 # Do not generate warnings when adjusting memory usage, nor
328 # exit with non-zero due to those not emitted warnings.
329 0 push @prog, qw(--quiet --no-warn);
330  
331 # Do not let xz fallback to single-threaded mode, to avoid
332 # non-reproducible output.
333 0 push @prog, '--no-adjust';
334  
335 # The xz -T1 option selects a single-threaded mode which generates
336 # different output than in multi-threaded mode. To avoid the
337 # non-reproducible output we pass -T+1 (supported with xz >= 5.4.0)
338 # to request multi-threaded mode with a single thread.
339 0 ▶ push @prog, $threads == 1 ? '-T+1' : "-T$threads";
Branch: 1 branch
BranchTrueFalseTotal
$threads == 1 ? : 0 0 0
340 } elsif ($comp eq 'gzip') {
341 # The gzip --rsyncable option is not universally supported, so we need
342 # to conditionally use it. Ideally we would invoke 'gzip --help' and
343 # check whether the option is supported, but that would imply forking
344 # and executing that process for any caller, which is not ideal
345 # performance-wise. Instead we will approximate it by osname, which is
346 # not ideal either, but better than nothing.
347 #
348 # Requires GNU gzip >= 1.7 for the --rsyncable option. On AIX GNU gzip
349 # is too old. On the BSDs they use their own implementation based on
350 # zlib, which does not currently support the --rsyncable option.
351 9 ▶ if (any { $Config{osname} eq $_ } qw(linux gnu solaris)) {
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $Dpkg::Compression::Config{"osname"} eq $_; } , "linux", "gnu", "solaris")) 9 0 9
352 9 push @prog, '--rsyncable';
353 }
354 }
355  
356 9 return @prog;
357 }
358  
359 =item @exec = compression_get_cmdline_decompress($comp)
360  
361 Returns a list ready to be passed to exec(), its first element is the
362 program name for decompression and the following elements are parameters
363 for the program.
364  
365 When executed the program will act as a filter between its standard input
366 and its standard output.
367  
368 =cut
369  
370 5 ▶sub compression_get_cmdline_decompress {
Subroutine compression_get_cmdline_decompress: called CC=3 SCAR=11.4
Pod documented
371 5 my $comp = shift;
372  
373 5 ▶ error(g_('%s is not a supported compression'), $comp)
Branch: 1 branch
BranchTrueFalseTotal
unless compression_is_supported($comp) 0 5 5
374 unless compression_is_supported($comp);
375  
376 5 my @prog = @{$COMP{$comp}{decomp_prog}};
377  
378 5 my $threads = compression_get_threads();
379 5 ▶ if ($comp eq 'xz') {
Branch: 1 branch
BranchTrueFalseTotal
if ($comp eq "xz") 0 5 5
380 0 push @prog, "-T$threads";
381 }
382  
383 5 return @prog;
384 }
385  
386 =back
387  
388 =head1 CHANGES
389  
390 =head2 Version 3.00 (dpkg 1.23.6)
391  
392 Remove function: compression_get_property().
393  
394 =head2 Version 2.01 (dpkg 1.21.14)
395  
396 New functions: compression_get_file_extension(), compression_get_level(),
397 compression_set_level(), compression_get_cmdline_compress(),
398 compression_get_cmdline_decompress(), compression_get_threads() and
399 compression_set_threads().
400  
401 Deprecated functions: compression_get_property().
402  
403 =head2 Version 2.00 (dpkg 1.20.0)
404  
405 Hide variables: $default_compression, $default_compression_level
406 and $compression_re_file_ext.
407  
408 =head2 Version 1.02 (dpkg 1.17.2)
409  
410 New function: compression_get_file_extension_regex()
411  
412 Deprecated variables: $default_compression, $default_compression_level
413 and $compression_re_file_ext
414  
415 =head2 Version 1.01 (dpkg 1.16.1)
416  
417 Default compression level is not global any more, it is per compressor type.
418  
419 =head2 Version 1.00 (dpkg 1.15.6)
420  
421 Mark the module as public.
422  
423 =cut
424  
425 22 1;
« Dpkg/Gettext.pm ↑ Summary Dpkg/Source/Package/V3/Custom.pm »