depcache.cc 39 KB

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