orderlist.cc 31 KB

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