File Coverage

File:Dpkg/Source/Archive.pm
Coverage:52.0%

linestmtbrancondsubpodtimecode
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
16package Dpkg::Source::Archive;
17
18
2
2
2
4
0
24
use strict;
19
2
2
2
2
2
52
use warnings;
20
21our $VERSION = '0.01';
22
23
2
2
2
4
0
46
use Carp;
24
2
2
2
242
1004
82
use Errno qw(ENOENT);
25
2
2
2
546
9258
56
use File::Temp qw(tempdir);
26
2
2
2
6
2
42
use File::Basename qw(basename);
27
2
2
2
2
2
16
use File::Spec;
28
2
2
2
4
0
32
use File::Find;
29
2
2
2
4
2
34
use Cwd;
30
31
2
2
2
254
0
20
use Dpkg ();
32
2
2
2
224
2
48
use Dpkg::Gettext;
33
2
2
2
230
16
60
use Dpkg::ErrorHandling;
34
2
2
2
252
2
50
use Dpkg::IPC;
35
2
2
2
238
0
56
use Dpkg::Source::Functions qw(erasedir fixperms);
36
37
2
2
2
4
2
2
use parent qw(Dpkg::Compression::FileHandle);
38
39sub create {
40
0
0
0
    my ($self, %opts) = @_;
41
0
0
    $opts{options} //= [];
42
0
0
    my %spawn_opts;
43    # Possibly run tar from another directory
44
0
0
    if ($opts{chdir}) {
45
0
0
        $spawn_opts{chdir} = $opts{chdir};
46
0
0
        *$self->{chdir} = $opts{chdir};
47    }
48    # Redirect input/output appropriately
49
0
0
    $self->ensure_open('w');
50
0
0
    $spawn_opts{to_handle} = $self->get_filehandle();
51
0
0
    $spawn_opts{from_pipe} = \*$self->{tar_input};
52    # Try to use a deterministic mtime.
53
0
0
    my $mtime = $opts{source_date} // $ENV{SOURCE_DATE_EPOCH} || time;
54    # Call tar creation process
55
0
0
    $spawn_opts{delete_env} = [ 'TAR_OPTIONS' ];
56    $spawn_opts{exec} = [ $Dpkg::PROGTAR, '-cf', '-', '--format=gnu', '--sort=name',
57                          '--mtime', "\@$mtime", '--clamp-mtime', '--null',
58                          '--numeric-owner', '--owner=0', '--group=0',
59
0
0
0
0
                          @{$opts{options}}, '-T', '-' ];
60
0
0
    *$self->{pid} = spawn(%spawn_opts);
61
0
0
    *$self->{cwd} = getcwd();
62}
63
64sub _add_entry {
65
0
0
    my ($self, $file) = @_;
66
0
0
    my $cwd = *$self->{cwd};
67
0
0
    croak 'call create() first' unless *$self->{tar_input};
68
0
0
    $file = $2 if ($file =~ /^\Q$cwd\E\/(.+)$/); # Relative names
69
0
0
0
0
    print({ *$self->{tar_input} } "$file\0")
70        or syserr(g_('write on tar input'));
71}
72
73sub add_file {
74
0
0
0
    my ($self, $file) = @_;
75
0
0
    my $testfile = $file;
76
0
0
    if (*$self->{chdir}) {
77
0
0
        $testfile = File::Spec->catfile(*$self->{chdir}, $file);
78    }
79
0
0
    croak 'add_file() does not handle directories'
80        if not -l $testfile and -d _;
81
0
0
    $self->_add_entry($file);
82}
83
84sub add_directory {
85
0
0
0
    my ($self, $file) = @_;
86
0
0
    my $testfile = $file;
87
0
0
    if (*$self->{chdir}) {
88
0
0
        $testfile = File::Spec->catdir(*$self->{chdir}, $file);
89    }
90
0
0
    croak 'add_directory() only handles directories'
91        if -l $testfile or not -d _;
92
0
0
    $self->_add_entry($file);
93}
94
95sub finish {
96
0
0
0
    my $self = shift;
97
98
0
0
    close(*$self->{tar_input}) or syserr(g_('close on tar input'));
99
0
0
    wait_child(*$self->{pid}, cmdline => 'tar -cf -');
100
0
0
    delete *$self->{pid};
101
0
0
    delete *$self->{tar_input};
102
0
0
    delete *$self->{cwd};
103
0
0
    delete *$self->{chdir};
104
0
0
    $self->close();
105}
106
107sub extract {
108
2
0
6
    my ($self, $dest, %opts) = @_;
109
2
10
    $opts{options} //= [];
110
2
4
    $opts{in_place} //= 0;
111
2
6
    $opts{no_fixperms} //= 0;
112
2
4
    my %spawn_opts = (wait_child => 1);
113
114    # Prepare destination
115
2
10
    my $template = basename($self->get_filename()) .  '.tmp-extract.XXXXX';
116
2
14
    unless (-e $dest) {
117        # Kludge so that realpath works
118
0
0
        mkdir($dest) or syserr(g_('cannot create directory %s'), $dest);
119    }
120
2
48
    my $tmp = tempdir($template, DIR => Cwd::realpath("$dest/.."), CLEANUP => 1);
121
2
572
    $spawn_opts{chdir} = $tmp;
122
123    # Prepare stuff that handles the input of tar
124
2
10
    $self->ensure_open('r', delete_sig => [ 'PIPE' ]);
125
2
10
    $spawn_opts{from_handle} = $self->get_filehandle();
126
127    # Call tar extraction process
128
2
6
    $spawn_opts{delete_env} = [ 'TAR_OPTIONS' ];
129    $spawn_opts{exec} = [ $Dpkg::PROGTAR, '-xf', '-', '--no-same-permissions',
130
2
2
2
8
                          '--no-same-owner', @{$opts{options}} ];
131
2
10
    spawn(%spawn_opts);
132
1
26
    $self->close();
133
134    # Fix permissions on extracted files because tar insists on applying
135    # our umask _to the original permissions_ rather than mostly-ignoring
136    # the original permissions.
137    # We still need --no-same-permissions because otherwise tar might
138    # extract directory setgid (which we want inherited, not
139    # extracted); we need --no-same-owner because putting the owner
140    # back is tedious - in particular, correct group ownership would
141    # have to be calculated using mount options and other madness.
142
1
6
    fixperms($tmp) unless $opts{no_fixperms};
143
144    # If we are extracting "in-place" do not remove the destination directory.
145
1
11
    if ($opts{in_place}) {
146
1
28
        my $canon_basedir = Cwd::realpath($dest);
147        # On Solaris /dev/null points to /devices/pseudo/mm@0:null.
148
1
7
        my $canon_devnull = Cwd::realpath('/dev/null');
149        my $check_symlink = sub {
150
6
4
            my $pathname = shift;
151
6
126
            my $canon_pathname = Cwd::realpath($pathname);
152
6
7
            if (not defined $canon_pathname) {
153
0
0
                return if $! == ENOENT;
154
155
0
0
                syserr(g_("pathname '%s' cannot be canonicalized"), $pathname);
156            }
157
6
5
            return if $canon_pathname eq $canon_devnull;
158
6
6
            return if $canon_pathname eq $canon_basedir;
159
6
26
            return if $canon_pathname =~ m{^\Q$canon_basedir/\E};
160
2
27
            warning(g_("pathname '%s' points outside source root (to '%s')"),
161                    $pathname, $canon_pathname);
162
1
19
        };
163
164        my $move_in_place = sub {
165
11
282
            my $relpath = File::Spec->abs2rel($File::Find::name, $tmp);
166
11
47
            my $destpath = File::Spec->catfile($dest, $relpath);
167
168
11
10
            my ($mode, $atime, $mtime);
169
11
36
            lstat $File::Find::name
170                or syserr(g_('cannot get source pathname %s metadata'), $File::Find::name);
171
11
17
            ((undef) x 2, $mode, (undef) x 5, $atime, $mtime) = lstat _;
172
11
9
            my $src_is_dir = -d _;
173
174
11
8
            my $dest_exists = 1;
175
11
35
            if (not lstat $destpath) {
176
2
9
                if ($! == ENOENT) {
177
2
2
                    $dest_exists = 0;
178                } else {
179
0
0
                    syserr(g_('cannot get target pathname %s metadata'), $destpath);
180                }
181            }
182
11
10
            my $dest_is_dir = -d _;
183
11
10
            if ($dest_exists) {
184
9
17
                if ($dest_is_dir && $src_is_dir) {
185                    # Refresh the destination directory attributes with the
186                    # ones from the tarball.
187
3
18
                    chmod $mode, $destpath
188                        or syserr(g_('cannot change directory %s mode'), $File::Find::name);
189
3
17
                    utime $atime, $mtime, $destpath
190                        or syserr(g_('cannot change directory %s times'), $File::Find::name);
191
192                    # We should do nothing, and just walk further tree.
193
3
107
                    return;
194                } elsif ($dest_is_dir) {
195
0
0
                    rmdir $destpath
196                        or syserr(g_('cannot remove destination directory %s'), $destpath);
197                } else {
198
6
6
                    $check_symlink->($destpath);
199
6
98
                    unlink $destpath
200                        or syserr(g_('cannot remove destination file %s'), $destpath);
201                }
202            }
203            # If we are moving a directory, we do not need to walk it.
204
8
10
            if ($src_is_dir) {
205
5
2
                $File::Find::prune = 1;
206            }
207
8
151
            rename $File::Find::name, $destpath
208                or syserr(g_('cannot move %s to %s'), $File::Find::name, $destpath);
209
1
7
        };
210
211
1
99
        find({
212            wanted => $move_in_place,
213            no_chdir => 1,
214            dangling_symlinks => 0,
215        }, $tmp);
216    } else {
217        # Rename extracted directory
218
0
0
        opendir(my $dir_dh, $tmp) or syserr(g_('cannot opendir %s'), $tmp);
219
0
0
0
0
        my @entries = grep { $_ ne '.' && $_ ne '..' } readdir($dir_dh);
220
0
0
        closedir($dir_dh);
221
222
0
0
        erasedir($dest);
223
224
0
0
        if (scalar(@entries) == 1 && ! -l "$tmp/$entries[0]" && -d _) {
225
0
0
            rename("$tmp/$entries[0]", $dest)
226                or syserr(g_('unable to rename %s to %s'),
227                          "$tmp/$entries[0]", $dest);
228        } else {
229
0
0
            rename($tmp, $dest)
230                or syserr(g_('unable to rename %s to %s'), $tmp, $dest);
231        }
232    }
233
1
11
    erasedir($tmp);
234}
235
2361;