File Coverage

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

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
16=encoding utf8
17
18 - 29
=head1 NAME

Dpkg::Source::Archive - source tarball archive support

=head1 DESCRIPTION

This module provides a class that implements support for handling
source tarballs.

B<Note>: This is a private module, its API can change at any time.

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

=head2 Version 0.xx

This is a private module.

=cut
263
2641;