depcache.cc 29 KB

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