Archive.pm 5.2 KB

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