Archive.pm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. package Dpkg::Source::Archive;
  13. use strict;
  14. use warnings;
  15. use Dpkg::Source::Functions qw(erasedir fixperms);
  16. use Dpkg::Gettext;
  17. use Dpkg::IPC;
  18. use Dpkg::ErrorHandling;
  19. use POSIX;
  20. use File::Temp qw(tempdir);
  21. use File::Basename qw(basename);
  22. use File::Spec;
  23. use Cwd;
  24. use base 'Dpkg::Source::CompressedFile';
  25. sub create {
  26. my ($self, %opts) = @_;
  27. $opts{"options"} ||= [];
  28. my %fork_opts;
  29. # Possibly run tar from another directory
  30. if ($opts{"chdir"}) {
  31. $fork_opts{"chdir"} = $opts{"chdir"};
  32. $self->{"chdir"} = $opts{"chdir"};
  33. }
  34. # Redirect input/output appropriately
  35. $fork_opts{"to_handle"} = $self->open_for_write();
  36. $fork_opts{"from_pipe"} = \$self->{'tar_input'};
  37. # Call tar creation process
  38. $fork_opts{"delete_env"} = [ "TAR_OPTIONS" ];
  39. $fork_opts{'exec'} = [ 'tar', '--null', '-T', '-', '--numeric-owner',
  40. '--owner', '0', '--group', '0',
  41. @{$opts{"options"}}, '-cf', '-' ];
  42. $self->{"pid"} = fork_and_exec(%fork_opts);
  43. $self->{"cwd"} = getcwd();
  44. }
  45. sub _add_entry {
  46. my ($self, $file) = @_;
  47. internerr("call create() first") unless $self->{"tar_input"};
  48. $file = $2 if ($file =~ /^\Q$self->{'cwd'}\E\/(.+)$/); # Relative names
  49. print({ $self->{'tar_input'} } "$file\0") ||
  50. syserr(_g("write on tar input"));
  51. }
  52. sub add_file {
  53. my ($self, $file) = @_;
  54. my $testfile = $file;
  55. if ($self->{"chdir"}) {
  56. $testfile = File::Spec->catfile($self->{"chdir"}, $file);
  57. }
  58. internerr("add_file() doesn't handle directories") if not -l $testfile and -d _;
  59. $self->_add_entry($file);
  60. }
  61. sub add_directory {
  62. my ($self, $file) = @_;
  63. my $testfile = $file;
  64. if ($self->{"chdir"}) {
  65. $testfile = File::Spec->catdir($self->{"chdir"}, $file);
  66. }
  67. internerr("add_directory() only handles directories") unless not -l $testfile and -d _;
  68. $self->_add_entry($file);
  69. }
  70. sub finish {
  71. my ($self) = @_;
  72. close($self->{'tar_input'}) or syserr(_g("close on tar input"));
  73. wait_child($self->{'pid'}, cmdline => 'tar -cf -');
  74. delete $self->{'pid'};
  75. delete $self->{'tar_input'};
  76. delete $self->{'cwd'};
  77. delete $self->{'chdir'};
  78. $self->cleanup_after_open();
  79. }
  80. sub extract {
  81. my ($self, $dest, %opts) = @_;
  82. $opts{"options"} ||= [];
  83. $opts{"in_place"} ||= 0;
  84. $opts{"no_fixperms"} ||= 0;
  85. my %fork_opts = (wait_child => 1);
  86. # Prepare destination
  87. my $tmp;
  88. if ($opts{"in_place"}) {
  89. $fork_opts{"chdir"} = $dest;
  90. $tmp = $dest; # So that fixperms call works
  91. } else {
  92. my $template = basename($self->get_filename()) . ".tmp-extract.XXXXX";
  93. unless (-e $dest) {
  94. # Kludge so that realpath works
  95. mkdir($dest) || syserr(_g("cannot create directory %s"), $dest);
  96. }
  97. $tmp = tempdir($template, DIR => Cwd::realpath("$dest/.."), CLEANUP => 1);
  98. $fork_opts{"chdir"} = $tmp;
  99. }
  100. # Prepare stuff that handles the input of tar
  101. $fork_opts{"from_handle"} = $self->open_for_read();
  102. # Call tar extraction process
  103. $fork_opts{"delete_env"} = [ "TAR_OPTIONS" ];
  104. $fork_opts{'exec'} = [ 'tar', '--no-same-owner', '--no-same-permissions',
  105. @{$opts{"options"}}, '-xkf', '-' ];
  106. fork_and_exec(%fork_opts);
  107. $self->cleanup_after_open();
  108. # Fix permissions on extracted files because tar insists on applying
  109. # our umask _to the original permissions_ rather than mostly-ignoring
  110. # the original permissions.
  111. # We still need --no-same-permissions because otherwise tar might
  112. # extract directory setgid (which we want inherited, not
  113. # extracted); we need --no-same-owner because putting the owner
  114. # back is tedious - in particular, correct group ownership would
  115. # have to be calculated using mount options and other madness.
  116. fixperms($tmp) unless $opts{"no_fixperms"};
  117. # Stop here if we extracted in-place as there's nothing to move around
  118. return if $opts{"in_place"};
  119. # Rename extracted directory
  120. opendir(D, $tmp) || syserr(_g("cannot opendir %s"), $tmp);
  121. my @entries = grep { $_ ne "." && $_ ne ".." } readdir(D);
  122. closedir(D);
  123. my $done = 0;
  124. erasedir($dest);
  125. if (scalar(@entries) == 1 && -d "$tmp/$entries[0]") {
  126. rename("$tmp/$entries[0]", $dest) ||
  127. syserr(_g("Unable to rename %s to %s"),
  128. "$tmp/$entries[0]", $dest);
  129. } else {
  130. rename($tmp, $dest) ||
  131. syserr(_g("Unable to rename %s to %s"), $tmp, $dest);
  132. }
  133. erasedir($tmp);
  134. }
  135. 1;