Deps.pm 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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.01";
  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. # Dpkg::Deps::_arch_qualifier_allows_implication($p, $q)
  104. #
  105. # Returns true if the arch qualifier $p and $q are compatible with the
  106. # implication $p -> $q, false otherwise. $p/$q can be
  107. # undef/"any"/"native" or an architecture string.
  108. sub _arch_qualifier_allows_implication {
  109. my ($p, $q) = @_;
  110. if (defined $p and $p eq "any") {
  111. return 1 if defined $q and $q eq "any";
  112. return 0;
  113. } elsif (defined $p and $p eq "native") {
  114. return 1 if defined $q and ($q eq "any" or $q eq "native");
  115. return 0;
  116. } elsif (defined $p) {
  117. return 1 if defined $q and ($p eq $q or $q eq "any");
  118. return 0;
  119. } else {
  120. return 0 if defined $q and $q ne "any" and $q ne "native";
  121. return 1;
  122. }
  123. }
  124. =item deps_eval_implication($rel_p, $v_p, $rel_q, $v_q)
  125. ($rel_p, $v_p) and ($rel_q, $v_q) express two dependencies as (relation,
  126. version). The relation variable can have the following values that are
  127. exported by Dpkg::Version: REL_EQ, REL_LT, REL_LE, REL_GT, REL_GT.
  128. This functions returns 1 if the "p" dependency implies the "q"
  129. dependency. It returns 0 if the "p" dependency implies that "q" is
  130. not satisfied. It returns undef when there's no implication.
  131. The $v_p and $v_q parameter should be Dpkg::Version objects.
  132. =cut
  133. sub deps_eval_implication {
  134. my ($rel_p, $v_p, $rel_q, $v_q) = @_;
  135. # If versions are not valid, we can't decide of any implication
  136. return undef unless defined($v_p) and $v_p->is_valid();
  137. return undef unless defined($v_q) and $v_q->is_valid();
  138. # q wants an exact version, so p must provide that exact version. p
  139. # disproves q if q's version is outside the range enforced by p.
  140. if ($rel_q eq REL_EQ) {
  141. if ($rel_p eq REL_LT) {
  142. return ($v_p <= $v_q) ? 0 : undef;
  143. } elsif ($rel_p eq REL_LE) {
  144. return ($v_p < $v_q) ? 0 : undef;
  145. } elsif ($rel_p eq REL_GT) {
  146. return ($v_p >= $v_q) ? 0 : undef;
  147. } elsif ($rel_p eq REL_GE) {
  148. return ($v_p > $v_q) ? 0 : undef;
  149. } elsif ($rel_p eq REL_EQ) {
  150. return ($v_p == $v_q);
  151. }
  152. }
  153. # A greater than clause may disprove a less than clause. An equal
  154. # cause might as well. Otherwise, if
  155. # p's clause is <<, <=, or =, the version must be <= q's to imply q.
  156. if ($rel_q eq REL_LE) {
  157. if ($rel_p eq REL_GT) {
  158. return ($v_p >= $v_q) ? 0 : undef;
  159. } elsif ($rel_p eq REL_GE) {
  160. return ($v_p > $v_q) ? 0 : undef;
  161. } elsif ($rel_p eq REL_EQ) {
  162. return ($v_p <= $v_q) ? 1 : 0;
  163. } else { # <<, <=
  164. return ($v_p <= $v_q) ? 1 : undef;
  165. }
  166. }
  167. # Similar, but << is stronger than <= so p's version must be << q's
  168. # version if the p relation is <= or =.
  169. if ($rel_q eq REL_LT) {
  170. if ($rel_p eq REL_GT or $rel_p eq REL_GE) {
  171. return ($v_p >= $v_p) ? 0 : undef;
  172. } elsif ($rel_p eq REL_LT) {
  173. return ($v_p <= $v_q) ? 1 : undef;
  174. } elsif ($rel_p eq REL_EQ) {
  175. return ($v_p < $v_q) ? 1 : 0;
  176. } else { # <<, <=
  177. return ($v_p < $v_q) ? 1 : undef;
  178. }
  179. }
  180. # Same logic as above, only inverted.
  181. if ($rel_q eq REL_GE) {
  182. if ($rel_p eq REL_LT) {
  183. return ($v_p <= $v_q) ? 0 : undef;
  184. } elsif ($rel_p eq REL_LE) {
  185. return ($v_p < $v_q) ? 0 : undef;
  186. } elsif ($rel_p eq REL_EQ) {
  187. return ($v_p >= $v_q) ? 1 : 0;
  188. } else { # >>, >=
  189. return ($v_p >= $v_q) ? 1 : undef;
  190. }
  191. }
  192. if ($rel_q eq REL_GT) {
  193. if ($rel_p eq REL_LT or $rel_p eq REL_LE) {
  194. return ($v_p <= $v_q) ? 0 : undef;
  195. } elsif ($rel_p eq REL_GT) {
  196. return ($v_p >= $v_q) ? 1 : undef;
  197. } elsif ($rel_p eq REL_EQ) {
  198. return ($v_p > $v_q) ? 1 : 0;
  199. } else {
  200. return ($v_p > $v_q) ? 1 : undef;
  201. }
  202. }
  203. return undef;
  204. }
  205. =item my $dep = deps_parse($line, %options)
  206. This function parses the dependency line and returns an object, either a
  207. Dpkg::Deps::AND or a Dpkg::Deps::Union. Various options can alter the
  208. behaviour of that function.
  209. =over 4
  210. =item use_arch (defaults to 1)
  211. Take into account the architecture restriction part of the dependencies.
  212. Set to 0 to completely ignore that information.
  213. =item host_arch (defaults to the current architecture)
  214. Define the host architecture. By default it uses
  215. Dpkg::Arch::get_host_arch() to identify the proper architecture.
  216. =item reduce_arch (defaults to 0)
  217. If set to 1, ignore dependencies that do not concern the current host
  218. architecture. This implicitely strips off the architecture restriction
  219. list so that the resulting dependencies are directly applicable to the
  220. current architecture.
  221. =item union (defaults to 0)
  222. If set to 1, returns a Dpkg::Deps::Union instead of a Dpkg::Deps::AND. Use
  223. this when parsing non-dependency fields like Conflicts.
  224. =back
  225. =cut
  226. sub deps_parse {
  227. my $dep_line = shift;
  228. my %options = (@_);
  229. $options{use_arch} = 1 if not exists $options{use_arch};
  230. $options{reduce_arch} = 0 if not exists $options{reduce_arch};
  231. $options{host_arch} = get_host_arch() if not exists $options{host_arch};
  232. $options{union} = 0 if not exists $options{union};
  233. # Strip trailing/leading spaces
  234. $dep_line =~ s/^\s+//;
  235. $dep_line =~ s/\s+$//;
  236. my @dep_list;
  237. foreach my $dep_and (split(/\s*,\s*/m, $dep_line)) {
  238. my @or_list = ();
  239. foreach my $dep_or (split(/\s*\|\s*/m, $dep_and)) {
  240. my $dep_simple = Dpkg::Deps::Simple->new($dep_or, host_arch =>
  241. $options{host_arch});
  242. if (not defined $dep_simple->{package}) {
  243. warning(_g("can't parse dependency %s"), $dep_or);
  244. return undef;
  245. }
  246. $dep_simple->{arches} = undef if not $options{use_arch};
  247. if ($options{reduce_arch}) {
  248. $dep_simple->reduce_arch($options{host_arch});
  249. next if not $dep_simple->arch_is_concerned($options{host_arch});
  250. }
  251. push @or_list, $dep_simple;
  252. }
  253. next if not @or_list;
  254. if (scalar @or_list == 1) {
  255. push @dep_list, $or_list[0];
  256. } else {
  257. my $dep_or = Dpkg::Deps::OR->new();
  258. $dep_or->add($_) foreach (@or_list);
  259. push @dep_list, $dep_or;
  260. }
  261. }
  262. my $dep_and;
  263. if ($options{union}) {
  264. $dep_and = Dpkg::Deps::Union->new();
  265. } else {
  266. $dep_and = Dpkg::Deps::AND->new();
  267. }
  268. foreach my $dep (@dep_list) {
  269. if ($options{union} and not $dep->isa("Dpkg::Deps::Simple")) {
  270. warning(_g("an union dependency can only contain simple dependencies"));
  271. return undef;
  272. }
  273. $dep_and->add($dep);
  274. }
  275. return $dep_and;
  276. }
  277. =item deps_compare($a, $b)
  278. Implements a comparison operator between two dependency objects.
  279. This function is mainly used to implement the sort() method.
  280. =back
  281. =cut
  282. our %relation_ordering = (
  283. 'undef' => 0,
  284. REL_GE() => 1,
  285. REL_GT() => 2,
  286. REL_EQ() => 3,
  287. REL_LT() => 4,
  288. REL_LE() => 5,
  289. );
  290. sub deps_compare {
  291. my ($a, $b) = @_;
  292. return -1 if $a->is_empty();
  293. return 1 if $b->is_empty();
  294. while ($a->isa('Dpkg::Deps::Multiple')) {
  295. return -1 if $a->is_empty();
  296. my @deps = $a->get_deps();
  297. $a = $deps[0];
  298. }
  299. while ($b->isa('Dpkg::Deps::Multiple')) {
  300. return 1 if $b->is_empty();
  301. my @deps = $b->get_deps();
  302. $b = $deps[0];
  303. }
  304. my $ar = defined($a->{relation}) ? $a->{relation} : "undef";
  305. my $br = defined($b->{relation}) ? $b->{relation} : "undef";
  306. return (($a->{package} cmp $b->{package}) ||
  307. ($relation_ordering{$ar} <=> $relation_ordering{$br}) ||
  308. ($a->{version} cmp $b->{version}));
  309. }
  310. package Dpkg::Deps::Simple;
  311. =head1 OBJECTS - Dpkg::Deps::*
  312. There are several kind of dependencies. A Dpkg::Deps::Simple dependency
  313. represents a single dependency statement (it relates to one package only).
  314. Dpkg::Deps::Multiple dependencies are built on top of this object
  315. and combine several dependencies in a different manners. Dpkg::Deps::AND
  316. represents the logical "AND" between dependencies while Dpkg::Deps::OR
  317. represents the logical "OR". Dpkg::Deps::Multiple objects can contain
  318. Dpkg::Deps::Simple object as well as other Dpkg::Deps::Multiple objects.
  319. In practice, the code is only meant to handle the realistic cases which,
  320. given Debian's dependencies structure, imply those restrictions: AND can
  321. contain Simple or OR objects, OR can only contain Simple objects.
  322. Dpkg::Deps::KnownFacts is a special object that is used while evaluating
  323. dependencies and while trying to simplify them. It represents a set of
  324. installed packages along with the virtual packages that they might
  325. provide.
  326. =head2 Common functions
  327. =over 4
  328. =item $dep->is_empty()
  329. Returns true if the dependency is empty and doesn't contain any useful
  330. information. This is true when a Dpkg::Deps::Simple object has not yet
  331. been initialized or when a (descendant of) Dpkg::Deps::Multiple contains
  332. an empty list of dependencies.
  333. =item $dep->get_deps()
  334. Returns a list of sub-dependencies. For Dpkg::Deps::Simple it returns
  335. itself.
  336. =item $dep->output([$fh])
  337. =item "$dep"
  338. Returns a string representing the dependency. If $fh is set, it prints
  339. the string to the filehandle.
  340. =item $dep->implies($other_dep)
  341. Returns 1 when $dep implies $other_dep. Returns 0 when $dep implies
  342. NOT($other_dep). Returns undef when there's no implication. $dep and
  343. $other_dep do not need to be of the same type.
  344. =item $dep->sort()
  345. Sorts alphabetically the internal list of dependencies. It's a no-op for
  346. Dpkg::Deps::Simple objects.
  347. =item $dep->arch_is_concerned($arch)
  348. Returns true if the dependency applies to the indicated architecture. For
  349. multiple dependencies, it returns true if at least one of the
  350. sub-dependencies apply to this architecture.
  351. =item $dep->reduce_arch($arch)
  352. Simplifies the dependency to contain only information relevant to the given
  353. architecture. A Dpkg::Deps::Simple object can be left empty after this
  354. operation. For Dpkg::Deps::Multiple objects, the non-relevant
  355. sub-dependencies are simply removed.
  356. This trims off the architecture restriction list of Dpkg::Deps::Simple
  357. objects.
  358. =item $dep->get_evaluation($facts)
  359. Evaluates the dependency given a list of installed packages and a list of
  360. virtual packages provided. Those lists are part of the
  361. Dpkg::Deps::KnownFacts object given as parameters.
  362. Returns 1 when it's true, 0 when it's false, undef when some information
  363. is lacking to conclude.
  364. =item $dep->simplify_deps($facts, @assumed_deps)
  365. Simplifies the dependency as much as possible given the list of facts (see
  366. object Dpkg::Deps::KnownFacts) and a list of other dependencies that are
  367. known to be true.
  368. =item $dep->has_arch_restriction()
  369. For a simple dependency, returns the package name if the dependency
  370. applies only to a subset of architectures. For multiple dependencies, it
  371. returns the list of package names that have such a restriction.
  372. =item $dep->reset()
  373. Clears any dependency information stored in $dep so that $dep->is_empty()
  374. returns true.
  375. =back
  376. =head2 Dpkg::Deps::Simple
  377. Such an object has four interesting properties:
  378. =over 4
  379. =item package
  380. The package name (can be undef if the dependency has not been initialized
  381. or if the simplification of the dependency lead to its removal).
  382. =item relation
  383. The relational operator: "=", "<<", "<=", ">=" or ">>". It can be
  384. undefined if the dependency had no version restriction. In that case the
  385. following field is also undefined.
  386. =item version
  387. The version.
  388. =item arches
  389. The list of architectures where this dependency is applicable. It's
  390. undefined when there's no restriction, otherwise it's an
  391. array ref. It can contain an exclusion list, in that case each
  392. architecture is prefixed with an exclamation mark.
  393. =item archqual
  394. The arch qualifier of the dependency (can be undef if there's none).
  395. In the dependency "python:any (>= 2.6)", the arch qualifier is "any".
  396. =back
  397. =head3 Methods
  398. =over 4
  399. =item $simple_dep->parse_string("dpkg-dev (>= 1.14.8) [!hurd-i386]")
  400. Parses the dependency and modifies internal properties to match the parsed
  401. dependency.
  402. =item $simple_dep->merge_union($other_dep)
  403. Returns true if $simple_dep could be modified to represent the union of
  404. both dependencies. Otherwise returns false.
  405. =back
  406. =cut
  407. use strict;
  408. use warnings;
  409. use Dpkg::Arch qw(debarch_is);
  410. use Dpkg::Version;
  411. use Dpkg::ErrorHandling;
  412. use Dpkg::Gettext;
  413. use base qw(Dpkg::Interface::Storable);
  414. sub new {
  415. my ($this, $arg, %opts) = @_;
  416. my $class = ref($this) || $this;
  417. my $self = {};
  418. bless $self, $class;
  419. $self->reset();
  420. $self->{host_arch} = $opts{host_arch} || Dpkg::Arch::get_host_arch();
  421. $self->parse_string($arg) if defined($arg);
  422. return $self;
  423. }
  424. sub reset {
  425. my ($self) = @_;
  426. $self->{'package'} = undef;
  427. $self->{'relation'} = undef;
  428. $self->{'version'} = undef;
  429. $self->{'arches'} = undef;
  430. $self->{'archqual'} = undef;
  431. }
  432. sub parse {
  433. my ($self, $fh, $desc) = @_;
  434. my $line = <$fh>;
  435. chomp($line);
  436. return $self->parse_string($line);
  437. }
  438. sub parse_string {
  439. my ($self, $dep) = @_;
  440. return if not $dep =~
  441. /^\s* # skip leading whitespace
  442. ([a-zA-Z0-9][a-zA-Z0-9+.-]*) # package name
  443. (?: # start of optional part
  444. : # colon for architecture
  445. (any) # architecture name
  446. )? # end of optional part
  447. (?: # start of optional part
  448. \s* \( # open parenthesis for version part
  449. \s* (<<|<=|=|>=|>>|<|>) # relation part
  450. \s* (.*?) # do not attempt to parse version
  451. \s* \) # closing parenthesis
  452. )? # end of optional part
  453. (?: # start of optional architecture
  454. \s* \[ # open bracket for architecture
  455. \s* (.*?) # don't parse architectures now
  456. \s* \] # closing bracket
  457. )? # end of optional architecture
  458. \s*$ # trailing spaces at end
  459. /x;
  460. $self->{package} = $1;
  461. $self->{archqual} = $2 if defined($2);
  462. $self->{relation} = version_normalize_relation($3) if defined($3);
  463. if (defined($4)) {
  464. $self->{version} = Dpkg::Version->new($4);
  465. }
  466. if (defined($5)) {
  467. $self->{arches} = [ split(/\s+/, $5) ];
  468. }
  469. }
  470. sub output {
  471. my ($self, $fh) = @_;
  472. my $res = $self->{package};
  473. if (defined($self->{archqual})) {
  474. $res .= ":" . $self->{archqual};
  475. }
  476. if (defined($self->{relation})) {
  477. $res .= " (" . $self->{relation} . " " . $self->{version} . ")";
  478. }
  479. if (defined($self->{'arches'})) {
  480. $res .= " [" . join(" ", @{$self->{arches}}) . "]";
  481. }
  482. if (defined($fh)) {
  483. print $fh $res;
  484. }
  485. return $res;
  486. }
  487. # Returns true if the dependency in parameter can deduced from the current
  488. # dependency. Returns false if it can be negated. Returns undef if nothing
  489. # can be concluded.
  490. sub implies {
  491. my ($self, $o) = @_;
  492. if ($o->isa('Dpkg::Deps::Simple')) {
  493. # An implication is only possible on the same package
  494. return undef if $self->{package} ne $o->{package};
  495. # Our architecture set must be a superset of the architectures for
  496. # o, otherwise we can't conclude anything.
  497. return undef unless Dpkg::Deps::_arch_is_superset($self->{arches}, $o->{arches});
  498. # The arch qualifier must not forbid an implication
  499. return undef unless
  500. Dpkg::Deps::_arch_qualifier_allows_implication($self->{archqual},
  501. $o->{archqual});
  502. # If o has no version clause, then our dependency is stronger
  503. return 1 if not defined $o->{relation};
  504. # If o has a version clause, we must also have one, otherwise there
  505. # can't be an implication
  506. return undef if not defined $self->{relation};
  507. return Dpkg::Deps::deps_eval_implication($self->{relation},
  508. $self->{version}, $o->{relation}, $o->{version});
  509. } elsif ($o->isa('Dpkg::Deps::AND')) {
  510. # TRUE: Need to imply all individual elements
  511. # FALSE: Need to NOT imply at least one individual element
  512. my $res = 1;
  513. foreach my $dep ($o->get_deps()) {
  514. my $implication = $self->implies($dep);
  515. unless (defined($implication) && $implication == 1) {
  516. $res = $implication;
  517. last if defined $res;
  518. }
  519. }
  520. return $res;
  521. } elsif ($o->isa('Dpkg::Deps::OR')) {
  522. # TRUE: Need to imply at least one individual element
  523. # FALSE: Need to not apply all individual elements
  524. # UNDEF: The rest
  525. my $res = undef;
  526. foreach my $dep ($o->get_deps()) {
  527. my $implication = $self->implies($dep);
  528. if (defined($implication)) {
  529. if (not defined $res) {
  530. $res = $implication;
  531. } else {
  532. if ($implication) {
  533. $res = 1;
  534. } else {
  535. $res = 0;
  536. }
  537. }
  538. last if defined($res) && $res == 1;
  539. }
  540. }
  541. return $res;
  542. } else {
  543. internerr("Dpkg::Deps::Simple can't evaluate implication with a %s!",
  544. ref($o));
  545. }
  546. }
  547. sub get_deps {
  548. my $self = shift;
  549. return $self;
  550. }
  551. sub sort {
  552. # Nothing to sort
  553. }
  554. sub arch_is_concerned {
  555. my ($self, $host_arch) = @_;
  556. return 0 if not defined $self->{package}; # Empty dep
  557. return 1 if not defined $self->{arches}; # Dep without arch spec
  558. my $seen_arch = 0;
  559. foreach my $arch (@{$self->{arches}}) {
  560. $arch=lc($arch);
  561. if ($arch =~ /^!/) {
  562. my $not_arch = $arch;
  563. $not_arch =~ s/^!//;
  564. if (debarch_is($host_arch, $not_arch)) {
  565. $seen_arch = 0;
  566. last;
  567. } else {
  568. # !arch includes by default all other arches
  569. # unless they also appear in a !otherarch
  570. $seen_arch = 1;
  571. }
  572. } elsif (debarch_is($host_arch, $arch)) {
  573. $seen_arch = 1;
  574. last;
  575. }
  576. }
  577. return $seen_arch;
  578. }
  579. sub reduce_arch {
  580. my ($self, $host_arch) = @_;
  581. if (not $self->arch_is_concerned($host_arch)) {
  582. $self->reset();
  583. } else {
  584. $self->{arches} = undef;
  585. }
  586. }
  587. sub has_arch_restriction {
  588. my ($self) = @_;
  589. if (defined $self->{arches}) {
  590. return $self->{package};
  591. } else {
  592. return ();
  593. }
  594. }
  595. sub get_evaluation {
  596. my ($self, $facts) = @_;
  597. return undef if not defined $self->{package};
  598. return $facts->_evaluate_simple_dep($self);
  599. }
  600. sub simplify_deps {
  601. my ($self, $facts) = @_;
  602. my $eval = $self->get_evaluation($facts);
  603. $self->reset() if defined $eval and $eval == 1;
  604. }
  605. sub is_empty {
  606. my $self = shift;
  607. return not defined $self->{package};
  608. }
  609. sub merge_union {
  610. my ($self, $o) = @_;
  611. return 0 if not $o->isa('Dpkg::Deps::Simple');
  612. return 0 if $self->is_empty() or $o->is_empty();
  613. return 0 if $self->{package} ne $o->{package};
  614. return 0 if defined $self->{arches} or defined $o->{arches};
  615. if (not defined $o->{relation} and defined $self->{relation}) {
  616. # Union is the non-versioned dependency
  617. $self->{relation} = undef;
  618. $self->{version} = undef;
  619. return 1;
  620. }
  621. my $implication = $self->implies($o);
  622. my $rev_implication = $o->implies($self);
  623. if (defined($implication)) {
  624. if ($implication) {
  625. $self->{relation} = $o->{relation};
  626. $self->{version} = $o->{version};
  627. return 1;
  628. } else {
  629. return 0;
  630. }
  631. }
  632. if (defined($rev_implication)) {
  633. if ($rev_implication) {
  634. # Already merged...
  635. return 1;
  636. } else {
  637. return 0;
  638. }
  639. }
  640. return 0;
  641. }
  642. package Dpkg::Deps::Multiple;
  643. =head2 Dpkg::Deps::Multiple
  644. This is the base class for Dpkg::Deps::{AND,OR,Union}. It implements
  645. the following methods:
  646. =over 4
  647. =item $mul->add($dep)
  648. Adds a new dependency object at the end of the list.
  649. =back
  650. =cut
  651. use strict;
  652. use warnings;
  653. use Dpkg::ErrorHandling;
  654. use base qw(Dpkg::Interface::Storable);
  655. sub new {
  656. my $this = shift;
  657. my $class = ref($this) || $this;
  658. my $self = { 'list' => [ @_ ] };
  659. bless $self, $class;
  660. return $self;
  661. }
  662. sub reset {
  663. my ($self) = @_;
  664. $self->{'list'} = [];
  665. }
  666. sub add {
  667. my $self = shift;
  668. push @{$self->{list}}, @_;
  669. }
  670. sub get_deps {
  671. my $self = shift;
  672. return grep { not $_->is_empty() } @{$self->{list}};
  673. }
  674. sub sort {
  675. my $self = shift;
  676. my @res = ();
  677. @res = sort { Dpkg::Deps::deps_compare($a, $b) } @{$self->{list}};
  678. $self->{list} = [ @res ];
  679. }
  680. sub arch_is_concerned {
  681. my ($self, $host_arch) = @_;
  682. my $res = 0;
  683. foreach my $dep (@{$self->{list}}) {
  684. $res = 1 if $dep->arch_is_concerned($host_arch);
  685. }
  686. return $res;
  687. }
  688. sub reduce_arch {
  689. my ($self, $host_arch) = @_;
  690. my @new;
  691. foreach my $dep (@{$self->{list}}) {
  692. $dep->reduce_arch($host_arch);
  693. push @new, $dep if $dep->arch_is_concerned($host_arch);
  694. }
  695. $self->{list} = [ @new ];
  696. }
  697. sub has_arch_restriction {
  698. my ($self) = @_;
  699. my @res;
  700. foreach my $dep (@{$self->{list}}) {
  701. push @res, $dep->has_arch_restriction();
  702. }
  703. return @res;
  704. }
  705. sub is_empty {
  706. my $self = shift;
  707. return scalar @{$self->{list}} == 0;
  708. }
  709. sub merge_union {
  710. internerr("The method merge_union() is only valid for Dpkg::Deps::Simple");
  711. }
  712. package Dpkg::Deps::AND;
  713. =head2 Dpkg::Deps::AND
  714. This object represents a list of dependencies who must be met at the same
  715. time.
  716. =over 4
  717. =item $and->output([$fh])
  718. The output method uses ", " to join the list of sub-dependencies.
  719. =back
  720. =cut
  721. use strict;
  722. use warnings;
  723. use base qw(Dpkg::Deps::Multiple);
  724. sub output {
  725. my ($self, $fh) = @_;
  726. my $res = join(", ", map { $_->output() } grep { not $_->is_empty() } $self->get_deps());
  727. if (defined($fh)) {
  728. print $fh $res;
  729. }
  730. return $res;
  731. }
  732. sub implies {
  733. my ($self, $o) = @_;
  734. # If any individual member can imply $o or NOT $o, we're fine
  735. foreach my $dep ($self->get_deps()) {
  736. my $implication = $dep->implies($o);
  737. return 1 if defined($implication) && $implication == 1;
  738. return 0 if defined($implication) && $implication == 0;
  739. }
  740. # If o is an AND, we might have an implication, if we find an
  741. # implication within us for each predicate in o
  742. if ($o->isa('Dpkg::Deps::AND')) {
  743. my $subset = 1;
  744. foreach my $odep ($o->get_deps()) {
  745. my $found = 0;
  746. foreach my $dep ($self->get_deps()) {
  747. $found = 1 if $dep->implies($odep);
  748. }
  749. $subset = 0 if not $found;
  750. }
  751. return 1 if $subset;
  752. }
  753. return undef;
  754. }
  755. sub get_evaluation {
  756. my ($self, $facts) = @_;
  757. # Return 1 only if all members evaluates to true
  758. # Return 0 if at least one member evaluates to false
  759. # Return undef otherwise
  760. my $result = 1;
  761. foreach my $dep ($self->get_deps()) {
  762. my $eval = $dep->get_evaluation($facts);
  763. if (not defined $eval) {
  764. $result = undef;
  765. } elsif ($eval == 0) {
  766. $result = 0;
  767. last;
  768. } elsif ($eval == 1) {
  769. # Still possible
  770. }
  771. }
  772. return $result;
  773. }
  774. sub simplify_deps {
  775. my ($self, $facts, @knowndeps) = @_;
  776. my @new;
  777. WHILELOOP:
  778. while (@{$self->{list}}) {
  779. my $dep = shift @{$self->{list}};
  780. my $eval = $dep->get_evaluation($facts);
  781. next if defined($eval) and $eval == 1;
  782. foreach my $odep (@knowndeps, @new) {
  783. next WHILELOOP if $odep->implies($dep);
  784. }
  785. # When a dependency is implied by another dependency that
  786. # follows, then invert them
  787. # "a | b, c, a" becomes "a, c" and not "c, a"
  788. my $i = 0;
  789. foreach my $odep (@{$self->{list}}) {
  790. if (defined $odep and $odep->implies($dep)) {
  791. splice @{$self->{list}}, $i, 1;
  792. unshift @{$self->{list}}, $odep;
  793. next WHILELOOP;
  794. }
  795. $i++;
  796. }
  797. push @new, $dep;
  798. }
  799. $self->{list} = [ @new ];
  800. }
  801. package Dpkg::Deps::OR;
  802. =head2 Dpkg::Deps::OR
  803. This object represents a list of dependencies of which only one must be met
  804. for the dependency to be true.
  805. =over 4
  806. =item $or->output([$fh])
  807. The output method uses " | " to join the list of sub-dependencies.
  808. =back
  809. =cut
  810. use strict;
  811. use warnings;
  812. use base qw(Dpkg::Deps::Multiple);
  813. sub output {
  814. my ($self, $fh) = @_;
  815. my $res = join(" | ", map { $_->output() } grep { not $_->is_empty() } $self->get_deps());
  816. if (defined($fh)) {
  817. print $fh $res;
  818. }
  819. return $res;
  820. }
  821. sub implies {
  822. my ($self, $o) = @_;
  823. # Special case for AND with a single member, replace it by its member
  824. if ($o->isa('Dpkg::Deps::AND')) {
  825. my @subdeps = $o->get_deps();
  826. if (scalar(@subdeps) == 1) {
  827. $o = $subdeps[0];
  828. }
  829. }
  830. # In general, an OR dependency can't imply anything except if each
  831. # of its member implies a member in the other OR dependency
  832. if ($o->isa('Dpkg::Deps::OR')) {
  833. my $subset = 1;
  834. foreach my $dep ($self->get_deps()) {
  835. my $found = 0;
  836. foreach my $odep ($o->get_deps()) {
  837. $found = 1 if $dep->implies($odep);
  838. }
  839. $subset = 0 if not $found;
  840. }
  841. return 1 if $subset;
  842. }
  843. return undef;
  844. }
  845. sub get_evaluation {
  846. my ($self, $facts) = @_;
  847. # Returns false if all members evaluates to 0
  848. # Returns true if at least one member evaluates to true
  849. # Returns undef otherwise
  850. my $result = 0;
  851. foreach my $dep ($self->get_deps()) {
  852. my $eval = $dep->get_evaluation($facts);
  853. if (not defined $eval) {
  854. $result = undef;
  855. } elsif ($eval == 1) {
  856. $result = 1;
  857. last;
  858. } elsif ($eval == 0) {
  859. # Still possible to have a false evaluation
  860. }
  861. }
  862. return $result;
  863. }
  864. sub simplify_deps {
  865. my ($self, $facts) = @_;
  866. my @new;
  867. WHILELOOP:
  868. while (@{$self->{list}}) {
  869. my $dep = shift @{$self->{list}};
  870. my $eval = $dep->get_evaluation($facts);
  871. if (defined($eval) and $eval == 1) {
  872. $self->{list} = [];
  873. return;
  874. }
  875. foreach my $odep (@new, @{$self->{list}}) {
  876. next WHILELOOP if $odep->implies($dep);
  877. }
  878. push @new, $dep;
  879. }
  880. $self->{list} = [ @new ];
  881. }
  882. package Dpkg::Deps::Union;
  883. =head2 Dpkg::Deps::Union
  884. This object represents a list of relationships.
  885. =over 4
  886. =item $union->output([$fh])
  887. The output method uses ", " to join the list of relationships.
  888. =item $union->implies($other_dep)
  889. =item $union->get_evaluation($other_dep)
  890. Those methods are not meaningful for this object and always return undef.
  891. =item $union->simplify_deps($facts)
  892. The simplication is done to generate an union of all the relationships.
  893. It uses $simple_dep->merge_union($other_dep) to get its job done.
  894. =back
  895. =cut
  896. use strict;
  897. use warnings;
  898. use base qw(Dpkg::Deps::Multiple);
  899. sub output {
  900. my ($self, $fh) = @_;
  901. my $res = join(", ", map { $_->output() } grep { not $_->is_empty() } $self->get_deps());
  902. if (defined($fh)) {
  903. print $fh $res;
  904. }
  905. return $res;
  906. }
  907. sub implies {
  908. # Implication test are not useful on Union
  909. return undef;
  910. }
  911. sub get_evaluation {
  912. # Evaluation are not useful on Union
  913. return undef;
  914. }
  915. sub simplify_deps {
  916. my ($self, $facts) = @_;
  917. my @new;
  918. WHILELOOP:
  919. while (@{$self->{list}}) {
  920. my $odep = shift @{$self->{list}};
  921. foreach my $dep (@new) {
  922. next WHILELOOP if $dep->merge_union($odep);
  923. }
  924. push @new, $odep;
  925. }
  926. $self->{list} = [ @new ];
  927. }
  928. package Dpkg::Deps::KnownFacts;
  929. =head2 Dpkg::Deps::KnownFacts
  930. This object represents a list of installed packages and a list of virtual
  931. packages provided (by the set of installed packages).
  932. =over 4
  933. =item my $facts = Dpkg::Deps::KnownFacts->new();
  934. Creates a new object.
  935. =cut
  936. use strict;
  937. use warnings;
  938. use Dpkg::Version;
  939. sub new {
  940. my $this = shift;
  941. my $class = ref($this) || $this;
  942. my $self = { 'pkg' => {}, 'virtualpkg' => {} };
  943. bless $self, $class;
  944. return $self;
  945. }
  946. =item $facts->add_installed_package($package, $version, $arch, $multiarch)
  947. Records that the given version of the package is installed. If
  948. $version/$arch is undefined we know that the package is installed but we
  949. don't know which version/architecture it is. $multiarch is the Multi-Arch
  950. field of the package. If $multiarch is undef, it will be equivalent to
  951. "Multi-Arch: no".
  952. Note that $multiarch is only used if $arch is provided.
  953. =cut
  954. sub add_installed_package {
  955. my ($self, $pkg, $ver, $arch, $multiarch) = @_;
  956. my $p = {
  957. "package" => $pkg,
  958. "version" => $ver,
  959. "architecture" => $arch,
  960. "multi-arch" => $multiarch || "no",
  961. };
  962. $self->{pkg}{"$pkg:$arch"} = $p if defined $arch;
  963. push @{$self->{pkg}{$pkg}}, $p;
  964. }
  965. =item $facts->add_provided_package($virtual, $relation, $version, $by)
  966. Records that the "$by" package provides the $virtual package. $relation
  967. and $version correspond to the associated relation given in the Provides
  968. field. This might be used in the future for versioned provides.
  969. =cut
  970. sub add_provided_package {
  971. my ($self, $pkg, $rel, $ver, $by) = @_;
  972. if (not exists $self->{virtualpkg}{$pkg}) {
  973. $self->{virtualpkg}{$pkg} = [];
  974. }
  975. push @{$self->{virtualpkg}{$pkg}}, [ $by, $rel, $ver ];
  976. }
  977. =item my ($check, $param) = $facts->check_package($package)
  978. $check is one when the package is found. For a real package, $param
  979. contains the version. For a virtual package, $param contains an array
  980. reference containing the list of packages that provide it (each package is
  981. listed as [ $provider, $relation, $version ]).
  982. This function is obsolete and should not be used. Dpkg::Deps::KnownFacts
  983. is only meant to be filled with data and then passed to Dpkg::Deps
  984. methods where appropriate, but it should not be directly queried.
  985. =back
  986. =cut
  987. sub check_package {
  988. my ($self, $pkg) = @_;
  989. if (exists $self->{pkg}{$pkg}) {
  990. return (1, $self->{pkg}{$pkg}[0]{version});
  991. }
  992. if (exists $self->{virtualpkg}{$pkg}) {
  993. return (1, $self->{virtualpkg}{$pkg});
  994. }
  995. return (0, undef);
  996. }
  997. ## The functions below are private to Dpkg::Deps
  998. sub _find_package {
  999. my ($self, $dep, $lackinfos) = @_;
  1000. my ($pkg, $archqual) = ($dep->{package}, $dep->{archqual});
  1001. return undef if not exists $self->{pkg}{$pkg};
  1002. my $host_arch = $dep->{host_arch};
  1003. foreach my $p (@{$self->{pkg}{$pkg}}) {
  1004. my $a = $p->{"architecture"};
  1005. my $ma = $p->{"multi-arch"};
  1006. if (not defined $a) {
  1007. $$lackinfos = 1;
  1008. next;
  1009. }
  1010. return $p if $ma eq "foreign";
  1011. if (not defined $archqual) {
  1012. return $p if $a eq $host_arch or $a eq "all";
  1013. } elsif ($archqual eq "any") {
  1014. return $p if $ma eq "allowed";
  1015. return $p if $a eq $host_arch or $a eq "all";
  1016. }
  1017. }
  1018. return undef;
  1019. }
  1020. sub _find_virtual_packages {
  1021. my ($self, $pkg) = @_;
  1022. return () if not exists $self->{virtualpkg}{$pkg};
  1023. return @{$self->{virtualpkg}{$pkg}};
  1024. }
  1025. sub _evaluate_simple_dep {
  1026. my ($self, $dep) = @_;
  1027. my ($lackinfos, $pkg) = (0, $dep->{package});
  1028. my $p = $self->_find_package($dep, \$lackinfos);
  1029. if ($p) {
  1030. if (defined $dep->{relation}) {
  1031. if (defined $p->{version}) {
  1032. return 1 if version_compare_relation($p->{version},
  1033. $dep->{relation}, $dep->{version});
  1034. } else {
  1035. $lackinfos = 1;
  1036. }
  1037. } else {
  1038. return 1;
  1039. }
  1040. }
  1041. foreach my $virtpkg ($self->_find_virtual_packages($pkg)) {
  1042. # XXX: Adapt when versioned provides are allowed
  1043. next if defined $virtpkg->[1];
  1044. next if defined $dep->{relation}; # Provides don't satisfy versioned deps
  1045. return 1;
  1046. }
  1047. return undef if $lackinfos;
  1048. return 0;
  1049. }
  1050. =head1 CHANGES
  1051. =head2 Version 1.01
  1052. =over
  1053. =item * Add new $dep->reset() method that all dependency objects support.
  1054. =item * Dpkg::Deps::Simple now recognizes the arch qualifier "any" and
  1055. stores it in the "archqual" property when present.
  1056. =item * Dpkg::Deps::KnownFacts->add_installed_package() now accepts 2
  1057. supplementary parameters ($arch and $multiarch).
  1058. =item * Dpkg::Deps::KnownFacts->check_package() is obsolete, it should
  1059. not have been part of the public API.
  1060. =back
  1061. =cut
  1062. 1;