orderlist.cc 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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->Type != pkgCache::Dep::Conflicts &&
  439. D->Type != pkgCache::Dep::DpkgBreaks &&
  440. D->Type != pkgCache::Dep::Obsoletes &&
  441. Cache[Pkg].InstallVer != *I)
  442. continue;
  443. if ((D->Type == pkgCache::Dep::Conflicts ||
  444. D->Type == pkgCache::Dep::DpkgBreaks ||
  445. D->Type == pkgCache::Dep::Obsoletes) &&
  446. (Version *)Pkg.CurrentVer() != *I)
  447. continue;
  448. // Skip over missing files
  449. if (Critical == false && IsMissing(D.ParentPkg()) == true)
  450. continue;
  451. if (VisitNode(Pkg) == false)
  452. return false;
  453. }
  454. return true;
  455. }
  456. /*}}}*/
  457. // OrderList::VisitNode - Recursive ordering director /*{{{*/
  458. // ---------------------------------------------------------------------
  459. /* This is the core ordering routine. It calls the set dependency
  460. consideration functions which then potentialy call this again. Finite
  461. depth is achived through the colouring mechinism. */
  462. bool pkgOrderList::VisitNode(PkgIterator Pkg)
  463. {
  464. // Looping or irrelevent.
  465. // This should probably trancend not installed packages
  466. if (Pkg.end() == true || IsFlag(Pkg,Added) == true ||
  467. IsFlag(Pkg,AddPending) == true || IsFlag(Pkg,InList) == false)
  468. return true;
  469. if (Debug == true)
  470. {
  471. for (int j = 0; j != Depth; j++) clog << ' ';
  472. clog << "Visit " << Pkg.FullName() << endl;
  473. }
  474. Depth++;
  475. // Color grey
  476. Flag(Pkg,AddPending);
  477. DepFunc Old = Primary;
  478. // Perform immedate configuration of the package if so flagged.
  479. if (IsFlag(Pkg,Immediate) == true && Primary != &pkgOrderList::DepUnPackPre)
  480. Primary = &pkgOrderList::DepUnPackPreD;
  481. if (IsNow(Pkg) == true)
  482. {
  483. bool Res = true;
  484. if (Cache[Pkg].Delete() == false)
  485. {
  486. // Primary
  487. Res &= Res && VisitDeps(Primary,Pkg);
  488. Res &= Res && VisitRDeps(Primary,Pkg);
  489. Res &= Res && VisitRProvides(Primary,Pkg.CurrentVer());
  490. Res &= Res && VisitRProvides(Primary,Cache[Pkg].InstVerIter(Cache));
  491. // RevDep
  492. Res &= Res && VisitRDeps(RevDepends,Pkg);
  493. Res &= Res && VisitRProvides(RevDepends,Pkg.CurrentVer());
  494. Res &= Res && VisitRProvides(RevDepends,Cache[Pkg].InstVerIter(Cache));
  495. // Secondary
  496. Res &= Res && VisitDeps(Secondary,Pkg);
  497. Res &= Res && VisitRDeps(Secondary,Pkg);
  498. Res &= Res && VisitRProvides(Secondary,Pkg.CurrentVer());
  499. Res &= Res && VisitRProvides(Secondary,Cache[Pkg].InstVerIter(Cache));
  500. }
  501. else
  502. {
  503. // RevDep
  504. Res &= Res && VisitRDeps(Remove,Pkg);
  505. Res &= Res && VisitRProvides(Remove,Pkg.CurrentVer());
  506. }
  507. }
  508. if (IsFlag(Pkg,Added) == false)
  509. {
  510. Flag(Pkg,Added,Added | AddPending);
  511. if (IsFlag(Pkg,After) == true)
  512. *AfterEnd++ = Pkg;
  513. else
  514. *End++ = Pkg;
  515. }
  516. Primary = Old;
  517. Depth--;
  518. if (Debug == true)
  519. {
  520. for (int j = 0; j != Depth; j++) clog << ' ';
  521. clog << "Leave " << Pkg.FullName() << ' ' << IsFlag(Pkg,Added) << ',' << IsFlag(Pkg,AddPending) << endl;
  522. }
  523. return true;
  524. }
  525. /*}}}*/
  526. // OrderList::DepUnPackCrit - Critical UnPacking ordering /*{{{*/
  527. // ---------------------------------------------------------------------
  528. /* Critical unpacking ordering strives to satisfy Conflicts: and
  529. PreDepends: only. When a prdepends is encountered the Primary
  530. DepFunc is changed to be DepUnPackPreD.
  531. Loops are preprocessed and logged. */
  532. bool pkgOrderList::DepUnPackCrit(DepIterator D)
  533. {
  534. for (; D.end() == false; D++)
  535. {
  536. if (D.Reverse() == true)
  537. {
  538. /* Reverse depenanices are only interested in conflicts,
  539. predepend breakage is ignored here */
  540. if (D->Type != pkgCache::Dep::Conflicts &&
  541. D->Type != pkgCache::Dep::Obsoletes)
  542. continue;
  543. // Duplication elimination, consider only the current version
  544. if (D.ParentPkg().CurrentVer() != D.ParentVer())
  545. continue;
  546. /* For reverse dependencies we wish to check if the
  547. dependency is satisifed in the install state. The
  548. target package (caller) is going to be in the installed
  549. state. */
  550. if (CheckDep(D) == true)
  551. continue;
  552. if (VisitNode(D.ParentPkg()) == false)
  553. return false;
  554. }
  555. else
  556. {
  557. /* Forward critical dependencies MUST be correct before the
  558. package can be unpacked. */
  559. if (D->Type != pkgCache::Dep::Conflicts &&
  560. D->Type != pkgCache::Dep::DpkgBreaks &&
  561. D->Type != pkgCache::Dep::Obsoletes &&
  562. D->Type != pkgCache::Dep::PreDepends)
  563. continue;
  564. /* We wish to check if the dep is okay in the now state of the
  565. target package against the install state of this package. */
  566. if (CheckDep(D) == true)
  567. {
  568. /* We want to catch predepends loops with the code below.
  569. Conflicts loops that are Dep OK are ignored */
  570. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  571. D->Type != pkgCache::Dep::PreDepends)
  572. continue;
  573. }
  574. // This is the loop detection
  575. if (IsFlag(D.TargetPkg(),Added) == true ||
  576. IsFlag(D.TargetPkg(),AddPending) == true)
  577. {
  578. if (IsFlag(D.TargetPkg(),AddPending) == true)
  579. AddLoop(D);
  580. continue;
  581. }
  582. /* Predepends require a special ordering stage, they must have
  583. all dependents installed as well */
  584. DepFunc Old = Primary;
  585. bool Res = false;
  586. if (D->Type == pkgCache::Dep::PreDepends)
  587. Primary = &pkgOrderList::DepUnPackPreD;
  588. Res = VisitProvides(D,true);
  589. Primary = Old;
  590. if (Res == false)
  591. return false;
  592. }
  593. }
  594. return true;
  595. }
  596. /*}}}*/
  597. // OrderList::DepUnPackPreD - Critical UnPacking ordering with depends /*{{{*/
  598. // ---------------------------------------------------------------------
  599. /* Critical PreDepends (also configure immediate and essential) strives to
  600. ensure not only that all conflicts+predepends are met but that this
  601. package will be immediately configurable when it is unpacked.
  602. Loops are preprocessed and logged. */
  603. bool pkgOrderList::DepUnPackPreD(DepIterator D)
  604. {
  605. if (D.Reverse() == true)
  606. return DepUnPackCrit(D);
  607. for (; D.end() == false; D++)
  608. {
  609. if (D.IsCritical() == false)
  610. continue;
  611. /* We wish to check if the dep is okay in the now state of the
  612. target package against the install state of this package. */
  613. if (CheckDep(D) == true)
  614. {
  615. /* We want to catch predepends loops with the code below.
  616. Conflicts loops that are Dep OK are ignored */
  617. if (IsFlag(D.TargetPkg(),AddPending) == false ||
  618. D->Type != pkgCache::Dep::PreDepends)
  619. continue;
  620. }
  621. // This is the loop detection
  622. if (IsFlag(D.TargetPkg(),Added) == true ||
  623. IsFlag(D.TargetPkg(),AddPending) == true)
  624. {
  625. if (IsFlag(D.TargetPkg(),AddPending) == true)
  626. AddLoop(D);
  627. continue;
  628. }
  629. if (VisitProvides(D,true) == false)
  630. return false;
  631. }
  632. return true;
  633. }
  634. /*}}}*/
  635. // OrderList::DepUnPackPre - Critical Predepends ordering /*{{{*/
  636. // ---------------------------------------------------------------------
  637. /* Critical PreDepends (also configure immediate and essential) strives to
  638. ensure not only that all conflicts+predepends are met but that this
  639. package will be immediately configurable when it is unpacked.
  640. Loops are preprocessed and logged. All loops will be fatal. */
  641. bool pkgOrderList::DepUnPackPre(DepIterator D)
  642. {
  643. if (D.Reverse() == true)
  644. return true;
  645. for (; D.end() == false; D++)
  646. {
  647. /* Only consider the PreDepends or Depends. Depends are only
  648. considered at the lowest depth or in the case of immediate
  649. configure */
  650. if (D->Type != pkgCache::Dep::PreDepends)
  651. {
  652. if (D->Type == pkgCache::Dep::Depends)
  653. {
  654. if (Depth == 1 && IsFlag(D.ParentPkg(),Immediate) == false)
  655. continue;
  656. }
  657. else
  658. continue;
  659. }
  660. /* We wish to check if the dep is okay in the now state of the
  661. target package against the install state of this package. */
  662. if (CheckDep(D) == true)
  663. {
  664. /* We want to catch predepends loops with the code below.
  665. Conflicts loops that are Dep OK are ignored */
  666. if (IsFlag(D.TargetPkg(),AddPending) == false)
  667. continue;
  668. }
  669. // This is the loop detection
  670. if (IsFlag(D.TargetPkg(),Added) == true ||
  671. IsFlag(D.TargetPkg(),AddPending) == true)
  672. {
  673. if (IsFlag(D.TargetPkg(),AddPending) == true)
  674. AddLoop(D);
  675. continue;
  676. }
  677. if (VisitProvides(D,true) == false)
  678. return false;
  679. }
  680. return true;
  681. }
  682. /*}}}*/
  683. // OrderList::DepUnPackDep - Reverse dependency considerations /*{{{*/
  684. // ---------------------------------------------------------------------
  685. /* Reverse dependencies are considered to determine if unpacking this
  686. package will break any existing dependencies. If so then those
  687. packages are ordered before this one so that they are in the
  688. UnPacked state.
  689. The forwards depends loop is designed to bring the packages dependents
  690. close to the package. This helps reduce deconfigure time.
  691. Loops are irrelevent to this. */
  692. bool pkgOrderList::DepUnPackDep(DepIterator D)
  693. {
  694. for (; D.end() == false; D++)
  695. if (D.IsCritical() == true)
  696. {
  697. if (D.Reverse() == true)
  698. {
  699. /* Duplication prevention. We consider rev deps only on
  700. the current version, a not installed package
  701. cannot break */
  702. if (D.ParentPkg()->CurrentVer == 0 ||
  703. D.ParentPkg().CurrentVer() != D.ParentVer())
  704. continue;
  705. // The dep will not break so it is irrelevent.
  706. if (CheckDep(D) == true)
  707. continue;
  708. // Skip over missing files
  709. if (IsMissing(D.ParentPkg()) == true)
  710. continue;
  711. if (VisitNode(D.ParentPkg()) == false)
  712. return false;
  713. }
  714. else
  715. {
  716. if (D->Type == pkgCache::Dep::Depends)
  717. if (VisitProvides(D,false) == false)
  718. return false;
  719. if (D->Type == pkgCache::Dep::DpkgBreaks)
  720. {
  721. if (CheckDep(D) == true)
  722. continue;
  723. if (VisitNode(D.TargetPkg()) == false)
  724. return false;
  725. }
  726. }
  727. }
  728. return true;
  729. }
  730. /*}}}*/
  731. // OrderList::DepConfigure - Configuration ordering /*{{{*/
  732. // ---------------------------------------------------------------------
  733. /* Configuration only ordering orders by the Depends: line only. It
  734. orders configuration so that when a package comes to be configured it's
  735. dependents are configured.
  736. Loops are ingored. Depends loop entry points are chaotic. */
  737. bool pkgOrderList::DepConfigure(DepIterator D)
  738. {
  739. // Never consider reverse configuration dependencies.
  740. if (D.Reverse() == true)
  741. return true;
  742. for (; D.end() == false; D++)
  743. if (D->Type == pkgCache::Dep::Depends)
  744. if (VisitProvides(D,false) == false)
  745. return false;
  746. return true;
  747. }
  748. /*}}}*/
  749. // OrderList::DepRemove - Removal ordering /*{{{*/
  750. // ---------------------------------------------------------------------
  751. /* Removal visits all reverse depends. It considers if the dependency
  752. of the Now state version to see if it is okay with removing this
  753. package. This check should always fail, but is provided for symetery
  754. with the other critical handlers.
  755. Loops are preprocessed and logged. Removal loops can also be
  756. detected in the critical handler. They are characterized by an
  757. old version of A depending on B but the new version of A conflicting
  758. with B, thus either A or B must break to install. */
  759. bool pkgOrderList::DepRemove(DepIterator D)
  760. {
  761. if (D.Reverse() == false)
  762. return true;
  763. for (; D.end() == false; D++)
  764. if (D->Type == pkgCache::Dep::Depends || D->Type == pkgCache::Dep::PreDepends)
  765. {
  766. // Duplication elimination, consider the current version only
  767. if (D.ParentPkg().CurrentVer() != D.ParentVer())
  768. continue;
  769. /* We wish to see if the dep on the parent package is okay
  770. in the removed (install) state of the target pkg. */
  771. bool tryFixDeps = false;
  772. if (CheckDep(D) == true)
  773. {
  774. // We want to catch loops with the code below.
  775. if (IsFlag(D.ParentPkg(),AddPending) == false)
  776. continue;
  777. }
  778. else
  779. tryFixDeps = true;
  780. // This is the loop detection
  781. if (IsFlag(D.ParentPkg(),Added) == true ||
  782. IsFlag(D.ParentPkg(),AddPending) == true)
  783. {
  784. if (IsFlag(D.ParentPkg(),AddPending) == true)
  785. AddLoop(D);
  786. continue;
  787. }
  788. if (tryFixDeps == true)
  789. {
  790. for (pkgCache::DepIterator F = D.ParentPkg().CurrentVer().DependsList();
  791. F.end() == false; ++F)
  792. {
  793. if (F->Type != pkgCache::Dep::Depends && F->Type != pkgCache::Dep::PreDepends)
  794. continue;
  795. // Check the Providers
  796. if (F.TargetPkg()->ProvidesList != 0)
  797. {
  798. pkgCache::PrvIterator Prov = F.TargetPkg().ProvidesList();
  799. for (; Prov.end() == false; ++Prov)
  800. {
  801. pkgCache::PkgIterator const P = Prov.OwnerPkg();
  802. if (IsFlag(P, InList) == true &&
  803. IsFlag(P, AddPending) == true &&
  804. IsFlag(P, Added) == false &&
  805. Cache[P].InstallVer == 0)
  806. break;
  807. }
  808. if (Prov.end() == false)
  809. for (pkgCache::PrvIterator Prv = F.TargetPkg().ProvidesList();
  810. Prv.end() == false; ++Prv)
  811. {
  812. pkgCache::PkgIterator const P = Prv.OwnerPkg();
  813. if (IsFlag(P, InList) == true &&
  814. IsFlag(P, AddPending) == false &&
  815. Cache[P].InstallVer != 0 &&
  816. VisitNode(P) == true)
  817. {
  818. Flag(P, Immediate);
  819. tryFixDeps = false;
  820. break;
  821. }
  822. }
  823. if (tryFixDeps == false)
  824. break;
  825. }
  826. // Check for Or groups
  827. if ((F->CompareOp & pkgCache::Dep::Or) != pkgCache::Dep::Or)
  828. continue;
  829. // Lets see if the package is part of the Or group
  830. pkgCache::DepIterator S = F;
  831. for (; S.end() == false; ++S)
  832. {
  833. if (S.TargetPkg() == D.TargetPkg())
  834. break;
  835. if ((S->CompareOp & pkgCache::Dep::Or) != pkgCache::Dep::Or ||
  836. CheckDep(S)) // Or group is satisfied by another package
  837. for (;S.end() == false; ++S);
  838. }
  839. if (S.end() == true)
  840. continue;
  841. // skip to the end of the or group
  842. for (;S.end() == false && (S->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or; ++S);
  843. ++S;
  844. // The soon to be removed is part of the Or group
  845. // start again in the or group and find something which will serve as replacement
  846. for (; F.end() == false && F != S; ++F)
  847. {
  848. if (IsFlag(F.TargetPkg(), InList) == true &&
  849. IsFlag(F.TargetPkg(), AddPending) == false &&
  850. Cache[F.TargetPkg()].InstallVer != 0 &&
  851. VisitNode(F.TargetPkg()) == true)
  852. {
  853. Flag(F.TargetPkg(), Immediate);
  854. tryFixDeps = false;
  855. break;
  856. }
  857. else if (F.TargetPkg()->ProvidesList != 0)
  858. {
  859. pkgCache::PrvIterator Prv = F.TargetPkg().ProvidesList();
  860. for (; Prv.end() == false; ++Prv)
  861. {
  862. if (IsFlag(Prv.OwnerPkg(), InList) == true &&
  863. IsFlag(Prv.OwnerPkg(), AddPending) == false &&
  864. Cache[Prv.OwnerPkg()].InstallVer != 0 &&
  865. VisitNode(Prv.OwnerPkg()) == true)
  866. {
  867. Flag(Prv.OwnerPkg(), Immediate);
  868. tryFixDeps = false;
  869. break;
  870. }
  871. }
  872. if (Prv.end() == false)
  873. break;
  874. }
  875. }
  876. if (tryFixDeps == false)
  877. break;
  878. }
  879. }
  880. // Skip over missing files
  881. if (IsMissing(D.ParentPkg()) == true)
  882. continue;
  883. if (VisitNode(D.ParentPkg()) == false)
  884. return false;
  885. }
  886. return true;
  887. }
  888. /*}}}*/
  889. // OrderList::AddLoop - Add a loop to the loop list /*{{{*/
  890. // ---------------------------------------------------------------------
  891. /* We record the loops. This is a relic since loop breaking is done
  892. genericaly as part of the safety routines. */
  893. bool pkgOrderList::AddLoop(DepIterator D)
  894. {
  895. if (LoopCount < 0 || LoopCount >= 20)
  896. return false;
  897. // Skip dups
  898. if (LoopCount != 0)
  899. {
  900. if (Loops[LoopCount - 1].ParentPkg() == D.ParentPkg() ||
  901. Loops[LoopCount - 1].TargetPkg() == D.ParentPkg())
  902. return true;
  903. }
  904. Loops[LoopCount++] = D;
  905. // Mark the packages as being part of a loop.
  906. Flag(D.TargetPkg(),Loop);
  907. Flag(D.ParentPkg(),Loop);
  908. return true;
  909. }
  910. /*}}}*/
  911. // OrderList::WipeFlags - Unset the given flags from all packages /*{{{*/
  912. // ---------------------------------------------------------------------
  913. /* */
  914. void pkgOrderList::WipeFlags(unsigned long F)
  915. {
  916. unsigned long Size = Cache.Head().PackageCount;
  917. for (unsigned long I = 0; I != Size; I++)
  918. Flags[I] &= ~F;
  919. }
  920. /*}}}*/
  921. // OrderList::CheckDep - Check a dependency for truth /*{{{*/
  922. // ---------------------------------------------------------------------
  923. /* This performs a complete analysis of the dependency wrt to the
  924. current add list. It returns true if after all events are
  925. performed it is still true. This sort of routine can be approximated
  926. by examining the DepCache, however in convoluted cases of provides
  927. this fails to produce a suitable result. */
  928. bool pkgOrderList::CheckDep(DepIterator D)
  929. {
  930. SPtrArray<Version *> List = D.AllTargets();
  931. bool Hit = false;
  932. for (Version **I = List; *I != 0; I++)
  933. {
  934. VerIterator Ver(Cache,*I);
  935. PkgIterator Pkg = Ver.ParentPkg();
  936. /* The meaning of Added and AddPending is subtle. AddPending is
  937. an indication that the package is looping. Because of the
  938. way ordering works Added means the package will be unpacked
  939. before this one and AddPending means after. It is therefore
  940. correct to ignore AddPending in all cases, but that exposes
  941. reverse-ordering loops which should be ignored. */
  942. if (IsFlag(Pkg,Added) == true ||
  943. (IsFlag(Pkg,AddPending) == true && D.Reverse() == true))
  944. {
  945. if (Cache[Pkg].InstallVer != *I)
  946. continue;
  947. }
  948. else
  949. if ((Version *)Pkg.CurrentVer() != *I ||
  950. Pkg.State() != PkgIterator::NeedsNothing)
  951. continue;
  952. /* Conflicts requires that all versions are not present, depends
  953. just needs one */
  954. if (D->Type != pkgCache::Dep::Conflicts &&
  955. D->Type != pkgCache::Dep::DpkgBreaks &&
  956. D->Type != pkgCache::Dep::Obsoletes)
  957. {
  958. /* Try to find something that does not have the after flag set
  959. if at all possible */
  960. if (IsFlag(Pkg,After) == true)
  961. {
  962. Hit = true;
  963. continue;
  964. }
  965. return true;
  966. }
  967. else
  968. {
  969. if (IsFlag(Pkg,After) == true)
  970. Flag(D.ParentPkg(),After);
  971. return false;
  972. }
  973. }
  974. // We found a hit, but it had the after flag set
  975. if (Hit == true && D->Type == pkgCache::Dep::PreDepends)
  976. {
  977. Flag(D.ParentPkg(),After);
  978. return true;
  979. }
  980. /* Conflicts requires that all versions are not present, depends
  981. just needs one */
  982. if (D->Type == pkgCache::Dep::Conflicts ||
  983. D->Type == pkgCache::Dep::Obsoletes)
  984. return true;
  985. return false;
  986. }
  987. /*}}}*/