depcache.cc 27 KB

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