Compression.pm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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.00";
  19. use Dpkg::ErrorHandling;
  20. use Dpkg::Gettext;
  21. use base qw(Exporter);
  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. },
  44. "bzip2" => {
  45. "file_ext" => "bz2",
  46. "comp_prog" => [ "bzip2" ],
  47. "decomp_prog" => [ "bunzip2" ],
  48. },
  49. "lzma" => {
  50. "file_ext" => "lzma",
  51. "comp_prog" => [ 'xz', '--format=lzma' ],
  52. "decomp_prog" => [ 'unxz', '--format=lzma' ],
  53. },
  54. "xz" => {
  55. "file_ext" => "xz",
  56. "comp_prog" => [ "xz" ],
  57. "decomp_prog" => [ "unxz" ],
  58. },
  59. };
  60. our $default_compression = "gzip";
  61. our $default_compression_level = 9;
  62. =item $compression_re_file_ext
  63. A regex that matches a file extension of a file compressed with one of the
  64. supported compression methods.
  65. =back
  66. =cut
  67. my $regex = join "|", map { $_->{"file_ext"} } values %$COMP;
  68. our $compression_re_file_ext = qr/(?:$regex)/;
  69. =head1 EXPORTED FUNCTIONS
  70. =over 4
  71. =item my @list = compression_get_list()
  72. Returns a list of supported compression methods (sorted alphabetically).
  73. =cut
  74. sub compression_get_list {
  75. return sort keys %$COMP;
  76. }
  77. =item compression_is_supported($comp)
  78. Returns a boolean indicating whether the give compression method is
  79. known and supported.
  80. =cut
  81. sub compression_is_supported {
  82. return exists $COMP->{$_[0]};
  83. }
  84. =item compression_get_property($comp, $property)
  85. Returns the requested property of the compression method. Returns undef if
  86. either the property or the compression method doesn't exist. Valid
  87. properties currently include "file_ext" for the file extension,
  88. "comp_prog" for the name of the compression program and "decomp_prog" for
  89. the name of the decompression program.
  90. =cut
  91. sub compression_get_property {
  92. my ($comp, $property) = @_;
  93. return undef unless compression_is_supported($comp);
  94. return $COMP->{$comp}{$property} if exists $COMP->{$comp}{$property};
  95. return undef;
  96. }
  97. =item compression_guess_from_filename($filename)
  98. Returns the compression method that is likely used on the indicated
  99. filename based on its file extension.
  100. =cut
  101. sub compression_guess_from_filename {
  102. my $filename = shift;
  103. foreach my $comp (compression_get_list()) {
  104. my $ext = compression_get_property($comp, "file_ext");
  105. if ($filename =~ /^(.*)\.\Q$ext\E$/) {
  106. return $comp;
  107. }
  108. }
  109. return undef;
  110. }
  111. =item my $comp = compression_get_default()
  112. Return the default compression method. It's "gzip" unless
  113. C<compression_set_default> has been used to change it.
  114. =item compression_set_default($comp)
  115. Change the default compression method. Errors out if the
  116. given compression method is not supported.
  117. =cut
  118. sub compression_get_default {
  119. return $default_compression;
  120. }
  121. sub compression_set_default {
  122. my ($method) = @_;
  123. error(_g("%s is not a supported compression"), $method)
  124. unless compression_is_supported($method);
  125. $default_compression = $method;
  126. }
  127. =item my $level = compression_get_default_level()
  128. Return the default compression level used when compressing data. It's "9"
  129. unless C<compression_set_default_level> has been used to change it.
  130. =item compression_set_default_level($level)
  131. Change the default compression level. Errors out if the
  132. level is not valid (see C<compression_is_valid_level>).
  133. either a number between 1 and 9 or "fast"
  134. or "best".
  135. =cut
  136. sub compression_get_default_level {
  137. return $default_compression_level;
  138. }
  139. sub compression_set_default_level {
  140. my ($level) = @_;
  141. error(_g("%s is not a compression level"), $level)
  142. unless compression_is_valid_level($level);
  143. $default_compression_level = $level;
  144. }
  145. =item compression_is_valid_level($level)
  146. Returns a boolean indicating whether $level is a valid compression level
  147. (it must be either a number between 1 and 9 or "fast" or "best")
  148. =cut
  149. sub compression_is_valid_level {
  150. my ($level) = @_;
  151. return $level =~ /^([1-9]|fast|best)$/;
  152. }
  153. =back
  154. =head1 AUTHOR
  155. Raphaël Hertzog <hertzog@debian.org>.
  156. =cut
  157. 1;