Archive.pm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 <https://www.gnu.org/licenses/>.
  15. package Dpkg::Source::Archive;
  16. use strict;
  17. use warnings;
  18. our $VERSION = '0.01';
  19. use Carp;
  20. use File::Temp qw(tempdir);
  21. use File::Basename qw(basename);
  22. use File::Spec;
  23. use Cwd;
  24. use Dpkg::Gettext;
  25. use Dpkg::ErrorHandling;
  26. use Dpkg::IPC;
  27. use Dpkg::Source::Functions qw(erasedir fixperms);
  28. use parent qw(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', '-cf', '-', '--format=gnu', '--null',
  45. '--numeric-owner', '--owner=0', '--group=0',
  46. @{$opts{options}}, '-T', '-' ];
  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. croak '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. or 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. croak 'add_file() does not handle directories'
  65. if not -l $testfile and -d _;
  66. $self->_add_entry($file);
  67. }
  68. sub add_directory {
  69. my ($self, $file) = @_;
  70. my $testfile = $file;
  71. if (*$self->{chdir}) {
  72. $testfile = File::Spec->catdir(*$self->{chdir}, $file);
  73. }
  74. croak 'add_directory() only handles directories'
  75. if -l $testfile or not -d _;
  76. $self->_add_entry($file);
  77. }
  78. sub finish {
  79. my $self = shift;
  80. close(*$self->{tar_input}) or syserr(g_('close on tar input'));
  81. wait_child(*$self->{pid}, cmdline => 'tar -cf -');
  82. delete *$self->{pid};
  83. delete *$self->{tar_input};
  84. delete *$self->{cwd};
  85. delete *$self->{chdir};
  86. $self->close();
  87. }
  88. sub extract {
  89. my ($self, $dest, %opts) = @_;
  90. $opts{options} //= [];
  91. $opts{in_place} //= 0;
  92. $opts{no_fixperms} //= 0;
  93. my %spawn_opts = (wait_child => 1);
  94. # Prepare destination
  95. my $tmp;
  96. if ($opts{in_place}) {
  97. $spawn_opts{chdir} = $dest;
  98. $tmp = $dest; # So that fixperms call works
  99. } else {
  100. my $template = basename($self->get_filename()) . '.tmp-extract.XXXXX';
  101. unless (-e $dest) {
  102. # Kludge so that realpath works
  103. mkdir($dest) or syserr(g_('cannot create directory %s'), $dest);
  104. }
  105. $tmp = tempdir($template, DIR => Cwd::realpath("$dest/.."), CLEANUP => 1);
  106. $spawn_opts{chdir} = $tmp;
  107. }
  108. # Prepare stuff that handles the input of tar
  109. $self->ensure_open('r', delete_sig => [ 'PIPE' ]);
  110. $spawn_opts{from_handle} = $self->get_filehandle();
  111. # Call tar extraction process
  112. $spawn_opts{delete_env} = [ 'TAR_OPTIONS' ];
  113. $spawn_opts{exec} = [ 'tar', '-xf', '-', '--no-same-permissions',
  114. '--no-same-owner', @{$opts{options}} ];
  115. spawn(%spawn_opts);
  116. $self->close();
  117. # Fix permissions on extracted files because tar insists on applying
  118. # our umask _to the original permissions_ rather than mostly-ignoring
  119. # the original permissions.
  120. # We still need --no-same-permissions because otherwise tar might
  121. # extract directory setgid (which we want inherited, not
  122. # extracted); we need --no-same-owner because putting the owner
  123. # back is tedious - in particular, correct group ownership would
  124. # have to be calculated using mount options and other madness.
  125. fixperms($tmp) unless $opts{no_fixperms};
  126. # Stop here if we extracted in-place as there's nothing to move around
  127. return if $opts{in_place};
  128. # Rename extracted directory
  129. opendir(my $dir_dh, $tmp) or syserr(g_('cannot opendir %s'), $tmp);
  130. my @entries = grep { $_ ne '.' && $_ ne '..' } readdir($dir_dh);
  131. closedir($dir_dh);
  132. my $done = 0;
  133. erasedir($dest);
  134. if (scalar(@entries) == 1 && ! -l "$tmp/$entries[0]" && -d _) {
  135. rename("$tmp/$entries[0]", $dest)
  136. or syserr(g_('unable to rename %s to %s'),
  137. "$tmp/$entries[0]", $dest);
  138. } else {
  139. rename($tmp, $dest)
  140. or syserr(g_('unable to rename %s to %s'), $tmp, $dest);
  141. }
  142. erasedir($tmp);
  143. }
  144. 1;