depcache.cc 39 KB

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