dpkg-scanpackages.pl 7.6 KB

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