Archive.pm 4.8 KB

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