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 Dpkg ();
  23. use Dpkg::Gettext;
  24. use Dpkg::ErrorHandling;
  25. use Dpkg::Util qw(:list);
  26. use Dpkg::Control;
  27. use Dpkg::Version;
  28. use Dpkg::Checksums;
  29. use Dpkg::Compression::FileHandle;
  30. textdomain('dpkg-dev');
  31. # Do not pollute STDOUT with info messages
  32. report_options(info_fh => \*STDERR);
  33. my (@samemaint, @changedmaint);
  34. my @spuriousover;
  35. my %packages;
  36. my %overridden;
  37. my %hash;
  38. my %options = (help => sub { usage(); exit 0; },
  39. version => sub { version(); exit 0; },
  40. type => undef,
  41. arch => undef,
  42. hash => 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. 'hash|h=s',
  53. 'multiversion|m!',
  54. 'extra-override|e=s',
  55. 'medium|M=s',
  56. );
  57. sub version {
  58. printf g_("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
  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. -h, --hash <hash-list> only generate hashes for the specified list.
  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 (none { $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. sub process_deb {
  132. my ($pathprefix, $fn) = @_;
  133. my $fields = Dpkg::Control->new(type => CTRL_INDEX_PKG);
  134. open my $output_fh, '-|', 'dpkg-deb', '-I', $fn, 'control'
  135. or syserr(g_('cannot fork for %s'), 'dpkg-deb');
  136. $fields->parse($output_fh, $fn)
  137. or error(g_("couldn't parse control information from %s"), $fn);
  138. close $output_fh;
  139. if ($?) {
  140. warning(g_("'dpkg-deb -I %s control' exited with %d, skipping package"),
  141. $fn, $?);
  142. return;
  143. }
  144. my $p = $fields->{'Package'};
  145. error(g_('no Package field in control file of %s'), $fn)
  146. if not defined $p;
  147. if (defined($packages{$p}) and not $options{multiversion}) {
  148. foreach my $pkg (@{$packages{$p}}) {
  149. if (version_compare_relation($fields->{'Version'}, REL_GT,
  150. $pkg->{'Version'}))
  151. {
  152. warning(g_('package %s (filename %s) is repeat but newer ' .
  153. 'version; used that one and ignored data from %s!'),
  154. $p, $fn, $pkg->{Filename});
  155. $packages{$p} = [];
  156. } else {
  157. warning(g_('package %s (filename %s) is repeat; ' .
  158. 'ignored that one and using data from %s!'),
  159. $p, $fn, $pkg->{Filename});
  160. return;
  161. }
  162. }
  163. }
  164. warning(g_('package %s (filename %s) has Filename field!'), $p, $fn)
  165. if defined($fields->{'Filename'});
  166. $fields->{'Filename'} = "$pathprefix$fn";
  167. my $sums = Dpkg::Checksums->new();
  168. $sums->add_from_file($fn);
  169. foreach my $alg (checksums_get_list()) {
  170. next if %hash and not $hash{$alg};
  171. if ($alg eq 'md5') {
  172. $fields->{'MD5sum'} = $sums->get_checksum($fn, $alg);
  173. } else {
  174. $fields->{$alg} = $sums->get_checksum($fn, $alg);
  175. }
  176. }
  177. $fields->{'Size'} = $sums->get_size($fn);
  178. $fields->{'X-Medium'} = $options{medium} if defined $options{medium};
  179. push @{$packages{$p}}, $fields;
  180. }
  181. {
  182. local $SIG{__WARN__} = sub { usageerr($_[0]) };
  183. GetOptions(\%options, @options_spec);
  184. }
  185. if (not (@ARGV >= 1 and @ARGV <= 3)) {
  186. usageerr(g_('one to three arguments expected'));
  187. }
  188. my $type = $options{type} // 'deb';
  189. my $arch = $options{arch};
  190. %hash = map { $_ => 1 } split /,/, $options{hash} // '';
  191. foreach my $alg (keys %hash) {
  192. if (not checksums_is_supported($alg)) {
  193. usageerr(g_('unsupported checksum \'%s\''), $alg);
  194. }
  195. }
  196. my @find_args;
  197. if ($options{arch}) {
  198. @find_args = ('(', '-name', "*_all.$type", '-o',
  199. '-name', "*_${arch}.$type", ')');
  200. }
  201. else {
  202. @find_args = ('-name', "*.$type");
  203. }
  204. my ($binarydir, $override, $pathprefix) = @ARGV;
  205. if (not -d $binarydir) {
  206. error(g_('binary directory %s not found'), $binarydir);
  207. }
  208. if (defined $override and not -e $override) {
  209. error(g_('override file %s not found'), $override);
  210. }
  211. $pathprefix //= '';
  212. open my $find_h, '-|', 'find', '-L', "$binarydir/", @find_args, '-print'
  213. or syserr(g_("couldn't open %s for reading"), $binarydir);
  214. while (my $fn = <$find_h>) {
  215. chomp $fn;
  216. process_deb($pathprefix, $fn);
  217. }
  218. close($find_h);
  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);