depcache.cc 66 KB

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