Archive.pm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 fixperms);
  17. use Dpkg::Gettext;
  18. use Dpkg::IPC;
  19. use Dpkg::ErrorHandling;
  20. use POSIX;
  21. use File::Temp qw(tempdir);
  22. use File::Basename qw(basename);
  23. use File::Spec;
  24. use Cwd;
  25. use base 'Dpkg::Source::CompressedFile';
  26. sub create {
  27. my ($self, %opts) = @_;
  28. $opts{"options"} ||= [];
  29. my %fork_opts;
  30. # Possibly run tar from another directory
  31. if ($opts{"chdir"}) {
  32. $fork_opts{"chdir"} = $opts{"chdir"};
  33. $self->{"chdir"} = $opts{"chdir"};
  34. }
  35. # Redirect input/output appropriately
  36. $fork_opts{"to_handle"} = $self->open_for_write();
  37. $fork_opts{"from_pipe"} = \$self->{'tar_input'};
  38. # Call tar creation process
  39. $fork_opts{"delete_env"} = [ "TAR_OPTIONS" ];
  40. $fork_opts{'exec'} = [ 'tar', '--null', '-T', '-', '--numeric-owner',
  41. '--owner', '0', '--group', '0',
  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. internerr("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. internerr("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. internerr("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. $opts{"in_place"} ||= 0;
  85. $opts{"no_fixperms"} ||= 0;
  86. my %fork_opts = (wait_child => 1);
  87. # Prepare destination
  88. my $tmp;
  89. if ($opts{"in_place"}) {
  90. $fork_opts{"chdir"} = $dest;
  91. $tmp = $dest; # So that fixperms call works
  92. } else {
  93. my $template = basename($self->get_filename()) . ".tmp-extract.XXXXX";
  94. unless (-e $dest) {
  95. # Kludge so that realpath works
  96. mkdir($dest) || syserr(_g("cannot create directory %s"), $dest);
  97. }
  98. $tmp = tempdir($template, DIR => Cwd::realpath("$dest/.."), CLEANUP => 1);
  99. $fork_opts{"chdir"} = $tmp;
  100. }
  101. # Prepare stuff that handles the input of tar
  102. $fork_opts{"from_handle"} = $self->open_for_read();
  103. # Call tar extraction process
  104. $fork_opts{"delete_env"} = [ "TAR_OPTIONS" ];
  105. $fork_opts{'exec'} = [ 'tar', '--no-same-owner', '--no-same-permissions',
  106. @{$opts{"options"}}, '-xkf', '-' ];
  107. fork_and_exec(%fork_opts);
  108. $self->cleanup_after_open();
  109. # Fix permissions on extracted files because tar insists on applying
  110. # our umask _to the original permissions_ rather than mostly-ignoring
  111. # the original permissions.
  112. # We still need --no-same-permissions because otherwise tar might
  113. # extract directory setgid (which we want inherited, not
  114. # extracted); we need --no-same-owner because putting the owner
  115. # back is tedious - in particular, correct group ownership would
  116. # have to be calculated using mount options and other madness.
  117. fixperms($tmp) unless $opts{"no_fixperms"};
  118. # Stop here if we extracted in-place as there's nothing to move around
  119. return if $opts{"in_place"};
  120. # Rename extracted directory
  121. opendir(D, $tmp) || syserr(_g("cannot opendir %s"), $tmp);
  122. my @entries = grep { $_ ne "." && $_ ne ".." } readdir(D);
  123. closedir(D);
  124. my $done = 0;
  125. erasedir($dest);
  126. if (scalar(@entries) == 1 && -d "$tmp/$entries[0]") {
  127. rename("$tmp/$entries[0]", $dest) ||
  128. syserr(_g("Unable to rename %s to %s"),
  129. "$tmp/$entries[0]", $dest);
  130. } else {
  131. rename($tmp, $dest) ||
  132. syserr(_g("Unable to rename %s to %s"), $tmp, $dest);
  133. }
  134. erasedir($tmp);
  135. }
  136. 1;