depcache.cc 62 KB

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