depcache.cc 65 KB

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