depcache.cc 29 KB

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