5 # Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <https://www.gnu.org/licenses/>.
23 use Getopt
::Long
qw(:config posix_default bundling_values no_ignorecase);
24 use List
::Util
qw(none);
29 use Dpkg
::ErrorHandling
;
33 use Dpkg
::Compression
::FileHandle
;
35 textdomain
('dpkg-dev');
37 # Do not pollute STDOUT with info messages
38 report_options
(info_fh
=> \
*STDERR
);
40 my (@samemaint, @changedmaint);
48 help
=> sub { usage
(); exit 0; },
49 version
=> sub { version
(); exit 0; },
54 'extra-override' => undef,
70 printf g_
("Debian %s version %s.\n"), $Dpkg::PROGNAME
, $Dpkg::PROGVERSION
;
75 "Usage: %s [<option>...] <binary-path> [<override-file> [<path-prefix>]] > Packages
78 -t, --type <type> scan for <type> packages (default is 'deb').
79 -a, --arch <arch> architecture to scan for.
80 -h, --hash <hash-list> only generate hashes for the specified list.
81 -m, --multiversion allow multiple versions of a single package.
82 -e, --extra-override <file>
83 use extra override file.
84 -M, --medium <medium> add X-Medium field for dselect media access method
85 -?, --help show this help message.
86 --version show the version.
93 my $comp_file = Dpkg
::Compression
::FileHandle
->new(filename
=> $override);
95 while (<$comp_file>) {
100 my ($p, $priority, $section, $maintainer) = split(/\s+/, $_, 4);
102 if (not defined($packages{$p})) {
103 push(@spuriousover, $p);
107 for my $package (@
{$packages{$p}}) {
109 if ($maintainer =~ m/(.+?)\s*=\>\s*(.+)/) {
112 my $debmaint = $$package{Maintainer
};
113 if (none
{ $debmaint eq $_ } split m{\s*//\s*}, $oldmaint) {
115 sprintf(g_
(' %s (package says %s, not %s)'),
116 $p, $$package{Maintainer
}, $oldmaint));
118 $$package{Maintainer
} = $newmaint;
120 } elsif ($$package{Maintainer
} eq $maintainer) {
121 push(@samemaint, " $p ($maintainer)");
123 warning
(g_
('unconditional maintainer override for %s'), $p);
124 $$package{Maintainer
} = $maintainer;
127 $$package{Priority
} = $priority;
128 $$package{Section
} = $section;
136 sub load_override_extra
138 my $extra_override = shift;
139 my $comp_file = Dpkg
::Compression
::FileHandle
->new(filename
=> $extra_override);
141 while (<$comp_file>) {
146 my ($p, $field, $value) = split(/\s+/, $_, 3);
148 next unless defined($packages{$p});
150 for my $package (@
{$packages{$p}}) {
151 $$package{$field} = $value;
159 my ($pathprefix, $fn) = @_;
161 my $fields = Dpkg
::Control
->new(type
=> CTRL_INDEX_PKG
);
163 open my $output_fh, '-|', 'dpkg-deb', '-I', $fn, 'control'
164 or syserr
(g_
('cannot fork for %s'), 'dpkg-deb');
165 $fields->parse($output_fh, $fn)
166 or error
(g_
("couldn't parse control information from %s"), $fn);
169 warning
(g_
("'dpkg-deb -I %s control' exited with %d, skipping package"),
174 my $p = $fields->{'Package'};
175 error
(g_
('no Package field in control file of %s'), $fn)
178 if (defined($packages{$p}) and not $options{multiversion
}) {
179 my $pkg = ${$packages{$p}}[0];
181 @multi_instances = ($pkg->{Filename
}) if @multi_instances == 0;
182 push @multi_instances, "$pathprefix$fn";
184 if (version_compare_relation
($fields->{'Version'}, REL_GT
,
187 warning
(g_
('package %s (filename %s) is repeat but newer ' .
188 'version; used that one and ignored data from %s!'),
189 $p, $fn, $pkg->{Filename
});
192 warning
(g_
('package %s (filename %s) is repeat; ' .
193 'ignored that one and using data from %s!'),
194 $p, $fn, $pkg->{Filename
});
199 warning
(g_
('package %s (filename %s) has Filename field!'), $p, $fn)
200 if defined($fields->{'Filename'});
201 $fields->{'Filename'} = "$pathprefix$fn";
203 my $sums = Dpkg
::Checksums
->new();
204 $sums->add_from_file($fn, checksums
=> \
@checksums);
205 foreach my $alg (@checksums) {
207 $fields->{'MD5sum'} = $sums->get_checksum($fn, $alg);
209 $fields->{$alg} = $sums->get_checksum($fn, $alg);
212 $fields->{'Size'} = $sums->get_size($fn);
213 $fields->{'X-Medium'} = $options{medium
} if defined $options{medium
};
215 push @
{$packages{$p}}, $fields;
219 local $SIG{__WARN__
} = sub { usageerr
($_[0]) };
220 GetOptions
(\
%options, @options_spec);
223 if (not 1 <= @ARGV <= 3) {
224 usageerr
(g_
('one to three arguments expected'));
227 my $type = $options{type
} // 'deb';
228 my $arch = $options{arch
};
229 my %hash = map { $_ => 1 } split /,/, $options{hash
} // '';
231 foreach my $alg (keys %hash) {
232 if (not checksums_is_supported
($alg)) {
233 usageerr
(g_
('unsupported checksum \'%s\''), $alg);
236 @checksums = %hash ?
keys %hash : checksums_get_list
();
238 my ($binarypath, $override, $pathprefix) = @ARGV;
240 if (not -e
$binarypath) {
241 error
(g_
('binary path %s not found'), $binarypath);
243 if (defined $override and not -e
$override) {
244 error
(g_
('override file %s not found'), $override);
250 if ($options{arch
}) {
251 $find_filter = qr/_(?:all|${arch})\.$type$/;
253 $find_filter = qr/\.$type$/;
256 my $scan_archives = sub {
257 push @archives, $File::Find
::name
if m/$find_filter/;
260 find
({ follow
=> 1, follow_skip
=> 2, wanted
=> $scan_archives}, $binarypath);
261 foreach my $fn (@archives) {
262 process_deb
($pathprefix, $fn);
265 load_override
($override) if defined $override;
266 load_override_extra
($options{'extra-override'}) if defined $options{'extra-override'};
268 my @missingover = ();
270 my $records_written = 0;
271 for my $p (sort keys %packages) {
272 if (defined($override) and not defined($overridden{$p})) {
273 push @missingover, $p;
275 for my $package (sort { $a->{Version
} cmp $b->{Version
} } @
{$packages{$p}}) {
276 print("$package\n") or syserr
(g_
('failed when writing stdout'));
280 close(STDOUT
) or syserr
(g_
("couldn't close stdout"));
282 if (@multi_instances) {
283 warning
(g_
('Packages with multiple instances but no --multiversion specified:'));
284 warning
($_) foreach (sort @multi_instances);
287 warning
(g_
('Packages in override file with incorrect old maintainer value:'));
288 warning
($_) foreach (@changedmaint);
291 warning
(g_
('Packages specifying same maintainer as override file:'));
292 warning
($_) foreach (@samemaint);
295 warning
(g_
('Packages in archive but missing from override file:'));
296 warning
(' %s', join(' ', @missingover));
299 warning
(g_
('Packages in override file but not in archive:'));
300 warning
(' %s', join(' ', @spuriousover));
303 info
(g_
('Wrote %s entries to output Packages file.'), $records_written);