dpkg-scanpackages.pl 8.1 KB

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