depcache.cc 54 KB

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