depcache.cc 46 KB

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