dpkg-scanpackages.pl 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #!/usr/bin/perl
  2. #
  3. # dpkg-scanpackages
  4. #
  5. # Copyright © 2006-2015 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 Getopt::Long qw(:config posix_default bundling no_ignorecase);
  22. use File::Find;
  23. use Dpkg ();
  24. use Dpkg::Gettext;
  25. use Dpkg::ErrorHandling;
  26. use Dpkg::Util qw(:list);
  27. use Dpkg::Control;
  28. use Dpkg::Version;
  29. use Dpkg::Checksums;
  30. use Dpkg::Compression::FileHandle;
  31. textdomain('dpkg-dev');
  32. # Do not pollute STDOUT with info messages
  33. report_options(info_fh => \*STDERR);
  34. my (@samemaint, @changedmaint);
  35. my @spuriousover;
  36. my %packages;
  37. my %overridden;
  38. my %hash;
  39. my %options = (help => sub { usage(); exit 0; },
  40. version => sub { version(); exit 0; },
  41. type => undef,
  42. arch => undef,
  43. hash => 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. 'hash|h=s',
  54. 'multiversion|m!',
  55. 'extra-override|e=s',
  56. 'medium|M=s',
  57. );
  58. sub version {
  59. printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  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. -h, --hash <hash-list> only generate hashes for the specified list.
  68. -m, --multiversion allow multiple versions of a single package.
  69. -e, --extra-override <file>
  70. use extra override file.
  71. -M, --medium <medium> add X-Medium field for dselect multicd access method
  72. -?, --help show this help message.
  73. --version show the version.
  74. "), $Dpkg::PROGNAME;
  75. }
  76. sub load_override
  77. {
  78. my $override = shift;
  79. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $override);
  80. while (<$comp_file>) {
  81. s/\#.*//;
  82. s/\s+$//;
  83. next unless $_;
  84. my ($p, $priority, $section, $maintainer) = split(/\s+/, $_, 4);
  85. if (not defined($packages{$p})) {
  86. push(@spuriousover, $p);
  87. next;
  88. }
  89. for my $package (@{$packages{$p}}) {
  90. if ($maintainer) {
  91. if ($maintainer =~ m/(.+?)\s*=\>\s*(.+)/) {
  92. my $oldmaint = $1;
  93. my $newmaint = $2;
  94. my $debmaint = $$package{Maintainer};
  95. if (none { $debmaint eq $_ } split m{\s*//\s*}, $oldmaint) {
  96. push(@changedmaint,
  97. sprintf(g_(' %s (package says %s, not %s)'),
  98. $p, $$package{Maintainer}, $oldmaint));
  99. } else {
  100. $$package{Maintainer} = $newmaint;
  101. }
  102. } elsif ($$package{Maintainer} eq $maintainer) {
  103. push(@samemaint, " $p ($maintainer)");
  104. } else {
  105. warning(g_('unconditional maintainer override for %s'), $p);
  106. $$package{Maintainer} = $maintainer;
  107. }
  108. }
  109. $$package{Priority} = $priority;
  110. $$package{Section} = $section;
  111. }
  112. $overridden{$p} = 1;
  113. }
  114. close($comp_file);
  115. }
  116. sub load_override_extra
  117. {
  118. my $extra_override = shift;
  119. my $comp_file = Dpkg::Compression::FileHandle->new(filename => $extra_override);
  120. while (<$comp_file>) {
  121. s/\#.*//;
  122. s/\s+$//;
  123. next unless $_;
  124. my ($p, $field, $value) = split(/\s+/, $_, 3);
  125. next unless defined($packages{$p});
  126. for my $package (@{$packages{$p}}) {
  127. $$package{$field} = $value;
  128. }
  129. }
  130. close($comp_file);
  131. }
  132. sub process_deb {
  133. my ($pathprefix, $fn) = @_;
  134. my $fields = Dpkg::Control->new(type => CTRL_INDEX_PKG);
  135. open my $output_fh, '-|', 'dpkg-deb', '-I', $fn, 'control'
  136. or syserr(g_('cannot fork for %s'), 'dpkg-deb');
  137. $fields->parse($output_fh, $fn)
  138. or error(g_("couldn't parse control information from %s"), $fn);
  139. close $output_fh;
  140. if ($?) {
  141. warning(g_("'dpkg-deb -I %s control' exited with %d, skipping package"),
  142. $fn, $?);
  143. return;
  144. }
  145. my $p = $fields->{'Package'};
  146. error(g_('no Package field in control file of %s'), $fn)
  147. if not defined $p;
  148. if (defined($packages{$p}) and not $options{multiversion}) {
  149. foreach my $pkg (@{$packages{$p}}) {
  150. if (version_compare_relation($fields->{'Version'}, REL_GT,
  151. $pkg->{'Version'}))
  152. {
  153. warning(g_('package %s (filename %s) is repeat but newer ' .
  154. 'version; used that one and ignored data from %s!'),
  155. $p, $fn, $pkg->{Filename});
  156. $packages{$p} = [];
  157. } else {
  158. warning(g_('package %s (filename %s) is repeat; ' .
  159. 'ignored that one and using data from %s!'),
  160. $p, $fn, $pkg->{Filename});
  161. return;
  162. }
  163. }
  164. }
  165. warning(g_('package %s (filename %s) has Filename field!'), $p, $fn)
  166. if defined($fields->{'Filename'});
  167. $fields->{'Filename'} = "$pathprefix$fn";
  168. my $sums = Dpkg::Checksums->new();
  169. $sums->add_from_file($fn);
  170. foreach my $alg (checksums_get_list()) {
  171. next if %hash and not $hash{$alg};
  172. if ($alg eq 'md5') {
  173. $fields->{'MD5sum'} = $sums->get_checksum($fn, $alg);
  174. } else {
  175. $fields->{$alg} = $sums->get_checksum($fn, $alg);
  176. }
  177. }
  178. $fields->{'Size'} = $sums->get_size($fn);
  179. $fields->{'X-Medium'} = $options{medium} if defined $options{medium};
  180. push @{$packages{$p}}, $fields;
  181. }
  182. {
  183. local $SIG{__WARN__} = sub { usageerr($_[0]) };
  184. GetOptions(\%options, @options_spec);
  185. }
  186. if (not (@ARGV >= 1 and @ARGV <= 3)) {
  187. usageerr(g_('one to three arguments expected'));
  188. }
  189. my $type = $options{type} // 'deb';
  190. my $arch = $options{arch};
  191. %hash = map { $_ => 1 } split /,/, $options{hash} // '';
  192. foreach my $alg (keys %hash) {
  193. if (not checksums_is_supported($alg)) {
  194. usageerr(g_('unsupported checksum \'%s\''), $alg);
  195. }
  196. }
  197. my ($binarypath, $override, $pathprefix) = @ARGV;
  198. if (not -e $binarypath) {
  199. error(g_('binary path %s not found'), $binarypath);
  200. }
  201. if (defined $override and not -e $override) {
  202. error(g_('override file %s not found'), $override);
  203. }
  204. $pathprefix //= '';
  205. my $find_filter;
  206. if ($options{arch}) {
  207. $find_filter = qr/_(?:all|${arch})\.$type$/;
  208. } else {
  209. $find_filter = qr/\.$type$/;
  210. }
  211. my @archives;
  212. my $scan_archives = sub {
  213. push @archives, $File::Find::name if m/$find_filter/;
  214. };
  215. find({ follow => 1, follow_skip => 2, wanted => $scan_archives}, $binarypath);
  216. foreach my $fn (@archives) {
  217. process_deb($pathprefix, $fn);
  218. }
  219. load_override($override) if defined $override;
  220. load_override_extra($options{'extra-override'}) if defined $options{'extra-override'};
  221. my @missingover=();
  222. my $records_written = 0;
  223. for my $p (sort keys %packages) {
  224. if (defined($override) and not defined($overridden{$p})) {
  225. push @missingover, $p;
  226. }
  227. for my $package (sort { $a->{Version} cmp $b->{Version} } @{$packages{$p}}) {
  228. print("$package\n") or syserr(g_('failed when writing stdout'));
  229. $records_written++;
  230. }
  231. }
  232. close(STDOUT) or syserr(g_("couldn't close stdout"));
  233. if (@changedmaint) {
  234. warning(g_('Packages in override file with incorrect old maintainer value:'));
  235. warning($_) foreach (@changedmaint);
  236. }
  237. if (@samemaint) {
  238. warning(g_('Packages specifying same maintainer as override file:'));
  239. warning($_) foreach (@samemaint);
  240. }
  241. if (@missingover) {
  242. warning(g_('Packages in archive but missing from override file:'));
  243. warning(' %s', join(' ', @missingover));
  244. }
  245. if (@spuriousover) {
  246. warning(g_('Packages in override file but not in archive:'));
  247. warning(' %s', join(' ', @spuriousover));
  248. }
  249. info(g_('Wrote %s entries to output Packages file.'), $records_written);