Archive.pm 5.2 KB

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