depcache.cc 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  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. // FIXME: String comparison isn't a save indicator!
  766. if (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. P->CurrentVer = V.Index();
  772. AddStates(P);
  773. Update(P);
  774. AddSizes(P);
  775. checkChanged = true;
  776. break;
  777. }
  778. }
  779. recheck.erase(g);
  780. }
  781. } while (checkChanged == true && recheck.empty() == false);
  782. if (_config->FindB("Debug::MultiArchKiller", false) == true)
  783. for(std::set<unsigned long>::const_iterator g = recheck.begin();
  784. g != recheck.end(); ++g)
  785. std::cout << "No pseudo package for »" << GrpIterator(*Cache, Cache->GrpP + *g).Name() << "« installed" << std::endl;
  786. }
  787. if (Prog != 0)
  788. Prog->Progress(Done);
  789. readStateFile(Prog);
  790. }
  791. /*}}}*/
  792. // DepCache::Update - Update the deps list of a package /*{{{*/
  793. // ---------------------------------------------------------------------
  794. /* This is a helper for update that only does the dep portion of the scan.
  795. It is mainly meant to scan reverse dependencies. */
  796. void pkgDepCache::Update(DepIterator D)
  797. {
  798. // Update the reverse deps
  799. for (;D.end() != true; D++)
  800. {
  801. unsigned char &State = DepState[D->ID];
  802. State = DependencyState(D);
  803. // Invert for Conflicts
  804. if (D->Type == Dep::Conflicts ||
  805. D->Type == Dep::DpkgBreaks ||
  806. D->Type == Dep::Obsoletes)
  807. State = ~State;
  808. RemoveStates(D.ParentPkg());
  809. BuildGroupOrs(D.ParentVer());
  810. UpdateVerState(D.ParentPkg());
  811. AddStates(D.ParentPkg());
  812. }
  813. }
  814. /*}}}*/
  815. // DepCache::Update - Update the related deps of a package /*{{{*/
  816. // ---------------------------------------------------------------------
  817. /* This is called whenever the state of a package changes. It updates
  818. all cached dependencies related to this package. */
  819. void pkgDepCache::Update(PkgIterator const &Pkg)
  820. {
  821. // Recompute the dep of the package
  822. RemoveStates(Pkg);
  823. UpdateVerState(Pkg);
  824. AddStates(Pkg);
  825. // Update the reverse deps
  826. Update(Pkg.RevDependsList());
  827. // Update the provides map for the current ver
  828. if (Pkg->CurrentVer != 0)
  829. for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
  830. P.end() != true; P++)
  831. Update(P.ParentPkg().RevDependsList());
  832. // Update the provides map for the candidate ver
  833. if (PkgState[Pkg->ID].CandidateVer != 0)
  834. for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
  835. P.end() != true; P++)
  836. Update(P.ParentPkg().RevDependsList());
  837. }
  838. /*}}}*/
  839. // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
  840. // ---------------------------------------------------------------------
  841. /* */
  842. void pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser,
  843. unsigned long Depth)
  844. {
  845. // Simplifies other routines.
  846. if (Pkg.end() == true)
  847. return;
  848. /* Reject an attempt to keep a non-source broken installed package, those
  849. must be upgraded */
  850. if (Pkg.State() == PkgIterator::NeedsUnpack &&
  851. Pkg.CurrentVer().Downloadable() == false)
  852. return;
  853. /** \todo Can this be moved later in the method? */
  854. ActionGroup group(*this);
  855. /* We changed the soft state all the time so the UI is a bit nicer
  856. to use */
  857. StateCache &P = PkgState[Pkg->ID];
  858. if (Soft == true)
  859. P.iFlags |= AutoKept;
  860. else
  861. P.iFlags &= ~AutoKept;
  862. // Check that it is not already kept
  863. if (P.Mode == ModeKeep)
  864. return;
  865. // We dont even try to keep virtual packages..
  866. if (Pkg->VersionList == 0)
  867. return;
  868. #if 0 // reseting the autoflag here means we lose the
  869. // auto-mark information if a user selects a package for removal
  870. // but changes his mind then and sets it for keep again
  871. // - this makes sense as default when all Garbage dependencies
  872. // are automatically marked for removal (as aptitude does).
  873. // setting a package for keep then makes it no longer autoinstalled
  874. // for all other use-case this action is rather suprising
  875. if(FromUser && !P.Marked)
  876. P.Flags &= ~Flag::Auto;
  877. #endif
  878. if (DebugMarker == true)
  879. std::clog << OutputInDepth(Depth) << "MarkKeep " << Pkg << " FU=" << FromUser << std::endl;
  880. RemoveSizes(Pkg);
  881. RemoveStates(Pkg);
  882. P.Mode = ModeKeep;
  883. if (Pkg->CurrentVer == 0)
  884. P.InstallVer = 0;
  885. else
  886. P.InstallVer = Pkg.CurrentVer();
  887. AddStates(Pkg);
  888. Update(Pkg);
  889. AddSizes(Pkg);
  890. }
  891. /*}}}*/
  892. // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
  893. // ---------------------------------------------------------------------
  894. /* */
  895. void pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge,
  896. unsigned long Depth, bool FromUser)
  897. {
  898. // Simplifies other routines.
  899. if (Pkg.end() == true)
  900. return;
  901. ActionGroup group(*this);
  902. // Check that it is not already marked for delete
  903. StateCache &P = PkgState[Pkg->ID];
  904. P.iFlags &= ~(AutoKept | Purge);
  905. if (rPurge == true)
  906. P.iFlags |= Purge;
  907. if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
  908. (Pkg.Purge() == true || rPurge == false))
  909. return;
  910. // We dont even try to delete virtual packages..
  911. if (Pkg->VersionList == 0)
  912. return;
  913. // check if we are allowed to install the package
  914. if (IsDeleteOk(Pkg,rPurge,Depth,FromUser) == false)
  915. return;
  916. if (DebugMarker == true)
  917. std::clog << OutputInDepth(Depth) << "MarkDelete " << Pkg << " FU=" << FromUser << std::endl;
  918. RemoveSizes(Pkg);
  919. RemoveStates(Pkg);
  920. if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
  921. P.Mode = ModeKeep;
  922. else
  923. P.Mode = ModeDelete;
  924. P.InstallVer = 0;
  925. AddStates(Pkg);
  926. Update(Pkg);
  927. AddSizes(Pkg);
  928. // if we remove the pseudo package, we also need to remove the "real"
  929. if (Pkg->CurrentVer != 0 && Pkg.CurrentVer().Pseudo() == true)
  930. MarkDelete(Pkg.Group().FindPkg("all"), rPurge, Depth+1, FromUser);
  931. }
  932. /*}}}*/
  933. // DepCache::IsDeleteOk - check if it is ok to remove this package /*{{{*/
  934. // ---------------------------------------------------------------------
  935. /* The default implementation just honors dpkg hold
  936. But an application using this library can override this method
  937. to control the MarkDelete behaviour */
  938. bool pkgDepCache::IsDeleteOk(PkgIterator const &Pkg,bool rPurge,
  939. unsigned long Depth, bool FromUser)
  940. {
  941. if (FromUser == false && Pkg->SelectedState == pkgCache::State::Hold && _config->FindB("APT::Ignore-Hold",false) == false)
  942. {
  943. if (DebugMarker == true)
  944. std::clog << OutputInDepth(Depth) << "Hold prevents MarkDelete of " << Pkg << " FU=" << FromUser << std::endl;
  945. return false;
  946. }
  947. return true;
  948. }
  949. /*}}}*/
  950. // DepCache::MarkInstall - Put the package in the install state /*{{{*/
  951. // ---------------------------------------------------------------------
  952. /* */
  953. void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
  954. unsigned long Depth, bool FromUser,
  955. bool ForceImportantDeps)
  956. {
  957. if (Depth > 100)
  958. return;
  959. // Simplifies other routines.
  960. if (Pkg.end() == true)
  961. return;
  962. ActionGroup group(*this);
  963. /* Check that it is not already marked for install and that it can be
  964. installed */
  965. StateCache &P = PkgState[Pkg->ID];
  966. P.iFlags &= ~AutoKept;
  967. if ((P.InstPolicyBroken() == false && P.InstBroken() == false) &&
  968. (P.Mode == ModeInstall ||
  969. P.CandidateVer == (Version *)Pkg.CurrentVer()))
  970. {
  971. if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
  972. MarkKeep(Pkg, false, FromUser, Depth+1);
  973. return;
  974. }
  975. // See if there is even any possible instalation candidate
  976. if (P.CandidateVer == 0)
  977. return;
  978. // We dont even try to install virtual packages..
  979. if (Pkg->VersionList == 0)
  980. return;
  981. // check if we are allowed to install the package
  982. if (IsInstallOk(Pkg,AutoInst,Depth,FromUser) == false)
  983. return;
  984. /* Target the candidate version and remove the autoflag. We reset the
  985. autoflag below if this was called recursively. Otherwise the user
  986. should have the ability to de-auto a package by changing its state */
  987. RemoveSizes(Pkg);
  988. RemoveStates(Pkg);
  989. P.Mode = ModeInstall;
  990. P.InstallVer = P.CandidateVer;
  991. if(FromUser)
  992. {
  993. // Set it to manual if it's a new install or cancelling the
  994. // removal of a garbage package.
  995. if(P.Status == 2 || (!Pkg.CurrentVer().end() && !P.Marked))
  996. P.Flags &= ~Flag::Auto;
  997. }
  998. else
  999. {
  1000. // Set it to auto if this is a new install.
  1001. if(P.Status == 2)
  1002. P.Flags |= Flag::Auto;
  1003. }
  1004. if (P.CandidateVer == (Version *)Pkg.CurrentVer())
  1005. P.Mode = ModeKeep;
  1006. AddStates(Pkg);
  1007. Update(Pkg);
  1008. AddSizes(Pkg);
  1009. if (AutoInst == false)
  1010. return;
  1011. if (DebugMarker == true)
  1012. std::clog << OutputInDepth(Depth) << "MarkInstall " << Pkg << " FU=" << FromUser << std::endl;
  1013. DepIterator Dep = P.InstVerIter(*this).DependsList();
  1014. for (; Dep.end() != true;)
  1015. {
  1016. // Grok or groups
  1017. DepIterator Start = Dep;
  1018. bool Result = true;
  1019. unsigned Ors = 0;
  1020. for (bool LastOR = true; Dep.end() == false && LastOR == true; Dep++,Ors++)
  1021. {
  1022. LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
  1023. if ((DepState[Dep->ID] & DepInstall) == DepInstall)
  1024. Result = false;
  1025. }
  1026. // Dep is satisfied okay.
  1027. if (Result == false)
  1028. continue;
  1029. /* Check if this dep should be consider for install. If it is a user
  1030. defined important dep and we are installed a new package then
  1031. it will be installed. Otherwise we only check for important
  1032. deps that have changed from the installed version
  1033. */
  1034. if (IsImportantDep(Start) == false)
  1035. continue;
  1036. /* Check if any ImportantDep() (but not Critical) were added
  1037. * since we installed the package. Also check for deps that
  1038. * were satisfied in the past: for instance, if a version
  1039. * restriction in a Recommends was tightened, upgrading the
  1040. * package should follow that Recommends rather than causing the
  1041. * dependency to be removed. (bug #470115)
  1042. */
  1043. bool isNewImportantDep = false;
  1044. bool isPreviouslySatisfiedImportantDep = false;
  1045. if(!ForceImportantDeps && !Start.IsCritical())
  1046. {
  1047. bool found=false;
  1048. VerIterator instVer = Pkg.CurrentVer();
  1049. if(!instVer.end())
  1050. {
  1051. for (DepIterator D = instVer.DependsList(); D.end() != true; D++)
  1052. {
  1053. //FIXME: deal better with or-groups(?)
  1054. DepIterator LocalStart = D;
  1055. if(IsImportantDep(D) && !D.IsCritical() &&
  1056. Start.TargetPkg() == D.TargetPkg())
  1057. {
  1058. if(!isPreviouslySatisfiedImportantDep)
  1059. {
  1060. DepIterator D2 = D;
  1061. while((D2->CompareOp & Dep::Or) != 0)
  1062. ++D2;
  1063. isPreviouslySatisfiedImportantDep =
  1064. (((*this)[D2] & DepGNow) != 0);
  1065. }
  1066. found=true;
  1067. }
  1068. }
  1069. // this is a new dep if it was not found to be already
  1070. // a important dep of the installed pacakge
  1071. isNewImportantDep = !found;
  1072. }
  1073. }
  1074. if(isNewImportantDep)
  1075. if(DebugAutoInstall == true)
  1076. std::clog << OutputInDepth(Depth) << "new important dependency: "
  1077. << Start.TargetPkg().Name() << std::endl;
  1078. if(isPreviouslySatisfiedImportantDep)
  1079. if(DebugAutoInstall == true)
  1080. std::clog << OutputInDepth(Depth) << "previously satisfied important dependency on "
  1081. << Start.TargetPkg().Name() << std::endl;
  1082. // skip important deps if the package is already installed
  1083. if (Pkg->CurrentVer != 0 && Start.IsCritical() == false
  1084. && !isNewImportantDep && !isPreviouslySatisfiedImportantDep
  1085. && !ForceImportantDeps)
  1086. continue;
  1087. /* If we are in an or group locate the first or that can
  1088. succeed. We have already cached this.. */
  1089. for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; Ors--)
  1090. Start++;
  1091. /* This bit is for processing the possibilty of an install/upgrade
  1092. fixing the problem */
  1093. SPtrArray<Version *> List = Start.AllTargets();
  1094. if (Start->Type != Dep::DpkgBreaks &&
  1095. (DepState[Start->ID] & DepCVer) == DepCVer)
  1096. {
  1097. // Right, find the best version to install..
  1098. Version **Cur = List;
  1099. PkgIterator P = Start.TargetPkg();
  1100. PkgIterator InstPkg(*Cache,0);
  1101. // See if there are direct matches (at the start of the list)
  1102. for (; *Cur != 0 && (*Cur)->ParentPkg == P.Index(); Cur++)
  1103. {
  1104. PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
  1105. if (PkgState[Pkg->ID].CandidateVer != *Cur)
  1106. continue;
  1107. InstPkg = Pkg;
  1108. break;
  1109. }
  1110. // Select the highest priority providing package
  1111. if (InstPkg.end() == true)
  1112. {
  1113. pkgPrioSortList(*Cache,Cur);
  1114. for (; *Cur != 0; Cur++)
  1115. {
  1116. PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
  1117. if (PkgState[Pkg->ID].CandidateVer != *Cur)
  1118. continue;
  1119. InstPkg = Pkg;
  1120. break;
  1121. }
  1122. }
  1123. if (InstPkg.end() == false)
  1124. {
  1125. if(DebugAutoInstall == true)
  1126. std::clog << OutputInDepth(Depth) << "Installing " << InstPkg.Name()
  1127. << " as " << Start.DepType() << " of " << Pkg.Name()
  1128. << std::endl;
  1129. // now check if we should consider it a automatic dependency or not
  1130. if(Pkg.Section() && ConfigValueInSubTree("APT::Never-MarkAuto-Sections", Pkg.Section()))
  1131. {
  1132. if(DebugAutoInstall == true)
  1133. std::clog << OutputInDepth(Depth) << "Setting NOT as auto-installed (direct "
  1134. << Start.DepType() << " of pkg in APT::Never-MarkAuto-Sections)" << std::endl;
  1135. MarkInstall(InstPkg,true,Depth + 1, true);
  1136. }
  1137. else
  1138. {
  1139. // mark automatic dependency
  1140. MarkInstall(InstPkg,true,Depth + 1, false, ForceImportantDeps);
  1141. // Set the autoflag, after MarkInstall because MarkInstall unsets it
  1142. if (P->CurrentVer == 0)
  1143. PkgState[InstPkg->ID].Flags |= Flag::Auto;
  1144. }
  1145. }
  1146. continue;
  1147. }
  1148. /* For conflicts we just de-install the package and mark as auto,
  1149. Conflicts may not have or groups. For dpkg's Breaks we try to
  1150. upgrade the package. */
  1151. if (Start->Type == Dep::Conflicts || Start->Type == Dep::Obsoletes ||
  1152. Start->Type == Dep::DpkgBreaks)
  1153. {
  1154. for (Version **I = List; *I != 0; I++)
  1155. {
  1156. VerIterator Ver(*this,*I);
  1157. PkgIterator Pkg = Ver.ParentPkg();
  1158. if (Start->Type != Dep::DpkgBreaks)
  1159. MarkDelete(Pkg,false,Depth + 1, false);
  1160. else if (PkgState[Pkg->ID].CandidateVer != *I)
  1161. MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps);
  1162. }
  1163. continue;
  1164. }
  1165. }
  1166. }
  1167. /*}}}*/
  1168. // DepCache::IsInstallOk - check if it is ok to install this package /*{{{*/
  1169. // ---------------------------------------------------------------------
  1170. /* The default implementation just honors dpkg hold
  1171. But an application using this library can override this method
  1172. to control the MarkInstall behaviour */
  1173. bool pkgDepCache::IsInstallOk(PkgIterator const &Pkg,bool AutoInst,
  1174. unsigned long Depth, bool FromUser)
  1175. {
  1176. if (FromUser == false && Pkg->SelectedState == pkgCache::State::Hold && _config->FindB("APT::Ignore-Hold",false) == false)
  1177. {
  1178. if (DebugMarker == true)
  1179. std::clog << OutputInDepth(Depth) << "Hold prevents MarkInstall of " << Pkg << " FU=" << FromUser << std::endl;
  1180. return false;
  1181. }
  1182. return true;
  1183. }
  1184. /*}}}*/
  1185. // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
  1186. // ---------------------------------------------------------------------
  1187. /* */
  1188. void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
  1189. {
  1190. ActionGroup group(*this);
  1191. RemoveSizes(Pkg);
  1192. RemoveStates(Pkg);
  1193. StateCache &P = PkgState[Pkg->ID];
  1194. if (To == true)
  1195. P.iFlags |= ReInstall;
  1196. else
  1197. P.iFlags &= ~ReInstall;
  1198. AddStates(Pkg);
  1199. AddSizes(Pkg);
  1200. }
  1201. /*}}}*/
  1202. // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
  1203. // ---------------------------------------------------------------------
  1204. /* */
  1205. void pkgDepCache::SetCandidateVersion(VerIterator TargetVer)
  1206. {
  1207. ActionGroup group(*this);
  1208. pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
  1209. StateCache &P = PkgState[Pkg->ID];
  1210. RemoveSizes(Pkg);
  1211. RemoveStates(Pkg);
  1212. if (P.CandidateVer == P.InstallVer)
  1213. P.InstallVer = (Version *)TargetVer;
  1214. P.CandidateVer = (Version *)TargetVer;
  1215. P.Update(Pkg,*this);
  1216. AddStates(Pkg);
  1217. Update(Pkg);
  1218. AddSizes(Pkg);
  1219. }
  1220. void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
  1221. {
  1222. StateCache &state = PkgState[Pkg->ID];
  1223. ActionGroup group(*this);
  1224. if(Auto)
  1225. state.Flags |= Flag::Auto;
  1226. else
  1227. state.Flags &= ~Flag::Auto;
  1228. }
  1229. /*}}}*/
  1230. // StateCache::Update - Compute the various static display things /*{{{*/
  1231. // ---------------------------------------------------------------------
  1232. /* This is called whenever the Candidate version changes. */
  1233. void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
  1234. {
  1235. // Some info
  1236. VerIterator Ver = CandidateVerIter(Cache);
  1237. // Use a null string or the version string
  1238. if (Ver.end() == true)
  1239. CandVersion = "";
  1240. else
  1241. CandVersion = Ver.VerStr();
  1242. // Find the current version
  1243. CurVersion = "";
  1244. if (Pkg->CurrentVer != 0)
  1245. CurVersion = Pkg.CurrentVer().VerStr();
  1246. // Strip off the epochs for display
  1247. CurVersion = StripEpoch(CurVersion);
  1248. CandVersion = StripEpoch(CandVersion);
  1249. // Figure out if its up or down or equal
  1250. Status = Ver.CompareVer(Pkg.CurrentVer());
  1251. if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
  1252. Status = 2;
  1253. }
  1254. /*}}}*/
  1255. // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
  1256. // ---------------------------------------------------------------------
  1257. /* */
  1258. const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
  1259. {
  1260. if (Ver == 0)
  1261. return 0;
  1262. // Strip any epoch
  1263. for (const char *I = Ver; *I != 0; I++)
  1264. if (*I == ':')
  1265. return I + 1;
  1266. return Ver;
  1267. }
  1268. /*}}}*/
  1269. // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
  1270. // ---------------------------------------------------------------------
  1271. /* The default just returns the highest available version that is not
  1272. a source and automatic. */
  1273. pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator Pkg)
  1274. {
  1275. /* Not source/not automatic versions cannot be a candidate version
  1276. unless they are already installed */
  1277. VerIterator Last(*(pkgCache *)this,0);
  1278. for (VerIterator I = Pkg.VersionList(); I.end() == false; I++)
  1279. {
  1280. if (Pkg.CurrentVer() == I)
  1281. return I;
  1282. for (VerFileIterator J = I.FileList(); J.end() == false; J++)
  1283. {
  1284. if ((J.File()->Flags & Flag::NotSource) != 0)
  1285. continue;
  1286. /* Stash the highest version of a not-automatic source, we use it
  1287. if there is nothing better */
  1288. if ((J.File()->Flags & Flag::NotAutomatic) != 0)
  1289. {
  1290. if (Last.end() == true)
  1291. Last = I;
  1292. continue;
  1293. }
  1294. return I;
  1295. }
  1296. }
  1297. return Last;
  1298. }
  1299. /*}}}*/
  1300. // Policy::IsImportantDep - True if the dependency is important /*{{{*/
  1301. // ---------------------------------------------------------------------
  1302. /* */
  1303. bool pkgDepCache::Policy::IsImportantDep(DepIterator Dep)
  1304. {
  1305. if(Dep.IsCritical())
  1306. return true;
  1307. else if(Dep->Type == pkgCache::Dep::Recommends)
  1308. {
  1309. if ( _config->FindB("APT::Install-Recommends", false))
  1310. return true;
  1311. // we suport a special mode to only install-recommends for certain
  1312. // sections
  1313. // FIXME: this is a meant as a temporarly solution until the
  1314. // recommends are cleaned up
  1315. const char *sec = Dep.ParentVer().Section();
  1316. if (sec && ConfigValueInSubTree("APT::Install-Recommends-Sections", sec))
  1317. return true;
  1318. }
  1319. else if(Dep->Type == pkgCache::Dep::Suggests)
  1320. return _config->FindB("APT::Install-Suggests", false);
  1321. return false;
  1322. }
  1323. /*}}}*/
  1324. pkgDepCache::DefaultRootSetFunc::DefaultRootSetFunc() /*{{{*/
  1325. : constructedSuccessfully(false)
  1326. {
  1327. Configuration::Item const *Opts;
  1328. Opts = _config->Tree("APT::NeverAutoRemove");
  1329. if (Opts != 0 && Opts->Child != 0)
  1330. {
  1331. Opts = Opts->Child;
  1332. for (; Opts != 0; Opts = Opts->Next)
  1333. {
  1334. if (Opts->Value.empty() == true)
  1335. continue;
  1336. regex_t *p = new regex_t;
  1337. if(regcomp(p,Opts->Value.c_str(),
  1338. REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
  1339. {
  1340. regfree(p);
  1341. delete p;
  1342. _error->Error("Regex compilation error for APT::NeverAutoRemove");
  1343. return;
  1344. }
  1345. rootSetRegexp.push_back(p);
  1346. }
  1347. }
  1348. constructedSuccessfully = true;
  1349. }
  1350. /*}}}*/
  1351. pkgDepCache::DefaultRootSetFunc::~DefaultRootSetFunc() /*{{{*/
  1352. {
  1353. for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
  1354. {
  1355. regfree(rootSetRegexp[i]);
  1356. delete rootSetRegexp[i];
  1357. }
  1358. }
  1359. /*}}}*/
  1360. bool pkgDepCache::DefaultRootSetFunc::InRootSet(const pkgCache::PkgIterator &pkg) /*{{{*/
  1361. {
  1362. for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
  1363. if (regexec(rootSetRegexp[i], pkg.Name(), 0, 0, 0) == 0)
  1364. return true;
  1365. return false;
  1366. }
  1367. /*}}}*/
  1368. pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc() /*{{{*/
  1369. {
  1370. DefaultRootSetFunc *f = new DefaultRootSetFunc;
  1371. if(f->wasConstructedSuccessfully())
  1372. return f;
  1373. else
  1374. {
  1375. delete f;
  1376. return NULL;
  1377. }
  1378. }
  1379. /*}}}*/
  1380. bool pkgDepCache::MarkFollowsRecommends()
  1381. {
  1382. return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
  1383. }
  1384. bool pkgDepCache::MarkFollowsSuggests()
  1385. {
  1386. return _config->FindB("APT::AutoRemove::SuggestsImportant", false);
  1387. }
  1388. // pkgDepCache::MarkRequired - the main mark algorithm /*{{{*/
  1389. bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
  1390. {
  1391. bool follow_recommends;
  1392. bool follow_suggests;
  1393. bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  1394. // init the states
  1395. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1396. {
  1397. PkgState[p->ID].Marked = false;
  1398. PkgState[p->ID].Garbage = false;
  1399. // debug output
  1400. if(debug_autoremove && PkgState[p->ID].Flags & Flag::Auto)
  1401. std::clog << "AutoDep: " << p.FullName() << std::endl;
  1402. }
  1403. // init vars
  1404. follow_recommends = MarkFollowsRecommends();
  1405. follow_suggests = MarkFollowsSuggests();
  1406. // do the mark part, this is the core bit of the algorithm
  1407. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1408. {
  1409. if(!(PkgState[p->ID].Flags & Flag::Auto) ||
  1410. (p->Flags & Flag::Essential) ||
  1411. userFunc.InRootSet(p))
  1412. {
  1413. // the package is installed (and set to keep)
  1414. if(PkgState[p->ID].Keep() && !p.CurrentVer().end())
  1415. MarkPackage(p, p.CurrentVer(),
  1416. follow_recommends, follow_suggests);
  1417. // the package is to be installed
  1418. else if(PkgState[p->ID].Install())
  1419. MarkPackage(p, PkgState[p->ID].InstVerIter(*this),
  1420. follow_recommends, follow_suggests);
  1421. }
  1422. }
  1423. return true;
  1424. }
  1425. /*}}}*/
  1426. // MarkPackage - mark a single package in Mark-and-Sweep /*{{{*/
  1427. void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
  1428. const pkgCache::VerIterator &ver,
  1429. bool const &follow_recommends,
  1430. bool const &follow_suggests)
  1431. {
  1432. pkgDepCache::StateCache &state = PkgState[pkg->ID];
  1433. // if we are marked already we are done
  1434. if(state.Marked)
  1435. return;
  1436. VerIterator const currver = pkg.CurrentVer();
  1437. VerIterator const candver = state.CandidateVerIter(*this);
  1438. VerIterator const instver = state.InstVerIter(*this);
  1439. #if 0
  1440. // If a package was garbage-collected but is now being marked, we
  1441. // should re-select it
  1442. // For cases when a pkg is set to upgrade and this trigger the
  1443. // removal of a no-longer used dependency. if the pkg is set to
  1444. // keep again later it will result in broken deps
  1445. if(state.Delete() && state.RemoveReason = Unused)
  1446. {
  1447. if(ver==candver)
  1448. mark_install(pkg, false, false, NULL);
  1449. else if(ver==pkg.CurrentVer())
  1450. MarkKeep(pkg, false, false);
  1451. instver=state.InstVerIter(*this);
  1452. }
  1453. #endif
  1454. // For packages that are not going to be removed, ignore versions
  1455. // other than the InstVer. For packages that are going to be
  1456. // removed, ignore versions other than the current version.
  1457. if(!(ver == instver && !instver.end()) &&
  1458. !(ver == currver && instver.end() && !ver.end()))
  1459. return;
  1460. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove", false);
  1461. if(debug_autoremove)
  1462. {
  1463. std::clog << "Marking: " << pkg.FullName();
  1464. if(!ver.end())
  1465. std::clog << " " << ver.VerStr();
  1466. if(!currver.end())
  1467. std::clog << ", Curr=" << currver.VerStr();
  1468. if(!instver.end())
  1469. std::clog << ", Inst=" << instver.VerStr();
  1470. std::clog << std::endl;
  1471. }
  1472. state.Marked=true;
  1473. if(ver.end() == true)
  1474. return;
  1475. // If the version belongs to a Multi-Arch all package
  1476. // we will mark all others in this Group with this version also
  1477. // Beware: We compare versions here the lazy way: string comparision
  1478. // this is bad if multiple repositories provide different versions
  1479. // of the package with an identical version number - but even in this
  1480. // case the dependencies are likely the same.
  1481. if (ver->MultiArch == pkgCache::Version::All &&
  1482. strcmp(ver.Arch(true), "all") == 0)
  1483. {
  1484. GrpIterator G = pkg.Group();
  1485. const char* const VerStr = ver.VerStr();
  1486. for (PkgIterator P = G.FindPkg("any");
  1487. P.end() != true; P = G.NextPkg(P))
  1488. {
  1489. for (VerIterator V = P.VersionList();
  1490. V.end() != true; ++V)
  1491. {
  1492. if (strcmp(VerStr, V.VerStr()) != 0)
  1493. continue;
  1494. MarkPackage(P, V, follow_recommends, follow_suggests);
  1495. break;
  1496. }
  1497. }
  1498. }
  1499. for(DepIterator d = ver.DependsList(); !d.end(); ++d)
  1500. {
  1501. if(d->Type == Dep::Depends ||
  1502. d->Type == Dep::PreDepends ||
  1503. (follow_recommends &&
  1504. d->Type == Dep::Recommends) ||
  1505. (follow_suggests &&
  1506. d->Type == Dep::Suggests))
  1507. {
  1508. // Try all versions of this package.
  1509. for(VerIterator V = d.TargetPkg().VersionList();
  1510. !V.end(); ++V)
  1511. {
  1512. if(_system->VS->CheckDep(V.VerStr(), d->CompareOp, d.TargetVer()))
  1513. {
  1514. if(debug_autoremove)
  1515. {
  1516. std::clog << "Following dep: " << d.ParentPkg().FullName()
  1517. << " " << d.ParentVer().VerStr() << " "
  1518. << d.DepType() << " " << d.TargetPkg().FullName();
  1519. if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
  1520. {
  1521. std::clog << " (" << d.CompType() << " "
  1522. << d.TargetVer() << ")";
  1523. }
  1524. std::clog << std::endl;
  1525. }
  1526. MarkPackage(V.ParentPkg(), V,
  1527. follow_recommends, follow_suggests);
  1528. }
  1529. }
  1530. // Now try virtual packages
  1531. for(PrvIterator prv=d.TargetPkg().ProvidesList();
  1532. !prv.end(); ++prv)
  1533. {
  1534. if(_system->VS->CheckDep(prv.ProvideVersion(), d->CompareOp,
  1535. d.TargetVer()))
  1536. {
  1537. if(debug_autoremove)
  1538. {
  1539. std::clog << "Following dep: " << d.ParentPkg().FullName() << " "
  1540. << d.ParentVer().VerStr() << " "
  1541. << d.DepType() << " " << d.TargetPkg().FullName() << " ";
  1542. if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
  1543. {
  1544. std::clog << " (" << d.CompType() << " "
  1545. << d.TargetVer() << ")";
  1546. }
  1547. std::clog << ", provided by "
  1548. << prv.OwnerPkg().FullName() << " "
  1549. << prv.OwnerVer().VerStr()
  1550. << std::endl;
  1551. }
  1552. MarkPackage(prv.OwnerPkg(), prv.OwnerVer(),
  1553. follow_recommends, follow_suggests);
  1554. }
  1555. }
  1556. }
  1557. }
  1558. }
  1559. /*}}}*/
  1560. bool pkgDepCache::Sweep() /*{{{*/
  1561. {
  1562. bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  1563. // do the sweep
  1564. for(PkgIterator p=PkgBegin(); !p.end(); ++p)
  1565. {
  1566. StateCache &state=PkgState[p->ID];
  1567. // skip required packages
  1568. if (!p.CurrentVer().end() &&
  1569. (p.CurrentVer()->Priority == pkgCache::State::Required))
  1570. continue;
  1571. // if it is not marked and it is installed, it's garbage
  1572. if(!state.Marked && (!p.CurrentVer().end() || state.Install()))
  1573. {
  1574. state.Garbage=true;
  1575. if(debug_autoremove)
  1576. std::cout << "Garbage: " << p.FullName() << std::endl;
  1577. }
  1578. }
  1579. return true;
  1580. }
  1581. /*}}}*/