depcache.cc 40 KB

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