depcache.cc 54 KB

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