depcache.cc 54 KB

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