orderlist.cc 34 KB

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