dpkg-scansources.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. #!/usr/bin/perl
  2. #
  3. # Copyright © 1999 Roderick Schertler
  4. # Copyright © 2002 Wichert Akkerman <wakkerma@debian.org>
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or (at
  9. # your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. # General Public License for more details.
  15. #
  16. # For a copy of the GNU General Public License write to the Free Software
  17. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. # Errors with a single package are warned about but don't affect the
  19. # exit code. Only errors which affect everything cause a non-zero exit.
  20. #
  21. # Dependencies are by request non-existant. I used to use the MD5 and
  22. # Proc::WaitStat modules.
  23. use strict;
  24. use warnings;
  25. use Dpkg;
  26. use Dpkg::Gettext;
  27. use Dpkg::ErrorHandling;
  28. use Dpkg::Checksums;
  29. textdomain("dpkg-dev");
  30. use Getopt::Long ();
  31. my $Exit = 0;
  32. # %Override is a hash of lists. The subs following describe what's in
  33. # the lists.
  34. my %Override;
  35. sub O_PRIORITY () { 0 }
  36. sub O_SECTION () { 1 }
  37. sub O_MAINT_FROM () { 2 } # undef for non-specific, else listref
  38. sub O_MAINT_TO () { 3 } # undef if there's no maint override
  39. my %Priority = (
  40. 'extra' => 1,
  41. 'optional' => 2,
  42. 'standard' => 3,
  43. 'important' => 4,
  44. 'required' => 5,
  45. );
  46. # Switches
  47. my $Debug = 0;
  48. my $No_sort = 0;
  49. my $Src_override = undef;
  50. my @Option_spec = (
  51. 'debug!' => \$Debug,
  52. 'help!' => \&usage,
  53. 'no-sort|n' => \$No_sort,
  54. 'source-override|s=s' => \$Src_override,
  55. 'version' => \&version,
  56. );
  57. sub debug {
  58. print @_, "\n" if $Debug;
  59. }
  60. sub version {
  61. printf _g("Debian %s version %s.\n"), $progname, $version;
  62. exit;
  63. }
  64. sub usage {
  65. printf _g(
  66. "Usage: %s [<option> ...] <binarypath> [<overridefile> [<pathprefix>]] > Sources
  67. Options:
  68. -n, --no-sort don't sort by package before outputting.
  69. -s, --source-override <file>
  70. use file for additional source overrides, default
  71. is regular override file with .src appended.
  72. --debug turn debugging on.
  73. --help show this help message.
  74. --version show the version.
  75. See the man page for the full documentation.
  76. "), $progname;
  77. exit;
  78. }
  79. # Getopt::Long has some really awful defaults. This function loads it
  80. # then configures it to use more sane settings.
  81. sub getopt(@);
  82. sub configure_getopt {
  83. Getopt::Long->import(2.11);
  84. *getopt = \&Getopt::Long::GetOptions;
  85. # I'm setting this environment variable lest he sneaks more bad
  86. # defaults into the module.
  87. local $ENV{POSIXLY_CORRECT} = 1;
  88. Getopt::Long::config qw(
  89. default
  90. no_autoabbrev
  91. no_getopt_compat
  92. require_order
  93. bundling
  94. no_ignorecase
  95. );
  96. }
  97. sub close_msg {
  98. my $name = shift;
  99. return sprintf(_g("error closing %s (\$? %d, \$! `%s')"),
  100. $name, $?, $!)."\n";
  101. }
  102. sub init {
  103. configure_getopt;
  104. getopt @Option_spec or usage;
  105. }
  106. sub load_override {
  107. my $file = shift;
  108. local $_;
  109. open OVERRIDE, $file or syserr(_g("can't read override file %s"), $file);
  110. while (<OVERRIDE>) {
  111. s/#.*//;
  112. next if /^\s*$/;
  113. s/\s+$//;
  114. my @data = split ' ', $_, 4;
  115. unless (@data == 3 || @data == 4) {
  116. warning(_g("invalid override entry at line %d (%d fields)"),
  117. $., 0 + @data);
  118. next;
  119. }
  120. my ($package, $priority, $section, $maintainer) = @data;
  121. if (exists $Override{$package}) {
  122. warning(_g("ignoring duplicate override entry for %s at line %d"),
  123. $package, $.);
  124. next;
  125. }
  126. if (!$Priority{$priority}) {
  127. warning(_g("ignoring override entry for %s, invalid priority %s"),
  128. $package, $priority);
  129. next;
  130. }
  131. $Override{$package} = [];
  132. $Override{$package}[O_PRIORITY] = $priority;
  133. $Override{$package}[O_SECTION] = $section;
  134. if (!defined $maintainer) {
  135. # do nothing
  136. }
  137. elsif ($maintainer =~ /^(.*\S)\s*=>\s*(.*)$/) {
  138. $Override{$package}[O_MAINT_FROM] = [split m-\s*//\s*-, $1];
  139. $Override{$package}[O_MAINT_TO] = $2;
  140. }
  141. else {
  142. $Override{$package}[O_MAINT_TO] = $maintainer;
  143. }
  144. }
  145. close OVERRIDE or syserr(_g("error closing override file"));
  146. }
  147. sub load_src_override {
  148. my ($user_file, $regular_file) = @_;
  149. my ($file);
  150. local $_;
  151. if (defined $user_file) {
  152. $file = $user_file;
  153. }
  154. elsif (defined $regular_file) {
  155. $file = "$regular_file.src";
  156. }
  157. else {
  158. return;
  159. }
  160. debug "source override file $file";
  161. unless (open SRC_OVERRIDE, $file) {
  162. return if !defined $user_file;
  163. syserr(_g("can't read source override file %s"), $file);
  164. }
  165. while (<SRC_OVERRIDE>) {
  166. s/#.*//;
  167. next if /^\s*$/;
  168. s/\s+$//;
  169. my @data = split ' ', $_;
  170. unless (@data == 2) {
  171. warning(_g("invalid source override entry at line %d (%d fields)"),
  172. $., 0 + @data);
  173. next;
  174. }
  175. my ($package, $section) = @data;
  176. my $key = "source/$package";
  177. if (exists $Override{$key}) {
  178. warning(_g("ignoring duplicate source override entry for %s at line %d"),
  179. $package, $.);
  180. next;
  181. }
  182. $Override{$key} = [];
  183. $Override{$key}[O_SECTION] = $section;
  184. }
  185. close SRC_OVERRIDE or syserr(_g("error closing source override file"));
  186. }
  187. # Given FILENAME (for error reporting) and STRING, drop the PGP info
  188. # from the string and undo the encoding (if present) and return it.
  189. sub de_pgp {
  190. my ($file, $s) = @_;
  191. if ($s =~ s/^-----BEGIN PGP SIGNED MESSAGE-----.*?\n\n//s) {
  192. unless ($s =~ s/\n
  193. -----BEGIN\040PGP\040SIGNATURE-----\n
  194. .*?\n
  195. -----END\040PGP\040SIGNATURE-----\n
  196. //xs) {
  197. warning(_g("%s has PGP start token but not end token"), $file);
  198. return;
  199. }
  200. $s =~ s/^- //mg;
  201. }
  202. return $s;
  203. }
  204. # Load DSC-FILE and return its size, MD5 and translated (de-PGPed)
  205. # contents.
  206. sub read_dsc {
  207. my $file = shift;
  208. my ($size, $nread, $contents);
  209. unless (open FILE, $file) {
  210. warning(_g("can't read %s: %s"), $file, $!);
  211. return;
  212. }
  213. $contents = '';
  214. do {
  215. $nread = read FILE, $contents, 16*1024, length $contents;
  216. unless (defined $nread) {
  217. warning(_g("error reading from %s: %s"), $file, $!);
  218. return;
  219. }
  220. } while $nread > 0;
  221. # Get checksums
  222. my $sums = {};
  223. getchecksums($file, $sums, \$size);
  224. unless (close FILE) {
  225. warning(_g("error closing %s: %s"), $file, $!);
  226. return;
  227. }
  228. $contents = de_pgp $file, $contents;
  229. return unless defined $contents;
  230. return $size, $sums, $contents;
  231. }
  232. # Given PREFIX and DSC-FILE, process the file and returning the source
  233. # package name and index record.
  234. sub process_dsc {
  235. my ($prefix, $file) = @_;
  236. my ($source, @binary, $priority, $section, $maintainer_override,
  237. $dir, $dir_field, $dsc_field_start);
  238. my ($size, $sums, $contents) = read_dsc $file or return;
  239. # Allow blank lines at the end of a file, because the other programs
  240. # do.
  241. $contents =~ s/\n\n+\Z/\n/;
  242. if ($contents =~ /^\n/ || $contents =~ /\n\n/) {
  243. warning(_g("%s invalid (contains blank line)"), $file);
  244. return;
  245. }
  246. # Take the $contents and create a list of (possibly multi-line)
  247. # fields. Fields can be continued by starting the next line with
  248. # white space. The tricky part is I don't want to modify the data
  249. # at all, so I can't just collapse continued fields.
  250. #
  251. # Implementation is to start from the last line and work backwards
  252. # to the second. If this line starts with space, append it to the
  253. # previous line and undef it. When done drop the undef entries.
  254. my @line = split /\n/, $contents;
  255. for (my $i = $#line; $i > 0; $i--) {
  256. if ($line[$i] =~ /^\s/) {
  257. $line[$i-1] .= "\n$line[$i]";
  258. $line[$i] = undef;
  259. }
  260. }
  261. my @field = map { "$_\n" } grep { defined } @line;
  262. # Extract information from the record.
  263. for my $orig_field (@field) {
  264. my $s = $orig_field;
  265. $s =~ s/\s+$//;
  266. $s =~ s/\n\s+/ /g;
  267. unless ($s =~ s/^([^:\s]+):\s*//) {
  268. warning(_g("invalid field in %s: %s"), $file, $orig_field);
  269. return;
  270. }
  271. my ($key, $val) = (lc $1, $s);
  272. # $source
  273. if ($key eq 'source') {
  274. if (defined $source) {
  275. warning(_g("duplicate source field in %s"), $file);
  276. return;
  277. }
  278. if ($val =~ /\s/) {
  279. warning(_g("invalid source field in %s"), $file);
  280. return;
  281. }
  282. $source = $val;
  283. next;
  284. }
  285. # @binary
  286. if ($key eq 'binary') {
  287. if (@binary) {
  288. warning(_g("duplicate binary field in %s"), $file);
  289. return;
  290. }
  291. @binary = split /\s*,\s*/, $val;
  292. unless (@binary) {
  293. warning(_g("no binary packages specified in %s"), $file);
  294. return;
  295. }
  296. }
  297. }
  298. # The priority for the source package is the highest priority of the
  299. # binary packages it produces.
  300. my @binary_by_priority = sort {
  301. ($Override{$a} ? $Priority{$Override{$a}[O_PRIORITY]} : 0)
  302. <=>
  303. ($Override{$b} ? $Priority{$Override{$b}[O_PRIORITY]} : 0)
  304. } @binary;
  305. my $priority_override = $Override{$binary_by_priority[-1]};
  306. $priority = $priority_override
  307. ? $priority_override->[O_PRIORITY]
  308. : undef;
  309. # For the section override, first check for a record from the source
  310. # override file, else use the regular override file.
  311. my $section_override = $Override{"source/$source"} || $Override{$source};
  312. $section = $section_override
  313. ? $section_override->[O_SECTION]
  314. : undef;
  315. # For the maintainer override, use the override record for the first
  316. # binary.
  317. $maintainer_override = $Override{$binary[0]};
  318. # A directory field will be inserted just before the files field.
  319. $dir = ($file =~ s-(.*)/--) ? $1 : '';
  320. $dir = "$prefix$dir";
  321. $dir =~ s-/+$--;
  322. $dir = '.' if $dir eq '';
  323. $dir_field .= "Directory: $dir\n";
  324. # The files field will get an entry for the .dsc file itself.
  325. my %listing;
  326. foreach my $alg (@check_supported) {
  327. if ($alg eq "md5") {
  328. $listing{$alg} = "Files:\n $sums->{$alg} $size $file\n";
  329. } else {
  330. $listing{$alg} = "Checksum-" . ucfirst($alg) .
  331. ":\n $sums->{$alg} $size $file\n";
  332. }
  333. }
  334. # Loop through @field, doing nececessary processing and building up
  335. # @new_field.
  336. my @new_field;
  337. for (@field) {
  338. # Rename the source field to package.
  339. s/^Source:/Package:/i;
  340. # Override the user's priority field.
  341. if (/^Priority:/i && defined $priority) {
  342. $_ = "Priority: $priority\n";
  343. undef $priority;
  344. }
  345. # Override the user's section field.
  346. if (/^Section:/i && defined $section) {
  347. $_ = "Section: $section\n";
  348. undef $section;
  349. }
  350. # Insert the directory line just before the files entry, and add
  351. # the dsc file to the files list.
  352. if (defined $dir_field && s/^Files:\s*//i) {
  353. push @new_field, $dir_field;
  354. $dir_field = undef;
  355. $_ = " $_" if length;
  356. $_ = "$listing{md5}$_";
  357. }
  358. if (/Checksums-(.*):/i) {
  359. my $alg = lc($1);
  360. s/Checksums-([^:]*):\s*//i;
  361. $_ = " $_" if length;
  362. $_ = "$listing{$alg}$_";
  363. }
  364. # Modify the maintainer if necessary.
  365. if ($maintainer_override
  366. && defined $maintainer_override->[O_MAINT_TO]
  367. && /^Maintainer:\s*(.*)\n/is) {
  368. my $maintainer = $1;
  369. $maintainer =~ s/\n\s+/ /g;
  370. if (!defined $maintainer_override->[O_MAINT_FROM]
  371. || grep { $maintainer eq $_ }
  372. @{ $maintainer_override->[O_MAINT_FROM] }){
  373. $_ = "Maintainer: $maintainer_override->[O_MAINT_TO]\n";
  374. }
  375. }
  376. }
  377. continue {
  378. push @new_field, $_ if defined $_;
  379. }
  380. # If there was no files entry, add one.
  381. if (defined $dir_field) {
  382. push @new_field, $dir_field;
  383. push @new_field, $dsc_field_start;
  384. }
  385. # Add the section field if it didn't override one the user supplied.
  386. if (defined $section) {
  387. # If the record starts with a package field put it after that,
  388. # otherwise put it first.
  389. my $pos = $new_field[0] =~ /^Package:/i ? 1 : 0;
  390. splice @new_field, $pos, 0, "Section: $section\n";
  391. }
  392. # Add the priority field if it didn't override one the user supplied.
  393. if (defined $priority) {
  394. # If the record starts with a package field put it after that,
  395. # otherwise put it first.
  396. my $pos = $new_field[0] =~ /^Package:/i ? 1 : 0;
  397. splice @new_field, $pos, 0, "Priority: $priority\n";
  398. }
  399. return $source, join '', @new_field, "\n";
  400. }
  401. sub main {
  402. my (@out);
  403. init;
  404. @ARGV >= 1 && @ARGV <= 3 or usageerr(_g("1 to 3 args expected\n"));
  405. push @ARGV, undef if @ARGV < 2;
  406. push @ARGV, '' if @ARGV < 3;
  407. my ($dir, $override, $prefix) = @ARGV;
  408. load_override $override if defined $override;
  409. load_src_override $Src_override, $override;
  410. open FIND, "find \Q$dir\E -follow -name '*.dsc' -print |"
  411. or syserr(_g("can't fork"));
  412. while (<FIND>) {
  413. chomp;
  414. s-^\./+--;
  415. my ($source, $out) = process_dsc $prefix, $_ or next;
  416. if ($No_sort) {
  417. print $out;
  418. }
  419. else {
  420. push @out, [$source, $out];
  421. }
  422. }
  423. close FIND or error(close_msg, 'find');
  424. if (@out) {
  425. print map { $_->[1] } sort { $a->[0] cmp $b->[0] } @out;
  426. }
  427. return 0;
  428. }
  429. $Exit = main || $Exit;
  430. $Exit = 1 if $Exit and not $Exit % 256;
  431. exit $Exit;