depcache.cc 43 KB

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