depcache.cc 61 KB

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