Deps.pm 36 KB

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