orderlist.cc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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<config.h>
  56. #include <apt-pkg/orderlist.h>
  57. #include <apt-pkg/depcache.h>
  58. #include <apt-pkg/error.h>
  59. #include <apt-pkg/sptr.h>
  60. #include <apt-pkg/configuration.h>
  61. #include <apt-pkg/cacheiterators.h>
  62. #include <apt-pkg/pkgcache.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <string>
  66. #include <iostream>
  67. /*}}}*/
  68. using namespace std;
  69. pkgOrderList *pkgOrderList::Me = 0;
  70. // OrderList::pkgOrderList - Constructor /*{{{*/
  71. // ---------------------------------------------------------------------
  72. /* */
  73. pkgOrderList::pkgOrderList(pkgDepCache *pCache) : d(NULL), Cache(*pCache),
  74. Primary(NULL), Secondary(NULL),
  75. RevDepends(NULL), Remove(NULL),
  76. AfterEnd(NULL), FileList(NULL),
  77. LoopCount(-1), Depth(0)
  78. {
  79. Debug = _config->FindB("Debug::pkgOrderList",false);
  80. /* Construct the arrays, egcs 1.0.1 bug requires the package count
  81. hack */
  82. unsigned long Size = Cache.Head().PackageCount;
  83. Flags = new unsigned short[Size];
  84. End = List = new Package *[Size];
  85. memset(Flags,0,sizeof(*Flags)*Size);
  86. }
  87. /*}}}*/
  88. // OrderList::~pkgOrderList - Destructor /*{{{*/
  89. // ---------------------------------------------------------------------
  90. /* */
  91. pkgOrderList::~pkgOrderList()
  92. {
  93. delete [] List;
  94. delete [] Flags;
  95. }
  96. /*}}}*/
  97. // OrderList::IsMissing - Check if a file is missing /*{{{*/
  98. // ---------------------------------------------------------------------
  99. /* */
  100. bool pkgOrderList::IsMissing(PkgIterator Pkg)
  101. {
  102. // Skip packages to erase
  103. if (Cache[Pkg].Delete() == true)
  104. return false;
  105. // Skip Packages that need configure only.
  106. if ((Pkg.State() == pkgCache::PkgIterator::NeedsConfigure ||
  107. Pkg.State() == pkgCache::PkgIterator::NeedsNothing) &&
  108. Cache[Pkg].Keep() == true)
  109. return false;
  110. if (FileList == 0)
  111. return false;
  112. if (FileList[Pkg->ID].empty() == false)
  113. return false;
  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), "DoRun") == 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. // Removals should be done after we dealt with essentials
  265. static int const ScoreDelete = _config->FindI("OrderList::Score::Delete", 100);
  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. if (Cache[Pkg].InstVerIter(Cache)->Priority <= 5)
  288. {
  289. signed short PrioMap[] = {0,5,4,3,1,0};
  290. Score += PrioMap[Cache[Pkg].InstVerIter(Cache)->Priority];
  291. }
  292. return Score;
  293. }
  294. /*}}}*/
  295. // OrderList::FileCmp - Compare by package file /*{{{*/
  296. // ---------------------------------------------------------------------
  297. /* This compares by the package file that the install version is in. */
  298. int pkgOrderList::FileCmp(PkgIterator A,PkgIterator B)
  299. {
  300. if (Cache[A].Delete() == true && Cache[B].Delete() == true)
  301. return 0;
  302. if (Cache[A].Delete() == true)
  303. return -1;
  304. if (Cache[B].Delete() == true)
  305. return 1;
  306. if (Cache[A].InstVerIter(Cache).FileList().end() == true)
  307. return -1;
  308. if (Cache[B].InstVerIter(Cache).FileList().end() == true)
  309. return 1;
  310. pkgCache::PackageFile *FA = Cache[A].InstVerIter(Cache).FileList().File();
  311. pkgCache::PackageFile *FB = Cache[B].InstVerIter(Cache).FileList().File();
  312. if (FA < FB)
  313. return -1;
  314. if (FA > FB)
  315. return 1;
  316. return 0;
  317. }
  318. /*}}}*/
  319. // BoolCompare - Comparison function for two booleans /*{{{*/
  320. // ---------------------------------------------------------------------
  321. /* */
  322. static int BoolCompare(bool A,bool B)
  323. {
  324. if (A == B)
  325. return 0;
  326. if (A == false)
  327. return -1;
  328. return 1;
  329. }
  330. /*}}}*/
  331. // OrderList::OrderCompareA - Order the installation by op /*{{{*/
  332. // ---------------------------------------------------------------------
  333. /* This provides a first-pass sort of the list and gives a decent starting
  334. point for further complete ordering. It is used by OrderUnpack only */
  335. int pkgOrderList::OrderCompareA(const void *a, const void *b)
  336. {
  337. PkgIterator A(Me->Cache,*(Package **)a);
  338. PkgIterator B(Me->Cache,*(Package **)b);
  339. // We order packages with a set state toward the front
  340. int Res;
  341. if ((Res = BoolCompare(Me->IsNow(A),Me->IsNow(B))) != 0)
  342. return -1*Res;
  343. // We order missing files to toward the end
  344. /* if (Me->FileList != 0)
  345. {
  346. if ((Res = BoolCompare(Me->IsMissing(A),
  347. Me->IsMissing(B))) != 0)
  348. return Res;
  349. }*/
  350. if (A.State() != pkgCache::PkgIterator::NeedsNothing &&
  351. B.State() == pkgCache::PkgIterator::NeedsNothing)
  352. return -1;
  353. if (A.State() == pkgCache::PkgIterator::NeedsNothing &&
  354. B.State() != pkgCache::PkgIterator::NeedsNothing)
  355. return 1;
  356. int ScoreA = Me->Score(A);
  357. int ScoreB = Me->Score(B);
  358. if (ScoreA > ScoreB)
  359. return -1;
  360. if (ScoreA < ScoreB)
  361. return 1;
  362. return strcmp(A.Name(),B.Name());
  363. }
  364. /*}}}*/
  365. // OrderList::OrderCompareB - Order the installation by source /*{{{*/
  366. // ---------------------------------------------------------------------
  367. /* This orders by installation source. This is useful to handle
  368. inter-source breaks */
  369. int pkgOrderList::OrderCompareB(const void *a, const void *b)
  370. {
  371. PkgIterator A(Me->Cache,*(Package **)a);
  372. PkgIterator B(Me->Cache,*(Package **)b);
  373. if (A.State() != pkgCache::PkgIterator::NeedsNothing &&
  374. B.State() == pkgCache::PkgIterator::NeedsNothing)
  375. return -1;
  376. if (A.State() == pkgCache::PkgIterator::NeedsNothing &&
  377. B.State() != pkgCache::PkgIterator::NeedsNothing)
  378. return 1;
  379. int F = Me->FileCmp(A,B);
  380. if (F != 0)
  381. {
  382. if (F > 0)
  383. return -1;
  384. return 1;
  385. }
  386. int ScoreA = Me->Score(A);
  387. int ScoreB = Me->Score(B);
  388. if (ScoreA > ScoreB)
  389. return -1;
  390. if (ScoreA < ScoreB)
  391. return 1;
  392. return strcmp(A.Name(),B.Name());
  393. }
  394. /*}}}*/
  395. // OrderList::VisitDeps - Visit forward install dependencies /*{{{*/
  396. // ---------------------------------------------------------------------
  397. /* This calls the dependency function for the normal forwards dependencies
  398. of the package */
  399. bool pkgOrderList::VisitDeps(DepFunc F,PkgIterator Pkg)
  400. {
  401. if (F == 0 || Pkg.end() == true || Cache[Pkg].InstallVer == 0)
  402. return true;
  403. return (this->*F)(Cache[Pkg].InstVerIter(Cache).DependsList());
  404. }
  405. /*}}}*/
  406. // OrderList::VisitRDeps - Visit reverse dependencies /*{{{*/
  407. // ---------------------------------------------------------------------
  408. /* This calls the dependency function for all of the normal reverse depends
  409. of the package */
  410. bool pkgOrderList::VisitRDeps(DepFunc F,PkgIterator Pkg)
  411. {
  412. if (F == 0 || Pkg.end() == true)
  413. return true;
  414. return (this->*F)(Pkg.RevDependsList());
  415. }
  416. /*}}}*/
  417. // OrderList::VisitRProvides - Visit provides reverse dependencies /*{{{*/
  418. // ---------------------------------------------------------------------
  419. /* This calls the dependency function for all reverse dependencies
  420. generated by the provides line on the package. */
  421. bool pkgOrderList::VisitRProvides(DepFunc F,VerIterator Ver)
  422. {
  423. if (F == 0 || Ver.end() == true)
  424. return true;
  425. bool Res = true;
  426. for (PrvIterator P = Ver.ProvidesList(); P.end() == false; ++P)
  427. Res &= (this->*F)(P.ParentPkg().RevDependsList());
  428. return Res;
  429. }
  430. /*}}}*/
  431. // OrderList::VisitProvides - Visit all of the providing packages /*{{{*/
  432. // ---------------------------------------------------------------------
  433. /* This routine calls visit on all providing packages.
  434. If the dependency is negative it first visits packages which are
  435. intended to be removed and after that all other packages.
  436. It does so to avoid situations in which this package is used to
  437. satisfy a (or-group/provides) dependency of another package which
  438. could have been satisfied also by upgrading another package -
  439. otherwise we have more broken packages dpkg needs to auto-
  440. deconfigure and in very complicated situations it even decides
  441. against it! */
  442. bool pkgOrderList::VisitProvides(DepIterator D,bool Critical)
  443. {
  444. SPtrArray<Version *> List = D.AllTargets();
  445. for (Version **I = List; *I != 0; ++I)
  446. {
  447. VerIterator Ver(Cache,*I);
  448. PkgIterator Pkg = Ver.ParentPkg();
  449. if (D.IsNegative() == true && Cache[Pkg].Delete() == false)
  450. continue;
  451. if (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing)
  452. continue;
  453. if (D.IsNegative() == false &&
  454. Cache[Pkg].InstallVer != *I)
  455. continue;
  456. if (D.IsNegative() == true &&
  457. (Version *)Pkg.CurrentVer() != *I)
  458. continue;
  459. // Skip over missing files
  460. if (Critical == false && IsMissing(D.ParentPkg()) == true)
  461. continue;
  462. if (VisitNode(Pkg, "Provides-1") == false)
  463. return false;
  464. }
  465. if (D.IsNegative() == false)
  466. return true;
  467. for (Version **I = List; *I != 0; ++I)
  468. {
  469. VerIterator Ver(Cache,*I);
  470. PkgIterator Pkg = Ver.ParentPkg();
  471. if (Cache[Pkg].Delete() == true)
  472. continue;
  473. if (Cache[Pkg].Keep() == true && Pkg.State() == PkgIterator::NeedsNothing)
  474. continue;
  475. if ((Version *)Pkg.CurrentVer() != *I)
  476. continue;
  477. // Skip over missing files
  478. if (Critical == false && IsMissing(D.ParentPkg()) == true)
  479. continue;
  480. if (VisitNode(Pkg, "Provides-2") == false)
  481. return false;
  482. }
  483. return true;
  484. }
  485. /*}}}*/
  486. // OrderList::VisitNode - Recursive ordering director /*{{{*/
  487. // ---------------------------------------------------------------------
  488. /* This is the core ordering routine. It calls the set dependency
  489. consideration functions which then potentialy call this again. Finite
  490. depth is achieved through the colouring mechinism. */
  491. bool pkgOrderList::VisitNode(PkgIterator Pkg, char const* from)
  492. {
  493. // Looping or irrelevant.
  494. // This should probably trancend not installed packages
  495. if (Pkg.end() == true || IsFlag(Pkg,Added) == true ||
  496. IsFlag(Pkg,AddPending) == true || IsFlag(Pkg,InList) == false)
  497. return true;
  498. if (Debug == true)
  499. {
  500. for (int j = 0; j != Depth; j++) clog << ' ';
  501. clog << "Visit " << Pkg.FullName() << " from " << from << endl;
  502. }
  503. Depth++;
  504. // Color grey
  505. Flag(Pkg,AddPending);
  506. DepFunc Old = Primary;
  507. // Perform immedate configuration of the package if so flagged.
  508. if (IsFlag(Pkg,Immediate) == true && Primary != &pkgOrderList::DepUnPackPre)
  509. Primary = &pkgOrderList::DepUnPackPreD;
  510. if (IsNow(Pkg) == true)
  511. {
  512. bool Res = true;
  513. if (Cache[Pkg].Delete() == false)
  514. {
  515. // Primary
  516. Res &= Res && VisitDeps(Primary,Pkg);
  517. Res &= Res && VisitRDeps(Primary,Pkg);
  518. Res &= Res && VisitRProvides(Primary,Pkg.CurrentVer());
  519. Res &= Res && VisitRProvides(Primary,Cache[Pkg].InstVerIter(Cache));
  520. // RevDep
  521. Res &= Res && VisitRDeps(RevDepends,Pkg);
  522. Res &= Res && VisitRProvides(RevDepends,Pkg.CurrentVer());
  523. Res &= Res && VisitRProvides(RevDepends,Cache[Pkg].InstVerIter(Cache));
  524. // Secondary
  525. Res &= Res && VisitDeps(Secondary,Pkg);
  526. Res &= Res && VisitRDeps(Secondary,Pkg);
  527. Res &= Res && VisitRProvides(Secondary,Pkg.CurrentVer());
  528. Res &= Res && VisitRProvides(Secondary,Cache[Pkg].InstVerIter(Cache));
  529. }
  530. else
  531. {
  532. // RevDep
  533. Res &= Res && VisitRDeps(Remove,Pkg);
  534. Res &= Res && VisitRProvides(Remove,Pkg.CurrentVer());
  535. }
  536. }
  537. if (IsFlag(Pkg,Added) == false)
  538. {
  539. Flag(Pkg,Added,Added | AddPending);
  540. if (IsFlag(Pkg,After) == true)
  541. *AfterEnd++ = Pkg;
  542. else
  543. *End++ = Pkg;
  544. }
  545. Primary = Old;
  546. Depth--;
  547. if (Debug == true)
  548. {
  549. for (int j = 0; j != Depth; j++) clog << ' ';
  550. clog << "Leave " << Pkg.FullName() << ' ' << IsFlag(Pkg,Added) << ',' << IsFlag(Pkg,AddPending) << endl;
  551. }
  552. return true;
  553. }
  554. /*}}}*/
  555. // OrderList::DepUnPackCrit - Critical UnPacking ordering /*{{{*/
  556. // ---------------------------------------------------------------------
  557. /* Critical unpacking ordering strives to satisfy Conflicts: and
  558. PreDepends: only. When a prdepends is encountered the Primary
  559. DepFunc is changed to be DepUnPackPreD.
  560. Loops are preprocessed and logged. */
  561. bool pkgOrderList::DepUnPackCrit(DepIterator D)
  562. {
  563. for (; D.end() == false; ++D)
  564. {
  565. if (D.Reverse() == true)
  566. {
  567. /* Reverse depenanices are only interested in conflicts,
  568. predepend breakage is ignored here */
  569. if (D->Type != pkgCache::Dep::Conflicts &&
  570. D->Type != pkgCache::Dep::Obsoletes)
  571. continue;
  572. // Duplication elimination, consider only the current version
  573. if (D.ParentPkg().CurrentVer() != D.ParentVer())
  574. continue;
  575. /* For reverse dependencies we wish to check if the
  576. dependency is satisifed in the install state. The
  577. target package (caller) is going to be in the installed
  578. state. */
  579. if (CheckDep(D) == true)
  580. continue;
  581. if (VisitNode(D.ParentPkg(), "UnPackCrit") == false)
  582. return false;
  583. }
  584. else
  585. {
  586. /* Forward critical dependencies MUST be correct before the
  587. package can be unpacked. */
  588. if (D.IsNegative() == false &&
  589. D->Type != pkgCache::Dep::PreDepends)
  590. continue;
  591. /* We wish to check if the dep is okay in the now state of the
  592. target package against the install state of this package. */
  593. if (CheckDep(D) == true)
  594. {
  595. /* We want to catch predepends loops with the code below.
  596. Conflicts loops that are Dep OK are ignored */
  597. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  598. D->Type != pkgCache::Dep::PreDepends)
  599. continue;
  600. }
  601. // This is the loop detection
  602. if (IsFlag(D.TargetPkg(),Added) == true ||
  603. IsFlag(D.TargetPkg(),AddPending) == true)
  604. {
  605. if (IsFlag(D.TargetPkg(),AddPending) == true)
  606. AddLoop(D);
  607. continue;
  608. }
  609. /* Predepends require a special ordering stage, they must have
  610. all dependents installed as well */
  611. DepFunc Old = Primary;
  612. bool Res = false;
  613. if (D->Type == pkgCache::Dep::PreDepends)
  614. Primary = &pkgOrderList::DepUnPackPreD;
  615. Res = VisitProvides(D,true);
  616. Primary = Old;
  617. if (Res == false)
  618. return false;
  619. }
  620. }
  621. return true;
  622. }
  623. /*}}}*/
  624. // OrderList::DepUnPackPreD - Critical UnPacking ordering with depends /*{{{*/
  625. // ---------------------------------------------------------------------
  626. /* Critical PreDepends (also configure immediate and essential) strives to
  627. ensure not only that all conflicts+predepends are met but that this
  628. package will be immediately configurable when it is unpacked.
  629. Loops are preprocessed and logged. */
  630. bool pkgOrderList::DepUnPackPreD(DepIterator D)
  631. {
  632. if (D.Reverse() == true)
  633. return DepUnPackCrit(D);
  634. for (; D.end() == false; ++D)
  635. {
  636. if (D.IsCritical() == false)
  637. continue;
  638. /* We wish to check if the dep is okay in the now state of the
  639. target package against the install state of this package. */
  640. if (CheckDep(D) == true)
  641. {
  642. /* We want to catch predepends loops with the code below.
  643. Conflicts loops that are Dep OK are ignored */
  644. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  645. D->Type != pkgCache::Dep::PreDepends)
  646. continue;
  647. }
  648. // This is the loop detection
  649. if (IsFlag(D.TargetPkg(),Added) == true ||
  650. IsFlag(D.TargetPkg(),AddPending) == true)
  651. {
  652. if (IsFlag(D.TargetPkg(),AddPending) == true)
  653. AddLoop(D);
  654. continue;
  655. }
  656. if (VisitProvides(D,true) == false)
  657. return false;
  658. }
  659. return true;
  660. }
  661. /*}}}*/
  662. // OrderList::DepUnPackPre - Critical Predepends ordering /*{{{*/
  663. // ---------------------------------------------------------------------
  664. /* Critical PreDepends (also configure immediate and essential) strives to
  665. ensure not only that all conflicts+predepends are met but that this
  666. package will be immediately configurable when it is unpacked.
  667. Loops are preprocessed and logged. All loops will be fatal. */
  668. bool pkgOrderList::DepUnPackPre(DepIterator D)
  669. {
  670. if (D.Reverse() == true)
  671. return true;
  672. for (; D.end() == false; ++D)
  673. {
  674. /* Only consider the PreDepends or Depends. Depends are only
  675. considered at the lowest depth or in the case of immediate
  676. configure */
  677. if (D->Type != pkgCache::Dep::PreDepends)
  678. {
  679. if (D->Type == pkgCache::Dep::Depends)
  680. {
  681. if (Depth == 1 && IsFlag(D.ParentPkg(),Immediate) == false)
  682. continue;
  683. }
  684. else
  685. continue;
  686. }
  687. /* We wish to check if the dep is okay in the now state of the
  688. target package against the install state of this package. */
  689. if (CheckDep(D) == true)
  690. {
  691. /* We want to catch predepends loops with the code below.
  692. Conflicts loops that are Dep OK are ignored */
  693. if (IsFlag(D.TargetPkg(),AddPending) == false)
  694. continue;
  695. }
  696. // This is the loop detection
  697. if (IsFlag(D.TargetPkg(),Added) == true ||
  698. IsFlag(D.TargetPkg(),AddPending) == true)
  699. {
  700. if (IsFlag(D.TargetPkg(),AddPending) == true)
  701. AddLoop(D);
  702. continue;
  703. }
  704. if (VisitProvides(D,true) == false)
  705. return false;
  706. }
  707. return true;
  708. }
  709. /*}}}*/
  710. // OrderList::DepUnPackDep - Reverse dependency considerations /*{{{*/
  711. // ---------------------------------------------------------------------
  712. /* Reverse dependencies are considered to determine if unpacking this
  713. package will break any existing dependencies. If so then those
  714. packages are ordered before this one so that they are in the
  715. UnPacked state.
  716. The forwards depends loop is designed to bring the packages dependents
  717. close to the package. This helps reduce deconfigure time.
  718. Loops are irrelevant to this. */
  719. bool pkgOrderList::DepUnPackDep(DepIterator D)
  720. {
  721. for (; D.end() == false; ++D)
  722. if (D.IsCritical() == true)
  723. {
  724. if (D.Reverse() == true)
  725. {
  726. /* Duplication prevention. We consider rev deps only on
  727. the current version, a not installed package
  728. cannot break */
  729. if (D.ParentPkg()->CurrentVer == 0 ||
  730. D.ParentPkg().CurrentVer() != D.ParentVer())
  731. continue;
  732. // The dep will not break so it is irrelevant.
  733. if (CheckDep(D) == true)
  734. continue;
  735. // Skip over missing files
  736. if (IsMissing(D.ParentPkg()) == true)
  737. continue;
  738. if (VisitNode(D.ParentPkg(), "UnPackDep-Parent") == false)
  739. return false;
  740. }
  741. else
  742. {
  743. if (D->Type == pkgCache::Dep::Depends)
  744. if (VisitProvides(D,false) == false)
  745. return false;
  746. if (D->Type == pkgCache::Dep::DpkgBreaks)
  747. {
  748. if (CheckDep(D) == true)
  749. continue;
  750. if (VisitNode(D.TargetPkg(), "UnPackDep-Target") == false)
  751. return false;
  752. }
  753. }
  754. }
  755. return true;
  756. }
  757. /*}}}*/
  758. // OrderList::DepConfigure - Configuration ordering /*{{{*/
  759. // ---------------------------------------------------------------------
  760. /* Configuration only ordering orders by the Depends: line only. It
  761. orders configuration so that when a package comes to be configured it's
  762. dependents are configured.
  763. Loops are ingored. Depends loop entry points are chaotic. */
  764. bool pkgOrderList::DepConfigure(DepIterator D)
  765. {
  766. // Never consider reverse configuration dependencies.
  767. if (D.Reverse() == true)
  768. return true;
  769. for (; D.end() == false; ++D)
  770. if (D->Type == pkgCache::Dep::Depends)
  771. if (VisitProvides(D,false) == false)
  772. return false;
  773. return true;
  774. }
  775. /*}}}*/
  776. // OrderList::DepRemove - Removal ordering /*{{{*/
  777. // ---------------------------------------------------------------------
  778. /* Checks all given dependencies if they are broken by the remove of a
  779. package and if so fix it by visiting another provider or or-group
  780. member to ensure that the dependee keeps working which is especially
  781. important for Immediate packages like e.g. those depending on an
  782. awk implementation. If the dependency can't be fixed with another
  783. package this means an upgrade of the package will solve the problem. */
  784. bool pkgOrderList::DepRemove(DepIterator Broken)
  785. {
  786. if (Broken.Reverse() == false)
  787. return true;
  788. for (; Broken.end() == false; ++Broken)
  789. {
  790. if (Broken->Type != pkgCache::Dep::Depends &&
  791. Broken->Type != pkgCache::Dep::PreDepends)
  792. continue;
  793. PkgIterator BrokenPkg = Broken.ParentPkg();
  794. // uninstalled packages can't break via a remove
  795. if (BrokenPkg->CurrentVer == 0)
  796. continue;
  797. // if its already added, we can't do anything useful
  798. if (IsFlag(BrokenPkg, AddPending) == true || IsFlag(BrokenPkg, Added) == true)
  799. continue;
  800. // if the dependee is going to be removed, visit it now
  801. if (Cache[BrokenPkg].Delete() == true)
  802. return VisitNode(BrokenPkg, "Remove-Dependee");
  803. // The package stays around, so find out how this is possible
  804. for (DepIterator D = BrokenPkg.CurrentVer().DependsList(); D.end() == false;)
  805. {
  806. // only important or-groups need fixing
  807. if (D->Type != pkgCache::Dep::Depends &&
  808. D->Type != pkgCache::Dep::PreDepends)
  809. {
  810. ++D;
  811. continue;
  812. }
  813. // Start is the beginning of the or-group, D is the first one after or
  814. DepIterator Start = D;
  815. bool foundBroken = false;
  816. for (bool LastOR = true; D.end() == false && LastOR == true; ++D)
  817. {
  818. LastOR = (D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  819. if (D == Broken)
  820. foundBroken = true;
  821. }
  822. // this or-group isn't the broken one: keep searching
  823. if (foundBroken == false)
  824. continue;
  825. // iterate over all members of the or-group searching for a ready replacement
  826. bool readyReplacement = false;
  827. for (DepIterator OrMember = Start; OrMember != D && readyReplacement == false; ++OrMember)
  828. {
  829. Version ** Replacements = OrMember.AllTargets();
  830. for (Version **R = Replacements; *R != 0; ++R)
  831. {
  832. VerIterator Ver(Cache,*R);
  833. // only currently installed packages can be a replacement
  834. PkgIterator RPkg = Ver.ParentPkg();
  835. if (RPkg.CurrentVer() != Ver)
  836. continue;
  837. // packages going to be removed can't be a replacement
  838. if (Cache[RPkg].Delete() == true)
  839. continue;
  840. readyReplacement = true;
  841. break;
  842. }
  843. delete[] Replacements;
  844. }
  845. // something else is ready to take over, do nothing
  846. if (readyReplacement == true)
  847. continue;
  848. // see if we can visit a replacement
  849. bool visitReplacement = false;
  850. for (DepIterator OrMember = Start; OrMember != D && visitReplacement == false; ++OrMember)
  851. {
  852. Version ** Replacements = OrMember.AllTargets();
  853. for (Version **R = Replacements; *R != 0; ++R)
  854. {
  855. VerIterator Ver(Cache,*R);
  856. // consider only versions we plan to install
  857. PkgIterator RPkg = Ver.ParentPkg();
  858. if (Cache[RPkg].Install() == false || Cache[RPkg].InstallVer != Ver)
  859. continue;
  860. // loops are not going to help us, so don't create them
  861. if (IsFlag(RPkg, AddPending) == true)
  862. continue;
  863. if (IsMissing(RPkg) == true)
  864. continue;
  865. visitReplacement = true;
  866. if (IsFlag(BrokenPkg, Immediate) == false)
  867. {
  868. if (VisitNode(RPkg, "Remove-Rep") == true)
  869. break;
  870. }
  871. else
  872. {
  873. Flag(RPkg, Immediate);
  874. if (VisitNode(RPkg, "Remove-ImmRep") == true)
  875. break;
  876. }
  877. visitReplacement = false;
  878. }
  879. delete[] Replacements;
  880. }
  881. if (visitReplacement == true)
  882. continue;
  883. // the broken package in current version can't be fixed, so install new version
  884. if (IsMissing(BrokenPkg) == true)
  885. break;
  886. if (VisitNode(BrokenPkg, "Remove-Upgrade") == false)
  887. return false;
  888. }
  889. }
  890. return true;
  891. }
  892. /*}}}*/
  893. // OrderList::AddLoop - Add a loop to the loop list /*{{{*/
  894. // ---------------------------------------------------------------------
  895. /* We record the loops. This is a relic since loop breaking is done
  896. genericaly as part of the safety routines. */
  897. bool pkgOrderList::AddLoop(DepIterator D)
  898. {
  899. if (LoopCount < 0 || LoopCount >= 20)
  900. return false;
  901. // Skip dups
  902. if (LoopCount != 0)
  903. {
  904. if (Loops[LoopCount - 1].ParentPkg() == D.ParentPkg() ||
  905. Loops[LoopCount - 1].TargetPkg() == D.ParentPkg())
  906. return true;
  907. }
  908. Loops[LoopCount++] = D;
  909. // Mark the packages as being part of a loop.
  910. //Flag(D.TargetPkg(),Loop);
  911. //Flag(D.ParentPkg(),Loop);
  912. /* This is currently disabled because the Loop flag is being used for
  913. loop management in the package manager. Check the orderlist.h file for more info */
  914. return true;
  915. }
  916. /*}}}*/
  917. // OrderList::WipeFlags - Unset the given flags from all packages /*{{{*/
  918. // ---------------------------------------------------------------------
  919. /* */
  920. void pkgOrderList::WipeFlags(unsigned long F)
  921. {
  922. unsigned long Size = Cache.Head().PackageCount;
  923. for (unsigned long I = 0; I != Size; I++)
  924. Flags[I] &= ~F;
  925. }
  926. /*}}}*/
  927. // OrderList::CheckDep - Check a dependency for truth /*{{{*/
  928. // ---------------------------------------------------------------------
  929. /* This performs a complete analysis of the dependency wrt to the
  930. current add list. It returns true if after all events are
  931. performed it is still true. This sort of routine can be approximated
  932. by examining the DepCache, however in convoluted cases of provides
  933. this fails to produce a suitable result. */
  934. bool pkgOrderList::CheckDep(DepIterator D)
  935. {
  936. SPtrArray<Version *> List = D.AllTargets();
  937. bool Hit = false;
  938. for (Version **I = List; *I != 0; I++)
  939. {
  940. VerIterator Ver(Cache,*I);
  941. PkgIterator Pkg = Ver.ParentPkg();
  942. /* The meaning of Added and AddPending is subtle. AddPending is
  943. an indication that the package is looping. Because of the
  944. way ordering works Added means the package will be unpacked
  945. before this one and AddPending means after. It is therefore
  946. correct to ignore AddPending in all cases, but that exposes
  947. reverse-ordering loops which should be ignored. */
  948. if (IsFlag(Pkg,Added) == true ||
  949. (IsFlag(Pkg,AddPending) == true && D.Reverse() == true))
  950. {
  951. if (Cache[Pkg].InstallVer != *I)
  952. continue;
  953. }
  954. else
  955. if ((Version *)Pkg.CurrentVer() != *I ||
  956. Pkg.State() != PkgIterator::NeedsNothing)
  957. continue;
  958. /* Conflicts requires that all versions are not present, depends
  959. just needs one */
  960. if (D.IsNegative() == false)
  961. {
  962. // ignore provides by older versions of this package
  963. if (((D.Reverse() == false && Pkg == D.ParentPkg()) ||
  964. (D.Reverse() == true && Pkg == D.TargetPkg())) &&
  965. Cache[Pkg].InstallVer != *I)
  966. continue;
  967. /* Try to find something that does not have the after flag set
  968. if at all possible */
  969. if (IsFlag(Pkg,After) == true)
  970. {
  971. Hit = true;
  972. continue;
  973. }
  974. return true;
  975. }
  976. else
  977. {
  978. if (IsFlag(Pkg,After) == true)
  979. Flag(D.ParentPkg(),After);
  980. return false;
  981. }
  982. }
  983. // We found a hit, but it had the after flag set
  984. if (Hit == true && D->Type == pkgCache::Dep::PreDepends)
  985. {
  986. Flag(D.ParentPkg(),After);
  987. return true;
  988. }
  989. /* Conflicts requires that all versions are not present, depends
  990. just needs one */
  991. if (D->Type == pkgCache::Dep::Conflicts ||
  992. D->Type == pkgCache::Dep::Obsoletes)
  993. return true;
  994. return false;
  995. }
  996. /*}}}*/