depcache.cc 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  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. #ifdef __GNUG__
  10. #pragma implementation "apt-pkg/depcache.h"
  11. #endif
  12. #include <apt-pkg/depcache.h>
  13. #include <apt-pkg/version.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/sptr.h>
  16. #include <apt-pkg/algorithms.h>
  17. #include <apt-pkg/fileutl.h>
  18. #include <apt-pkg/configuration.h>
  19. #include <apt-pkg/pkgsystem.h>
  20. #include <apt-pkg/tagfile.h>
  21. #include <iostream>
  22. #include <sstream>
  23. #include <set>
  24. #include <sys/stat.h>
  25. #include <apti18n.h>
  26. pkgDepCache::ActionGroup::ActionGroup(pkgDepCache &cache) :
  27. cache(cache), released(false)
  28. {
  29. ++cache.group_level;
  30. }
  31. void pkgDepCache::ActionGroup::release()
  32. {
  33. if(!released)
  34. {
  35. if(cache.group_level == 0)
  36. std::cerr << "W: Unbalanced action groups, expect badness" << std::endl;
  37. else
  38. {
  39. --cache.group_level;
  40. if(cache.group_level == 0)
  41. cache.MarkAndSweep();
  42. }
  43. released = false;
  44. }
  45. }
  46. pkgDepCache::ActionGroup::~ActionGroup()
  47. {
  48. release();
  49. }
  50. // DepCache::pkgDepCache - Constructors /*{{{*/
  51. // ---------------------------------------------------------------------
  52. /* */
  53. pkgDepCache::pkgDepCache(pkgCache *pCache,Policy *Plcy) :
  54. group_level(0), Cache(pCache), PkgState(0), DepState(0)
  55. {
  56. delLocalPolicy = 0;
  57. LocalPolicy = Plcy;
  58. if (LocalPolicy == 0)
  59. delLocalPolicy = LocalPolicy = new Policy;
  60. }
  61. /*}}}*/
  62. // DepCache::~pkgDepCache - Destructor /*{{{*/
  63. // ---------------------------------------------------------------------
  64. /* */
  65. pkgDepCache::~pkgDepCache()
  66. {
  67. delete [] PkgState;
  68. delete [] DepState;
  69. delete delLocalPolicy;
  70. }
  71. /*}}}*/
  72. // DepCache::Init - Generate the initial extra structures. /*{{{*/
  73. // ---------------------------------------------------------------------
  74. /* This allocats the extension buffers and initializes them. */
  75. bool pkgDepCache::Init(OpProgress *Prog)
  76. {
  77. // Suppress mark updates during this operation (just in case) and
  78. // run a mark operation when Init terminates.
  79. ActionGroup actions(*this);
  80. delete [] PkgState;
  81. delete [] DepState;
  82. PkgState = new StateCache[Head().PackageCount];
  83. DepState = new unsigned char[Head().DependsCount];
  84. memset(PkgState,0,sizeof(*PkgState)*Head().PackageCount);
  85. memset(DepState,0,sizeof(*DepState)*Head().DependsCount);
  86. if (Prog != 0)
  87. {
  88. Prog->OverallProgress(0,2*Head().PackageCount,Head().PackageCount,
  89. _("Building dependency tree"));
  90. Prog->SubProgress(Head().PackageCount,_("Candidate versions"));
  91. }
  92. /* Set the current state of everything. In this state all of the
  93. packages are kept exactly as is. See AllUpgrade */
  94. int Done = 0;
  95. for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
  96. {
  97. if (Prog != 0)
  98. Prog->Progress(Done);
  99. // Find the proper cache slot
  100. StateCache &State = PkgState[I->ID];
  101. State.iFlags = 0;
  102. // Figure out the install version
  103. State.CandidateVer = GetCandidateVer(I);
  104. State.InstallVer = I.CurrentVer();
  105. State.Mode = ModeKeep;
  106. State.Update(I,*this);
  107. }
  108. if (Prog != 0)
  109. {
  110. Prog->OverallProgress(Head().PackageCount,2*Head().PackageCount,
  111. Head().PackageCount,
  112. _("Building dependency tree"));
  113. Prog->SubProgress(Head().PackageCount,_("Dependency generation"));
  114. }
  115. Update(Prog);
  116. if(Prog != 0)
  117. Prog->Done();
  118. return true;
  119. }
  120. /*}}}*/
  121. bool pkgDepCache::readStateFile(OpProgress *Prog)
  122. {
  123. FileFd state_file;
  124. string state = _config->FindDir("Dir::State") + "extended_states";
  125. if(FileExists(state)) {
  126. state_file.Open(state, FileFd::ReadOnly);
  127. int file_size = state_file.Size();
  128. if(Prog != NULL)
  129. Prog->OverallProgress(0, file_size, 1,
  130. _("Reading state information"));
  131. pkgTagFile tagfile(&state_file);
  132. pkgTagSection section;
  133. int amt=0;
  134. while(tagfile.Step(section)) {
  135. string pkgname = section.FindS("Package");
  136. pkgCache::PkgIterator pkg=Cache->FindPkg(pkgname);
  137. // Silently ignore unknown packages and packages with no actual
  138. // version.
  139. if(!pkg.end() && !pkg.VersionList().end()) {
  140. short reason = section.FindI("Auto-Installed", 0);
  141. if(reason > 0)
  142. PkgState[pkg->ID].Flags |= Flag::Auto;
  143. if(_config->FindB("Debug::pkgAutoRemove",false))
  144. std::cout << "Auto-Installed : " << pkgname << std::endl;
  145. amt+=section.size();
  146. if(Prog != NULL)
  147. Prog->OverallProgress(amt, file_size, 1,
  148. _("Reading state information"));
  149. }
  150. if(Prog != NULL)
  151. Prog->OverallProgress(file_size, file_size, 1,
  152. _("Reading state information"));
  153. }
  154. }
  155. return true;
  156. }
  157. bool pkgDepCache::writeStateFile(OpProgress *prog, bool InstalledOnly)
  158. {
  159. if(_config->FindB("Debug::pkgAutoRemove",false))
  160. std::clog << "pkgDepCache::writeStateFile()" << std::endl;
  161. FileFd StateFile;
  162. string state = _config->FindDir("Dir::State") + "extended_states";
  163. // if it does not exist, create a empty one
  164. if(!FileExists(state))
  165. {
  166. StateFile.Open(state, FileFd::WriteEmpty);
  167. StateFile.Close();
  168. }
  169. // open it
  170. if(!StateFile.Open(state, FileFd::ReadOnly))
  171. return _error->Error(_("Failed to open StateFile %s"),
  172. state.c_str());
  173. FILE *OutFile;
  174. string outfile = state + ".tmp";
  175. if((OutFile = fopen(outfile.c_str(),"w")) == NULL)
  176. return _error->Error(_("Failed to write temporary StateFile %s"),
  177. outfile.c_str());
  178. // first merge with the existing sections
  179. pkgTagFile tagfile(&StateFile);
  180. pkgTagSection section;
  181. std::set<string> pkgs_seen;
  182. const char *nullreorderlist[] = {0};
  183. while(tagfile.Step(section)) {
  184. string pkgname = section.FindS("Package");
  185. // Silently ignore unknown packages and packages with no actual
  186. // version.
  187. pkgCache::PkgIterator pkg=Cache->FindPkg(pkgname);
  188. if(pkg.end() || pkg.VersionList().end())
  189. continue;
  190. bool oldAuto = section.FindI("Auto-Installed");
  191. bool newAuto = (PkgState[pkg->ID].Flags & Flag::Auto);
  192. if(_config->FindB("Debug::pkgAutoRemove",false))
  193. std::clog << "Update exisiting AutoInstall info: "
  194. << pkg.Name() << std::endl;
  195. TFRewriteData rewrite[2];
  196. rewrite[0].Tag = "Auto-Installed";
  197. rewrite[0].Rewrite = newAuto ? "1" : "0";
  198. rewrite[0].NewTag = 0;
  199. rewrite[1].Tag = 0;
  200. TFRewrite(OutFile, section, nullreorderlist, rewrite);
  201. fprintf(OutFile,"\n");
  202. pkgs_seen.insert(pkgname);
  203. }
  204. // then write the ones we have not seen yet
  205. std::ostringstream ostr;
  206. for(pkgCache::PkgIterator pkg=Cache->PkgBegin(); !pkg.end(); pkg++) {
  207. if(PkgState[pkg->ID].Flags & Flag::Auto) {
  208. if (pkgs_seen.find(pkg.Name()) != pkgs_seen.end()) {
  209. if(_config->FindB("Debug::pkgAutoRemove",false))
  210. std::clog << "Skipping already written " << pkg.Name() << std::endl;
  211. continue;
  212. }
  213. // skip not installed ones if requested
  214. if(InstalledOnly && pkg->CurrentVer == 0)
  215. continue;
  216. if(_config->FindB("Debug::pkgAutoRemove",false))
  217. std::clog << "Writing new AutoInstall: "
  218. << pkg.Name() << std::endl;
  219. ostr.str(string(""));
  220. ostr << "Package: " << pkg.Name()
  221. << "\nAuto-Installed: 1\n\n";
  222. fprintf(OutFile,ostr.str().c_str());
  223. fprintf(OutFile,"\n");
  224. }
  225. }
  226. fclose(OutFile);
  227. // move the outfile over the real file and set permissions
  228. rename(outfile.c_str(), state.c_str());
  229. chmod(state.c_str(), 0644);
  230. return true;
  231. }
  232. // DepCache::CheckDep - Checks a single dependency /*{{{*/
  233. // ---------------------------------------------------------------------
  234. /* This first checks the dependency against the main target package and
  235. then walks along the package provides list and checks if each provides
  236. will be installed then checks the provides against the dep. Res will be
  237. set to the package which was used to satisfy the dep. */
  238. bool pkgDepCache::CheckDep(DepIterator Dep,int Type,PkgIterator &Res)
  239. {
  240. Res = Dep.TargetPkg();
  241. /* Check simple depends. A depends -should- never self match but
  242. we allow it anyhow because dpkg does. Technically it is a packaging
  243. bug. Conflicts may never self match */
  244. if (Dep.TargetPkg() != Dep.ParentPkg() ||
  245. (Dep->Type != Dep::Conflicts && Dep->Type != Dep::DpkgBreaks && Dep->Type != Dep::Obsoletes))
  246. {
  247. PkgIterator Pkg = Dep.TargetPkg();
  248. // Check the base package
  249. if (Type == NowVersion && Pkg->CurrentVer != 0)
  250. if (VS().CheckDep(Pkg.CurrentVer().VerStr(),Dep->CompareOp,
  251. Dep.TargetVer()) == true)
  252. return true;
  253. if (Type == InstallVersion && PkgState[Pkg->ID].InstallVer != 0)
  254. if (VS().CheckDep(PkgState[Pkg->ID].InstVerIter(*this).VerStr(),
  255. Dep->CompareOp,Dep.TargetVer()) == true)
  256. return true;
  257. if (Type == CandidateVersion && PkgState[Pkg->ID].CandidateVer != 0)
  258. if (VS().CheckDep(PkgState[Pkg->ID].CandidateVerIter(*this).VerStr(),
  259. Dep->CompareOp,Dep.TargetVer()) == true)
  260. return true;
  261. }
  262. if (Dep->Type == Dep::Obsoletes)
  263. return false;
  264. // Check the providing packages
  265. PrvIterator P = Dep.TargetPkg().ProvidesList();
  266. PkgIterator Pkg = Dep.ParentPkg();
  267. for (; P.end() != true; P++)
  268. {
  269. /* Provides may never be applied against the same package if it is
  270. a conflicts. See the comment above. */
  271. if (P.OwnerPkg() == Pkg &&
  272. (Dep->Type == Dep::Conflicts || Dep->Type == Dep::DpkgBreaks))
  273. continue;
  274. // Check if the provides is a hit
  275. if (Type == NowVersion)
  276. {
  277. if (P.OwnerPkg().CurrentVer() != P.OwnerVer())
  278. continue;
  279. }
  280. if (Type == InstallVersion)
  281. {
  282. StateCache &State = PkgState[P.OwnerPkg()->ID];
  283. if (State.InstallVer != (Version *)P.OwnerVer())
  284. continue;
  285. }
  286. if (Type == CandidateVersion)
  287. {
  288. StateCache &State = PkgState[P.OwnerPkg()->ID];
  289. if (State.CandidateVer != (Version *)P.OwnerVer())
  290. continue;
  291. }
  292. // Compare the versions.
  293. if (VS().CheckDep(P.ProvideVersion(),Dep->CompareOp,Dep.TargetVer()) == true)
  294. {
  295. Res = P.OwnerPkg();
  296. return true;
  297. }
  298. }
  299. return false;
  300. }
  301. /*}}}*/
  302. // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
  303. // ---------------------------------------------------------------------
  304. /* Call with Mult = -1 to preform the inverse opration */
  305. void pkgDepCache::AddSizes(const PkgIterator &Pkg,signed long Mult)
  306. {
  307. StateCache &P = PkgState[Pkg->ID];
  308. if (Pkg->VersionList == 0)
  309. return;
  310. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  311. P.Keep() == true)
  312. return;
  313. // Compute the size data
  314. if (P.NewInstall() == true)
  315. {
  316. iUsrSize += (signed)(Mult*P.InstVerIter(*this)->InstalledSize);
  317. iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
  318. return;
  319. }
  320. // Upgrading
  321. if (Pkg->CurrentVer != 0 &&
  322. (P.InstallVer != (Version *)Pkg.CurrentVer() ||
  323. (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
  324. {
  325. iUsrSize += (signed)(Mult*((signed)P.InstVerIter(*this)->InstalledSize -
  326. (signed)Pkg.CurrentVer()->InstalledSize));
  327. iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
  328. return;
  329. }
  330. // Reinstall
  331. if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
  332. P.Delete() == false)
  333. {
  334. iDownloadSize += (signed)(Mult*P.InstVerIter(*this)->Size);
  335. return;
  336. }
  337. // Removing
  338. if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
  339. {
  340. iUsrSize -= (signed)(Mult*Pkg.CurrentVer()->InstalledSize);
  341. return;
  342. }
  343. }
  344. /*}}}*/
  345. // DepCache::AddStates - Add the package to the state counter /*{{{*/
  346. // ---------------------------------------------------------------------
  347. /* This routine is tricky to use, you must make sure that it is never
  348. called twice for the same package. This means the Remove/Add section
  349. should be as short as possible and not encompass any code that will
  350. calld Remove/Add itself. Remember, dependencies can be circular so
  351. while processing a dep for Pkg it is possible that Add/Remove
  352. will be called on Pkg */
  353. void pkgDepCache::AddStates(const PkgIterator &Pkg,int Add)
  354. {
  355. StateCache &State = PkgState[Pkg->ID];
  356. // The Package is broken (either minimal dep or policy dep)
  357. if ((State.DepState & DepInstMin) != DepInstMin)
  358. iBrokenCount += Add;
  359. if ((State.DepState & DepInstPolicy) != DepInstPolicy)
  360. iPolicyBrokenCount += Add;
  361. // Bad state
  362. if (Pkg.State() != PkgIterator::NeedsNothing)
  363. iBadCount += Add;
  364. // Not installed
  365. if (Pkg->CurrentVer == 0)
  366. {
  367. if (State.Mode == ModeDelete &&
  368. (State.iFlags | Purge) == Purge && Pkg.Purge() == false)
  369. iDelCount += Add;
  370. if (State.Mode == ModeInstall)
  371. iInstCount += Add;
  372. return;
  373. }
  374. // Installed, no upgrade
  375. if (State.Status == 0)
  376. {
  377. if (State.Mode == ModeDelete)
  378. iDelCount += Add;
  379. else
  380. if ((State.iFlags & ReInstall) == ReInstall)
  381. iInstCount += Add;
  382. return;
  383. }
  384. // Alll 3 are possible
  385. if (State.Mode == ModeDelete)
  386. iDelCount += Add;
  387. if (State.Mode == ModeKeep)
  388. iKeepCount += Add;
  389. if (State.Mode == ModeInstall)
  390. iInstCount += Add;
  391. }
  392. /*}}}*/
  393. // DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
  394. // ---------------------------------------------------------------------
  395. /* The or group results are stored in the last item of the or group. This
  396. allows easy detection of the state of a whole or'd group. */
  397. void pkgDepCache::BuildGroupOrs(VerIterator const &V)
  398. {
  399. unsigned char Group = 0;
  400. for (DepIterator D = V.DependsList(); D.end() != true; D++)
  401. {
  402. // Build the dependency state.
  403. unsigned char &State = DepState[D->ID];
  404. /* Invert for Conflicts. We have to do this twice to get the
  405. right sense for a conflicts group */
  406. if (D->Type == Dep::Conflicts ||
  407. D->Type == Dep::DpkgBreaks ||
  408. D->Type == Dep::Obsoletes)
  409. State = ~State;
  410. // Add to the group if we are within an or..
  411. State &= 0x7;
  412. Group |= State;
  413. State |= Group << 3;
  414. if ((D->CompareOp & Dep::Or) != Dep::Or)
  415. Group = 0;
  416. // Invert for Conflicts
  417. if (D->Type == Dep::Conflicts ||
  418. D->Type == Dep::DpkgBreaks ||
  419. D->Type == Dep::Obsoletes)
  420. State = ~State;
  421. }
  422. }
  423. /*}}}*/
  424. // DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
  425. // ---------------------------------------------------------------------
  426. /* This is used to run over a dependency list and determine the dep
  427. state of the list, filtering it through both a Min check and a Policy
  428. check. The return result will have SetMin/SetPolicy low if a check
  429. fails. It uses the DepState cache for it's computations. */
  430. unsigned char pkgDepCache::VersionState(DepIterator D,unsigned char Check,
  431. unsigned char SetMin,
  432. unsigned char SetPolicy)
  433. {
  434. unsigned char Dep = 0xFF;
  435. while (D.end() != true)
  436. {
  437. // Compute a single dependency element (glob or)
  438. DepIterator Start = D;
  439. unsigned char State = 0;
  440. for (bool LastOR = true; D.end() == false && LastOR == true; D++)
  441. {
  442. State |= DepState[D->ID];
  443. LastOR = (D->CompareOp & Dep::Or) == Dep::Or;
  444. }
  445. // Minimum deps that must be satisfied to have a working package
  446. if (Start.IsCritical() == true)
  447. if ((State & Check) != Check)
  448. Dep &= ~SetMin;
  449. // Policy deps that must be satisfied to install the package
  450. if (IsImportantDep(Start) == true &&
  451. (State & Check) != Check)
  452. Dep &= ~SetPolicy;
  453. }
  454. return Dep;
  455. }
  456. /*}}}*/
  457. // DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
  458. // ---------------------------------------------------------------------
  459. /* This is the main dependency computation bit. It computes the 3 main
  460. results for a dependencys, Now, Install and Candidate. Callers must
  461. invert the result if dealing with conflicts. */
  462. unsigned char pkgDepCache::DependencyState(DepIterator &D)
  463. {
  464. unsigned char State = 0;
  465. if (CheckDep(D,NowVersion) == true)
  466. State |= DepNow;
  467. if (CheckDep(D,InstallVersion) == true)
  468. State |= DepInstall;
  469. if (CheckDep(D,CandidateVersion) == true)
  470. State |= DepCVer;
  471. return State;
  472. }
  473. /*}}}*/
  474. // DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
  475. // ---------------------------------------------------------------------
  476. /* This determines the combined dependency representation of a package
  477. for its two states now and install. This is done by using the pre-generated
  478. dependency information. */
  479. void pkgDepCache::UpdateVerState(PkgIterator Pkg)
  480. {
  481. // Empty deps are always true
  482. StateCache &State = PkgState[Pkg->ID];
  483. State.DepState = 0xFF;
  484. // Check the Current state
  485. if (Pkg->CurrentVer != 0)
  486. {
  487. DepIterator D = Pkg.CurrentVer().DependsList();
  488. State.DepState &= VersionState(D,DepNow,DepNowMin,DepNowPolicy);
  489. }
  490. /* Check the candidate state. We do not compare against the whole as
  491. a candidate state but check the candidate version against the
  492. install states */
  493. if (State.CandidateVer != 0)
  494. {
  495. DepIterator D = State.CandidateVerIter(*this).DependsList();
  496. State.DepState &= VersionState(D,DepInstall,DepCandMin,DepCandPolicy);
  497. }
  498. // Check target state which can only be current or installed
  499. if (State.InstallVer != 0)
  500. {
  501. DepIterator D = State.InstVerIter(*this).DependsList();
  502. State.DepState &= VersionState(D,DepInstall,DepInstMin,DepInstPolicy);
  503. }
  504. }
  505. /*}}}*/
  506. // DepCache::Update - Figure out all the state information /*{{{*/
  507. // ---------------------------------------------------------------------
  508. /* This will figure out the state of all the packages and all the
  509. dependencies based on the current policy. */
  510. void pkgDepCache::Update(OpProgress *Prog)
  511. {
  512. iUsrSize = 0;
  513. iDownloadSize = 0;
  514. iDelCount = 0;
  515. iInstCount = 0;
  516. iKeepCount = 0;
  517. iBrokenCount = 0;
  518. iBadCount = 0;
  519. // Perform the depends pass
  520. int Done = 0;
  521. for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
  522. {
  523. if (Prog != 0 && Done%20 == 0)
  524. Prog->Progress(Done);
  525. for (VerIterator V = I.VersionList(); V.end() != true; V++)
  526. {
  527. unsigned char Group = 0;
  528. for (DepIterator D = V.DependsList(); D.end() != true; D++)
  529. {
  530. // Build the dependency state.
  531. unsigned char &State = DepState[D->ID];
  532. State = DependencyState(D);
  533. // Add to the group if we are within an or..
  534. Group |= State;
  535. State |= Group << 3;
  536. if ((D->CompareOp & Dep::Or) != Dep::Or)
  537. Group = 0;
  538. // Invert for Conflicts
  539. if (D->Type == Dep::Conflicts ||
  540. D->Type == Dep::DpkgBreaks ||
  541. D->Type == Dep::Obsoletes)
  542. State = ~State;
  543. }
  544. }
  545. // Compute the pacakge dependency state and size additions
  546. AddSizes(I);
  547. UpdateVerState(I);
  548. AddStates(I);
  549. }
  550. if (Prog != 0)
  551. Prog->Progress(Done);
  552. readStateFile(Prog);
  553. }
  554. /*}}}*/
  555. // DepCache::Update - Update the deps list of a package /*{{{*/
  556. // ---------------------------------------------------------------------
  557. /* This is a helper for update that only does the dep portion of the scan.
  558. It is mainly meant to scan reverse dependencies. */
  559. void pkgDepCache::Update(DepIterator D)
  560. {
  561. // Update the reverse deps
  562. for (;D.end() != true; D++)
  563. {
  564. unsigned char &State = DepState[D->ID];
  565. State = DependencyState(D);
  566. // Invert for Conflicts
  567. if (D->Type == Dep::Conflicts ||
  568. D->Type == Dep::DpkgBreaks ||
  569. D->Type == Dep::Obsoletes)
  570. State = ~State;
  571. RemoveStates(D.ParentPkg());
  572. BuildGroupOrs(D.ParentVer());
  573. UpdateVerState(D.ParentPkg());
  574. AddStates(D.ParentPkg());
  575. }
  576. }
  577. /*}}}*/
  578. // DepCache::Update - Update the related deps of a package /*{{{*/
  579. // ---------------------------------------------------------------------
  580. /* This is called whenever the state of a package changes. It updates
  581. all cached dependencies related to this package. */
  582. void pkgDepCache::Update(PkgIterator const &Pkg)
  583. {
  584. // Recompute the dep of the package
  585. RemoveStates(Pkg);
  586. UpdateVerState(Pkg);
  587. AddStates(Pkg);
  588. // Update the reverse deps
  589. Update(Pkg.RevDependsList());
  590. // Update the provides map for the current ver
  591. if (Pkg->CurrentVer != 0)
  592. for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
  593. P.end() != true; P++)
  594. Update(P.ParentPkg().RevDependsList());
  595. // Update the provides map for the candidate ver
  596. if (PkgState[Pkg->ID].CandidateVer != 0)
  597. for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
  598. P.end() != true; P++)
  599. Update(P.ParentPkg().RevDependsList());
  600. }
  601. /*}}}*/
  602. // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
  603. // ---------------------------------------------------------------------
  604. /* */
  605. void pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser)
  606. {
  607. // Simplifies other routines.
  608. if (Pkg.end() == true)
  609. return;
  610. /* Reject an attempt to keep a non-source broken installed package, those
  611. must be upgraded */
  612. if (Pkg.State() == PkgIterator::NeedsUnpack &&
  613. Pkg.CurrentVer().Downloadable() == false)
  614. return;
  615. /** \todo Can this be moved later in the method? */
  616. ActionGroup group(*this);
  617. /* We changed the soft state all the time so the UI is a bit nicer
  618. to use */
  619. StateCache &P = PkgState[Pkg->ID];
  620. if (Soft == true)
  621. P.iFlags |= AutoKept;
  622. else
  623. P.iFlags &= ~AutoKept;
  624. // Check that it is not already kept
  625. if (P.Mode == ModeKeep)
  626. return;
  627. // We dont even try to keep virtual packages..
  628. if (Pkg->VersionList == 0)
  629. return;
  630. #if 0 // reseting the autoflag here means we lose the
  631. // auto-mark information if a user selects a package for removal
  632. // but changes his mind then and sets it for keep again
  633. // - this makes sense as default when all Garbage dependencies
  634. // are automatically marked for removal (as aptitude does).
  635. // setting a package for keep then makes it no longer autoinstalled
  636. // for all other use-case this action is rather suprising
  637. if(FromUser && !P.Marked)
  638. P.Flags &= ~Flag::Auto;
  639. #endif
  640. RemoveSizes(Pkg);
  641. RemoveStates(Pkg);
  642. P.Mode = ModeKeep;
  643. if (Pkg->CurrentVer == 0)
  644. P.InstallVer = 0;
  645. else
  646. P.InstallVer = Pkg.CurrentVer();
  647. AddStates(Pkg);
  648. Update(Pkg);
  649. AddSizes(Pkg);
  650. }
  651. /*}}}*/
  652. // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
  653. // ---------------------------------------------------------------------
  654. /* */
  655. void pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge)
  656. {
  657. // Simplifies other routines.
  658. if (Pkg.end() == true)
  659. return;
  660. ActionGroup group(*this);
  661. // Check that it is not already marked for delete
  662. StateCache &P = PkgState[Pkg->ID];
  663. P.iFlags &= ~(AutoKept | Purge);
  664. if (rPurge == true)
  665. P.iFlags |= Purge;
  666. if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
  667. (Pkg.Purge() == true || rPurge == false))
  668. return;
  669. // We dont even try to delete virtual packages..
  670. if (Pkg->VersionList == 0)
  671. return;
  672. RemoveSizes(Pkg);
  673. RemoveStates(Pkg);
  674. if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
  675. P.Mode = ModeKeep;
  676. else
  677. P.Mode = ModeDelete;
  678. P.InstallVer = 0;
  679. AddStates(Pkg);
  680. Update(Pkg);
  681. AddSizes(Pkg);
  682. }
  683. /*}}}*/
  684. // DepCache::MarkInstall - Put the package in the install state /*{{{*/
  685. // ---------------------------------------------------------------------
  686. /* */
  687. void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
  688. unsigned long Depth, bool FromUser,
  689. bool ForceImportantDeps)
  690. {
  691. if (Depth > 100)
  692. return;
  693. // Simplifies other routines.
  694. if (Pkg.end() == true)
  695. return;
  696. ActionGroup group(*this);
  697. /* Check that it is not already marked for install and that it can be
  698. installed */
  699. StateCache &P = PkgState[Pkg->ID];
  700. P.iFlags &= ~AutoKept;
  701. if ((P.InstPolicyBroken() == false && P.InstBroken() == false) &&
  702. (P.Mode == ModeInstall ||
  703. P.CandidateVer == (Version *)Pkg.CurrentVer()))
  704. {
  705. if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
  706. MarkKeep(Pkg, false, FromUser);
  707. return;
  708. }
  709. // See if there is even any possible instalation candidate
  710. if (P.CandidateVer == 0)
  711. return;
  712. // We dont even try to install virtual packages..
  713. if (Pkg->VersionList == 0)
  714. return;
  715. /* Target the candidate version and remove the autoflag. We reset the
  716. autoflag below if this was called recursively. Otherwise the user
  717. should have the ability to de-auto a package by changing its state */
  718. RemoveSizes(Pkg);
  719. RemoveStates(Pkg);
  720. P.Mode = ModeInstall;
  721. P.InstallVer = P.CandidateVer;
  722. if(FromUser)
  723. {
  724. // Set it to manual if it's a new install or cancelling the
  725. // removal of a garbage package.
  726. if(P.Status == 2 || (!Pkg.CurrentVer().end() && !P.Marked))
  727. P.Flags &= ~Flag::Auto;
  728. }
  729. else
  730. {
  731. // Set it to auto if this is a new install.
  732. if(P.Status == 2)
  733. P.Flags |= Flag::Auto;
  734. }
  735. if (P.CandidateVer == (Version *)Pkg.CurrentVer())
  736. P.Mode = ModeKeep;
  737. AddStates(Pkg);
  738. Update(Pkg);
  739. AddSizes(Pkg);
  740. if (AutoInst == false)
  741. return;
  742. DepIterator Dep = P.InstVerIter(*this).DependsList();
  743. for (; Dep.end() != true;)
  744. {
  745. // Grok or groups
  746. DepIterator Start = Dep;
  747. bool Result = true;
  748. unsigned Ors = 0;
  749. for (bool LastOR = true; Dep.end() == false && LastOR == true; Dep++,Ors++)
  750. {
  751. LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
  752. if ((DepState[Dep->ID] & DepInstall) == DepInstall)
  753. Result = false;
  754. }
  755. // Dep is satisfied okay.
  756. if (Result == false)
  757. continue;
  758. /* Check if this dep should be consider for install. If it is a user
  759. defined important dep and we are installed a new package then
  760. it will be installed. Otherwise we only check for important
  761. deps that have changed from the installed version
  762. */
  763. if (IsImportantDep(Start) == false)
  764. continue;
  765. /* check if any ImportantDep() (but not Critial) where added
  766. * since we installed the package
  767. */
  768. bool isNewImportantDep = false;
  769. if(!ForceImportantDeps && !Start.IsCritical())
  770. {
  771. bool found=false;
  772. VerIterator instVer = Pkg.CurrentVer();
  773. if(!instVer.end())
  774. {
  775. for (DepIterator D = instVer.DependsList(); D.end() != true; D++)
  776. {
  777. //FIXME: deal better with or-groups(?)
  778. DepIterator LocalStart = D;
  779. if(IsImportantDep(D) && Start.TargetPkg() == D.TargetPkg())
  780. found=true;
  781. }
  782. // this is a new dep if it was not found to be already
  783. // a important dep of the installed pacakge
  784. isNewImportantDep = !found;
  785. }
  786. }
  787. if(isNewImportantDep)
  788. if(_config->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
  789. std::clog << "new important dependency: "
  790. << Start.TargetPkg().Name() << std::endl;
  791. // skip important deps if the package is already installed
  792. if (Pkg->CurrentVer != 0 && Start.IsCritical() == false
  793. && !isNewImportantDep && !ForceImportantDeps)
  794. continue;
  795. /* If we are in an or group locate the first or that can
  796. succeed. We have already cached this.. */
  797. for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; Ors--)
  798. Start++;
  799. /* This bit is for processing the possibilty of an install/upgrade
  800. fixing the problem */
  801. SPtrArray<Version *> List = Start.AllTargets();
  802. if (Start->Type != Dep::DpkgBreaks &&
  803. (DepState[Start->ID] & DepCVer) == DepCVer)
  804. {
  805. // Right, find the best version to install..
  806. Version **Cur = List;
  807. PkgIterator P = Start.TargetPkg();
  808. PkgIterator InstPkg(*Cache,0);
  809. // See if there are direct matches (at the start of the list)
  810. for (; *Cur != 0 && (*Cur)->ParentPkg == P.Index(); Cur++)
  811. {
  812. PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
  813. if (PkgState[Pkg->ID].CandidateVer != *Cur)
  814. continue;
  815. InstPkg = Pkg;
  816. break;
  817. }
  818. // Select the highest priority providing package
  819. if (InstPkg.end() == true)
  820. {
  821. pkgPrioSortList(*Cache,Cur);
  822. for (; *Cur != 0; Cur++)
  823. {
  824. PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
  825. if (PkgState[Pkg->ID].CandidateVer != *Cur)
  826. continue;
  827. InstPkg = Pkg;
  828. break;
  829. }
  830. }
  831. if (InstPkg.end() == false)
  832. {
  833. if(_config->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
  834. std::clog << "Installing " << InstPkg.Name()
  835. << " as dep of " << Pkg.Name()
  836. << std::endl;
  837. // now check if we should consider it a automatic dependency or not
  838. string sec = _config->Find("APT::Never-MarkAuto-Section","");
  839. if(Pkg.Section() && (string(Pkg.Section()) == sec))
  840. {
  841. if(_config->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
  842. std::clog << "Setting NOT as auto-installed because its a direct dep of a package in section " << sec << std::endl;
  843. MarkInstall(InstPkg,true,Depth + 1, true, ForceImportantDeps);
  844. }
  845. else
  846. {
  847. // mark automatic dependency
  848. MarkInstall(InstPkg,true,Depth + 1, false, ForceImportantDeps);
  849. // Set the autoflag, after MarkInstall because MarkInstall unsets it
  850. if (P->CurrentVer == 0)
  851. PkgState[InstPkg->ID].Flags |= Flag::Auto;
  852. }
  853. }
  854. continue;
  855. }
  856. /* For conflicts we just de-install the package and mark as auto,
  857. Conflicts may not have or groups. For dpkg's Breaks we try to
  858. upgrade the package. */
  859. if (Start->Type == Dep::Conflicts || Start->Type == Dep::Obsoletes ||
  860. Start->Type == Dep::DpkgBreaks)
  861. {
  862. for (Version **I = List; *I != 0; I++)
  863. {
  864. VerIterator Ver(*this,*I);
  865. PkgIterator Pkg = Ver.ParentPkg();
  866. if (Start->Type != Dep::DpkgBreaks)
  867. MarkDelete(Pkg);
  868. else
  869. if (PkgState[Pkg->ID].CandidateVer != *I)
  870. MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps);
  871. }
  872. continue;
  873. }
  874. }
  875. }
  876. /*}}}*/
  877. // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
  878. // ---------------------------------------------------------------------
  879. /* */
  880. void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
  881. {
  882. ActionGroup group(*this);
  883. RemoveSizes(Pkg);
  884. RemoveStates(Pkg);
  885. StateCache &P = PkgState[Pkg->ID];
  886. if (To == true)
  887. P.iFlags |= ReInstall;
  888. else
  889. P.iFlags &= ~ReInstall;
  890. AddStates(Pkg);
  891. AddSizes(Pkg);
  892. }
  893. /*}}}*/
  894. // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
  895. // ---------------------------------------------------------------------
  896. /* */
  897. void pkgDepCache::SetCandidateVersion(VerIterator TargetVer)
  898. {
  899. ActionGroup group(*this);
  900. pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
  901. StateCache &P = PkgState[Pkg->ID];
  902. RemoveSizes(Pkg);
  903. RemoveStates(Pkg);
  904. if (P.CandidateVer == P.InstallVer)
  905. P.InstallVer = (Version *)TargetVer;
  906. P.CandidateVer = (Version *)TargetVer;
  907. P.Update(Pkg,*this);
  908. AddStates(Pkg);
  909. Update(Pkg);
  910. AddSizes(Pkg);
  911. }
  912. void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
  913. {
  914. StateCache &state = PkgState[Pkg->ID];
  915. ActionGroup group(*this);
  916. if(Auto)
  917. state.Flags |= Flag::Auto;
  918. else
  919. state.Flags &= ~Flag::Auto;
  920. }
  921. /*}}}*/
  922. // StateCache::Update - Compute the various static display things /*{{{*/
  923. // ---------------------------------------------------------------------
  924. /* This is called whenever the Candidate version changes. */
  925. void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
  926. {
  927. // Some info
  928. VerIterator Ver = CandidateVerIter(Cache);
  929. // Use a null string or the version string
  930. if (Ver.end() == true)
  931. CandVersion = "";
  932. else
  933. CandVersion = Ver.VerStr();
  934. // Find the current version
  935. CurVersion = "";
  936. if (Pkg->CurrentVer != 0)
  937. CurVersion = Pkg.CurrentVer().VerStr();
  938. // Strip off the epochs for display
  939. CurVersion = StripEpoch(CurVersion);
  940. CandVersion = StripEpoch(CandVersion);
  941. // Figure out if its up or down or equal
  942. Status = Ver.CompareVer(Pkg.CurrentVer());
  943. if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
  944. Status = 2;
  945. }
  946. /*}}}*/
  947. // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
  948. // ---------------------------------------------------------------------
  949. /* */
  950. const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
  951. {
  952. if (Ver == 0)
  953. return 0;
  954. // Strip any epoch
  955. for (const char *I = Ver; *I != 0; I++)
  956. if (*I == ':')
  957. return I + 1;
  958. return Ver;
  959. }
  960. /*}}}*/
  961. // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
  962. // ---------------------------------------------------------------------
  963. /* The default just returns the highest available version that is not
  964. a source and automatic. */
  965. pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator Pkg)
  966. {
  967. /* Not source/not automatic versions cannot be a candidate version
  968. unless they are already installed */
  969. VerIterator Last(*(pkgCache *)this,0);
  970. for (VerIterator I = Pkg.VersionList(); I.end() == false; I++)
  971. {
  972. if (Pkg.CurrentVer() == I)
  973. return I;
  974. for (VerFileIterator J = I.FileList(); J.end() == false; J++)
  975. {
  976. if ((J.File()->Flags & Flag::NotSource) != 0)
  977. continue;
  978. /* Stash the highest version of a not-automatic source, we use it
  979. if there is nothing better */
  980. if ((J.File()->Flags & Flag::NotAutomatic) != 0)
  981. {
  982. if (Last.end() == true)
  983. Last = I;
  984. continue;
  985. }
  986. return I;
  987. }
  988. }
  989. return Last;
  990. }
  991. /*}}}*/
  992. /*}}}*/
  993. pkgDepCache::DefaultRootSetFunc::DefaultRootSetFunc()
  994. : constructedSuccessfully(false)
  995. {
  996. Configuration::Item const *Opts;
  997. Opts = _config->Tree("APT::NeverAutoRemove");
  998. if (Opts != 0 && Opts->Child != 0)
  999. {
  1000. Opts = Opts->Child;
  1001. for (; Opts != 0; Opts = Opts->Next)
  1002. {
  1003. if (Opts->Value.empty() == true)
  1004. continue;
  1005. regex_t *p = new regex_t;
  1006. if(regcomp(p,Opts->Value.c_str(),
  1007. REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
  1008. {
  1009. regfree(p);
  1010. delete p;
  1011. _error->Error("Regex compilation error for APT::NeverAutoRemove");
  1012. return;
  1013. }
  1014. rootSetRegexp.push_back(p);
  1015. }
  1016. }
  1017. constructedSuccessfully = true;
  1018. }
  1019. pkgDepCache::DefaultRootSetFunc::~DefaultRootSetFunc()
  1020. {
  1021. for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
  1022. {
  1023. regfree(rootSetRegexp[i]);
  1024. delete rootSetRegexp[i];
  1025. }
  1026. }
  1027. bool pkgDepCache::DefaultRootSetFunc::InRootSet(const pkgCache::PkgIterator &pkg)
  1028. {
  1029. for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
  1030. if (regexec(rootSetRegexp[i], pkg.Name(), 0, 0, 0) == 0)
  1031. return true;
  1032. return false;
  1033. }
  1034. pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc()
  1035. {
  1036. DefaultRootSetFunc *f = new DefaultRootSetFunc;
  1037. if(f->wasConstructedSuccessfully())
  1038. return f;
  1039. else
  1040. {
  1041. delete f;
  1042. return NULL;
  1043. }
  1044. }
  1045. bool pkgDepCache::MarkFollowsRecommends()
  1046. {
  1047. return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
  1048. }
  1049. bool pkgDepCache::MarkFollowsSuggests()
  1050. {
  1051. return _config->FindB("APT::AutoRemove::SuggestsImportant", false);
  1052. }
  1053. // the main mark algorithm
  1054. bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
  1055. {
  1056. bool follow_recommends;
  1057. bool follow_suggests;
  1058. // init the states
  1059. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1060. {
  1061. PkgState[p->ID].Marked = false;
  1062. PkgState[p->ID].Garbage = false;
  1063. // debug output
  1064. if(_config->FindB("Debug::pkgAutoRemove",false)
  1065. && PkgState[p->ID].Flags & Flag::Auto)
  1066. std::clog << "AutoDep: " << p.Name() << std::endl;
  1067. }
  1068. // init vars
  1069. follow_recommends = MarkFollowsRecommends();
  1070. follow_suggests = MarkFollowsSuggests();
  1071. // do the mark part, this is the core bit of the algorithm
  1072. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1073. {
  1074. if(!(PkgState[p->ID].Flags & Flag::Auto) ||
  1075. (p->Flags & Flag::Essential) ||
  1076. userFunc.InRootSet(p))
  1077. {
  1078. // the package is installed (and set to keep)
  1079. if(PkgState[p->ID].Keep() && !p.CurrentVer().end())
  1080. MarkPackage(p, p.CurrentVer(),
  1081. follow_recommends, follow_suggests);
  1082. // the package is to be installed
  1083. else if(PkgState[p->ID].Install())
  1084. MarkPackage(p, PkgState[p->ID].InstVerIter(*this),
  1085. follow_recommends, follow_suggests);
  1086. }
  1087. }
  1088. return true;
  1089. }
  1090. // mark a single package in Mark-and-Sweep
  1091. void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
  1092. const pkgCache::VerIterator &ver,
  1093. bool follow_recommends,
  1094. bool follow_suggests)
  1095. {
  1096. pkgDepCache::StateCache &state = PkgState[pkg->ID];
  1097. VerIterator candver = state.CandidateVerIter(*this);
  1098. VerIterator instver = state.InstVerIter(*this);
  1099. #if 0
  1100. // If a package was garbage-collected but is now being marked, we
  1101. // should re-select it
  1102. // For cases when a pkg is set to upgrade and this trigger the
  1103. // removal of a no-longer used dependency. if the pkg is set to
  1104. // keep again later it will result in broken deps
  1105. if(state.Delete() && state.RemoveReason = Unused)
  1106. {
  1107. if(ver==candver)
  1108. mark_install(pkg, false, false, NULL);
  1109. else if(ver==pkg.CurrentVer())
  1110. MarkKeep(pkg, false, false);
  1111. instver=state.InstVerIter(*this);
  1112. }
  1113. #endif
  1114. // Ignore versions other than the InstVer, and ignore packages
  1115. // that are already going to be removed or just left uninstalled.
  1116. if(!(ver == instver && !instver.end()))
  1117. return;
  1118. // if we are marked already we are done
  1119. if(state.Marked)
  1120. return;
  1121. //std::cout << "Setting Marked for: " << pkg.Name() << std::endl;
  1122. state.Marked=true;
  1123. if(!ver.end())
  1124. {
  1125. for(DepIterator d = ver.DependsList(); !d.end(); ++d)
  1126. {
  1127. if(d->Type == Dep::Depends ||
  1128. d->Type == Dep::PreDepends ||
  1129. (follow_recommends &&
  1130. d->Type == Dep::Recommends) ||
  1131. (follow_suggests &&
  1132. d->Type == Dep::Suggests))
  1133. {
  1134. // Try all versions of this package.
  1135. for(VerIterator V = d.TargetPkg().VersionList();
  1136. !V.end(); ++V)
  1137. {
  1138. if(_system->VS->CheckDep(V.VerStr(), d->CompareOp, d.TargetVer()))
  1139. {
  1140. MarkPackage(V.ParentPkg(), V,
  1141. follow_recommends, follow_suggests);
  1142. }
  1143. }
  1144. // Now try virtual packages
  1145. for(PrvIterator prv=d.TargetPkg().ProvidesList();
  1146. !prv.end(); ++prv)
  1147. {
  1148. if(_system->VS->CheckDep(prv.ProvideVersion(), d->CompareOp,
  1149. d.TargetVer()))
  1150. {
  1151. MarkPackage(prv.OwnerPkg(), prv.OwnerVer(),
  1152. follow_recommends, follow_suggests);
  1153. }
  1154. }
  1155. }
  1156. }
  1157. }
  1158. }
  1159. bool pkgDepCache::Sweep()
  1160. {
  1161. // do the sweep
  1162. for(PkgIterator p=PkgBegin(); !p.end(); ++p)
  1163. {
  1164. StateCache &state=PkgState[p->ID];
  1165. // skip required packages
  1166. if (!p.CurrentVer().end() &&
  1167. (p.CurrentVer()->Priority == pkgCache::State::Required))
  1168. continue;
  1169. // if it is not marked and it is installed, it's garbage
  1170. if(!state.Marked && (!p.CurrentVer().end() || state.Install()))
  1171. {
  1172. state.Garbage=true;
  1173. if(_config->FindB("Debug::pkgAutoRemove",false))
  1174. std::cout << "Garbage: " << p.Name() << std::endl;
  1175. }
  1176. }
  1177. return true;
  1178. }
  1179. // Policy::IsImportantDep - True if the dependency is important /*{{{*/
  1180. // ---------------------------------------------------------------------
  1181. /* */
  1182. bool pkgDepCache::Policy::IsImportantDep(DepIterator Dep)
  1183. {
  1184. if(Dep.IsCritical())
  1185. return true;
  1186. else if(Dep->Type == pkgCache::Dep::Recommends)
  1187. {
  1188. if ( _config->FindB("APT::Install-Recommends", false))
  1189. return true;
  1190. // we suport a special mode to only install-recommends for certain
  1191. // sections
  1192. // FIXME: this is a meant as a temporarly solution until the
  1193. // recommends are cleaned up
  1194. string s = _config->Find("APT::Install-Recommends-Section","");
  1195. if(s.size() > 0)
  1196. {
  1197. const char *sec = Dep.ParentVer().Section();
  1198. if (sec && strcmp(sec, s.c_str()) == 0)
  1199. return true;
  1200. }
  1201. }
  1202. else if(Dep->Type == pkgCache::Dep::Suggests)
  1203. return _config->FindB("APT::Install-Suggests", false);
  1204. return false;
  1205. }
  1206. /*}}}*/