Compression.pm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. use Dpkg::ErrorHandling;
  21. use Dpkg::Gettext;
  22. use Exporter qw(import);
  23. our @EXPORT = qw($compression_re_file_ext compression_get_list
  24. compression_is_supported compression_get_property
  25. compression_guess_from_filename
  26. compression_get_file_extension_regex
  27. compression_get_default compression_set_default
  28. compression_get_default_level
  29. compression_set_default_level
  30. compression_is_valid_level);
  31. =encoding utf8
  32. =head1 NAME
  33. Dpkg::Compression - simple database of available compression methods
  34. =head1 DESCRIPTION
  35. This modules provides a few public funcions and a public regex to
  36. interact with the set of supported compression methods.
  37. =cut
  38. my $COMP = {
  39. gzip => {
  40. file_ext => 'gz',
  41. comp_prog => [ 'gzip', '--no-name', '--rsyncable' ],
  42. decomp_prog => [ 'gunzip' ],
  43. default_level => 9,
  44. },
  45. bzip2 => {
  46. file_ext => 'bz2',
  47. comp_prog => [ 'bzip2' ],
  48. decomp_prog => [ 'bunzip2' ],
  49. default_level => 9,
  50. },
  51. lzma => {
  52. file_ext => 'lzma',
  53. comp_prog => [ 'xz', '--format=lzma' ],
  54. decomp_prog => [ 'unxz', '--format=lzma' ],
  55. default_level => 6,
  56. },
  57. xz => {
  58. file_ext => 'xz',
  59. comp_prog => [ 'xz' ],
  60. decomp_prog => [ 'unxz' ],
  61. default_level => 6,
  62. },
  63. };
  64. # XXX: Backwards compatibility, stop exporting on VERSION 2.00.
  65. ## no critic (Variables::ProhibitPackageVars)
  66. our $default_compression = 'xz';
  67. our $default_compression_level = undef;
  68. my $regex = join '|', map { $_->{file_ext} } values %$COMP;
  69. our $compression_re_file_ext = qr/(?:$regex)/;
  70. ## use critic
  71. =head1 FUNCTIONS
  72. =over 4
  73. =item my @list = compression_get_list()
  74. Returns a list of supported compression methods (sorted alphabetically).
  75. =cut
  76. sub compression_get_list {
  77. my @list = sort keys %$COMP;
  78. return @list;
  79. }
  80. =item compression_is_supported($comp)
  81. Returns a boolean indicating whether the give compression method is
  82. known and supported.
  83. =cut
  84. sub compression_is_supported {
  85. my $comp = shift;
  86. return exists $COMP->{$comp};
  87. }
  88. =item compression_get_property($comp, $property)
  89. Returns the requested property of the compression method. Returns undef if
  90. either the property or the compression method doesn't exist. Valid
  91. properties currently include "file_ext" for the file extension,
  92. "default_level" for the default compression level,
  93. "comp_prog" for the name of the compression program and "decomp_prog" for
  94. the name of the decompression program.
  95. =cut
  96. sub compression_get_property {
  97. my ($comp, $property) = @_;
  98. return unless compression_is_supported($comp);
  99. return $COMP->{$comp}{$property} if exists $COMP->{$comp}{$property};
  100. return;
  101. }
  102. =item compression_guess_from_filename($filename)
  103. Returns the compression method that is likely used on the indicated
  104. filename based on its file extension.
  105. =cut
  106. sub compression_guess_from_filename {
  107. my $filename = shift;
  108. foreach my $comp (compression_get_list()) {
  109. my $ext = compression_get_property($comp, 'file_ext');
  110. if ($filename =~ /^(.*)\.\Q$ext\E$/) {
  111. return $comp;
  112. }
  113. }
  114. return;
  115. }
  116. =item my $regex = compression_get_file_extension_regex()
  117. Returns a regex that matches a file extension of a file compressed with
  118. one of the supported compression methods.
  119. =cut
  120. sub compression_get_file_extension_regex {
  121. return $compression_re_file_ext;
  122. }
  123. =item my $comp = compression_get_default()
  124. Return the default compression method. It's "gzip" unless
  125. C<compression_set_default> has been used to change it.
  126. =item compression_set_default($comp)
  127. Change the default compression method. Errors out if the
  128. given compression method is not supported.
  129. =cut
  130. sub compression_get_default {
  131. return $default_compression;
  132. }
  133. sub compression_set_default {
  134. my ($method) = @_;
  135. error(_g('%s is not a supported compression'), $method)
  136. unless compression_is_supported($method);
  137. $default_compression = $method;
  138. }
  139. =item my $level = compression_get_default_level()
  140. Return the default compression level used when compressing data. It's "9"
  141. for "gzip" and "bzip2", "6" for "xz" and "lzma", unless
  142. C<compression_set_default_level> has been used to change it.
  143. =item compression_set_default_level($level)
  144. Change the default compression level. Passing undef as the level will
  145. reset it to the compressor specific default, otherwise errors out if the
  146. level is not valid (see C<compression_is_valid_level>).
  147. =cut
  148. sub compression_get_default_level {
  149. if (defined $default_compression_level) {
  150. return $default_compression_level;
  151. } else {
  152. return compression_get_property($default_compression, 'default_level');
  153. }
  154. }
  155. sub compression_set_default_level {
  156. my ($level) = @_;
  157. error(_g('%s is not a compression level'), $level)
  158. if defined($level) and not compression_is_valid_level($level);
  159. $default_compression_level = $level;
  160. }
  161. =item compression_is_valid_level($level)
  162. Returns a boolean indicating whether $level is a valid compression level
  163. (it must be either a number between 1 and 9 or "fast" or "best")
  164. =cut
  165. sub compression_is_valid_level {
  166. my ($level) = @_;
  167. return $level =~ /^([1-9]|fast|best)$/;
  168. }
  169. =back
  170. =head1 CHANGES
  171. =head2 Version 1.02
  172. New function: compression_get_file_extension_regex()
  173. Deprecated variables: $default_compression, $default_compression_level
  174. and $compression_re_file_ext
  175. =head2 Version 1.01
  176. Default compression level is not global any more, it is per compressor type.
  177. =head2 Version 1.00
  178. Mark the module as public.
  179. =head1 AUTHOR
  180. Raphaël Hertzog <hertzog@debian.org>.
  181. =cut
  182. 1;