depcache.cc 62 KB

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