Archive.pm 5.4 KB

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