depcache.cc 54 KB

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