Archive.pm 5.4 KB

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