Deps.pm 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. # Copyright © 2007-2009 Raphael Hertzog <hertzog@debian.org>
  2. #
  3. # This program is free software; you may redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2, or (at your option)
  6. # any later version.
  7. #
  8. # This is distributed in the hope that it will be useful, but without
  9. # any warranty; without even the implied warranty of merchantability or
  10. # fitness for a particular purpose. See the GNU General Public License
  11. # for more details.
  12. #
  13. # A copy of the GNU General Public License is available as
  14. # /usr/share/common-licenses/GPL in the Debian GNU/Linux distribution
  15. # or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html.
  16. # You can also obtain it by writing to the Free Software Foundation, Inc.,
  17. # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  18. #########################################################################
  19. # Several parts are inspired by lib/Dep.pm from lintian (same license)
  20. #
  21. # Copyright © 1998 Richard Braakman
  22. # Portions Copyright © 1999 Darren Benham
  23. # Portions Copyright © 2000 Sean 'Shaleh' Perry
  24. # Portions Copyright © 2004 Frank Lichtenheld
  25. # Portions Copyright © 2006 Russ Allbery
  26. package Dpkg::Deps;
  27. =head1 NAME
  28. Dpkg::Deps - parse and manipulate dependencies of Debian packages
  29. =head1 DESCRIPTION
  30. The Dpkg::Deps module provides one generic Dpkg::Deps::parse() function
  31. to turn a dependency line in a set of Dpkg::Deps::{Simple,AND,OR,Union}
  32. objects depending on the case.
  33. It also provides some constants:
  34. =over 4
  35. =item @pkg_dep_fields
  36. List of fields that contains dependency-like information in a binary
  37. Debian package. The fields that express real dependencies are sorted from
  38. the stronger to the weaker.
  39. =item @src_dep_fields
  40. List of fields that contains dependencies-like information in a binary
  41. Debian package.
  42. =item %dep_field_type
  43. Associate to each field a type, either "normal" for a real dependency field
  44. (Pre-Depends, Depends, ...) or "union" for other relation fields sharing
  45. the same syntax (Conflicts, Breaks, etc.).
  46. =back
  47. =head1 FUNCTIONS
  48. =over 4
  49. =cut
  50. use strict;
  51. use warnings;
  52. use Dpkg::Version qw(compare_versions);
  53. use Dpkg::Arch qw(get_host_arch);
  54. use Dpkg::ErrorHandling;
  55. use Dpkg::Gettext;
  56. use base qw(Exporter);
  57. our @EXPORT_OK = qw(@pkg_dep_fields @src_dep_fields %dep_field_type
  58. %relation_ordering);
  59. # Some generic variables
  60. our @pkg_dep_fields = qw(Pre-Depends Depends Recommends Suggests Enhances
  61. Conflicts Breaks Replaces Provides);
  62. our @src_dep_fields = qw(Build-Depends Build-Depends-Indep
  63. Build-Conflicts Build-Conflicts-Indep);
  64. our %dep_field_type = (
  65. 'Pre-Depends' => 'normal',
  66. 'Depends' => 'normal',
  67. 'Recommends' => 'normal',
  68. 'Suggests' => 'normal',
  69. 'Enhances' => 'union',
  70. 'Conflicts' => 'union',
  71. 'Breaks' => 'union',
  72. 'Replaces' => 'union',
  73. 'Provides' => 'union',
  74. 'Build-Depends' => 'normal',
  75. 'Build-Depends-Indep' => 'normal',
  76. 'Build-Conflicts' => 'union',
  77. 'Build-Conflicts-Indep' => 'union',
  78. );
  79. our %relation_ordering = (
  80. 'undef' => 0,
  81. '>=' => 1,
  82. '>>' => 2,
  83. '=' => 3,
  84. '<<' => 4,
  85. '<=' => 5,
  86. );
  87. # Some factorized function
  88. =item Dpkg::Deps::arch_is_superset(\@p, \@q)
  89. Returns true if the arch list @p is a superset of arch list @q.
  90. The arguments can also be undef in case there's no explicit architecture
  91. restriction.
  92. =cut
  93. sub arch_is_superset {
  94. my ($p, $q) = @_;
  95. my $p_arch_neg = defined($p) && $p->[0] =~ /^!/;
  96. my $q_arch_neg = defined($q) && $q->[0] =~ /^!/;
  97. # If "p" has no arches, it is a superset of q and we should fall through
  98. # to the version check.
  99. if (not defined $p) {
  100. return 1;
  101. }
  102. # If q has no arches, it is a superset of p and there are no useful
  103. # implications.
  104. elsif (not defined $q) {
  105. return 0;
  106. }
  107. # Both have arches. If neither are negated, we know nothing useful
  108. # unless q is a subset of p.
  109. elsif (not $p_arch_neg and not $q_arch_neg) {
  110. my %p_arches = map { $_ => 1 } @{$p};
  111. my $subset = 1;
  112. for my $arch (@{$q}) {
  113. $subset = 0 unless $p_arches{$arch};
  114. }
  115. return 0 unless $subset;
  116. }
  117. # If both are negated, we know nothing useful unless p is a subset of
  118. # q (and therefore has fewer things excluded, and therefore is more
  119. # general).
  120. elsif ($p_arch_neg and $q_arch_neg) {
  121. my %q_arches = map { $_ => 1 } @{$q};
  122. my $subset = 1;
  123. for my $arch (@{$p}) {
  124. $subset = 0 unless $q_arches{$arch};
  125. }
  126. return 0 unless $subset;
  127. }
  128. # If q is negated and p isn't, we'd need to know the full list of
  129. # arches to know if there's any relationship, so bail.
  130. elsif (not $p_arch_neg and $q_arch_neg) {
  131. return 0;
  132. }
  133. # If p is negated and q isn't, q is a subset of p if none of the
  134. # negated arches in p are present in q.
  135. elsif ($p_arch_neg and not $q_arch_neg) {
  136. my %q_arches = map { $_ => 1 } @{$q};
  137. my $subset = 1;
  138. for my $arch (@{$p}) {
  139. $subset = 0 if $q_arches{substr($arch, 1)};
  140. }
  141. return 0 unless $subset;
  142. }
  143. return 1;
  144. }
  145. =item Dpkg::Deps::version_implies($rel_p, $v_p, $rel_q, $v_q)
  146. ($rel_p, $v_p) and ($rel_q, $v_q) express two dependencies as (relation,
  147. version). The relation variable can have the following values: '=', '<<',
  148. '<=', '>=', '>>'.
  149. This functions returns 1 if the "p" dependency implies the "q"
  150. dependency. It returns 0 if the "p" dependency implies that "q" is
  151. not satisfied. It returns undef when there's no implication.
  152. =cut
  153. sub version_implies {
  154. my ($rel_p, $v_p, $rel_q, $v_q) = @_;
  155. # q wants an exact version, so p must provide that exact version. p
  156. # disproves q if q's version is outside the range enforced by p.
  157. if ($rel_q eq '=') {
  158. if ($rel_p eq '<<') {
  159. return compare_versions($v_p, '<=', $v_q) ? 0 : undef;
  160. } elsif ($rel_p eq '<=') {
  161. return compare_versions($v_p, '<<', $v_q) ? 0 : undef;
  162. } elsif ($rel_p eq '>>') {
  163. return compare_versions($v_p, '>=', $v_q) ? 0 : undef;
  164. } elsif ($rel_p eq '>=') {
  165. return compare_versions($v_p, '>>', $v_q) ? 0 : undef;
  166. } elsif ($rel_p eq '=') {
  167. return compare_versions($v_p, '=', $v_q);
  168. }
  169. }
  170. # A greater than clause may disprove a less than clause. An equal
  171. # cause might as well. Otherwise, if
  172. # p's clause is <<, <=, or =, the version must be <= q's to imply q.
  173. if ($rel_q eq '<=') {
  174. if ($rel_p eq '>>') {
  175. return compare_versions($v_p, '>=', $v_q) ? 0 : undef;
  176. } elsif ($rel_p eq '>=') {
  177. return compare_versions($v_p, '>>', $v_q) ? 0 : undef;
  178. } elsif ($rel_p eq '=') {
  179. return compare_versions($v_p, '<=', $v_q) ? 1 : 0;
  180. } else { # <<, <=
  181. return compare_versions($v_p, '<=', $v_q) ? 1 : undef;
  182. }
  183. }
  184. # Similar, but << is stronger than <= so p's version must be << q's
  185. # version if the p relation is <= or =.
  186. if ($rel_q eq '<<') {
  187. if ($rel_p eq '>>' or $rel_p eq '>=') {
  188. return compare_versions($v_p, '>=', $v_p) ? 0 : undef;
  189. } elsif ($rel_p eq '<<') {
  190. return compare_versions($v_p, '<=', $v_q) ? 1 : undef;
  191. } elsif ($rel_p eq '=') {
  192. return compare_versions($v_p, '<<', $v_q) ? 1 : 0;
  193. } else { # <<, <=
  194. return compare_versions($v_p, '<<', $v_q) ? 1 : undef;
  195. }
  196. }
  197. # Same logic as above, only inverted.
  198. if ($rel_q eq '>=') {
  199. if ($rel_p eq '<<') {
  200. return compare_versions($v_p, '<=', $v_q) ? 0 : undef;
  201. } elsif ($rel_p eq '<=') {
  202. return compare_versions($v_p, '<<', $v_q) ? 0 : undef;
  203. } elsif ($rel_p eq '=') {
  204. return compare_versions($v_p, '>=', $v_q) ? 1 : 0;
  205. } else { # >>, >=
  206. return compare_versions($v_p, '>=', $v_q) ? 1 : undef;
  207. }
  208. }
  209. if ($rel_q eq '>>') {
  210. if ($rel_p eq '<<' or $rel_p eq '<=') {
  211. return compare_versions($v_p, '<=', $v_q) ? 0 : undef;
  212. } elsif ($rel_p eq '>>') {
  213. return compare_versions($v_p, '>=', $v_q) ? 1 : undef;
  214. } elsif ($rel_p eq '=') {
  215. return compare_versions($v_p, '>>', $v_q) ? 1 : 0;
  216. } else {
  217. return compare_versions($v_p, '>>', $v_q) ? 1 : undef;
  218. }
  219. }
  220. return undef;
  221. }
  222. =item Dpkg::Deps::parse($line, %options)
  223. This function parse the dependency line and returns an object, either a
  224. Dpkg::Deps::AND or a Dpkg::Deps::Union. Various options can alter the
  225. behaviour of that function.
  226. =over 4
  227. =item use_arch (defaults to 1)
  228. Take into account the architecture restriction part of the dependencies.
  229. Set to 0 to completely ignore that information.
  230. =item host_arch (defaults to the current architecture)
  231. Define the host architecture. Needed only if the reduce_arch option is
  232. set to 1. By default it uses Dpkg::Arch::get_host_arch() to identify
  233. the proper architecture.
  234. =item reduce_arch (defaults to 0)
  235. If set to 1, ignore dependencies that do not concern the current host
  236. architecture. This implicitely strips off the architecture restriction
  237. list so that the resulting dependencies are directly applicable to the
  238. current architecture.
  239. =item union (defaults to 0)
  240. If set to 1, returns a Dpkg::Deps::Union instead of a Dpkg::Deps::AND. Use
  241. this when parsing non-dependency fields like Conflicts (see
  242. %dep_field_type).
  243. =back
  244. =cut
  245. sub parse {
  246. my $dep_line = shift;
  247. my %options = (@_);
  248. $options{use_arch} = 1 if not exists $options{use_arch};
  249. $options{reduce_arch} = 0 if not exists $options{reduce_arch};
  250. $options{host_arch} = get_host_arch() if not exists $options{host_arch};
  251. $options{union} = 0 if not exists $options{union};
  252. # Strip trailing/leading spaces
  253. $dep_line =~ s/^\s+//;
  254. $dep_line =~ s/\s+$//;
  255. my @dep_list;
  256. foreach my $dep_and (split(/\s*,\s*/m, $dep_line)) {
  257. my @or_list = ();
  258. foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
  259. my $dep_simple = Dpkg::Deps::Simple->new($dep_or);
  260. if (not defined $dep_simple->{package}) {
  261. warning(_g("can't parse dependency %s"), $dep_or);
  262. return undef;
  263. }
  264. $dep_simple->{arches} = undef if not $options{use_arch};
  265. if ($options{reduce_arch}) {
  266. $dep_simple->reduce_arch($options{host_arch});
  267. next if not $dep_simple->arch_is_concerned($options{host_arch});
  268. }
  269. push @or_list, $dep_simple;
  270. }
  271. next if not @or_list;
  272. if (scalar @or_list == 1) {
  273. push @dep_list, $or_list[0];
  274. } else {
  275. my $dep_or = Dpkg::Deps::OR->new();
  276. $dep_or->add($_) foreach (@or_list);
  277. push @dep_list, $dep_or;
  278. }
  279. }
  280. my $dep_and;
  281. if ($options{union}) {
  282. $dep_and = Dpkg::Deps::Union->new();
  283. } else {
  284. $dep_and = Dpkg::Deps::AND->new();
  285. }
  286. foreach my $dep (@dep_list) {
  287. if ($options{union} and not $dep->isa("Dpkg::Deps::Simple")) {
  288. warning(_g("an union dependency can only contain simple dependencies"));
  289. return undef;
  290. }
  291. $dep_and->add($dep);
  292. }
  293. return $dep_and;
  294. }
  295. =item Dpkg::Deps::compare($a, $b)
  296. This functions is used to order AND and Union dependencies prior to
  297. dumping.
  298. =back
  299. =cut
  300. sub compare {
  301. my ($a, $b) = @_;
  302. return -1 if $a->is_empty();
  303. return 1 if $b->is_empty();
  304. while ($a->isa('Dpkg::Deps::Multiple')) {
  305. return -1 if $a->is_empty();
  306. my @deps = $a->get_deps();
  307. $a = $deps[0];
  308. }
  309. while ($b->isa('Dpkg::Deps::Multiple')) {
  310. return 1 if $b->is_empty();
  311. my @deps = $b->get_deps();
  312. $b = $deps[0];
  313. }
  314. my $ar = defined($a->{relation}) ? $a->{relation} : "undef";
  315. my $br = defined($b->{relation}) ? $b->{relation} : "undef";
  316. return (($a->{package} cmp $b->{package}) ||
  317. ($relation_ordering{$ar} <=> $relation_ordering{$br}) ||
  318. ($a->{version} cmp $b->{version}));
  319. }
  320. package Dpkg::Deps::Simple;
  321. =head1 OBJECTS - Dpkg::Deps::*
  322. There are several kind of dependencies. A Dpkg::Deps::Simple dependency
  323. represents a single dependency statement (it relates to one package only).
  324. Dpkg::Deps::Multiple dependencies are built on top of this object
  325. and combine several dependencies in a different manners. Dpkg::Deps::AND
  326. represents the logical "AND" between dependencies while Dpkg::Deps::OR
  327. represents the logical "OR". Dpkg::Deps::Multiple objects can contain
  328. Dpkg::Deps::Simple object as well as other Dpkg::Deps::Multiple objects.
  329. In practice, the code is only meant to handle the realistic cases which,
  330. given Debian's dependencies structure, imply those restrictions: AND can
  331. contain Simple or OR objects, OR can only contain Simple objects.
  332. Dpkg::Deps::KnowFacts is a special object that is used while evaluating
  333. dependencies and while trying to simplify them. It represents a set of
  334. installed packages along with the virtual packages that they might
  335. provide.
  336. =head2 Common functions
  337. =over 4
  338. =item $dep->is_empty()
  339. Returns true if the dependency is empty and doesn't contain any useful
  340. information. This is true when a Dpkg::Deps::Simple object has not yet
  341. been initialized or when a (descendant of) Dpkg::Deps::Multiple contains
  342. an empty list of dependencies.
  343. =item $dep->get_deps()
  344. Return a list of sub-dependencies. For Dpkg::Deps::Simple it returns
  345. itself.
  346. =item $dep->dump()
  347. Return a string representing the dependency.
  348. =item $dep->implies($other_dep)
  349. Returns 1 when $dep implies $other_dep. Returns 0 when $dep implies
  350. NOT($other_dep). Returns undef when there's no implication. $dep and
  351. $other_dep do not need to be of the same type.
  352. =item $dep->sort()
  353. Sort alphabetically the internal list of dependencies. It's a no-op for
  354. Dpkg::Deps::Simple objects.
  355. =item $dep->arch_is_concerned($arch)
  356. Returns true if the dependency applies to the indicated architecture. For
  357. multiple dependencies, it returns true if at least one of the
  358. sub-dependencies apply to this architecture.
  359. =item $dep->reduce_arch($arch)
  360. Simplify the dependency to contain only information relevant to the given
  361. architecture. A Dpkg::Deps::Simple object can be left empty after this
  362. operation. For Dpkg::Deps::Multiple objects, the non-relevant
  363. sub-dependencies are simply removed.
  364. This trims off the architecture restriction list of Dpkg::Deps::Simple
  365. objects.
  366. =item $dep->get_evaluation($facts)
  367. Evaluates the dependency given a list of installed packages and a list of
  368. virtual packages provided. Those lists are part of the
  369. Dpkg::Deps::KnownFacts object given as parameters.
  370. Returns 1 when it's true, 0 when it's false, undef when some information
  371. is lacking to conclude.
  372. =item $dep->simplify_deps($facts, @assumed_deps)
  373. Simplify the dependency as much as possible given the list of facts (see
  374. object Dpkg::Deps::KnownFacts) and a list of other dependencies that we
  375. know to be true.
  376. =back
  377. =head2 Dpkg::Deps::Simple
  378. Such an object has four interesting properties:
  379. =over 4
  380. =item package
  381. The package name (can be undef if the dependency has not been initialized
  382. or if the simplification of the dependency lead to its removal).
  383. =item relation
  384. The relational operator: "=", "<<", "<=", ">=" or ">>". It can be
  385. undefined if the dependency had no version restriction. In that case the
  386. following field is also undefined.
  387. =item version
  388. The version.
  389. =item arches
  390. The list of architectures where this dependency is applicable. It's
  391. undefined when there's no restriction, otherwise it's an
  392. array ref. It can contain an exclusion list, in that case each
  393. architecture is prefixed with an exclamation mark.
  394. =back
  395. =head3 Methods
  396. =over 4
  397. =item $simple_dep->parse("dpkg-dev (>= 1.14.8) [!hurd-i386]")
  398. Parse the dependency and modify internal properties to match the parsed
  399. dependency.
  400. =item $simple_dep->merge_union($other_dep)
  401. Returns true if $simple_dep could be modified to represent the union of
  402. both dependencies. Otherwise returns false.
  403. =back
  404. =cut
  405. use strict;
  406. use warnings;
  407. use Dpkg::Arch qw(debarch_is);
  408. use Dpkg::Version qw(compare_versions);
  409. use Dpkg::ErrorHandling;
  410. use Dpkg::Gettext;
  411. sub new {
  412. my ($this, $arg) = @_;
  413. my $class = ref($this) || $this;
  414. my $self = {
  415. 'package' => undef,
  416. 'relation' => undef,
  417. 'version' => undef,
  418. 'arches' => undef,
  419. };
  420. bless $self, $class;
  421. $self->parse($arg) if defined($arg);
  422. return $self;
  423. }
  424. sub parse {
  425. my ($self, $dep) = @_;
  426. return if not $dep =~
  427. /^\s* # skip leading whitespace
  428. ([a-zA-Z0-9][a-zA-Z0-9+.-]*) # package name
  429. (?: # start of optional part
  430. \s* \( # open parenthesis for version part
  431. \s* (<<|<=|=|>=|>>|<|>) # relation part
  432. \s* (.*?) # do not attempt to parse version
  433. \s* \) # closing parenthesis
  434. )? # end of optional part
  435. (?: # start of optional architecture
  436. \s* \[ # open bracket for architecture
  437. \s* (.*?) # don't parse architectures now
  438. \s* \] # closing bracket
  439. )? # end of optional architecture
  440. \s*$ # trailing spaces at end
  441. /x;
  442. $self->{package} = $1;
  443. $self->{relation} = $2;
  444. $self->{version} = $3;
  445. if (defined($4)) {
  446. $self->{arches} = [ split(/\s+/, $4) ];
  447. }
  448. # Standardize relation field
  449. if (defined($self->{relation})) {
  450. $self->{relation} = '<<' if ($self->{relation} eq '<');
  451. $self->{relation} = '>>' if ($self->{relation} eq '>');
  452. }
  453. }
  454. sub dump {
  455. my $self = shift;
  456. my $res = $self->{package};
  457. if (defined($self->{relation})) {
  458. $res .= " (" . $self->{relation} . " " . $self->{version} . ")";
  459. }
  460. if (defined($self->{'arches'})) {
  461. $res .= " [" . join(" ", @{$self->{arches}}) . "]";
  462. }
  463. return $res;
  464. }
  465. # Returns true if the dependency in parameter can deduced from the current
  466. # dependency. Returns false if it can be negated. Returns undef if nothing
  467. # can be concluded.
  468. sub implies {
  469. my ($self, $o) = @_;
  470. if ($o->isa('Dpkg::Deps::Simple')) {
  471. # An implication is only possible on the same package
  472. return undef if $self->{package} ne $o->{package};
  473. # Our architecture set must be a superset of the architectures for
  474. # o, otherwise we can't conclude anything.
  475. return undef unless Dpkg::Deps::arch_is_superset($self->{arches}, $o->{arches});
  476. # If o has no version clause, then our dependency is stronger
  477. return 1 if not defined $o->{relation};
  478. # If o has a version clause, we must also have one, otherwise there
  479. # can't be an implication
  480. return undef if not defined $self->{relation};
  481. return Dpkg::Deps::version_implies($self->{relation}, $self->{version},
  482. $o->{relation}, $o->{version});
  483. } elsif ($o->isa('Dpkg::Deps::AND')) {
  484. # TRUE: Need to imply all individual elements
  485. # FALSE: Need to NOT imply at least one individual element
  486. my $res = 1;
  487. foreach my $dep ($o->get_deps()) {
  488. my $implication = $self->implies($dep);
  489. unless (defined($implication) && $implication == 1) {
  490. $res = $implication;
  491. last if defined $res;
  492. }
  493. }
  494. return $res;
  495. } elsif ($o->isa('Dpkg::Deps::OR')) {
  496. # TRUE: Need to imply at least one individual element
  497. # FALSE: Need to not apply all individual elements
  498. # UNDEF: The rest
  499. my $res = undef;
  500. foreach my $dep ($o->get_deps()) {
  501. my $implication = $self->implies($dep);
  502. if (defined($implication)) {
  503. if (not defined $res) {
  504. $res = $implication;
  505. } else {
  506. if ($implication) {
  507. $res = 1;
  508. } else {
  509. $res = 0;
  510. }
  511. }
  512. last if defined($res) && $res == 1;
  513. }
  514. }
  515. return $res;
  516. } else {
  517. internerr("Dpkg::Deps::Simple can't evaluate implication with a %s!",
  518. ref($o));
  519. }
  520. }
  521. sub get_deps {
  522. my $self = shift;
  523. return $self;
  524. }
  525. sub sort {
  526. # Nothing to sort
  527. }
  528. sub arch_is_concerned {
  529. my ($self, $host_arch) = @_;
  530. return 0 if not defined $self->{package}; # Empty dep
  531. return 1 if not defined $self->{arches}; # Dep without arch spec
  532. my $seen_arch = 0;
  533. foreach my $arch (@{$self->{arches}}) {
  534. $arch=lc($arch);
  535. if ($arch =~ /^!/) {
  536. my $not_arch = $arch;
  537. $not_arch =~ s/^!//;
  538. if (debarch_is($host_arch, $not_arch)) {
  539. $seen_arch = 0;
  540. last;
  541. } else {
  542. # !arch includes by default all other arches
  543. # unless they also appear in a !otherarch
  544. $seen_arch = 1;
  545. }
  546. } elsif (debarch_is($host_arch, $arch)) {
  547. $seen_arch = 1;
  548. last;
  549. }
  550. }
  551. return $seen_arch;
  552. }
  553. sub reduce_arch {
  554. my ($self, $host_arch) = @_;
  555. if (not $self->arch_is_concerned($host_arch)) {
  556. $self->{package} = undef;
  557. $self->{relation} = undef;
  558. $self->{version} = undef;
  559. $self->{arches} = undef;
  560. } else {
  561. $self->{arches} = undef;
  562. }
  563. }
  564. sub get_evaluation {
  565. my ($self, $facts) = @_;
  566. return undef if not defined $self->{package};
  567. my ($check, $param) = $facts->check_package($self->{package});
  568. if ($check) {
  569. if (defined $self->{relation}) {
  570. if (ref($param)) {
  571. # Provided packages
  572. # XXX: Once support for versioned provides is in place,
  573. # this part must be adapted
  574. return 0;
  575. } else {
  576. if (defined($param)) {
  577. if (compare_versions($param, $self->{relation}, $self->{version})) {
  578. return 1;
  579. } else {
  580. return 0;
  581. }
  582. } else {
  583. return undef;
  584. }
  585. }
  586. } else {
  587. return 1;
  588. }
  589. }
  590. return 0;
  591. }
  592. sub simplify_deps {
  593. my ($self, $facts) = @_;
  594. my $eval = $self->get_evaluation($facts);
  595. if (defined($eval) and $eval == 1) {
  596. $self->{package} = undef;
  597. $self->{relation} = undef;
  598. $self->{version} = undef;
  599. $self->{arches} = undef;
  600. }
  601. }
  602. sub is_empty {
  603. my $self = shift;
  604. return not defined $self->{package};
  605. }
  606. sub merge_union {
  607. my ($self, $o) = @_;
  608. return 0 if not $o->isa('Dpkg::Deps::Simple');
  609. return 0 if $self->is_empty() or $o->is_empty();
  610. return 0 if $self->{package} ne $o->{package};
  611. return 0 if defined $self->{arches} or defined $o->{arches};
  612. if (not defined $o->{relation} and defined $self->{relation}) {
  613. # Union is the non-versioned dependency
  614. $self->{relation} = undef;
  615. $self->{version} = undef;
  616. return 1;
  617. }
  618. my $implication = $self->implies($o);
  619. my $rev_implication = $o->implies($self);
  620. if (defined($implication)) {
  621. if ($implication) {
  622. $self->{relation} = $o->{relation};
  623. $self->{version} = $o->{version};
  624. return 1;
  625. } else {
  626. return 0;
  627. }
  628. }
  629. if (defined($rev_implication)) {
  630. if ($rev_implication) {
  631. # Already merged...
  632. return 1;
  633. } else {
  634. return 0;
  635. }
  636. }
  637. return 0;
  638. }
  639. package Dpkg::Deps::Multiple;
  640. =head2 Dpkg::Deps::Multiple
  641. This the base class for Dpkg::Deps::{AND,OR,Union}. It contains the
  642. =over 4
  643. =item $mul->add($dep)
  644. Add a new dependency object at the end of the list.
  645. =back
  646. =cut
  647. use strict;
  648. use warnings;
  649. use Dpkg::ErrorHandling;
  650. sub new {
  651. my $this = shift;
  652. my $class = ref($this) || $this;
  653. my $self = { 'list' => [ @_ ] };
  654. bless $self, $class;
  655. return $self;
  656. }
  657. sub add {
  658. my $self = shift;
  659. push @{$self->{list}}, @_;
  660. }
  661. sub get_deps {
  662. my $self = shift;
  663. return grep { not $_->is_empty() } @{$self->{list}};
  664. }
  665. sub sort {
  666. my $self = shift;
  667. my @res = ();
  668. @res = sort { Dpkg::Deps::compare($a, $b) } @{$self->{list}};
  669. $self->{list} = [ @res ];
  670. }
  671. sub arch_is_concerned {
  672. my ($self, $host_arch) = @_;
  673. my $res = 0;
  674. foreach my $dep (@{$self->{list}}) {
  675. $res = 1 if $dep->arch_is_concerned($host_arch);
  676. }
  677. return $res;
  678. }
  679. sub reduce_arch {
  680. my ($self, $host_arch) = @_;
  681. my @new;
  682. foreach my $dep (@{$self->{list}}) {
  683. $dep->reduce_arch($host_arch);
  684. push @new, $dep if $dep->arch_is_concerned($host_arch);
  685. }
  686. $self->{list} = [ @new ];
  687. }
  688. sub is_empty {
  689. my $self = shift;
  690. return scalar @{$self->{list}} == 0;
  691. }
  692. sub merge_union {
  693. internerr("The method merge_union() is only valid for Dpkg::Deps::Simple");
  694. }
  695. package Dpkg::Deps::AND;
  696. =head2 Dpkg::Deps::AND
  697. This object represents a list of dependencies who must be met at the same
  698. time.
  699. =over 4
  700. =item $and->dump()
  701. The dump method uses ", " to join the list of sub-dependencies.
  702. =back
  703. =cut
  704. use strict;
  705. use warnings;
  706. our @ISA = qw(Dpkg::Deps::Multiple);
  707. sub dump {
  708. my $self = shift;
  709. return join(", ", map { $_->dump() } grep { not $_->is_empty() } $self->get_deps());
  710. }
  711. sub implies {
  712. my ($self, $o) = @_;
  713. # If any individual member can imply $o or NOT $o, we're fine
  714. foreach my $dep ($self->get_deps()) {
  715. my $implication = $dep->implies($o);
  716. return 1 if defined($implication) && $implication == 1;
  717. return 0 if defined($implication) && $implication == 0;
  718. }
  719. # If o is an AND, we might have an implication, if we find an
  720. # implication within us for each predicate in o
  721. if ($o->isa('Dpkg::Deps::AND')) {
  722. my $subset = 1;
  723. foreach my $odep ($o->get_deps()) {
  724. my $found = 0;
  725. foreach my $dep ($self->get_deps()) {
  726. $found = 1 if $dep->implies($odep);
  727. }
  728. $subset = 0 if not $found;
  729. }
  730. return 1 if $subset;
  731. }
  732. return undef;
  733. }
  734. sub get_evaluation {
  735. my ($self, $facts) = @_;
  736. # Return 1 only if all members evaluates to true
  737. # Return 0 if at least one member evaluates to false
  738. # Return undef otherwise
  739. my $result = 1;
  740. foreach my $dep ($self->get_deps()) {
  741. my $eval = $dep->get_evaluation($facts);
  742. if (not defined $eval) {
  743. $result = undef;
  744. } elsif ($eval == 0) {
  745. $result = 0;
  746. last;
  747. } elsif ($eval == 1) {
  748. # Still possible
  749. }
  750. }
  751. return $result;
  752. }
  753. sub simplify_deps {
  754. my ($self, $facts, @knowndeps) = @_;
  755. my @new;
  756. WHILELOOP:
  757. while (@{$self->{list}}) {
  758. my $dep = shift @{$self->{list}};
  759. my $eval = $dep->get_evaluation($facts);
  760. next if defined($eval) and $eval == 1;
  761. foreach my $odep (@knowndeps, @new) {
  762. next WHILELOOP if $odep->implies($dep);
  763. }
  764. # When a dependency is implied by another dependency that
  765. # follows, then invert them
  766. # "a | b, c, a" becomes "a, c" and not "c, a"
  767. my $i = 0;
  768. foreach my $odep (@{$self->{list}}) {
  769. if (defined $odep and $odep->implies($dep)) {
  770. splice @{$self->{list}}, $i, 1;
  771. unshift @{$self->{list}}, $odep;
  772. next WHILELOOP;
  773. }
  774. $i++;
  775. }
  776. push @new, $dep;
  777. }
  778. $self->{list} = [ @new ];
  779. }
  780. package Dpkg::Deps::OR;
  781. =head2 Dpkg::Deps::OR
  782. This object represents a list of dependencies of which only one must be met
  783. for the dependency to be true.
  784. =over 4
  785. =item $or->dump()
  786. The dump method uses " | " to join the list of sub-dependencies.
  787. =back
  788. =cut
  789. use strict;
  790. use warnings;
  791. our @ISA = qw(Dpkg::Deps::Multiple);
  792. sub dump {
  793. my $self = shift;
  794. return join(" | ", map { $_->dump() } grep { not $_->is_empty() } $self->get_deps());
  795. }
  796. sub implies {
  797. my ($self, $o) = @_;
  798. # Special case for AND with a single member, replace it by its member
  799. if ($o->isa('Dpkg::Deps::AND')) {
  800. my @subdeps = $o->get_deps();
  801. if (scalar(@subdeps) == 1) {
  802. $o = $subdeps[0];
  803. }
  804. }
  805. # In general, an OR dependency can't imply anything except if each
  806. # of its member implies a member in the other OR dependency
  807. if ($o->isa('Dpkg::Deps::OR')) {
  808. my $subset = 1;
  809. foreach my $dep ($self->get_deps()) {
  810. my $found = 0;
  811. foreach my $odep ($o->get_deps()) {
  812. $found = 1 if $dep->implies($odep);
  813. }
  814. $subset = 0 if not $found;
  815. }
  816. return 1 if $subset;
  817. }
  818. return undef;
  819. }
  820. sub get_evaluation {
  821. my ($self, $facts) = @_;
  822. # Returns false if all members evaluates to 0
  823. # Returns true if at least one member evaluates to true
  824. # Returns undef otherwise
  825. my $result = 0;
  826. foreach my $dep ($self->get_deps()) {
  827. my $eval = $dep->get_evaluation($facts);
  828. if (not defined $eval) {
  829. $result = undef;
  830. } elsif ($eval == 1) {
  831. $result = 1;
  832. last;
  833. } elsif ($eval == 0) {
  834. # Still possible to have a false evaluation
  835. }
  836. }
  837. return $result;
  838. }
  839. sub simplify_deps {
  840. my ($self, $facts) = @_;
  841. my @new;
  842. WHILELOOP:
  843. while (@{$self->{list}}) {
  844. my $dep = shift @{$self->{list}};
  845. my $eval = $dep->get_evaluation($facts);
  846. if (defined($eval) and $eval == 1) {
  847. $self->{list} = [];
  848. return;
  849. }
  850. foreach my $odep (@new, @{$self->{list}}) {
  851. next WHILELOOP if $odep->implies($dep);
  852. }
  853. push @new, $dep;
  854. }
  855. $self->{list} = [ @new ];
  856. }
  857. package Dpkg::Deps::Union;
  858. =head2 Dpkg::Deps::Union
  859. This object represents a list of relationships.
  860. =over 4
  861. =item $union->dump()
  862. The dump method uses ", " to join the list of relationships.
  863. =item $union->implies($other_dep)
  864. =item $union->get_evaluation($other_dep)
  865. Those methods are not meaningful for this object and always return undef.
  866. =item $union->simplify_deps($facts)
  867. The simplication is done to generate an union of all the relationships.
  868. It uses $simple_dep->merge_union($other_dep) to get the its job done.
  869. =back
  870. =cut
  871. use strict;
  872. use warnings;
  873. our @ISA = qw(Dpkg::Deps::Multiple);
  874. sub dump {
  875. my $self = shift;
  876. return join(", ", map { $_->dump() } grep { not $_->is_empty() } $self->get_deps());
  877. }
  878. sub implies {
  879. # Implication test are not useful on Union
  880. return undef;
  881. }
  882. sub get_evaluation {
  883. # Evaluation are not useful on Union
  884. return undef;
  885. }
  886. sub simplify_deps {
  887. my ($self, $facts) = @_;
  888. my @new;
  889. WHILELOOP:
  890. while (@{$self->{list}}) {
  891. my $dep = shift @{$self->{list}};
  892. foreach my $odep (@new) {
  893. next WHILELOOP if $dep->merge_union($odep);
  894. }
  895. push @new, $dep;
  896. }
  897. $self->{list} = [ @new ];
  898. }
  899. package Dpkg::Deps::KnownFacts;
  900. =head2 Dpkg::Deps::KnowFacts
  901. This object represents a list of installed packages and a list of virtual
  902. packages provided (by the set of installed packages).
  903. =over 4
  904. =item my $facts = Dpkg::Deps::KnownFacts->new();
  905. Create a new object.
  906. =cut
  907. use strict;
  908. use warnings;
  909. sub new {
  910. my $this = shift;
  911. my $class = ref($this) || $this;
  912. my $self = { 'pkg' => {}, 'virtualpkg' => {} };
  913. bless $self, $class;
  914. return $self;
  915. }
  916. =item $facts->add_installed_package($package, $version)
  917. Record that the given version of the package is installed. If $version is
  918. undefined we know that the package is installed but we don't know which
  919. version it is.
  920. =cut
  921. sub add_installed_package {
  922. my ($self, $pkg, $ver) = @_;
  923. $self->{pkg}{$pkg} = $ver;
  924. }
  925. =item $facts->add_provided_package($virtual, $relation, $version, $by)
  926. Record that the "$by" package provides the $virtual package. $relation
  927. and $version correspond to the associated relation given in the Provides
  928. field. This might be used in the future for versioned provides.
  929. =cut
  930. sub add_provided_package {
  931. my ($self, $pkg, $rel, $ver, $by) = @_;
  932. if (not exists $self->{virtualpkg}{$pkg}) {
  933. $self->{virtualpkg}{$pkg} = [];
  934. }
  935. push @{$self->{virtualpkg}{$pkg}}, [ $by, $rel, $ver ];
  936. }
  937. =item my ($check, $param) = $facts->check_package($package)
  938. $check is one when the package is found. For a real package, $param
  939. contains the version. For a virtual package, $param contains an array
  940. reference containing the list of packages that provide it (each package is
  941. listed as [ $provider, $relation, $version ]).
  942. =back
  943. =cut
  944. sub check_package {
  945. my ($self, $pkg) = @_;
  946. if (exists $self->{pkg}{$pkg}) {
  947. return (1, $self->{pkg}{$pkg});
  948. }
  949. if (exists $self->{virtualpkg}{$pkg}) {
  950. return (1, $self->{virtualpkg}{$pkg});
  951. }
  952. return (0, undef);
  953. }
  954. 1;