Compression.pm 5.1 KB

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