depcache.cc 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  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. #include <apt-pkg/depcache.h>
  10. #include <apt-pkg/version.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/sptr.h>
  13. #include <apt-pkg/algorithms.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/pkgsystem.h>
  17. #include <apt-pkg/tagfile.h>
  18. #include <iostream>
  19. #include <sstream>
  20. #include <set>
  21. #include <sys/stat.h>
  22. #include <apti18n.h>
  23. pkgDepCache::ActionGroup::ActionGroup(pkgDepCache &cache) :
  24. cache(cache), released(false)
  25. {
  26. ++cache.group_level;
  27. }
  28. void pkgDepCache::ActionGroup::release()
  29. {
  30. if(!released)
  31. {
  32. if(cache.group_level == 0)
  33. std::cerr << "W: Unbalanced action groups, expect badness" << std::endl;
  34. else
  35. {
  36. --cache.group_level;
  37. if(cache.group_level == 0)
  38. cache.MarkAndSweep();
  39. }
  40. released = false;
  41. }
  42. }
  43. pkgDepCache::ActionGroup::~ActionGroup()
  44. {
  45. release();
  46. }
  47. // DepCache::pkgDepCache - Constructors /*{{{*/
  48. // ---------------------------------------------------------------------
  49. /* */
  50. pkgDepCache::pkgDepCache(pkgCache *pCache,Policy *Plcy) :
  51. group_level(0), Cache(pCache), PkgState(0), DepState(0)
  52. {
  53. delLocalPolicy = 0;
  54. LocalPolicy = Plcy;
  55. if (LocalPolicy == 0)
  56. delLocalPolicy = LocalPolicy = new Policy;
  57. }
  58. /*}}}*/
  59. // DepCache::~pkgDepCache - Destructor /*{{{*/
  60. // ---------------------------------------------------------------------
  61. /* */
  62. pkgDepCache::~pkgDepCache()
  63. {
  64. delete [] PkgState;
  65. delete [] DepState;
  66. delete delLocalPolicy;
  67. }
  68. /*}}}*/
  69. // DepCache::Init - Generate the initial extra structures. /*{{{*/
  70. // ---------------------------------------------------------------------
  71. /* This allocats the extension buffers and initializes them. */
  72. bool pkgDepCache::Init(OpProgress *Prog)
  73. {
  74. // Suppress mark updates during this operation (just in case) and
  75. // run a mark operation when Init terminates.
  76. ActionGroup actions(*this);
  77. delete [] PkgState;
  78. delete [] DepState;
  79. PkgState = new StateCache[Head().PackageCount];
  80. DepState = new unsigned char[Head().DependsCount];
  81. memset(PkgState,0,sizeof(*PkgState)*Head().PackageCount);
  82. memset(DepState,0,sizeof(*DepState)*Head().DependsCount);
  83. if (Prog != 0)
  84. {
  85. Prog->OverallProgress(0,2*Head().PackageCount,Head().PackageCount,
  86. _("Building dependency tree"));
  87. Prog->SubProgress(Head().PackageCount,_("Candidate versions"));
  88. }
  89. /* Set the current state of everything. In this state all of the
  90. packages are kept exactly as is. See AllUpgrade */
  91. int Done = 0;
  92. for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
  93. {
  94. if (Prog != 0)
  95. Prog->Progress(Done);
  96. // Find the proper cache slot
  97. StateCache &State = PkgState[I->ID];
  98. State.iFlags = 0;
  99. // Figure out the install version
  100. State.CandidateVer = GetCandidateVer(I);
  101. State.InstallVer = I.CurrentVer();
  102. State.Mode = ModeKeep;
  103. State.Update(I,*this);
  104. }
  105. if (Prog != 0)
  106. {
  107. Prog->OverallProgress(Head().PackageCount,2*Head().PackageCount,
  108. Head().PackageCount,
  109. _("Building dependency tree"));
  110. Prog->SubProgress(Head().PackageCount,_("Dependency generation"));
  111. }
  112. Update(Prog);
  113. if(Prog != 0)
  114. Prog->Done();
  115. return true;
  116. }
  117. /*}}}*/
  118. bool pkgDepCache::readStateFile(OpProgress *Prog)
  119. {
  120. FileFd state_file;
  121. string state = _config->FindDir("Dir::State") + "extended_states";
  122. if(FileExists(state)) {
  123. state_file.Open(state, FileFd::ReadOnly);
  124. int file_size = state_file.Size();
  125. if(Prog != NULL)
  126. Prog->OverallProgress(0, file_size, 1,
  127. _("Reading state information"));
  128. pkgTagFile tagfile(&state_file);
  129. pkgTagSection section;
  130. int amt=0;
  131. while(tagfile.Step(section)) {
  132. string pkgname = section.FindS("Package");
  133. pkgCache::PkgIterator pkg=Cache->FindPkg(pkgname);
  134. // Silently ignore unknown packages and packages with no actual
  135. // version.
  136. if(!pkg.end() && !pkg.VersionList().end()) {
  137. short reason = section.FindI("Auto-Installed", 0);
  138. if(reason > 0)
  139. PkgState[pkg->ID].Flags |= Flag::Auto;
  140. if(_config->FindB("Debug::pkgAutoRemove",false))
  141. std::cout << "Auto-Installed : " << pkgname << std::endl;
  142. amt+=section.size();
  143. if(Prog != NULL)
  144. Prog->OverallProgress(amt, file_size, 1,
  145. _("Reading state information"));
  146. }
  147. if(Prog != NULL)
  148. Prog->OverallProgress(file_size, file_size, 1,
  149. _("Reading state information"));
  150. }
  151. }
  152. return true;
  153. }
  154. bool pkgDepCache::writeStateFile(OpProgress *prog, bool InstalledOnly)
  155. {
  156. if(_config->FindB("Debug::pkgAutoRemove",false))
  157. std::clog << "pkgDepCache::writeStateFile()" << std::endl;
  158. FileFd StateFile;
  159. string state = _config->FindDir("Dir::State") + "extended_states";
  160. // if it does not exist, create a empty one
  161. if(!FileExists(state))
  162. {
  163. StateFile.Open(state, FileFd::WriteEmpty);
  164. StateFile.Close();
  165. }
  166. // open it
  167. if(!StateFile.Open(state, FileFd::ReadOnly))
  168. return _error->Error(_("Failed to open StateFile %s"),
  169. state.c_str());
  170. FILE *OutFile;
  171. string outfile = state + ".tmp";
  172. if((OutFile = fopen(outfile.c_str(),"w")) == NULL)
  173. return _error->Error(_("Failed to write temporary StateFile %s"),
  174. outfile.c_str());
  175. // first merge with the existing sections
  176. pkgTagFile tagfile(&StateFile);
  177. pkgTagSection section;
  178. std::set<string> pkgs_seen;
  179. const char *nullreorderlist[] = {0};
  180. while(tagfile.Step(section)) {
  181. string pkgname = section.FindS("Package");
  182. // Silently ignore unknown packages and packages with no actual
  183. // version.
  184. pkgCache::PkgIterator pkg=Cache->FindPkg(pkgname);
  185. if(pkg.end() || pkg.VersionList().end())
  186. continue;
  187. bool newAuto = (PkgState[pkg->ID].Flags & Flag::Auto);
  188. if(_config->FindB("Debug::pkgAutoRemove",false))
  189. std::clog << "Update exisiting AutoInstall info: "
  190. << pkg.Name() << std::endl;
  191. TFRewriteData rewrite[2];
  192. rewrite[0].Tag = "Auto-Installed";
  193. rewrite[0].Rewrite = newAuto ? "1" : "0";
  194. rewrite[0].NewTag = 0;
  195. rewrite[1].Tag = 0;
  196. TFRewrite(OutFile, section, nullreorderlist, rewrite);
  197. fprintf(OutFile,"\n");
  198. pkgs_seen.insert(pkgname);
  199. }
  200. // then write the ones we have not seen yet
  201. std::ostringstream ostr;
  202. for(pkgCache::PkgIterator pkg=Cache->PkgBegin(); !pkg.end(); pkg++) {
  203. if(PkgState[pkg->ID].Flags & Flag::Auto) {
  204. if (pkgs_seen.find(pkg.Name()) != pkgs_seen.end()) {
  205. if(_config->FindB("Debug::pkgAutoRemove",false))
  206. std::clog << "Skipping already written " << pkg.Name() << std::endl;
  207. continue;
  208. }
  209. // skip not installed ones if requested
  210. if(InstalledOnly && pkg->CurrentVer == 0)
  211. continue;
  212. if(_config->FindB("Debug::pkgAutoRemove",false))
  213. std::clog << "Writing new AutoInstall: "
  214. << pkg.Name() << std::endl;
  215. ostr.str(string(""));
  216. ostr << "Package: " << pkg.Name()
  217. << "\nAuto-Installed: 1\n\n";
  218. fprintf(OutFile,ostr.str().c_str());
  219. fprintf(OutFile,"\n");
  220. }
  221. }
  222. fclose(OutFile);
  223. // move the outfile over the real file and set permissions
  224. rename(outfile.c_str(), state.c_str());
  225. chmod(state.c_str(), 0644);
  226. return true;
  227. }
  228. // DepCache::CheckDep - Checks a single dependency /*{{{*/
  229. // ---------------------------------------------------------------------
  230. /* This first checks the dependency against the main target package and
  231. then walks along the package provides list and checks if each provides
  232. will be installed then checks the provides against the dep. Res will be
  233. set to the package which was used to satisfy the dep. */
  234. bool pkgDepCache::CheckDep(DepIterator Dep,int Type,PkgIterator &Res)
  235. {
  236. Res = Dep.TargetPkg();
  237. /* Check simple depends. A depends -should- never self match but
  238. we allow it anyhow because dpkg does. Technically it is a packaging
  239. bug. Conflicts may never self match */
  240. if (Dep.TargetPkg() != Dep.ParentPkg() ||
  241. (Dep->Type != Dep::Conflicts && Dep->Type != Dep::DpkgBreaks && Dep->Type != Dep::Obsoletes))
  242. {
  243. PkgIterator Pkg = Dep.TargetPkg();
  244. // Check the base package
  245. if (Type == NowVersion && Pkg->CurrentVer != 0)
  246. if (VS().CheckDep(Pkg.CurrentVer().VerStr(),Dep->CompareOp,
  247. Dep.TargetVer()) == true)
  248. return true;
  249. if (Type == InstallVersion && PkgState[Pkg->ID].InstallVer != 0)
  250. if (VS().CheckDep(PkgState[Pkg->ID].InstVerIter(*this).VerStr(),
  251. Dep->CompareOp,Dep.TargetVer()) == true)
  252. return true;
  253. if (Type == CandidateVersion && PkgState[Pkg->ID].CandidateVer != 0)
  254. if (VS().CheckDep(PkgState[Pkg->ID].CandidateVerIter(*this).VerStr(),
  255. Dep->CompareOp,Dep.TargetVer()) == true)
  256. return true;
  257. }
  258. if (Dep->Type == Dep::Obsoletes)
  259. return false;
  260. // Check the providing packages
  261. PrvIterator P = Dep.TargetPkg().ProvidesList();
  262. PkgIterator Pkg = Dep.ParentPkg();
  263. for (; P.end() != true; P++)
  264. {
  265. /* Provides may never be applied against the same package if it is
  266. a conflicts. See the comment above. */
  267. if (P.OwnerPkg() == Pkg &&
  268. (Dep->Type == Dep::Conflicts || Dep->Type == Dep::DpkgBreaks))
  269. continue;
  270. // Check if the provides is a hit
  271. if (Type == NowVersion)
  272. {
  273. if (P.OwnerPkg().CurrentVer() != P.OwnerVer())
  274. continue;
  275. }
  276. if (Type == InstallVersion)
  277. {
  278. StateCache &State = PkgState[P.OwnerPkg()->ID];
  279. if (State.InstallVer != (Version *)P.OwnerVer())
  280. continue;
  281. }
  282. if (Type == CandidateVersion)
  283. {
  284. StateCache &State = PkgState[P.OwnerPkg()->ID];
  285. if (State.CandidateVer != (Version *)P.OwnerVer())
  286. continue;
  287. }
  288. // Compare the versions.
  289. if (VS().CheckDep(P.ProvideVersion(),Dep->CompareOp,Dep.TargetVer()) == true)
  290. {
  291. Res = P.OwnerPkg();
  292. return true;
  293. }
  294. }
  295. return false;
  296. }
  297. /*}}}*/
  298. // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
  299. // ---------------------------------------------------------------------
  300. /* Call with Mult = -1 to preform the inverse opration */
  301. void pkgDepCache::AddSizes(const PkgIterator &Pkg,signed long Mult)
  302. {
  303. StateCache &P = PkgState[Pkg->ID];
  304. if (Pkg->VersionList == 0)
  305. return;
  306. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  307. P.Keep() == true)
  308. return;
  309. // Compute the size data
  310. if (P.NewInstall() == true)
  311. {
  312. iUsrSize += (signed)(Mult*P.InstVerIter(*this)->InstalledSize);
  313. iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
  314. return;
  315. }
  316. // Upgrading
  317. if (Pkg->CurrentVer != 0 &&
  318. (P.InstallVer != (Version *)Pkg.CurrentVer() ||
  319. (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
  320. {
  321. iUsrSize += (signed)(Mult*((signed)P.InstVerIter(*this)->InstalledSize -
  322. (signed)Pkg.CurrentVer()->InstalledSize));
  323. iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
  324. return;
  325. }
  326. // Reinstall
  327. if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
  328. P.Delete() == false)
  329. {
  330. iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
  331. return;
  332. }
  333. // Removing
  334. if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
  335. {
  336. iUsrSize -= (signed)(Mult*Pkg.CurrentVer()->InstalledSize);
  337. return;
  338. }
  339. }
  340. /*}}}*/
  341. // DepCache::AddStates - Add the package to the state counter /*{{{*/
  342. // ---------------------------------------------------------------------
  343. /* This routine is tricky to use, you must make sure that it is never
  344. called twice for the same package. This means the Remove/Add section
  345. should be as short as possible and not encompass any code that will
  346. calld Remove/Add itself. Remember, dependencies can be circular so
  347. while processing a dep for Pkg it is possible that Add/Remove
  348. will be called on Pkg */
  349. void pkgDepCache::AddStates(const PkgIterator &Pkg,int Add)
  350. {
  351. StateCache &State = PkgState[Pkg->ID];
  352. // The Package is broken (either minimal dep or policy dep)
  353. if ((State.DepState & DepInstMin) != DepInstMin)
  354. iBrokenCount += Add;
  355. if ((State.DepState & DepInstPolicy) != DepInstPolicy)
  356. iPolicyBrokenCount += Add;
  357. // Bad state
  358. if (Pkg.State() != PkgIterator::NeedsNothing)
  359. iBadCount += Add;
  360. // Not installed
  361. if (Pkg->CurrentVer == 0)
  362. {
  363. if (State.Mode == ModeDelete &&
  364. (State.iFlags | Purge) == Purge && Pkg.Purge() == false)
  365. iDelCount += Add;
  366. if (State.Mode == ModeInstall)
  367. iInstCount += Add;
  368. return;
  369. }
  370. // Installed, no upgrade
  371. if (State.Status == 0)
  372. {
  373. if (State.Mode == ModeDelete)
  374. iDelCount += Add;
  375. else
  376. if ((State.iFlags & ReInstall) == ReInstall)
  377. iInstCount += Add;
  378. return;
  379. }
  380. // Alll 3 are possible
  381. if (State.Mode == ModeDelete)
  382. iDelCount += Add;
  383. if (State.Mode == ModeKeep)
  384. iKeepCount += Add;
  385. if (State.Mode == ModeInstall)
  386. iInstCount += Add;
  387. }
  388. /*}}}*/
  389. // DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
  390. // ---------------------------------------------------------------------
  391. /* The or group results are stored in the last item of the or group. This
  392. allows easy detection of the state of a whole or'd group. */
  393. void pkgDepCache::BuildGroupOrs(VerIterator const &V)
  394. {
  395. unsigned char Group = 0;
  396. for (DepIterator D = V.DependsList(); D.end() != true; D++)
  397. {
  398. // Build the dependency state.
  399. unsigned char &State = DepState[D->ID];
  400. /* Invert for Conflicts. We have to do this twice to get the
  401. right sense for a conflicts group */
  402. if (D->Type == Dep::Conflicts ||
  403. D->Type == Dep::DpkgBreaks ||
  404. D->Type == Dep::Obsoletes)
  405. State = ~State;
  406. // Add to the group if we are within an or..
  407. State &= 0x7;
  408. Group |= State;
  409. State |= Group << 3;
  410. if ((D->CompareOp & Dep::Or) != Dep::Or)
  411. Group = 0;
  412. // Invert for Conflicts
  413. if (D->Type == Dep::Conflicts ||
  414. D->Type == Dep::DpkgBreaks ||
  415. D->Type == Dep::Obsoletes)
  416. State = ~State;
  417. }
  418. }
  419. /*}}}*/
  420. // DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
  421. // ---------------------------------------------------------------------
  422. /* This is used to run over a dependency list and determine the dep
  423. state of the list, filtering it through both a Min check and a Policy
  424. check. The return result will have SetMin/SetPolicy low if a check
  425. fails. It uses the DepState cache for it's computations. */
  426. unsigned char pkgDepCache::VersionState(DepIterator D,unsigned char Check,
  427. unsigned char SetMin,
  428. unsigned char SetPolicy)
  429. {
  430. unsigned char Dep = 0xFF;
  431. while (D.end() != true)
  432. {
  433. // Compute a single dependency element (glob or)
  434. DepIterator Start = D;
  435. unsigned char State = 0;
  436. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  437. {
  438. State |= DepState[D->ID];
  439. LastOR = (D->CompareOp & Dep::Or) == Dep::Or;
  440. }
  441. // Minimum deps that must be satisfied to have a working package
  442. if (Start.IsCritical() == true)
  443. if ((State & Check) != Check)
  444. Dep &= ~SetMin;
  445. // Policy deps that must be satisfied to install the package
  446. if (IsImportantDep(Start) == true &&
  447. (State & Check) != Check)
  448. Dep &= ~SetPolicy;
  449. }
  450. return Dep;
  451. }
  452. /*}}}*/
  453. // DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
  454. // ---------------------------------------------------------------------
  455. /* This is the main dependency computation bit. It computes the 3 main
  456. results for a dependencys, Now, Install and Candidate. Callers must
  457. invert the result if dealing with conflicts. */
  458. unsigned char pkgDepCache::DependencyState(DepIterator &D)
  459. {
  460. unsigned char State = 0;
  461. if (CheckDep(D,NowVersion) == true)
  462. State |= DepNow;
  463. if (CheckDep(D,InstallVersion) == true)
  464. State |= DepInstall;
  465. if (CheckDep(D,CandidateVersion) == true)
  466. State |= DepCVer;
  467. return State;
  468. }
  469. /*}}}*/
  470. // DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
  471. // ---------------------------------------------------------------------
  472. /* This determines the combined dependency representation of a package
  473. for its two states now and install. This is done by using the pre-generated
  474. dependency information. */
  475. void pkgDepCache::UpdateVerState(PkgIterator Pkg)
  476. {
  477. // Empty deps are always true
  478. StateCache &State = PkgState[Pkg->ID];
  479. State.DepState = 0xFF;
  480. // Check the Current state
  481. if (Pkg->CurrentVer != 0)
  482. {
  483. DepIterator D = Pkg.CurrentVer().DependsList();
  484. State.DepState &= VersionState(D,DepNow,DepNowMin,DepNowPolicy);
  485. }
  486. /* Check the candidate state. We do not compare against the whole as
  487. a candidate state but check the candidate version against the
  488. install states */
  489. if (State.CandidateVer != 0)
  490. {
  491. DepIterator D = State.CandidateVerIter(*this).DependsList();
  492. State.DepState &= VersionState(D,DepInstall,DepCandMin,DepCandPolicy);
  493. }
  494. // Check target state which can only be current or installed
  495. if (State.InstallVer != 0)
  496. {
  497. DepIterator D = State.InstVerIter(*this).DependsList();
  498. State.DepState &= VersionState(D,DepInstall,DepInstMin,DepInstPolicy);
  499. }
  500. }
  501. /*}}}*/
  502. // DepCache::Update - Figure out all the state information /*{{{*/
  503. // ---------------------------------------------------------------------
  504. /* This will figure out the state of all the packages and all the
  505. dependencies based on the current policy. */
  506. void pkgDepCache::Update(OpProgress *Prog)
  507. {
  508. iUsrSize = 0;
  509. iDownloadSize = 0;
  510. iDelCount = 0;
  511. iInstCount = 0;
  512. iKeepCount = 0;
  513. iBrokenCount = 0;
  514. iBadCount = 0;
  515. // Perform the depends pass
  516. int Done = 0;
  517. for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
  518. {
  519. if (Prog != 0 && Done%20 == 0)
  520. Prog->Progress(Done);
  521. for (VerIterator V = I.VersionList(); V.end() != true; V++)
  522. {
  523. unsigned char Group = 0;
  524. for (DepIterator D = V.DependsList(); D.end() != true; D++)
  525. {
  526. // Build the dependency state.
  527. unsigned char &State = DepState[D->ID];
  528. State = DependencyState(D);
  529. // Add to the group if we are within an or..
  530. Group |= State;
  531. State |= Group << 3;
  532. if ((D->CompareOp & Dep::Or) != Dep::Or)
  533. Group = 0;
  534. // Invert for Conflicts
  535. if (D->Type == Dep::Conflicts ||
  536. D->Type == Dep::DpkgBreaks ||
  537. D->Type == Dep::Obsoletes)
  538. State = ~State;
  539. }
  540. }
  541. // Compute the pacakge dependency state and size additions
  542. AddSizes(I);
  543. UpdateVerState(I);
  544. AddStates(I);
  545. }
  546. if (Prog != 0)
  547. Prog->Progress(Done);
  548. readStateFile(Prog);
  549. }
  550. /*}}}*/
  551. // DepCache::Update - Update the deps list of a package /*{{{*/
  552. // ---------------------------------------------------------------------
  553. /* This is a helper for update that only does the dep portion of the scan.
  554. It is mainly meant to scan reverse dependencies. */
  555. void pkgDepCache::Update(DepIterator D)
  556. {
  557. // Update the reverse deps
  558. for (;D.end() != true; D++)
  559. {
  560. unsigned char &State = DepState[D->ID];
  561. State = DependencyState(D);
  562. // Invert for Conflicts
  563. if (D->Type == Dep::Conflicts ||
  564. D->Type == Dep::DpkgBreaks ||
  565. D->Type == Dep::Obsoletes)
  566. State = ~State;
  567. RemoveStates(D.ParentPkg());
  568. BuildGroupOrs(D.ParentVer());
  569. UpdateVerState(D.ParentPkg());
  570. AddStates(D.ParentPkg());
  571. }
  572. }
  573. /*}}}*/
  574. // DepCache::Update - Update the related deps of a package /*{{{*/
  575. // ---------------------------------------------------------------------
  576. /* This is called whenever the state of a package changes. It updates
  577. all cached dependencies related to this package. */
  578. void pkgDepCache::Update(PkgIterator const &Pkg)
  579. {
  580. // Recompute the dep of the package
  581. RemoveStates(Pkg);
  582. UpdateVerState(Pkg);
  583. AddStates(Pkg);
  584. // Update the reverse deps
  585. Update(Pkg.RevDependsList());
  586. // Update the provides map for the current ver
  587. if (Pkg->CurrentVer != 0)
  588. for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
  589. P.end() != true; P++)
  590. Update(P.ParentPkg().RevDependsList());
  591. // Update the provides map for the candidate ver
  592. if (PkgState[Pkg->ID].CandidateVer != 0)
  593. for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
  594. P.end() != true; P++)
  595. Update(P.ParentPkg().RevDependsList());
  596. }
  597. /*}}}*/
  598. // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
  599. // ---------------------------------------------------------------------
  600. /* */
  601. void pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser)
  602. {
  603. // Simplifies other routines.
  604. if (Pkg.end() == true)
  605. return;
  606. /* Reject an attempt to keep a non-source broken installed package, those
  607. must be upgraded */
  608. if (Pkg.State() == PkgIterator::NeedsUnpack &&
  609. Pkg.CurrentVer().Downloadable() == false)
  610. return;
  611. /** \todo Can this be moved later in the method? */
  612. ActionGroup group(*this);
  613. /* We changed the soft state all the time so the UI is a bit nicer
  614. to use */
  615. StateCache &P = PkgState[Pkg->ID];
  616. if (Soft == true)
  617. P.iFlags |= AutoKept;
  618. else
  619. P.iFlags &= ~AutoKept;
  620. // Check that it is not already kept
  621. if (P.Mode == ModeKeep)
  622. return;
  623. // We dont even try to keep virtual packages..
  624. if (Pkg->VersionList == 0)
  625. return;
  626. #if 0 // reseting the autoflag here means we lose the
  627. // auto-mark information if a user selects a package for removal
  628. // but changes his mind then and sets it for keep again
  629. // - this makes sense as default when all Garbage dependencies
  630. // are automatically marked for removal (as aptitude does).
  631. // setting a package for keep then makes it no longer autoinstalled
  632. // for all other use-case this action is rather suprising
  633. if(FromUser && !P.Marked)
  634. P.Flags &= ~Flag::Auto;
  635. #endif
  636. RemoveSizes(Pkg);
  637. RemoveStates(Pkg);
  638. P.Mode = ModeKeep;
  639. if (Pkg->CurrentVer == 0)
  640. P.InstallVer = 0;
  641. else
  642. P.InstallVer = Pkg.CurrentVer();
  643. AddStates(Pkg);
  644. Update(Pkg);
  645. AddSizes(Pkg);
  646. }
  647. /*}}}*/
  648. // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
  649. // ---------------------------------------------------------------------
  650. /* */
  651. void pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge)
  652. {
  653. // Simplifies other routines.
  654. if (Pkg.end() == true)
  655. return;
  656. ActionGroup group(*this);
  657. // Check that it is not already marked for delete
  658. StateCache &P = PkgState[Pkg->ID];
  659. P.iFlags &= ~(AutoKept | Purge);
  660. if (rPurge == true)
  661. P.iFlags |= Purge;
  662. if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
  663. (Pkg.Purge() == true || rPurge == false))
  664. return;
  665. // We dont even try to delete virtual packages..
  666. if (Pkg->VersionList == 0)
  667. return;
  668. RemoveSizes(Pkg);
  669. RemoveStates(Pkg);
  670. if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
  671. P.Mode = ModeKeep;
  672. else
  673. P.Mode = ModeDelete;
  674. P.InstallVer = 0;
  675. AddStates(Pkg);
  676. Update(Pkg);
  677. AddSizes(Pkg);
  678. }
  679. /*}}}*/
  680. // DepCache::MarkInstall - Put the package in the install state /*{{{*/
  681. // ---------------------------------------------------------------------
  682. /* */
  683. void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
  684. unsigned long Depth, bool FromUser,
  685. bool ForceImportantDeps)
  686. {
  687. if (Depth > 100)
  688. return;
  689. // Simplifies other routines.
  690. if (Pkg.end() == true)
  691. return;
  692. ActionGroup group(*this);
  693. /* Check that it is not already marked for install and that it can be
  694. installed */
  695. StateCache &P = PkgState[Pkg->ID];
  696. P.iFlags &= ~AutoKept;
  697. if ((P.InstPolicyBroken() == false && P.InstBroken() == false) &&
  698. (P.Mode == ModeInstall ||
  699. P.CandidateVer == (Version *)Pkg.CurrentVer()))
  700. {
  701. if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
  702. MarkKeep(Pkg, false, FromUser);
  703. return;
  704. }
  705. // See if there is even any possible instalation candidate
  706. if (P.CandidateVer == 0)
  707. return;
  708. // We dont even try to install virtual packages..
  709. if (Pkg->VersionList == 0)
  710. return;
  711. /* Target the candidate version and remove the autoflag. We reset the
  712. autoflag below if this was called recursively. Otherwise the user
  713. should have the ability to de-auto a package by changing its state */
  714. RemoveSizes(Pkg);
  715. RemoveStates(Pkg);
  716. P.Mode = ModeInstall;
  717. P.InstallVer = P.CandidateVer;
  718. if(FromUser)
  719. {
  720. // Set it to manual if it's a new install or cancelling the
  721. // removal of a garbage package.
  722. if(P.Status == 2 || (!Pkg.CurrentVer().end() && !P.Marked))
  723. P.Flags &= ~Flag::Auto;
  724. }
  725. else
  726. {
  727. // Set it to auto if this is a new install.
  728. if(P.Status == 2)
  729. P.Flags |= Flag::Auto;
  730. }
  731. if (P.CandidateVer == (Version *)Pkg.CurrentVer())
  732. P.Mode = ModeKeep;
  733. AddStates(Pkg);
  734. Update(Pkg);
  735. AddSizes(Pkg);
  736. if (AutoInst == false)
  737. return;
  738. DepIterator Dep = P.InstVerIter(*this).DependsList();
  739. for (; Dep.end() != true;)
  740. {
  741. // Grok or groups
  742. DepIterator Start = Dep;
  743. bool Result = true;
  744. unsigned Ors = 0;
  745. for (bool LastOR = true; Dep.end() == false && LastOR == true; Dep++,Ors++)
  746. {
  747. LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
  748. if ((DepState[Dep->ID] & DepInstall) == DepInstall)
  749. Result = false;
  750. }
  751. // Dep is satisfied okay.
  752. if (Result == false)
  753. continue;
  754. /* Check if this dep should be consider for install. If it is a user
  755. defined important dep and we are installed a new package then
  756. it will be installed. Otherwise we only check for important
  757. deps that have changed from the installed version
  758. */
  759. if (IsImportantDep(Start) == false)
  760. continue;
  761. /* check if any ImportantDep() (but not Critial) where added
  762. * since we installed the package
  763. */
  764. bool isNewImportantDep = false;
  765. if(!ForceImportantDeps && !Start.IsCritical())
  766. {
  767. bool found=false;
  768. VerIterator instVer = Pkg.CurrentVer();
  769. if(!instVer.end())
  770. {
  771. for (DepIterator D = instVer.DependsList(); D.end() != true; D++)
  772. {
  773. //FIXME: deal better with or-groups(?)
  774. DepIterator LocalStart = D;
  775. if(IsImportantDep(D) && Start.TargetPkg() == D.TargetPkg())
  776. found=true;
  777. }
  778. // this is a new dep if it was not found to be already
  779. // a important dep of the installed pacakge
  780. isNewImportantDep = !found;
  781. }
  782. }
  783. if(isNewImportantDep)
  784. if(_config->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
  785. std::clog << "new important dependency: "
  786. << Start.TargetPkg().Name() << std::endl;
  787. // skip important deps if the package is already installed
  788. if (Pkg->CurrentVer != 0 && Start.IsCritical() == false
  789. && !isNewImportantDep && !ForceImportantDeps)
  790. continue;
  791. /* If we are in an or group locate the first or that can
  792. succeed. We have already cached this.. */
  793. for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; Ors--)
  794. Start++;
  795. /* This bit is for processing the possibilty of an install/upgrade
  796. fixing the problem */
  797. SPtrArray<Version *> List = Start.AllTargets();
  798. if (Start->Type != Dep::DpkgBreaks &&
  799. (DepState[Start->ID] & DepCVer) == DepCVer)
  800. {
  801. // Right, find the best version to install..
  802. Version **Cur = List;
  803. PkgIterator P = Start.TargetPkg();
  804. PkgIterator InstPkg(*Cache,0);
  805. // See if there are direct matches (at the start of the list)
  806. for (; *Cur != 0 && (*Cur)->ParentPkg == P.Index(); Cur++)
  807. {
  808. PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
  809. if (PkgState[Pkg->ID].CandidateVer != *Cur)
  810. continue;
  811. InstPkg = Pkg;
  812. break;
  813. }
  814. // Select the highest priority providing package
  815. if (InstPkg.end() == true)
  816. {
  817. pkgPrioSortList(*Cache,Cur);
  818. for (; *Cur != 0; Cur++)
  819. {
  820. PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
  821. if (PkgState[Pkg->ID].CandidateVer != *Cur)
  822. continue;
  823. InstPkg = Pkg;
  824. break;
  825. }
  826. }
  827. if (InstPkg.end() == false)
  828. {
  829. if(_config->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
  830. std::clog << "Installing " << InstPkg.Name()
  831. << " as dep of " << Pkg.Name()
  832. << std::endl;
  833. // now check if we should consider it a automatic dependency or not
  834. string sec = _config->Find("APT::Never-MarkAuto-Section","");
  835. if(Pkg.Section() && (string(Pkg.Section()) == sec))
  836. {
  837. if(_config->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
  838. std::clog << "Setting NOT as auto-installed because its a direct dep of a package in section " << sec << std::endl;
  839. MarkInstall(InstPkg,true,Depth + 1, true);
  840. }
  841. else
  842. {
  843. // mark automatic dependency
  844. MarkInstall(InstPkg,true,Depth + 1, false, ForceImportantDeps);
  845. // Set the autoflag, after MarkInstall because MarkInstall unsets it
  846. if (P->CurrentVer == 0)
  847. PkgState[InstPkg->ID].Flags |= Flag::Auto;
  848. }
  849. }
  850. continue;
  851. }
  852. /* For conflicts we just de-install the package and mark as auto,
  853. Conflicts may not have or groups. For dpkg's Breaks we try to
  854. upgrade the package. */
  855. if (Start->Type == Dep::Conflicts || Start->Type == Dep::Obsoletes ||
  856. Start->Type == Dep::DpkgBreaks)
  857. {
  858. for (Version **I = List; *I != 0; I++)
  859. {
  860. VerIterator Ver(*this,*I);
  861. PkgIterator Pkg = Ver.ParentPkg();
  862. if (Start->Type != Dep::DpkgBreaks)
  863. MarkDelete(Pkg);
  864. else
  865. if (PkgState[Pkg->ID].CandidateVer != *I)
  866. MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps);
  867. }
  868. continue;
  869. }
  870. }
  871. }
  872. /*}}}*/
  873. // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
  874. // ---------------------------------------------------------------------
  875. /* */
  876. void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
  877. {
  878. ActionGroup group(*this);
  879. RemoveSizes(Pkg);
  880. RemoveStates(Pkg);
  881. StateCache &P = PkgState[Pkg->ID];
  882. if (To == true)
  883. P.iFlags |= ReInstall;
  884. else
  885. P.iFlags &= ~ReInstall;
  886. AddStates(Pkg);
  887. AddSizes(Pkg);
  888. }
  889. /*}}}*/
  890. // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
  891. // ---------------------------------------------------------------------
  892. /* */
  893. void pkgDepCache::SetCandidateVersion(VerIterator TargetVer)
  894. {
  895. ActionGroup group(*this);
  896. pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
  897. StateCache &P = PkgState[Pkg->ID];
  898. RemoveSizes(Pkg);
  899. RemoveStates(Pkg);
  900. if (P.CandidateVer == P.InstallVer)
  901. P.InstallVer = (Version *)TargetVer;
  902. P.CandidateVer = (Version *)TargetVer;
  903. P.Update(Pkg,*this);
  904. AddStates(Pkg);
  905. Update(Pkg);
  906. AddSizes(Pkg);
  907. }
  908. void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
  909. {
  910. StateCache &state = PkgState[Pkg->ID];
  911. ActionGroup group(*this);
  912. if(Auto)
  913. state.Flags |= Flag::Auto;
  914. else
  915. state.Flags &= ~Flag::Auto;
  916. }
  917. /*}}}*/
  918. // StateCache::Update - Compute the various static display things /*{{{*/
  919. // ---------------------------------------------------------------------
  920. /* This is called whenever the Candidate version changes. */
  921. void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
  922. {
  923. // Some info
  924. VerIterator Ver = CandidateVerIter(Cache);
  925. // Use a null string or the version string
  926. if (Ver.end() == true)
  927. CandVersion = "";
  928. else
  929. CandVersion = Ver.VerStr();
  930. // Find the current version
  931. CurVersion = "";
  932. if (Pkg->CurrentVer != 0)
  933. CurVersion = Pkg.CurrentVer().VerStr();
  934. // Strip off the epochs for display
  935. CurVersion = StripEpoch(CurVersion);
  936. CandVersion = StripEpoch(CandVersion);
  937. // Figure out if its up or down or equal
  938. Status = Ver.CompareVer(Pkg.CurrentVer());
  939. if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
  940. Status = 2;
  941. }
  942. /*}}}*/
  943. // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
  944. // ---------------------------------------------------------------------
  945. /* */
  946. const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
  947. {
  948. if (Ver == 0)
  949. return 0;
  950. // Strip any epoch
  951. for (const char *I = Ver; *I != 0; I++)
  952. if (*I == ':')
  953. return I + 1;
  954. return Ver;
  955. }
  956. /*}}}*/
  957. // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
  958. // ---------------------------------------------------------------------
  959. /* The default just returns the highest available version that is not
  960. a source and automatic. */
  961. pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator Pkg)
  962. {
  963. /* Not source/not automatic versions cannot be a candidate version
  964. unless they are already installed */
  965. VerIterator Last(*(pkgCache *)this,0);
  966. for (VerIterator I = Pkg.VersionList(); I.end() == false; I++)
  967. {
  968. if (Pkg.CurrentVer() == I)
  969. return I;
  970. for (VerFileIterator J = I.FileList(); J.end() == false; J++)
  971. {
  972. if ((J.File()->Flags & Flag::NotSource) != 0)
  973. continue;
  974. /* Stash the highest version of a not-automatic source, we use it
  975. if there is nothing better */
  976. if ((J.File()->Flags & Flag::NotAutomatic) != 0)
  977. {
  978. if (Last.end() == true)
  979. Last = I;
  980. continue;
  981. }
  982. return I;
  983. }
  984. }
  985. return Last;
  986. }
  987. /*}}}*/
  988. // Policy::IsImportantDep - True if the dependency is important /*{{{*/
  989. // ---------------------------------------------------------------------
  990. /* */
  991. bool pkgDepCache::Policy::IsImportantDep(DepIterator Dep)
  992. {
  993. if(Dep.IsCritical())
  994. return true;
  995. else if(Dep->Type == pkgCache::Dep::Recommends)
  996. {
  997. if ( _config->FindB("APT::Install-Recommends", false))
  998. return true;
  999. // we suport a special mode to only install-recommends for certain
  1000. // sections
  1001. // FIXME: this is a meant as a temporarly solution until the
  1002. // recommends are cleaned up
  1003. string s = _config->Find("APT::Install-Recommends-Section","");
  1004. if(s.size() > 0)
  1005. {
  1006. const char *sec = Dep.ParentVer().Section();
  1007. if (sec && strcmp(sec, s.c_str()) == 0)
  1008. return true;
  1009. }
  1010. }
  1011. else if(Dep->Type == pkgCache::Dep::Suggests)
  1012. return _config->FindB("APT::Install-Suggests", false);
  1013. return false;
  1014. }
  1015. /*}}}*/
  1016. pkgDepCache::DefaultRootSetFunc::DefaultRootSetFunc()
  1017. : constructedSuccessfully(false)
  1018. {
  1019. Configuration::Item const *Opts;
  1020. Opts = _config->Tree("APT::NeverAutoRemove");
  1021. if (Opts != 0 && Opts->Child != 0)
  1022. {
  1023. Opts = Opts->Child;
  1024. for (; Opts != 0; Opts = Opts->Next)
  1025. {
  1026. if (Opts->Value.empty() == true)
  1027. continue;
  1028. regex_t *p = new regex_t;
  1029. if(regcomp(p,Opts->Value.c_str(),
  1030. REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
  1031. {
  1032. regfree(p);
  1033. delete p;
  1034. _error->Error("Regex compilation error for APT::NeverAutoRemove");
  1035. return;
  1036. }
  1037. rootSetRegexp.push_back(p);
  1038. }
  1039. }
  1040. constructedSuccessfully = true;
  1041. }
  1042. pkgDepCache::DefaultRootSetFunc::~DefaultRootSetFunc()
  1043. {
  1044. for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
  1045. {
  1046. regfree(rootSetRegexp[i]);
  1047. delete rootSetRegexp[i];
  1048. }
  1049. }
  1050. bool pkgDepCache::DefaultRootSetFunc::InRootSet(const pkgCache::PkgIterator &pkg)
  1051. {
  1052. for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
  1053. if (regexec(rootSetRegexp[i], pkg.Name(), 0, 0, 0) == 0)
  1054. return true;
  1055. return false;
  1056. }
  1057. pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc()
  1058. {
  1059. DefaultRootSetFunc *f = new DefaultRootSetFunc;
  1060. if(f->wasConstructedSuccessfully())
  1061. return f;
  1062. else
  1063. {
  1064. delete f;
  1065. return NULL;
  1066. }
  1067. }
  1068. bool pkgDepCache::MarkFollowsRecommends()
  1069. {
  1070. return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
  1071. }
  1072. bool pkgDepCache::MarkFollowsSuggests()
  1073. {
  1074. return _config->FindB("APT::AutoRemove::SuggestsImportant", false);
  1075. }
  1076. // the main mark algorithm
  1077. bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
  1078. {
  1079. bool follow_recommends;
  1080. bool follow_suggests;
  1081. // init the states
  1082. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1083. {
  1084. PkgState[p->ID].Marked = false;
  1085. PkgState[p->ID].Garbage = false;
  1086. // debug output
  1087. if(_config->FindB("Debug::pkgAutoRemove",false)
  1088. && PkgState[p->ID].Flags & Flag::Auto)
  1089. std::clog << "AutoDep: " << p.Name() << std::endl;
  1090. }
  1091. // init vars
  1092. follow_recommends = MarkFollowsRecommends();
  1093. follow_suggests = MarkFollowsSuggests();
  1094. // do the mark part, this is the core bit of the algorithm
  1095. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1096. {
  1097. if(!(PkgState[p->ID].Flags & Flag::Auto) ||
  1098. (p->Flags & Flag::Essential) ||
  1099. userFunc.InRootSet(p))
  1100. {
  1101. // the package is installed (and set to keep)
  1102. if(PkgState[p->ID].Keep() && !p.CurrentVer().end())
  1103. MarkPackage(p, p.CurrentVer(),
  1104. follow_recommends, follow_suggests);
  1105. // the package is to be installed
  1106. else if(PkgState[p->ID].Install())
  1107. MarkPackage(p, PkgState[p->ID].InstVerIter(*this),
  1108. follow_recommends, follow_suggests);
  1109. }
  1110. }
  1111. return true;
  1112. }
  1113. // mark a single package in Mark-and-Sweep
  1114. void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
  1115. const pkgCache::VerIterator &ver,
  1116. bool follow_recommends,
  1117. bool follow_suggests)
  1118. {
  1119. pkgDepCache::StateCache &state = PkgState[pkg->ID];
  1120. VerIterator currver = pkg.CurrentVer();
  1121. VerIterator candver = state.CandidateVerIter(*this);
  1122. VerIterator instver = state.InstVerIter(*this);
  1123. #if 0
  1124. // If a package was garbage-collected but is now being marked, we
  1125. // should re-select it
  1126. // For cases when a pkg is set to upgrade and this trigger the
  1127. // removal of a no-longer used dependency. if the pkg is set to
  1128. // keep again later it will result in broken deps
  1129. if(state.Delete() && state.RemoveReason = Unused)
  1130. {
  1131. if(ver==candver)
  1132. mark_install(pkg, false, false, NULL);
  1133. else if(ver==pkg.CurrentVer())
  1134. MarkKeep(pkg, false, false);
  1135. instver=state.InstVerIter(*this);
  1136. }
  1137. #endif
  1138. // For packages that are not going to be removed, ignore versions
  1139. // other than the InstVer. For packages that are going to be
  1140. // removed, ignore versions other than the current version.
  1141. if(!(ver == instver && !instver.end()) &&
  1142. !(ver == currver && instver.end() && !ver.end()))
  1143. return;
  1144. // if we are marked already we are done
  1145. if(state.Marked)
  1146. return;
  1147. //std::cout << "Setting Marked for: " << pkg.Name() << std::endl;
  1148. state.Marked=true;
  1149. if(!ver.end())
  1150. {
  1151. for(DepIterator d = ver.DependsList(); !d.end(); ++d)
  1152. {
  1153. if(d->Type == Dep::Depends ||
  1154. d->Type == Dep::PreDepends ||
  1155. (follow_recommends &&
  1156. d->Type == Dep::Recommends) ||
  1157. (follow_suggests &&
  1158. d->Type == Dep::Suggests))
  1159. {
  1160. // Try all versions of this package.
  1161. for(VerIterator V = d.TargetPkg().VersionList();
  1162. !V.end(); ++V)
  1163. {
  1164. if(_system->VS->CheckDep(V.VerStr(), d->CompareOp, d.TargetVer()))
  1165. {
  1166. MarkPackage(V.ParentPkg(), V,
  1167. follow_recommends, follow_suggests);
  1168. }
  1169. }
  1170. // Now try virtual packages
  1171. for(PrvIterator prv=d.TargetPkg().ProvidesList();
  1172. !prv.end(); ++prv)
  1173. {
  1174. if(_system->VS->CheckDep(prv.ProvideVersion(), d->CompareOp,
  1175. d.TargetVer()))
  1176. {
  1177. MarkPackage(prv.OwnerPkg(), prv.OwnerVer(),
  1178. follow_recommends, follow_suggests);
  1179. }
  1180. }
  1181. }
  1182. }
  1183. }
  1184. }
  1185. bool pkgDepCache::Sweep()
  1186. {
  1187. // do the sweep
  1188. for(PkgIterator p=PkgBegin(); !p.end(); ++p)
  1189. {
  1190. StateCache &state=PkgState[p->ID];
  1191. // skip required packages
  1192. if (!p.CurrentVer().end() &&
  1193. (p.CurrentVer()->Priority == pkgCache::State::Required))
  1194. continue;
  1195. // if it is not marked and it is installed, it's garbage
  1196. if(!state.Marked && (!p.CurrentVer().end() || state.Install()))
  1197. {
  1198. state.Garbage=true;
  1199. if(_config->FindB("Debug::pkgAutoRemove",false))
  1200. std::cout << "Garbage: " << p.Name() << std::endl;
  1201. }
  1202. }
  1203. return true;
  1204. }