depcache.cc 28 KB

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