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