depcache.cc 45 KB

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