Archiver.pm 4.8 KB

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