depcache.cc 39 KB

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