Archive.pm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # Copyright © 2008 Raphaël Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package Dpkg::Source::Archive;
  16. use strict;
  17. use warnings;
  18. use Dpkg::Source::Functions qw(erasedir fixperms);
  19. use Dpkg::Gettext;
  20. use Dpkg::IPC;
  21. use Dpkg::ErrorHandling;
  22. use POSIX;
  23. use File::Temp qw(tempdir);
  24. use File::Basename qw(basename);
  25. use File::Spec;
  26. use Cwd;
  27. use base 'Dpkg::Compression::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{"delete_env"} = [ "TAR_OPTIONS" ];
  42. $fork_opts{'exec'} = [ 'tar', '--null', '-T', '-', '--numeric-owner',
  43. '--owner', '0', '--group', '0',
  44. @{$opts{"options"}}, '-cf', '-' ];
  45. $self->{"pid"} = fork_and_exec(%fork_opts);
  46. $self->{"cwd"} = getcwd();
  47. }
  48. sub _add_entry {
  49. my ($self, $file) = @_;
  50. internerr("call create() first") unless $self->{"tar_input"};
  51. $file = $2 if ($file =~ /^\Q$self->{'cwd'}\E\/(.+)$/); # Relative names
  52. print({ $self->{'tar_input'} } "$file\0") ||
  53. syserr(_g("write on tar input"));
  54. }
  55. sub add_file {
  56. my ($self, $file) = @_;
  57. my $testfile = $file;
  58. if ($self->{"chdir"}) {
  59. $testfile = File::Spec->catfile($self->{"chdir"}, $file);
  60. }
  61. internerr("add_file() doesn't handle directories") if not -l $testfile and -d _;
  62. $self->_add_entry($file);
  63. }
  64. sub add_directory {
  65. my ($self, $file) = @_;
  66. my $testfile = $file;
  67. if ($self->{"chdir"}) {
  68. $testfile = File::Spec->catdir($self->{"chdir"}, $file);
  69. }
  70. internerr("add_directory() only handles directories") unless not -l $testfile and -d _;
  71. $self->_add_entry($file);
  72. }
  73. sub finish {
  74. my ($self) = @_;
  75. close($self->{'tar_input'}) or syserr(_g("close on tar input"));
  76. wait_child($self->{'pid'}, cmdline => 'tar -cf -');
  77. delete $self->{'pid'};
  78. delete $self->{'tar_input'};
  79. delete $self->{'cwd'};
  80. delete $self->{'chdir'};
  81. $self->cleanup_after_open();
  82. }
  83. sub extract {
  84. my ($self, $dest, %opts) = @_;
  85. $opts{"options"} ||= [];
  86. $opts{"in_place"} ||= 0;
  87. $opts{"no_fixperms"} ||= 0;
  88. my %fork_opts = (wait_child => 1);
  89. # Prepare destination
  90. my $tmp;
  91. if ($opts{"in_place"}) {
  92. $fork_opts{"chdir"} = $dest;
  93. $tmp = $dest; # So that fixperms call works
  94. } else {
  95. my $template = basename($self->get_filename()) . ".tmp-extract.XXXXX";
  96. unless (-e $dest) {
  97. # Kludge so that realpath works
  98. mkdir($dest) || syserr(_g("cannot create directory %s"), $dest);
  99. }
  100. $tmp = tempdir($template, DIR => Cwd::realpath("$dest/.."), CLEANUP => 1);
  101. $fork_opts{"chdir"} = $tmp;
  102. }
  103. # Prepare stuff that handles the input of tar
  104. $fork_opts{"from_handle"} = $self->open_for_read();
  105. # Call tar extraction process
  106. $fork_opts{"delete_env"} = [ "TAR_OPTIONS" ];
  107. $fork_opts{'exec'} = [ 'tar', '--no-same-owner', '--no-same-permissions',
  108. @{$opts{"options"}}, '-xkf', '-' ];
  109. fork_and_exec(%fork_opts);
  110. $self->cleanup_after_open();
  111. # Fix permissions on extracted files because tar insists on applying
  112. # our umask _to the original permissions_ rather than mostly-ignoring
  113. # the original permissions.
  114. # We still need --no-same-permissions because otherwise tar might
  115. # extract directory setgid (which we want inherited, not
  116. # extracted); we need --no-same-owner because putting the owner
  117. # back is tedious - in particular, correct group ownership would
  118. # have to be calculated using mount options and other madness.
  119. fixperms($tmp) unless $opts{"no_fixperms"};
  120. # Stop here if we extracted in-place as there's nothing to move around
  121. return if $opts{"in_place"};
  122. # Rename extracted directory
  123. opendir(D, $tmp) || syserr(_g("cannot opendir %s"), $tmp);
  124. my @entries = grep { $_ ne "." && $_ ne ".." } readdir(D);
  125. closedir(D);
  126. my $done = 0;
  127. erasedir($dest);
  128. if (scalar(@entries) == 1 && -d "$tmp/$entries[0]") {
  129. rename("$tmp/$entries[0]", $dest) ||
  130. syserr(_g("Unable to rename %s to %s"),
  131. "$tmp/$entries[0]", $dest);
  132. } else {
  133. rename($tmp, $dest) ||
  134. syserr(_g("Unable to rename %s to %s"), $tmp, $dest);
  135. }
  136. erasedir($tmp);
  137. }
  138. 1;