depcache.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: depcache.cc,v 1.25 2001/05/27 05:36:04 jgg Exp $
  4. /* ######################################################################
  5. Dependency Cache - Caches Dependency information.
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #ifdef __GNUG__
  10. #pragma implementation "apt-pkg/depcache.h"
  11. #endif
  12. #include <apt-pkg/depcache.h>
  13. #include <apt-pkg/version.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/sptr.h>
  16. #include <apt-pkg/algorithms.h>
  17. #include <apti18n.h>
  18. /*}}}*/
  19. // DepCache::pkgDepCache - Constructors /*{{{*/
  20. // ---------------------------------------------------------------------
  21. /* */
  22. pkgDepCache::pkgDepCache(pkgCache *pCache,Policy *Plcy) :
  23. Cache(pCache), PkgState(0), DepState(0)
  24. {
  25. delLocalPolicy = 0;
  26. LocalPolicy = Plcy;
  27. if (LocalPolicy == 0)
  28. delLocalPolicy = LocalPolicy = new Policy;
  29. }
  30. /*}}}*/
  31. // DepCache::~pkgDepCache - Destructor /*{{{*/
  32. // ---------------------------------------------------------------------
  33. /* */
  34. pkgDepCache::~pkgDepCache()
  35. {
  36. delete [] PkgState;
  37. delete [] DepState;
  38. delete delLocalPolicy;
  39. }
  40. /*}}}*/
  41. // DepCache::Init - Generate the initial extra structures. /*{{{*/
  42. // ---------------------------------------------------------------------
  43. /* This allocats the extension buffers and initializes them. */
  44. bool pkgDepCache::Init(OpProgress *Prog)
  45. {
  46. delete [] PkgState;
  47. delete [] DepState;
  48. PkgState = new StateCache[Head().PackageCount];
  49. DepState = new unsigned char[Head().DependsCount];
  50. memset(PkgState,0,sizeof(*PkgState)*Head().PackageCount);
  51. memset(DepState,0,sizeof(*DepState)*Head().DependsCount);
  52. if (Prog != 0)
  53. {
  54. Prog->OverallProgress(0,2*Head().PackageCount,Head().PackageCount,
  55. _("Building Dependency Tree"));
  56. Prog->SubProgress(Head().PackageCount,_("Candidate Versions"));
  57. }
  58. /* Set the current state of everything. In this state all of the
  59. packages are kept exactly as is. See AllUpgrade */
  60. int Done = 0;
  61. for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
  62. {
  63. if (Prog != 0)
  64. Prog->Progress(Done);
  65. // Find the proper cache slot
  66. StateCache &State = PkgState[I->ID];
  67. State.iFlags = 0;
  68. // Figure out the install version
  69. State.CandidateVer = GetCandidateVer(I);
  70. State.InstallVer = I.CurrentVer();
  71. State.Mode = ModeKeep;
  72. State.Update(I,*this);
  73. }
  74. if (Prog != 0)
  75. {
  76. Prog->OverallProgress(Head().PackageCount,2*Head().PackageCount,
  77. Head().PackageCount,
  78. _("Building Dependency Tree"));
  79. Prog->SubProgress(Head().PackageCount,_("Dependency Generation"));
  80. }
  81. Update(Prog);
  82. return true;
  83. }
  84. /*}}}*/
  85. // DepCache::CheckDep - Checks a single dependency /*{{{*/
  86. // ---------------------------------------------------------------------
  87. /* This first checks the dependency against the main target package and
  88. then walks along the package provides list and checks if each provides
  89. will be installed then checks the provides against the dep. Res will be
  90. set to the package which was used to satisfy the dep. */
  91. bool pkgDepCache::CheckDep(DepIterator Dep,int Type,PkgIterator &Res)
  92. {
  93. Res = Dep.TargetPkg();
  94. /* Check simple depends. A depends -should- never self match but
  95. we allow it anyhow because dpkg does. Technically it is a packaging
  96. bug. Conflicts may never self match */
  97. if (Dep.TargetPkg() != Dep.ParentPkg() ||
  98. (Dep->Type != Dep::Conflicts && Dep->Type != Dep::Obsoletes))
  99. {
  100. PkgIterator Pkg = Dep.TargetPkg();
  101. // Check the base package
  102. if (Type == NowVersion && Pkg->CurrentVer != 0)
  103. if (VS().CheckDep(Pkg.CurrentVer().VerStr(),Dep->CompareOp,
  104. Dep.TargetVer()) == true)
  105. return true;
  106. if (Type == InstallVersion && PkgState[Pkg->ID].InstallVer != 0)
  107. if (VS().CheckDep(PkgState[Pkg->ID].InstVerIter(*this).VerStr(),
  108. Dep->CompareOp,Dep.TargetVer()) == true)
  109. return true;
  110. if (Type == CandidateVersion && PkgState[Pkg->ID].CandidateVer != 0)
  111. if (VS().CheckDep(PkgState[Pkg->ID].CandidateVerIter(*this).VerStr(),
  112. Dep->CompareOp,Dep.TargetVer()) == true)
  113. return true;
  114. }
  115. if (Dep->Type == Dep::Obsoletes)
  116. return false;
  117. // Check the providing packages
  118. PrvIterator P = Dep.TargetPkg().ProvidesList();
  119. PkgIterator Pkg = Dep.ParentPkg();
  120. for (; P.end() != true; P++)
  121. {
  122. /* Provides may never be applied against the same package if it is
  123. a conflicts. See the comment above. */
  124. if (P.OwnerPkg() == Pkg && Dep->Type == Dep::Conflicts)
  125. continue;
  126. // Check if the provides is a hit
  127. if (Type == NowVersion)
  128. {
  129. if (P.OwnerPkg().CurrentVer() != P.OwnerVer())
  130. continue;
  131. }
  132. if (Type == InstallVersion)
  133. {
  134. StateCache &State = PkgState[P.OwnerPkg()->ID];
  135. if (State.InstallVer != (Version *)P.OwnerVer())
  136. continue;
  137. }
  138. if (Type == CandidateVersion)
  139. {
  140. StateCache &State = PkgState[P.OwnerPkg()->ID];
  141. if (State.CandidateVer != (Version *)P.OwnerVer())
  142. continue;
  143. }
  144. // Compare the versions.
  145. if (VS().CheckDep(P.ProvideVersion(),Dep->CompareOp,Dep.TargetVer()) == true)
  146. {
  147. Res = P.OwnerPkg();
  148. return true;
  149. }
  150. }
  151. return false;
  152. }
  153. /*}}}*/
  154. // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
  155. // ---------------------------------------------------------------------
  156. /* Call with Mult = -1 to preform the inverse opration */
  157. void pkgDepCache::AddSizes(const PkgIterator &Pkg,signed long Mult)
  158. {
  159. StateCache &P = PkgState[Pkg->ID];
  160. if (Pkg->VersionList == 0)
  161. return;
  162. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  163. P.Keep() == true)
  164. return;
  165. // Compute the size data
  166. if (P.NewInstall() == true)
  167. {
  168. iUsrSize += (signed)(Mult*P.InstVerIter(*this)->InstalledSize);
  169. iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
  170. return;
  171. }
  172. // Upgrading
  173. if (Pkg->CurrentVer != 0 &&
  174. (P.InstallVer != (Version *)Pkg.CurrentVer() ||
  175. (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
  176. {
  177. iUsrSize += (signed)(Mult*((signed)P.InstVerIter(*this)->InstalledSize -
  178. (signed)Pkg.CurrentVer()->InstalledSize));
  179. iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
  180. return;
  181. }
  182. // Reinstall
  183. if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
  184. P.Delete() == false)
  185. {
  186. iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
  187. return;
  188. }
  189. // Removing
  190. if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
  191. {
  192. iUsrSize -= (signed)(Mult*Pkg.CurrentVer()->InstalledSize);
  193. return;
  194. }
  195. }
  196. /*}}}*/
  197. // DepCache::AddStates - Add the package to the state counter /*{{{*/
  198. // ---------------------------------------------------------------------
  199. /* This routine is tricky to use, you must make sure that it is never
  200. called twice for the same package. This means the Remove/Add section
  201. should be as short as possible and not encompass any code that will
  202. calld Remove/Add itself. Remember, dependencies can be circular so
  203. while processing a dep for Pkg it is possible that Add/Remove
  204. will be called on Pkg */
  205. void pkgDepCache::AddStates(const PkgIterator &Pkg,int Add)
  206. {
  207. StateCache &State = PkgState[Pkg->ID];
  208. // The Package is broken
  209. if ((State.DepState & DepInstMin) != DepInstMin)
  210. iBrokenCount += Add;
  211. // Bad state
  212. if (Pkg.State() != PkgIterator::NeedsNothing)
  213. iBadCount += Add;
  214. // Not installed
  215. if (Pkg->CurrentVer == 0)
  216. {
  217. if (State.Mode == ModeDelete &&
  218. (State.iFlags | Purge) == Purge && Pkg.Purge() == false)
  219. iDelCount += Add;
  220. if (State.Mode == ModeInstall)
  221. iInstCount += Add;
  222. return;
  223. }
  224. // Installed, no upgrade
  225. if (State.Status == 0)
  226. {
  227. if (State.Mode == ModeDelete)
  228. iDelCount += Add;
  229. else
  230. if ((State.iFlags & ReInstall) == ReInstall)
  231. iInstCount += Add;
  232. return;
  233. }
  234. // Alll 3 are possible
  235. if (State.Mode == ModeDelete)
  236. iDelCount += Add;
  237. if (State.Mode == ModeKeep)
  238. iKeepCount += Add;
  239. if (State.Mode == ModeInstall)
  240. iInstCount += Add;
  241. }
  242. /*}}}*/
  243. // DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
  244. // ---------------------------------------------------------------------
  245. /* The or group results are stored in the last item of the or group. This
  246. allows easy detection of the state of a whole or'd group. */
  247. void pkgDepCache::BuildGroupOrs(VerIterator const &V)
  248. {
  249. unsigned char Group = 0;
  250. for (DepIterator D = V.DependsList(); D.end() != true; D++)
  251. {
  252. // Build the dependency state.
  253. unsigned char &State = DepState[D->ID];
  254. /* Invert for Conflicts. We have to do this twice to get the
  255. right sense for a conflicts group */
  256. if (D->Type == Dep::Conflicts || D->Type == Dep::Obsoletes)
  257. State = ~State;
  258. // Add to the group if we are within an or..
  259. State &= 0x7;
  260. Group |= State;
  261. State |= Group << 3;
  262. if ((D->CompareOp & Dep::Or) != Dep::Or)
  263. Group = 0;
  264. // Invert for Conflicts
  265. if (D->Type == Dep::Conflicts || D->Type == Dep::Obsoletes)
  266. State = ~State;
  267. }
  268. }
  269. /*}}}*/
  270. // DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
  271. // ---------------------------------------------------------------------
  272. /* This is used to run over a dependency list and determine the dep
  273. state of the list, filtering it through both a Min check and a Policy
  274. check. The return result will have SetMin/SetPolicy low if a check
  275. fails. It uses the DepState cache for it's computations. */
  276. unsigned char pkgDepCache::VersionState(DepIterator D,unsigned char Check,
  277. unsigned char SetMin,
  278. unsigned char SetPolicy)
  279. {
  280. unsigned char Dep = 0xFF;
  281. while (D.end() != true)
  282. {
  283. // Compute a single dependency element (glob or)
  284. DepIterator Start = D;
  285. unsigned char State = 0;
  286. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  287. {
  288. State |= DepState[D->ID];
  289. LastOR = (D->CompareOp & Dep::Or) == Dep::Or;
  290. }
  291. // Minimum deps that must be satisfied to have a working package
  292. if (Start.IsCritical() == true)
  293. if ((State & Check) != Check)
  294. Dep &= ~SetMin;
  295. // Policy deps that must be satisfied to install the package
  296. if (IsImportantDep(Start) == true &&
  297. (State & Check) != Check)
  298. Dep &= ~SetPolicy;
  299. }
  300. return Dep;
  301. }
  302. /*}}}*/
  303. // DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
  304. // ---------------------------------------------------------------------
  305. /* This is the main dependency computation bit. It computes the 3 main
  306. results for a dependencys, Now, Install and Candidate. Callers must
  307. invert the result if dealing with conflicts. */
  308. unsigned char pkgDepCache::DependencyState(DepIterator &D)
  309. {
  310. unsigned char State = 0;
  311. if (CheckDep(D,NowVersion) == true)
  312. State |= DepNow;
  313. if (CheckDep(D,InstallVersion) == true)
  314. State |= DepInstall;
  315. if (CheckDep(D,CandidateVersion) == true)
  316. State |= DepCVer;
  317. return State;
  318. }
  319. /*}}}*/
  320. // DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
  321. // ---------------------------------------------------------------------
  322. /* This determines the combined dependency representation of a package
  323. for its two states now and install. This is done by using the pre-generated
  324. dependency information. */
  325. void pkgDepCache::UpdateVerState(PkgIterator Pkg)
  326. {
  327. // Empty deps are always true
  328. StateCache &State = PkgState[Pkg->ID];
  329. State.DepState = 0xFF;
  330. // Check the Current state
  331. if (Pkg->CurrentVer != 0)
  332. {
  333. DepIterator D = Pkg.CurrentVer().DependsList();
  334. State.DepState &= VersionState(D,DepNow,DepNowMin,DepNowPolicy);
  335. }
  336. /* Check the candidate state. We do not compare against the whole as
  337. a candidate state but check the candidate version against the
  338. install states */
  339. if (State.CandidateVer != 0)
  340. {
  341. DepIterator D = State.CandidateVerIter(*this).DependsList();
  342. State.DepState &= VersionState(D,DepInstall,DepCandMin,DepCandPolicy);
  343. }
  344. // Check target state which can only be current or installed
  345. if (State.InstallVer != 0)
  346. {
  347. DepIterator D = State.InstVerIter(*this).DependsList();
  348. State.DepState &= VersionState(D,DepInstall,DepInstMin,DepInstPolicy);
  349. }
  350. }
  351. /*}}}*/
  352. // DepCache::Update - Figure out all the state information /*{{{*/
  353. // ---------------------------------------------------------------------
  354. /* This will figure out the state of all the packages and all the
  355. dependencies based on the current policy. */
  356. void pkgDepCache::Update(OpProgress *Prog)
  357. {
  358. iUsrSize = 0;
  359. iDownloadSize = 0;
  360. iDelCount = 0;
  361. iInstCount = 0;
  362. iKeepCount = 0;
  363. iBrokenCount = 0;
  364. iBadCount = 0;
  365. // Perform the depends pass
  366. int Done = 0;
  367. for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
  368. {
  369. if (Prog != 0 && Done%20 == 0)
  370. Prog->Progress(Done);
  371. for (VerIterator V = I.VersionList(); V.end() != true; V++)
  372. {
  373. unsigned char Group = 0;
  374. for (DepIterator D = V.DependsList(); D.end() != true; D++)
  375. {
  376. // Build the dependency state.
  377. unsigned char &State = DepState[D->ID];
  378. State = DependencyState(D);
  379. // Add to the group if we are within an or..
  380. Group |= State;
  381. State |= Group << 3;
  382. if ((D->CompareOp & Dep::Or) != Dep::Or)
  383. Group = 0;
  384. // Invert for Conflicts
  385. if (D->Type == Dep::Conflicts || D->Type == Dep::Obsoletes)
  386. State = ~State;
  387. }
  388. }
  389. // Compute the pacakge dependency state and size additions
  390. AddSizes(I);
  391. UpdateVerState(I);
  392. AddStates(I);
  393. }
  394. if (Prog != 0)
  395. Prog->Progress(Done);
  396. }
  397. /*}}}*/
  398. // DepCache::Update - Update the deps list of a package /*{{{*/
  399. // ---------------------------------------------------------------------
  400. /* This is a helper for update that only does the dep portion of the scan.
  401. It is mainly ment to scan reverse dependencies. */
  402. void pkgDepCache::Update(DepIterator D)
  403. {
  404. // Update the reverse deps
  405. for (;D.end() != true; D++)
  406. {
  407. unsigned char &State = DepState[D->ID];
  408. State = DependencyState(D);
  409. // Invert for Conflicts
  410. if (D->Type == Dep::Conflicts || D->Type == Dep::Obsoletes)
  411. State = ~State;
  412. RemoveStates(D.ParentPkg());
  413. BuildGroupOrs(D.ParentVer());
  414. UpdateVerState(D.ParentPkg());
  415. AddStates(D.ParentPkg());
  416. }
  417. }
  418. /*}}}*/
  419. // DepCache::Update - Update the related deps of a package /*{{{*/
  420. // ---------------------------------------------------------------------
  421. /* This is called whenever the state of a package changes. It updates
  422. all cached dependencies related to this package. */
  423. void pkgDepCache::Update(PkgIterator const &Pkg)
  424. {
  425. // Recompute the dep of the package
  426. RemoveStates(Pkg);
  427. UpdateVerState(Pkg);
  428. AddStates(Pkg);
  429. // Update the reverse deps
  430. Update(Pkg.RevDependsList());
  431. // Update the provides map for the current ver
  432. if (Pkg->CurrentVer != 0)
  433. for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
  434. P.end() != true; P++)
  435. Update(P.ParentPkg().RevDependsList());
  436. // Update the provides map for the candidate ver
  437. if (PkgState[Pkg->ID].CandidateVer != 0)
  438. for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
  439. P.end() != true; P++)
  440. Update(P.ParentPkg().RevDependsList());
  441. }
  442. /*}}}*/
  443. // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
  444. // ---------------------------------------------------------------------
  445. /* */
  446. void pkgDepCache::MarkKeep(PkgIterator const &Pkg,bool Soft)
  447. {
  448. // Simplifies other routines.
  449. if (Pkg.end() == true)
  450. return;
  451. /* Reject an attempt to keep a non-source broken installed package, those
  452. must be upgraded */
  453. if (Pkg.State() == PkgIterator::NeedsUnpack &&
  454. Pkg.CurrentVer().Downloadable() == false)
  455. return;
  456. /* We changed the soft state all the time so the UI is a bit nicer
  457. to use */
  458. StateCache &P = PkgState[Pkg->ID];
  459. if (Soft == true)
  460. P.iFlags |= AutoKept;
  461. else
  462. P.iFlags &= ~AutoKept;
  463. // Check that it is not already kept
  464. if (P.Mode == ModeKeep)
  465. return;
  466. // We dont even try to keep virtual packages..
  467. if (Pkg->VersionList == 0)
  468. return;
  469. P.Flags &= ~Flag::Auto;
  470. RemoveSizes(Pkg);
  471. RemoveStates(Pkg);
  472. P.Mode = ModeKeep;
  473. if (Pkg->CurrentVer == 0)
  474. P.InstallVer = 0;
  475. else
  476. P.InstallVer = Pkg.CurrentVer();
  477. AddStates(Pkg);
  478. Update(Pkg);
  479. AddSizes(Pkg);
  480. }
  481. /*}}}*/
  482. // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
  483. // ---------------------------------------------------------------------
  484. /* */
  485. void pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge)
  486. {
  487. // Simplifies other routines.
  488. if (Pkg.end() == true)
  489. return;
  490. // Check that it is not already marked for delete
  491. StateCache &P = PkgState[Pkg->ID];
  492. P.iFlags &= ~(AutoKept | Purge);
  493. if (rPurge == true)
  494. P.iFlags |= Purge;
  495. if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
  496. (Pkg.Purge() == true || rPurge == false))
  497. return;
  498. // We dont even try to delete virtual packages..
  499. if (Pkg->VersionList == 0)
  500. return;
  501. RemoveSizes(Pkg);
  502. RemoveStates(Pkg);
  503. if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
  504. P.Mode = ModeKeep;
  505. else
  506. P.Mode = ModeDelete;
  507. P.InstallVer = 0;
  508. P.Flags &= Flag::Auto;
  509. AddStates(Pkg);
  510. Update(Pkg);
  511. AddSizes(Pkg);
  512. }
  513. /*}}}*/
  514. // DepCache::MarkInstall - Put the package in the install state /*{{{*/
  515. // ---------------------------------------------------------------------
  516. /* */
  517. void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
  518. unsigned long Depth)
  519. {
  520. if (Depth > 100)
  521. return;
  522. // Simplifies other routines.
  523. if (Pkg.end() == true)
  524. return;
  525. /* Check that it is not already marked for install and that it can be
  526. installed */
  527. StateCache &P = PkgState[Pkg->ID];
  528. P.iFlags &= ~AutoKept;
  529. if (P.InstBroken() == false && (P.Mode == ModeInstall ||
  530. P.CandidateVer == (Version *)Pkg.CurrentVer()))
  531. {
  532. if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
  533. MarkKeep(Pkg);
  534. return;
  535. }
  536. // See if there is even any possible instalation candidate
  537. if (P.CandidateVer == 0)
  538. return;
  539. // We dont even try to install virtual packages..
  540. if (Pkg->VersionList == 0)
  541. return;
  542. /* Target the candidate version and remove the autoflag. We reset the
  543. autoflag below if this was called recursively. Otherwise the user
  544. should have the ability to de-auto a package by changing its state */
  545. RemoveSizes(Pkg);
  546. RemoveStates(Pkg);
  547. P.Mode = ModeInstall;
  548. P.InstallVer = P.CandidateVer;
  549. P.Flags &= ~Flag::Auto;
  550. if (P.CandidateVer == (Version *)Pkg.CurrentVer())
  551. P.Mode = ModeKeep;
  552. AddStates(Pkg);
  553. Update(Pkg);
  554. AddSizes(Pkg);
  555. if (AutoInst == false)
  556. return;
  557. DepIterator Dep = P.InstVerIter(*this).DependsList();
  558. for (; Dep.end() != true;)
  559. {
  560. // Grok or groups
  561. DepIterator Start = Dep;
  562. bool Result = true;
  563. unsigned Ors = 0;
  564. for (bool LastOR = true; Dep.end() == false && LastOR == true; Dep++,Ors++)
  565. {
  566. LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
  567. if ((DepState[Dep->ID] & DepInstall) == DepInstall)
  568. Result = false;
  569. }
  570. // Dep is satisfied okay.
  571. if (Result == false)
  572. continue;
  573. /* Check if this dep should be consider for install. If it is a user
  574. defined important dep and we are installed a new package then
  575. it will be installed. Otherwise we only worry about critical deps */
  576. if (IsImportantDep(Start) == false)
  577. continue;
  578. if (Pkg->CurrentVer != 0 && Start.IsCritical() == false)
  579. continue;
  580. /* If we are in an or group locate the first or that can
  581. succeed. We have already cached this.. */
  582. for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; Ors--)
  583. Start++;
  584. /* This bit is for processing the possibilty of an install/upgrade
  585. fixing the problem */
  586. SPtrArray<Version *> List = Start.AllTargets();
  587. if ((DepState[Start->ID] & DepCVer) == DepCVer)
  588. {
  589. // Right, find the best version to install..
  590. Version **Cur = List;
  591. PkgIterator P = Start.TargetPkg();
  592. PkgIterator InstPkg(*Cache,0);
  593. // See if there are direct matches (at the start of the list)
  594. for (; *Cur != 0 && (*Cur)->ParentPkg == P.Index(); Cur++)
  595. {
  596. PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
  597. if (PkgState[Pkg->ID].CandidateVer != *Cur)
  598. continue;
  599. InstPkg = Pkg;
  600. break;
  601. }
  602. // Select the highest priority providing package
  603. if (InstPkg.end() == true)
  604. {
  605. pkgPrioSortList(*Cache,Cur);
  606. for (; *Cur != 0; Cur++)
  607. {
  608. PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
  609. if (PkgState[Pkg->ID].CandidateVer != *Cur)
  610. continue;
  611. InstPkg = Pkg;
  612. break;
  613. }
  614. }
  615. if (InstPkg.end() == false)
  616. {
  617. MarkInstall(InstPkg,true,Depth + 1);
  618. // Set the autoflag, after MarkInstall because MarkInstall unsets it
  619. if (P->CurrentVer == 0)
  620. PkgState[InstPkg->ID].Flags |= Flag::Auto;
  621. }
  622. continue;
  623. }
  624. /* For conflicts we just de-install the package and mark as auto,
  625. Conflicts may not have or groups */
  626. if (Start->Type == Dep::Conflicts || Start->Type == Dep::Obsoletes)
  627. {
  628. for (Version **I = List; *I != 0; I++)
  629. {
  630. VerIterator Ver(*this,*I);
  631. PkgIterator Pkg = Ver.ParentPkg();
  632. MarkDelete(Pkg);
  633. PkgState[Pkg->ID].Flags |= Flag::Auto;
  634. }
  635. continue;
  636. }
  637. }
  638. }
  639. /*}}}*/
  640. // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
  641. // ---------------------------------------------------------------------
  642. /* */
  643. void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
  644. {
  645. RemoveSizes(Pkg);
  646. RemoveStates(Pkg);
  647. StateCache &P = PkgState[Pkg->ID];
  648. if (To == true)
  649. P.iFlags |= ReInstall;
  650. else
  651. P.iFlags &= ~ReInstall;
  652. AddStates(Pkg);
  653. AddSizes(Pkg);
  654. }
  655. /*}}}*/
  656. // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
  657. // ---------------------------------------------------------------------
  658. /* */
  659. void pkgDepCache::SetCandidateVersion(VerIterator TargetVer)
  660. {
  661. pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
  662. StateCache &P = PkgState[Pkg->ID];
  663. RemoveSizes(Pkg);
  664. RemoveStates(Pkg);
  665. if (P.CandidateVer == P.InstallVer)
  666. P.InstallVer = (Version *)TargetVer;
  667. P.CandidateVer = (Version *)TargetVer;
  668. P.Update(Pkg,*this);
  669. AddStates(Pkg);
  670. Update(Pkg);
  671. AddSizes(Pkg);
  672. }
  673. /*}}}*/
  674. // StateCache::Update - Compute the various static display things /*{{{*/
  675. // ---------------------------------------------------------------------
  676. /* This is called whenever the Candidate version changes. */
  677. void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
  678. {
  679. // Some info
  680. VerIterator Ver = CandidateVerIter(Cache);
  681. // Use a null string or the version string
  682. if (Ver.end() == true)
  683. CandVersion = "";
  684. else
  685. CandVersion = Ver.VerStr();
  686. // Find the current version
  687. CurVersion = "";
  688. if (Pkg->CurrentVer != 0)
  689. CurVersion = Pkg.CurrentVer().VerStr();
  690. // Strip off the epochs for display
  691. CurVersion = StripEpoch(CurVersion);
  692. CandVersion = StripEpoch(CandVersion);
  693. // Figure out if its up or down or equal
  694. Status = Ver.CompareVer(Pkg.CurrentVer());
  695. if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
  696. Status = 2;
  697. }
  698. /*}}}*/
  699. // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
  700. // ---------------------------------------------------------------------
  701. /* */
  702. const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
  703. {
  704. if (Ver == 0)
  705. return 0;
  706. // Strip any epoch
  707. for (const char *I = Ver; *I != 0; I++)
  708. if (*I == ':')
  709. return I + 1;
  710. return Ver;
  711. }
  712. /*}}}*/
  713. // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
  714. // ---------------------------------------------------------------------
  715. /* The default just returns the highest available version that is not
  716. a source and automatic. */
  717. pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator Pkg)
  718. {
  719. /* Not source/not automatic versions cannot be a candidate version
  720. unless they are already installed */
  721. VerIterator Last(*(pkgCache *)this,0);
  722. for (VerIterator I = Pkg.VersionList(); I.end() == false; I++)
  723. {
  724. if (Pkg.CurrentVer() == I)
  725. return I;
  726. for (VerFileIterator J = I.FileList(); J.end() == false; J++)
  727. {
  728. if ((J.File()->Flags & Flag::NotSource) != 0)
  729. continue;
  730. /* Stash the highest version of a not-automatic source, we use it
  731. if there is nothing better */
  732. if ((J.File()->Flags & Flag::NotAutomatic) != 0)
  733. {
  734. if (Last.end() == true)
  735. Last = I;
  736. continue;
  737. }
  738. return I;
  739. }
  740. }
  741. return Last;
  742. }
  743. /*}}}*/
  744. // Policy::IsImportantDep - True if the dependency is important /*{{{*/
  745. // ---------------------------------------------------------------------
  746. /* */
  747. bool pkgDepCache::Policy::IsImportantDep(DepIterator Dep)
  748. {
  749. return Dep.IsCritical();
  750. }
  751. /*}}}*/