Archive.pm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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{'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{'exec'} = [ 'tar', '--no-same-owner', '--no-same-permissions',
  104. @{$opts{"options"}}, '-xkf', '-' ];
  105. fork_and_exec(%fork_opts);
  106. $self->cleanup_after_open();
  107. # Fix permissions on extracted files because tar insists on applying
  108. # our umask _to the original permissions_ rather than mostly-ignoring
  109. # the original permissions.
  110. # We still need --no-same-permissions because otherwise tar might
  111. # extract directory setgid (which we want inherited, not
  112. # extracted); we need --no-same-owner because putting the owner
  113. # back is tedious - in particular, correct group ownership would
  114. # have to be calculated using mount options and other madness.
  115. fixperms($tmp) unless $opts{"no_fixperms"};
  116. # Stop here if we extracted in-place as there's nothing to move around
  117. return if $opts{"in_place"};
  118. # Rename extracted directory
  119. opendir(D, $tmp) || syserr(_g("cannot opendir %s"), $tmp);
  120. my @entries = grep { $_ ne "." && $_ ne ".." } readdir(D);
  121. closedir(D);
  122. my $done = 0;
  123. erasedir($dest);
  124. if (scalar(@entries) == 1 && -d "$tmp/$entries[0]") {
  125. rename("$tmp/$entries[0]", $dest) ||
  126. syserr(_g("Unable to rename %s to %s"),
  127. "$tmp/$entries[0]", $dest);
  128. } else {
  129. rename($tmp, $dest) ||
  130. syserr(_g("Unable to rename %s to %s"), $tmp, $dest);
  131. }
  132. erasedir($tmp);
  133. }
  134. 1;