depcache.cc 28 KB

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