Archiver.pm 6.8 KB

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