Deps.pm 30 KB

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