File Coverage

blib/lib/Dpkg/Source/Archive.pm
Criterion Covered Total %
statement 39 113 34.5
branch 0 38 0.0
condition 0 23 0.0
subroutine 13 19 68.4
pod 0 5 0.0
total 52 198 26.2


line stmt bran cond sub pod time code
1             # Copyright © 2008 Raphaël Hertzog
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 .
15              
16             package Dpkg::Source::Archive;
17              
18 1     1   72257 use strict;
  1         10  
  1         31  
19 1     1   5 use warnings;
  1         2  
  1         59  
20              
21             our $VERSION = '0.01';
22              
23 1     1   7 use Carp;
  1         1  
  1         75  
24 1     1   811 use File::Temp qw(tempdir);
  1         22144  
  1         69  
25 1     1   8 use File::Basename qw(basename);
  1         2  
  1         94  
26 1     1   8 use File::Spec;
  1         2  
  1         18  
27 1     1   7 use Cwd;
  1         3  
  1         52  
28              
29 1     1   466 use Dpkg ();
  1         3  
  1         23  
30 1     1   411 use Dpkg::Gettext;
  1         4  
  1         73  
31 1     1   461 use Dpkg::ErrorHandling;
  1         3  
  1         74  
32 1     1   463 use Dpkg::IPC;
  1         3  
  1         62  
33 1     1   453 use Dpkg::Source::Functions qw(erasedir fixperms);
  1         3  
  1         60  
34              
35 1     1   7 use parent qw(Dpkg::Compression::FileHandle);
  1         2  
  1         10  
36              
37             sub create {
38 0     0 0   my ($self, %opts) = @_;
39 0   0       $opts{options} //= [];
40 0           my %spawn_opts;
41             # Possibly run tar from another directory
42 0 0         if ($opts{chdir}) {
43 0           $spawn_opts{chdir} = $opts{chdir};
44 0           *$self->{chdir} = $opts{chdir};
45             }
46             # Redirect input/output appropriately
47 0           $self->ensure_open('w');
48 0           $spawn_opts{to_handle} = $self->get_filehandle();
49 0           $spawn_opts{from_pipe} = \*$self->{tar_input};
50             # Try to use a deterministic mtime.
51 0   0       my $mtime = $opts{source_date} // $ENV{SOURCE_DATE_EPOCH} || time;
52             # Call tar creation process
53 0           $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 0           @{$opts{options}}, '-T', '-' ];
  0            
58 0           *$self->{pid} = spawn(%spawn_opts);
59 0           *$self->{cwd} = getcwd();
60             }
61              
62             sub _add_entry {
63 0     0     my ($self, $file) = @_;
64 0           my $cwd = *$self->{cwd};
65 0 0         croak 'call create() first' unless *$self->{tar_input};
66 0 0         $file = $2 if ($file =~ /^\Q$cwd\E\/(.+)$/); # Relative names
67 0 0         print({ *$self->{tar_input} } "$file\0")
  0            
68             or syserr(g_('write on tar input'));
69             }
70              
71             sub add_file {
72 0     0 0   my ($self, $file) = @_;
73 0           my $testfile = $file;
74 0 0         if (*$self->{chdir}) {
75 0           $testfile = File::Spec->catfile(*$self->{chdir}, $file);
76             }
77 0 0 0       croak 'add_file() does not handle directories'
78             if not -l $testfile and -d _;
79 0           $self->_add_entry($file);
80             }
81              
82             sub add_directory {
83 0     0 0   my ($self, $file) = @_;
84 0           my $testfile = $file;
85 0 0         if (*$self->{chdir}) {
86 0           $testfile = File::Spec->catdir(*$self->{chdir}, $file);
87             }
88 0 0 0       croak 'add_directory() only handles directories'
89             if -l $testfile or not -d _;
90 0           $self->_add_entry($file);
91             }
92              
93             sub finish {
94 0     0 0   my $self = shift;
95              
96 0 0         close(*$self->{tar_input}) or syserr(g_('close on tar input'));
97 0           wait_child(*$self->{pid}, cmdline => 'tar -cf -');
98 0           delete *$self->{pid};
99 0           delete *$self->{tar_input};
100 0           delete *$self->{cwd};
101 0           delete *$self->{chdir};
102 0           $self->close();
103             }
104              
105             sub extract {
106 0     0 0   my ($self, $dest, %opts) = @_;
107 0   0       $opts{options} //= [];
108 0   0       $opts{in_place} //= 0;
109 0   0       $opts{no_fixperms} //= 0;
110 0           my %spawn_opts = (wait_child => 1);
111              
112             # Prepare destination
113 0           my $tmp;
114 0 0         if ($opts{in_place}) {
115 0           $spawn_opts{chdir} = $dest;
116 0           $tmp = $dest; # So that fixperms call works
117             } else {
118 0           my $template = basename($self->get_filename()) . '.tmp-extract.XXXXX';
119 0 0         unless (-e $dest) {
120             # Kludge so that realpath works
121 0 0         mkdir($dest) or syserr(g_('cannot create directory %s'), $dest);
122             }
123 0           $tmp = tempdir($template, DIR => Cwd::realpath("$dest/.."), CLEANUP => 1);
124 0           $spawn_opts{chdir} = $tmp;
125             }
126              
127             # Prepare stuff that handles the input of tar
128 0           $self->ensure_open('r', delete_sig => [ 'PIPE' ]);
129 0           $spawn_opts{from_handle} = $self->get_filehandle();
130              
131             # Call tar extraction process
132 0           $spawn_opts{delete_env} = [ 'TAR_OPTIONS' ];
133             $spawn_opts{exec} = [ $Dpkg::PROGTAR, '-xf', '-', '--no-same-permissions',
134 0           '--no-same-owner', @{$opts{options}} ];
  0            
135 0           spawn(%spawn_opts);
136 0           $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 0 0         fixperms($tmp) unless $opts{no_fixperms};
147              
148             # Stop here if we extracted in-place as there's nothing to move around
149 0 0         return if $opts{in_place};
150              
151             # Rename extracted directory
152 0 0         opendir(my $dir_dh, $tmp) or syserr(g_('cannot opendir %s'), $tmp);
153 0 0         my @entries = grep { $_ ne '.' && $_ ne '..' } readdir($dir_dh);
  0            
154 0           closedir($dir_dh);
155 0           my $done = 0;
156 0           erasedir($dest);
157 0 0 0       if (scalar(@entries) == 1 && ! -l "$tmp/$entries[0]" && -d _) {
      0        
158 0 0         rename("$tmp/$entries[0]", $dest)
159             or syserr(g_('unable to rename %s to %s'),
160             "$tmp/$entries[0]", $dest);
161             } else {
162 0 0         rename($tmp, $dest)
163             or syserr(g_('unable to rename %s to %s'), $tmp, $dest);
164             }
165 0           erasedir($tmp);
166             }
167              
168             1;