depcache.cc 28 KB

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