depcache.cc 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800
  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/versionmatch.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/sptr.h>
  14. #include <apt-pkg/algorithms.h>
  15. #include <apt-pkg/fileutl.h>
  16. #include <apt-pkg/strutl.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <apt-pkg/aptconfiguration.h>
  19. #include <apt-pkg/pkgsystem.h>
  20. #include <apt-pkg/tagfile.h>
  21. #include <iostream>
  22. #include <sstream>
  23. #include <set>
  24. #include <sys/stat.h>
  25. #include <apti18n.h>
  26. /*}}}*/
  27. // helper for Install-Recommends-Sections and Never-MarkAuto-Sections /*{{{*/
  28. static bool
  29. ConfigValueInSubTree(const char* SubTree, const char *needle)
  30. {
  31. Configuration::Item const *Opts;
  32. Opts = _config->Tree(SubTree);
  33. if (Opts != 0 && Opts->Child != 0)
  34. {
  35. Opts = Opts->Child;
  36. for (; Opts != 0; Opts = Opts->Next)
  37. {
  38. if (Opts->Value.empty() == true)
  39. continue;
  40. if (strcmp(needle, Opts->Value.c_str()) == 0)
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. /*}}}*/
  47. pkgDepCache::ActionGroup::ActionGroup(pkgDepCache &cache) : /*{{{*/
  48. cache(cache), released(false)
  49. {
  50. ++cache.group_level;
  51. }
  52. void pkgDepCache::ActionGroup::release()
  53. {
  54. if(!released)
  55. {
  56. if(cache.group_level == 0)
  57. std::cerr << "W: Unbalanced action groups, expect badness" << std::endl;
  58. else
  59. {
  60. --cache.group_level;
  61. if(cache.group_level == 0)
  62. cache.MarkAndSweep();
  63. }
  64. released = false;
  65. }
  66. }
  67. pkgDepCache::ActionGroup::~ActionGroup()
  68. {
  69. release();
  70. }
  71. /*}}}*/
  72. // DepCache::pkgDepCache - Constructors /*{{{*/
  73. // ---------------------------------------------------------------------
  74. /* */
  75. pkgDepCache::pkgDepCache(pkgCache *pCache,Policy *Plcy) :
  76. group_level(0), Cache(pCache), PkgState(0), DepState(0)
  77. {
  78. DebugMarker = _config->FindB("Debug::pkgDepCache::Marker", false);
  79. DebugAutoInstall = _config->FindB("Debug::pkgDepCache::AutoInstall", false);
  80. delLocalPolicy = 0;
  81. LocalPolicy = Plcy;
  82. if (LocalPolicy == 0)
  83. delLocalPolicy = LocalPolicy = new Policy;
  84. }
  85. /*}}}*/
  86. // DepCache::~pkgDepCache - Destructor /*{{{*/
  87. // ---------------------------------------------------------------------
  88. /* */
  89. pkgDepCache::~pkgDepCache()
  90. {
  91. delete [] PkgState;
  92. delete [] DepState;
  93. delete delLocalPolicy;
  94. }
  95. /*}}}*/
  96. // DepCache::Init - Generate the initial extra structures. /*{{{*/
  97. // ---------------------------------------------------------------------
  98. /* This allocats the extension buffers and initializes them. */
  99. bool pkgDepCache::Init(OpProgress *Prog)
  100. {
  101. // Suppress mark updates during this operation (just in case) and
  102. // run a mark operation when Init terminates.
  103. ActionGroup actions(*this);
  104. delete [] PkgState;
  105. delete [] DepState;
  106. PkgState = new StateCache[Head().PackageCount];
  107. DepState = new unsigned char[Head().DependsCount];
  108. memset(PkgState,0,sizeof(*PkgState)*Head().PackageCount);
  109. memset(DepState,0,sizeof(*DepState)*Head().DependsCount);
  110. if (Prog != 0)
  111. {
  112. Prog->OverallProgress(0,2*Head().PackageCount,Head().PackageCount,
  113. _("Building dependency tree"));
  114. Prog->SubProgress(Head().PackageCount,_("Candidate versions"));
  115. }
  116. /* Set the current state of everything. In this state all of the
  117. packages are kept exactly as is. See AllUpgrade */
  118. int Done = 0;
  119. for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
  120. {
  121. if (Prog != 0 && Done%20 == 0)
  122. Prog->Progress(Done);
  123. // Find the proper cache slot
  124. StateCache &State = PkgState[I->ID];
  125. State.iFlags = 0;
  126. // Figure out the install version
  127. State.CandidateVer = GetCandidateVer(I);
  128. State.InstallVer = I.CurrentVer();
  129. State.Mode = ModeKeep;
  130. State.Update(I,*this);
  131. }
  132. if (Prog != 0)
  133. {
  134. Prog->OverallProgress(Head().PackageCount,2*Head().PackageCount,
  135. Head().PackageCount,
  136. _("Building dependency tree"));
  137. Prog->SubProgress(Head().PackageCount,_("Dependency generation"));
  138. }
  139. Update(Prog);
  140. if(Prog != 0)
  141. Prog->Done();
  142. return true;
  143. }
  144. /*}}}*/
  145. bool pkgDepCache::readStateFile(OpProgress *Prog) /*{{{*/
  146. {
  147. FileFd state_file;
  148. string const state = _config->FindFile("Dir::State::extended_states");
  149. if(RealFileExists(state)) {
  150. state_file.Open(state, FileFd::ReadOnly);
  151. int const file_size = state_file.Size();
  152. if(Prog != NULL)
  153. Prog->OverallProgress(0, file_size, 1,
  154. _("Reading state information"));
  155. pkgTagFile tagfile(&state_file);
  156. pkgTagSection section;
  157. int amt = 0;
  158. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  159. while(tagfile.Step(section)) {
  160. string const pkgname = section.FindS("Package");
  161. string pkgarch = section.FindS("Architecture");
  162. if (pkgarch.empty() == true)
  163. pkgarch = "any";
  164. pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
  165. // Silently ignore unknown packages and packages with no actual version.
  166. if(pkg.end() == true || pkg->VersionList == 0)
  167. continue;
  168. short const reason = section.FindI("Auto-Installed", 0);
  169. if(reason > 0)
  170. {
  171. PkgState[pkg->ID].Flags |= Flag::Auto;
  172. if (unlikely(debug_autoremove))
  173. std::clog << "Auto-Installed : " << pkg.FullName() << std::endl;
  174. if (pkgarch == "any")
  175. {
  176. pkgCache::GrpIterator G = pkg.Group();
  177. for (pkg = G.NextPkg(pkg); pkg.end() != true; pkg = G.NextPkg(pkg))
  178. if (pkg->VersionList != 0)
  179. PkgState[pkg->ID].Flags |= Flag::Auto;
  180. }
  181. }
  182. amt += section.size();
  183. if(Prog != NULL)
  184. Prog->OverallProgress(amt, file_size, 1,
  185. _("Reading state information"));
  186. }
  187. if(Prog != NULL)
  188. Prog->OverallProgress(file_size, file_size, 1,
  189. _("Reading state information"));
  190. }
  191. return true;
  192. }
  193. /*}}}*/
  194. bool pkgDepCache::writeStateFile(OpProgress *prog, bool InstalledOnly) /*{{{*/
  195. {
  196. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  197. if(debug_autoremove)
  198. std::clog << "pkgDepCache::writeStateFile()" << std::endl;
  199. FileFd StateFile;
  200. string const state = _config->FindFile("Dir::State::extended_states");
  201. // if it does not exist, create a empty one
  202. if(!RealFileExists(state))
  203. {
  204. StateFile.Open(state, FileFd::WriteAtomic);
  205. StateFile.Close();
  206. }
  207. // open it
  208. if(!StateFile.Open(state, FileFd::ReadOnly))
  209. return _error->Error(_("Failed to open StateFile %s"),
  210. state.c_str());
  211. FILE *OutFile;
  212. string const outfile = state + ".tmp";
  213. if((OutFile = fopen(outfile.c_str(),"w")) == NULL)
  214. return _error->Error(_("Failed to write temporary StateFile %s"),
  215. outfile.c_str());
  216. // first merge with the existing sections
  217. pkgTagFile tagfile(&StateFile);
  218. pkgTagSection section;
  219. std::set<string> pkgs_seen;
  220. const char *nullreorderlist[] = {0};
  221. while(tagfile.Step(section)) {
  222. string const pkgname = section.FindS("Package");
  223. string pkgarch = section.FindS("Architecture");
  224. if (pkgarch.empty() == true)
  225. pkgarch = "native";
  226. // Silently ignore unknown packages and packages with no actual
  227. // version.
  228. pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
  229. if(pkg.end() || pkg.VersionList().end())
  230. continue;
  231. StateCache const &P = PkgState[pkg->ID];
  232. bool newAuto = (P.Flags & Flag::Auto);
  233. // skip not installed or now-removed ones if requested
  234. if (InstalledOnly && (
  235. (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
  236. (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
  237. {
  238. // The section is obsolete if it contains no other tag
  239. unsigned int const count = section.Count();
  240. if (count < 2 ||
  241. (count == 2 && section.Exists("Auto-Installed")) ||
  242. (count == 3 && section.Exists("Auto-Installed") && section.Exists("Architecture")))
  243. continue;
  244. else
  245. newAuto = false;
  246. }
  247. if(_config->FindB("Debug::pkgAutoRemove",false))
  248. std::clog << "Update existing AutoInstall info: "
  249. << pkg.FullName() << std::endl;
  250. TFRewriteData rewrite[3];
  251. rewrite[0].Tag = "Architecture";
  252. rewrite[0].Rewrite = pkg.Arch();
  253. rewrite[0].NewTag = 0;
  254. rewrite[1].Tag = "Auto-Installed";
  255. rewrite[1].Rewrite = newAuto ? "1" : "0";
  256. rewrite[1].NewTag = 0;
  257. rewrite[2].Tag = 0;
  258. TFRewrite(OutFile, section, nullreorderlist, rewrite);
  259. fprintf(OutFile,"\n");
  260. pkgs_seen.insert(pkg.FullName());
  261. }
  262. // then write the ones we have not seen yet
  263. std::ostringstream ostr;
  264. for(pkgCache::PkgIterator pkg=Cache->PkgBegin(); !pkg.end(); pkg++) {
  265. StateCache const &P = PkgState[pkg->ID];
  266. if(P.Flags & Flag::Auto) {
  267. if (pkgs_seen.find(pkg.FullName()) != pkgs_seen.end()) {
  268. if(debug_autoremove)
  269. std::clog << "Skipping already written " << pkg.FullName() << std::endl;
  270. continue;
  271. }
  272. // skip not installed ones if requested
  273. if (InstalledOnly && (
  274. (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
  275. (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
  276. continue;
  277. const char* const pkgarch = pkg.Arch();
  278. if (strcmp(pkgarch, "all") == 0)
  279. continue;
  280. if(debug_autoremove)
  281. std::clog << "Writing new AutoInstall: " << pkg.FullName() << std::endl;
  282. ostr.str(string(""));
  283. ostr << "Package: " << pkg.Name()
  284. << "\nArchitecture: " << pkgarch
  285. << "\nAuto-Installed: 1\n\n";
  286. fprintf(OutFile,"%s",ostr.str().c_str());
  287. }
  288. }
  289. fclose(OutFile);
  290. // move the outfile over the real file and set permissions
  291. rename(outfile.c_str(), state.c_str());
  292. chmod(state.c_str(), 0644);
  293. return true;
  294. }
  295. /*}}}*/
  296. // DepCache::CheckDep - Checks a single dependency /*{{{*/
  297. // ---------------------------------------------------------------------
  298. /* This first checks the dependency against the main target package and
  299. then walks along the package provides list and checks if each provides
  300. will be installed then checks the provides against the dep. Res will be
  301. set to the package which was used to satisfy the dep. */
  302. bool pkgDepCache::CheckDep(DepIterator Dep,int Type,PkgIterator &Res)
  303. {
  304. Res = Dep.TargetPkg();
  305. /* Check simple depends. A depends -should- never self match but
  306. we allow it anyhow because dpkg does. Technically it is a packaging
  307. bug. Conflicts may never self match */
  308. if (Dep.TargetPkg() != Dep.ParentPkg() ||
  309. (Dep->Type != Dep::Conflicts && Dep->Type != Dep::DpkgBreaks && Dep->Type != Dep::Obsoletes))
  310. {
  311. PkgIterator Pkg = Dep.TargetPkg();
  312. // Check the base package
  313. if (Type == NowVersion && Pkg->CurrentVer != 0)
  314. if (VS().CheckDep(Pkg.CurrentVer().VerStr(),Dep->CompareOp,
  315. Dep.TargetVer()) == true)
  316. return true;
  317. if (Type == InstallVersion && PkgState[Pkg->ID].InstallVer != 0)
  318. if (VS().CheckDep(PkgState[Pkg->ID].InstVerIter(*this).VerStr(),
  319. Dep->CompareOp,Dep.TargetVer()) == true)
  320. return true;
  321. if (Type == CandidateVersion && PkgState[Pkg->ID].CandidateVer != 0)
  322. if (VS().CheckDep(PkgState[Pkg->ID].CandidateVerIter(*this).VerStr(),
  323. Dep->CompareOp,Dep.TargetVer()) == true)
  324. return true;
  325. }
  326. if (Dep->Type == Dep::Obsoletes)
  327. return false;
  328. // Check the providing packages
  329. PrvIterator P = Dep.TargetPkg().ProvidesList();
  330. PkgIterator Pkg = Dep.ParentPkg();
  331. for (; P.end() != true; P++)
  332. {
  333. /* Provides may never be applied against the same package (or group)
  334. if it is a conflicts. See the comment above. */
  335. if (P.OwnerPkg()->Group == Pkg->Group &&
  336. (Dep->Type == Dep::Conflicts || Dep->Type == Dep::DpkgBreaks))
  337. continue;
  338. // Check if the provides is a hit
  339. if (Type == NowVersion)
  340. {
  341. if (P.OwnerPkg().CurrentVer() != P.OwnerVer())
  342. continue;
  343. }
  344. if (Type == InstallVersion)
  345. {
  346. StateCache &State = PkgState[P.OwnerPkg()->ID];
  347. if (State.InstallVer != (Version *)P.OwnerVer())
  348. continue;
  349. }
  350. if (Type == CandidateVersion)
  351. {
  352. StateCache &State = PkgState[P.OwnerPkg()->ID];
  353. if (State.CandidateVer != (Version *)P.OwnerVer())
  354. continue;
  355. }
  356. // Compare the versions.
  357. if (VS().CheckDep(P.ProvideVersion(),Dep->CompareOp,Dep.TargetVer()) == true)
  358. {
  359. Res = P.OwnerPkg();
  360. return true;
  361. }
  362. }
  363. return false;
  364. }
  365. /*}}}*/
  366. // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
  367. // ---------------------------------------------------------------------
  368. /* Call with Mult = -1 to preform the inverse opration
  369. The Mult increases the complexity of the calulations here and is unused -
  370. or do we really have a usecase for removing the size of a package two
  371. times? So let us replace it with a simple bool and be done with it… */
  372. __deprecated void pkgDepCache::AddSizes(const PkgIterator &Pkg,signed long Mult)
  373. {
  374. StateCache &P = PkgState[Pkg->ID];
  375. if (Pkg->VersionList == 0)
  376. return;
  377. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  378. P.Keep() == true)
  379. return;
  380. // Compute the size data
  381. if (P.NewInstall() == true)
  382. {
  383. iUsrSize += (signed long long)(Mult*P.InstVerIter(*this)->InstalledSize);
  384. iDownloadSize += (signed long long)(Mult*P.InstVerIter(*this)->Size);
  385. return;
  386. }
  387. // Upgrading
  388. if (Pkg->CurrentVer != 0 &&
  389. (P.InstallVer != (Version *)Pkg.CurrentVer() ||
  390. (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
  391. {
  392. iUsrSize += (signed long long)(Mult*((signed long long)P.InstVerIter(*this)->InstalledSize -
  393. (signed long long)Pkg.CurrentVer()->InstalledSize));
  394. iDownloadSize += (signed long long)(Mult*P.InstVerIter(*this)->Size);
  395. return;
  396. }
  397. // Reinstall
  398. if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
  399. P.Delete() == false)
  400. {
  401. iDownloadSize += (signed long long)(Mult*P.InstVerIter(*this)->Size);
  402. return;
  403. }
  404. // Removing
  405. if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
  406. {
  407. iUsrSize -= (signed long long)(Mult*Pkg.CurrentVer()->InstalledSize);
  408. return;
  409. }
  410. }
  411. /*}}}*/
  412. // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
  413. // ---------------------------------------------------------------------
  414. /* Call with Inverse = true to preform the inverse opration */
  415. void pkgDepCache::AddSizes(const PkgIterator &Pkg, bool const &Inverse)
  416. {
  417. StateCache &P = PkgState[Pkg->ID];
  418. if (Pkg->VersionList == 0)
  419. return;
  420. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  421. P.Keep() == true)
  422. return;
  423. // Compute the size data
  424. if (P.NewInstall() == true)
  425. {
  426. if (Inverse == false) {
  427. iUsrSize += P.InstVerIter(*this)->InstalledSize;
  428. iDownloadSize += P.InstVerIter(*this)->Size;
  429. } else {
  430. iUsrSize -= P.InstVerIter(*this)->InstalledSize;
  431. iDownloadSize -= P.InstVerIter(*this)->Size;
  432. }
  433. return;
  434. }
  435. // Upgrading
  436. if (Pkg->CurrentVer != 0 &&
  437. (P.InstallVer != (Version *)Pkg.CurrentVer() ||
  438. (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
  439. {
  440. if (Inverse == false) {
  441. iUsrSize -= Pkg.CurrentVer()->InstalledSize;
  442. iUsrSize += P.InstVerIter(*this)->InstalledSize;
  443. iDownloadSize += P.InstVerIter(*this)->Size;
  444. } else {
  445. iUsrSize -= P.InstVerIter(*this)->InstalledSize;
  446. iUsrSize += Pkg.CurrentVer()->InstalledSize;
  447. iDownloadSize -= P.InstVerIter(*this)->Size;
  448. }
  449. return;
  450. }
  451. // Reinstall
  452. if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
  453. P.Delete() == false)
  454. {
  455. if (Inverse == false)
  456. iDownloadSize += P.InstVerIter(*this)->Size;
  457. else
  458. iDownloadSize -= P.InstVerIter(*this)->Size;
  459. return;
  460. }
  461. // Removing
  462. if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
  463. {
  464. if (Inverse == false)
  465. iUsrSize -= Pkg.CurrentVer()->InstalledSize;
  466. else
  467. iUsrSize += Pkg.CurrentVer()->InstalledSize;
  468. return;
  469. }
  470. }
  471. /*}}}*/
  472. // DepCache::AddStates - Add the package to the state counter /*{{{*/
  473. // ---------------------------------------------------------------------
  474. /* This routine is tricky to use, you must make sure that it is never
  475. called twice for the same package. This means the Remove/Add section
  476. should be as short as possible and not encompass any code that will
  477. calld Remove/Add itself. Remember, dependencies can be circular so
  478. while processing a dep for Pkg it is possible that Add/Remove
  479. will be called on Pkg */
  480. void pkgDepCache::AddStates(const PkgIterator &Pkg,int Add)
  481. {
  482. StateCache &State = PkgState[Pkg->ID];
  483. // The Package is broken (either minimal dep or policy dep)
  484. if ((State.DepState & DepInstMin) != DepInstMin)
  485. iBrokenCount += Add;
  486. if ((State.DepState & DepInstPolicy) != DepInstPolicy)
  487. iPolicyBrokenCount += Add;
  488. // Bad state
  489. if (Pkg.State() != PkgIterator::NeedsNothing)
  490. iBadCount += Add;
  491. // Not installed
  492. if (Pkg->CurrentVer == 0)
  493. {
  494. if (State.Mode == ModeDelete &&
  495. (State.iFlags | Purge) == Purge && Pkg.Purge() == false)
  496. iDelCount += Add;
  497. if (State.Mode == ModeInstall)
  498. iInstCount += Add;
  499. return;
  500. }
  501. // Installed, no upgrade
  502. if (State.Status == 0)
  503. {
  504. if (State.Mode == ModeDelete)
  505. iDelCount += Add;
  506. else
  507. if ((State.iFlags & ReInstall) == ReInstall)
  508. iInstCount += Add;
  509. return;
  510. }
  511. // Alll 3 are possible
  512. if (State.Mode == ModeDelete)
  513. iDelCount += Add;
  514. if (State.Mode == ModeKeep)
  515. iKeepCount += Add;
  516. if (State.Mode == ModeInstall)
  517. iInstCount += Add;
  518. }
  519. /*}}}*/
  520. // DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
  521. // ---------------------------------------------------------------------
  522. /* The or group results are stored in the last item of the or group. This
  523. allows easy detection of the state of a whole or'd group. */
  524. void pkgDepCache::BuildGroupOrs(VerIterator const &V)
  525. {
  526. unsigned char Group = 0;
  527. for (DepIterator D = V.DependsList(); D.end() != true; D++)
  528. {
  529. // Build the dependency state.
  530. unsigned char &State = DepState[D->ID];
  531. /* Invert for Conflicts. We have to do this twice to get the
  532. right sense for a conflicts group */
  533. if (D->Type == Dep::Conflicts ||
  534. D->Type == Dep::DpkgBreaks ||
  535. D->Type == Dep::Obsoletes)
  536. State = ~State;
  537. // Add to the group if we are within an or..
  538. State &= 0x7;
  539. Group |= State;
  540. State |= Group << 3;
  541. if ((D->CompareOp & Dep::Or) != Dep::Or)
  542. Group = 0;
  543. // Invert for Conflicts
  544. if (D->Type == Dep::Conflicts ||
  545. D->Type == Dep::DpkgBreaks ||
  546. D->Type == Dep::Obsoletes)
  547. State = ~State;
  548. }
  549. }
  550. /*}}}*/
  551. // DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
  552. // ---------------------------------------------------------------------
  553. /* This is used to run over a dependency list and determine the dep
  554. state of the list, filtering it through both a Min check and a Policy
  555. check. The return result will have SetMin/SetPolicy low if a check
  556. fails. It uses the DepState cache for it's computations. */
  557. unsigned char pkgDepCache::VersionState(DepIterator D,unsigned char Check,
  558. unsigned char SetMin,
  559. unsigned char SetPolicy)
  560. {
  561. unsigned char Dep = 0xFF;
  562. while (D.end() != true)
  563. {
  564. // Compute a single dependency element (glob or)
  565. DepIterator Start = D;
  566. unsigned char State = 0;
  567. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  568. {
  569. State |= DepState[D->ID];
  570. LastOR = (D->CompareOp & Dep::Or) == Dep::Or;
  571. }
  572. // Minimum deps that must be satisfied to have a working package
  573. if (Start.IsCritical() == true)
  574. if ((State & Check) != Check)
  575. Dep &= ~SetMin;
  576. // Policy deps that must be satisfied to install the package
  577. if (IsImportantDep(Start) == true &&
  578. (State & Check) != Check)
  579. Dep &= ~SetPolicy;
  580. }
  581. return Dep;
  582. }
  583. /*}}}*/
  584. // DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
  585. // ---------------------------------------------------------------------
  586. /* This is the main dependency computation bit. It computes the 3 main
  587. results for a dependencys, Now, Install and Candidate. Callers must
  588. invert the result if dealing with conflicts. */
  589. unsigned char pkgDepCache::DependencyState(DepIterator &D)
  590. {
  591. unsigned char State = 0;
  592. if (CheckDep(D,NowVersion) == true)
  593. State |= DepNow;
  594. if (CheckDep(D,InstallVersion) == true)
  595. State |= DepInstall;
  596. if (CheckDep(D,CandidateVersion) == true)
  597. State |= DepCVer;
  598. return State;
  599. }
  600. /*}}}*/
  601. // DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
  602. // ---------------------------------------------------------------------
  603. /* This determines the combined dependency representation of a package
  604. for its two states now and install. This is done by using the pre-generated
  605. dependency information. */
  606. void pkgDepCache::UpdateVerState(PkgIterator Pkg)
  607. {
  608. // Empty deps are always true
  609. StateCache &State = PkgState[Pkg->ID];
  610. State.DepState = 0xFF;
  611. // Check the Current state
  612. if (Pkg->CurrentVer != 0)
  613. {
  614. DepIterator D = Pkg.CurrentVer().DependsList();
  615. State.DepState &= VersionState(D,DepNow,DepNowMin,DepNowPolicy);
  616. }
  617. /* Check the candidate state. We do not compare against the whole as
  618. a candidate state but check the candidate version against the
  619. install states */
  620. if (State.CandidateVer != 0)
  621. {
  622. DepIterator D = State.CandidateVerIter(*this).DependsList();
  623. State.DepState &= VersionState(D,DepInstall,DepCandMin,DepCandPolicy);
  624. }
  625. // Check target state which can only be current or installed
  626. if (State.InstallVer != 0)
  627. {
  628. DepIterator D = State.InstVerIter(*this).DependsList();
  629. State.DepState &= VersionState(D,DepInstall,DepInstMin,DepInstPolicy);
  630. }
  631. }
  632. /*}}}*/
  633. // DepCache::Update - Figure out all the state information /*{{{*/
  634. // ---------------------------------------------------------------------
  635. /* This will figure out the state of all the packages and all the
  636. dependencies based on the current policy. */
  637. void pkgDepCache::Update(OpProgress *Prog)
  638. {
  639. iUsrSize = 0;
  640. iDownloadSize = 0;
  641. iDelCount = 0;
  642. iInstCount = 0;
  643. iKeepCount = 0;
  644. iBrokenCount = 0;
  645. iBadCount = 0;
  646. // Perform the depends pass
  647. int Done = 0;
  648. for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
  649. {
  650. if (Prog != 0 && Done%20 == 0)
  651. Prog->Progress(Done);
  652. for (VerIterator V = I.VersionList(); V.end() != true; V++)
  653. {
  654. unsigned char Group = 0;
  655. for (DepIterator D = V.DependsList(); D.end() != true; D++)
  656. {
  657. // Build the dependency state.
  658. unsigned char &State = DepState[D->ID];
  659. State = DependencyState(D);
  660. // Add to the group if we are within an or..
  661. Group |= State;
  662. State |= Group << 3;
  663. if ((D->CompareOp & Dep::Or) != Dep::Or)
  664. Group = 0;
  665. // Invert for Conflicts
  666. if (D->Type == Dep::Conflicts ||
  667. D->Type == Dep::DpkgBreaks ||
  668. D->Type == Dep::Obsoletes)
  669. State = ~State;
  670. }
  671. }
  672. // Compute the package dependency state and size additions
  673. AddSizes(I);
  674. UpdateVerState(I);
  675. AddStates(I);
  676. }
  677. if (Prog != 0)
  678. Prog->Progress(Done);
  679. readStateFile(Prog);
  680. }
  681. /*}}}*/
  682. // DepCache::Update - Update the deps list of a package /*{{{*/
  683. // ---------------------------------------------------------------------
  684. /* This is a helper for update that only does the dep portion of the scan.
  685. It is mainly meant to scan reverse dependencies. */
  686. void pkgDepCache::Update(DepIterator D)
  687. {
  688. // Update the reverse deps
  689. for (;D.end() != true; D++)
  690. {
  691. unsigned char &State = DepState[D->ID];
  692. State = DependencyState(D);
  693. // Invert for Conflicts
  694. if (D->Type == Dep::Conflicts ||
  695. D->Type == Dep::DpkgBreaks ||
  696. D->Type == Dep::Obsoletes)
  697. State = ~State;
  698. RemoveStates(D.ParentPkg());
  699. BuildGroupOrs(D.ParentVer());
  700. UpdateVerState(D.ParentPkg());
  701. AddStates(D.ParentPkg());
  702. }
  703. }
  704. /*}}}*/
  705. // DepCache::Update - Update the related deps of a package /*{{{*/
  706. // ---------------------------------------------------------------------
  707. /* This is called whenever the state of a package changes. It updates
  708. all cached dependencies related to this package. */
  709. void pkgDepCache::Update(PkgIterator const &Pkg)
  710. {
  711. // Recompute the dep of the package
  712. RemoveStates(Pkg);
  713. UpdateVerState(Pkg);
  714. AddStates(Pkg);
  715. // Update the reverse deps
  716. Update(Pkg.RevDependsList());
  717. // Update the provides map for the current ver
  718. if (Pkg->CurrentVer != 0)
  719. for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
  720. P.end() != true; P++)
  721. Update(P.ParentPkg().RevDependsList());
  722. // Update the provides map for the candidate ver
  723. if (PkgState[Pkg->ID].CandidateVer != 0)
  724. for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
  725. P.end() != true; P++)
  726. Update(P.ParentPkg().RevDependsList());
  727. }
  728. /*}}}*/
  729. // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
  730. // ---------------------------------------------------------------------
  731. /* */
  732. void pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser,
  733. unsigned long Depth)
  734. {
  735. if (IsModeChangeOk(ModeKeep, Pkg, Depth, FromUser) == false)
  736. return;
  737. /* Reject an attempt to keep a non-source broken installed package, those
  738. must be upgraded */
  739. if (Pkg.State() == PkgIterator::NeedsUnpack &&
  740. Pkg.CurrentVer().Downloadable() == false)
  741. return;
  742. /* We changed the soft state all the time so the UI is a bit nicer
  743. to use */
  744. StateCache &P = PkgState[Pkg->ID];
  745. // Check that it is not already kept
  746. if (P.Mode == ModeKeep)
  747. return;
  748. if (Soft == true)
  749. P.iFlags |= AutoKept;
  750. else
  751. P.iFlags &= ~AutoKept;
  752. ActionGroup group(*this);
  753. #if 0 // reseting the autoflag here means we lose the
  754. // auto-mark information if a user selects a package for removal
  755. // but changes his mind then and sets it for keep again
  756. // - this makes sense as default when all Garbage dependencies
  757. // are automatically marked for removal (as aptitude does).
  758. // setting a package for keep then makes it no longer autoinstalled
  759. // for all other use-case this action is rather suprising
  760. if(FromUser && !P.Marked)
  761. P.Flags &= ~Flag::Auto;
  762. #endif
  763. if (DebugMarker == true)
  764. std::clog << OutputInDepth(Depth) << "MarkKeep " << Pkg << " FU=" << FromUser << std::endl;
  765. RemoveSizes(Pkg);
  766. RemoveStates(Pkg);
  767. P.Mode = ModeKeep;
  768. if (Pkg->CurrentVer == 0)
  769. P.InstallVer = 0;
  770. else
  771. P.InstallVer = Pkg.CurrentVer();
  772. AddStates(Pkg);
  773. Update(Pkg);
  774. AddSizes(Pkg);
  775. }
  776. /*}}}*/
  777. // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
  778. // ---------------------------------------------------------------------
  779. /* */
  780. void pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge,
  781. unsigned long Depth, bool FromUser)
  782. {
  783. if (IsModeChangeOk(ModeDelete, Pkg, Depth, FromUser) == false)
  784. return;
  785. StateCache &P = PkgState[Pkg->ID];
  786. // Check that it is not already marked for delete
  787. if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
  788. (Pkg.Purge() == true || rPurge == false))
  789. return;
  790. // check if we are allowed to remove the package
  791. if (IsDeleteOk(Pkg,rPurge,Depth,FromUser) == false)
  792. return;
  793. P.iFlags &= ~(AutoKept | Purge);
  794. if (rPurge == true)
  795. P.iFlags |= Purge;
  796. ActionGroup group(*this);
  797. if (DebugMarker == true)
  798. std::clog << OutputInDepth(Depth) << (rPurge ? "MarkPurge " : "MarkDelete ") << Pkg << " FU=" << FromUser << std::endl;
  799. RemoveSizes(Pkg);
  800. RemoveStates(Pkg);
  801. if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
  802. P.Mode = ModeKeep;
  803. else
  804. P.Mode = ModeDelete;
  805. P.InstallVer = 0;
  806. AddStates(Pkg);
  807. Update(Pkg);
  808. AddSizes(Pkg);
  809. }
  810. /*}}}*/
  811. // DepCache::IsDeleteOk - check if it is ok to remove this package /*{{{*/
  812. // ---------------------------------------------------------------------
  813. /* The default implementation tries to prevent deletion of install requests.
  814. dpkg holds are enforced by the private IsModeChangeOk */
  815. bool pkgDepCache::IsDeleteOk(PkgIterator const &Pkg,bool rPurge,
  816. unsigned long Depth, bool FromUser)
  817. {
  818. if (FromUser == false && Pkg->CurrentVer == 0)
  819. {
  820. StateCache &P = PkgState[Pkg->ID];
  821. if (P.InstallVer != 0 && P.Status == 2 && (P.Flags & Flag::Auto) != Flag::Auto)
  822. {
  823. if (DebugMarker == true)
  824. std::clog << OutputInDepth(Depth) << "Manual install request prevents MarkDelete of " << Pkg << std::endl;
  825. return false;
  826. }
  827. }
  828. return true;
  829. }
  830. /*}}}*/
  831. // DepCache::IsModeChangeOk - check if it is ok to change the mode /*{{{*/
  832. // ---------------------------------------------------------------------
  833. /* this is used by all Mark methods on the very first line to check sanity
  834. and prevents mode changes for packages on hold for example.
  835. If you want to check Mode specific stuff you can use the virtual public
  836. Is<Mode>Ok methods instead */
  837. char const* PrintMode(char const mode)
  838. {
  839. switch (mode)
  840. {
  841. case pkgDepCache::ModeInstall: return "Install";
  842. case pkgDepCache::ModeKeep: return "Keep";
  843. case pkgDepCache::ModeDelete: return "Delete";
  844. default: return "UNKNOWN";
  845. }
  846. }
  847. bool pkgDepCache::IsModeChangeOk(ModeList const mode, PkgIterator const &Pkg,
  848. unsigned long const Depth, bool const FromUser)
  849. {
  850. // we are not trying to hard…
  851. if (unlikely(Depth > 100))
  852. return false;
  853. // general sanity
  854. if (unlikely(Pkg.end() == true || Pkg->VersionList == 0))
  855. return false;
  856. // the user is always right
  857. if (FromUser == true)
  858. return true;
  859. StateCache &P = PkgState[Pkg->ID];
  860. // if previous state was set by user only user can reset it
  861. if ((P.iFlags & Protected) == Protected)
  862. {
  863. if (unlikely(DebugMarker == true) && P.Mode != mode)
  864. std::clog << OutputInDepth(Depth) << "Ignore Mark" << PrintMode(mode)
  865. << " of " << Pkg << " as its mode (" << PrintMode(P.Mode)
  866. << ") is protected" << std::endl;
  867. return false;
  868. }
  869. // enforce dpkg holds
  870. else if (mode != ModeKeep && Pkg->SelectedState == pkgCache::State::Hold &&
  871. _config->FindB("APT::Ignore-Hold",false) == false)
  872. {
  873. if (unlikely(DebugMarker == true) && P.Mode != mode)
  874. std::clog << OutputInDepth(Depth) << "Hold prevents Mark" << PrintMode(mode)
  875. << " of " << Pkg << std::endl;
  876. return false;
  877. }
  878. return true;
  879. }
  880. /*}}}*/
  881. // DepCache::MarkInstall - Put the package in the install state /*{{{*/
  882. // ---------------------------------------------------------------------
  883. /* */
  884. void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
  885. unsigned long Depth, bool FromUser,
  886. bool ForceImportantDeps)
  887. {
  888. if (IsModeChangeOk(ModeInstall, Pkg, Depth, FromUser) == false)
  889. return;
  890. StateCache &P = PkgState[Pkg->ID];
  891. // See if there is even any possible instalation candidate
  892. if (P.CandidateVer == 0)
  893. return;
  894. /* Check that it is not already marked for install and that it can be
  895. installed */
  896. if ((P.InstPolicyBroken() == false && P.InstBroken() == false) &&
  897. (P.Mode == ModeInstall ||
  898. P.CandidateVer == (Version *)Pkg.CurrentVer()))
  899. {
  900. if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
  901. MarkKeep(Pkg, false, FromUser, Depth+1);
  902. return;
  903. }
  904. // check if we are allowed to install the package
  905. if (IsInstallOk(Pkg,AutoInst,Depth,FromUser) == false)
  906. return;
  907. ActionGroup group(*this);
  908. P.iFlags &= ~AutoKept;
  909. /* Target the candidate version and remove the autoflag. We reset the
  910. autoflag below if this was called recursively. Otherwise the user
  911. should have the ability to de-auto a package by changing its state */
  912. RemoveSizes(Pkg);
  913. RemoveStates(Pkg);
  914. P.Mode = ModeInstall;
  915. P.InstallVer = P.CandidateVer;
  916. if(FromUser)
  917. {
  918. // Set it to manual if it's a new install or already installed,
  919. // but only if its not marked by the autoremover (aptitude depend on this behavior)
  920. // or if we do automatic installation (aptitude never does it)
  921. if(P.Status == 2 || (Pkg->CurrentVer != 0 && (AutoInst == true || P.Marked == false)))
  922. P.Flags &= ~Flag::Auto;
  923. }
  924. else
  925. {
  926. // Set it to auto if this is a new install.
  927. if(P.Status == 2)
  928. P.Flags |= Flag::Auto;
  929. }
  930. if (P.CandidateVer == (Version *)Pkg.CurrentVer())
  931. P.Mode = ModeKeep;
  932. AddStates(Pkg);
  933. Update(Pkg);
  934. AddSizes(Pkg);
  935. if (AutoInst == false)
  936. return;
  937. if (DebugMarker == true)
  938. std::clog << OutputInDepth(Depth) << "MarkInstall " << Pkg << " FU=" << FromUser << std::endl;
  939. DepIterator Dep = P.InstVerIter(*this).DependsList();
  940. for (; Dep.end() != true;)
  941. {
  942. // Grok or groups
  943. DepIterator Start = Dep;
  944. bool Result = true;
  945. unsigned Ors = 0;
  946. for (bool LastOR = true; Dep.end() == false && LastOR == true; Dep++,Ors++)
  947. {
  948. LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
  949. if ((DepState[Dep->ID] & DepInstall) == DepInstall)
  950. Result = false;
  951. }
  952. // Dep is satisfied okay.
  953. if (Result == false)
  954. continue;
  955. /* Check if this dep should be consider for install. If it is a user
  956. defined important dep and we are installed a new package then
  957. it will be installed. Otherwise we only check for important
  958. deps that have changed from the installed version
  959. */
  960. if (IsImportantDep(Start) == false)
  961. continue;
  962. /* Check if any ImportantDep() (but not Critical) were added
  963. * since we installed the package. Also check for deps that
  964. * were satisfied in the past: for instance, if a version
  965. * restriction in a Recommends was tightened, upgrading the
  966. * package should follow that Recommends rather than causing the
  967. * dependency to be removed. (bug #470115)
  968. */
  969. bool isNewImportantDep = false;
  970. bool isPreviouslySatisfiedImportantDep = false;
  971. if(!ForceImportantDeps && !Start.IsCritical())
  972. {
  973. bool found=false;
  974. VerIterator instVer = Pkg.CurrentVer();
  975. if(!instVer.end())
  976. {
  977. for (DepIterator D = instVer.DependsList(); D.end() != true; D++)
  978. {
  979. //FIXME: deal better with or-groups(?)
  980. if(IsImportantDep(D) && !D.IsCritical() &&
  981. Start.TargetPkg() == D.TargetPkg())
  982. {
  983. if(!isPreviouslySatisfiedImportantDep)
  984. {
  985. DepIterator D2 = D;
  986. while((D2->CompareOp & Dep::Or) != 0)
  987. ++D2;
  988. isPreviouslySatisfiedImportantDep =
  989. (((*this)[D2] & DepGNow) != 0);
  990. }
  991. found=true;
  992. }
  993. }
  994. // this is a new dep if it was not found to be already
  995. // a important dep of the installed pacakge
  996. isNewImportantDep = !found;
  997. }
  998. }
  999. if(isNewImportantDep)
  1000. if(DebugAutoInstall == true)
  1001. std::clog << OutputInDepth(Depth) << "new important dependency: "
  1002. << Start.TargetPkg().Name() << std::endl;
  1003. if(isPreviouslySatisfiedImportantDep)
  1004. if(DebugAutoInstall == true)
  1005. std::clog << OutputInDepth(Depth) << "previously satisfied important dependency on "
  1006. << Start.TargetPkg().Name() << std::endl;
  1007. // skip important deps if the package is already installed
  1008. if (Pkg->CurrentVer != 0 && Start.IsCritical() == false
  1009. && !isNewImportantDep && !isPreviouslySatisfiedImportantDep
  1010. && !ForceImportantDeps)
  1011. continue;
  1012. /* If we are in an or group locate the first or that can
  1013. succeed. We have already cached this.. */
  1014. for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; Ors--)
  1015. Start++;
  1016. /* This bit is for processing the possibilty of an install/upgrade
  1017. fixing the problem */
  1018. SPtrArray<Version *> List = Start.AllTargets();
  1019. if (Start->Type != Dep::DpkgBreaks &&
  1020. (DepState[Start->ID] & DepCVer) == DepCVer)
  1021. {
  1022. // Right, find the best version to install..
  1023. Version **Cur = List;
  1024. PkgIterator P = Start.TargetPkg();
  1025. PkgIterator InstPkg(*Cache,0);
  1026. // See if there are direct matches (at the start of the list)
  1027. for (; *Cur != 0 && (*Cur)->ParentPkg == P.Index(); Cur++)
  1028. {
  1029. PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
  1030. if (PkgState[Pkg->ID].CandidateVer != *Cur)
  1031. continue;
  1032. InstPkg = Pkg;
  1033. break;
  1034. }
  1035. // Select the highest priority providing package
  1036. if (InstPkg.end() == true)
  1037. {
  1038. pkgPrioSortList(*Cache,Cur);
  1039. for (; *Cur != 0; Cur++)
  1040. {
  1041. PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
  1042. if (PkgState[Pkg->ID].CandidateVer != *Cur)
  1043. continue;
  1044. InstPkg = Pkg;
  1045. break;
  1046. }
  1047. }
  1048. if (InstPkg.end() == false)
  1049. {
  1050. if(DebugAutoInstall == true)
  1051. std::clog << OutputInDepth(Depth) << "Installing " << InstPkg.Name()
  1052. << " as " << Start.DepType() << " of " << Pkg.Name()
  1053. << std::endl;
  1054. // now check if we should consider it a automatic dependency or not
  1055. if(Pkg.Section() && ConfigValueInSubTree("APT::Never-MarkAuto-Sections", Pkg.Section()))
  1056. {
  1057. if(DebugAutoInstall == true)
  1058. std::clog << OutputInDepth(Depth) << "Setting NOT as auto-installed (direct "
  1059. << Start.DepType() << " of pkg in APT::Never-MarkAuto-Sections)" << std::endl;
  1060. MarkInstall(InstPkg,true,Depth + 1, true);
  1061. }
  1062. else
  1063. {
  1064. // mark automatic dependency
  1065. MarkInstall(InstPkg,true,Depth + 1, false, ForceImportantDeps);
  1066. // Set the autoflag, after MarkInstall because MarkInstall unsets it
  1067. if (P->CurrentVer == 0)
  1068. PkgState[InstPkg->ID].Flags |= Flag::Auto;
  1069. }
  1070. }
  1071. continue;
  1072. }
  1073. /* For conflicts we just de-install the package and mark as auto,
  1074. Conflicts may not have or groups. For dpkg's Breaks we try to
  1075. upgrade the package. */
  1076. if (Start->Type == Dep::Conflicts || Start->Type == Dep::Obsoletes ||
  1077. Start->Type == Dep::DpkgBreaks)
  1078. {
  1079. for (Version **I = List; *I != 0; I++)
  1080. {
  1081. VerIterator Ver(*this,*I);
  1082. PkgIterator Pkg = Ver.ParentPkg();
  1083. /* The List includes all packages providing this dependency,
  1084. even providers which are not installed, so skip them. */
  1085. if (PkgState[Pkg->ID].InstallVer == 0)
  1086. continue;
  1087. if (PkgState[Pkg->ID].CandidateVer != *I &&
  1088. Start->Type == Dep::DpkgBreaks)
  1089. MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps);
  1090. else
  1091. MarkDelete(Pkg,false,Depth + 1, false);
  1092. }
  1093. continue;
  1094. }
  1095. }
  1096. }
  1097. /*}}}*/
  1098. // DepCache::IsInstallOk - check if it is ok to install this package /*{{{*/
  1099. // ---------------------------------------------------------------------
  1100. /* The default implementation does nothing.
  1101. dpkg holds are enforced by the private IsModeChangeOk */
  1102. bool pkgDepCache::IsInstallOk(PkgIterator const &Pkg,bool AutoInst,
  1103. unsigned long Depth, bool FromUser)
  1104. {
  1105. return true;
  1106. }
  1107. /*}}}*/
  1108. // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
  1109. // ---------------------------------------------------------------------
  1110. /* */
  1111. void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
  1112. {
  1113. if (unlikely(Pkg.end() == true))
  1114. return;
  1115. ActionGroup group(*this);
  1116. RemoveSizes(Pkg);
  1117. RemoveStates(Pkg);
  1118. StateCache &P = PkgState[Pkg->ID];
  1119. if (To == true)
  1120. P.iFlags |= ReInstall;
  1121. else
  1122. P.iFlags &= ~ReInstall;
  1123. AddStates(Pkg);
  1124. AddSizes(Pkg);
  1125. }
  1126. /*}}}*/
  1127. // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
  1128. // ---------------------------------------------------------------------
  1129. /* */
  1130. void pkgDepCache::SetCandidateVersion(VerIterator TargetVer, bool const &Pseudo)
  1131. {
  1132. pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
  1133. StateCache &P = PkgState[Pkg->ID];
  1134. if (P.CandidateVer == TargetVer)
  1135. return;
  1136. ActionGroup group(*this);
  1137. RemoveSizes(Pkg);
  1138. RemoveStates(Pkg);
  1139. if (P.CandidateVer == P.InstallVer && P.Install() == true)
  1140. P.InstallVer = (Version *)TargetVer;
  1141. P.CandidateVer = (Version *)TargetVer;
  1142. P.Update(Pkg,*this);
  1143. AddStates(Pkg);
  1144. Update(Pkg);
  1145. AddSizes(Pkg);
  1146. }
  1147. /*}}}*/
  1148. // DepCache::SetCandidateRelease - Change the candidate version /*{{{*/
  1149. // ---------------------------------------------------------------------
  1150. /* changes the candidate of a package and walks over all its dependencies
  1151. to check if it needs to change the candidate of the dependency, too,
  1152. to reach a installable versionstate */
  1153. bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer,
  1154. std::string const &TargetRel)
  1155. {
  1156. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > Changed;
  1157. return SetCandidateRelease(TargetVer, TargetRel, Changed);
  1158. }
  1159. bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer,
  1160. std::string const &TargetRel,
  1161. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed)
  1162. {
  1163. ActionGroup group(*this);
  1164. SetCandidateVersion(TargetVer);
  1165. if (TargetRel == "installed" || TargetRel == "candidate") // both doesn't make sense in this context
  1166. return true;
  1167. pkgVersionMatch Match(TargetRel, pkgVersionMatch::Release);
  1168. // save the position of the last element we will not undo - if we have to
  1169. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::iterator newChanged = --(Changed.end());
  1170. for (pkgCache::DepIterator D = TargetVer.DependsList(); D.end() == false; ++D)
  1171. {
  1172. if (D->Type != pkgCache::Dep::PreDepends && D->Type != pkgCache::Dep::Depends &&
  1173. ((D->Type != pkgCache::Dep::Recommends && D->Type != pkgCache::Dep::Suggests) ||
  1174. IsImportantDep(D) == false))
  1175. continue;
  1176. // walk over an or-group and check if we need to do anything
  1177. // for simpilicity no or-group is handled as a or-group including one dependency
  1178. pkgCache::DepIterator Start = D;
  1179. bool itsFine = false;
  1180. for (bool stillOr = true; stillOr == true; ++Start)
  1181. {
  1182. stillOr = (Start->CompareOp & Dep::Or) == Dep::Or;
  1183. pkgCache::PkgIterator const P = Start.TargetPkg();
  1184. // virtual packages can't be a solution
  1185. if (P.end() == true || (P->ProvidesList == 0 && P->VersionList == 0))
  1186. continue;
  1187. pkgCache::VerIterator const Cand = PkgState[P->ID].CandidateVerIter(*this);
  1188. // no versioned dependency - but is it installable?
  1189. if (Start.TargetVer() == 0 || Start.TargetVer()[0] == '\0')
  1190. {
  1191. // Check if one of the providers is installable
  1192. if (P->ProvidesList != 0)
  1193. {
  1194. pkgCache::PrvIterator Prv = P.ProvidesList();
  1195. for (; Prv.end() == false; ++Prv)
  1196. {
  1197. pkgCache::VerIterator const C = PkgState[Prv.OwnerPkg()->ID].CandidateVerIter(*this);
  1198. if (C.end() == true || C != Prv.OwnerVer() ||
  1199. (VersionState(C.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) != DepCandMin)
  1200. continue;
  1201. break;
  1202. }
  1203. if (Prv.end() == true)
  1204. continue;
  1205. }
  1206. // no providers, so check if we have an installable candidate version
  1207. else if (Cand.end() == true ||
  1208. (VersionState(Cand.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) != DepCandMin)
  1209. continue;
  1210. itsFine = true;
  1211. break;
  1212. }
  1213. if (Cand.end() == true)
  1214. continue;
  1215. // check if the current candidate is enough for the versioned dependency - and installable?
  1216. if (VS().CheckDep(P.CandVersion(), Start->CompareOp, Start.TargetVer()) == true &&
  1217. (VersionState(Cand.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) == DepCandMin)
  1218. {
  1219. itsFine = true;
  1220. break;
  1221. }
  1222. }
  1223. if (itsFine == true) {
  1224. // something in the or-group was fine, skip all other members
  1225. for (; (D->CompareOp & Dep::Or) == Dep::Or; ++D);
  1226. continue;
  1227. }
  1228. // walk again over the or-group and check each if a candidate switch would help
  1229. itsFine = false;
  1230. for (bool stillOr = true; stillOr == true; ++D)
  1231. {
  1232. stillOr = (D->CompareOp & Dep::Or) == Dep::Or;
  1233. // changing candidate will not help if the dependency is not versioned
  1234. if (D.TargetVer() == 0 || D.TargetVer()[0] == '\0')
  1235. {
  1236. if (stillOr == true)
  1237. continue;
  1238. break;
  1239. }
  1240. pkgCache::VerIterator V;
  1241. if (TargetRel == "newest")
  1242. V = D.TargetPkg().VersionList();
  1243. else
  1244. V = Match.Find(D.TargetPkg());
  1245. // check if the version from this release could satisfy the dependency
  1246. if (V.end() == true || VS().CheckDep(V.VerStr(), D->CompareOp, D.TargetVer()) == false)
  1247. {
  1248. if (stillOr == true)
  1249. continue;
  1250. break;
  1251. }
  1252. pkgCache::VerIterator oldCand = PkgState[D.TargetPkg()->ID].CandidateVerIter(*this);
  1253. if (V == oldCand)
  1254. {
  1255. // Do we already touched this Version? If so, their versioned dependencies are okay, no need to check again
  1256. for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = Changed.begin();
  1257. c != Changed.end(); ++c)
  1258. {
  1259. if (c->first->ParentPkg != V->ParentPkg)
  1260. continue;
  1261. itsFine = true;
  1262. break;
  1263. }
  1264. }
  1265. if (itsFine == false)
  1266. {
  1267. // change the candidate
  1268. Changed.push_back(make_pair(oldCand, TargetVer));
  1269. if (SetCandidateRelease(V, TargetRel, Changed) == false)
  1270. {
  1271. if (stillOr == false)
  1272. break;
  1273. // undo the candidate changing
  1274. SetCandidateVersion(oldCand);
  1275. Changed.pop_back();
  1276. continue;
  1277. }
  1278. itsFine = true;
  1279. }
  1280. // something in the or-group was fine, skip all other members
  1281. for (; (D->CompareOp & Dep::Or) == Dep::Or; ++D);
  1282. break;
  1283. }
  1284. if (itsFine == false && (D->Type == pkgCache::Dep::PreDepends || D->Type == pkgCache::Dep::Depends))
  1285. {
  1286. // undo all changes which aren't lead to a solution
  1287. for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = ++newChanged;
  1288. c != Changed.end(); ++c)
  1289. SetCandidateVersion(c->first);
  1290. Changed.erase(newChanged, Changed.end());
  1291. return false;
  1292. }
  1293. }
  1294. return true;
  1295. }
  1296. /*}}}*/
  1297. // DepCache::MarkAuto - set the Auto flag for a package /*{{{*/
  1298. // ---------------------------------------------------------------------
  1299. /* */
  1300. void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
  1301. {
  1302. StateCache &state = PkgState[Pkg->ID];
  1303. ActionGroup group(*this);
  1304. if(Auto)
  1305. state.Flags |= Flag::Auto;
  1306. else
  1307. state.Flags &= ~Flag::Auto;
  1308. }
  1309. /*}}}*/
  1310. // StateCache::Update - Compute the various static display things /*{{{*/
  1311. // ---------------------------------------------------------------------
  1312. /* This is called whenever the Candidate version changes. */
  1313. void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
  1314. {
  1315. // Some info
  1316. VerIterator Ver = CandidateVerIter(Cache);
  1317. // Use a null string or the version string
  1318. if (Ver.end() == true)
  1319. CandVersion = "";
  1320. else
  1321. CandVersion = Ver.VerStr();
  1322. // Find the current version
  1323. CurVersion = "";
  1324. if (Pkg->CurrentVer != 0)
  1325. CurVersion = Pkg.CurrentVer().VerStr();
  1326. // Strip off the epochs for display
  1327. CurVersion = StripEpoch(CurVersion);
  1328. CandVersion = StripEpoch(CandVersion);
  1329. // Figure out if its up or down or equal
  1330. Status = Ver.CompareVer(Pkg.CurrentVer());
  1331. if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
  1332. Status = 2;
  1333. }
  1334. /*}}}*/
  1335. // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
  1336. // ---------------------------------------------------------------------
  1337. /* */
  1338. const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
  1339. {
  1340. if (Ver == 0)
  1341. return 0;
  1342. // Strip any epoch
  1343. for (const char *I = Ver; *I != 0; I++)
  1344. if (*I == ':')
  1345. return I + 1;
  1346. return Ver;
  1347. }
  1348. /*}}}*/
  1349. // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
  1350. // ---------------------------------------------------------------------
  1351. /* The default just returns the highest available version that is not
  1352. a source and automatic. */
  1353. pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator const &Pkg)
  1354. {
  1355. /* Not source/not automatic versions cannot be a candidate version
  1356. unless they are already installed */
  1357. VerIterator Last(*(pkgCache *)this,0);
  1358. for (VerIterator I = Pkg.VersionList(); I.end() == false; I++)
  1359. {
  1360. if (Pkg.CurrentVer() == I)
  1361. return I;
  1362. for (VerFileIterator J = I.FileList(); J.end() == false; J++)
  1363. {
  1364. if ((J.File()->Flags & Flag::NotSource) != 0)
  1365. continue;
  1366. /* Stash the highest version of a not-automatic source, we use it
  1367. if there is nothing better */
  1368. if ((J.File()->Flags & Flag::NotAutomatic) != 0 ||
  1369. (J.File()->Flags & Flag::ButAutomaticUpgrades) != 0)
  1370. {
  1371. if (Last.end() == true)
  1372. Last = I;
  1373. continue;
  1374. }
  1375. return I;
  1376. }
  1377. }
  1378. return Last;
  1379. }
  1380. /*}}}*/
  1381. // Policy::IsImportantDep - True if the dependency is important /*{{{*/
  1382. // ---------------------------------------------------------------------
  1383. /* */
  1384. bool pkgDepCache::Policy::IsImportantDep(DepIterator const &Dep)
  1385. {
  1386. if(Dep.IsCritical())
  1387. return true;
  1388. else if(Dep->Type == pkgCache::Dep::Recommends)
  1389. {
  1390. if ( _config->FindB("APT::Install-Recommends", false))
  1391. return true;
  1392. // we suport a special mode to only install-recommends for certain
  1393. // sections
  1394. // FIXME: this is a meant as a temporarly solution until the
  1395. // recommends are cleaned up
  1396. const char *sec = Dep.ParentVer().Section();
  1397. if (sec && ConfigValueInSubTree("APT::Install-Recommends-Sections", sec))
  1398. return true;
  1399. }
  1400. else if(Dep->Type == pkgCache::Dep::Suggests)
  1401. return _config->FindB("APT::Install-Suggests", false);
  1402. return false;
  1403. }
  1404. /*}}}*/
  1405. pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc() /*{{{*/
  1406. {
  1407. DefaultRootSetFunc *f = new DefaultRootSetFunc;
  1408. if(f->wasConstructedSuccessfully())
  1409. return f;
  1410. else
  1411. {
  1412. delete f;
  1413. return NULL;
  1414. }
  1415. }
  1416. /*}}}*/
  1417. bool pkgDepCache::MarkFollowsRecommends()
  1418. {
  1419. return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
  1420. }
  1421. bool pkgDepCache::MarkFollowsSuggests()
  1422. {
  1423. return _config->FindB("APT::AutoRemove::SuggestsImportant", false);
  1424. }
  1425. // pkgDepCache::MarkRequired - the main mark algorithm /*{{{*/
  1426. bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
  1427. {
  1428. bool follow_recommends;
  1429. bool follow_suggests;
  1430. bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  1431. // init the states
  1432. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1433. {
  1434. PkgState[p->ID].Marked = false;
  1435. PkgState[p->ID].Garbage = false;
  1436. // debug output
  1437. if(debug_autoremove && PkgState[p->ID].Flags & Flag::Auto)
  1438. std::clog << "AutoDep: " << p.FullName() << std::endl;
  1439. }
  1440. // init vars
  1441. follow_recommends = MarkFollowsRecommends();
  1442. follow_suggests = MarkFollowsSuggests();
  1443. // do the mark part, this is the core bit of the algorithm
  1444. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1445. {
  1446. if(!(PkgState[p->ID].Flags & Flag::Auto) ||
  1447. (p->Flags & Flag::Essential) ||
  1448. userFunc.InRootSet(p) ||
  1449. // be nice even then a required package violates the policy (#583517)
  1450. // and do the full mark process also for required packages
  1451. (p.CurrentVer().end() != true &&
  1452. p.CurrentVer()->Priority == pkgCache::State::Required))
  1453. {
  1454. // the package is installed (and set to keep)
  1455. if(PkgState[p->ID].Keep() && !p.CurrentVer().end())
  1456. MarkPackage(p, p.CurrentVer(),
  1457. follow_recommends, follow_suggests);
  1458. // the package is to be installed
  1459. else if(PkgState[p->ID].Install())
  1460. MarkPackage(p, PkgState[p->ID].InstVerIter(*this),
  1461. follow_recommends, follow_suggests);
  1462. }
  1463. }
  1464. return true;
  1465. }
  1466. /*}}}*/
  1467. // MarkPackage - mark a single package in Mark-and-Sweep /*{{{*/
  1468. void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
  1469. const pkgCache::VerIterator &ver,
  1470. bool const &follow_recommends,
  1471. bool const &follow_suggests)
  1472. {
  1473. pkgDepCache::StateCache &state = PkgState[pkg->ID];
  1474. // if we are marked already we are done
  1475. if(state.Marked)
  1476. return;
  1477. VerIterator const currver = pkg.CurrentVer();
  1478. VerIterator const instver = state.InstVerIter(*this);
  1479. #if 0
  1480. VerIterator const candver = state.CandidateVerIter(*this);
  1481. // If a package was garbage-collected but is now being marked, we
  1482. // should re-select it
  1483. // For cases when a pkg is set to upgrade and this trigger the
  1484. // removal of a no-longer used dependency. if the pkg is set to
  1485. // keep again later it will result in broken deps
  1486. if(state.Delete() && state.RemoveReason = Unused)
  1487. {
  1488. if(ver==candver)
  1489. mark_install(pkg, false, false, NULL);
  1490. else if(ver==pkg.CurrentVer())
  1491. MarkKeep(pkg, false, false);
  1492. instver=state.InstVerIter(*this);
  1493. }
  1494. #endif
  1495. // For packages that are not going to be removed, ignore versions
  1496. // other than the InstVer. For packages that are going to be
  1497. // removed, ignore versions other than the current version.
  1498. if(!(ver == instver && !instver.end()) &&
  1499. !(ver == currver && instver.end() && !ver.end()))
  1500. return;
  1501. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove", false);
  1502. if(debug_autoremove)
  1503. {
  1504. std::clog << "Marking: " << pkg.FullName();
  1505. if(!ver.end())
  1506. std::clog << " " << ver.VerStr();
  1507. if(!currver.end())
  1508. std::clog << ", Curr=" << currver.VerStr();
  1509. if(!instver.end())
  1510. std::clog << ", Inst=" << instver.VerStr();
  1511. std::clog << std::endl;
  1512. }
  1513. state.Marked=true;
  1514. if(ver.end() == true)
  1515. return;
  1516. for(DepIterator d = ver.DependsList(); !d.end(); ++d)
  1517. {
  1518. if(d->Type == Dep::Depends ||
  1519. d->Type == Dep::PreDepends ||
  1520. (follow_recommends &&
  1521. d->Type == Dep::Recommends) ||
  1522. (follow_suggests &&
  1523. d->Type == Dep::Suggests))
  1524. {
  1525. // Try all versions of this package.
  1526. for(VerIterator V = d.TargetPkg().VersionList();
  1527. !V.end(); ++V)
  1528. {
  1529. if(_system->VS->CheckDep(V.VerStr(), d->CompareOp, d.TargetVer()))
  1530. {
  1531. if(debug_autoremove)
  1532. {
  1533. std::clog << "Following dep: " << d.ParentPkg().FullName()
  1534. << " " << d.ParentVer().VerStr() << " "
  1535. << d.DepType() << " " << d.TargetPkg().FullName();
  1536. if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
  1537. {
  1538. std::clog << " (" << d.CompType() << " "
  1539. << d.TargetVer() << ")";
  1540. }
  1541. std::clog << std::endl;
  1542. }
  1543. MarkPackage(V.ParentPkg(), V,
  1544. follow_recommends, follow_suggests);
  1545. }
  1546. }
  1547. // Now try virtual packages
  1548. for(PrvIterator prv=d.TargetPkg().ProvidesList();
  1549. !prv.end(); ++prv)
  1550. {
  1551. if(_system->VS->CheckDep(prv.ProvideVersion(), d->CompareOp,
  1552. d.TargetVer()))
  1553. {
  1554. if(debug_autoremove)
  1555. {
  1556. std::clog << "Following dep: " << d.ParentPkg().FullName() << " "
  1557. << d.ParentVer().VerStr() << " "
  1558. << d.DepType() << " " << d.TargetPkg().FullName() << " ";
  1559. if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
  1560. {
  1561. std::clog << " (" << d.CompType() << " "
  1562. << d.TargetVer() << ")";
  1563. }
  1564. std::clog << ", provided by "
  1565. << prv.OwnerPkg().FullName() << " "
  1566. << prv.OwnerVer().VerStr()
  1567. << std::endl;
  1568. }
  1569. MarkPackage(prv.OwnerPkg(), prv.OwnerVer(),
  1570. follow_recommends, follow_suggests);
  1571. }
  1572. }
  1573. }
  1574. }
  1575. }
  1576. /*}}}*/
  1577. bool pkgDepCache::Sweep() /*{{{*/
  1578. {
  1579. bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  1580. // do the sweep
  1581. for(PkgIterator p=PkgBegin(); !p.end(); ++p)
  1582. {
  1583. StateCache &state=PkgState[p->ID];
  1584. // skip required packages
  1585. if (!p.CurrentVer().end() &&
  1586. (p.CurrentVer()->Priority == pkgCache::State::Required))
  1587. continue;
  1588. // if it is not marked and it is installed, it's garbage
  1589. if(!state.Marked && (!p.CurrentVer().end() || state.Install()))
  1590. {
  1591. state.Garbage=true;
  1592. if(debug_autoremove)
  1593. std::clog << "Garbage: " << p.FullName() << std::endl;
  1594. }
  1595. }
  1596. return true;
  1597. }
  1598. /*}}}*/