Archiver.pm 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # Copyright 2008 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Source::Archiver;
  14. use strict;
  15. use warnings;
  16. use Dpkg::Source::Compressor;
  17. use Dpkg::Compression;
  18. use Dpkg::Gettext;
  19. use Dpkg::IPC;
  20. use Dpkg::ErrorHandling qw(error syserr warning);
  21. use POSIX;
  22. use File::Temp qw(tempdir);
  23. use File::Path qw(rmtree mkpath);
  24. use File::Basename qw(basename);
  25. sub new {
  26. my ($this, %args) = @_;
  27. my $class = ref($this) || $this;
  28. my $self = {};
  29. bless $self, $class;
  30. if (exists $args{"compression"}) {
  31. $self->use_compression($args{"compression"});
  32. }
  33. if (exists $args{"compression_level"}) {
  34. $self->set_compression_level($args{"compression_level"});
  35. }
  36. if (exists $args{"filename"}) {
  37. $self->set_filename($args{"filename"});
  38. }
  39. return $self;
  40. }
  41. sub reset {
  42. my ($self) = @_;
  43. %{$self} = ();
  44. }
  45. sub use_compression {
  46. my ($self, $method) = @_;
  47. error(_g("%s is not a supported compression method"), $method)
  48. unless $comp_supported{$method};
  49. $self->{"compression"} = $method;
  50. }
  51. sub set_compression_level {
  52. my ($self, $level) = @_;
  53. error(_g("%s is not a compression level"), $level)
  54. unless $level =~ /^([1-9]|fast|best)$/;
  55. $self->{"compression_level"} = $level;
  56. }
  57. sub set_filename {
  58. my ($self, $filename) = @_;
  59. $self->{"filename"} = $filename;
  60. # Check if compression is used
  61. foreach my $comp (@comp_supported) {
  62. if ($filename =~ /^(.*)\.\Q$comp_ext{$comp}\E$/) {
  63. $self->use_compression($comp);
  64. last;
  65. }
  66. }
  67. }
  68. sub get_filename {
  69. my $self = shift;
  70. return $self->{"filename"};
  71. }
  72. sub create {
  73. my ($self, %opts) = @_;
  74. $opts{"options"} ||= [];
  75. my %fork_opts;
  76. # Prepare stuff that handles the output of tar
  77. if ($self->{"compression"}) {
  78. $self->{"compressor"} = Dpkg::Source::Compressor->new(
  79. compressed_filename => $self->get_filename(),
  80. compression => $self->{"compression"},
  81. );
  82. if ($self->{"compression_level"}) {
  83. $self->{"compressor"}->set_compression_level(
  84. $self->{"compression_level"});
  85. }
  86. $self->{"compressor"}->compress(from_pipe => \$fork_opts{"to_handle"});
  87. } else {
  88. $fork_opts{"to_file"} = $self->get_filename();
  89. }
  90. # Prepare input to tar
  91. $fork_opts{"from_pipe"} = \$self->{'tar_input'};
  92. # Call tar creation process
  93. $fork_opts{'exec'} = [ 'tar', '--null', '-T', '-', @{$opts{"options"}}, '-cf', '-' ];
  94. $self->{"pid"} = fork_and_exec(%fork_opts);
  95. binmode($self->{'tar_input'});
  96. $self->{"cwd"} = getcwd();
  97. }
  98. sub _add_entry {
  99. my ($self, $file) = @_;
  100. error("call create first") unless $self->{"tar_input"};
  101. $file = $2 if ($file =~ /^\Q$self->{'cwd'}\E\/(.+)$/); # Relative names
  102. print { $self->{'tar_input'} } "$file\0" ||
  103. syserr(_g("write on tar input"));
  104. }
  105. sub add_file {
  106. my ($self, $file) = @_;
  107. error("add_file() doesn't handle directories") if not -l $file and -d _;
  108. $self->_add_entry($file);
  109. }
  110. sub add_directory {
  111. my ($self, $file) = @_;
  112. error("add_directory() only handles directories") unless not -l $file and -d _;
  113. $self->_add_entry($file);
  114. }
  115. sub close {
  116. my ($self) = @_;
  117. close($self->{'tar_input'}) or syserr(_g("close on tar input"));
  118. wait_child($self->{'pid'}, cmdline => 'tar -cf -');
  119. $self->{'compressor'}->wait_end_process() if $self->{'compressor'};
  120. delete $self->{'pid'};
  121. delete $self->{'tar_input'};
  122. delete $self->{'cwd'};
  123. delete $self->{'compressor'};
  124. }
  125. sub extract {
  126. my ($self, $dest, %opts) = @_;
  127. $opts{"options"} ||= [];
  128. my %fork_opts = (wait_child => 1);
  129. # Prepare destination
  130. my $template = basename($self->get_filename()) . ".tmp-extract.XXXXX";
  131. my $tmp = tempdir($template, DIR => getcwd(), CLEANUP => 1);
  132. $fork_opts{"chdir"} = $tmp;
  133. # Prepare stuff that handles the input of tar
  134. if ($self->{"compression"}) {
  135. $self->{"compressor"} = Dpkg::Source::Compressor->new(
  136. compressed_filename => $self->get_filename(),
  137. compression => $self->{"compression"},
  138. );
  139. $self->{"compressor"}->uncompress(to_pipe => \$fork_opts{"from_handle"});
  140. } else {
  141. $fork_opts{"from_file"} = $self->get_filename();
  142. }
  143. # Call tar extraction process
  144. $fork_opts{'exec'} = [ 'tar', '--no-same-owner', '--no-same-permissions',
  145. @{$opts{"options"}}, '-xkf', '-' ];
  146. fork_and_exec(%fork_opts);
  147. # Clean up compressor
  148. $self->{'compressor'}->wait_end_process() if $self->{'compressor'};
  149. delete $self->{'compressor'};
  150. # Fix permissions on extracted files...
  151. my ($mode, $modes_set, $i, $j);
  152. # Unfortunately tar insists on applying our umask _to the original
  153. # permissions_ rather than mostly-ignoring the original
  154. # permissions. We fix it up with chmod -R (which saves us some
  155. # work) but we have to construct a u+/- string which is a bit
  156. # of a palaver. (Numeric doesn't work because we need [ugo]+X
  157. # and [ugo]=<stuff> doesn't work because that unsets sgid on dirs.)
  158. #
  159. # We still need --no-same-permissions because otherwise tar might
  160. # extract directory setgid (which we want inherited, not
  161. # extracted); we need --no-same-owner because putting the owner
  162. # back is tedious - in particular, correct group ownership would
  163. # have to be calculated using mount options and other madness.
  164. #
  165. # It would be nice if tar could do it right, or if pax could cope
  166. # with GNU format tarfiles with long filenames.
  167. #
  168. $mode = 0777 & ~umask;
  169. for ($i = 0; $i < 9; $i += 3) {
  170. $modes_set .= ',' if $i;
  171. $modes_set .= qw(u g o)[$i/3];
  172. for ($j = 0; $j < 3; $j++) {
  173. $modes_set .= $mode & (0400 >> ($i+$j)) ? '+' : '-';
  174. $modes_set .= qw(r w X)[$j];
  175. }
  176. }
  177. system('chmod', '-R', $modes_set, '--', $tmp);
  178. subprocerr("chmod -R $modes_set $tmp") if $?;
  179. # Rename extracted directory
  180. opendir(D, $tmp) || syserr(_g("Unable to open dir %s"), $tmp);
  181. my @entries = grep { $_ ne "." && $_ ne ".." } readdir(D);
  182. closedir(D);
  183. my $done = 0;
  184. rmtree($dest) if -e $dest;
  185. if (scalar(@entries) == 1 && -d "$tmp/$entries[0]") {
  186. rename("$tmp/$entries[0]", $dest) ||
  187. syserr(_g("Unable to rename %s to %s"),
  188. "$tmp/$entries[0]", $dest);
  189. } else {
  190. rename($tmp, $dest) ||
  191. syserr(_g("Unable to rename %s to %s"), $tmp, $dest);
  192. }
  193. rmtree($tmp);
  194. }
  195. 1;