Compression.pm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2010-2013 Guillem Jover <guillem@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. package Dpkg::Compression;
  17. use strict;
  18. use warnings;
  19. our $VERSION = '1.02';
  20. our @EXPORT = qw(
  21. $compression_re_file_ext
  22. compression_is_supported
  23. compression_get_list
  24. compression_get_property
  25. compression_guess_from_filename
  26. compression_get_file_extension_regex
  27. compression_get_default
  28. compression_set_default
  29. compression_get_default_level
  30. compression_set_default_level
  31. compression_is_valid_level
  32. );
  33. use Exporter qw(import);
  34. use Config;
  35. use Dpkg::ErrorHandling;
  36. use Dpkg::Gettext;
  37. =encoding utf8
  38. =head1 NAME
  39. Dpkg::Compression - simple database of available compression methods
  40. =head1 DESCRIPTION
  41. This modules provides a few public functions and a public regex to
  42. interact with the set of supported compression methods.
  43. =cut
  44. my $COMP = {
  45. gzip => {
  46. file_ext => 'gz',
  47. comp_prog => [ 'gzip', '--no-name' ],
  48. decomp_prog => [ 'gunzip' ],
  49. default_level => 9,
  50. },
  51. bzip2 => {
  52. file_ext => 'bz2',
  53. comp_prog => [ 'bzip2' ],
  54. decomp_prog => [ 'bunzip2' ],
  55. default_level => 9,
  56. },
  57. lzma => {
  58. file_ext => 'lzma',
  59. comp_prog => [ 'xz', '--format=lzma' ],
  60. decomp_prog => [ 'unxz', '--format=lzma' ],
  61. default_level => 6,
  62. },
  63. xz => {
  64. file_ext => 'xz',
  65. comp_prog => [ 'xz' ],
  66. decomp_prog => [ 'unxz' ],
  67. default_level => 6,
  68. },
  69. };
  70. #
  71. # XXX: The gzip package in Debian at some point acquired a Debian-specific
  72. # --rsyncable option via a vendor patch. Which is not present in most of the
  73. # major distributions, dpkg downstream systems, nor gzip upstream, who have
  74. # stated they will most probably not accept it because people should be using
  75. # pigz instead.
  76. #
  77. # This option should have never been accepted in dpkg, ever. But removing it
  78. # now would probably cause demands for tarring and feathering. In addition
  79. # we cannot use the Dpkg::Vendor logic because that would cause circular
  80. # module dependencies. The whole affair is pretty disgusting really.
  81. #
  82. # Check the perl Config to discern Debian and hopefully derivatives too.
  83. #
  84. if ($Config{cf_by} eq 'Debian Project') {
  85. push @{$COMP->{gzip}->{comp_prog}}, '--rsyncable';
  86. }
  87. # XXX: Backwards compatibility, stop exporting on VERSION 2.00.
  88. ## no critic (Variables::ProhibitPackageVars)
  89. our $default_compression = 'xz';
  90. our $default_compression_level = undef;
  91. my $regex = join '|', map { $_->{file_ext} } values %$COMP;
  92. our $compression_re_file_ext = qr/(?:$regex)/;
  93. ## use critic
  94. =head1 FUNCTIONS
  95. =over 4
  96. =item @list = compression_get_list()
  97. Returns a list of supported compression methods (sorted alphabetically).
  98. =cut
  99. sub compression_get_list {
  100. my @list = sort keys %$COMP;
  101. return @list;
  102. }
  103. =item compression_is_supported($comp)
  104. Returns a boolean indicating whether the give compression method is
  105. known and supported.
  106. =cut
  107. sub compression_is_supported {
  108. my $comp = shift;
  109. return exists $COMP->{$comp};
  110. }
  111. =item compression_get_property($comp, $property)
  112. Returns the requested property of the compression method. Returns undef if
  113. either the property or the compression method doesn't exist. Valid
  114. properties currently include "file_ext" for the file extension,
  115. "default_level" for the default compression level,
  116. "comp_prog" for the name of the compression program and "decomp_prog" for
  117. the name of the decompression program.
  118. =cut
  119. sub compression_get_property {
  120. my ($comp, $property) = @_;
  121. return unless compression_is_supported($comp);
  122. return $COMP->{$comp}{$property} if exists $COMP->{$comp}{$property};
  123. return;
  124. }
  125. =item compression_guess_from_filename($filename)
  126. Returns the compression method that is likely used on the indicated
  127. filename based on its file extension.
  128. =cut
  129. sub compression_guess_from_filename {
  130. my $filename = shift;
  131. foreach my $comp (compression_get_list()) {
  132. my $ext = compression_get_property($comp, 'file_ext');
  133. if ($filename =~ /^(.*)\.\Q$ext\E$/) {
  134. return $comp;
  135. }
  136. }
  137. return;
  138. }
  139. =item $regex = compression_get_file_extension_regex()
  140. Returns a regex that matches a file extension of a file compressed with
  141. one of the supported compression methods.
  142. =cut
  143. sub compression_get_file_extension_regex {
  144. return $compression_re_file_ext;
  145. }
  146. =item $comp = compression_get_default()
  147. Return the default compression method. It is "xz" unless
  148. C<compression_set_default> has been used to change it.
  149. =item compression_set_default($comp)
  150. Change the default compression method. Errors out if the
  151. given compression method is not supported.
  152. =cut
  153. sub compression_get_default {
  154. return $default_compression;
  155. }
  156. sub compression_set_default {
  157. my $method = shift;
  158. error(g_('%s is not a supported compression'), $method)
  159. unless compression_is_supported($method);
  160. $default_compression = $method;
  161. }
  162. =item $level = compression_get_default_level()
  163. Return the default compression level used when compressing data. It's "9"
  164. for "gzip" and "bzip2", "6" for "xz" and "lzma", unless
  165. C<compression_set_default_level> has been used to change it.
  166. =item compression_set_default_level($level)
  167. Change the default compression level. Passing undef as the level will
  168. reset it to the compressor specific default, otherwise errors out if the
  169. level is not valid (see C<compression_is_valid_level>).
  170. =cut
  171. sub compression_get_default_level {
  172. if (defined $default_compression_level) {
  173. return $default_compression_level;
  174. } else {
  175. return compression_get_property($default_compression, 'default_level');
  176. }
  177. }
  178. sub compression_set_default_level {
  179. my $level = shift;
  180. error(g_('%s is not a compression level'), $level)
  181. if defined($level) and not compression_is_valid_level($level);
  182. $default_compression_level = $level;
  183. }
  184. =item compression_is_valid_level($level)
  185. Returns a boolean indicating whether $level is a valid compression level
  186. (it must be either a number between 1 and 9 or "fast" or "best")
  187. =cut
  188. sub compression_is_valid_level {
  189. my $level = shift;
  190. return $level =~ /^([1-9]|fast|best)$/;
  191. }
  192. =back
  193. =head1 CHANGES
  194. =head2 Version 1.02 (dpkg 1.17.2)
  195. New function: compression_get_file_extension_regex()
  196. Deprecated variables: $default_compression, $default_compression_level
  197. and $compression_re_file_ext
  198. =head2 Version 1.01 (dpkg 1.16.1)
  199. Default compression level is not global any more, it is per compressor type.
  200. =head2 Version 1.00 (dpkg 1.15.6)
  201. Mark the module as public.
  202. =cut
  203. 1;