Archive.pm 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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::Archive;
  14. use strict;
  15. use warnings;
  16. use Dpkg::Source::Functions qw(erasedir);
  17. use Dpkg::Source::CompressedFile;
  18. use Dpkg::Source::Compressor;
  19. use Dpkg::Compression;
  20. use Dpkg::Gettext;
  21. use Dpkg::IPC;
  22. use Dpkg::ErrorHandling qw(error syserr warning);
  23. use POSIX;
  24. use File::Temp qw(tempdir);
  25. use File::Basename qw(basename);
  26. use File::Spec;
  27. use base 'Dpkg::Source::CompressedFile';
  28. sub create {
  29. my ($self, %opts) = @_;
  30. $opts{"options"} ||= [];
  31. my %fork_opts;
  32. # Possibly run tar from another directory
  33. if ($opts{"chdir"}) {
  34. $fork_opts{"chdir"} = $opts{"chdir"};
  35. $self->{"chdir"} = $opts{"chdir"};
  36. }
  37. # Redirect input/output appropriately
  38. $fork_opts{"to_handle"} = $self->open_for_write();
  39. $fork_opts{"from_pipe"} = \$self->{'tar_input'};
  40. # Call tar creation process
  41. $fork_opts{'exec'} = [ 'tar', '--null', '-T', '-',
  42. @{$opts{"options"}}, '-cf', '-' ];
  43. $self->{"pid"} = fork_and_exec(%fork_opts);
  44. $self->{"cwd"} = getcwd();
  45. }
  46. sub _add_entry {
  47. my ($self, $file) = @_;
  48. error("call create first") unless $self->{"tar_input"};
  49. $file = $2 if ($file =~ /^\Q$self->{'cwd'}\E\/(.+)$/); # Relative names
  50. print({ $self->{'tar_input'} } "$file\0") ||
  51. syserr(_g("write on tar input"));
  52. }
  53. sub add_file {
  54. my ($self, $file) = @_;
  55. my $testfile = $file;
  56. if ($self->{"chdir"}) {
  57. $testfile = File::Spec->catfile($self->{"chdir"}, $file);
  58. }
  59. error("add_file() doesn't handle directories") if not -l $testfile and -d _;
  60. $self->_add_entry($file);
  61. }
  62. sub add_directory {
  63. my ($self, $file) = @_;
  64. my $testfile = $file;
  65. if ($self->{"chdir"}) {
  66. $testfile = File::Spec->catdir($self->{"chdir"}, $file);
  67. }
  68. error("add_directory() only handles directories") unless not -l $testfile and -d _;
  69. $self->_add_entry($file);
  70. }
  71. sub finish {
  72. my ($self) = @_;
  73. close($self->{'tar_input'}) or syserr(_g("close on tar input"));
  74. wait_child($self->{'pid'}, cmdline => 'tar -cf -');
  75. delete $self->{'pid'};
  76. delete $self->{'tar_input'};
  77. delete $self->{'cwd'};
  78. delete $self->{'chdir'};
  79. $self->cleanup_after_open();
  80. }
  81. sub extract {
  82. my ($self, $dest, %opts) = @_;
  83. $opts{"options"} ||= [];
  84. my %fork_opts = (wait_child => 1);
  85. # Prepare destination
  86. my $template = basename($self->get_filename()) . ".tmp-extract.XXXXX";
  87. my $tmp = tempdir($template, DIR => getcwd(), CLEANUP => 1);
  88. $fork_opts{"chdir"} = $tmp;
  89. # Prepare stuff that handles the input of tar
  90. $fork_opts{"from_handle"} = $self->open_for_read();
  91. # Call tar extraction process
  92. $fork_opts{'exec'} = [ 'tar', '--no-same-owner', '--no-same-permissions',
  93. @{$opts{"options"}}, '-xkf', '-' ];
  94. fork_and_exec(%fork_opts);
  95. $self->cleanup_after_open();
  96. # Fix permissions on extracted files...
  97. my ($mode, $modes_set, $i, $j);
  98. # Unfortunately tar insists on applying our umask _to the original
  99. # permissions_ rather than mostly-ignoring the original
  100. # permissions. We fix it up with chmod -R (which saves us some
  101. # work) but we have to construct a u+/- string which is a bit
  102. # of a palaver. (Numeric doesn't work because we need [ugo]+X
  103. # and [ugo]=<stuff> doesn't work because that unsets sgid on dirs.)
  104. #
  105. # We still need --no-same-permissions because otherwise tar might
  106. # extract directory setgid (which we want inherited, not
  107. # extracted); we need --no-same-owner because putting the owner
  108. # back is tedious - in particular, correct group ownership would
  109. # have to be calculated using mount options and other madness.
  110. #
  111. # It would be nice if tar could do it right, or if pax could cope
  112. # with GNU format tarfiles with long filenames.
  113. #
  114. $mode = 0777 & ~umask;
  115. for ($i = 0; $i < 9; $i += 3) {
  116. $modes_set .= ',' if $i;
  117. $modes_set .= qw(u g o)[$i/3];
  118. for ($j = 0; $j < 3; $j++) {
  119. $modes_set .= $mode & (0400 >> ($i+$j)) ? '+' : '-';
  120. $modes_set .= qw(r w X)[$j];
  121. }
  122. }
  123. system('chmod', '-R', $modes_set, '--', $tmp);
  124. subprocerr("chmod -R $modes_set $tmp") if $?;
  125. # Rename extracted directory
  126. opendir(D, $tmp) || syserr(_g("cannot opendir %s"), $tmp);
  127. my @entries = grep { $_ ne "." && $_ ne ".." } readdir(D);
  128. closedir(D);
  129. my $done = 0;
  130. erasedir($dest);
  131. if (scalar(@entries) == 1 && -d "$tmp/$entries[0]") {
  132. rename("$tmp/$entries[0]", $dest) ||
  133. syserr(_g("Unable to rename %s to %s"),
  134. "$tmp/$entries[0]", $dest);
  135. } else {
  136. rename($tmp, $dest) ||
  137. syserr(_g("Unable to rename %s to %s"), $tmp, $dest);
  138. }
  139. erasedir($tmp);
  140. }
  141. 1;