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 File::Temp qw(tempdir);
  24. use File::Basename qw(basename);
  25. use File::Spec;
  26. use Cwd;
  27. use parent qw(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. or 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')
  73. if -l $testfile or not -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) or 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(my $dir_dh, $tmp) or syserr(_g('cannot opendir %s'), $tmp);
  128. my @entries = grep { $_ ne '.' && $_ ne '..' } readdir($dir_dh);
  129. closedir($dir_dh);
  130. my $done = 0;
  131. erasedir($dest);
  132. if (scalar(@entries) == 1 && ! -l "$tmp/$entries[0]" && -d _) {
  133. rename("$tmp/$entries[0]", $dest)
  134. or syserr(_g('unable to rename %s to %s'),
  135. "$tmp/$entries[0]", $dest);
  136. } else {
  137. rename($tmp, $dest)
  138. or syserr(_g('unable to rename %s to %s'), $tmp, $dest);
  139. }
  140. erasedir($tmp);
  141. }
  142. 1;