dpkg-scanpackages.pl 7.7 KB

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