depcache.cc 26 KB

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