Archive.pm 5.0 KB

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