Archiver.pm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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{"filename"}) {
  34. $self->set_filename($args{"filename"});
  35. }
  36. return $self;
  37. }
  38. sub reset {
  39. my ($self) = @_;
  40. %{$self} = ();
  41. }
  42. sub use_compression {
  43. my ($self, $method) = @_;
  44. error(_g("%s is not a supported compression method"), $method)
  45. unless $comp_supported{$method};
  46. $self->{"compression"} = $method;
  47. }
  48. sub set_filename {
  49. my ($self, $filename) = @_;
  50. $self->{"filename"} = $filename;
  51. # Check if compression is used
  52. my $comp = get_compression_from_filename($filename);
  53. $self->use_compression($comp) if $comp;
  54. }
  55. sub get_filename {
  56. my $self = shift;
  57. return $self->{"filename"};
  58. }
  59. sub create {
  60. my ($self, %opts) = @_;
  61. $opts{"options"} ||= [];
  62. my %fork_opts;
  63. # Prepare stuff that handles the output of tar
  64. if ($self->{"compression"}) {
  65. $self->{"compressor"} = Dpkg::Source::Compressor->new(
  66. compressed_filename => $self->get_filename(),
  67. compression => $self->{"compression"},
  68. );
  69. $self->{"compressor"}->compress(from_pipe => \$fork_opts{"to_handle"});
  70. } else {
  71. $fork_opts{"to_file"} = $self->get_filename();
  72. }
  73. # Prepare input to tar
  74. $fork_opts{"from_pipe"} = \$self->{'tar_input'};
  75. # Call tar creation process
  76. $fork_opts{'exec'} = [ 'tar', '--null', '-T', '-', @{$opts{"options"}}, '-cf', '-' ];
  77. $self->{"pid"} = fork_and_exec(%fork_opts);
  78. binmode($self->{'tar_input'});
  79. $self->{"cwd"} = getcwd();
  80. }
  81. sub _add_entry {
  82. my ($self, $file) = @_;
  83. error("call create first") unless $self->{"tar_input"};
  84. $file = $2 if ($file =~ /^\Q$self->{'cwd'}\E\/(.+)$/); # Relative names
  85. print({ $self->{'tar_input'} } "$file\0") ||
  86. syserr(_g("write on tar input"));
  87. }
  88. sub add_file {
  89. my ($self, $file) = @_;
  90. error("add_file() doesn't handle directories") if not -l $file and -d _;
  91. $self->_add_entry($file);
  92. }
  93. sub add_directory {
  94. my ($self, $file) = @_;
  95. error("add_directory() only handles directories") unless not -l $file and -d _;
  96. $self->_add_entry($file);
  97. }
  98. sub close {
  99. my ($self) = @_;
  100. close($self->{'tar_input'}) or syserr(_g("close on tar input"));
  101. wait_child($self->{'pid'}, cmdline => 'tar -cf -');
  102. $self->{'compressor'}->wait_end_process() if $self->{'compressor'};
  103. delete $self->{'pid'};
  104. delete $self->{'tar_input'};
  105. delete $self->{'cwd'};
  106. delete $self->{'compressor'};
  107. }
  108. sub extract {
  109. my ($self, $dest, %opts) = @_;
  110. $opts{"options"} ||= [];
  111. my %fork_opts = (wait_child => 1);
  112. # Prepare destination
  113. my $template = basename($self->get_filename()) . ".tmp-extract.XXXXX";
  114. my $tmp = tempdir($template, DIR => getcwd(), CLEANUP => 1);
  115. $fork_opts{"chdir"} = $tmp;
  116. # Prepare stuff that handles the input of tar
  117. if ($self->{"compression"}) {
  118. $self->{"compressor"} = Dpkg::Source::Compressor->new(
  119. compressed_filename => $self->get_filename(),
  120. compression => $self->{"compression"},
  121. );
  122. $self->{"compressor"}->uncompress(to_pipe => \$fork_opts{"from_handle"});
  123. } else {
  124. $fork_opts{"from_file"} = $self->get_filename();
  125. }
  126. # Call tar extraction process
  127. $fork_opts{'exec'} = [ 'tar', '--no-same-owner', '--no-same-permissions',
  128. @{$opts{"options"}}, '-xkf', '-' ];
  129. fork_and_exec(%fork_opts);
  130. # Clean up compressor
  131. $self->{'compressor'}->wait_end_process() if $self->{'compressor'};
  132. delete $self->{'compressor'};
  133. # Fix permissions on extracted files...
  134. my ($mode, $modes_set, $i, $j);
  135. # Unfortunately tar insists on applying our umask _to the original
  136. # permissions_ rather than mostly-ignoring the original
  137. # permissions. We fix it up with chmod -R (which saves us some
  138. # work) but we have to construct a u+/- string which is a bit
  139. # of a palaver. (Numeric doesn't work because we need [ugo]+X
  140. # and [ugo]=<stuff> doesn't work because that unsets sgid on dirs.)
  141. #
  142. # We still need --no-same-permissions because otherwise tar might
  143. # extract directory setgid (which we want inherited, not
  144. # extracted); we need --no-same-owner because putting the owner
  145. # back is tedious - in particular, correct group ownership would
  146. # have to be calculated using mount options and other madness.
  147. #
  148. # It would be nice if tar could do it right, or if pax could cope
  149. # with GNU format tarfiles with long filenames.
  150. #
  151. $mode = 0777 & ~umask;
  152. for ($i = 0; $i < 9; $i += 3) {
  153. $modes_set .= ',' if $i;
  154. $modes_set .= qw(u g o)[$i/3];
  155. for ($j = 0; $j < 3; $j++) {
  156. $modes_set .= $mode & (0400 >> ($i+$j)) ? '+' : '-';
  157. $modes_set .= qw(r w X)[$j];
  158. }
  159. }
  160. system('chmod', '-R', $modes_set, '--', $tmp);
  161. subprocerr("chmod -R $modes_set $tmp") if $?;
  162. # Rename extracted directory
  163. opendir(D, $tmp) || syserr(_g("Unable to open dir %s"), $tmp);
  164. my @entries = grep { $_ ne "." && $_ ne ".." } readdir(D);
  165. closedir(D);
  166. my $done = 0;
  167. rmtree($dest) if -e $dest;
  168. if (scalar(@entries) == 1 && -d "$tmp/$entries[0]") {
  169. rename("$tmp/$entries[0]", $dest) ||
  170. syserr(_g("Unable to rename %s to %s"),
  171. "$tmp/$entries[0]", $dest);
  172. } else {
  173. rename($tmp, $dest) ||
  174. syserr(_g("Unable to rename %s to %s"), $tmp, $dest);
  175. }
  176. rmtree($tmp);
  177. }
  178. 1;