dpkg-scanpackages.pl 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-scanpackages
  4. #
  5. # Copyright © 2006-2012 Guillem Jover <guillem@debian.org>
  6. #
  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.
  11. #
  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.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. use warnings;
  20. use strict;
  21. use IO::Handle;
  22. use IO::File;
  23. use Getopt::Long qw(:config posix_default bundling no_ignorecase);
  24. use Dpkg ();
  25. use Dpkg::Gettext;
  26. use Dpkg::ErrorHandling;
  27. use Dpkg::Util qw(:list);
  28. use Dpkg::Control;
  29. use Dpkg::Version;
  30. use Dpkg::Checksums;
  31. use Dpkg::Compression::FileHandle;
  32. use Dpkg::IPC;
  33. textdomain('dpkg-dev');
  34. # Do not pollute STDOUT with info messages
  35. report_options(info_fh => \*STDERR);
  36. my (@samemaint, @changedmaint);
  37. my @spuriousover;
  38. my %packages;
  39. my %overridden;
  40. my %options = (help => sub { usage(); exit 0; },
  41. version => \&version,
  42. type => undef,
  43. arch => undef,
  44. multiversion => 0,
  45. 'extra-override'=> undef,
  46. medium => undef,
  47. );
  48. my @options_spec = (
  49. 'help|?',
  50. 'version',
  51. 'type|t=s',
  52. 'arch|a=s',
  53. 'multiversion|m!',
  54. 'extra-override|e=s',
  55. 'medium|M=s',
  56. );
  57. sub version {
  58. printf _g("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  59. exit;
  60. }
  61. sub usage {
  62. printf _g(
  63. "Usage: %s [<option>...] <binary-path> [<override-file> [<path-prefix>]] > Packages
  64. Options:
  65. -t, --type <type> scan for <type> packages (default is 'deb').
  66. -a, --arch <arch> architecture to scan for.
  67. -m, --multiversion allow multiple versions of a single package.
  68. -e, --extra-override <file>
  69. use extra override file.
  70. -M, --medium <medium> add X-Medium field for dselect multicd access method
  71. -?, --help show this help message.
  72. --version show the version.
  73. "), $Dpkg::PROGNAME;
  74. }
  75. sub load_override
  76. {
  77. my $override = shift;
  78. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $override);
  79. while (<$comp_file>) {
  80. s/\#.*//;
  81. s/\s+$//;
  82. next unless $_;
  83. my ($p, $priority, $section, $maintainer) = split(/\s+/, $_, 4);
  84. if (not defined($packages{$p})) {
  85. push(@spuriousover, $p);
  86. next;
  87. }
  88. for my $package (@{$packages{$p}}) {
  89. if ($maintainer) {
  90. if ($maintainer =~ m/(.+?)\s*=\>\s*(.+)/) {
  91. my $oldmaint = $1;
  92. my $newmaint = $2;
  93. my $debmaint = $$package{Maintainer};
  94. if (none { $debmaint eq $_ } split m{\s*//\s*}, $oldmaint) {
  95. push(@changedmaint,
  96. sprintf(_g(' %s (package says %s, not %s)'),
  97. $p, $$package{Maintainer}, $oldmaint));
  98. } else {
  99. $$package{Maintainer} = $newmaint;
  100. }
  101. } elsif ($$package{Maintainer} eq $maintainer) {
  102. push(@samemaint, " $p ($maintainer)");
  103. } else {
  104. warning(_g('unconditional maintainer override for %s'), $p);
  105. $$package{Maintainer} = $maintainer;
  106. }
  107. }
  108. $$package{Priority} = $priority;
  109. $$package{Section} = $section;
  110. }
  111. $overridden{$p} = 1;
  112. }
  113. close($comp_file);
  114. }
  115. sub load_override_extra
  116. {
  117. my $extra_override = shift;
  118. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $extra_override);
  119. while (<$comp_file>) {
  120. s/\#.*//;
  121. s/\s+$//;
  122. next unless $_;
  123. my ($p, $field, $value) = split(/\s+/, $_, 3);
  124. next unless defined($packages{$p});
  125. for my $package (@{$packages{$p}}) {
  126. $$package{$field} = $value;
  127. }
  128. }
  129. close($comp_file);
  130. }
  131. {
  132. local $SIG{__WARN__} = sub { usageerr($_[0]) };
  133. GetOptions(\%options, @options_spec);
  134. }
  135. if (not (@ARGV >= 1 and @ARGV <= 3)) {
  136. usageerr(_g('one to three arguments expected'));
  137. }
  138. my $type = defined($options{type}) ? $options{type} : 'deb';
  139. my $arch = $options{arch};
  140. my @find_args;
  141. if ($options{arch}) {
  142. @find_args = ('(', '-name', "*_all.$type", '-o',
  143. '-name', "*_${arch}.$type", ')');
  144. }
  145. else {
  146. @find_args = ('-name', "*.$type");
  147. }
  148. my ($binarydir, $override, $pathprefix) = @ARGV;
  149. -d $binarydir or error(_g('binary dir %s not found'), $binarydir);
  150. defined($override) and (-e $override or
  151. error(_g('override file %s not found'), $override));
  152. $pathprefix //= '';
  153. my $find_h = IO::Handle->new();
  154. open($find_h, '-|', 'find', '-L', "$binarydir/", @find_args, '-print')
  155. or syserr(_g("couldn't open %s for reading"), $binarydir);
  156. FILE:
  157. while (<$find_h>) {
  158. chomp;
  159. my $fn = $_;
  160. my $output;
  161. my $pid = spawn(exec => [ 'dpkg-deb', '-I', $fn, 'control' ],
  162. to_pipe => \$output);
  163. my $fields = Dpkg::Control->new(type => CTRL_INDEX_PKG);
  164. $fields->parse($output, $fn)
  165. or error(_g("couldn't parse control information from %s"), $fn);
  166. wait_child($pid, no_check => 1);
  167. if ($?) {
  168. warning(_g("\`dpkg-deb -I %s control' exited with %d, skipping package"),
  169. $fn, $?);
  170. next;
  171. }
  172. defined($fields->{'Package'})
  173. or error(_g('no Package field in control file of %s'), $fn);
  174. my $p = $fields->{'Package'};
  175. if (defined($packages{$p}) and not $options{multiversion}) {
  176. foreach (@{$packages{$p}}) {
  177. if (version_compare_relation($fields->{'Version'}, REL_GT,
  178. $_->{'Version'}))
  179. {
  180. warning(_g('package %s (filename %s) is repeat but newer version;'),
  181. $p, $fn);
  182. warning(_g('used that one and ignored data from %s!'),
  183. $_->{Filename});
  184. $packages{$p} = [];
  185. } else {
  186. warning(_g('package %s (filename %s) is repeat;'), $p, $fn);
  187. warning(_g('ignored that one and using data from %s!'),
  188. $_->{Filename});
  189. next FILE;
  190. }
  191. }
  192. }
  193. warning(_g('package %s (filename %s) has Filename field!'), $p, $fn)
  194. if defined($fields->{'Filename'});
  195. $fields->{'Filename'} = "$pathprefix$fn";
  196. my $sums = Dpkg::Checksums->new();
  197. $sums->add_from_file($fn);
  198. foreach my $alg (checksums_get_list()) {
  199. if ($alg eq 'md5') {
  200. $fields->{'MD5sum'} = $sums->get_checksum($fn, $alg);
  201. } else {
  202. $fields->{$alg} = $sums->get_checksum($fn, $alg);
  203. }
  204. }
  205. $fields->{'Size'} = $sums->get_size($fn);
  206. $fields->{'X-Medium'} = $options{medium} if defined $options{medium};
  207. push @{$packages{$p}}, $fields;
  208. }
  209. close($find_h);
  210. load_override($override) if defined $override;
  211. load_override_extra($options{'extra-override'}) if defined $options{'extra-override'};
  212. my @missingover=();
  213. my $records_written = 0;
  214. for my $p (sort keys %packages) {
  215. if (defined($override) and not defined($overridden{$p})) {
  216. push(@missingover,$p);
  217. }
  218. for my $package (@{$packages{$p}}) {
  219. print(STDOUT "$package\n") or syserr(_g('failed when writing stdout'));
  220. $records_written++;
  221. }
  222. }
  223. close(STDOUT) or syserr(_g("couldn't close stdout"));
  224. if (@changedmaint) {
  225. warning(_g('Packages in override file with incorrect old maintainer value:'));
  226. warning($_) foreach (@changedmaint);
  227. }
  228. if (@samemaint) {
  229. warning(_g('Packages specifying same maintainer as override file:'));
  230. warning($_) foreach (@samemaint);
  231. }
  232. if (@missingover) {
  233. warning(_g('Packages in archive but missing from override file:'));
  234. warning(' %s', join(' ', @missingover));
  235. }
  236. if (@spuriousover) {
  237. warning(_g('Packages in override file but not in archive:'));
  238. warning(' %s', join(' ', @spuriousover));
  239. }
  240. info(_g('Wrote %s entries to output Packages file.'), $records_written);