depcache.cc 27 KB

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