dpkg-scanpackages.pl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. my $result = GetOptions(\%options, @options_spec);
  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 (!grep($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. usage() and exit 1 if not $result;
  132. if (not @ARGV >= 1 && @ARGV <= 3) {
  133. usageerr(_g('one to three arguments expected'));
  134. }
  135. my $type = defined($options{type}) ? $options{type} : 'deb';
  136. my $arch = $options{arch};
  137. my @find_args;
  138. if ($options{arch}) {
  139. @find_args = ('(', '-name', "*_all.$type", '-o',
  140. '-name', "*_${arch}.$type", ')');
  141. }
  142. else {
  143. @find_args = ('-name', "*.$type");
  144. }
  145. my ($binarydir, $override, $pathprefix) = @ARGV;
  146. -d $binarydir or error(_g('Binary dir %s not found'), $binarydir);
  147. defined($override) and (-e $override or
  148. error(_g('Override file %s not found'), $override));
  149. $pathprefix //= '';
  150. my $find_h = IO::Handle->new();
  151. open($find_h, '-|', 'find', '-L', "$binarydir/", @find_args, '-print')
  152. or syserr(_g("Couldn't open %s for reading"), $binarydir);
  153. FILE:
  154. while (<$find_h>) {
  155. chomp;
  156. my $fn = $_;
  157. my $output;
  158. my $pid = spawn(exec => [ 'dpkg-deb', '-I', $fn, 'control' ],
  159. to_pipe => \$output);
  160. my $fields = Dpkg::Control->new(type => CTRL_INDEX_PKG);
  161. $fields->parse($output, $fn)
  162. or error(_g("couldn't parse control information from %s"), $fn);
  163. wait_child($pid, no_check => 1);
  164. if ($?) {
  165. warning(_g("\`dpkg-deb -I %s control' exited with %d, skipping package"),
  166. $fn, $?);
  167. next;
  168. }
  169. defined($fields->{'Package'})
  170. or error(_g('No Package field in control file of %s'), $fn);
  171. my $p = $fields->{'Package'};
  172. if (defined($packages{$p}) and not $options{multiversion}) {
  173. foreach (@{$packages{$p}}) {
  174. if (version_compare_relation($fields->{'Version'}, REL_GT,
  175. $_->{'Version'}))
  176. {
  177. warning(_g('Package %s (filename %s) is repeat but newer version;'),
  178. $p, $fn);
  179. warning(_g('used that one and ignored data from %s!'),
  180. $_->{Filename});
  181. $packages{$p} = [];
  182. } else {
  183. warning(_g('Package %s (filename %s) is repeat;'), $p, $fn);
  184. warning(_g('ignored that one and using data from %s!'),
  185. $_->{Filename});
  186. next FILE;
  187. }
  188. }
  189. }
  190. warning(_g('Package %s (filename %s) has Filename field!'), $p, $fn)
  191. if defined($fields->{'Filename'});
  192. $fields->{'Filename'} = "$pathprefix$fn";
  193. my $sums = Dpkg::Checksums->new();
  194. $sums->add_from_file($fn);
  195. foreach my $alg (checksums_get_list()) {
  196. if ($alg eq 'md5') {
  197. $fields->{'MD5sum'} = $sums->get_checksum($fn, $alg);
  198. } else {
  199. $fields->{$alg} = $sums->get_checksum($fn, $alg);
  200. }
  201. }
  202. $fields->{'Size'} = $sums->get_size($fn);
  203. $fields->{'X-Medium'} = $options{medium} if defined $options{medium};
  204. push @{$packages{$p}}, $fields;
  205. }
  206. close($find_h);
  207. load_override($override) if defined $override;
  208. load_override_extra($options{'extra-override'}) if defined $options{'extra-override'};
  209. my @missingover=();
  210. my $records_written = 0;
  211. for my $p (sort keys %packages) {
  212. if (defined($override) and not defined($overridden{$p})) {
  213. push(@missingover,$p);
  214. }
  215. for my $package (@{$packages{$p}}) {
  216. print(STDOUT "$package\n") or syserr(_g('Failed when writing stdout'));
  217. $records_written++;
  218. }
  219. }
  220. close(STDOUT) or syserr(_g("Couldn't close stdout"));
  221. if (@changedmaint) {
  222. warning(_g('Packages in override file with incorrect old maintainer value:'));
  223. warning($_) foreach (@changedmaint);
  224. }
  225. if (@samemaint) {
  226. warning(_g('Packages specifying same maintainer as override file:'));
  227. warning($_) foreach (@samemaint);
  228. }
  229. if (@missingover) {
  230. warning(_g('Packages in archive but missing from override file:'));
  231. warning(' %s', join(' ', @missingover));
  232. }
  233. if (@spuriousover) {
  234. warning(_g('Packages in override file but not in archive:'));
  235. warning(' %s', join(' ', @spuriousover));
  236. }
  237. info(_g('Wrote %s entries to output Packages file.'), $records_written);