depcache.cc 28 KB

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