depcache.cc 56 KB

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