depcache.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: depcache.cc,v 1.3 1998/09/07 05:28:32 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. /*}}}*/
  16. // DepCache::pkgDepCache - Constructors /*{{{*/
  17. // ---------------------------------------------------------------------
  18. /* */
  19. pkgDepCache::pkgDepCache(MMap &Map,OpProgress &Prog) :
  20. pkgCache(Map), PkgState(0), DepState(0)
  21. {
  22. if (_error->PendingError() == false)
  23. Init(&Prog);
  24. }
  25. /*}}}*/
  26. // DepCache::~pkgDepCache - Destructor /*{{{*/
  27. // ---------------------------------------------------------------------
  28. /* */
  29. pkgDepCache::~pkgDepCache()
  30. {
  31. delete [] PkgState;
  32. delete [] DepState;
  33. }
  34. /*}}}*/
  35. // DepCache::Init - Generate the initial extra structures. /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* This allocats the extension buffers and initializes them. */
  38. bool pkgDepCache::Init(OpProgress *Prog)
  39. {
  40. delete [] PkgState;
  41. delete [] DepState;
  42. PkgState = new StateCache[Head().PackageCount];
  43. DepState = new unsigned char[Head().DependsCount];
  44. memset(PkgState,0,sizeof(*PkgState)*Head().PackageCount);
  45. memset(DepState,0,sizeof(*DepState)*Head().DependsCount);
  46. if (Prog != 0)
  47. {
  48. Prog->OverallProgress(0,2*Head().PackageCount,Head().PackageCount,
  49. "Building Dependancy Tree");
  50. Prog->SubProgress(Head().PackageCount,"Candidate Versions");
  51. }
  52. /* Set the current state of everything. In this state all of the
  53. packages are kept exactly as is. See AllUpgrade */
  54. int Done = 0;
  55. for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
  56. {
  57. if (Prog != 0)
  58. Prog->Progress(Done);
  59. // Find the proper cache slot
  60. StateCache &State = PkgState[I->ID];
  61. State.iFlags = 0;
  62. // Figure out the install version
  63. State.CandidateVer = GetCandidateVer(I);
  64. State.InstallVer = I.CurrentVer();
  65. State.Mode = ModeKeep;
  66. State.Update(I,*this);
  67. }
  68. if (Prog != 0)
  69. {
  70. Prog->OverallProgress(Head().PackageCount,2*Head().PackageCount,
  71. Head().PackageCount,
  72. "Building Dependancy Tree");
  73. Prog->SubProgress(Head().PackageCount,"Dependency Generation");
  74. }
  75. Update(Prog);
  76. return true;
  77. }
  78. /*}}}*/
  79. // DepCache::GetCandidateVer - Returns the Candidate install version /*{{{*/
  80. // ---------------------------------------------------------------------
  81. /* The default just returns the target version if it exists or the
  82. highest version. */
  83. pkgDepCache::VerIterator pkgDepCache::GetCandidateVer(PkgIterator Pkg)
  84. {
  85. // Try to use an explicit target
  86. if (Pkg->TargetVer == 0)
  87. {
  88. /* Not source versions cannot be a candidate version unless they
  89. are already installed */
  90. for (VerIterator I = Pkg.VersionList(); I.end() == false; I++)
  91. {
  92. if (Pkg.CurrentVer() == I)
  93. return I;
  94. for (VerFileIterator J = I.FileList(); J.end() == false; J++)
  95. if ((J.File()->Flags & Flag::NotSource) == 0)
  96. return I;
  97. }
  98. return VerIterator(*this,0);
  99. }
  100. else
  101. return Pkg.TargetVer();
  102. }
  103. /*}}}*/
  104. // DepCache::IsImportantDep - True if the dependency is important /*{{{*/
  105. // ---------------------------------------------------------------------
  106. /* */
  107. bool pkgDepCache::IsImportantDep(DepIterator Dep)
  108. {
  109. return Dep.IsCritical();
  110. }
  111. /*}}}*/
  112. // DepCache::CheckDep - Checks a single dependency /*{{{*/
  113. // ---------------------------------------------------------------------
  114. /* This first checks the dependency against the main target package and
  115. then walks along the package provides list and checks if each provides
  116. will be installed then checks the provides against the dep. Res will be
  117. set to the package which was used to satisfy the dep. */
  118. bool pkgDepCache::CheckDep(DepIterator Dep,int Type,PkgIterator &Res)
  119. {
  120. Res = Dep.TargetPkg();
  121. /* Check simple depends. A depends -should- never self match but
  122. we allow it anyhow because dpkg does. Technically it is a packaging
  123. bug. Conflicts may never self match */
  124. if (Dep.TargetPkg() != Dep.ParentPkg() || Dep->Type != Dep::Conflicts)
  125. {
  126. PkgIterator Pkg = Dep.TargetPkg();
  127. // Check the base package
  128. if (Type == NowVersion && Pkg->CurrentVer != 0)
  129. if (pkgCheckDep(Dep.TargetVer(),
  130. Pkg.CurrentVer().VerStr(),Dep->CompareOp) == true)
  131. return true;
  132. if (Type == InstallVersion && PkgState[Pkg->ID].InstallVer != 0)
  133. if (pkgCheckDep(Dep.TargetVer(),
  134. PkgState[Pkg->ID].InstVerIter(*this).VerStr(),
  135. Dep->CompareOp) == true)
  136. return true;
  137. if (Type == CandidateVersion && PkgState[Pkg->ID].CandidateVer != 0)
  138. if (pkgCheckDep(Dep.TargetVer(),
  139. PkgState[Pkg->ID].CandidateVerIter(*this).VerStr(),
  140. Dep->CompareOp) == true)
  141. return true;
  142. }
  143. // Check the providing packages
  144. PrvIterator P = Dep.TargetPkg().ProvidesList();
  145. PkgIterator Pkg = Dep.ParentPkg();
  146. for (; P.end() != true; P++)
  147. {
  148. /* Provides may never be applied against the same package if it is
  149. a conflicts. See the comment above. */
  150. if (P.OwnerPkg() == Pkg && Dep->Type == Dep::Conflicts)
  151. continue;
  152. // Check if the provides is a hit
  153. if (Type == NowVersion)
  154. {
  155. if (P.OwnerPkg().CurrentVer() != P.OwnerVer())
  156. continue;
  157. }
  158. if (Type == InstallVersion)
  159. {
  160. StateCache &State = PkgState[P.OwnerPkg()->ID];
  161. if (State.InstallVer != (Version *)P.OwnerVer())
  162. continue;
  163. }
  164. if (Type == CandidateVersion)
  165. {
  166. StateCache &State = PkgState[P.OwnerPkg()->ID];
  167. if (State.CandidateVer != (Version *)P.OwnerVer())
  168. continue;
  169. }
  170. // Compare the versions.
  171. if (pkgCheckDep(Dep.TargetVer(),P.ProvideVersion(),Dep->CompareOp) == true)
  172. {
  173. Res = P.OwnerPkg();
  174. return true;
  175. }
  176. }
  177. return false;
  178. }
  179. /*}}}*/
  180. // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
  181. // ---------------------------------------------------------------------
  182. /* Call with Mult = -1 to preform the inverse opration */
  183. void pkgDepCache::AddSizes(const PkgIterator &Pkg,long Mult)
  184. {
  185. StateCache &P = PkgState[Pkg->ID];
  186. // Compute the size data
  187. if (P.NewInstall() == true)
  188. {
  189. iUsrSize += Mult*P.InstVerIter(*this)->InstalledSize;
  190. iDownloadSize += Mult*P.InstVerIter(*this)->Size;
  191. }
  192. // Upgrading
  193. if (Pkg->CurrentVer != 0 && P.InstallVer != (Version *)Pkg.CurrentVer() &&
  194. P.InstallVer != 0)
  195. {
  196. iUsrSize += Mult*(P.InstVerIter(*this)->InstalledSize -
  197. Pkg.CurrentVer()->InstalledSize);
  198. iDownloadSize += Mult*P.InstVerIter(*this)->Size;
  199. }
  200. // Removing
  201. if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
  202. iUsrSize -= Mult*Pkg.CurrentVer()->InstalledSize;
  203. }
  204. /*}}}*/
  205. // DepCache::AddStates - Add the package to the state counter /*{{{*/
  206. // ---------------------------------------------------------------------
  207. /* This routine is tricky to use, you must make sure that it is never
  208. called twice for the same package. This means the Remove/Add section
  209. should be as short as possible and not encompass any code that will
  210. calld Remove/Add itself. Remember, dependencies can be circular so
  211. while processing a dep for Pkg it is possible that Add/Remove
  212. will be called on Pkg */
  213. void pkgDepCache::AddStates(const PkgIterator &Pkg,int Add)
  214. {
  215. StateCache &State = PkgState[Pkg->ID];
  216. // The Package is broken
  217. if ((State.DepState & DepInstMin) != DepInstMin)
  218. iBrokenCount += Add;
  219. // Bad state
  220. if (Pkg.State() != PkgIterator::NeedsNothing)
  221. iBadCount += Add;
  222. // Not installed
  223. if (Pkg->CurrentVer == 0)
  224. {
  225. if (State.Mode == ModeInstall)
  226. iInstCount += Add;
  227. return;
  228. }
  229. // Installed, no upgrade
  230. if (State.Upgradable() == false)
  231. {
  232. if (State.Mode == ModeDelete)
  233. iDelCount += Add;
  234. return;
  235. }
  236. // Alll 3 are possible
  237. if (State.Mode == ModeDelete)
  238. iDelCount += Add;
  239. if (State.Mode == ModeKeep)
  240. iKeepCount += Add;
  241. if (State.Mode == ModeInstall)
  242. iInstCount += Add;
  243. }
  244. /*}}}*/
  245. // DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
  246. // ---------------------------------------------------------------------
  247. /* The or group results are stored in the last item of the or group. This
  248. allows easy detection of the state of a whole or'd group. */
  249. void pkgDepCache::BuildGroupOrs(VerIterator const &V)
  250. {
  251. unsigned char Group = 0;
  252. for (DepIterator D = V.DependsList(); D.end() != true; D++)
  253. {
  254. // Build the dependency state.
  255. unsigned char &State = DepState[D->ID];
  256. /* Invert for Conflicts. We have to do this twice to get the
  257. right sense for a conflicts group */
  258. if (D->Type == Dep::Conflicts)
  259. State = ~State;
  260. // Add to the group if we are within an or..
  261. Group |= State;
  262. State |= Group << 3;
  263. if ((D->CompareOp & Dep::Or) != Dep::Or)
  264. Group = 0;
  265. // Invert for Conflicts
  266. if (D->Type == Dep::Conflicts)
  267. State = ~State;
  268. }
  269. }
  270. /*}}}*/
  271. // DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
  272. // ---------------------------------------------------------------------
  273. /* This is used to run over a dependency list and determine the dep
  274. state of the list, filtering it through both a Min check and a Policy
  275. check. The return result will have SetMin/SetPolicy low if a check
  276. fails. It uses the DepState cache for it's computations. */
  277. unsigned char pkgDepCache::VersionState(DepIterator D,unsigned char Check,
  278. unsigned char SetMin,
  279. unsigned char SetPolicy)
  280. {
  281. unsigned char Dep = 0xFF;
  282. while (D.end() != true)
  283. {
  284. // Compute a single dependency element (glob or)
  285. DepIterator Start = D;
  286. unsigned char State = 0;
  287. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  288. {
  289. State |= DepState[D->ID];
  290. LastOR = (D->CompareOp & Dep::Or) == Dep::Or;
  291. }
  292. // Minimum deps that must be satisfied to have a working package
  293. if (Start.IsCritical() == true)
  294. if ((State & Check) != Check)
  295. Dep &= ~SetMin;
  296. // Policy deps that must be satisfied to install the package
  297. if (IsImportantDep(Start) == true &&
  298. (State & Check) != Check)
  299. Dep &= ~SetPolicy;
  300. }
  301. return Dep;
  302. }
  303. /*}}}*/
  304. // DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
  305. // ---------------------------------------------------------------------
  306. /* This is the main dependency computation bit. It computes the 3 main
  307. results for a dependencys, Now, Install and Candidate. Callers must
  308. invert the result if dealing with conflicts. */
  309. unsigned char pkgDepCache::DependencyState(DepIterator &D)
  310. {
  311. unsigned char State = 0;
  312. if (CheckDep(D,NowVersion) == true)
  313. State |= DepNow;
  314. if (CheckDep(D,InstallVersion) == true)
  315. State |= DepInstall;
  316. if (CheckDep(D,CandidateVersion) == true)
  317. State |= DepCVer;
  318. return State;
  319. }
  320. /*}}}*/
  321. // DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
  322. // ---------------------------------------------------------------------
  323. /* This determines the combined dependency representation of a package
  324. for its two states now and install. This is done by using the pre-generated
  325. dependency information. */
  326. void pkgDepCache::UpdateVerState(PkgIterator Pkg)
  327. {
  328. // Empty deps are always true
  329. StateCache &State = PkgState[Pkg->ID];
  330. State.DepState = 0xFF;
  331. // Check the Current state
  332. if (Pkg->CurrentVer != 0)
  333. {
  334. DepIterator D = Pkg.CurrentVer().DependsList();
  335. State.DepState &= VersionState(D,DepNow,DepNowMin,DepNowPolicy);
  336. }
  337. /* Check the candidate state. We do not compare against the whole as
  338. a candidate state but check the candidate version against the
  339. install states */
  340. if (State.CandidateVer != 0)
  341. {
  342. DepIterator D = State.CandidateVerIter(*this).DependsList();
  343. State.DepState &= VersionState(D,DepInstall,DepCandMin,DepCandPolicy);
  344. }
  345. // Check target state which can only be current or installed
  346. if (State.InstallVer != 0)
  347. {
  348. DepIterator D = State.InstVerIter(*this).DependsList();
  349. State.DepState &= VersionState(D,DepInstall,DepInstMin,DepInstPolicy);
  350. }
  351. }
  352. /*}}}*/
  353. // DepCache::Update - Figure out all the state information /*{{{*/
  354. // ---------------------------------------------------------------------
  355. /* This will figure out the state of all the packages and all the
  356. dependencies based on the current policy. */
  357. void pkgDepCache::Update(OpProgress *Prog)
  358. {
  359. iUsrSize = 0;
  360. iDownloadSize = 0;
  361. iDelCount = 0;
  362. iInstCount = 0;
  363. iKeepCount = 0;
  364. iBrokenCount = 0;
  365. iBadCount = 0;
  366. // Perform the depends pass
  367. int Done = 0;
  368. for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
  369. {
  370. if (Prog != 0 && Done%20 == 0)
  371. Prog->Progress(Done);
  372. for (VerIterator V = I.VersionList(); V.end() != true; V++)
  373. {
  374. unsigned char Group = 0;
  375. for (DepIterator D = V.DependsList(); D.end() != true; D++)
  376. {
  377. // Build the dependency state.
  378. unsigned char &State = DepState[D->ID];
  379. State = DependencyState(D);;
  380. // Add to the group if we are within an or..
  381. Group |= State;
  382. State |= Group << 3;
  383. if ((D->CompareOp & Dep::Or) != Dep::Or)
  384. Group = 0;
  385. // Invert for Conflicts
  386. if (D->Type == Dep::Conflicts)
  387. State = ~State;
  388. }
  389. }
  390. // Compute the pacakge dependency state and size additions
  391. AddSizes(I);
  392. UpdateVerState(I);
  393. AddStates(I);
  394. }
  395. if (Prog != 0)
  396. Prog->Progress(Done);
  397. }
  398. /*}}}*/
  399. // DepCache::Update - Update the deps list of a package /*{{{*/
  400. // ---------------------------------------------------------------------
  401. /* This is a helper for update that only does the dep portion of the scan.
  402. It is mainly ment to scan reverse dependencies. */
  403. void pkgDepCache::Update(DepIterator D)
  404. {
  405. // Update the reverse deps
  406. for (;D.end() != true; D++)
  407. {
  408. unsigned char &State = DepState[D->ID];
  409. State = DependencyState(D);
  410. // Invert for Conflicts
  411. if (D->Type == Dep::Conflicts)
  412. State = ~State;
  413. RemoveStates(D.ParentPkg());
  414. BuildGroupOrs(D.ParentVer());
  415. UpdateVerState(D.ParentPkg());
  416. AddStates(D.ParentPkg());
  417. }
  418. }
  419. /*}}}*/
  420. // DepCache::Update - Update the related deps of a package /*{{{*/
  421. // ---------------------------------------------------------------------
  422. /* This is called whenever the state of a package changes. It updates
  423. all cached dependencies related to this package. */
  424. void pkgDepCache::Update(PkgIterator const &Pkg)
  425. {
  426. // Recompute the dep of the package
  427. RemoveStates(Pkg);
  428. UpdateVerState(Pkg);
  429. AddStates(Pkg);
  430. // Update the reverse deps
  431. Update(Pkg.RevDependsList());
  432. // Update the provides map for the current ver
  433. if (Pkg->CurrentVer != 0)
  434. for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
  435. P.end() != true; P++)
  436. Update(P.ParentPkg().RevDependsList());
  437. // Update the provides map for the candidate ver
  438. for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
  439. P.end() != true; P++)
  440. Update(P.ParentPkg().RevDependsList());
  441. }
  442. /*}}}*/
  443. // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
  444. // ---------------------------------------------------------------------
  445. /* */
  446. void pkgDepCache::MarkKeep(PkgIterator const &Pkg,bool Soft)
  447. {
  448. // Simplifies other routines.
  449. if (Pkg.end() == true)
  450. return;
  451. /* We changed the soft state all the time so the UI is a bit nicer
  452. to use */
  453. StateCache &P = PkgState[Pkg->ID];
  454. if (Soft == true)
  455. P.iFlags |= AutoKept;
  456. else
  457. P.iFlags &= ~AutoKept;
  458. // Check that it is not already kept
  459. if (P.Mode == ModeKeep)
  460. return;
  461. // We dont even try to keep virtual packages..
  462. if (Pkg->VersionList == 0)
  463. return;
  464. P.Flags &= ~Flag::Auto;
  465. RemoveSizes(Pkg);
  466. RemoveStates(Pkg);
  467. P.Mode = ModeKeep;
  468. if (Pkg->CurrentVer == 0)
  469. P.InstallVer = 0;
  470. else
  471. P.InstallVer = Pkg.CurrentVer();
  472. AddStates(Pkg);
  473. Update(Pkg);
  474. AddSizes(Pkg);
  475. }
  476. /*}}}*/
  477. // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
  478. // ---------------------------------------------------------------------
  479. /* */
  480. void pkgDepCache::MarkDelete(PkgIterator const &Pkg)
  481. {
  482. // Simplifies other routines.
  483. if (Pkg.end() == true)
  484. return;
  485. // Check that it is not already marked for delete
  486. StateCache &P = PkgState[Pkg->ID];
  487. P.iFlags &= ~AutoKept;
  488. if (P.Mode == ModeDelete || P.InstallVer == 0)
  489. return;
  490. // We dont even try to delete virtual packages..
  491. if (Pkg->VersionList == 0)
  492. return;
  493. RemoveSizes(Pkg);
  494. RemoveStates(Pkg);
  495. P.Mode = ModeDelete;
  496. P.InstallVer = 0;
  497. P.Flags &= Flag::Auto;
  498. AddStates(Pkg);
  499. Update(Pkg);
  500. AddSizes(Pkg);
  501. }
  502. /*}}}*/
  503. // DepCache::MarkInstall - Put the package in the install state /*{{{*/
  504. // ---------------------------------------------------------------------
  505. /* */
  506. void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst)
  507. {
  508. // Simplifies other routines.
  509. if (Pkg.end() == true)
  510. return;
  511. /* Check that it is not already marked for install and that it can be
  512. installed */
  513. StateCache &P = PkgState[Pkg->ID];
  514. P.iFlags &= ~AutoKept;
  515. if (P.InstBroken() == false && (P.Mode == ModeInstall ||
  516. P.CandidateVer == (Version *)Pkg.CurrentVer()))
  517. {
  518. if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
  519. MarkKeep(Pkg);
  520. return;
  521. }
  522. // We dont even try to install virtual packages..
  523. if (Pkg->VersionList == 0)
  524. return;
  525. /* Target the candidate version and remove the autoflag. We reset the
  526. autoflag below if this was called recursively. Otherwise the user
  527. should have the ability to de-auto a package by changing its state */
  528. RemoveSizes(Pkg);
  529. RemoveStates(Pkg);
  530. P.Mode = ModeInstall;
  531. P.InstallVer = P.CandidateVer;
  532. P.Flags &= ~Flag::Auto;
  533. if (P.CandidateVer == (Version *)Pkg.CurrentVer())
  534. P.Mode = ModeKeep;
  535. AddStates(Pkg);
  536. Update(Pkg);
  537. AddSizes(Pkg);
  538. if (AutoInst == false)
  539. return;
  540. DepIterator Dep = P.InstVerIter(*this).DependsList();
  541. for (; Dep.end() != true;)
  542. {
  543. // Grok or groups
  544. DepIterator Start = Dep;
  545. bool Result = true;
  546. for (bool LastOR = true; Dep.end() == false && LastOR == true; Dep++)
  547. {
  548. LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
  549. if ((DepState[Dep->ID] & DepInstall) == DepInstall)
  550. Result = false;
  551. }
  552. // Dep is satisfied okay.
  553. if (Result == false)
  554. continue;
  555. /* Check if this dep should be consider for install. If it is a user
  556. defined important dep and we are installed a new package then
  557. it will be installed. Otherwise we only worry about critical deps */
  558. if (IsImportantDep(Start) == false)
  559. continue;
  560. if (Pkg->CurrentVer != 0 && Start.IsCritical() == false)
  561. continue;
  562. // Now we have to take action...
  563. PkgIterator P = Start.SmartTargetPkg();
  564. if ((DepState[Start->ID] & DepCVer) == DepCVer)
  565. {
  566. MarkInstall(P,true);
  567. // Set the autoflag, after MarkInstall because MarkInstall unsets it
  568. if (P->CurrentVer == 0)
  569. PkgState[P->ID].Flags |= Flag::Auto;
  570. continue;
  571. }
  572. // For conflicts we just de-install the package and mark as auto
  573. if (Start->Type == Dep::Conflicts)
  574. {
  575. Version **List = Start.AllTargets();
  576. for (Version **I = List; *I != 0; I++)
  577. {
  578. VerIterator Ver(*this,*I);
  579. PkgIterator Pkg = Ver.ParentPkg();
  580. MarkDelete(Pkg);
  581. PkgState[Pkg->ID].Flags |= Flag::Auto;
  582. }
  583. delete [] List;
  584. continue;
  585. }
  586. }
  587. }
  588. /*}}}*/
  589. #if 0
  590. // DepCache::ResolveConflicts - Figure the auto upgrades /*{{{*/
  591. // ---------------------------------------------------------------------
  592. /* This routine attempts to resolve conflicts generated by automatic
  593. upgrading. It is known as 'Stage 1' but that name isn't as proper anymore.
  594. It's most important function is during the initial load of APT. The
  595. loading code will mark every package for upgrade to it's candidate version
  596. and then call this routine. This routine will then 'soft keep' every
  597. package that causes conflict, is conflicted, or so on. It is a bit
  598. agressive in that it may unselect more packages in some odd cases
  599. than are strictly necessary but in the case where no packages were
  600. conflicting before it will always produce a system with no packages
  601. conflicting after.
  602. This routine is also used during state changes that require autoupgrade
  603. scanning. That is, if a new package is marked for install then all packages
  604. that have been soft kept are reconsidered for upgrade.
  605. It is called with state information about what can be un-upgraded and
  606. what the pre-upgrade install state was. It is expected that the caller
  607. has already marked the desired packages to the install state. Bit 0 is
  608. the original install state and Bit 1 is controls whether the package
  609. should be touched.
  610. */
  611. void pkgDepCache::ResolveConflicts(unsigned char *Touched)
  612. {
  613. bool Again = false;
  614. do
  615. {
  616. Again = false;
  617. for (PkgIterator I = PkgBegin(); I.end() != true; I++)
  618. {
  619. // The package will install OK
  620. if ((PkgState[I->ID].DepState & DepInstMin) == DepInstMin)
  621. continue;
  622. /* The package was broken before and this upgrade will not
  623. make things better. We simply mark the package for keep
  624. and assume an upgrade attempt will be hopeless. This might
  625. not be ideal. */
  626. if ((Touched[I->ID] & (1 << 0)) != (1 << 0))
  627. {
  628. // The package isnt to be touched
  629. if ((Touched[I->ID] & (1 << 1)) == (1 << 1))
  630. MarkKeep(I,true);
  631. continue;
  632. }
  633. // Check to see if not upgrading it will solve the problem
  634. if (I->CurrentVer != 0)
  635. {
  636. // The package isnt to be touched
  637. if ((Touched[I->ID] & (1 << 1)) == (1 << 1))
  638. {
  639. if (PkgState[I->ID].Mode != ModeKeep)
  640. Again = true;
  641. MarkKeep(I,true);
  642. }
  643. /* Check if the package is sill broken. If so then we cant leave
  644. it as is and get a working system. Lets try marking some
  645. depends for 'keep'. This is brutal, it keeps everything in
  646. sight to fix the problem. */
  647. DepIterator D = I.CurrentVer().DependsList();
  648. for (;(PkgState[I->ID].DepState & DepInstMin) != DepInstMin &&
  649. D.end() != true; D++)
  650. {
  651. // We only worry about critical deps.
  652. if (D.IsCritical() != true)
  653. continue;
  654. unsigned char State = DepState[D->ID];
  655. // This dep never was set before so we dont need to set it now
  656. if ((State & DepNow) != DepNow)
  657. continue;
  658. // The dep is okay now no worries.
  659. if ((State & DepInstall) == DepInstall)
  660. continue;
  661. // Locate a target to keep
  662. PkgIterator P(*this);
  663. if (CheckDep(D,NowVersion,P) == true)
  664. {
  665. // We cant touch this package
  666. if ((Touched[P->ID] & (1 << 1)) == (1 << 1))
  667. MarkKeep(P,true);
  668. }
  669. }
  670. }
  671. }
  672. }
  673. while (Again == true);
  674. }
  675. /*}}}*/
  676. // DepCache::PromoteAutoKeep - Gentler version of the above /*{{{*/
  677. // ---------------------------------------------------------------------
  678. /* This is used when installing packages, all it does is attempt to promote
  679. everything that has been auto-kept. It simply promotes everything
  680. irregardless of it having a chance to work and then runs ResolveConflicts
  681. on the result. This allows circular depends loops to work but isn't
  682. terribly fast. */
  683. void pkgDepCache::PromoteAutoKeep()
  684. {
  685. /* Initialize the touchd array. Bit 0 is the old install state bit 1
  686. is the touch value */
  687. unsigned char *Touch = new unsigned char[Head().PackageCount];
  688. for (unsigned int I = 0; I != Head().PackageCount; I++)
  689. {
  690. if ((PkgState[I].DepState & DepInstMin) == DepInstMin)
  691. Touch[I] = 1 << 0;
  692. else
  693. Touch[I] = 0;
  694. }
  695. // This allows circular depends to work
  696. for (PkgIterator I = PkgBegin(); I.end() != true; I++)
  697. {
  698. /* It wasnt installed before or it is not autokept or it is not
  699. upgradeable */
  700. StateCache &P = PkgState[I->ID];
  701. if (I->CurrentVer == 0 || P.Mode != ModeKeep || I->VersionList == 0 ||
  702. P.CandidateVer == (Version *)I.CurrentVer() ||
  703. (P.iFlags & AutoKept) != AutoKept)
  704. continue;
  705. P.Mode = ModeInstall;
  706. P.InstallVer = P.CandidateVer;
  707. if (P.CandidateVer == (Version *)I.CurrentVer())
  708. P.Mode = ModeKeep;
  709. // Okay autoupgrade it.
  710. Touch[I->ID] |= 1 << 1;
  711. }
  712. Update();
  713. ResolveConflicts(Touch);
  714. delete [] Touch;
  715. }
  716. /*}}}*/
  717. // DepCache::AllUpgrade - Try to upgrade everything /*{{{*/
  718. // ---------------------------------------------------------------------
  719. /* */
  720. void pkgDepCache::AllUpgrade()
  721. {
  722. // Set everything to an upgrade mode
  723. for (PkgIterator I = PkgBegin(); I.end() != true; I++)
  724. {
  725. StateCache &State = PkgState[I->ID];
  726. /* We dont upgrade packages marked for deletion or that are
  727. not installed or that don't have an upgrade */
  728. if (State.Mode == ModeDelete || I->CurrentVer == 0 ||
  729. (Version *)I.CurrentVer() == State.CandidateVer)
  730. continue;
  731. // Set the state to upgrade
  732. State.iFlags = 0;
  733. State.Mode = ModeInstall;
  734. State.InstallVer = State.CandidateVer;
  735. if (State.CandidateVer == (Version *)I.CurrentVer())
  736. State.Mode = ModeKeep;
  737. // Do not upgrade things that have the hold flag set.
  738. if (I->SelectedState == State::Hold)
  739. {
  740. State.InstallVer = I.CurrentVer();
  741. State.Mode = ModeKeep;
  742. }
  743. State.Update(I,*this);
  744. }
  745. Update();
  746. /* Initialize the touchd array. Bit 0 is the old install state bit 1
  747. is the touch value */
  748. unsigned char *Touch = new unsigned char[Head().PackageCount];
  749. for (unsigned int I = 0; I != Head().PackageCount; I++)
  750. {
  751. if ((PkgState[I].DepState & DepNowMin) == DepNowMin)
  752. Touch[I] = (1 << 0) | (1 << 1);
  753. else
  754. Touch[I] = 1 << 1;
  755. }
  756. // Now downgrade everything that is broken
  757. ResolveConflicts(Touch);
  758. delete [] Touch;
  759. }
  760. /*}}}*/
  761. #endif
  762. // StateCache::Update - Compute the various static display things /*{{{*/
  763. // ---------------------------------------------------------------------
  764. /* This is called whenever the Candidate version changes. */
  765. void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
  766. {
  767. // Some info
  768. VerIterator Ver = CandidateVerIter(Cache);
  769. // Use a null string or the version string
  770. if (Ver.end() == true)
  771. CandVersion = "";
  772. else
  773. CandVersion = Ver.VerStr();
  774. // Find the current version
  775. CurVersion = "";
  776. if (Pkg->CurrentVer != 0)
  777. CurVersion = Pkg.CurrentVer().VerStr();
  778. // Strip off the epochs for display
  779. CurVersion = StripEpoch(CurVersion);
  780. CandVersion = StripEpoch(CandVersion);
  781. // Figure out if its up or down or equal
  782. Status = Ver.CompareVer(Pkg.CurrentVer());
  783. if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
  784. Status = 2;
  785. }
  786. /*}}}*/
  787. // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
  788. // ---------------------------------------------------------------------
  789. /* */
  790. const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
  791. {
  792. if (Ver == 0)
  793. return 0;
  794. // Strip any epoch
  795. for (const char *I = Ver; *I != 0; I++)
  796. if (*I == ':')
  797. return I + 1;
  798. return Ver;
  799. }
  800. /*}}}*/