depcache.cc 28 KB

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