depcache.cc 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962
  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 * const pCache,Policy * const 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 * const 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 * const 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 * const /*prog*/, bool const 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 const &Dep,int const 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)
  320. {
  321. if (Pkg->CurrentVer != 0 && Dep.IsSatisfied(Pkg.CurrentVer()) == true)
  322. return true;
  323. }
  324. else if (Type == InstallVersion)
  325. {
  326. if (PkgState[Pkg->ID].InstallVer != 0 &&
  327. Dep.IsSatisfied(PkgState[Pkg->ID].InstVerIter(*this)) == true)
  328. return true;
  329. }
  330. else if (Type == CandidateVersion)
  331. if (PkgState[Pkg->ID].CandidateVer != 0 &&
  332. Dep.IsSatisfied(PkgState[Pkg->ID].CandidateVerIter(*this)) == true)
  333. return true;
  334. }
  335. if (Dep->Type == Dep::Obsoletes)
  336. return false;
  337. // Check the providing packages
  338. PrvIterator P = Dep.TargetPkg().ProvidesList();
  339. for (; P.end() != true; ++P)
  340. {
  341. if (Dep.IsIgnorable(P) == true)
  342. continue;
  343. // Check if the provides is a hit
  344. if (Type == NowVersion)
  345. {
  346. if (P.OwnerPkg().CurrentVer() != P.OwnerVer())
  347. continue;
  348. }
  349. else if (Type == InstallVersion)
  350. {
  351. StateCache &State = PkgState[P.OwnerPkg()->ID];
  352. if (State.InstallVer != (Version *)P.OwnerVer())
  353. continue;
  354. }
  355. else if (Type == CandidateVersion)
  356. {
  357. StateCache &State = PkgState[P.OwnerPkg()->ID];
  358. if (State.CandidateVer != (Version *)P.OwnerVer())
  359. continue;
  360. }
  361. // Compare the versions.
  362. if (Dep.IsSatisfied(P) == true)
  363. {
  364. Res = P.OwnerPkg();
  365. return true;
  366. }
  367. }
  368. return false;
  369. }
  370. /*}}}*/
  371. // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
  372. // ---------------------------------------------------------------------
  373. /* Call with Inverse = true to preform the inverse opration */
  374. void pkgDepCache::AddSizes(const PkgIterator &Pkg, bool const Inverse)
  375. {
  376. StateCache &P = PkgState[Pkg->ID];
  377. if (Pkg->VersionList == 0)
  378. return;
  379. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  380. P.Keep() == true)
  381. return;
  382. // Compute the size data
  383. if (P.NewInstall() == true)
  384. {
  385. if (Inverse == false) {
  386. iUsrSize += P.InstVerIter(*this)->InstalledSize;
  387. iDownloadSize += P.InstVerIter(*this)->Size;
  388. } else {
  389. iUsrSize -= P.InstVerIter(*this)->InstalledSize;
  390. iDownloadSize -= P.InstVerIter(*this)->Size;
  391. }
  392. return;
  393. }
  394. // Upgrading
  395. if (Pkg->CurrentVer != 0 &&
  396. (P.InstallVer != (Version *)Pkg.CurrentVer() ||
  397. (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
  398. {
  399. if (Inverse == false) {
  400. iUsrSize -= Pkg.CurrentVer()->InstalledSize;
  401. iUsrSize += P.InstVerIter(*this)->InstalledSize;
  402. iDownloadSize += P.InstVerIter(*this)->Size;
  403. } else {
  404. iUsrSize -= P.InstVerIter(*this)->InstalledSize;
  405. iUsrSize += Pkg.CurrentVer()->InstalledSize;
  406. iDownloadSize -= P.InstVerIter(*this)->Size;
  407. }
  408. return;
  409. }
  410. // Reinstall
  411. if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
  412. P.Delete() == false)
  413. {
  414. if (Inverse == false)
  415. iDownloadSize += P.InstVerIter(*this)->Size;
  416. else
  417. iDownloadSize -= P.InstVerIter(*this)->Size;
  418. return;
  419. }
  420. // Removing
  421. if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
  422. {
  423. if (Inverse == false)
  424. iUsrSize -= Pkg.CurrentVer()->InstalledSize;
  425. else
  426. iUsrSize += Pkg.CurrentVer()->InstalledSize;
  427. return;
  428. }
  429. }
  430. /*}}}*/
  431. // DepCache::AddStates - Add the package to the state counter /*{{{*/
  432. // ---------------------------------------------------------------------
  433. /* This routine is tricky to use, you must make sure that it is never
  434. called twice for the same package. This means the Remove/Add section
  435. should be as short as possible and not encompass any code that will
  436. calld Remove/Add itself. Remember, dependencies can be circular so
  437. while processing a dep for Pkg it is possible that Add/Remove
  438. will be called on Pkg */
  439. void pkgDepCache::AddStates(const PkgIterator &Pkg, bool const Invert)
  440. {
  441. signed char const Add = (Invert == false) ? 1 : -1;
  442. StateCache &State = PkgState[Pkg->ID];
  443. // The Package is broken (either minimal dep or policy dep)
  444. if ((State.DepState & DepInstMin) != DepInstMin)
  445. iBrokenCount += Add;
  446. if ((State.DepState & DepInstPolicy) != DepInstPolicy)
  447. iPolicyBrokenCount += Add;
  448. // Bad state
  449. if (Pkg.State() != PkgIterator::NeedsNothing)
  450. iBadCount += Add;
  451. // Not installed
  452. if (Pkg->CurrentVer == 0)
  453. {
  454. if (State.Mode == ModeDelete &&
  455. (State.iFlags & Purge) == Purge && Pkg.Purge() == false)
  456. iDelCount += Add;
  457. if (State.Mode == ModeInstall)
  458. iInstCount += Add;
  459. return;
  460. }
  461. // Installed, no upgrade
  462. if (State.Status == 0)
  463. {
  464. if (State.Mode == ModeDelete)
  465. iDelCount += Add;
  466. else
  467. if ((State.iFlags & ReInstall) == ReInstall)
  468. iInstCount += Add;
  469. return;
  470. }
  471. // Alll 3 are possible
  472. if (State.Mode == ModeDelete)
  473. iDelCount += Add;
  474. else if (State.Mode == ModeKeep)
  475. iKeepCount += Add;
  476. else if (State.Mode == ModeInstall)
  477. iInstCount += Add;
  478. }
  479. /*}}}*/
  480. // DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
  481. // ---------------------------------------------------------------------
  482. /* The or group results are stored in the last item of the or group. This
  483. allows easy detection of the state of a whole or'd group. */
  484. void pkgDepCache::BuildGroupOrs(VerIterator const &V)
  485. {
  486. unsigned char Group = 0;
  487. for (DepIterator D = V.DependsList(); D.end() != true; ++D)
  488. {
  489. // Build the dependency state.
  490. unsigned char &State = DepState[D->ID];
  491. /* Invert for Conflicts. We have to do this twice to get the
  492. right sense for a conflicts group */
  493. if (D.IsNegative() == true)
  494. State = ~State;
  495. // Add to the group if we are within an or..
  496. State &= 0x7;
  497. Group |= State;
  498. State |= Group << 3;
  499. if ((D->CompareOp & Dep::Or) != Dep::Or)
  500. Group = 0;
  501. // Invert for Conflicts
  502. if (D.IsNegative() == true)
  503. State = ~State;
  504. }
  505. }
  506. /*}}}*/
  507. // DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
  508. // ---------------------------------------------------------------------
  509. /* This is used to run over a dependency list and determine the dep
  510. state of the list, filtering it through both a Min check and a Policy
  511. check. The return result will have SetMin/SetPolicy low if a check
  512. fails. It uses the DepState cache for it's computations. */
  513. unsigned char pkgDepCache::VersionState(DepIterator D, unsigned char const Check,
  514. unsigned char const SetMin,
  515. unsigned char const SetPolicy) const
  516. {
  517. unsigned char Dep = 0xFF;
  518. while (D.end() != true)
  519. {
  520. // the last or-dependency has the state of all previous or'ed
  521. DepIterator Start, End;
  522. D.GlobOr(Start, End);
  523. // ignore if we are called with Dep{Install,…} or DepG{Install,…}
  524. // the later would be more correct, but the first is what we get
  525. unsigned char const State = DepState[End->ID] | (DepState[End->ID] >> 3);
  526. // Minimum deps that must be satisfied to have a working package
  527. if (Start.IsCritical() == true)
  528. {
  529. if ((State & Check) != Check)
  530. return Dep &= ~(SetMin | SetPolicy);
  531. }
  532. // Policy deps that must be satisfied to install the package
  533. else if (IsImportantDep(Start) == true &&
  534. (State & Check) != Check)
  535. Dep &= ~SetPolicy;
  536. }
  537. return Dep;
  538. }
  539. /*}}}*/
  540. // DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
  541. // ---------------------------------------------------------------------
  542. /* This is the main dependency computation bit. It computes the 3 main
  543. results for a dependencys, Now, Install and Candidate. Callers must
  544. invert the result if dealing with conflicts. */
  545. unsigned char pkgDepCache::DependencyState(DepIterator const &D)
  546. {
  547. unsigned char State = 0;
  548. if (CheckDep(D,NowVersion) == true)
  549. State |= DepNow;
  550. if (CheckDep(D,InstallVersion) == true)
  551. State |= DepInstall;
  552. if (CheckDep(D,CandidateVersion) == true)
  553. State |= DepCVer;
  554. return State;
  555. }
  556. /*}}}*/
  557. // DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
  558. // ---------------------------------------------------------------------
  559. /* This determines the combined dependency representation of a package
  560. for its two states now and install. This is done by using the pre-generated
  561. dependency information. */
  562. void pkgDepCache::UpdateVerState(PkgIterator const &Pkg)
  563. {
  564. // Empty deps are always true
  565. StateCache &State = PkgState[Pkg->ID];
  566. State.DepState = 0xFF;
  567. // Check the Current state
  568. if (Pkg->CurrentVer != 0)
  569. {
  570. DepIterator D = Pkg.CurrentVer().DependsList();
  571. State.DepState &= VersionState(D,DepNow,DepNowMin,DepNowPolicy);
  572. }
  573. /* Check the candidate state. We do not compare against the whole as
  574. a candidate state but check the candidate version against the
  575. install states */
  576. if (State.CandidateVer != 0)
  577. {
  578. DepIterator D = State.CandidateVerIter(*this).DependsList();
  579. State.DepState &= VersionState(D,DepInstall,DepCandMin,DepCandPolicy);
  580. }
  581. // Check target state which can only be current or installed
  582. if (State.InstallVer != 0)
  583. {
  584. DepIterator D = State.InstVerIter(*this).DependsList();
  585. State.DepState &= VersionState(D,DepInstall,DepInstMin,DepInstPolicy);
  586. }
  587. }
  588. /*}}}*/
  589. // DepCache::Update - Figure out all the state information /*{{{*/
  590. // ---------------------------------------------------------------------
  591. /* This will figure out the state of all the packages and all the
  592. dependencies based on the current policy. */
  593. void pkgDepCache::Update(OpProgress * const Prog)
  594. {
  595. iUsrSize = 0;
  596. iDownloadSize = 0;
  597. iInstCount = 0;
  598. iDelCount = 0;
  599. iKeepCount = 0;
  600. iBrokenCount = 0;
  601. iPolicyBrokenCount = 0;
  602. iBadCount = 0;
  603. // Perform the depends pass
  604. int Done = 0;
  605. for (PkgIterator I = PkgBegin(); I.end() != true; ++I, ++Done)
  606. {
  607. if (Prog != 0 && Done%20 == 0)
  608. Prog->Progress(Done);
  609. for (VerIterator V = I.VersionList(); V.end() != true; ++V)
  610. {
  611. unsigned char Group = 0;
  612. for (DepIterator D = V.DependsList(); D.end() != true; ++D)
  613. {
  614. // Build the dependency state.
  615. unsigned char &State = DepState[D->ID];
  616. State = DependencyState(D);
  617. // Add to the group if we are within an or..
  618. Group |= State;
  619. State |= Group << 3;
  620. if ((D->CompareOp & Dep::Or) != Dep::Or)
  621. Group = 0;
  622. // Invert for Conflicts
  623. if (D.IsNegative() == true)
  624. State = ~State;
  625. }
  626. }
  627. // Compute the package dependency state and size additions
  628. AddSizes(I);
  629. UpdateVerState(I);
  630. AddStates(I);
  631. }
  632. if (Prog != 0)
  633. Prog->Progress(Done);
  634. readStateFile(Prog);
  635. }
  636. /*}}}*/
  637. // DepCache::Update - Update the deps list of a package /*{{{*/
  638. // ---------------------------------------------------------------------
  639. /* This is a helper for update that only does the dep portion of the scan.
  640. It is mainly meant to scan reverse dependencies. */
  641. void pkgDepCache::Update(DepIterator D)
  642. {
  643. // Update the reverse deps
  644. for (;D.end() != true; ++D)
  645. {
  646. unsigned char &State = DepState[D->ID];
  647. State = DependencyState(D);
  648. // Invert for Conflicts
  649. if (D.IsNegative() == true)
  650. State = ~State;
  651. RemoveStates(D.ParentPkg());
  652. BuildGroupOrs(D.ParentVer());
  653. UpdateVerState(D.ParentPkg());
  654. AddStates(D.ParentPkg());
  655. }
  656. }
  657. /*}}}*/
  658. // DepCache::Update - Update the related deps of a package /*{{{*/
  659. // ---------------------------------------------------------------------
  660. /* This is called whenever the state of a package changes. It updates
  661. all cached dependencies related to this package. */
  662. void pkgDepCache::Update(PkgIterator const &Pkg)
  663. {
  664. // Recompute the dep of the package
  665. RemoveStates(Pkg);
  666. UpdateVerState(Pkg);
  667. AddStates(Pkg);
  668. // Update the reverse deps
  669. Update(Pkg.RevDependsList());
  670. // Update the provides map for the current ver
  671. if (Pkg->CurrentVer != 0)
  672. for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
  673. P.end() != true; ++P)
  674. Update(P.ParentPkg().RevDependsList());
  675. // Update the provides map for the candidate ver
  676. if (PkgState[Pkg->ID].CandidateVer != 0)
  677. for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
  678. P.end() != true; ++P)
  679. Update(P.ParentPkg().RevDependsList());
  680. }
  681. /*}}}*/
  682. // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
  683. // ---------------------------------------------------------------------
  684. /* */
  685. bool pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser,
  686. unsigned long Depth)
  687. {
  688. if (IsModeChangeOk(ModeKeep, Pkg, Depth, FromUser) == false)
  689. return false;
  690. /* Reject an attempt to keep a non-source broken installed package, those
  691. must be upgraded */
  692. if (Pkg.State() == PkgIterator::NeedsUnpack &&
  693. Pkg.CurrentVer().Downloadable() == false)
  694. return false;
  695. /* We changed the soft state all the time so the UI is a bit nicer
  696. to use */
  697. StateCache &P = PkgState[Pkg->ID];
  698. // Check that it is not already kept
  699. if (P.Mode == ModeKeep)
  700. return true;
  701. if (Soft == true)
  702. P.iFlags |= AutoKept;
  703. else
  704. P.iFlags &= ~AutoKept;
  705. ActionGroup group(*this);
  706. #if 0 // reseting the autoflag here means we lose the
  707. // auto-mark information if a user selects a package for removal
  708. // but changes his mind then and sets it for keep again
  709. // - this makes sense as default when all Garbage dependencies
  710. // are automatically marked for removal (as aptitude does).
  711. // setting a package for keep then makes it no longer autoinstalled
  712. // for all other use-case this action is rather surprising
  713. if(FromUser && !P.Marked)
  714. P.Flags &= ~Flag::Auto;
  715. #endif
  716. if (DebugMarker == true)
  717. std::clog << OutputInDepth(Depth) << "MarkKeep " << Pkg << " FU=" << FromUser << std::endl;
  718. RemoveSizes(Pkg);
  719. RemoveStates(Pkg);
  720. P.Mode = ModeKeep;
  721. if (Pkg->CurrentVer == 0)
  722. P.InstallVer = 0;
  723. else
  724. P.InstallVer = Pkg.CurrentVer();
  725. AddStates(Pkg);
  726. Update(Pkg);
  727. AddSizes(Pkg);
  728. return true;
  729. }
  730. /*}}}*/
  731. // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
  732. // ---------------------------------------------------------------------
  733. /* */
  734. bool pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge,
  735. unsigned long Depth, bool FromUser)
  736. {
  737. if (IsModeChangeOk(ModeDelete, Pkg, Depth, FromUser) == false)
  738. return false;
  739. StateCache &P = PkgState[Pkg->ID];
  740. // Check that it is not already marked for delete
  741. if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
  742. (Pkg.Purge() == true || rPurge == false))
  743. return true;
  744. // check if we are allowed to remove the package
  745. if (IsDeleteOk(Pkg,rPurge,Depth,FromUser) == false)
  746. return false;
  747. P.iFlags &= ~(AutoKept | Purge);
  748. if (rPurge == true)
  749. P.iFlags |= Purge;
  750. ActionGroup group(*this);
  751. if (DebugMarker == true)
  752. std::clog << OutputInDepth(Depth) << (rPurge ? "MarkPurge " : "MarkDelete ") << Pkg << " FU=" << FromUser << std::endl;
  753. RemoveSizes(Pkg);
  754. RemoveStates(Pkg);
  755. if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
  756. P.Mode = ModeKeep;
  757. else
  758. P.Mode = ModeDelete;
  759. P.InstallVer = 0;
  760. AddStates(Pkg);
  761. Update(Pkg);
  762. AddSizes(Pkg);
  763. return true;
  764. }
  765. /*}}}*/
  766. // DepCache::IsDeleteOk - check if it is ok to remove this package /*{{{*/
  767. // ---------------------------------------------------------------------
  768. /* The default implementation tries to prevent deletion of install requests.
  769. dpkg holds are enforced by the private IsModeChangeOk */
  770. bool pkgDepCache::IsDeleteOk(PkgIterator const &Pkg,bool rPurge,
  771. unsigned long Depth, bool FromUser)
  772. {
  773. return IsDeleteOkProtectInstallRequests(Pkg, rPurge, Depth, FromUser);
  774. }
  775. bool pkgDepCache::IsDeleteOkProtectInstallRequests(PkgIterator const &Pkg,
  776. bool const /*rPurge*/, unsigned long const Depth, bool const FromUser)
  777. {
  778. if (FromUser == false && Pkg->CurrentVer == 0)
  779. {
  780. StateCache &P = PkgState[Pkg->ID];
  781. if (P.InstallVer != 0 && P.Status == 2 && (P.Flags & Flag::Auto) != Flag::Auto)
  782. {
  783. if (DebugMarker == true)
  784. std::clog << OutputInDepth(Depth) << "Manual install request prevents MarkDelete of " << Pkg << std::endl;
  785. return false;
  786. }
  787. }
  788. return true;
  789. }
  790. /*}}}*/
  791. // DepCache::IsModeChangeOk - check if it is ok to change the mode /*{{{*/
  792. // ---------------------------------------------------------------------
  793. /* this is used by all Mark methods on the very first line to check sanity
  794. and prevents mode changes for packages on hold for example.
  795. If you want to check Mode specific stuff you can use the virtual public
  796. Is<Mode>Ok methods instead */
  797. static char const* PrintMode(char const mode)
  798. {
  799. switch (mode)
  800. {
  801. case pkgDepCache::ModeInstall: return "Install";
  802. case pkgDepCache::ModeKeep: return "Keep";
  803. case pkgDepCache::ModeDelete: return "Delete";
  804. case pkgDepCache::ModeGarbage: return "Garbage";
  805. default: return "UNKNOWN";
  806. }
  807. }
  808. bool pkgDepCache::IsModeChangeOk(ModeList const mode, PkgIterator const &Pkg,
  809. unsigned long const Depth, bool const FromUser)
  810. {
  811. // we are not trying to hard…
  812. if (unlikely(Depth > 100))
  813. return false;
  814. // general sanity
  815. if (unlikely(Pkg.end() == true || Pkg->VersionList == 0))
  816. return false;
  817. // the user is always right
  818. if (FromUser == true)
  819. return true;
  820. StateCache &P = PkgState[Pkg->ID];
  821. // not changing the mode is obviously also fine as we might want to call
  822. // e.g. MarkInstall multiple times with different arguments for the same package
  823. if (P.Mode == mode)
  824. return true;
  825. // if previous state was set by user only user can reset it
  826. if ((P.iFlags & Protected) == Protected)
  827. {
  828. if (unlikely(DebugMarker == true))
  829. std::clog << OutputInDepth(Depth) << "Ignore Mark" << PrintMode(mode)
  830. << " of " << Pkg << " as its mode (" << PrintMode(P.Mode)
  831. << ") is protected" << std::endl;
  832. return false;
  833. }
  834. // enforce dpkg holds
  835. else if (mode != ModeKeep && Pkg->SelectedState == pkgCache::State::Hold &&
  836. _config->FindB("APT::Ignore-Hold",false) == false)
  837. {
  838. if (unlikely(DebugMarker == true))
  839. std::clog << OutputInDepth(Depth) << "Hold prevents Mark" << PrintMode(mode)
  840. << " of " << Pkg << std::endl;
  841. return false;
  842. }
  843. return true;
  844. }
  845. /*}}}*/
  846. // DepCache::MarkInstall - Put the package in the install state /*{{{*/
  847. // ---------------------------------------------------------------------
  848. /* */
  849. struct CompareProviders {
  850. pkgCache::PkgIterator const Pkg;
  851. explicit CompareProviders(pkgCache::DepIterator const &Dep) : Pkg(Dep.TargetPkg()) {};
  852. //bool operator() (APT::VersionList::iterator const &AV, APT::VersionList::iterator const &BV)
  853. bool operator() (pkgCache::VerIterator const &AV, pkgCache::VerIterator const &BV)
  854. {
  855. pkgCache::PkgIterator const A = AV.ParentPkg();
  856. pkgCache::PkgIterator const B = BV.ParentPkg();
  857. // Prefer MA:same packages if other architectures for it are installed
  858. if ((AV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same ||
  859. (BV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  860. {
  861. bool instA = false;
  862. if ((AV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  863. {
  864. pkgCache::GrpIterator Grp = A.Group();
  865. for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
  866. if (P->CurrentVer != 0)
  867. {
  868. instA = true;
  869. break;
  870. }
  871. }
  872. bool instB = false;
  873. if ((BV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  874. {
  875. pkgCache::GrpIterator Grp = B.Group();
  876. for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
  877. {
  878. if (P->CurrentVer != 0)
  879. {
  880. instB = true;
  881. break;
  882. }
  883. }
  884. }
  885. if (instA != instB)
  886. return instA == false;
  887. }
  888. // Prefer packages in the same group as the target; e.g. foo:i386, foo:amd64
  889. if (A->Group != B->Group)
  890. {
  891. if (A->Group == Pkg->Group && B->Group != Pkg->Group)
  892. return false;
  893. else if (B->Group == Pkg->Group && A->Group != Pkg->Group)
  894. return true;
  895. }
  896. // we like essentials
  897. if ((A->Flags & pkgCache::Flag::Essential) != (B->Flags & pkgCache::Flag::Essential))
  898. {
  899. if ((A->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  900. return false;
  901. else if ((B->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  902. return true;
  903. }
  904. if ((A->Flags & pkgCache::Flag::Important) != (B->Flags & pkgCache::Flag::Important))
  905. {
  906. if ((A->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
  907. return false;
  908. else if ((B->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
  909. return true;
  910. }
  911. // prefer native architecture
  912. if (strcmp(A.Arch(), B.Arch()) != 0)
  913. {
  914. if (strcmp(A.Arch(), A.Cache()->NativeArch()) == 0)
  915. return false;
  916. else if (strcmp(B.Arch(), B.Cache()->NativeArch()) == 0)
  917. return true;
  918. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  919. for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
  920. if (*a == A.Arch())
  921. return false;
  922. else if (*a == B.Arch())
  923. return true;
  924. }
  925. // higher priority seems like a good idea
  926. if (AV->Priority != BV->Priority)
  927. return AV->Priority > BV->Priority;
  928. // unable to decide…
  929. return A->ID < B->ID;
  930. }
  931. };
  932. bool pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
  933. unsigned long Depth, bool FromUser,
  934. bool ForceImportantDeps)
  935. {
  936. if (IsModeChangeOk(ModeInstall, Pkg, Depth, FromUser) == false)
  937. return false;
  938. StateCache &P = PkgState[Pkg->ID];
  939. // See if there is even any possible instalation candidate
  940. if (P.CandidateVer == 0)
  941. return false;
  942. /* Check that it is not already marked for install and that it can be
  943. installed */
  944. if ((P.InstPolicyBroken() == false && P.InstBroken() == false) &&
  945. (P.Mode == ModeInstall ||
  946. P.CandidateVer == (Version *)Pkg.CurrentVer()))
  947. {
  948. if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
  949. return MarkKeep(Pkg, false, FromUser, Depth+1);
  950. return true;
  951. }
  952. // check if we are allowed to install the package
  953. if (IsInstallOk(Pkg,AutoInst,Depth,FromUser) == false)
  954. return false;
  955. ActionGroup group(*this);
  956. P.iFlags &= ~AutoKept;
  957. /* Target the candidate version and remove the autoflag. We reset the
  958. autoflag below if this was called recursively. Otherwise the user
  959. should have the ability to de-auto a package by changing its state */
  960. RemoveSizes(Pkg);
  961. RemoveStates(Pkg);
  962. P.Mode = ModeInstall;
  963. P.InstallVer = P.CandidateVer;
  964. if(FromUser)
  965. {
  966. // Set it to manual if it's a new install or already installed,
  967. // but only if its not marked by the autoremover (aptitude depend on this behavior)
  968. // or if we do automatic installation (aptitude never does it)
  969. if(P.Status == 2 || (Pkg->CurrentVer != 0 && (AutoInst == true || P.Marked == false)))
  970. P.Flags &= ~Flag::Auto;
  971. }
  972. else
  973. {
  974. // Set it to auto if this is a new install.
  975. if(P.Status == 2)
  976. P.Flags |= Flag::Auto;
  977. }
  978. if (P.CandidateVer == (Version *)Pkg.CurrentVer())
  979. P.Mode = ModeKeep;
  980. AddStates(Pkg);
  981. Update(Pkg);
  982. AddSizes(Pkg);
  983. if (AutoInst == false || _config->Find("APT::Solver", "internal") != "internal")
  984. return true;
  985. if (DebugMarker == true)
  986. std::clog << OutputInDepth(Depth) << "MarkInstall " << Pkg << " FU=" << FromUser << std::endl;
  987. DepIterator Dep = P.InstVerIter(*this).DependsList();
  988. for (; Dep.end() != true;)
  989. {
  990. // Grok or groups
  991. DepIterator Start = Dep;
  992. bool Result = true;
  993. unsigned Ors = 0;
  994. for (bool LastOR = true; Dep.end() == false && LastOR == true; ++Dep, ++Ors)
  995. {
  996. LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
  997. if ((DepState[Dep->ID] & DepInstall) == DepInstall)
  998. Result = false;
  999. }
  1000. // Dep is satisfied okay.
  1001. if (Result == false)
  1002. continue;
  1003. /* Check if this dep should be consider for install. If it is a user
  1004. defined important dep and we are installed a new package then
  1005. it will be installed. Otherwise we only check for important
  1006. deps that have changed from the installed version */
  1007. if (IsImportantDep(Start) == false)
  1008. continue;
  1009. /* If we are in an or group locate the first or that can
  1010. succeed. We have already cached this… */
  1011. for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; --Ors)
  1012. ++Start;
  1013. /* unsatisfiable dependency: IsInstallOkDependenciesSatisfiableByCandidates
  1014. would have prevented us to get here if not overridden, so just skip
  1015. over the problem here as the frontend will know what it is doing */
  1016. if (Ors == 1 && (DepState[Start->ID] &DepCVer) != DepCVer && Start.IsNegative() == false)
  1017. continue;
  1018. /* Check if any ImportantDep() (but not Critical) were added
  1019. * since we installed the package. Also check for deps that
  1020. * were satisfied in the past: for instance, if a version
  1021. * restriction in a Recommends was tightened, upgrading the
  1022. * package should follow that Recommends rather than causing the
  1023. * dependency to be removed. (bug #470115)
  1024. */
  1025. if (Pkg->CurrentVer != 0 && ForceImportantDeps == false && Start.IsCritical() == false)
  1026. {
  1027. bool isNewImportantDep = true;
  1028. bool isPreviouslySatisfiedImportantDep = false;
  1029. for (DepIterator D = Pkg.CurrentVer().DependsList(); D.end() != true; ++D)
  1030. {
  1031. //FIXME: Should we handle or-group better here?
  1032. // We do not check if the package we look for is part of the same or-group
  1033. // we might find while searching, but could that really be a problem?
  1034. if (D.IsCritical() == true || IsImportantDep(D) == false ||
  1035. Start.TargetPkg() != D.TargetPkg())
  1036. continue;
  1037. isNewImportantDep = false;
  1038. while ((D->CompareOp & Dep::Or) != 0)
  1039. ++D;
  1040. isPreviouslySatisfiedImportantDep = (((*this)[D] & DepGNow) != 0);
  1041. if (isPreviouslySatisfiedImportantDep == true)
  1042. break;
  1043. }
  1044. if(isNewImportantDep == true)
  1045. {
  1046. if (DebugAutoInstall == true)
  1047. std::clog << OutputInDepth(Depth) << "new important dependency: "
  1048. << Start.TargetPkg().FullName() << std::endl;
  1049. }
  1050. else if(isPreviouslySatisfiedImportantDep == true)
  1051. {
  1052. if (DebugAutoInstall == true)
  1053. std::clog << OutputInDepth(Depth) << "previously satisfied important dependency on "
  1054. << Start.TargetPkg().FullName() << std::endl;
  1055. }
  1056. else
  1057. {
  1058. if (DebugAutoInstall == true)
  1059. std::clog << OutputInDepth(Depth) << "ignore old unsatisfied important dependency on "
  1060. << Start.TargetPkg().FullName() << std::endl;
  1061. continue;
  1062. }
  1063. }
  1064. /* This bit is for processing the possibility of an install/upgrade
  1065. fixing the problem for "positive" dependencies */
  1066. if (Start.IsNegative() == false && (DepState[Start->ID] & DepCVer) == DepCVer)
  1067. {
  1068. pkgCacheFile CacheFile(this);
  1069. APT::VersionList verlist = APT::VersionList::FromDependency(CacheFile, Start, APT::CacheSetHelper::CANDIDATE);
  1070. CompareProviders comp(Start);
  1071. do {
  1072. APT::VersionList::iterator InstVer = std::max_element(verlist.begin(), verlist.end(), comp);
  1073. if (InstVer == verlist.end())
  1074. break;
  1075. pkgCache::PkgIterator InstPkg = InstVer.ParentPkg();
  1076. if(DebugAutoInstall == true)
  1077. std::clog << OutputInDepth(Depth) << "Installing " << InstPkg.Name()
  1078. << " as " << Start.DepType() << " of " << Pkg.Name()
  1079. << std::endl;
  1080. if (MarkInstall(InstPkg, true, Depth + 1, false, ForceImportantDeps) == false)
  1081. {
  1082. verlist.erase(InstVer);
  1083. continue;
  1084. }
  1085. // now check if we should consider it a automatic dependency or not
  1086. if(InstPkg->CurrentVer == 0 && InstVer->Section != 0 && ConfigValueInSubTree("APT::Never-MarkAuto-Sections", InstVer.Section()))
  1087. {
  1088. if(DebugAutoInstall == true)
  1089. std::clog << OutputInDepth(Depth) << "Setting NOT as auto-installed (direct "
  1090. << Start.DepType() << " of pkg in APT::Never-MarkAuto-Sections)" << std::endl;
  1091. MarkAuto(InstPkg, false);
  1092. }
  1093. break;
  1094. } while(true);
  1095. continue;
  1096. }
  1097. /* Negative dependencies have no or-group
  1098. If the dependency isn't versioned, we try if an upgrade might solve the problem.
  1099. Otherwise we remove the offender if needed */
  1100. else if (Start.IsNegative() == true && Start->Type != pkgCache::Dep::Obsoletes)
  1101. {
  1102. SPtrArray<Version *> List = Start.AllTargets();
  1103. pkgCache::PkgIterator TrgPkg = Start.TargetPkg();
  1104. for (Version **I = List; *I != 0; I++)
  1105. {
  1106. VerIterator Ver(*this,*I);
  1107. PkgIterator Pkg = Ver.ParentPkg();
  1108. /* The List includes all packages providing this dependency,
  1109. even providers which are not installed, so skip them. */
  1110. if (PkgState[Pkg->ID].InstallVer == 0)
  1111. continue;
  1112. /* Ignore negative dependencies that we are not going to
  1113. get installed */
  1114. if (PkgState[Pkg->ID].InstallVer != *I)
  1115. continue;
  1116. if ((Start->Version != 0 || TrgPkg != Pkg) &&
  1117. PkgState[Pkg->ID].CandidateVer != PkgState[Pkg->ID].InstallVer &&
  1118. PkgState[Pkg->ID].CandidateVer != *I &&
  1119. MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps) == true)
  1120. continue;
  1121. else if (Start->Type == pkgCache::Dep::Conflicts ||
  1122. Start->Type == pkgCache::Dep::DpkgBreaks)
  1123. {
  1124. if(DebugAutoInstall == true)
  1125. std::clog << OutputInDepth(Depth)
  1126. << " Removing: " << Pkg.Name()
  1127. << std::endl;
  1128. if (MarkDelete(Pkg,false,Depth + 1, false) == false)
  1129. break;
  1130. }
  1131. }
  1132. continue;
  1133. }
  1134. }
  1135. return Dep.end() == true;
  1136. }
  1137. /*}}}*/
  1138. // DepCache::IsInstallOk - check if it is ok to install this package /*{{{*/
  1139. // ---------------------------------------------------------------------
  1140. /* The default implementation checks if the installation of an M-A:same
  1141. package would lead us into a version-screw and if so forbids it.
  1142. dpkg holds are enforced by the private IsModeChangeOk */
  1143. bool pkgDepCache::IsInstallOk(PkgIterator const &Pkg,bool AutoInst,
  1144. unsigned long Depth, bool FromUser)
  1145. {
  1146. return IsInstallOkMultiArchSameVersionSynced(Pkg,AutoInst, Depth, FromUser) &&
  1147. IsInstallOkDependenciesSatisfiableByCandidates(Pkg,AutoInst, Depth, FromUser);
  1148. }
  1149. bool pkgDepCache::IsInstallOkMultiArchSameVersionSynced(PkgIterator const &Pkg,
  1150. bool const /*AutoInst*/, unsigned long const Depth, bool const FromUser)
  1151. {
  1152. if (FromUser == true) // as always: user is always right
  1153. return true;
  1154. // if we have checked before and it was okay, it will still be okay
  1155. if (PkgState[Pkg->ID].Mode == ModeInstall &&
  1156. PkgState[Pkg->ID].InstallVer == PkgState[Pkg->ID].CandidateVer)
  1157. return true;
  1158. // ignore packages with none-M-A:same candidates
  1159. VerIterator const CandVer = PkgState[Pkg->ID].CandidateVerIter(*this);
  1160. if (unlikely(CandVer.end() == true) || CandVer == Pkg.CurrentVer() ||
  1161. (CandVer->MultiArch & pkgCache::Version::Same) != pkgCache::Version::Same)
  1162. return true;
  1163. GrpIterator const Grp = Pkg.Group();
  1164. for (PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
  1165. {
  1166. // not installed or self-check: fine by definition
  1167. if (P->CurrentVer == 0 || P == Pkg)
  1168. continue;
  1169. // not having a candidate or being in sync
  1170. // (simple string-compare as stuff like '1' == '0:1-0' can't happen here)
  1171. VerIterator CV = PkgState[P->ID].CandidateVerIter(*this);
  1172. if (CV.end() == true || strcmp(Pkg.CandVersion(), CV.VerStr()) == 0)
  1173. continue;
  1174. // packages losing M-A:same can be out-of-sync
  1175. if ((CV->MultiArch & pkgCache::Version::Same) != pkgCache::Version::Same)
  1176. continue;
  1177. // not downloadable means the package is obsolete, so allow out-of-sync
  1178. if (CV.Downloadable() == false)
  1179. continue;
  1180. PkgState[Pkg->ID].iFlags |= AutoKept;
  1181. if (unlikely(DebugMarker == true))
  1182. std::clog << OutputInDepth(Depth) << "Ignore MarkInstall of " << Pkg
  1183. << " as it is not in sync with its M-A:same sibling " << P
  1184. << " (" << Pkg.CandVersion() << " != " << CV.VerStr() << ")" << std::endl;
  1185. return false;
  1186. }
  1187. return true;
  1188. }
  1189. bool pkgDepCache::IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator const &Pkg,
  1190. bool const AutoInst, unsigned long const Depth, bool const /*FromUser*/)
  1191. {
  1192. if (AutoInst == false)
  1193. return true;
  1194. VerIterator const CandVer = PkgState[Pkg->ID].CandidateVerIter(*this);
  1195. if (unlikely(CandVer.end() == true) || CandVer == Pkg.CurrentVer())
  1196. return true;
  1197. for (DepIterator Dep = CandVer.DependsList(); Dep.end() != true;)
  1198. {
  1199. // Grok or groups
  1200. DepIterator Start = Dep;
  1201. bool Result = true;
  1202. unsigned Ors = 0;
  1203. for (bool LastOR = true; Dep.end() == false && LastOR == true; ++Dep, ++Ors)
  1204. {
  1205. LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
  1206. if ((DepState[Dep->ID] & DepInstall) == DepInstall)
  1207. Result = false;
  1208. }
  1209. if (Start.IsCritical() == false || Start.IsNegative() == true || Result == false)
  1210. continue;
  1211. /* If we are in an or group locate the first or that can succeed.
  1212. We have already cached this… */
  1213. for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; --Ors)
  1214. ++Start;
  1215. if (Ors == 1 && (DepState[Start->ID] &DepCVer) != DepCVer)
  1216. {
  1217. if (DebugAutoInstall == true)
  1218. std::clog << OutputInDepth(Depth) << Start << " can't be satisfied!" << std::endl;
  1219. // the dependency is critical, but can't be installed, so discard the candidate
  1220. // as the problemresolver will trip over it otherwise trying to install it (#735967)
  1221. if (Pkg->CurrentVer != 0 && (PkgState[Pkg->ID].iFlags & Protected) != Protected)
  1222. SetCandidateVersion(Pkg.CurrentVer());
  1223. return false;
  1224. }
  1225. }
  1226. return true;
  1227. }
  1228. /*}}}*/
  1229. // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
  1230. // ---------------------------------------------------------------------
  1231. /* */
  1232. void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
  1233. {
  1234. if (unlikely(Pkg.end() == true))
  1235. return;
  1236. APT::PackageList pkglist;
  1237. if (Pkg->CurrentVer != 0 &&
  1238. (Pkg.CurrentVer()-> MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  1239. {
  1240. pkgCache::GrpIterator Grp = Pkg.Group();
  1241. for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
  1242. {
  1243. if (P->CurrentVer != 0)
  1244. pkglist.insert(P);
  1245. }
  1246. }
  1247. else
  1248. pkglist.insert(Pkg);
  1249. ActionGroup group(*this);
  1250. for (APT::PackageList::const_iterator Pkg = pkglist.begin(); Pkg != pkglist.end(); ++Pkg)
  1251. {
  1252. RemoveSizes(Pkg);
  1253. RemoveStates(Pkg);
  1254. StateCache &P = PkgState[Pkg->ID];
  1255. if (To == true)
  1256. P.iFlags |= ReInstall;
  1257. else
  1258. P.iFlags &= ~ReInstall;
  1259. AddStates(Pkg);
  1260. AddSizes(Pkg);
  1261. }
  1262. }
  1263. /*}}}*/
  1264. // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
  1265. // ---------------------------------------------------------------------
  1266. /* */
  1267. void pkgDepCache::SetCandidateVersion(VerIterator TargetVer)
  1268. {
  1269. pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
  1270. StateCache &P = PkgState[Pkg->ID];
  1271. if (P.CandidateVer == TargetVer)
  1272. return;
  1273. ActionGroup group(*this);
  1274. RemoveSizes(Pkg);
  1275. RemoveStates(Pkg);
  1276. if (P.CandidateVer == P.InstallVer && P.Install() == true)
  1277. P.InstallVer = (Version *)TargetVer;
  1278. P.CandidateVer = (Version *)TargetVer;
  1279. P.Update(Pkg,*this);
  1280. AddStates(Pkg);
  1281. Update(Pkg);
  1282. AddSizes(Pkg);
  1283. }
  1284. /*}}}*/
  1285. // DepCache::SetCandidateRelease - Change the candidate version /*{{{*/
  1286. // ---------------------------------------------------------------------
  1287. /* changes the candidate of a package and walks over all its dependencies
  1288. to check if it needs to change the candidate of the dependency, too,
  1289. to reach a installable versionstate */
  1290. bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer,
  1291. std::string const &TargetRel)
  1292. {
  1293. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > Changed;
  1294. return SetCandidateRelease(TargetVer, TargetRel, Changed);
  1295. }
  1296. bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer,
  1297. std::string const &TargetRel,
  1298. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed)
  1299. {
  1300. ActionGroup group(*this);
  1301. SetCandidateVersion(TargetVer);
  1302. if (TargetRel == "installed" || TargetRel == "candidate") // both doesn't make sense in this context
  1303. return true;
  1304. pkgVersionMatch Match(TargetRel, pkgVersionMatch::Release);
  1305. // save the position of the last element we will not undo - if we have to
  1306. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::iterator newChanged = --(Changed.end());
  1307. for (pkgCache::DepIterator D = TargetVer.DependsList(); D.end() == false; ++D)
  1308. {
  1309. if (D->Type != pkgCache::Dep::PreDepends && D->Type != pkgCache::Dep::Depends &&
  1310. ((D->Type != pkgCache::Dep::Recommends && D->Type != pkgCache::Dep::Suggests) ||
  1311. IsImportantDep(D) == false))
  1312. continue;
  1313. // walk over an or-group and check if we need to do anything
  1314. // for simpilicity no or-group is handled as a or-group including one dependency
  1315. pkgCache::DepIterator Start = D;
  1316. bool itsFine = false;
  1317. for (bool stillOr = true; stillOr == true; ++Start)
  1318. {
  1319. stillOr = (Start->CompareOp & Dep::Or) == Dep::Or;
  1320. pkgCache::PkgIterator const P = Start.TargetPkg();
  1321. // virtual packages can't be a solution
  1322. if (P.end() == true || (P->ProvidesList == 0 && P->VersionList == 0))
  1323. continue;
  1324. pkgCache::VerIterator const Cand = PkgState[P->ID].CandidateVerIter(*this);
  1325. // no versioned dependency - but is it installable?
  1326. if (Start.TargetVer() == 0 || Start.TargetVer()[0] == '\0')
  1327. {
  1328. // Check if one of the providers is installable
  1329. if (P->ProvidesList != 0)
  1330. {
  1331. pkgCache::PrvIterator Prv = P.ProvidesList();
  1332. for (; Prv.end() == false; ++Prv)
  1333. {
  1334. pkgCache::VerIterator const C = PkgState[Prv.OwnerPkg()->ID].CandidateVerIter(*this);
  1335. if (C.end() == true || C != Prv.OwnerVer() ||
  1336. (VersionState(C.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) != DepCandMin)
  1337. continue;
  1338. break;
  1339. }
  1340. if (Prv.end() == true)
  1341. continue;
  1342. }
  1343. // no providers, so check if we have an installable candidate version
  1344. else if (Cand.end() == true ||
  1345. (VersionState(Cand.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) != DepCandMin)
  1346. continue;
  1347. itsFine = true;
  1348. break;
  1349. }
  1350. if (Cand.end() == true)
  1351. continue;
  1352. // check if the current candidate is enough for the versioned dependency - and installable?
  1353. if (Start.IsSatisfied(Cand) == true &&
  1354. (VersionState(Cand.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) == DepCandMin)
  1355. {
  1356. itsFine = true;
  1357. break;
  1358. }
  1359. }
  1360. if (itsFine == true) {
  1361. // something in the or-group was fine, skip all other members
  1362. for (; (D->CompareOp & Dep::Or) == Dep::Or; ++D);
  1363. continue;
  1364. }
  1365. // walk again over the or-group and check each if a candidate switch would help
  1366. itsFine = false;
  1367. for (bool stillOr = true; stillOr == true; ++D)
  1368. {
  1369. stillOr = (D->CompareOp & Dep::Or) == Dep::Or;
  1370. // changing candidate will not help if the dependency is not versioned
  1371. if (D.TargetVer() == 0 || D.TargetVer()[0] == '\0')
  1372. {
  1373. if (stillOr == true)
  1374. continue;
  1375. break;
  1376. }
  1377. pkgCache::VerIterator V;
  1378. if (TargetRel == "newest")
  1379. V = D.TargetPkg().VersionList();
  1380. else
  1381. V = Match.Find(D.TargetPkg());
  1382. // check if the version from this release could satisfy the dependency
  1383. if (V.end() == true || D.IsSatisfied(V) == false)
  1384. {
  1385. if (stillOr == true)
  1386. continue;
  1387. break;
  1388. }
  1389. pkgCache::VerIterator oldCand = PkgState[D.TargetPkg()->ID].CandidateVerIter(*this);
  1390. if (V == oldCand)
  1391. {
  1392. // Do we already touched this Version? If so, their versioned dependencies are okay, no need to check again
  1393. for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = Changed.begin();
  1394. c != Changed.end(); ++c)
  1395. {
  1396. if (c->first->ParentPkg != V->ParentPkg)
  1397. continue;
  1398. itsFine = true;
  1399. break;
  1400. }
  1401. }
  1402. if (itsFine == false)
  1403. {
  1404. // change the candidate
  1405. Changed.push_back(make_pair(V, TargetVer));
  1406. if (SetCandidateRelease(V, TargetRel, Changed) == false)
  1407. {
  1408. if (stillOr == false)
  1409. break;
  1410. // undo the candidate changing
  1411. SetCandidateVersion(oldCand);
  1412. Changed.pop_back();
  1413. continue;
  1414. }
  1415. itsFine = true;
  1416. }
  1417. // something in the or-group was fine, skip all other members
  1418. for (; (D->CompareOp & Dep::Or) == Dep::Or; ++D);
  1419. break;
  1420. }
  1421. if (itsFine == false && (D->Type == pkgCache::Dep::PreDepends || D->Type == pkgCache::Dep::Depends))
  1422. {
  1423. // undo all changes which aren't lead to a solution
  1424. for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = ++newChanged;
  1425. c != Changed.end(); ++c)
  1426. SetCandidateVersion(c->first);
  1427. Changed.erase(newChanged, Changed.end());
  1428. return false;
  1429. }
  1430. }
  1431. return true;
  1432. }
  1433. /*}}}*/
  1434. // DepCache::MarkAuto - set the Auto flag for a package /*{{{*/
  1435. // ---------------------------------------------------------------------
  1436. /* */
  1437. void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
  1438. {
  1439. StateCache &state = PkgState[Pkg->ID];
  1440. ActionGroup group(*this);
  1441. if(Auto)
  1442. state.Flags |= Flag::Auto;
  1443. else
  1444. state.Flags &= ~Flag::Auto;
  1445. }
  1446. /*}}}*/
  1447. // StateCache::Update - Compute the various static display things /*{{{*/
  1448. // ---------------------------------------------------------------------
  1449. /* This is called whenever the Candidate version changes. */
  1450. void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
  1451. {
  1452. // Some info
  1453. VerIterator Ver = CandidateVerIter(Cache);
  1454. // Use a null string or the version string
  1455. if (Ver.end() == true)
  1456. CandVersion = "";
  1457. else
  1458. CandVersion = Ver.VerStr();
  1459. // Find the current version
  1460. CurVersion = "";
  1461. if (Pkg->CurrentVer != 0)
  1462. CurVersion = Pkg.CurrentVer().VerStr();
  1463. // Strip off the epochs for display
  1464. CurVersion = StripEpoch(CurVersion);
  1465. CandVersion = StripEpoch(CandVersion);
  1466. // Figure out if its up or down or equal
  1467. Status = Ver.CompareVer(Pkg.CurrentVer());
  1468. if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
  1469. Status = 2;
  1470. }
  1471. /*}}}*/
  1472. // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
  1473. // ---------------------------------------------------------------------
  1474. /* */
  1475. const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
  1476. {
  1477. if (Ver == 0)
  1478. return 0;
  1479. // Strip any epoch
  1480. char const * const I = strchr(Ver, ':');
  1481. if (I == nullptr)
  1482. return Ver;
  1483. return I + 1;
  1484. }
  1485. /*}}}*/
  1486. // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
  1487. // ---------------------------------------------------------------------
  1488. /* The default just returns the highest available version that is not
  1489. a source and automatic. */
  1490. pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator const &Pkg)
  1491. {
  1492. /* Not source/not automatic versions cannot be a candidate version
  1493. unless they are already installed */
  1494. VerIterator Last;
  1495. for (VerIterator I = Pkg.VersionList(); I.end() == false; ++I)
  1496. {
  1497. if (Pkg.CurrentVer() == I)
  1498. return I;
  1499. for (VerFileIterator J = I.FileList(); J.end() == false; ++J)
  1500. {
  1501. if (J.File().Flagged(Flag::NotSource))
  1502. continue;
  1503. /* Stash the highest version of a not-automatic source, we use it
  1504. if there is nothing better */
  1505. if (J.File().Flagged(Flag::NotAutomatic) ||
  1506. J.File().Flagged(Flag::ButAutomaticUpgrades))
  1507. {
  1508. if (Last.end() == true)
  1509. Last = I;
  1510. continue;
  1511. }
  1512. return I;
  1513. }
  1514. }
  1515. return Last;
  1516. }
  1517. /*}}}*/
  1518. // Policy::IsImportantDep - True if the dependency is important /*{{{*/
  1519. // ---------------------------------------------------------------------
  1520. /* */
  1521. bool pkgDepCache::Policy::IsImportantDep(DepIterator const &Dep) const
  1522. {
  1523. if(Dep.IsCritical())
  1524. return true;
  1525. else if(Dep->Type == pkgCache::Dep::Recommends)
  1526. {
  1527. if (InstallRecommends)
  1528. return true;
  1529. // we suport a special mode to only install-recommends for certain
  1530. // sections
  1531. // FIXME: this is a meant as a temporarly solution until the
  1532. // recommends are cleaned up
  1533. const char *sec = Dep.ParentVer().Section();
  1534. if (sec && ConfigValueInSubTree("APT::Install-Recommends-Sections", sec))
  1535. return true;
  1536. }
  1537. else if(Dep->Type == pkgCache::Dep::Suggests)
  1538. return InstallSuggests;
  1539. return false;
  1540. }
  1541. /*}}}*/
  1542. // Policy::GetPriority - Get the priority of the package pin /*{{{*/
  1543. APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgIterator const &/*Pkg*/)
  1544. { return 0; }
  1545. APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgFileIterator const &/*File*/)
  1546. { return 0; }
  1547. /*}}}*/
  1548. pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc() /*{{{*/
  1549. {
  1550. DefaultRootSetFunc *f = new DefaultRootSetFunc;
  1551. if(f->wasConstructedSuccessfully())
  1552. return f;
  1553. else
  1554. {
  1555. delete f;
  1556. return NULL;
  1557. }
  1558. }
  1559. /*}}}*/
  1560. bool pkgDepCache::MarkFollowsRecommends()
  1561. {
  1562. return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
  1563. }
  1564. bool pkgDepCache::MarkFollowsSuggests()
  1565. {
  1566. return _config->FindB("APT::AutoRemove::SuggestsImportant", true);
  1567. }
  1568. // pkgDepCache::MarkRequired - the main mark algorithm /*{{{*/
  1569. bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
  1570. {
  1571. if (_config->Find("APT::Solver", "internal") != "internal")
  1572. return true;
  1573. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  1574. // init the states
  1575. map_id_t const PackagesCount = Head().PackageCount;
  1576. for(map_id_t i = 0; i < PackagesCount; ++i)
  1577. {
  1578. PkgState[i].Marked = false;
  1579. PkgState[i].Garbage = false;
  1580. }
  1581. if (debug_autoremove)
  1582. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1583. if(PkgState[p->ID].Flags & Flag::Auto)
  1584. std::clog << "AutoDep: " << p.FullName() << std::endl;
  1585. bool const follow_recommends = MarkFollowsRecommends();
  1586. bool const follow_suggests = MarkFollowsSuggests();
  1587. // do the mark part, this is the core bit of the algorithm
  1588. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1589. {
  1590. if(!(PkgState[p->ID].Flags & Flag::Auto) ||
  1591. (p->Flags & Flag::Essential) ||
  1592. (p->Flags & Flag::Important) ||
  1593. userFunc.InRootSet(p) ||
  1594. // be nice even then a required package violates the policy (#583517)
  1595. // and do the full mark process also for required packages
  1596. (p.CurrentVer().end() != true &&
  1597. p.CurrentVer()->Priority == pkgCache::State::Required) ||
  1598. // packages which can't be changed (like holds) can't be garbage
  1599. (IsModeChangeOk(ModeGarbage, p, 0, false) == false))
  1600. {
  1601. // the package is installed (and set to keep)
  1602. if(PkgState[p->ID].Keep() && !p.CurrentVer().end())
  1603. MarkPackage(p, p.CurrentVer(),
  1604. follow_recommends, follow_suggests);
  1605. // the package is to be installed
  1606. else if(PkgState[p->ID].Install())
  1607. MarkPackage(p, PkgState[p->ID].InstVerIter(*this),
  1608. follow_recommends, follow_suggests);
  1609. }
  1610. }
  1611. return true;
  1612. }
  1613. /*}}}*/
  1614. // MarkPackage - mark a single package in Mark-and-Sweep /*{{{*/
  1615. void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
  1616. const pkgCache::VerIterator &ver,
  1617. bool const &follow_recommends,
  1618. bool const &follow_suggests)
  1619. {
  1620. pkgDepCache::StateCache &state = PkgState[pkg->ID];
  1621. // if we are marked already we are done
  1622. if(state.Marked)
  1623. return;
  1624. VerIterator const currver = pkg.CurrentVer();
  1625. VerIterator const instver = state.InstVerIter(*this);
  1626. #if 0
  1627. VerIterator const candver = state.CandidateVerIter(*this);
  1628. // If a package was garbage-collected but is now being marked, we
  1629. // should re-select it
  1630. // For cases when a pkg is set to upgrade and this trigger the
  1631. // removal of a no-longer used dependency. if the pkg is set to
  1632. // keep again later it will result in broken deps
  1633. if(state.Delete() && state.RemoveReason = Unused)
  1634. {
  1635. if(ver==candver)
  1636. mark_install(pkg, false, false, NULL);
  1637. else if(ver==pkg.CurrentVer())
  1638. MarkKeep(pkg, false, false);
  1639. instver=state.InstVerIter(*this);
  1640. }
  1641. #endif
  1642. // For packages that are not going to be removed, ignore versions
  1643. // other than the InstVer. For packages that are going to be
  1644. // removed, ignore versions other than the current version.
  1645. if(!(ver == instver && !instver.end()) &&
  1646. !(ver == currver && instver.end() && !ver.end()))
  1647. return;
  1648. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove", false);
  1649. if(debug_autoremove)
  1650. {
  1651. std::clog << "Marking: " << pkg.FullName();
  1652. if(!ver.end())
  1653. std::clog << " " << ver.VerStr();
  1654. if(!currver.end())
  1655. std::clog << ", Curr=" << currver.VerStr();
  1656. if(!instver.end())
  1657. std::clog << ", Inst=" << instver.VerStr();
  1658. std::clog << std::endl;
  1659. }
  1660. state.Marked=true;
  1661. if(ver.end() == true)
  1662. return;
  1663. for(DepIterator d = ver.DependsList(); !d.end(); ++d)
  1664. {
  1665. if(d->Type == Dep::Depends ||
  1666. d->Type == Dep::PreDepends ||
  1667. (follow_recommends &&
  1668. d->Type == Dep::Recommends) ||
  1669. (follow_suggests &&
  1670. d->Type == Dep::Suggests))
  1671. {
  1672. // Try all versions of this package.
  1673. for(VerIterator V = d.TargetPkg().VersionList();
  1674. !V.end(); ++V)
  1675. {
  1676. if(d.IsSatisfied(V))
  1677. {
  1678. if(debug_autoremove)
  1679. {
  1680. std::clog << "Following dep: " << d.ParentPkg().FullName()
  1681. << " " << d.ParentVer().VerStr() << " "
  1682. << d.DepType() << " " << d.TargetPkg().FullName();
  1683. if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
  1684. {
  1685. std::clog << " (" << d.CompType() << " "
  1686. << d.TargetVer() << ")";
  1687. }
  1688. std::clog << std::endl;
  1689. }
  1690. MarkPackage(V.ParentPkg(), V,
  1691. follow_recommends, follow_suggests);
  1692. }
  1693. }
  1694. // Now try virtual packages
  1695. for(PrvIterator prv=d.TargetPkg().ProvidesList();
  1696. !prv.end(); ++prv)
  1697. {
  1698. if(d.IsSatisfied(prv))
  1699. {
  1700. if(debug_autoremove)
  1701. {
  1702. std::clog << "Following dep: " << d.ParentPkg().FullName() << " "
  1703. << d.ParentVer().VerStr() << " "
  1704. << d.DepType() << " " << d.TargetPkg().FullName() << " ";
  1705. if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
  1706. {
  1707. std::clog << " (" << d.CompType() << " "
  1708. << d.TargetVer() << ")";
  1709. }
  1710. std::clog << ", provided by "
  1711. << prv.OwnerPkg().FullName() << " "
  1712. << prv.OwnerVer().VerStr()
  1713. << std::endl;
  1714. }
  1715. MarkPackage(prv.OwnerPkg(), prv.OwnerVer(),
  1716. follow_recommends, follow_suggests);
  1717. }
  1718. }
  1719. }
  1720. }
  1721. }
  1722. /*}}}*/
  1723. bool pkgDepCache::Sweep() /*{{{*/
  1724. {
  1725. bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  1726. // do the sweep
  1727. for(PkgIterator p=PkgBegin(); !p.end(); ++p)
  1728. {
  1729. StateCache &state=PkgState[p->ID];
  1730. // skip required packages
  1731. if (!p.CurrentVer().end() &&
  1732. (p.CurrentVer()->Priority == pkgCache::State::Required))
  1733. continue;
  1734. // if it is not marked and it is installed, it's garbage
  1735. if(!state.Marked && (!p.CurrentVer().end() || state.Install()))
  1736. {
  1737. state.Garbage=true;
  1738. if(debug_autoremove)
  1739. std::clog << "Garbage: " << p.FullName() << std::endl;
  1740. }
  1741. }
  1742. return true;
  1743. }
  1744. /*}}}*/
  1745. // DepCache::MarkAndSweep /*{{{*/
  1746. bool pkgDepCache::MarkAndSweep(InRootSetFunc &rootFunc)
  1747. {
  1748. return MarkRequired(rootFunc) && Sweep();
  1749. }
  1750. bool pkgDepCache::MarkAndSweep()
  1751. {
  1752. std::auto_ptr<InRootSetFunc> f(GetRootSetFunc());
  1753. if(f.get() != NULL)
  1754. return MarkAndSweep(*f.get());
  1755. else
  1756. return false;
  1757. }
  1758. /*}}}*/