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.
 
 
 
 
 
 

1102 lines
34 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. Pkg.State() == pkgCache::PkgIterator::NeedsNothing) &&
  105. Cache[Pkg].Keep() == true)
  106. return false;
  107. if (FileList == 0)
  108. return false;
  109. if (FileList[Pkg->ID].empty() == false)
  110. return false;
  111. // Missing Pseudo packages are missing if the real package is missing
  112. if (pkgCache::VerIterator(Cache, Cache[Pkg].CandidateVer).Pseudo() == true)
  113. return IsMissing(Pkg.Group().FindPkg("all"));
  114. return true;
  115. }
  116. /*}}}*/
  117. // OrderList::DoRun - Does an order run /*{{{*/
  118. // ---------------------------------------------------------------------
  119. /* The caller is expeted to have setup the desired probe state */
  120. bool pkgOrderList::DoRun()
  121. {
  122. // Temp list
  123. unsigned long Size = Cache.Head().PackageCount;
  124. SPtrArray<Package *> NList = new Package *[Size];
  125. SPtrArray<Package *> AfterList = new Package *[Size];
  126. AfterEnd = AfterList;
  127. Depth = 0;
  128. WipeFlags(Added | AddPending | Loop | InList);
  129. for (iterator I = List; I != End; I++)
  130. Flag(*I,InList);
  131. // Rebuild the main list into the temp list.
  132. iterator OldEnd = End;
  133. End = NList;
  134. for (iterator I = List; I != OldEnd; I++)
  135. if (VisitNode(PkgIterator(Cache,*I)) == false)
  136. {
  137. End = OldEnd;
  138. return false;
  139. }
  140. // Copy the after list to the end of the main list
  141. for (Package **I = AfterList; I != AfterEnd; I++)
  142. *End++ = *I;
  143. // Swap the main list to the new list
  144. delete [] List;
  145. List = NList.UnGuard();
  146. return true;
  147. }
  148. /*}}}*/
  149. // OrderList::OrderCritical - Perform critical unpacking ordering /*{{{*/
  150. // ---------------------------------------------------------------------
  151. /* This performs predepends and immediate configuration ordering only.
  152. This is termed critical unpacking ordering. Any loops that form are
  153. fatal and indicate that the packages cannot be installed. */
  154. bool pkgOrderList::OrderCritical()
  155. {
  156. FileList = 0;
  157. Primary = &pkgOrderList::DepUnPackPreD;
  158. Secondary = 0;
  159. RevDepends = 0;
  160. Remove = 0;
  161. LoopCount = 0;
  162. // Sort
  163. Me = this;
  164. qsort(List,End - List,sizeof(*List),&OrderCompareB);
  165. if (DoRun() == false)
  166. return false;
  167. if (LoopCount != 0)
  168. return _error->Error("Fatal, predepends looping detected");
  169. if (Debug == true)
  170. {
  171. clog << "** Critical Unpack ordering done" << endl;
  172. for (iterator I = List; I != End; I++)
  173. {
  174. PkgIterator P(Cache,*I);
  175. if (IsNow(P) == true)
  176. clog << " " << P.FullName() << ' ' << IsMissing(P) << ',' << IsFlag(P,After) << endl;
  177. }
  178. }
  179. return true;
  180. }
  181. /*}}}*/
  182. // OrderList::OrderUnpack - Perform complete unpacking ordering /*{{{*/
  183. // ---------------------------------------------------------------------
  184. /* This performs complete unpacking ordering and creates an order that is
  185. suitable for unpacking */
  186. bool pkgOrderList::OrderUnpack(string *FileList)
  187. {
  188. this->FileList = FileList;
  189. // Setup the after flags
  190. if (FileList != 0)
  191. {
  192. WipeFlags(After);
  193. // Set the inlist flag
  194. for (iterator I = List; I != End; I++)
  195. {
  196. PkgIterator P(Cache,*I);
  197. if (IsMissing(P) == true && IsNow(P) == true)
  198. Flag(*I,After);
  199. }
  200. }
  201. Primary = &pkgOrderList::DepUnPackCrit;
  202. Secondary = &pkgOrderList::DepConfigure;
  203. RevDepends = &pkgOrderList::DepUnPackDep;
  204. Remove = &pkgOrderList::DepRemove;
  205. LoopCount = -1;
  206. // Sort
  207. Me = this;
  208. qsort(List,End - List,sizeof(*List),&OrderCompareA);
  209. if (Debug == true)
  210. clog << "** Pass A" << endl;
  211. if (DoRun() == false)
  212. return false;
  213. if (Debug == true)
  214. clog << "** Pass B" << endl;
  215. Secondary = 0;
  216. if (DoRun() == false)
  217. return false;
  218. if (Debug == true)
  219. clog << "** Pass C" << endl;
  220. LoopCount = 0;
  221. RevDepends = 0;
  222. Remove = 0; // Otherwise the libreadline remove problem occures
  223. if (DoRun() == false)
  224. return false;
  225. if (Debug == true)
  226. clog << "** Pass D" << endl;
  227. LoopCount = 0;
  228. Primary = &pkgOrderList::DepUnPackPre;
  229. if (DoRun() == false)
  230. return false;
  231. if (Debug == true)
  232. {
  233. clog << "** Unpack ordering done" << endl;
  234. for (iterator I = List; I != End; I++)
  235. {
  236. PkgIterator P(Cache,*I);
  237. if (IsNow(P) == true)
  238. clog << " " << P.FullName() << ' ' << IsMissing(P) << ',' << IsFlag(P,After) << endl;
  239. }
  240. }
  241. return true;
  242. }
  243. /*}}}*/
  244. // OrderList::OrderConfigure - Perform configuration ordering /*{{{*/
  245. // ---------------------------------------------------------------------
  246. /* This orders by depends only and produces an order which is suitable
  247. for configuration */
  248. bool pkgOrderList::OrderConfigure()
  249. {
  250. FileList = 0;
  251. Primary = &pkgOrderList::DepConfigure;
  252. Secondary = 0;
  253. RevDepends = 0;
  254. Remove = 0;
  255. LoopCount = -1;
  256. return DoRun();
  257. }
  258. /*}}}*/
  259. // OrderList::Score - Score the package for sorting /*{{{*/
  260. // ---------------------------------------------------------------------
  261. /* Higher scores order earlier */
  262. int pkgOrderList::Score(PkgIterator Pkg)
  263. {
  264. static int const ScoreDelete = _config->FindI("OrderList::Score::Delete", 500);
  265. // Removal is always done first
  266. if (Cache[Pkg].Delete() == true)
  267. return ScoreDelete;
  268. // This should never happen..
  269. if (Cache[Pkg].InstVerIter(Cache).end() == true)
  270. return -1;
  271. static int const ScoreEssential = _config->FindI("OrderList::Score::Essential", 200);
  272. static int const ScoreImmediate = _config->FindI("OrderList::Score::Immediate", 10);
  273. static int const ScorePreDepends = _config->FindI("OrderList::Score::PreDepends", 50);
  274. int Score = 0;
  275. if ((Pkg->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  276. Score += ScoreEssential;
  277. if (IsFlag(Pkg,Immediate) == true)
  278. Score += ScoreImmediate;
  279. for (DepIterator D = Cache[Pkg].InstVerIter(Cache).DependsList();
  280. D.end() == false; D++)
  281. if (D->Type == pkgCache::Dep::PreDepends)
  282. {
  283. Score += ScorePreDepends;
  284. break;
  285. }
  286. // Important Required Standard Optional Extra
  287. signed short PrioMap[] = {0,5,4,3,1,0};
  288. if (Cache[Pkg].InstVerIter(Cache)->Priority <= 5)
  289. Score += PrioMap[Cache[Pkg].InstVerIter(Cache)->Priority];
  290. return Score;
  291. }
  292. /*}}}*/
  293. // OrderList::FileCmp - Compare by package file /*{{{*/
  294. // ---------------------------------------------------------------------
  295. /* This compares by the package file that the install version is in. */
  296. int pkgOrderList::FileCmp(PkgIterator A,PkgIterator B)
  297. {
  298. if (Cache[A].Delete() == true && Cache[B].Delete() == true)
  299. return 0;
  300. if (Cache[A].Delete() == true)
  301. return -1;
  302. if (Cache[B].Delete() == true)
  303. return 1;
  304. if (Cache[A].InstVerIter(Cache).FileList().end() == true)
  305. return -1;
  306. if (Cache[B].InstVerIter(Cache).FileList().end() == true)
  307. return 1;
  308. pkgCache::PackageFile *FA = Cache[A].InstVerIter(Cache).FileList().File();
  309. pkgCache::PackageFile *FB = Cache[B].InstVerIter(Cache).FileList().File();
  310. if (FA < FB)
  311. return -1;
  312. if (FA > FB)
  313. return 1;
  314. return 0;
  315. }
  316. /*}}}*/
  317. // BoolCompare - Comparison function for two booleans /*{{{*/
  318. // ---------------------------------------------------------------------
  319. /* */
  320. static int BoolCompare(bool A,bool B)
  321. {
  322. if (A == B)
  323. return 0;
  324. if (A == false)
  325. return -1;
  326. return 1;
  327. }
  328. /*}}}*/
  329. // OrderList::OrderCompareA - Order the installation by op /*{{{*/
  330. // ---------------------------------------------------------------------
  331. /* This provides a first-pass sort of the list and gives a decent starting
  332. point for further complete ordering. It is used by OrderUnpack only */
  333. int pkgOrderList::OrderCompareA(const void *a, const void *b)
  334. {
  335. PkgIterator A(Me->Cache,*(Package **)a);
  336. PkgIterator B(Me->Cache,*(Package **)b);
  337. // We order packages with a set state toward the front
  338. int Res;
  339. if ((Res = BoolCompare(Me->IsNow(A),Me->IsNow(B))) != 0)
  340. return -1*Res;
  341. // We order missing files to toward the end
  342. /* if (Me->FileList != 0)
  343. {
  344. if ((Res = BoolCompare(Me->IsMissing(A),
  345. Me->IsMissing(B))) != 0)
  346. return Res;
  347. }*/
  348. if (A.State() != pkgCache::PkgIterator::NeedsNothing &&
  349. B.State() == pkgCache::PkgIterator::NeedsNothing)
  350. return -1;
  351. if (A.State() == pkgCache::PkgIterator::NeedsNothing &&
  352. B.State() != pkgCache::PkgIterator::NeedsNothing)
  353. return 1;
  354. int ScoreA = Me->Score(A);
  355. int ScoreB = Me->Score(B);
  356. if (ScoreA > ScoreB)
  357. return -1;
  358. if (ScoreA < ScoreB)
  359. return 1;
  360. return strcmp(A.Name(),B.Name());
  361. }
  362. /*}}}*/
  363. // OrderList::OrderCompareB - Order the installation by source /*{{{*/
  364. // ---------------------------------------------------------------------
  365. /* This orders by installation source. This is useful to handle
  366. inter-source breaks */
  367. int pkgOrderList::OrderCompareB(const void *a, const void *b)
  368. {
  369. PkgIterator A(Me->Cache,*(Package **)a);
  370. PkgIterator B(Me->Cache,*(Package **)b);
  371. if (A.State() != pkgCache::PkgIterator::NeedsNothing &&
  372. B.State() == pkgCache::PkgIterator::NeedsNothing)
  373. return -1;
  374. if (A.State() == pkgCache::PkgIterator::NeedsNothing &&
  375. B.State() != pkgCache::PkgIterator::NeedsNothing)
  376. return 1;
  377. int F = Me->FileCmp(A,B);
  378. if (F != 0)
  379. {
  380. if (F > 0)
  381. return -1;
  382. return 1;
  383. }
  384. int ScoreA = Me->Score(A);
  385. int ScoreB = Me->Score(B);
  386. if (ScoreA > ScoreB)
  387. return -1;
  388. if (ScoreA < ScoreB)
  389. return 1;
  390. return strcmp(A.Name(),B.Name());
  391. }
  392. /*}}}*/
  393. // OrderList::VisitDeps - Visit forward install dependencies /*{{{*/
  394. // ---------------------------------------------------------------------
  395. /* This calls the dependency function for the normal forwards dependencies
  396. of the package */
  397. bool pkgOrderList::VisitDeps(DepFunc F,PkgIterator Pkg)
  398. {
  399. if (F == 0 || Pkg.end() == true || Cache[Pkg].InstallVer == 0)
  400. return true;
  401. return (this->*F)(Cache[Pkg].InstVerIter(Cache).DependsList());
  402. }
  403. /*}}}*/
  404. // OrderList::VisitRDeps - Visit reverse dependencies /*{{{*/
  405. // ---------------------------------------------------------------------
  406. /* This calls the dependency function for all of the normal reverse depends
  407. of the package */
  408. bool pkgOrderList::VisitRDeps(DepFunc F,PkgIterator Pkg)
  409. {
  410. if (F == 0 || Pkg.end() == true)
  411. return true;
  412. return (this->*F)(Pkg.RevDependsList());
  413. }
  414. /*}}}*/
  415. // OrderList::VisitRProvides - Visit provides reverse dependencies /*{{{*/
  416. // ---------------------------------------------------------------------
  417. /* This calls the dependency function for all reverse dependencies
  418. generated by the provides line on the package. */
  419. bool pkgOrderList::VisitRProvides(DepFunc F,VerIterator Ver)
  420. {
  421. if (F == 0 || Ver.end() == true)
  422. return true;
  423. bool Res = true;
  424. for (PrvIterator P = Ver.ProvidesList(); P.end() == false; P++)
  425. Res &= (this->*F)(P.ParentPkg().RevDependsList());
  426. return true;
  427. }
  428. /*}}}*/
  429. // OrderList::VisitProvides - Visit all of the providing packages /*{{{*/
  430. // ---------------------------------------------------------------------
  431. /* This routine calls visit on all providing packages. */
  432. bool pkgOrderList::VisitProvides(DepIterator D,bool Critical)
  433. {
  434. SPtrArray<Version *> List = D.AllTargets();
  435. for (Version **I = List; *I != 0; I++)
  436. {
  437. VerIterator Ver(Cache,*I);
  438. PkgIterator Pkg = Ver.ParentPkg();
  439. if (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing)
  440. continue;
  441. if (D->Type != pkgCache::Dep::Conflicts &&
  442. D->Type != pkgCache::Dep::DpkgBreaks &&
  443. D->Type != pkgCache::Dep::Obsoletes &&
  444. Cache[Pkg].InstallVer != *I)
  445. continue;
  446. if ((D->Type == pkgCache::Dep::Conflicts ||
  447. D->Type == pkgCache::Dep::DpkgBreaks ||
  448. D->Type == pkgCache::Dep::Obsoletes) &&
  449. (Version *)Pkg.CurrentVer() != *I)
  450. continue;
  451. // Skip over missing files
  452. if (Critical == false && IsMissing(D.ParentPkg()) == true)
  453. continue;
  454. if (VisitNode(Pkg) == false)
  455. return false;
  456. }
  457. return true;
  458. }
  459. /*}}}*/
  460. // OrderList::VisitNode - Recursive ordering director /*{{{*/
  461. // ---------------------------------------------------------------------
  462. /* This is the core ordering routine. It calls the set dependency
  463. consideration functions which then potentialy call this again. Finite
  464. depth is achived through the colouring mechinism. */
  465. bool pkgOrderList::VisitNode(PkgIterator Pkg)
  466. {
  467. // Looping or irrelevent.
  468. // This should probably trancend not installed packages
  469. if (Pkg.end() == true || IsFlag(Pkg,Added) == true ||
  470. IsFlag(Pkg,AddPending) == true || IsFlag(Pkg,InList) == false)
  471. return true;
  472. if (Debug == true)
  473. {
  474. for (int j = 0; j != Depth; j++) clog << ' ';
  475. clog << "Visit " << Pkg.FullName() << endl;
  476. }
  477. Depth++;
  478. // Color grey
  479. Flag(Pkg,AddPending);
  480. DepFunc Old = Primary;
  481. // Perform immedate configuration of the package if so flagged.
  482. if (IsFlag(Pkg,Immediate) == true && Primary != &pkgOrderList::DepUnPackPre)
  483. Primary = &pkgOrderList::DepUnPackPreD;
  484. if (IsNow(Pkg) == true)
  485. {
  486. bool Res = true;
  487. if (Cache[Pkg].Delete() == false)
  488. {
  489. // Primary
  490. Res &= Res && VisitDeps(Primary,Pkg);
  491. Res &= Res && VisitRDeps(Primary,Pkg);
  492. Res &= Res && VisitRProvides(Primary,Pkg.CurrentVer());
  493. Res &= Res && VisitRProvides(Primary,Cache[Pkg].InstVerIter(Cache));
  494. // RevDep
  495. Res &= Res && VisitRDeps(RevDepends,Pkg);
  496. Res &= Res && VisitRProvides(RevDepends,Pkg.CurrentVer());
  497. Res &= Res && VisitRProvides(RevDepends,Cache[Pkg].InstVerIter(Cache));
  498. // Secondary
  499. Res &= Res && VisitDeps(Secondary,Pkg);
  500. Res &= Res && VisitRDeps(Secondary,Pkg);
  501. Res &= Res && VisitRProvides(Secondary,Pkg.CurrentVer());
  502. Res &= Res && VisitRProvides(Secondary,Cache[Pkg].InstVerIter(Cache));
  503. }
  504. else
  505. {
  506. // RevDep
  507. Res &= Res && VisitRDeps(Remove,Pkg);
  508. Res &= Res && VisitRProvides(Remove,Pkg.CurrentVer());
  509. }
  510. }
  511. if (IsFlag(Pkg,Added) == false)
  512. {
  513. Flag(Pkg,Added,Added | AddPending);
  514. if (IsFlag(Pkg,After) == true)
  515. *AfterEnd++ = Pkg;
  516. else
  517. *End++ = Pkg;
  518. }
  519. Primary = Old;
  520. Depth--;
  521. if (Debug == true)
  522. {
  523. for (int j = 0; j != Depth; j++) clog << ' ';
  524. clog << "Leave " << Pkg.FullName() << ' ' << IsFlag(Pkg,Added) << ',' << IsFlag(Pkg,AddPending) << endl;
  525. }
  526. return true;
  527. }
  528. /*}}}*/
  529. // OrderList::DepUnPackCrit - Critical UnPacking ordering /*{{{*/
  530. // ---------------------------------------------------------------------
  531. /* Critical unpacking ordering strives to satisfy Conflicts: and
  532. PreDepends: only. When a prdepends is encountered the Primary
  533. DepFunc is changed to be DepUnPackPreD.
  534. Loops are preprocessed and logged. */
  535. bool pkgOrderList::DepUnPackCrit(DepIterator D)
  536. {
  537. for (; D.end() == false; D++)
  538. {
  539. if (D.Reverse() == true)
  540. {
  541. /* Reverse depenanices are only interested in conflicts,
  542. predepend breakage is ignored here */
  543. if (D->Type != pkgCache::Dep::Conflicts &&
  544. D->Type != pkgCache::Dep::Obsoletes)
  545. continue;
  546. // Duplication elimination, consider only the current version
  547. if (D.ParentPkg().CurrentVer() != D.ParentVer())
  548. continue;
  549. /* For reverse dependencies we wish to check if the
  550. dependency is satisifed in the install state. The
  551. target package (caller) is going to be in the installed
  552. state. */
  553. if (CheckDep(D) == true)
  554. continue;
  555. if (VisitNode(D.ParentPkg()) == false)
  556. return false;
  557. }
  558. else
  559. {
  560. /* Forward critical dependencies MUST be correct before the
  561. package can be unpacked. */
  562. if (D->Type != pkgCache::Dep::Conflicts &&
  563. D->Type != pkgCache::Dep::DpkgBreaks &&
  564. D->Type != pkgCache::Dep::Obsoletes &&
  565. D->Type != pkgCache::Dep::PreDepends)
  566. continue;
  567. /* We wish to check if the dep is okay in the now state of the
  568. target package against the install state of this package. */
  569. if (CheckDep(D) == true)
  570. {
  571. /* We want to catch predepends loops with the code below.
  572. Conflicts loops that are Dep OK are ignored */
  573. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  574. D->Type != pkgCache::Dep::PreDepends)
  575. continue;
  576. }
  577. // This is the loop detection
  578. if (IsFlag(D.TargetPkg(),Added) == true ||
  579. IsFlag(D.TargetPkg(),AddPending) == true)
  580. {
  581. if (IsFlag(D.TargetPkg(),AddPending) == true)
  582. AddLoop(D);
  583. continue;
  584. }
  585. /* Predepends require a special ordering stage, they must have
  586. all dependents installed as well */
  587. DepFunc Old = Primary;
  588. bool Res = false;
  589. if (D->Type == pkgCache::Dep::PreDepends)
  590. Primary = &pkgOrderList::DepUnPackPreD;
  591. Res = VisitProvides(D,true);
  592. Primary = Old;
  593. if (Res == false)
  594. return false;
  595. }
  596. }
  597. return true;
  598. }
  599. /*}}}*/
  600. // OrderList::DepUnPackPreD - Critical UnPacking ordering with depends /*{{{*/
  601. // ---------------------------------------------------------------------
  602. /* Critical PreDepends (also configure immediate and essential) strives to
  603. ensure not only that all conflicts+predepends are met but that this
  604. package will be immediately configurable when it is unpacked.
  605. Loops are preprocessed and logged. */
  606. bool pkgOrderList::DepUnPackPreD(DepIterator D)
  607. {
  608. if (D.Reverse() == true)
  609. return DepUnPackCrit(D);
  610. for (; D.end() == false; D++)
  611. {
  612. if (D.IsCritical() == false)
  613. continue;
  614. /* We wish to check if the dep is okay in the now state of the
  615. target package against the install state of this package. */
  616. if (CheckDep(D) == true)
  617. {
  618. /* We want to catch predepends loops with the code below.
  619. Conflicts loops that are Dep OK are ignored */
  620. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  621. D->Type != pkgCache::Dep::PreDepends)
  622. continue;
  623. }
  624. // This is the loop detection
  625. if (IsFlag(D.TargetPkg(),Added) == true ||
  626. IsFlag(D.TargetPkg(),AddPending) == true)
  627. {
  628. if (IsFlag(D.TargetPkg(),AddPending) == true)
  629. AddLoop(D);
  630. continue;
  631. }
  632. if (VisitProvides(D,true) == false)
  633. return false;
  634. }
  635. return true;
  636. }
  637. /*}}}*/
  638. // OrderList::DepUnPackPre - Critical Predepends ordering /*{{{*/
  639. // ---------------------------------------------------------------------
  640. /* Critical PreDepends (also configure immediate and essential) strives to
  641. ensure not only that all conflicts+predepends are met but that this
  642. package will be immediately configurable when it is unpacked.
  643. Loops are preprocessed and logged. All loops will be fatal. */
  644. bool pkgOrderList::DepUnPackPre(DepIterator D)
  645. {
  646. if (D.Reverse() == true)
  647. return true;
  648. for (; D.end() == false; D++)
  649. {
  650. /* Only consider the PreDepends or Depends. Depends are only
  651. considered at the lowest depth or in the case of immediate
  652. configure */
  653. if (D->Type != pkgCache::Dep::PreDepends)
  654. {
  655. if (D->Type == pkgCache::Dep::Depends)
  656. {
  657. if (Depth == 1 && IsFlag(D.ParentPkg(),Immediate) == false)
  658. continue;
  659. }
  660. else
  661. continue;
  662. }
  663. /* We wish to check if the dep is okay in the now state of the
  664. target package against the install state of this package. */
  665. if (CheckDep(D) == true)
  666. {
  667. /* We want to catch predepends loops with the code below.
  668. Conflicts loops that are Dep OK are ignored */
  669. if (IsFlag(D.TargetPkg(),AddPending) == false)
  670. continue;
  671. }
  672. // This is the loop detection
  673. if (IsFlag(D.TargetPkg(),Added) == true ||
  674. IsFlag(D.TargetPkg(),AddPending) == true)
  675. {
  676. if (IsFlag(D.TargetPkg(),AddPending) == true)
  677. AddLoop(D);
  678. continue;
  679. }
  680. if (VisitProvides(D,true) == false)
  681. return false;
  682. }
  683. return true;
  684. }
  685. /*}}}*/
  686. // OrderList::DepUnPackDep - Reverse dependency considerations /*{{{*/
  687. // ---------------------------------------------------------------------
  688. /* Reverse dependencies are considered to determine if unpacking this
  689. package will break any existing dependencies. If so then those
  690. packages are ordered before this one so that they are in the
  691. UnPacked state.
  692. The forwards depends loop is designed to bring the packages dependents
  693. close to the package. This helps reduce deconfigure time.
  694. Loops are irrelevent to this. */
  695. bool pkgOrderList::DepUnPackDep(DepIterator D)
  696. {
  697. for (; D.end() == false; D++)
  698. if (D.IsCritical() == true)
  699. {
  700. if (D.Reverse() == true)
  701. {
  702. /* Duplication prevention. We consider rev deps only on
  703. the current version, a not installed package
  704. cannot break */
  705. if (D.ParentPkg()->CurrentVer == 0 ||
  706. D.ParentPkg().CurrentVer() != D.ParentVer())
  707. continue;
  708. // The dep will not break so it is irrelevent.
  709. if (CheckDep(D) == true)
  710. continue;
  711. // Skip over missing files
  712. if (IsMissing(D.ParentPkg()) == true)
  713. continue;
  714. if (VisitNode(D.ParentPkg()) == false)
  715. return false;
  716. }
  717. else
  718. {
  719. if (D->Type == pkgCache::Dep::Depends)
  720. if (VisitProvides(D,false) == false)
  721. return false;
  722. if (D->Type == pkgCache::Dep::DpkgBreaks)
  723. {
  724. if (CheckDep(D) == true)
  725. continue;
  726. if (VisitNode(D.TargetPkg()) == false)
  727. return false;
  728. }
  729. }
  730. }
  731. return true;
  732. }
  733. /*}}}*/
  734. // OrderList::DepConfigure - Configuration ordering /*{{{*/
  735. // ---------------------------------------------------------------------
  736. /* Configuration only ordering orders by the Depends: line only. It
  737. orders configuration so that when a package comes to be configured it's
  738. dependents are configured.
  739. Loops are ingored. Depends loop entry points are chaotic. */
  740. bool pkgOrderList::DepConfigure(DepIterator D)
  741. {
  742. // Never consider reverse configuration dependencies.
  743. if (D.Reverse() == true)
  744. return true;
  745. for (; D.end() == false; D++)
  746. if (D->Type == pkgCache::Dep::Depends)
  747. if (VisitProvides(D,false) == false)
  748. return false;
  749. return true;
  750. }
  751. /*}}}*/
  752. // OrderList::DepRemove - Removal ordering /*{{{*/
  753. // ---------------------------------------------------------------------
  754. /* Removal visits all reverse depends. It considers if the dependency
  755. of the Now state version to see if it is okay with removing this
  756. package. This check should always fail, but is provided for symetery
  757. with the other critical handlers.
  758. Loops are preprocessed and logged. Removal loops can also be
  759. detected in the critical handler. They are characterized by an
  760. old version of A depending on B but the new version of A conflicting
  761. with B, thus either A or B must break to install. */
  762. bool pkgOrderList::DepRemove(DepIterator D)
  763. {
  764. if (D.Reverse() == false)
  765. return true;
  766. for (; D.end() == false; D++)
  767. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  768. {
  769. // Duplication elimination, consider the current version only
  770. if (D.ParentPkg().CurrentVer() != D.ParentVer())
  771. continue;
  772. /* We wish to see if the dep on the parent package is okay
  773. in the removed (install) state of the target pkg. */
  774. bool tryFixDeps = false;
  775. if (CheckDep(D) == true)
  776. {
  777. // We want to catch loops with the code below.
  778. if (IsFlag(D.ParentPkg(),AddPending) == false)
  779. continue;
  780. }
  781. else
  782. tryFixDeps = true;
  783. // This is the loop detection
  784. if (IsFlag(D.ParentPkg(),Added) == true ||
  785. IsFlag(D.ParentPkg(),AddPending) == true)
  786. {
  787. if (IsFlag(D.ParentPkg(),AddPending) == true)
  788. AddLoop(D);
  789. continue;
  790. }
  791. if (tryFixDeps == true)
  792. {
  793. for (pkgCache::DepIterator F = D.ParentPkg().CurrentVer().DependsList();
  794. F.end() == false; ++F)
  795. {
  796. if (F->Type != pkgCache::Dep::Depends && F->Type != pkgCache::Dep::PreDepends)
  797. continue;
  798. // Check the Providers
  799. if (F.TargetPkg()->ProvidesList != 0)
  800. {
  801. pkgCache::PrvIterator Prov = F.TargetPkg().ProvidesList();
  802. for (; Prov.end() == false; ++Prov)
  803. {
  804. pkgCache::PkgIterator const P = Prov.OwnerPkg();
  805. if (IsFlag(P, InList) == true &&
  806. IsFlag(P, AddPending) == true &&
  807. IsFlag(P, Added) == false &&
  808. Cache[P].InstallVer == 0)
  809. break;
  810. }
  811. if (Prov.end() == false)
  812. for (pkgCache::PrvIterator Prv = F.TargetPkg().ProvidesList();
  813. Prv.end() == false; ++Prv)
  814. {
  815. pkgCache::PkgIterator const P = Prv.OwnerPkg();
  816. if (IsFlag(P, InList) == true &&
  817. IsFlag(P, AddPending) == false &&
  818. Cache[P].InstallVer != 0 &&
  819. VisitNode(P) == true)
  820. {
  821. Flag(P, Immediate);
  822. tryFixDeps = false;
  823. break;
  824. }
  825. }
  826. if (tryFixDeps == false)
  827. break;
  828. }
  829. // Check for Or groups
  830. if ((F->CompareOp & pkgCache::Dep::Or) != pkgCache::Dep::Or)
  831. continue;
  832. // Lets see if the package is part of the Or group
  833. pkgCache::DepIterator S = F;
  834. for (; S.end() == false; ++S)
  835. {
  836. if (S.TargetPkg() == D.TargetPkg())
  837. break;
  838. if ((S->CompareOp & pkgCache::Dep::Or) != pkgCache::Dep::Or ||
  839. CheckDep(S)) // Or group is satisfied by another package
  840. for (;S.end() == false; ++S);
  841. }
  842. if (S.end() == true)
  843. continue;
  844. // skip to the end of the or group
  845. for (;S.end() == false && (S->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or; ++S);
  846. ++S;
  847. // The soon to be removed is part of the Or group
  848. // start again in the or group and find something which will serve as replacement
  849. for (; F.end() == false && F != S; ++F)
  850. {
  851. if (F.TargetPkg() == D.TargetPkg() ||
  852. IsFlag(F.TargetPkg(), InList) == false ||
  853. VisitNode(F.TargetPkg()) == false)
  854. continue;
  855. Flag(F.TargetPkg(), Immediate);
  856. tryFixDeps = false;
  857. break;
  858. }
  859. if (tryFixDeps == false)
  860. break;
  861. }
  862. }
  863. // Skip over missing files
  864. if (IsMissing(D.ParentPkg()) == true)
  865. continue;
  866. if (VisitNode(D.ParentPkg()) == false)
  867. return false;
  868. }
  869. return true;
  870. }
  871. /*}}}*/
  872. // OrderList::AddLoop - Add a loop to the loop list /*{{{*/
  873. // ---------------------------------------------------------------------
  874. /* We record the loops. This is a relic since loop breaking is done
  875. genericaly as part of the safety routines. */
  876. bool pkgOrderList::AddLoop(DepIterator D)
  877. {
  878. if (LoopCount < 0 || LoopCount >= 20)
  879. return false;
  880. // Skip dups
  881. if (LoopCount != 0)
  882. {
  883. if (Loops[LoopCount - 1].ParentPkg() == D.ParentPkg() ||
  884. Loops[LoopCount - 1].TargetPkg() == D.ParentPkg())
  885. return true;
  886. }
  887. Loops[LoopCount++] = D;
  888. // Mark the packages as being part of a loop.
  889. Flag(D.TargetPkg(),Loop);
  890. Flag(D.ParentPkg(),Loop);
  891. return true;
  892. }
  893. /*}}}*/
  894. // OrderList::WipeFlags - Unset the given flags from all packages /*{{{*/
  895. // ---------------------------------------------------------------------
  896. /* */
  897. void pkgOrderList::WipeFlags(unsigned long F)
  898. {
  899. unsigned long Size = Cache.Head().PackageCount;
  900. for (unsigned long I = 0; I != Size; I++)
  901. Flags[I] &= ~F;
  902. }
  903. /*}}}*/
  904. // OrderList::CheckDep - Check a dependency for truth /*{{{*/
  905. // ---------------------------------------------------------------------
  906. /* This performs a complete analysis of the dependency wrt to the
  907. current add list. It returns true if after all events are
  908. performed it is still true. This sort of routine can be approximated
  909. by examining the DepCache, however in convoluted cases of provides
  910. this fails to produce a suitable result. */
  911. bool pkgOrderList::CheckDep(DepIterator D)
  912. {
  913. SPtrArray<Version *> List = D.AllTargets();
  914. bool Hit = false;
  915. for (Version **I = List; *I != 0; I++)
  916. {
  917. VerIterator Ver(Cache,*I);
  918. PkgIterator Pkg = Ver.ParentPkg();
  919. /* The meaning of Added and AddPending is subtle. AddPending is
  920. an indication that the package is looping. Because of the
  921. way ordering works Added means the package will be unpacked
  922. before this one and AddPending means after. It is therefore
  923. correct to ignore AddPending in all cases, but that exposes
  924. reverse-ordering loops which should be ignored. */
  925. if (IsFlag(Pkg,Added) == true ||
  926. (IsFlag(Pkg,AddPending) == true && D.Reverse() == true))
  927. {
  928. if (Cache[Pkg].InstallVer != *I)
  929. continue;
  930. }
  931. else
  932. if ((Version *)Pkg.CurrentVer() != *I ||
  933. Pkg.State() != PkgIterator::NeedsNothing)
  934. continue;
  935. /* Conflicts requires that all versions are not present, depends
  936. just needs one */
  937. if (D->Type != pkgCache::Dep::Conflicts &&
  938. D->Type != pkgCache::Dep::DpkgBreaks &&
  939. D->Type != pkgCache::Dep::Obsoletes)
  940. {
  941. /* Try to find something that does not have the after flag set
  942. if at all possible */
  943. if (IsFlag(Pkg,After) == true)
  944. {
  945. Hit = true;
  946. continue;
  947. }
  948. return true;
  949. }
  950. else
  951. {
  952. if (IsFlag(Pkg,After) == true)
  953. Flag(D.ParentPkg(),After);
  954. return false;
  955. }
  956. }
  957. // We found a hit, but it had the after flag set
  958. if (Hit == true && D->Type == pkgCache::Dep::PreDepends)
  959. {
  960. Flag(D.ParentPkg(),After);
  961. return true;
  962. }
  963. /* Conflicts requires that all versions are not present, depends
  964. just needs one */
  965. if (D->Type == pkgCache::Dep::Conflicts ||
  966. D->Type == pkgCache::Dep::Obsoletes)
  967. return true;
  968. return false;
  969. }
  970. /*}}}*/