depcache.cc 39 KB

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