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