depcache.cc 39 KB

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