You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1004 lines
30 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: orderlist.cc,v 1.14 2001/05/07 05:49:43 jgg Exp $
  4. /* ######################################################################
  5. Order List - Represents and Manipulates an ordered list of packages.
  6. A list of packages can be ordered by a number of conflicting criteria
  7. each given a specific priority. Each package also has a set of flags
  8. indicating some useful things about it that are derived in the
  9. course of sorting. The pkgPackageManager class uses this class for
  10. all of it's installation ordering needs.
  11. This is a modified version of Manoj's Routine B. It consists of four
  12. independent ordering algorithms that can be applied at for different
  13. points in the ordering. By appling progressivly fewer ordering
  14. operations it is possible to give each consideration it's own
  15. priority and create an order that satisfies the lowest applicable
  16. consideration.
  17. The rules for unpacking ordering are:
  18. 1) Unpacking ignores Depends: on all packages
  19. 2) Unpacking requires Conflicts: on -ALL- packages to be satisfied
  20. 3) Unpacking requires PreDepends: on this package only to be satisfied
  21. 4) Removing requires that no packages depend on the package to be
  22. removed.
  23. And the rule for configuration ordering is:
  24. 1) Configuring requires that the Depends: of the package be satisfied
  25. Conflicts+PreDepends are ignored because unpacking says they are
  26. already correct [exageration, it does check but we need not be
  27. concerned]
  28. And some features that are valuable for unpacking ordering.
  29. f1) Unpacking a new package should advoid breaking dependencies of
  30. configured packages
  31. f2) Removal should not require a force, corrolory of f1
  32. f3) Unpacking should order by depends rather than fall back to random
  33. ordering.
  34. Each of the features can be enabled in the sorting routine at an
  35. arbitrary priority to give quite abit of control over the final unpacking
  36. order.
  37. The rules listed above may never be violated and are called Critical.
  38. When a critical rule is violated then a loop condition is recorded
  39. and will have to be delt with in the caller.
  40. The ordering keeps two lists, the main list and the 'After List'. The
  41. purpose of the after list is to allow packages to be delayed. This is done
  42. by setting the after flag on the package. Any package which requires this
  43. package to be ordered before will inherit the after flag and so on. This
  44. is used for CD swap ordering where all packages on a second CD have the
  45. after flag set. This forces them and all their dependents to be ordered
  46. toward the end.
  47. There are complications in this algorithm when presented with cycles.
  48. For all known practical cases it works, all cases where it doesn't work
  49. is fixable by tweaking the package descriptions. However, it should be
  50. possible to impove this further to make some better choices when
  51. presented with cycles.
  52. ##################################################################### */
  53. /*}}}*/
  54. // Include Files /*{{{*/
  55. #include <apt-pkg/orderlist.h>
  56. #include <apt-pkg/depcache.h>
  57. #include <apt-pkg/error.h>
  58. #include <apt-pkg/version.h>
  59. #include <apt-pkg/sptr.h>
  60. #include <apt-pkg/configuration.h>
  61. #include <iostream>
  62. /*}}}*/
  63. using namespace std;
  64. pkgOrderList *pkgOrderList::Me = 0;
  65. // OrderList::pkgOrderList - Constructor /*{{{*/
  66. // ---------------------------------------------------------------------
  67. /* */
  68. pkgOrderList::pkgOrderList(pkgDepCache *pCache) : Cache(*pCache)
  69. {
  70. FileList = 0;
  71. Primary = 0;
  72. Secondary = 0;
  73. RevDepends = 0;
  74. Remove = 0;
  75. LoopCount = -1;
  76. Debug = _config->FindB("Debug::pkgOrderList",false);
  77. /* Construct the arrays, egcs 1.0.1 bug requires the package count
  78. hack */
  79. unsigned long Size = Cache.Head().PackageCount;
  80. Flags = new unsigned short[Size];
  81. End = List = new Package *[Size];
  82. memset(Flags,0,sizeof(*Flags)*Size);
  83. }
  84. /*}}}*/
  85. // OrderList::~pkgOrderList - Destructor /*{{{*/
  86. // ---------------------------------------------------------------------
  87. /* */
  88. pkgOrderList::~pkgOrderList()
  89. {
  90. delete [] List;
  91. delete [] Flags;
  92. }
  93. /*}}}*/
  94. // OrderList::IsMissing - Check if a file is missing /*{{{*/
  95. // ---------------------------------------------------------------------
  96. /* */
  97. bool pkgOrderList::IsMissing(PkgIterator Pkg)
  98. {
  99. // Skip packages to erase
  100. if (Cache[Pkg].Delete() == true)
  101. return false;
  102. // Skip Packages that need configure only.
  103. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  104. Cache[Pkg].Keep() == true)
  105. return false;
  106. if (FileList == 0)
  107. return false;
  108. if (FileList[Pkg->ID].empty() == false)
  109. return false;
  110. return true;
  111. }
  112. /*}}}*/
  113. // OrderList::DoRun - Does an order run /*{{{*/
  114. // ---------------------------------------------------------------------
  115. /* The caller is expeted to have setup the desired probe state */
  116. bool pkgOrderList::DoRun()
  117. {
  118. // Temp list
  119. unsigned long Size = Cache.Head().PackageCount;
  120. SPtrArray<Package *> NList = new Package *[Size];
  121. SPtrArray<Package *> AfterList = new Package *[Size];
  122. AfterEnd = AfterList;
  123. Depth = 0;
  124. WipeFlags(Added | AddPending | Loop | InList);
  125. for (iterator I = List; I != End; I++)
  126. Flag(*I,InList);
  127. // Rebuild the main list into the temp list.
  128. iterator OldEnd = End;
  129. End = NList;
  130. for (iterator I = List; I != OldEnd; I++)
  131. if (VisitNode(PkgIterator(Cache,*I)) == false)
  132. {
  133. End = OldEnd;
  134. return false;
  135. }
  136. // Copy the after list to the end of the main list
  137. for (Package **I = AfterList; I != AfterEnd; I++)
  138. *End++ = *I;
  139. // Swap the main list to the new list
  140. delete [] List;
  141. List = NList.UnGuard();
  142. return true;
  143. }
  144. /*}}}*/
  145. // OrderList::OrderCritical - Perform critical unpacking ordering /*{{{*/
  146. // ---------------------------------------------------------------------
  147. /* This performs predepends and immediate configuration ordering only.
  148. This is termed critical unpacking ordering. Any loops that form are
  149. fatal and indicate that the packages cannot be installed. */
  150. bool pkgOrderList::OrderCritical()
  151. {
  152. FileList = 0;
  153. Primary = &pkgOrderList::DepUnPackPre;
  154. Secondary = 0;
  155. RevDepends = 0;
  156. Remove = 0;
  157. LoopCount = 0;
  158. // Sort
  159. Me = this;
  160. qsort(List,End - List,sizeof(*List),&OrderCompareB);
  161. if (DoRun() == false)
  162. return false;
  163. if (LoopCount != 0)
  164. return _error->Error("Fatal, predepends looping detected");
  165. return true;
  166. }
  167. /*}}}*/
  168. // OrderList::OrderUnpack - Perform complete unpacking ordering /*{{{*/
  169. // ---------------------------------------------------------------------
  170. /* This performs complete unpacking ordering and creates an order that is
  171. suitable for unpacking */
  172. bool pkgOrderList::OrderUnpack(string *FileList)
  173. {
  174. this->FileList = FileList;
  175. // Setup the after flags
  176. if (FileList != 0)
  177. {
  178. WipeFlags(After);
  179. // Set the inlist flag
  180. for (iterator I = List; I != End; I++)
  181. {
  182. PkgIterator P(Cache,*I);
  183. if (IsMissing(P) == true && IsNow(P) == true)
  184. Flag(*I,After);
  185. }
  186. }
  187. Primary = &pkgOrderList::DepUnPackCrit;
  188. Secondary = &pkgOrderList::DepConfigure;
  189. RevDepends = &pkgOrderList::DepUnPackDep;
  190. Remove = &pkgOrderList::DepRemove;
  191. LoopCount = -1;
  192. // Sort
  193. Me = this;
  194. qsort(List,End - List,sizeof(*List),&OrderCompareA);
  195. if (Debug == true)
  196. clog << "** Pass A" << endl;
  197. if (DoRun() == false)
  198. return false;
  199. if (Debug == true)
  200. clog << "** Pass B" << endl;
  201. Secondary = 0;
  202. if (DoRun() == false)
  203. return false;
  204. if (Debug == true)
  205. clog << "** Pass C" << endl;
  206. LoopCount = 0;
  207. RevDepends = 0;
  208. Remove = 0; // Otherwise the libreadline remove problem occures
  209. if (DoRun() == false)
  210. return false;
  211. if (Debug == true)
  212. clog << "** Pass D" << endl;
  213. LoopCount = 0;
  214. Primary = &pkgOrderList::DepUnPackPre;
  215. if (DoRun() == false)
  216. return false;
  217. if (Debug == true)
  218. {
  219. clog << "** Unpack ordering done" << endl;
  220. for (iterator I = List; I != End; I++)
  221. {
  222. PkgIterator P(Cache,*I);
  223. if (IsNow(P) == true)
  224. clog << P.Name() << ' ' << IsMissing(P) << ',' << IsFlag(P,After) << endl;
  225. }
  226. }
  227. return true;
  228. }
  229. /*}}}*/
  230. // OrderList::OrderConfigure - Perform configuration ordering /*{{{*/
  231. // ---------------------------------------------------------------------
  232. /* This orders by depends only and produces an order which is suitable
  233. for configuration */
  234. bool pkgOrderList::OrderConfigure()
  235. {
  236. FileList = 0;
  237. Primary = &pkgOrderList::DepConfigure;
  238. Secondary = 0;
  239. RevDepends = 0;
  240. Remove = 0;
  241. LoopCount = -1;
  242. return DoRun();
  243. }
  244. /*}}}*/
  245. // OrderList::Score - Score the package for sorting /*{{{*/
  246. // ---------------------------------------------------------------------
  247. /* Higher scores order earlier */
  248. int pkgOrderList::Score(PkgIterator Pkg)
  249. {
  250. // Removal is always done first
  251. if (Cache[Pkg].Delete() == true)
  252. return 200;
  253. // This should never happen..
  254. if (Cache[Pkg].InstVerIter(Cache).end() == true)
  255. return -1;
  256. int Score = 0;
  257. if ((Pkg->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  258. Score += 100;
  259. if (IsFlag(Pkg,Immediate) == true)
  260. Score += 10;
  261. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
  262. D.end() == false; D++)
  263. if (D->Type == pkgCache::Dep::PreDepends)
  264. {
  265. Score += 50;
  266. break;
  267. }
  268. // Important Required Standard Optional Extra
  269. signed short PrioMap[] = {0,5,4,3,1,0};
  270. if (Cache[Pkg].InstVerIter(Cache)->Priority <= 5)
  271. Score += PrioMap[Cache[Pkg].InstVerIter(Cache)->Priority];
  272. return Score;
  273. }
  274. /*}}}*/
  275. // OrderList::FileCmp - Compare by package file /*{{{*/
  276. // ---------------------------------------------------------------------
  277. /* This compares by the package file that the install version is in. */
  278. int pkgOrderList::FileCmp(PkgIterator A,PkgIterator B)
  279. {
  280. if (Cache[A].Delete() == true && Cache[B].Delete() == true)
  281. return 0;
  282. if (Cache[A].Delete() == true)
  283. return -1;
  284. if (Cache[B].Delete() == true)
  285. return 1;
  286. if (Cache[A].InstVerIter(Cache).FileList().end() == true)
  287. return -1;
  288. if (Cache[B].InstVerIter(Cache).FileList().end() == true)
  289. return 1;
  290. pkgCache::PackageFile *FA = Cache[A].InstVerIter(Cache).FileList().File();
  291. pkgCache::PackageFile *FB = Cache[B].InstVerIter(Cache).FileList().File();
  292. if (FA < FB)
  293. return -1;
  294. if (FA > FB)
  295. return 1;
  296. return 0;
  297. }
  298. /*}}}*/
  299. // BoolCompare - Comparison function for two booleans /*{{{*/
  300. // ---------------------------------------------------------------------
  301. /* */
  302. static int BoolCompare(bool A,bool B)
  303. {
  304. if (A == B)
  305. return 0;
  306. if (A == false)
  307. return -1;
  308. return 1;
  309. }
  310. /*}}}*/
  311. // OrderList::OrderCompareA - Order the installation by op /*{{{*/
  312. // ---------------------------------------------------------------------
  313. /* This provides a first-pass sort of the list and gives a decent starting
  314. point for further complete ordering. It is used by OrderUnpack only */
  315. int pkgOrderList::OrderCompareA(const void *a, const void *b)
  316. {
  317. PkgIterator A(Me->Cache,*(Package **)a);
  318. PkgIterator B(Me->Cache,*(Package **)b);
  319. // We order packages with a set state toward the front
  320. int Res;
  321. if ((Res = BoolCompare(Me->IsNow(A),Me->IsNow(B))) != 0)
  322. return -1*Res;
  323. // We order missing files to toward the end
  324. /* if (Me->FileList != 0)
  325. {
  326. if ((Res = BoolCompare(Me->IsMissing(A),
  327. Me->IsMissing(B))) != 0)
  328. return Res;
  329. }*/
  330. if (A.State() != pkgCache::PkgIterator::NeedsNothing &&
  331. B.State() == pkgCache::PkgIterator::NeedsNothing)
  332. return -1;
  333. if (A.State() == pkgCache::PkgIterator::NeedsNothing &&
  334. B.State() != pkgCache::PkgIterator::NeedsNothing)
  335. return 1;
  336. int ScoreA = Me->Score(A);
  337. int ScoreB = Me->Score(B);
  338. if (ScoreA > ScoreB)
  339. return -1;
  340. if (ScoreA < ScoreB)
  341. return 1;
  342. return strcmp(A.Name(),B.Name());
  343. }
  344. /*}}}*/
  345. // OrderList::OrderCompareB - Order the installation by source /*{{{*/
  346. // ---------------------------------------------------------------------
  347. /* This orders by installation source. This is useful to handle
  348. inter-source breaks */
  349. int pkgOrderList::OrderCompareB(const void *a, const void *b)
  350. {
  351. PkgIterator A(Me->Cache,*(Package **)a);
  352. PkgIterator B(Me->Cache,*(Package **)b);
  353. if (A.State() != pkgCache::PkgIterator::NeedsNothing &&
  354. B.State() == pkgCache::PkgIterator::NeedsNothing)
  355. return -1;
  356. if (A.State() == pkgCache::PkgIterator::NeedsNothing &&
  357. B.State() != pkgCache::PkgIterator::NeedsNothing)
  358. return 1;
  359. int F = Me->FileCmp(A,B);
  360. if (F != 0)
  361. {
  362. if (F > 0)
  363. return -1;
  364. return 1;
  365. }
  366. int ScoreA = Me->Score(A);
  367. int ScoreB = Me->Score(B);
  368. if (ScoreA > ScoreB)
  369. return -1;
  370. if (ScoreA < ScoreB)
  371. return 1;
  372. return strcmp(A.Name(),B.Name());
  373. }
  374. /*}}}*/
  375. // OrderList::VisitDeps - Visit forward install dependencies /*{{{*/
  376. // ---------------------------------------------------------------------
  377. /* This calls the dependency function for the normal forwards dependencies
  378. of the package */
  379. bool pkgOrderList::VisitDeps(DepFunc F,PkgIterator Pkg)
  380. {
  381. if (F == 0 || Pkg.end() == true || Cache[Pkg].InstallVer == 0)
  382. return true;
  383. return (this->*F)(Cache[Pkg].InstVerIter(Cache).DependsList());
  384. }
  385. /*}}}*/
  386. // OrderList::VisitRDeps - Visit reverse dependencies /*{{{*/
  387. // ---------------------------------------------------------------------
  388. /* This calls the dependency function for all of the normal reverse depends
  389. of the package */
  390. bool pkgOrderList::VisitRDeps(DepFunc F,PkgIterator Pkg)
  391. {
  392. if (F == 0 || Pkg.end() == true)
  393. return true;
  394. return (this->*F)(Pkg.RevDependsList());
  395. }
  396. /*}}}*/
  397. // OrderList::VisitRProvides - Visit provides reverse dependencies /*{{{*/
  398. // ---------------------------------------------------------------------
  399. /* This calls the dependency function for all reverse dependencies
  400. generated by the provides line on the package. */
  401. bool pkgOrderList::VisitRProvides(DepFunc F,VerIterator Ver)
  402. {
  403. if (F == 0 || Ver.end() == true)
  404. return true;
  405. bool Res = true;
  406. for (PrvIterator P = Ver.ProvidesList(); P.end() == false; P++)
  407. Res &= (this->*F)(P.ParentPkg().RevDependsList());
  408. return true;
  409. }
  410. /*}}}*/
  411. // OrderList::VisitProvides - Visit all of the providing packages /*{{{*/
  412. // ---------------------------------------------------------------------
  413. /* This routine calls visit on all providing packages. */
  414. bool pkgOrderList::VisitProvides(DepIterator D,bool Critical)
  415. {
  416. SPtrArray<Version *> List = D.AllTargets();
  417. for (Version **I = List; *I != 0; I++)
  418. {
  419. VerIterator Ver(Cache,*I);
  420. PkgIterator Pkg = Ver.ParentPkg();
  421. if (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing)
  422. continue;
  423. if (D->Type != pkgCache::Dep::Conflicts &&
  424. D->Type != pkgCache::Dep::DpkgBreaks &&
  425. D->Type != pkgCache::Dep::Obsoletes &&
  426. Cache[Pkg].InstallVer != *I)
  427. continue;
  428. if ((D->Type == pkgCache::Dep::Conflicts ||
  429. D->Type == pkgCache::Dep::DpkgBreaks ||
  430. D->Type == pkgCache::Dep::Obsoletes) &&
  431. (Version *)Pkg.CurrentVer() != *I)
  432. continue;
  433. // Skip over missing files
  434. if (Critical == false && IsMissing(D.ParentPkg()) == true)
  435. continue;
  436. if (VisitNode(Pkg) == false)
  437. return false;
  438. }
  439. return true;
  440. }
  441. /*}}}*/
  442. // OrderList::VisitNode - Recursive ordering director /*{{{*/
  443. // ---------------------------------------------------------------------
  444. /* This is the core ordering routine. It calls the set dependency
  445. consideration functions which then potentialy call this again. Finite
  446. depth is achived through the colouring mechinism. */
  447. bool pkgOrderList::VisitNode(PkgIterator Pkg)
  448. {
  449. // Looping or irrelevent.
  450. // This should probably trancend not installed packages
  451. if (Pkg.end() == true || IsFlag(Pkg,Added) == true ||
  452. IsFlag(Pkg,AddPending) == true || IsFlag(Pkg,InList) == false)
  453. return true;
  454. if (Debug == true)
  455. {
  456. for (int j = 0; j != Depth; j++) clog << ' ';
  457. clog << "Visit " << Pkg.Name() << endl;
  458. }
  459. Depth++;
  460. // Color grey
  461. Flag(Pkg,AddPending);
  462. DepFunc Old = Primary;
  463. // Perform immedate configuration of the package if so flagged.
  464. if (IsFlag(Pkg,Immediate) == true && Primary != &pkgOrderList::DepUnPackPre)
  465. Primary = &pkgOrderList::DepUnPackPreD;
  466. if (IsNow(Pkg) == true)
  467. {
  468. bool Res = true;
  469. if (Cache[Pkg].Delete() == false)
  470. {
  471. // Primary
  472. Res &= Res && VisitDeps(Primary,Pkg);
  473. Res &= Res && VisitRDeps(Primary,Pkg);
  474. Res &= Res && VisitRProvides(Primary,Pkg.CurrentVer());
  475. Res &= Res && VisitRProvides(Primary,Cache[Pkg].InstVerIter(Cache));
  476. // RevDep
  477. Res &= Res && VisitRDeps(RevDepends,Pkg);
  478. Res &= Res && VisitRProvides(RevDepends,Pkg.CurrentVer());
  479. Res &= Res && VisitRProvides(RevDepends,Cache[Pkg].InstVerIter(Cache));
  480. // Secondary
  481. Res &= Res && VisitDeps(Secondary,Pkg);
  482. Res &= Res && VisitRDeps(Secondary,Pkg);
  483. Res &= Res && VisitRProvides(Secondary,Pkg.CurrentVer());
  484. Res &= Res && VisitRProvides(Secondary,Cache[Pkg].InstVerIter(Cache));
  485. }
  486. else
  487. {
  488. // RevDep
  489. Res &= Res && VisitRDeps(Remove,Pkg);
  490. Res &= Res && VisitRProvides(Remove,Pkg.CurrentVer());
  491. }
  492. }
  493. if (IsFlag(Pkg,Added) == false)
  494. {
  495. Flag(Pkg,Added,Added | AddPending);
  496. if (IsFlag(Pkg,After) == true)
  497. *AfterEnd++ = Pkg;
  498. else
  499. *End++ = Pkg;
  500. }
  501. Primary = Old;
  502. Depth--;
  503. if (Debug == true)
  504. {
  505. for (int j = 0; j != Depth; j++) clog << ' ';
  506. clog << "Leave " << Pkg.Name() << ' ' << IsFlag(Pkg,Added) << ',' << IsFlag(Pkg,AddPending) << endl;
  507. }
  508. return true;
  509. }
  510. /*}}}*/
  511. // OrderList::DepUnPackCrit - Critical UnPacking ordering /*{{{*/
  512. // ---------------------------------------------------------------------
  513. /* Critical unpacking ordering strives to satisfy Conflicts: and
  514. PreDepends: only. When a prdepends is encountered the Primary
  515. DepFunc is changed to be DepUnPackPreD.
  516. Loops are preprocessed and logged. */
  517. bool pkgOrderList::DepUnPackCrit(DepIterator D)
  518. {
  519. for (; D.end() == false; D++)
  520. {
  521. if (D.Reverse() == true)
  522. {
  523. /* Reverse depenanices are only interested in conflicts,
  524. predepend breakage is ignored here */
  525. if (D->Type != pkgCache::Dep::Conflicts &&
  526. D->Type != pkgCache::Dep::Obsoletes)
  527. continue;
  528. // Duplication elimination, consider only the current version
  529. if (D.ParentPkg().CurrentVer() != D.ParentVer())
  530. continue;
  531. /* For reverse dependencies we wish to check if the
  532. dependency is satisifed in the install state. The
  533. target package (caller) is going to be in the installed
  534. state. */
  535. if (CheckDep(D) == true)
  536. continue;
  537. if (VisitNode(D.ParentPkg()) == false)
  538. return false;
  539. }
  540. else
  541. {
  542. /* Forward critical dependencies MUST be correct before the
  543. package can be unpacked. */
  544. if (D->Type != pkgCache::Dep::Conflicts &&
  545. D->Type != pkgCache::Dep::DpkgBreaks &&
  546. D->Type != pkgCache::Dep::Obsoletes &&
  547. D->Type != pkgCache::Dep::PreDepends)
  548. continue;
  549. /* We wish to check if the dep is okay in the now state of the
  550. target package against the install state of this package. */
  551. if (CheckDep(D) == true)
  552. {
  553. /* We want to catch predepends loops with the code below.
  554. Conflicts loops that are Dep OK are ignored */
  555. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  556. D->Type != pkgCache::Dep::PreDepends)
  557. continue;
  558. }
  559. // This is the loop detection
  560. if (IsFlag(D.TargetPkg(),Added) == true ||
  561. IsFlag(D.TargetPkg(),AddPending) == true)
  562. {
  563. if (IsFlag(D.TargetPkg(),AddPending) == true)
  564. AddLoop(D);
  565. continue;
  566. }
  567. /* Predepends require a special ordering stage, they must have
  568. all dependents installed as well */
  569. DepFunc Old = Primary;
  570. bool Res = false;
  571. if (D->Type == pkgCache::Dep::PreDepends)
  572. Primary = &pkgOrderList::DepUnPackPreD;
  573. Res = VisitProvides(D,true);
  574. Primary = Old;
  575. if (Res == false)
  576. return false;
  577. }
  578. }
  579. return true;
  580. }
  581. // OrderList::DepUnPackPreD - Critical UnPacking ordering with depends /*{{{*/
  582. // ---------------------------------------------------------------------
  583. /* Critical PreDepends (also configure immediate and essential) strives to
  584. ensure not only that all conflicts+predepends are met but that this
  585. package will be immediately configurable when it is unpacked.
  586. Loops are preprocessed and logged. */
  587. bool pkgOrderList::DepUnPackPreD(DepIterator D)
  588. {
  589. if (D.Reverse() == true)
  590. return DepUnPackCrit(D);
  591. for (; D.end() == false; D++)
  592. {
  593. if (D.IsCritical() == false)
  594. continue;
  595. /* We wish to check if the dep is okay in the now state of the
  596. target package against the install state of this package. */
  597. if (CheckDep(D) == true)
  598. {
  599. /* We want to catch predepends loops with the code below.
  600. Conflicts loops that are Dep OK are ignored */
  601. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  602. D->Type != pkgCache::Dep::PreDepends)
  603. continue;
  604. }
  605. // This is the loop detection
  606. if (IsFlag(D.TargetPkg(),Added) == true ||
  607. IsFlag(D.TargetPkg(),AddPending) == true)
  608. {
  609. if (IsFlag(D.TargetPkg(),AddPending) == true)
  610. AddLoop(D);
  611. continue;
  612. }
  613. if (VisitProvides(D,true) == false)
  614. return false;
  615. }
  616. return true;
  617. }
  618. /*}}}*/
  619. // OrderList::DepUnPackPre - Critical Predepends ordering /*{{{*/
  620. // ---------------------------------------------------------------------
  621. /* Critical PreDepends (also configure immediate and essential) strives to
  622. ensure not only that all conflicts+predepends are met but that this
  623. package will be immediately configurable when it is unpacked.
  624. Loops are preprocessed and logged. All loops will be fatal. */
  625. bool pkgOrderList::DepUnPackPre(DepIterator D)
  626. {
  627. if (D.Reverse() == true)
  628. return true;
  629. for (; D.end() == false; D++)
  630. {
  631. /* Only consider the PreDepends or Depends. Depends are only
  632. considered at the lowest depth or in the case of immediate
  633. configure */
  634. if (D->Type != pkgCache::Dep::PreDepends)
  635. {
  636. if (D->Type == pkgCache::Dep::Depends)
  637. {
  638. if (Depth == 1 && IsFlag(D.ParentPkg(),Immediate) == false)
  639. continue;
  640. }
  641. else
  642. continue;
  643. }
  644. /* We wish to check if the dep is okay in the now state of the
  645. target package against the install state of this package. */
  646. if (CheckDep(D) == true)
  647. {
  648. /* We want to catch predepends loops with the code below.
  649. Conflicts loops that are Dep OK are ignored */
  650. if (IsFlag(D.TargetPkg(),AddPending) == false)
  651. continue;
  652. }
  653. // This is the loop detection
  654. if (IsFlag(D.TargetPkg(),Added) == true ||
  655. IsFlag(D.TargetPkg(),AddPending) == true)
  656. {
  657. if (IsFlag(D.TargetPkg(),AddPending) == true)
  658. AddLoop(D);
  659. continue;
  660. }
  661. if (VisitProvides(D,true) == false)
  662. return false;
  663. }
  664. return true;
  665. }
  666. /*}}}*/
  667. // OrderList::DepUnPackDep - Reverse dependency considerations /*{{{*/
  668. // ---------------------------------------------------------------------
  669. /* Reverse dependencies are considered to determine if unpacking this
  670. package will break any existing dependencies. If so then those
  671. packages are ordered before this one so that they are in the
  672. UnPacked state.
  673. The forwards depends loop is designed to bring the packages dependents
  674. close to the package. This helps reduce deconfigure time.
  675. Loops are irrelevent to this. */
  676. bool pkgOrderList::DepUnPackDep(DepIterator D)
  677. {
  678. for (; D.end() == false; D++)
  679. if (D.IsCritical() == true)
  680. {
  681. if (D.Reverse() == true)
  682. {
  683. /* Duplication prevention. We consider rev deps only on
  684. the current version, a not installed package
  685. cannot break */
  686. if (D.ParentPkg()->CurrentVer == 0 ||
  687. D.ParentPkg().CurrentVer() != D.ParentVer())
  688. continue;
  689. // The dep will not break so it is irrelevent.
  690. if (CheckDep(D) == true)
  691. continue;
  692. // Skip over missing files
  693. if (IsMissing(D.ParentPkg()) == true)
  694. continue;
  695. if (VisitNode(D.ParentPkg()) == false)
  696. return false;
  697. }
  698. else
  699. {
  700. if (D->Type == pkgCache::Dep::Depends)
  701. if (VisitProvides(D,false) == false)
  702. return false;
  703. if (D->Type == pkgCache::Dep::DpkgBreaks)
  704. {
  705. if (CheckDep(D) == true)
  706. continue;
  707. if (VisitNode(D.TargetPkg()) == false)
  708. return false;
  709. }
  710. }
  711. }
  712. return true;
  713. }
  714. /*}}}*/
  715. // OrderList::DepConfigure - Configuration ordering /*{{{*/
  716. // ---------------------------------------------------------------------
  717. /* Configuration only ordering orders by the Depends: line only. It
  718. orders configuration so that when a package comes to be configured it's
  719. dependents are configured.
  720. Loops are ingored. Depends loop entry points are chaotic. */
  721. bool pkgOrderList::DepConfigure(DepIterator D)
  722. {
  723. // Never consider reverse configuration dependencies.
  724. if (D.Reverse() == true)
  725. return true;
  726. for (; D.end() == false; D++)
  727. if (D->Type == pkgCache::Dep::Depends)
  728. if (VisitProvides(D,false) == false)
  729. return false;
  730. return true;
  731. }
  732. /*}}}*/
  733. // OrderList::DepRemove - Removal ordering /*{{{*/
  734. // ---------------------------------------------------------------------
  735. /* Removal visits all reverse depends. It considers if the dependency
  736. of the Now state version to see if it is okay with removing this
  737. package. This check should always fail, but is provided for symetery
  738. with the other critical handlers.
  739. Loops are preprocessed and logged. Removal loops can also be
  740. detected in the critical handler. They are characterized by an
  741. old version of A depending on B but the new version of A conflicting
  742. with B, thus either A or B must break to install. */
  743. bool pkgOrderList::DepRemove(DepIterator D)
  744. {
  745. if (D.Reverse() == false)
  746. return true;
  747. for (; D.end() == false; D++)
  748. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  749. {
  750. // Duplication elimination, consider the current version only
  751. if (D.ParentPkg().CurrentVer() != D.ParentVer())
  752. continue;
  753. /* We wish to see if the dep on the parent package is okay
  754. in the removed (install) state of the target pkg. */
  755. if (CheckDep(D) == true)
  756. {
  757. // We want to catch loops with the code below.
  758. if (IsFlag(D.ParentPkg(),AddPending) == false)
  759. continue;
  760. }
  761. // This is the loop detection
  762. if (IsFlag(D.ParentPkg(),Added) == true ||
  763. IsFlag(D.ParentPkg(),AddPending) == true)
  764. {
  765. if (IsFlag(D.ParentPkg(),AddPending) == true)
  766. AddLoop(D);
  767. continue;
  768. }
  769. // Skip over missing files
  770. if (IsMissing(D.ParentPkg()) == true)
  771. continue;
  772. if (VisitNode(D.ParentPkg()) == false)
  773. return false;
  774. }
  775. return true;
  776. }
  777. /*}}}*/
  778. // OrderList::AddLoop - Add a loop to the loop list /*{{{*/
  779. // ---------------------------------------------------------------------
  780. /* We record the loops. This is a relic since loop breaking is done
  781. genericaly as part of the safety routines. */
  782. bool pkgOrderList::AddLoop(DepIterator D)
  783. {
  784. if (LoopCount < 0 || LoopCount >= 20)
  785. return false;
  786. // Skip dups
  787. if (LoopCount != 0)
  788. {
  789. if (Loops[LoopCount - 1].ParentPkg() == D.ParentPkg() ||
  790. Loops[LoopCount - 1].TargetPkg() == D.ParentPkg())
  791. return true;
  792. }
  793. Loops[LoopCount++] = D;
  794. // Mark the packages as being part of a loop.
  795. Flag(D.TargetPkg(),Loop);
  796. Flag(D.ParentPkg(),Loop);
  797. return true;
  798. }
  799. /*}}}*/
  800. // OrderList::WipeFlags - Unset the given flags from all packages /*{{{*/
  801. // ---------------------------------------------------------------------
  802. /* */
  803. void pkgOrderList::WipeFlags(unsigned long F)
  804. {
  805. unsigned long Size = Cache.Head().PackageCount;
  806. for (unsigned long I = 0; I != Size; I++)
  807. Flags[I] &= ~F;
  808. }
  809. /*}}}*/
  810. // OrderList::CheckDep - Check a dependency for truth /*{{{*/
  811. // ---------------------------------------------------------------------
  812. /* This performs a complete analysis of the dependency wrt to the
  813. current add list. It returns true if after all events are
  814. performed it is still true. This sort of routine can be approximated
  815. by examining the DepCache, however in convoluted cases of provides
  816. this fails to produce a suitable result. */
  817. bool pkgOrderList::CheckDep(DepIterator D)
  818. {
  819. SPtrArray<Version *> List = D.AllTargets();
  820. bool Hit = false;
  821. for (Version **I = List; *I != 0; I++)
  822. {
  823. VerIterator Ver(Cache,*I);
  824. PkgIterator Pkg = Ver.ParentPkg();
  825. /* The meaning of Added and AddPending is subtle. AddPending is
  826. an indication that the package is looping. Because of the
  827. way ordering works Added means the package will be unpacked
  828. before this one and AddPending means after. It is therefore
  829. correct to ignore AddPending in all cases, but that exposes
  830. reverse-ordering loops which should be ignored. */
  831. if (IsFlag(Pkg,Added) == true ||
  832. (IsFlag(Pkg,AddPending) == true && D.Reverse() == true))
  833. {
  834. if (Cache[Pkg].InstallVer != *I)
  835. continue;
  836. }
  837. else
  838. if ((Version *)Pkg.CurrentVer() != *I ||
  839. Pkg.State() != PkgIterator::NeedsNothing)
  840. continue;
  841. /* Conflicts requires that all versions are not present, depends
  842. just needs one */
  843. if (D->Type != pkgCache::Dep::Conflicts &&
  844. D->Type != pkgCache::Dep::DpkgBreaks &&
  845. D->Type != pkgCache::Dep::Obsoletes)
  846. {
  847. /* Try to find something that does not have the after flag set
  848. if at all possible */
  849. if (IsFlag(Pkg,After) == true)
  850. {
  851. Hit = true;
  852. continue;
  853. }
  854. return true;
  855. }
  856. else
  857. {
  858. if (IsFlag(Pkg,After) == true)
  859. Flag(D.ParentPkg(),After);
  860. return false;
  861. }
  862. }
  863. // We found a hit, but it had the after flag set
  864. if (Hit == true && D->Type == pkgCache::Dep::PreDepends)
  865. {
  866. Flag(D.ParentPkg(),After);
  867. return true;
  868. }
  869. /* Conflicts requires that all versions are not present, depends
  870. just needs one */
  871. if (D->Type == pkgCache::Dep::Conflicts ||
  872. D->Type == pkgCache::Dep::Obsoletes)
  873. return true;
  874. return false;
  875. }
  876. /*}}}*/