depcache.cc 25 KB

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