depcache.cc 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  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 <sstream>
  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. cache(cache), released(false)
  60. {
  61. ++cache.group_level;
  62. }
  63. void pkgDepCache::ActionGroup::release()
  64. {
  65. if(!released)
  66. {
  67. if(cache.group_level == 0)
  68. std::cerr << "W: Unbalanced action groups, expect badness" << std::endl;
  69. else
  70. {
  71. --cache.group_level;
  72. if(cache.group_level == 0)
  73. cache.MarkAndSweep();
  74. }
  75. released = true;
  76. }
  77. }
  78. pkgDepCache::ActionGroup::~ActionGroup()
  79. {
  80. release();
  81. }
  82. /*}}}*/
  83. // DepCache::pkgDepCache - Constructors /*{{{*/
  84. // ---------------------------------------------------------------------
  85. /* */
  86. pkgDepCache::pkgDepCache(pkgCache *pCache,Policy *Plcy) :
  87. group_level(0), Cache(pCache), PkgState(0), DepState(0)
  88. {
  89. DebugMarker = _config->FindB("Debug::pkgDepCache::Marker", false);
  90. DebugAutoInstall = _config->FindB("Debug::pkgDepCache::AutoInstall", false);
  91. delLocalPolicy = 0;
  92. LocalPolicy = Plcy;
  93. if (LocalPolicy == 0)
  94. delLocalPolicy = LocalPolicy = new Policy;
  95. }
  96. /*}}}*/
  97. // DepCache::~pkgDepCache - Destructor /*{{{*/
  98. // ---------------------------------------------------------------------
  99. /* */
  100. pkgDepCache::~pkgDepCache()
  101. {
  102. delete [] PkgState;
  103. delete [] DepState;
  104. delete delLocalPolicy;
  105. }
  106. /*}}}*/
  107. // DepCache::Init - Generate the initial extra structures. /*{{{*/
  108. // ---------------------------------------------------------------------
  109. /* This allocats the extension buffers and initializes them. */
  110. bool pkgDepCache::Init(OpProgress *Prog)
  111. {
  112. // Suppress mark updates during this operation (just in case) and
  113. // run a mark operation when Init terminates.
  114. ActionGroup actions(*this);
  115. delete [] PkgState;
  116. delete [] DepState;
  117. PkgState = new StateCache[Head().PackageCount];
  118. DepState = new unsigned char[Head().DependsCount];
  119. memset(PkgState,0,sizeof(*PkgState)*Head().PackageCount);
  120. memset(DepState,0,sizeof(*DepState)*Head().DependsCount);
  121. if (Prog != 0)
  122. {
  123. Prog->OverallProgress(0,2*Head().PackageCount,Head().PackageCount,
  124. _("Building dependency tree"));
  125. Prog->SubProgress(Head().PackageCount,_("Candidate versions"));
  126. }
  127. /* Set the current state of everything. In this state all of the
  128. packages are kept exactly as is. See AllUpgrade */
  129. int Done = 0;
  130. for (PkgIterator I = PkgBegin(); I.end() != true; ++I, ++Done)
  131. {
  132. if (Prog != 0 && Done%20 == 0)
  133. Prog->Progress(Done);
  134. // Find the proper cache slot
  135. StateCache &State = PkgState[I->ID];
  136. State.iFlags = 0;
  137. // Figure out the install version
  138. State.CandidateVer = GetCandidateVer(I);
  139. State.InstallVer = I.CurrentVer();
  140. State.Mode = ModeKeep;
  141. State.Update(I,*this);
  142. }
  143. if (Prog != 0)
  144. {
  145. Prog->OverallProgress(Head().PackageCount,2*Head().PackageCount,
  146. Head().PackageCount,
  147. _("Building dependency tree"));
  148. Prog->SubProgress(Head().PackageCount,_("Dependency generation"));
  149. }
  150. Update(Prog);
  151. if(Prog != 0)
  152. Prog->Done();
  153. return true;
  154. }
  155. /*}}}*/
  156. bool pkgDepCache::readStateFile(OpProgress *Prog) /*{{{*/
  157. {
  158. FileFd state_file;
  159. string const state = _config->FindFile("Dir::State::extended_states");
  160. if(RealFileExists(state)) {
  161. state_file.Open(state, FileFd::ReadOnly);
  162. off_t const file_size = state_file.Size();
  163. if(Prog != NULL)
  164. Prog->OverallProgress(0, file_size, 1,
  165. _("Reading state information"));
  166. pkgTagFile tagfile(&state_file);
  167. pkgTagSection section;
  168. off_t amt = 0;
  169. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  170. while(tagfile.Step(section)) {
  171. string const pkgname = section.FindS("Package");
  172. string pkgarch = section.FindS("Architecture");
  173. if (pkgarch.empty() == true)
  174. pkgarch = "any";
  175. pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
  176. // Silently ignore unknown packages and packages with no actual version.
  177. if(pkg.end() == true || pkg->VersionList == 0)
  178. continue;
  179. short const reason = section.FindI("Auto-Installed", 0);
  180. if(reason > 0)
  181. {
  182. PkgState[pkg->ID].Flags |= Flag::Auto;
  183. if (unlikely(debug_autoremove))
  184. std::clog << "Auto-Installed : " << pkg.FullName() << std::endl;
  185. if (pkgarch == "any")
  186. {
  187. pkgCache::GrpIterator G = pkg.Group();
  188. for (pkg = G.NextPkg(pkg); pkg.end() != true; pkg = G.NextPkg(pkg))
  189. if (pkg->VersionList != 0)
  190. PkgState[pkg->ID].Flags |= Flag::Auto;
  191. }
  192. }
  193. amt += section.size();
  194. if(Prog != NULL)
  195. Prog->OverallProgress(amt, file_size, 1,
  196. _("Reading state information"));
  197. }
  198. if(Prog != NULL)
  199. Prog->OverallProgress(file_size, file_size, 1,
  200. _("Reading state information"));
  201. }
  202. return true;
  203. }
  204. /*}}}*/
  205. bool pkgDepCache::writeStateFile(OpProgress * /*prog*/, bool InstalledOnly) /*{{{*/
  206. {
  207. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  208. if(debug_autoremove)
  209. std::clog << "pkgDepCache::writeStateFile()" << std::endl;
  210. FileFd StateFile;
  211. string const state = _config->FindFile("Dir::State::extended_states");
  212. if (CreateAPTDirectoryIfNeeded(_config->FindDir("Dir::State"), flNotFile(state)) == false)
  213. return false;
  214. // if it does not exist, create a empty one
  215. if(!RealFileExists(state))
  216. {
  217. StateFile.Open(state, FileFd::WriteAtomic);
  218. StateFile.Close();
  219. }
  220. // open it
  221. if(!StateFile.Open(state, FileFd::ReadOnly))
  222. return _error->Error(_("Failed to open StateFile %s"),
  223. state.c_str());
  224. FILE *OutFile;
  225. string const outfile = state + ".tmp";
  226. if((OutFile = fopen(outfile.c_str(),"w")) == NULL)
  227. return _error->Error(_("Failed to write temporary StateFile %s"),
  228. outfile.c_str());
  229. // first merge with the existing sections
  230. pkgTagFile tagfile(&StateFile);
  231. pkgTagSection section;
  232. std::set<string> pkgs_seen;
  233. const char *nullreorderlist[] = {0};
  234. while(tagfile.Step(section)) {
  235. string const pkgname = section.FindS("Package");
  236. string pkgarch = section.FindS("Architecture");
  237. if (pkgarch.empty() == true)
  238. pkgarch = "native";
  239. // Silently ignore unknown packages and packages with no actual
  240. // version.
  241. pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
  242. if(pkg.end() || pkg.VersionList().end())
  243. continue;
  244. StateCache const &P = PkgState[pkg->ID];
  245. bool newAuto = (P.Flags & Flag::Auto);
  246. // skip not installed or now-removed ones if requested
  247. if (InstalledOnly && (
  248. (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
  249. (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
  250. {
  251. // The section is obsolete if it contains no other tag
  252. unsigned int const count = section.Count();
  253. if (count < 2 ||
  254. (count == 2 && section.Exists("Auto-Installed")) ||
  255. (count == 3 && section.Exists("Auto-Installed") && section.Exists("Architecture")))
  256. continue;
  257. else
  258. newAuto = false;
  259. }
  260. if(_config->FindB("Debug::pkgAutoRemove",false))
  261. std::clog << "Update existing AutoInstall info: "
  262. << pkg.FullName() << std::endl;
  263. TFRewriteData rewrite[3];
  264. rewrite[0].Tag = "Architecture";
  265. rewrite[0].Rewrite = pkg.Arch();
  266. rewrite[0].NewTag = 0;
  267. rewrite[1].Tag = "Auto-Installed";
  268. rewrite[1].Rewrite = newAuto ? "1" : "0";
  269. rewrite[1].NewTag = 0;
  270. rewrite[2].Tag = 0;
  271. TFRewrite(OutFile, section, nullreorderlist, rewrite);
  272. fprintf(OutFile,"\n");
  273. pkgs_seen.insert(pkg.FullName());
  274. }
  275. // then write the ones we have not seen yet
  276. std::ostringstream ostr;
  277. for(pkgCache::PkgIterator pkg=Cache->PkgBegin(); !pkg.end(); ++pkg) {
  278. StateCache const &P = PkgState[pkg->ID];
  279. if(P.Flags & Flag::Auto) {
  280. if (pkgs_seen.find(pkg.FullName()) != pkgs_seen.end()) {
  281. if(debug_autoremove)
  282. std::clog << "Skipping already written " << pkg.FullName() << std::endl;
  283. continue;
  284. }
  285. // skip not installed ones if requested
  286. if (InstalledOnly && (
  287. (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
  288. (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
  289. continue;
  290. const char* const pkgarch = pkg.Arch();
  291. if (strcmp(pkgarch, "all") == 0)
  292. continue;
  293. if(debug_autoremove)
  294. std::clog << "Writing new AutoInstall: " << pkg.FullName() << std::endl;
  295. ostr.str(string(""));
  296. ostr << "Package: " << pkg.Name()
  297. << "\nArchitecture: " << pkgarch
  298. << "\nAuto-Installed: 1\n\n";
  299. fprintf(OutFile,"%s",ostr.str().c_str());
  300. }
  301. }
  302. fclose(OutFile);
  303. // move the outfile over the real file and set permissions
  304. rename(outfile.c_str(), state.c_str());
  305. chmod(state.c_str(), 0644);
  306. return true;
  307. }
  308. /*}}}*/
  309. // DepCache::CheckDep - Checks a single dependency /*{{{*/
  310. // ---------------------------------------------------------------------
  311. /* This first checks the dependency against the main target package and
  312. then walks along the package provides list and checks if each provides
  313. will be installed then checks the provides against the dep. Res will be
  314. set to the package which was used to satisfy the dep. */
  315. bool pkgDepCache::CheckDep(DepIterator Dep,int Type,PkgIterator &Res)
  316. {
  317. Res = Dep.TargetPkg();
  318. /* Check simple depends. A depends -should- never self match but
  319. we allow it anyhow because dpkg does. Technically it is a packaging
  320. bug. Conflicts may never self match */
  321. if (Dep.IsIgnorable(Res) == false)
  322. {
  323. PkgIterator Pkg = Dep.TargetPkg();
  324. // Check the base package
  325. if (Type == NowVersion && Pkg->CurrentVer != 0)
  326. if (Dep.IsSatisfied(Pkg.CurrentVer()) == true)
  327. return true;
  328. if (Type == InstallVersion && PkgState[Pkg->ID].InstallVer != 0)
  329. if (Dep.IsSatisfied(PkgState[Pkg->ID].InstVerIter(*this)) == true)
  330. return true;
  331. if (Type == CandidateVersion && PkgState[Pkg->ID].CandidateVer != 0)
  332. if (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. if (Type == InstallVersion)
  350. {
  351. StateCache &State = PkgState[P.OwnerPkg()->ID];
  352. if (State.InstallVer != (Version *)P.OwnerVer())
  353. continue;
  354. }
  355. 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. if (State.Mode == ModeKeep)
  475. iKeepCount += Add;
  476. 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 Check,
  514. unsigned char SetMin,
  515. unsigned char SetPolicy)
  516. {
  517. unsigned char Dep = 0xFF;
  518. while (D.end() != true)
  519. {
  520. // Compute a single dependency element (glob or)
  521. DepIterator Start = D;
  522. unsigned char State = 0;
  523. for (bool LastOR = true; D.end() == false && LastOR == true; ++D)
  524. {
  525. State |= DepState[D->ID];
  526. LastOR = (D->CompareOp & Dep::Or) == Dep::Or;
  527. }
  528. // Minimum deps that must be satisfied to have a working package
  529. if (Start.IsCritical() == true)
  530. if ((State & Check) != Check)
  531. Dep &= ~SetMin;
  532. // Policy deps that must be satisfied to install the package
  533. 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 &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 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 *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. 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. APT::VersionList verlist;
  1069. pkgCache::VerIterator Cand = PkgState[Start.TargetPkg()->ID].CandidateVerIter(*this);
  1070. if (Cand.end() == false && Start.IsSatisfied(Cand) == true)
  1071. verlist.insert(Cand);
  1072. for (PrvIterator Prv = Start.TargetPkg().ProvidesList(); Prv.end() != true; ++Prv)
  1073. {
  1074. pkgCache::VerIterator V = Prv.OwnerVer();
  1075. pkgCache::VerIterator Cand = PkgState[Prv.OwnerPkg()->ID].CandidateVerIter(*this);
  1076. if (Cand.end() == true || V != Cand || Start.IsSatisfied(Prv) == false)
  1077. continue;
  1078. verlist.insert(Cand);
  1079. }
  1080. CompareProviders comp(Start);
  1081. do {
  1082. APT::VersionList::iterator InstVer = std::max_element(verlist.begin(), verlist.end(), comp);
  1083. if (InstVer == verlist.end())
  1084. break;
  1085. pkgCache::PkgIterator InstPkg = InstVer.ParentPkg();
  1086. if(DebugAutoInstall == true)
  1087. std::clog << OutputInDepth(Depth) << "Installing " << InstPkg.Name()
  1088. << " as " << Start.DepType() << " of " << Pkg.Name()
  1089. << std::endl;
  1090. if (MarkInstall(InstPkg, true, Depth + 1, false, ForceImportantDeps) == false)
  1091. {
  1092. verlist.erase(InstVer);
  1093. continue;
  1094. }
  1095. // now check if we should consider it a automatic dependency or not
  1096. if(InstPkg->CurrentVer == 0 && InstVer->Section != 0 && ConfigValueInSubTree("APT::Never-MarkAuto-Sections", InstVer.Section()))
  1097. {
  1098. if(DebugAutoInstall == true)
  1099. std::clog << OutputInDepth(Depth) << "Setting NOT as auto-installed (direct "
  1100. << Start.DepType() << " of pkg in APT::Never-MarkAuto-Sections)" << std::endl;
  1101. MarkAuto(InstPkg, false);
  1102. }
  1103. break;
  1104. } while(true);
  1105. continue;
  1106. }
  1107. /* Negative dependencies have no or-group
  1108. If the dependency isn't versioned, we try if an upgrade might solve the problem.
  1109. Otherwise we remove the offender if needed */
  1110. else if (Start.IsNegative() == true && Start->Type != pkgCache::Dep::Obsoletes)
  1111. {
  1112. SPtrArray<Version *> List = Start.AllTargets();
  1113. pkgCache::PkgIterator TrgPkg = Start.TargetPkg();
  1114. for (Version **I = List; *I != 0; I++)
  1115. {
  1116. VerIterator Ver(*this,*I);
  1117. PkgIterator Pkg = Ver.ParentPkg();
  1118. /* The List includes all packages providing this dependency,
  1119. even providers which are not installed, so skip them. */
  1120. if (PkgState[Pkg->ID].InstallVer == 0)
  1121. continue;
  1122. /* Ignore negative dependencies that we are not going to
  1123. get installed */
  1124. if (PkgState[Pkg->ID].InstallVer != *I)
  1125. continue;
  1126. if ((Start->Version != 0 || TrgPkg != Pkg) &&
  1127. PkgState[Pkg->ID].CandidateVer != PkgState[Pkg->ID].InstallVer &&
  1128. PkgState[Pkg->ID].CandidateVer != *I &&
  1129. MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps) == true)
  1130. continue;
  1131. else if (Start->Type == pkgCache::Dep::Conflicts ||
  1132. Start->Type == pkgCache::Dep::DpkgBreaks)
  1133. {
  1134. if(DebugAutoInstall == true)
  1135. std::clog << OutputInDepth(Depth)
  1136. << " Removing: " << Pkg.Name()
  1137. << std::endl;
  1138. if (MarkDelete(Pkg,false,Depth + 1, false) == false)
  1139. break;
  1140. }
  1141. }
  1142. continue;
  1143. }
  1144. }
  1145. return Dep.end() == true;
  1146. }
  1147. /*}}}*/
  1148. // DepCache::IsInstallOk - check if it is ok to install this package /*{{{*/
  1149. // ---------------------------------------------------------------------
  1150. /* The default implementation checks if the installation of an M-A:same
  1151. package would lead us into a version-screw and if so forbids it.
  1152. dpkg holds are enforced by the private IsModeChangeOk */
  1153. bool pkgDepCache::IsInstallOk(PkgIterator const &Pkg,bool AutoInst,
  1154. unsigned long Depth, bool FromUser)
  1155. {
  1156. return IsInstallOkMultiArchSameVersionSynced(Pkg,AutoInst, Depth, FromUser) &&
  1157. IsInstallOkDependenciesSatisfiableByCandidates(Pkg,AutoInst, Depth, FromUser);
  1158. }
  1159. bool pkgDepCache::IsInstallOkMultiArchSameVersionSynced(PkgIterator const &Pkg,
  1160. bool const /*AutoInst*/, unsigned long const Depth, bool const FromUser)
  1161. {
  1162. if (FromUser == true) // as always: user is always right
  1163. return true;
  1164. // if we have checked before and it was okay, it will still be okay
  1165. if (PkgState[Pkg->ID].Mode == ModeInstall &&
  1166. PkgState[Pkg->ID].InstallVer == PkgState[Pkg->ID].CandidateVer)
  1167. return true;
  1168. // ignore packages with none-M-A:same candidates
  1169. VerIterator const CandVer = PkgState[Pkg->ID].CandidateVerIter(*this);
  1170. if (unlikely(CandVer.end() == true) || CandVer == Pkg.CurrentVer() ||
  1171. (CandVer->MultiArch & pkgCache::Version::Same) != pkgCache::Version::Same)
  1172. return true;
  1173. GrpIterator const Grp = Pkg.Group();
  1174. for (PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
  1175. {
  1176. // not installed or version synced: fine by definition
  1177. // (simple string-compare as stuff like '1' == '0:1-0' can't happen here)
  1178. if (P->CurrentVer == 0 || strcmp(Pkg.CandVersion(), P.CandVersion()) == 0)
  1179. continue;
  1180. // packages losing M-A:same can be out-of-sync
  1181. VerIterator CV = PkgState[P->ID].CandidateVerIter(*this);
  1182. if (unlikely(CV.end() == true) ||
  1183. (CV->MultiArch & pkgCache::Version::Same) != pkgCache::Version::Same)
  1184. continue;
  1185. // not downloadable means the package is obsolete, so allow out-of-sync
  1186. if (CV.Downloadable() == false)
  1187. continue;
  1188. PkgState[Pkg->ID].iFlags |= AutoKept;
  1189. if (unlikely(DebugMarker == true))
  1190. std::clog << OutputInDepth(Depth) << "Ignore MarkInstall of " << Pkg
  1191. << " as its M-A:same siblings are not version-synced" << std::endl;
  1192. return false;
  1193. }
  1194. return true;
  1195. }
  1196. bool pkgDepCache::IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator const &Pkg,
  1197. bool const AutoInst, unsigned long const Depth, bool const /*FromUser*/)
  1198. {
  1199. if (AutoInst == false)
  1200. return true;
  1201. VerIterator const CandVer = PkgState[Pkg->ID].CandidateVerIter(*this);
  1202. if (unlikely(CandVer.end() == true) || CandVer == Pkg.CurrentVer())
  1203. return true;
  1204. for (DepIterator Dep = CandVer.DependsList(); Dep.end() != true;)
  1205. {
  1206. // Grok or groups
  1207. DepIterator Start = Dep;
  1208. bool Result = true;
  1209. unsigned Ors = 0;
  1210. for (bool LastOR = true; Dep.end() == false && LastOR == true; ++Dep, ++Ors)
  1211. {
  1212. LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
  1213. if ((DepState[Dep->ID] & DepInstall) == DepInstall)
  1214. Result = false;
  1215. }
  1216. if (Start.IsCritical() == false || Start.IsNegative() == true || Result == false)
  1217. continue;
  1218. /* If we are in an or group locate the first or that can succeed.
  1219. We have already cached this… */
  1220. for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; --Ors)
  1221. ++Start;
  1222. if (Ors == 1 && (DepState[Start->ID] &DepCVer) != DepCVer)
  1223. {
  1224. if (DebugAutoInstall == true)
  1225. std::clog << OutputInDepth(Depth) << Start << " can't be satisfied!" << std::endl;
  1226. // the dependency is critical, but can't be installed, so discard the candidate
  1227. // as the problemresolver will trip over it otherwise trying to install it (#735967)
  1228. if (Pkg->CurrentVer != 0 && (PkgState[Pkg->ID].iFlags & Protected) != Protected)
  1229. SetCandidateVersion(Pkg.CurrentVer());
  1230. return false;
  1231. }
  1232. }
  1233. return true;
  1234. }
  1235. /*}}}*/
  1236. // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
  1237. // ---------------------------------------------------------------------
  1238. /* */
  1239. void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
  1240. {
  1241. if (unlikely(Pkg.end() == true))
  1242. return;
  1243. APT::PackageList pkglist;
  1244. if (Pkg->CurrentVer != 0 &&
  1245. (Pkg.CurrentVer()-> MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  1246. {
  1247. pkgCache::GrpIterator Grp = Pkg.Group();
  1248. for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
  1249. {
  1250. if (P->CurrentVer != 0)
  1251. pkglist.insert(P);
  1252. }
  1253. }
  1254. else
  1255. pkglist.insert(Pkg);
  1256. ActionGroup group(*this);
  1257. for (APT::PackageList::const_iterator Pkg = pkglist.begin(); Pkg != pkglist.end(); ++Pkg)
  1258. {
  1259. RemoveSizes(Pkg);
  1260. RemoveStates(Pkg);
  1261. StateCache &P = PkgState[Pkg->ID];
  1262. if (To == true)
  1263. P.iFlags |= ReInstall;
  1264. else
  1265. P.iFlags &= ~ReInstall;
  1266. AddStates(Pkg);
  1267. AddSizes(Pkg);
  1268. }
  1269. }
  1270. /*}}}*/
  1271. // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
  1272. // ---------------------------------------------------------------------
  1273. /* */
  1274. void pkgDepCache::SetCandidateVersion(VerIterator TargetVer)
  1275. {
  1276. pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
  1277. StateCache &P = PkgState[Pkg->ID];
  1278. if (P.CandidateVer == TargetVer)
  1279. return;
  1280. ActionGroup group(*this);
  1281. RemoveSizes(Pkg);
  1282. RemoveStates(Pkg);
  1283. if (P.CandidateVer == P.InstallVer && P.Install() == true)
  1284. P.InstallVer = (Version *)TargetVer;
  1285. P.CandidateVer = (Version *)TargetVer;
  1286. P.Update(Pkg,*this);
  1287. AddStates(Pkg);
  1288. Update(Pkg);
  1289. AddSizes(Pkg);
  1290. }
  1291. /*}}}*/
  1292. // DepCache::SetCandidateRelease - Change the candidate version /*{{{*/
  1293. // ---------------------------------------------------------------------
  1294. /* changes the candidate of a package and walks over all its dependencies
  1295. to check if it needs to change the candidate of the dependency, too,
  1296. to reach a installable versionstate */
  1297. bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer,
  1298. std::string const &TargetRel)
  1299. {
  1300. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > Changed;
  1301. return SetCandidateRelease(TargetVer, TargetRel, Changed);
  1302. }
  1303. bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer,
  1304. std::string const &TargetRel,
  1305. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed)
  1306. {
  1307. ActionGroup group(*this);
  1308. SetCandidateVersion(TargetVer);
  1309. if (TargetRel == "installed" || TargetRel == "candidate") // both doesn't make sense in this context
  1310. return true;
  1311. pkgVersionMatch Match(TargetRel, pkgVersionMatch::Release);
  1312. // save the position of the last element we will not undo - if we have to
  1313. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::iterator newChanged = --(Changed.end());
  1314. for (pkgCache::DepIterator D = TargetVer.DependsList(); D.end() == false; ++D)
  1315. {
  1316. if (D->Type != pkgCache::Dep::PreDepends && D->Type != pkgCache::Dep::Depends &&
  1317. ((D->Type != pkgCache::Dep::Recommends && D->Type != pkgCache::Dep::Suggests) ||
  1318. IsImportantDep(D) == false))
  1319. continue;
  1320. // walk over an or-group and check if we need to do anything
  1321. // for simpilicity no or-group is handled as a or-group including one dependency
  1322. pkgCache::DepIterator Start = D;
  1323. bool itsFine = false;
  1324. for (bool stillOr = true; stillOr == true; ++Start)
  1325. {
  1326. stillOr = (Start->CompareOp & Dep::Or) == Dep::Or;
  1327. pkgCache::PkgIterator const P = Start.TargetPkg();
  1328. // virtual packages can't be a solution
  1329. if (P.end() == true || (P->ProvidesList == 0 && P->VersionList == 0))
  1330. continue;
  1331. pkgCache::VerIterator const Cand = PkgState[P->ID].CandidateVerIter(*this);
  1332. // no versioned dependency - but is it installable?
  1333. if (Start.TargetVer() == 0 || Start.TargetVer()[0] == '\0')
  1334. {
  1335. // Check if one of the providers is installable
  1336. if (P->ProvidesList != 0)
  1337. {
  1338. pkgCache::PrvIterator Prv = P.ProvidesList();
  1339. for (; Prv.end() == false; ++Prv)
  1340. {
  1341. pkgCache::VerIterator const C = PkgState[Prv.OwnerPkg()->ID].CandidateVerIter(*this);
  1342. if (C.end() == true || C != Prv.OwnerVer() ||
  1343. (VersionState(C.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) != DepCandMin)
  1344. continue;
  1345. break;
  1346. }
  1347. if (Prv.end() == true)
  1348. continue;
  1349. }
  1350. // no providers, so check if we have an installable candidate version
  1351. else if (Cand.end() == true ||
  1352. (VersionState(Cand.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) != DepCandMin)
  1353. continue;
  1354. itsFine = true;
  1355. break;
  1356. }
  1357. if (Cand.end() == true)
  1358. continue;
  1359. // check if the current candidate is enough for the versioned dependency - and installable?
  1360. if (Start.IsSatisfied(Cand) == true &&
  1361. (VersionState(Cand.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) == DepCandMin)
  1362. {
  1363. itsFine = true;
  1364. break;
  1365. }
  1366. }
  1367. if (itsFine == true) {
  1368. // something in the or-group was fine, skip all other members
  1369. for (; (D->CompareOp & Dep::Or) == Dep::Or; ++D);
  1370. continue;
  1371. }
  1372. // walk again over the or-group and check each if a candidate switch would help
  1373. itsFine = false;
  1374. for (bool stillOr = true; stillOr == true; ++D)
  1375. {
  1376. stillOr = (D->CompareOp & Dep::Or) == Dep::Or;
  1377. // changing candidate will not help if the dependency is not versioned
  1378. if (D.TargetVer() == 0 || D.TargetVer()[0] == '\0')
  1379. {
  1380. if (stillOr == true)
  1381. continue;
  1382. break;
  1383. }
  1384. pkgCache::VerIterator V;
  1385. if (TargetRel == "newest")
  1386. V = D.TargetPkg().VersionList();
  1387. else
  1388. V = Match.Find(D.TargetPkg());
  1389. // check if the version from this release could satisfy the dependency
  1390. if (V.end() == true || D.IsSatisfied(V) == false)
  1391. {
  1392. if (stillOr == true)
  1393. continue;
  1394. break;
  1395. }
  1396. pkgCache::VerIterator oldCand = PkgState[D.TargetPkg()->ID].CandidateVerIter(*this);
  1397. if (V == oldCand)
  1398. {
  1399. // Do we already touched this Version? If so, their versioned dependencies are okay, no need to check again
  1400. for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = Changed.begin();
  1401. c != Changed.end(); ++c)
  1402. {
  1403. if (c->first->ParentPkg != V->ParentPkg)
  1404. continue;
  1405. itsFine = true;
  1406. break;
  1407. }
  1408. }
  1409. if (itsFine == false)
  1410. {
  1411. // change the candidate
  1412. Changed.push_back(make_pair(V, TargetVer));
  1413. if (SetCandidateRelease(V, TargetRel, Changed) == false)
  1414. {
  1415. if (stillOr == false)
  1416. break;
  1417. // undo the candidate changing
  1418. SetCandidateVersion(oldCand);
  1419. Changed.pop_back();
  1420. continue;
  1421. }
  1422. itsFine = true;
  1423. }
  1424. // something in the or-group was fine, skip all other members
  1425. for (; (D->CompareOp & Dep::Or) == Dep::Or; ++D);
  1426. break;
  1427. }
  1428. if (itsFine == false && (D->Type == pkgCache::Dep::PreDepends || D->Type == pkgCache::Dep::Depends))
  1429. {
  1430. // undo all changes which aren't lead to a solution
  1431. for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = ++newChanged;
  1432. c != Changed.end(); ++c)
  1433. SetCandidateVersion(c->first);
  1434. Changed.erase(newChanged, Changed.end());
  1435. return false;
  1436. }
  1437. }
  1438. return true;
  1439. }
  1440. /*}}}*/
  1441. // DepCache::MarkAuto - set the Auto flag for a package /*{{{*/
  1442. // ---------------------------------------------------------------------
  1443. /* */
  1444. void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
  1445. {
  1446. StateCache &state = PkgState[Pkg->ID];
  1447. ActionGroup group(*this);
  1448. if(Auto)
  1449. state.Flags |= Flag::Auto;
  1450. else
  1451. state.Flags &= ~Flag::Auto;
  1452. }
  1453. /*}}}*/
  1454. // StateCache::Update - Compute the various static display things /*{{{*/
  1455. // ---------------------------------------------------------------------
  1456. /* This is called whenever the Candidate version changes. */
  1457. void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
  1458. {
  1459. // Some info
  1460. VerIterator Ver = CandidateVerIter(Cache);
  1461. // Use a null string or the version string
  1462. if (Ver.end() == true)
  1463. CandVersion = "";
  1464. else
  1465. CandVersion = Ver.VerStr();
  1466. // Find the current version
  1467. CurVersion = "";
  1468. if (Pkg->CurrentVer != 0)
  1469. CurVersion = Pkg.CurrentVer().VerStr();
  1470. // Strip off the epochs for display
  1471. CurVersion = StripEpoch(CurVersion);
  1472. CandVersion = StripEpoch(CandVersion);
  1473. // Figure out if its up or down or equal
  1474. Status = Ver.CompareVer(Pkg.CurrentVer());
  1475. if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
  1476. Status = 2;
  1477. }
  1478. /*}}}*/
  1479. // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
  1480. // ---------------------------------------------------------------------
  1481. /* */
  1482. const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
  1483. {
  1484. if (Ver == 0)
  1485. return 0;
  1486. // Strip any epoch
  1487. for (const char *I = Ver; *I != 0; I++)
  1488. if (*I == ':')
  1489. return I + 1;
  1490. return Ver;
  1491. }
  1492. /*}}}*/
  1493. // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
  1494. // ---------------------------------------------------------------------
  1495. /* The default just returns the highest available version that is not
  1496. a source and automatic. */
  1497. pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator const &Pkg)
  1498. {
  1499. /* Not source/not automatic versions cannot be a candidate version
  1500. unless they are already installed */
  1501. VerIterator Last;
  1502. for (VerIterator I = Pkg.VersionList(); I.end() == false; ++I)
  1503. {
  1504. if (Pkg.CurrentVer() == I)
  1505. return I;
  1506. for (VerFileIterator J = I.FileList(); J.end() == false; ++J)
  1507. {
  1508. if ((J.File()->Flags & Flag::NotSource) != 0)
  1509. continue;
  1510. /* Stash the highest version of a not-automatic source, we use it
  1511. if there is nothing better */
  1512. if ((J.File()->Flags & Flag::NotAutomatic) != 0 ||
  1513. (J.File()->Flags & Flag::ButAutomaticUpgrades) != 0)
  1514. {
  1515. if (Last.end() == true)
  1516. Last = I;
  1517. continue;
  1518. }
  1519. return I;
  1520. }
  1521. }
  1522. return Last;
  1523. }
  1524. /*}}}*/
  1525. // Policy::IsImportantDep - True if the dependency is important /*{{{*/
  1526. // ---------------------------------------------------------------------
  1527. /* */
  1528. bool pkgDepCache::Policy::IsImportantDep(DepIterator const &Dep)
  1529. {
  1530. if(Dep.IsCritical())
  1531. return true;
  1532. else if(Dep->Type == pkgCache::Dep::Recommends)
  1533. {
  1534. if (InstallRecommends)
  1535. return true;
  1536. // we suport a special mode to only install-recommends for certain
  1537. // sections
  1538. // FIXME: this is a meant as a temporarly solution until the
  1539. // recommends are cleaned up
  1540. const char *sec = Dep.ParentVer().Section();
  1541. if (sec && ConfigValueInSubTree("APT::Install-Recommends-Sections", sec))
  1542. return true;
  1543. }
  1544. else if(Dep->Type == pkgCache::Dep::Suggests)
  1545. return InstallSuggests;
  1546. return false;
  1547. }
  1548. /*}}}*/
  1549. // Policy::GetPriority - Get the priority of the package pin /*{{{*/
  1550. APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgIterator const &/*Pkg*/)
  1551. { return 0; }
  1552. APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgFileIterator const &/*File*/)
  1553. { return 0; }
  1554. /*}}}*/
  1555. pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc() /*{{{*/
  1556. {
  1557. DefaultRootSetFunc *f = new DefaultRootSetFunc;
  1558. if(f->wasConstructedSuccessfully())
  1559. return f;
  1560. else
  1561. {
  1562. delete f;
  1563. return NULL;
  1564. }
  1565. }
  1566. /*}}}*/
  1567. bool pkgDepCache::MarkFollowsRecommends()
  1568. {
  1569. return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
  1570. }
  1571. bool pkgDepCache::MarkFollowsSuggests()
  1572. {
  1573. return _config->FindB("APT::AutoRemove::SuggestsImportant", true);
  1574. }
  1575. // pkgDepCache::MarkRequired - the main mark algorithm /*{{{*/
  1576. bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
  1577. {
  1578. if (_config->Find("APT::Solver", "internal") != "internal")
  1579. return true;
  1580. bool follow_recommends;
  1581. bool follow_suggests;
  1582. bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  1583. // init the states
  1584. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1585. {
  1586. PkgState[p->ID].Marked = false;
  1587. PkgState[p->ID].Garbage = false;
  1588. // debug output
  1589. if(debug_autoremove && PkgState[p->ID].Flags & Flag::Auto)
  1590. std::clog << "AutoDep: " << p.FullName() << std::endl;
  1591. }
  1592. // init vars
  1593. follow_recommends = MarkFollowsRecommends();
  1594. follow_suggests = MarkFollowsSuggests();
  1595. // do the mark part, this is the core bit of the algorithm
  1596. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1597. {
  1598. if(!(PkgState[p->ID].Flags & Flag::Auto) ||
  1599. (p->Flags & Flag::Essential) ||
  1600. (p->Flags & Flag::Important) ||
  1601. userFunc.InRootSet(p) ||
  1602. // be nice even then a required package violates the policy (#583517)
  1603. // and do the full mark process also for required packages
  1604. (p.CurrentVer().end() != true &&
  1605. p.CurrentVer()->Priority == pkgCache::State::Required) ||
  1606. // packages which can't be changed (like holds) can't be garbage
  1607. (IsModeChangeOk(ModeGarbage, p, 0, false) == false))
  1608. {
  1609. // the package is installed (and set to keep)
  1610. if(PkgState[p->ID].Keep() && !p.CurrentVer().end())
  1611. MarkPackage(p, p.CurrentVer(),
  1612. follow_recommends, follow_suggests);
  1613. // the package is to be installed
  1614. else if(PkgState[p->ID].Install())
  1615. MarkPackage(p, PkgState[p->ID].InstVerIter(*this),
  1616. follow_recommends, follow_suggests);
  1617. }
  1618. }
  1619. return true;
  1620. }
  1621. /*}}}*/
  1622. // MarkPackage - mark a single package in Mark-and-Sweep /*{{{*/
  1623. void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
  1624. const pkgCache::VerIterator &ver,
  1625. bool const &follow_recommends,
  1626. bool const &follow_suggests)
  1627. {
  1628. pkgDepCache::StateCache &state = PkgState[pkg->ID];
  1629. // if we are marked already we are done
  1630. if(state.Marked)
  1631. return;
  1632. VerIterator const currver = pkg.CurrentVer();
  1633. VerIterator const instver = state.InstVerIter(*this);
  1634. #if 0
  1635. VerIterator const candver = state.CandidateVerIter(*this);
  1636. // If a package was garbage-collected but is now being marked, we
  1637. // should re-select it
  1638. // For cases when a pkg is set to upgrade and this trigger the
  1639. // removal of a no-longer used dependency. if the pkg is set to
  1640. // keep again later it will result in broken deps
  1641. if(state.Delete() && state.RemoveReason = Unused)
  1642. {
  1643. if(ver==candver)
  1644. mark_install(pkg, false, false, NULL);
  1645. else if(ver==pkg.CurrentVer())
  1646. MarkKeep(pkg, false, false);
  1647. instver=state.InstVerIter(*this);
  1648. }
  1649. #endif
  1650. // For packages that are not going to be removed, ignore versions
  1651. // other than the InstVer. For packages that are going to be
  1652. // removed, ignore versions other than the current version.
  1653. if(!(ver == instver && !instver.end()) &&
  1654. !(ver == currver && instver.end() && !ver.end()))
  1655. return;
  1656. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove", false);
  1657. if(debug_autoremove)
  1658. {
  1659. std::clog << "Marking: " << pkg.FullName();
  1660. if(!ver.end())
  1661. std::clog << " " << ver.VerStr();
  1662. if(!currver.end())
  1663. std::clog << ", Curr=" << currver.VerStr();
  1664. if(!instver.end())
  1665. std::clog << ", Inst=" << instver.VerStr();
  1666. std::clog << std::endl;
  1667. }
  1668. state.Marked=true;
  1669. if(ver.end() == true)
  1670. return;
  1671. for(DepIterator d = ver.DependsList(); !d.end(); ++d)
  1672. {
  1673. if(d->Type == Dep::Depends ||
  1674. d->Type == Dep::PreDepends ||
  1675. (follow_recommends &&
  1676. d->Type == Dep::Recommends) ||
  1677. (follow_suggests &&
  1678. d->Type == Dep::Suggests))
  1679. {
  1680. // Try all versions of this package.
  1681. for(VerIterator V = d.TargetPkg().VersionList();
  1682. !V.end(); ++V)
  1683. {
  1684. if(d.IsSatisfied(V))
  1685. {
  1686. if(debug_autoremove)
  1687. {
  1688. std::clog << "Following dep: " << d.ParentPkg().FullName()
  1689. << " " << d.ParentVer().VerStr() << " "
  1690. << d.DepType() << " " << d.TargetPkg().FullName();
  1691. if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
  1692. {
  1693. std::clog << " (" << d.CompType() << " "
  1694. << d.TargetVer() << ")";
  1695. }
  1696. std::clog << std::endl;
  1697. }
  1698. MarkPackage(V.ParentPkg(), V,
  1699. follow_recommends, follow_suggests);
  1700. }
  1701. }
  1702. // Now try virtual packages
  1703. for(PrvIterator prv=d.TargetPkg().ProvidesList();
  1704. !prv.end(); ++prv)
  1705. {
  1706. if(d.IsSatisfied(prv))
  1707. {
  1708. if(debug_autoremove)
  1709. {
  1710. std::clog << "Following dep: " << d.ParentPkg().FullName() << " "
  1711. << d.ParentVer().VerStr() << " "
  1712. << d.DepType() << " " << d.TargetPkg().FullName() << " ";
  1713. if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
  1714. {
  1715. std::clog << " (" << d.CompType() << " "
  1716. << d.TargetVer() << ")";
  1717. }
  1718. std::clog << ", provided by "
  1719. << prv.OwnerPkg().FullName() << " "
  1720. << prv.OwnerVer().VerStr()
  1721. << std::endl;
  1722. }
  1723. MarkPackage(prv.OwnerPkg(), prv.OwnerVer(),
  1724. follow_recommends, follow_suggests);
  1725. }
  1726. }
  1727. }
  1728. }
  1729. }
  1730. /*}}}*/
  1731. bool pkgDepCache::Sweep() /*{{{*/
  1732. {
  1733. bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  1734. // do the sweep
  1735. for(PkgIterator p=PkgBegin(); !p.end(); ++p)
  1736. {
  1737. StateCache &state=PkgState[p->ID];
  1738. // skip required packages
  1739. if (!p.CurrentVer().end() &&
  1740. (p.CurrentVer()->Priority == pkgCache::State::Required))
  1741. continue;
  1742. // if it is not marked and it is installed, it's garbage
  1743. if(!state.Marked && (!p.CurrentVer().end() || state.Install()))
  1744. {
  1745. state.Garbage=true;
  1746. if(debug_autoremove)
  1747. std::clog << "Garbage: " << p.FullName() << std::endl;
  1748. }
  1749. }
  1750. return true;
  1751. }
  1752. /*}}}*/
  1753. // DepCache::MarkAndSweep /*{{{*/
  1754. bool pkgDepCache::MarkAndSweep(InRootSetFunc &rootFunc)
  1755. {
  1756. return MarkRequired(rootFunc) && Sweep();
  1757. }
  1758. bool pkgDepCache::MarkAndSweep()
  1759. {
  1760. std::auto_ptr<InRootSetFunc> f(GetRootSetFunc());
  1761. if(f.get() != NULL)
  1762. return MarkAndSweep(*f.get());
  1763. else
  1764. return false;
  1765. }
  1766. /*}}}*/