depcache.cc 63 KB

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