depcache.cc 61 KB

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