dpkg-shlibdeps.pl 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use warnings;
  4. use English;
  5. use POSIX qw(:errno_h :signal_h);
  6. use Cwd qw(realpath);
  7. use Dpkg;
  8. use Dpkg::Gettext;
  9. use Dpkg::ErrorHandling qw(warning error failure syserr usageerr);
  10. use Dpkg::Path qw(relative_to_pkg_root guess_pkg_root_dir
  11. check_files_are_the_same);
  12. use Dpkg::Version qw(compare_versions);
  13. use Dpkg::Shlibs qw(find_library);
  14. use Dpkg::Shlibs::Objdump;
  15. use Dpkg::Shlibs::SymbolFile;
  16. use Dpkg::Arch qw(get_host_arch);
  17. use Dpkg::Fields qw(capit);
  18. use Dpkg::Deps;
  19. push(@INC,$dpkglibdir);
  20. require 'controllib.pl';
  21. # By increasing importance
  22. my @depfields = qw(Suggests Recommends Depends Pre-Depends);
  23. my $i = 0; my %depstrength = map { $_ => $i++ } @depfields;
  24. textdomain("dpkg-dev");
  25. my $shlibsoverride = '/etc/dpkg/shlibs.override';
  26. my $shlibsdefault = '/etc/dpkg/shlibs.default';
  27. my $shlibslocal = 'debian/shlibs.local';
  28. my $packagetype = 'deb';
  29. my $dependencyfield = 'Depends';
  30. my $varlistfile = 'debian/substvars';
  31. my $varnameprefix = 'shlibs';
  32. my $ignore_missing_info = 0;
  33. my $debug = 0;
  34. my @exclude = ();
  35. my $host_arch = get_host_arch();
  36. my (@pkg_shlibs, @pkg_symbols);
  37. if (-d "debian") {
  38. push @pkg_symbols, <debian/*/DEBIAN/symbols>;
  39. push @pkg_shlibs, <debian/*/DEBIAN/shlibs>;
  40. }
  41. my ($stdout, %exec);
  42. foreach (@ARGV) {
  43. if (m/^-T(.*)$/) {
  44. $varlistfile = $1;
  45. } elsif (m/^-p(\w[-:0-9A-Za-z]*)$/) {
  46. $varnameprefix = $1;
  47. } elsif (m/^-L(.*)$/) {
  48. $shlibslocal = $1;
  49. } elsif (m/^-O$/) {
  50. $stdout = 1;
  51. } elsif (m/^-(h|-help)$/) {
  52. usage(); exit(0);
  53. } elsif (m/^--version$/) {
  54. version(); exit(0);
  55. } elsif (m/^--admindir=(.*)$/) {
  56. $admindir = $1;
  57. -d $admindir ||
  58. error(_g("administrative directory '%s' does not exist"), $admindir);
  59. } elsif (m/^-d(.*)$/) {
  60. $dependencyfield = capit($1);
  61. defined($depstrength{$dependencyfield}) ||
  62. warning(_g("unrecognised dependency field \`%s'"), $dependencyfield);
  63. } elsif (m/^-e(.*)$/) {
  64. $exec{$1} = $dependencyfield;
  65. } elsif (m/^--ignore-missing-info$/) {
  66. $ignore_missing_info = 1;
  67. } elsif (m/^-t(.*)$/) {
  68. $packagetype = $1;
  69. } elsif (m/^-v$/) {
  70. $debug = 1;
  71. } elsif (m/^-x(.*)$/) {
  72. push @exclude, $1;
  73. } elsif (m/^-/) {
  74. usageerr(_g("unknown option \`%s'"), $_);
  75. } else {
  76. $exec{$_} = $dependencyfield;
  77. }
  78. }
  79. scalar keys %exec || usageerr(_g("need at least one executable"));
  80. our %fi;
  81. parsecontrolfile("debian/control");
  82. my $build_depends = defined($fi{"C Build-Depends"}) ?
  83. $fi{"C Build-Depends"} : "";
  84. my $build_deps = Dpkg::Deps::parse($build_depends, reduce_arch => 1);
  85. my %dependencies;
  86. my %shlibs;
  87. my $cur_field;
  88. foreach my $file (keys %exec) {
  89. $cur_field = $exec{$file};
  90. print "Scanning $file (for $cur_field field)\n" if $debug;
  91. my $obj = Dpkg::Shlibs::Objdump::Object->new($file);
  92. my @sonames = $obj->get_needed_libraries;
  93. # Load symbols files for all needed libraries (identified by SONAME)
  94. my %libfiles;
  95. my %altlibfiles;
  96. my %soname_notfound;
  97. foreach my $soname (@sonames) {
  98. my $lib = my_find_library($soname, $obj->{RPATH}, $obj->{format}, $file);
  99. unless (defined $lib) {
  100. $soname_notfound{$soname} = 1;
  101. my $msg = _g("couldn't find library %s needed by %s (its RPATH is '%s').\n" .
  102. "Note: libraries are not searched in other binary packages " .
  103. "that do not have any shlibs file.\nTo help dpkg-shlibdeps " .
  104. "find private libraries, you might need to set LD_LIBRARY_PATH.");
  105. if (scalar(split_soname($soname))) {
  106. failure($msg, $soname, $file, join(":", @{$obj->{RPATH}}));
  107. } else {
  108. warning($msg, $soname, $file, join(":", @{$obj->{RPATH}}));
  109. }
  110. next;
  111. }
  112. $libfiles{$lib} = $soname;
  113. my $reallib = realpath($lib);
  114. if ($reallib ne $lib) {
  115. $altlibfiles{$reallib} = $soname;
  116. }
  117. print "Library $soname found in $lib\n" if $debug;
  118. }
  119. my $file2pkg = find_packages(keys %libfiles, keys %altlibfiles);
  120. my $symfile = Dpkg::Shlibs::SymbolFile->new();
  121. my $dumplibs_wo_symfile = Dpkg::Shlibs::Objdump->new();
  122. my @soname_wo_symfile;
  123. foreach my $lib (keys %libfiles) {
  124. my $soname = $libfiles{$lib};
  125. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  126. # The path of the library as calculated is not the
  127. # official path of a packaged file, try to fallback on
  128. # on the realpath() first, maybe this one is part of a package
  129. my $reallib = realpath($lib);
  130. if (exists $file2pkg->{$reallib}) {
  131. $file2pkg->{$lib} = $file2pkg->{$reallib};
  132. }
  133. }
  134. if (not scalar(grep { $_ ne '' } @{$file2pkg->{$lib}})) {
  135. # If the library is really not available in an installed package,
  136. # it's because it's in the process of being built
  137. # Empty package name will lead to consideration of symbols
  138. # file from the package being built only
  139. $file2pkg->{$lib} = [""];
  140. print "No associated package found for $lib\n" if $debug;
  141. }
  142. # Load symbols/shlibs files from packages providing libraries
  143. foreach my $pkg (@{$file2pkg->{$lib}}) {
  144. my $dpkg_symfile;
  145. if ($packagetype eq "deb") {
  146. # Use fine-grained dependencies only on real deb
  147. $dpkg_symfile = find_symbols_file($pkg, $soname);
  148. if (defined $dpkg_symfile) {
  149. # Load symbol information
  150. print "Using symbols file $dpkg_symfile for $soname\n" if $debug;
  151. $symfile->load($dpkg_symfile);
  152. }
  153. }
  154. if (defined($dpkg_symfile) && $symfile->has_object($soname)) {
  155. # Initialize dependencies as an unversioned dependency
  156. my $dep = $symfile->get_dependency($soname);
  157. foreach my $subdep (split /\s*,\s*/, $dep) {
  158. if (not exists $dependencies{$cur_field}{$subdep}) {
  159. $dependencies{$cur_field}{$subdep} = '';
  160. }
  161. }
  162. } else {
  163. # No symbol file found, fall back to standard shlibs
  164. my $id = $dumplibs_wo_symfile->parse($lib);
  165. push @soname_wo_symfile, $soname;
  166. my $libobj = $dumplibs_wo_symfile->get_object($id);
  167. # Only try to generate a dependency for libraries with a SONAME
  168. if ($libobj->is_public_library() and not add_shlibs_dep($soname, $pkg)) {
  169. # This failure is fairly new, try to be kind by
  170. # ignoring as many cases that can be safely ignored
  171. my $ignore = 0;
  172. # 1/ when the lib and the binary are in the same
  173. # package
  174. my $root_file = guess_pkg_root_dir($file);
  175. my $root_lib = guess_pkg_root_dir($lib);
  176. $ignore++ if defined $root_file and defined $root_lib
  177. and check_files_are_the_same($root_file, $root_lib);
  178. # 2/ when the lib is not versioned and can't be
  179. # handled by shlibs
  180. $ignore++ unless scalar(split_soname($soname));
  181. # 3/ when we have been asked to do so
  182. $ignore++ if $ignore_missing_info;
  183. failure(_g("no dependency information found for %s " .
  184. "(used by %s)."), $lib, $file)
  185. unless $ignore;
  186. }
  187. }
  188. }
  189. }
  190. # Scan all undefined symbols of the binary and resolve to a
  191. # dependency
  192. my %used_sonames = map { $_ => 0 } @sonames;
  193. my $nb_warnings = 0;
  194. my $nb_skipped_warnings = 0;
  195. # Disable warnings about missing symbols when we have not been able to
  196. # find all libs
  197. my $disable_warnings = scalar(keys(%soname_notfound));
  198. foreach my $sym ($obj->get_undefined_dynamic_symbols()) {
  199. my $name = $sym->{name};
  200. if ($sym->{version}) {
  201. $name .= "\@$sym->{version}";
  202. } else {
  203. $name .= "\@Base";
  204. }
  205. my $symdep = $symfile->lookup_symbol($name, \@sonames);
  206. if (defined($symdep)) {
  207. $used_sonames{$symdep->{soname}}++;
  208. update_dependency_version($symdep->{depends},
  209. $symdep->{minver});
  210. } else {
  211. my $syminfo = $dumplibs_wo_symfile->locate_symbol($name);
  212. if (not defined($syminfo)) {
  213. next if $disable_warnings;
  214. # Complain about missing symbols only for executables
  215. # and public libraries
  216. if ($obj->is_executable() or $obj->is_public_library()) {
  217. my $print_name = $name;
  218. # Drop the default suffix for readability
  219. $print_name =~ s/\@Base$//;
  220. unless ($sym->{weak}) {
  221. if ($debug or $nb_warnings < 10) {
  222. warning(_g("symbol %s used by %s found in none of the " .
  223. "libraries."), $print_name, $file);
  224. $nb_warnings++;
  225. } else {
  226. $nb_skipped_warnings++;
  227. }
  228. }
  229. }
  230. } else {
  231. $used_sonames{$syminfo->{soname}}++;
  232. }
  233. }
  234. }
  235. warning(_g("%d other similar warnings have been skipped (use -v to see " .
  236. "them all)."), $nb_skipped_warnings) if $nb_skipped_warnings;
  237. foreach my $soname (@sonames) {
  238. # Adjust minimal version of dependencies with information
  239. # extracted from build-dependencies
  240. my $dev_pkg = $symfile->get_field($soname, 'Build-Depends-Package');
  241. if (defined $dev_pkg) {
  242. my $minver = get_min_version_from_deps($build_deps, $dev_pkg);
  243. if (defined $minver) {
  244. foreach my $dep ($symfile->get_dependencies($soname)) {
  245. update_dependency_version($dep, $minver);
  246. }
  247. }
  248. }
  249. # Warn about un-NEEDED libraries
  250. unless ($soname_notfound{$soname} or $used_sonames{$soname}) {
  251. # Ignore warning for libm.so.6 if also linked against libstdc++
  252. next if ($soname =~ /^libm\.so\.\d+$/ and
  253. scalar grep(/^libstdc\+\+\.so\.\d+/, @sonames));
  254. warning(_g("%s shouldn't be linked with %s (it uses none of its " .
  255. "symbols)."), $file, $soname);
  256. }
  257. }
  258. }
  259. # Open substvars file
  260. my $fh;
  261. if ($stdout) {
  262. $fh = \*STDOUT;
  263. } else {
  264. open(NEW, ">", "$varlistfile.new") ||
  265. syserr(_g("open new substvars file \`%s'"), "$varlistfile.new");
  266. if (-e $varlistfile) {
  267. open(OLD, "<", $varlistfile) ||
  268. syserr(_g("open old varlist file \`%s' for reading"), $varlistfile);
  269. foreach my $entry (grep { not m/^\Q$varnameprefix\E:/ } (<OLD>)) {
  270. print(NEW $entry) ||
  271. syserr(_g("copy old entry to new varlist file \`%s'"),
  272. "$varlistfile.new");
  273. }
  274. close(OLD);
  275. }
  276. $fh = \*NEW;
  277. }
  278. # Write out the shlibs substvars
  279. my %depseen;
  280. sub filter_deps {
  281. my ($dep, $field) = @_;
  282. # Skip dependencies on excluded packages
  283. foreach my $exc (@exclude) {
  284. return 0 if $dep =~ /^\s*\Q$exc\E\b/;
  285. }
  286. # Don't include dependencies if they are already
  287. # mentionned in a higher priority field
  288. if (not defined($depseen{$dep})) {
  289. $depseen{$dep} = $dependencies{$field}{$dep};
  290. return 1;
  291. } else {
  292. # Since dependencies can be versionned, we have to
  293. # verify if the dependency is stronger than the
  294. # previously seen one
  295. if (compare_versions($depseen{$dep}, '>>', $dependencies{$field}{$dep})) {
  296. return 0;
  297. } else {
  298. $depseen{$dep} = $dependencies{$field}{$dep};
  299. return 1;
  300. }
  301. }
  302. }
  303. foreach my $field (reverse @depfields) {
  304. my $dep = "";
  305. if (exists $dependencies{$field} and scalar keys %{$dependencies{$field}}) {
  306. $dep = join ", ",
  307. map {
  308. # Translate dependency templates into real dependencies
  309. if ($dependencies{$field}{$_}) {
  310. s/#MINVER#/(>= $dependencies{$field}{$_})/g;
  311. } else {
  312. s/#MINVER#//g;
  313. }
  314. s/\s+/ /g;
  315. $_;
  316. } grep { filter_deps($_, $field) }
  317. keys %{$dependencies{$field}};
  318. }
  319. if ($dep) {
  320. print $fh "$varnameprefix:$field=$dep\n";
  321. }
  322. }
  323. # Replace old file by new one
  324. if (!$stdout) {
  325. close($fh);
  326. rename("$varlistfile.new",$varlistfile) ||
  327. syserr(_g("install new varlist file \`%s'"), $varlistfile);
  328. }
  329. ##
  330. ## Functions
  331. ##
  332. sub version {
  333. printf _g("Debian %s version %s.\n"), $progname, $version;
  334. printf _g("
  335. Copyright (C) 1996 Ian Jackson.
  336. Copyright (C) 2000 Wichert Akkerman.
  337. Copyright (C) 2006 Frank Lichtenheld.
  338. Copyright (C) 2007 Raphael Hertzog.
  339. ");
  340. printf _g("
  341. This is free software; see the GNU General Public Licence version 2 or
  342. later for copying conditions. There is NO warranty.
  343. ");
  344. }
  345. sub usage {
  346. printf _g(
  347. "Usage: %s [<option> ...] <executable>|-e<executable> [<option> ...]
  348. Positional options (order is significant):
  349. <executable> include dependencies for <executable>,
  350. -e<executable> (use -e if <executable> starts with \`-')
  351. -d<dependencyfield> next executable(s) set shlibs:<dependencyfield>.
  352. Options:
  353. -p<varnameprefix> set <varnameprefix>:* instead of shlibs:*.
  354. -O print variable settings to stdout.
  355. -L<localshlibsfile> shlibs override file, not debian/shlibs.local.
  356. -T<varlistfile> update variables here, not debian/substvars.
  357. -t<type> set package type (default is deb).
  358. -x<package> exclude package from the generated dependencies.
  359. --admindir=<directory> change the administrative directory.
  360. -h, --help show this help message.
  361. --version show the version.
  362. Dependency fields recognised are:
  363. %s
  364. "), $progname, join("/",@depfields);
  365. }
  366. sub get_min_version_from_deps {
  367. my ($dep, $pkg) = @_;
  368. if ($dep->isa('Dpkg::Deps::Simple')) {
  369. if (($dep->{package} eq $pkg) &&
  370. defined($dep->{relation}) &&
  371. (($dep->{relation} eq ">=") ||
  372. ($dep->{relation} eq ">>")))
  373. {
  374. return $dep->{version};
  375. }
  376. return undef;
  377. } else {
  378. my $res;
  379. foreach my $subdep ($dep->get_deps()) {
  380. my $minver = get_min_version_from_deps($subdep, $pkg);
  381. next if not defined $minver;
  382. if (defined $res) {
  383. if (compare_versions($minver, '>>', $res)) {
  384. $res = $minver;
  385. }
  386. } else {
  387. $res = $minver;
  388. }
  389. }
  390. return $res;
  391. }
  392. }
  393. sub update_dependency_version {
  394. my ($dep, $minver) = @_;
  395. return if not defined($minver);
  396. foreach my $subdep (split /\s*,\s*/, $dep) {
  397. if (exists $dependencies{$cur_field}{$subdep} and
  398. defined($dependencies{$cur_field}{$subdep}))
  399. {
  400. if ($dependencies{$cur_field}{$subdep} eq '' or
  401. compare_versions($minver, '>>',
  402. $dependencies{$cur_field}{$subdep}))
  403. {
  404. $dependencies{$cur_field}{$subdep} = $minver;
  405. }
  406. } else {
  407. $dependencies{$cur_field}{$subdep} = $minver;
  408. }
  409. }
  410. }
  411. sub add_shlibs_dep {
  412. my ($soname, $pkg) = @_;
  413. print "Looking up shlibs dependency of $soname provided by '$pkg'\n" if $debug;
  414. foreach my $file ($shlibslocal, $shlibsoverride, @pkg_shlibs,
  415. "$admindir/info/$pkg.shlibs",
  416. $shlibsdefault)
  417. {
  418. next if not -e $file;
  419. my $dep = extract_from_shlibs($soname, $file);
  420. if (defined($dep)) {
  421. print "Found $dep in $file\n" if $debug;
  422. foreach (split(/,\s*/, $dep)) {
  423. $dependencies{$cur_field}{$_} = 1;
  424. }
  425. return 1;
  426. }
  427. }
  428. print "Found nothing\n" if $debug;
  429. return 0;
  430. }
  431. sub split_soname {
  432. my $soname = shift;
  433. if ($soname =~ /^(.*)\.so\.(.*)$/) {
  434. return wantarray ? ($1, $2) : 1;
  435. } elsif ($soname =~ /^(.*)-(\d.*)\.so$/) {
  436. return wantarray ? ($1, $2) : 1;
  437. } else {
  438. return wantarray ? () : 0;
  439. }
  440. }
  441. sub extract_from_shlibs {
  442. my ($soname, $shlibfile) = @_;
  443. # Split soname in name/version
  444. my ($libname, $libversion) = split_soname($soname);
  445. unless (defined $libname) {
  446. warning(_g("Can't extract name and version from library name \`%s'"),
  447. $soname);
  448. return;
  449. }
  450. # Open shlibs file
  451. $shlibfile = "./$shlibfile" if $shlibfile =~ m/^\s/;
  452. open(SHLIBS, "<", $shlibfile) ||
  453. syserr(_g("unable to open shared libs info file \`%s'"), $shlibfile);
  454. my $dep;
  455. while (<SHLIBS>) {
  456. s/\s*\n$//;
  457. next if m/^\#/;
  458. if (!m/^\s*(?:(\S+):\s+)?(\S+)\s+(\S+)(?:\s+(\S.*\S))?\s*$/) {
  459. warning(_g("shared libs info file \`%s' line %d: bad line \`%s'"),
  460. $shlibfile, $., $_);
  461. next;
  462. }
  463. my $depread = defined($4) ? $4 : '';
  464. if (($libname eq $2) && ($libversion eq $3)) {
  465. # Define dep and end here if the package type explicitely
  466. # matches. Otherwise if the packagetype is not specified, use
  467. # the dep only as a default that can be overriden by a later
  468. # line
  469. if (defined($1)) {
  470. if ($1 eq $packagetype) {
  471. $dep = $depread;
  472. last;
  473. }
  474. } else {
  475. $dep = $depread unless defined $dep;
  476. }
  477. }
  478. }
  479. close(SHLIBS);
  480. return $dep;
  481. }
  482. sub find_symbols_file {
  483. my ($pkg, $soname) = @_;
  484. foreach my $file (@pkg_symbols,
  485. "/etc/dpkg/symbols/$pkg.symbols.$host_arch",
  486. "/etc/dpkg/symbols/$pkg.symbols",
  487. "$admindir/info/$pkg.symbols")
  488. {
  489. if (-e $file and symfile_has_soname($file, $soname)) {
  490. return $file;
  491. }
  492. }
  493. return undef;
  494. }
  495. sub symfile_has_soname {
  496. my ($file, $soname) = @_;
  497. open(SYM_FILE, "<", $file) ||
  498. syserr(_g("cannot open file %s"), $file);
  499. my $result = 0;
  500. while (<SYM_FILE>) {
  501. if (/^\Q$soname\E /) {
  502. $result = 1;
  503. last;
  504. }
  505. }
  506. close(SYM_FILE);
  507. return $result;
  508. }
  509. # find_library ($soname, \@rpath, $format)
  510. sub my_find_library {
  511. my ($lib, $rpath, $format, $execfile) = @_;
  512. my $file;
  513. # Create real RPATH in case $ORIGIN is used
  514. # Note: ld.so also supports $PLATFORM and $LIB but they are
  515. # used in real case (yet)
  516. my $libdir = relative_to_pkg_root($execfile);
  517. my $origin;
  518. if (defined $libdir) {
  519. $origin = "/$libdir";
  520. $origin =~ s{/+[^/]*$}{};
  521. }
  522. my @RPATH = ();
  523. foreach my $path (@{$rpath}) {
  524. if ($path =~ /\$ORIGIN|\$\{ORIGIN\}/) {
  525. if (defined $origin) {
  526. $path =~ s/\$ORIGIN/$origin/g;
  527. $path =~ s/\$\{ORIGIN\}/$origin/g;
  528. } else {
  529. warning(_g("\$ORIGIN is used in RPATH of %s and the corresponding " .
  530. "directory could not be identified due to lack of DEBIAN " .
  531. "sub-directory in the root of package's build tree"), $execfile);
  532. }
  533. }
  534. push @RPATH, $path;
  535. }
  536. # Look into the packages we're currently building (but only those
  537. # that provides shlibs file and the one that contains the binary being
  538. # anlyzed...)
  539. # TODO: we should probably replace that by a cleaner way to look into
  540. # the various temporary build directories...
  541. my @copy = (@pkg_shlibs);
  542. my $pkg_root = guess_pkg_root_dir($execfile);
  543. unshift @copy, $pkg_root if defined $pkg_root;
  544. foreach my $builddir (map { s{/DEBIAN/shlibs$}{}; $_ } @copy) {
  545. $file = find_library($lib, \@RPATH, $format, $builddir);
  546. return $file if defined($file);
  547. }
  548. # Fallback in the root directory if we have not found what we were
  549. # looking for in the packages
  550. $file = find_library($lib, \@RPATH, $format, "");
  551. return $file if defined($file);
  552. return undef;
  553. }
  554. my %cached_pkgmatch = ();
  555. sub find_packages {
  556. my @files;
  557. my $pkgmatch = {};
  558. foreach (@_) {
  559. if (exists $cached_pkgmatch{$_}) {
  560. $pkgmatch->{$_} = $cached_pkgmatch{$_};
  561. } else {
  562. push @files, $_;
  563. $cached_pkgmatch{$_} = [""]; # placeholder to cache misses too.
  564. $pkgmatch->{$_} = [""]; # might be replaced later on
  565. }
  566. }
  567. return $pkgmatch unless scalar(@files);
  568. my $pid = open(DPKG, "-|");
  569. syserr(_g("cannot fork for dpkg --search")) unless defined($pid);
  570. if (!$pid) {
  571. # Child process running dpkg --search and discarding errors
  572. close STDERR;
  573. open STDERR, ">", "/dev/null";
  574. $ENV{LC_ALL} = "C";
  575. exec("dpkg", "--search", "--", @files)
  576. || syserr(_g("cannot exec dpkg"));
  577. }
  578. while(defined($_ = <DPKG>)) {
  579. chomp($_);
  580. if (m/^local diversion |^diversion by/) {
  581. warning(_g("diversions involved - output may be incorrect"));
  582. print(STDERR " $_\n")
  583. || syserr(_g("write diversion info to stderr"));
  584. } elsif (m/^([^:]+): (\S+)$/) {
  585. $cached_pkgmatch{$2} = $pkgmatch->{$2} = [ split(/, /, $1) ];
  586. } else {
  587. warning(_g("unknown output from dpkg --search: '%s'"), $_);
  588. }
  589. }
  590. close(DPKG);
  591. return $pkgmatch;
  592. }