depcache.cc 57 KB

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