Compression.pm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. # Copyright © 2010 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Compression;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '1.02';
  19. use Dpkg::ErrorHandling;
  20. use Dpkg::Gettext;
  21. use Exporter qw(import);
  22. our @EXPORT = qw($compression_re_file_ext compression_get_list
  23. compression_is_supported compression_get_property
  24. compression_guess_from_filename
  25. compression_get_default compression_set_default
  26. compression_get_default_level
  27. compression_set_default_level
  28. compression_is_valid_level);
  29. =encoding utf8
  30. =head1 NAME
  31. Dpkg::Compression - simple database of available compression methods
  32. =head1 DESCRIPTION
  33. This modules provides a few public funcions and a public regex to
  34. interact with the set of supported compression methods.
  35. =head1 EXPORTED VARIABLES
  36. =over 4
  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 = 'gzip';
  67. our $default_compression_level = undef;
  68. ## use critic
  69. =item $compression_re_file_ext
  70. A regex that matches a file extension of a file compressed with one of the
  71. supported compression methods.
  72. =back
  73. =cut
  74. my $regex = join '|', map { $_->{file_ext} } values %$COMP;
  75. our $compression_re_file_ext = qr/(?:$regex)/;
  76. =head1 EXPORTED FUNCTIONS
  77. =over 4
  78. =item my @list = compression_get_list()
  79. Returns a list of supported compression methods (sorted alphabetically).
  80. =cut
  81. sub compression_get_list {
  82. my @list = sort keys %$COMP;
  83. return @list;
  84. }
  85. =item compression_is_supported($comp)
  86. Returns a boolean indicating whether the give compression method is
  87. known and supported.
  88. =cut
  89. sub compression_is_supported {
  90. return exists $COMP->{$_[0]};
  91. }
  92. =item compression_get_property($comp, $property)
  93. Returns the requested property of the compression method. Returns undef if
  94. either the property or the compression method doesn't exist. Valid
  95. properties currently include "file_ext" for the file extension,
  96. "default_level" for the default compression level,
  97. "comp_prog" for the name of the compression program and "decomp_prog" for
  98. the name of the decompression program.
  99. =cut
  100. sub compression_get_property {
  101. my ($comp, $property) = @_;
  102. return unless compression_is_supported($comp);
  103. return $COMP->{$comp}{$property} if exists $COMP->{$comp}{$property};
  104. return;
  105. }
  106. =item compression_guess_from_filename($filename)
  107. Returns the compression method that is likely used on the indicated
  108. filename based on its file extension.
  109. =cut
  110. sub compression_guess_from_filename {
  111. my $filename = shift;
  112. foreach my $comp (compression_get_list()) {
  113. my $ext = compression_get_property($comp, 'file_ext');
  114. if ($filename =~ /^(.*)\.\Q$ext\E$/) {
  115. return $comp;
  116. }
  117. }
  118. return;
  119. }
  120. =item my $comp = compression_get_default()
  121. Return the default compression method. It's "gzip" unless
  122. C<compression_set_default> has been used to change it.
  123. =item compression_set_default($comp)
  124. Change the default compression method. Errors out if the
  125. given compression method is not supported.
  126. =cut
  127. sub compression_get_default {
  128. return $default_compression;
  129. }
  130. sub compression_set_default {
  131. my ($method) = @_;
  132. error(_g('%s is not a supported compression'), $method)
  133. unless compression_is_supported($method);
  134. $default_compression = $method;
  135. }
  136. =item my $level = compression_get_default_level()
  137. Return the default compression level used when compressing data. It's "9"
  138. for "gzip" and "bzip2", "6" for "xz" and "lzma", unless
  139. C<compression_set_default_level> has been used to change it.
  140. =item compression_set_default_level($level)
  141. Change the default compression level. Passing undef as the level will
  142. reset it to the compressor specific default, otherwise errors out if the
  143. level is not valid (see C<compression_is_valid_level>).
  144. =cut
  145. sub compression_get_default_level {
  146. if (defined $default_compression_level) {
  147. return $default_compression_level;
  148. } else {
  149. return compression_get_property($default_compression, 'default_level');
  150. }
  151. }
  152. sub compression_set_default_level {
  153. my ($level) = @_;
  154. error(_g('%s is not a compression level'), $level)
  155. if defined($level) and not compression_is_valid_level($level);
  156. $default_compression_level = $level;
  157. }
  158. =item compression_is_valid_level($level)
  159. Returns a boolean indicating whether $level is a valid compression level
  160. (it must be either a number between 1 and 9 or "fast" or "best")
  161. =cut
  162. sub compression_is_valid_level {
  163. my ($level) = @_;
  164. return $level =~ /^([1-9]|fast|best)$/;
  165. }
  166. =back
  167. =head1 CHANGES
  168. =head2 Version 1.02
  169. Deprecated variables: $default_compression, $default_compression_level
  170. =head2 Version 1.01
  171. Default compression level is not global any more, it is per compressor type.
  172. =head1 AUTHOR
  173. Raphaël Hertzog <hertzog@debian.org>.
  174. =cut
  175. 1;