1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
|
#!/usr/bin/perl -w
use strict;
# $Id$
# Copyright 1999 Roderick Schertler
# Copyright 2002 Wichert Akkerman <wakkerma@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# For a copy of the GNU General Public License write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# User documentation is at the __END__.
#
# Errors with a single package are warned about but don't affect the
# exit code. Only errors which affect everything cause a non-zero exit.
#
# Dependencies are by request non-existant. I used to use the MD5 and
# Proc::WaitStat modules.
use Getopt::Long ();
my $Exit = 0;
(my $Me = $0) =~ s-.*/--;
my $Version = q$Revision$ =~ /(\d\S+)/ ? $1 : '?';
# %Override is a hash of lists. The subs following describe what's in
# the lists.
my %Override;
sub O_PRIORITY () { 0 }
sub O_SECTION () { 1 }
sub O_MAINT_FROM () { 2 } # undef for non-specific, else listref
sub O_MAINT_TO () { 3 } # undef if there's no maint override
my %Priority = (
'extra' => 1,
'optional' => 2,
'standard' => 3,
'important' => 4,
'required' => 5,
);
# Switches
my $Debug = 0;
my $No_sort = 0;
my $Src_override = undef;
my @Option_spec = (
'debug!' => \$Debug,
'help!' => sub { usage() },
'no-sort|n' => \$No_sort,
'source-override|s=s' => \$Src_override,
'version' => sub { print "$Me version $Version\n"; exit },
);
my $Usage = <<EOF;
usage: $Me [switch]... binary-dir [override-file [path-prefix]] > Sources
switches:
--debug turn debugging on
--help show this and then die
-n, --no-sort don\'t sort by package before outputting
-s, --source-override file
use file for additional source overrides, default
is regular override file with .src appended
--version show the version and exit
See the man page or \`perldoc $Me\' for the full documentation.
EOF
sub debug {
print @_, "\n" if $Debug;
}
sub xwarndie_mess {
my @mess = ("$Me: ", @_);
$mess[$#mess] =~ s/:$/: $!\n/; # XXX loses if it's really /:\n/
return @mess;
}
sub xdie {
die xwarndie_mess @_;
}
sub xwarn {
warn xwarndie_mess @_;
$Exit ||= 1;
}
sub xwarn_noerror {
warn xwarndie_mess @_;
}
sub usage {
xwarn @_ if @_;
die $Usage;
}
# Getopt::Long has some really awful defaults. This function loads it
# then configures it to use more sane settings.
sub getopt;
sub configure_getopt {
Getopt::Long->import(2.11);
*getopt = \&Getopt::Long::GetOptions;
# I'm setting this environment variable lest he sneaks more bad
# defaults into the module.
local $ENV{POSIXLY_CORRECT} = 1;
Getopt::Long::config qw(
default
no_autoabbrev
no_getopt_compat
require_order
bundling
no_ignorecase
);
}
sub close_msg {
my $name = shift;
return "error closing $name (\$? $?, \$! `$!')\n";
}
sub init {
configure_getopt;
getopt @Option_spec or usage;
}
sub load_override {
my $file = shift;
local $_;
open OVERRIDE, $file or xdie "can't read override file $file:";
while (<OVERRIDE>) {
s/#.*//;
next if /^\s*$/;
s/\s+$//;
my @data = split ' ', $_, 4;
unless (@data == 3 || @data == 4) {
xwarn_noerror "invalid override entry at line $. (",
0+@data, " fields)\n";
next;
}
my ($package, $priority, $section, $maintainer) = @data;
if (exists $Override{$package}) {
xwarn_noerror "ignoring duplicate override entry for $package",
" at line $.\n";
next;
}
if (!$Priority{$priority}) {
xwarn_noerror "ignoring override entry for $package,",
" invalid priority $priority\n";
next;
}
$Override{$package} = [];
$Override{$package}[O_PRIORITY] = $priority;
$Override{$package}[O_SECTION] = $section;
if (!defined $maintainer) {
# do nothing
}
elsif ($maintainer =~ /^(.*\S)\s*=>\s*(.*)$/) {
$Override{$package}[O_MAINT_FROM] = [split m-\s*//\s*-, $1];
$Override{$package}[O_MAINT_TO] = $2;
}
else {
$Override{$package}[O_MAINT_TO] = $maintainer;
}
}
close OVERRIDE or xdie "error closing override file:";
}
sub load_src_override {
my ($user_file, $regular_file) = @_;
my ($file);
local $_;
if (defined $user_file) {
$file = $user_file;
}
elsif (defined $regular_file) {
$file = "$regular_file.src";
}
else {
return;
}
debug "source override file $file";
unless (open SRC_OVERRIDE, $file) {
return if !defined $user_file;
xdie "can't read source override file $file:";
}
while (<SRC_OVERRIDE>) {
s/#.*//;
next if /^\s*$/;
s/\s+$//;
my @data = split ' ', $_;
unless (@data == 2) {
xwarn_noerror "invalid source override entry at line $. (",
0+@data, " fields)\n";
next;
}
my ($package, $section) = @data;
my $key = "source/$package";
if (exists $Override{$key}) {
xwarn_noerror "ignoring duplicate source override entry",
" for $package at line $.\n";
next;
}
$Override{$key} = [];
$Override{$key}[O_SECTION] = $section;
}
close SRC_OVERRIDE or xdie "error closing source override file:";
}
# Given FILENAME (for error reporting) and STRING, drop the PGP info
# from the string and undo the encoding (if present) and return it.
sub de_pgp {
my ($file, $s) = @_;
if ($s =~ s/^-----BEGIN PGP SIGNED MESSAGE-----.*?\n\n//s) {
unless ($s =~ s/\n
-----BEGIN\040PGP\040SIGNATURE-----\n
.*?\n
-----END\040PGP\040SIGNATURE-----\n
//xs) {
xwarn_noerror "$file has PGP start token but not end token\n";
return;
}
$s =~ s/^- //mg;
}
return $s;
}
# Load DSC-FILE and return its size, MD5 and translated (de-PGPed)
# contents.
sub read_dsc {
my $file = shift;
my ($size, $md5, $nread, $contents);
unless (open FILE, $file) {
xwarn_noerror "can't read $file:";
return;
}
$size = -s FILE;
unless (defined $size) {
xwarn_noerror "error doing fstat on $file:";
return;
}
$contents = '';
do {
$nread = read FILE, $contents, 16*1024, length $contents;
unless (defined $nread) {
xwarn_noerror "error reading from $file:";
return;
}
} while $nread > 0;
# Rewind the .dsc file and feed it to md5sum as stdin.
my $pid = open MD5, '-|';
unless (defined $pid) {
xwarn_noerror "can't fork:";
return;
}
if (!$pid) {
open STDIN, '<&FILE' or xdie "can't dup $file:";
seek STDIN, 0, 0 or xdie "can't rewind $file:";
exec 'md5sum' or xdie "can't exec md5sum:";
}
chomp($md5 = join '', <MD5>);
unless (close MD5) {
xwarn_noerror close_msg 'md5sum';
return;
}
$md5 =~ s/ *-$//; # Remove trailing spaces and -, to work with GNU md5sum
unless (length($md5) == 32 && $md5 !~ /[^\da-f]/i) {
xwarn_noerror "invalid md5 output for $file ($md5)\n";
return;
}
unless (close FILE) {
xwarn_noerror "error closing $file:";
return;
}
$contents = de_pgp $file, $contents;
return unless defined $contents;
return $size, $md5, $contents;
}
# Given PREFIX and DSC-FILE, process the file and returning the source
# package name and index record.
sub process_dsc {
my ($prefix, $file) = @_;
my ($source, @binary, $priority, $section, $maintainer_override,
$dir, $dir_field, $dsc_field_start);
my ($size, $md5, $contents) = read_dsc $file or return;
# Allow blank lines at the end of a file, because the other programs
# do.
$contents =~ s/\n\n+\Z/\n/;
if ($contents =~ /^\n/ || $contents =~ /\n\n/) {
xwarn_noerror "$file invalid (contains blank line)\n";
return;
}
# Take the $contents and create a list of (possibly multi-line)
# fields. Fields can be continued by starting the next line with
# white space. The tricky part is I don't want to modify the data
# at all, so I can't just collapse continued fields.
#
# Implementation is to start from the last line and work backwards
# to the second. If this line starts with space, append it to the
# previous line and undef it. When done drop the undef entries.
my @line = split /\n/, $contents;
for (my $i = $#line; $i > 0; $i--) {
if ($line[$i] =~ /^\s/) {
$line[$i-1] .= "\n$line[$i]";
$line[$i] = undef;
}
}
my @field = map { "$_\n" } grep { defined } @line;
# Extract information from the record.
for my $orig_field (@field) {
my $s = $orig_field;
$s =~ s/\s+$//;
$s =~ s/\n\s+/ /g;
unless ($s =~ s/^([^:\s]+):\s*//) {
xwarn_noerror "invalid field in $file: $orig_field";
return;
}
my ($key, $val) = (lc $1, $s);
# $source
if ($key eq 'source') {
if (defined $source) {
xwarn_noerror "duplicate source field in $file\n";
return;
}
if ($val =~ /\s/) {
xwarn_noerror "invalid source field in $file\n";
return;
}
$source = $val;
next;
}
# @binary
if ($key eq 'binary') {
if (@binary) {
xwarn_noerror "duplicate binary field in $file\n";
return;
}
@binary = split /\s*,\s*/, $val;
unless (@binary) {
xwarn_noerror "no binary packages specified in $file\n";
return;
}
}
}
# The priority for the source package is the highest priority of the
# binary packages it produces.
my @binary_by_priority = sort {
($Override{$a} ? $Priority{$Override{$a}[O_PRIORITY]} : 0)
<=>
($Override{$b} ? $Priority{$Override{$b}[O_PRIORITY]} : 0)
} @binary;
my $priority_override = $Override{$binary_by_priority[-1]};
$priority = $priority_override
? $priority_override->[O_PRIORITY]
: undef;
# For the section override, first check for a record from the source
# override file, else use the regular override file.
my $section_override = $Override{"source/$source"} || $Override{$source};
$section = $section_override
? $section_override->[O_SECTION]
: undef;
# For the maintainer override, use the override record for the first
# binary.
$maintainer_override = $Override{$binary[0]};
# A directory field will be inserted just before the files field.
$dir = ($file =~ s-(.*)/--) ? $1 : '';
$dir = "$prefix$dir";
$dir =~ s-/+$--;
$dir = '.' if $dir eq '';
$dir_field .= "Directory: $dir\n";
# The files field will get an entry for the .dsc file itself.
$dsc_field_start = "Files:\n $md5 $size $file\n";
# Loop through @field, doing nececessary processing and building up
# @new_field.
my @new_field;
for (@field) {
# Rename the source field to package.
s/^Source:/Package:/i;
# Override the user's priority field.
if (/^Priority:/i && defined $priority) {
$_ = "Priority: $priority\n";
undef $priority;
}
# Override the user's section field.
if (/^Section:/i && defined $section) {
$_ = "Section: $section\n";
undef $section;
}
# Insert the directory line just before the files entry, and add
# the dsc file to the files list.
if (defined $dir_field && s/^Files:\s*//i) {
push @new_field, $dir_field;
$dir_field = undef;
$_ = " $_" if length;
$_ = "$dsc_field_start$_";
}
# Modify the maintainer if necessary.
if ($maintainer_override
&& defined $maintainer_override->[O_MAINT_TO]
&& /^Maintainer:\s*(.*)\n/is) {
my $maintainer = $1;
$maintainer =~ s/\n\s+/ /g;
if (!defined $maintainer_override->[O_MAINT_FROM]
|| grep { $maintainer eq $_ }
@{ $maintainer_override->[O_MAINT_FROM] }){
$_ = "Maintainer: $maintainer_override->[O_MAINT_TO]\n";
}
}
}
continue {
push @new_field, $_ if defined $_;
}
# If there was no files entry, add one.
if (defined $dir_field) {
push @new_field, $dir_field;
push @new_field, $dsc_field_start;
}
# Add the section field if it didn't override one the user supplied.
if (defined $section) {
# If the record starts with a package field put it after that,
# otherwise put it first.
my $pos = $new_field[0] =~ /^Package:/i ? 1 : 0;
splice @new_field, $pos, 0, "Section: $section\n";
}
# Add the priority field if it didn't override one the user supplied.
if (defined $priority) {
# If the record starts with a package field put it after that,
# otherwise put it first.
my $pos = $new_field[0] =~ /^Package:/i ? 1 : 0;
splice @new_field, $pos, 0, "Priority: $priority\n";
}
return $source, join '', @new_field, "\n";
}
sub main {
my (@out);
init;
@ARGV >= 1 && @ARGV <= 3 or usage "1 to 3 args expected\n";
push @ARGV, undef if @ARGV < 2;
push @ARGV, '' if @ARGV < 3;
my ($dir, $override, $prefix) = @ARGV;
load_override $override if defined $override;
load_src_override $Src_override, $override;
open FIND, "find \Q$dir\E -follow -name '*.dsc' -print |"
or xdie "can't fork:";
while (<FIND>) {
chomp;
s-^\./+--;
my ($source, $out) = process_dsc $prefix, $_ or next;
if ($No_sort) {
print $out;
}
else {
push @out, [$source, $out];
}
}
close FIND or xdie close_msg 'find';
if (@out) {
print map { $_->[1] } sort { $a->[0] cmp $b->[0] } @out;
}
return 0;
}
$Exit = main || $Exit;
$Exit = 1 if $Exit and not $Exit % 256;
exit $Exit;
__END__
=head1 NAME
dpkg-scansources - search for '.dsc' files and build the 'Sources' index
=head1 SYNOPSIS
B<dpkg-scansources> [switch]... I<binary-dir> [I<override-file>
[I<path-prefix>]] > Sources
=head1 DESCRIPTION
B<dpkg-scansources> scans the given I<binary-dir> for F<.dsc> files.
These are used to create a Debian source index, which is output to
stdout.
The I<override-file>, if given, is used to set priorities in the resulting
index records and to override the maintainer field given in the F<.dsc>
files. See L<dpkg-scanpackages> for the format of this file. NB: Since
the override file is indexed by binary, not source, packages, there's a bit
of a problem here. The current implementation uses the highest priority of
all the binary packages produced by a F<.dsc> file for the priority of the
source package, and the override entry for the first binary package listed
in the F<.dsc> file to modify maintainer information. This might change.
The I<path-prefix>, if given, is prepended to the directory field in the
generated source index. You generally use this to make the directory
fields contain the path from the top of the Debian archive hierarchy.
=head1 OPTIONS
=over 4
=item B<--debug>
Turn debugging on.
=item B<--help>
Show the usage message and die.
=item B<-n>, B<--no-sort>
Don't sort the index records. Normally they are sorted by source package
name.
=item B<-s>, B<--source-override> I<file>
Use I<file> as the source override file. The default is the name of the
override file you specified with I<.src> appended.
The source override file is in a different format from the binary override
file. It contains only two whitespace separated fields, the first is the
source package name and the second is the section. Blank lines and comment
lines are ignored in the normal manner. If a package appears in both files
the source override takes precedence for setting the section.
=item B<--version>
Print the version number and exit.
=back
=head1 SEE ALSO
dpkg-scanpackages(8)
=head1 AUTHOR
Roderick Schertler <roderick@argon.org>
=cut
|