depcache.cc 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  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. unsigned char const DepState = VersionState(V.DependsList(),DepInstall,DepInstMin,DepInstPolicy);
  563. if ((DepState & DepInstMin) == DepInstMin)
  564. return false;
  565. // Dependencies for this arch all are not statisfied
  566. // so we installed it only for our convenience: get right of it now.
  567. RemoveSizes(Pkg);
  568. RemoveStates(Pkg);
  569. Pkg->CurrentVer = 0;
  570. PkgState[Pkg->ID].InstallVer = 0;
  571. AddStates(Pkg);
  572. Update(Pkg);
  573. AddSizes(Pkg);
  574. // After the remove previously satisfied pseudo pkg could be now
  575. // no longer satisfied, so we need to recheck the reverse dependencies
  576. for (DepIterator d = Pkg.RevDependsList(); d.end() != true; ++d)
  577. {
  578. PkgIterator const P = d.ParentPkg();
  579. if (P->CurrentVer != 0)
  580. recheck.insert(P.Index());
  581. }
  582. if (V.end() != true)
  583. for (PrvIterator Prv = V.ProvidesList(); Prv.end() != true; Prv++)
  584. for (DepIterator d = Prv.ParentPkg().RevDependsList();
  585. d.end() != true; ++d)
  586. {
  587. PkgIterator const P = d.ParentPkg();
  588. if (P->CurrentVer != 0)
  589. recheck.insert(P.Index());
  590. }
  591. return true;
  592. }
  593. /*}}}*/
  594. // DepCache::Update - Figure out all the state information /*{{{*/
  595. // ---------------------------------------------------------------------
  596. /* This will figure out the state of all the packages and all the
  597. dependencies based on the current policy. */
  598. void pkgDepCache::Update(OpProgress *Prog)
  599. {
  600. iUsrSize = 0;
  601. iDownloadSize = 0;
  602. iDelCount = 0;
  603. iInstCount = 0;
  604. iKeepCount = 0;
  605. iBrokenCount = 0;
  606. iBadCount = 0;
  607. std::set<unsigned long> recheck;
  608. // Perform the depends pass
  609. int Done = 0;
  610. bool const checkMultiArch = APT::Configuration::getArchitectures().size() > 1;
  611. unsigned long killed = 0;
  612. for (PkgIterator I = PkgBegin(); I.end() != true; I++,Done++)
  613. {
  614. if (Prog != 0 && Done%20 == 0)
  615. Prog->Progress(Done);
  616. for (VerIterator V = I.VersionList(); V.end() != true; V++)
  617. {
  618. unsigned char Group = 0;
  619. for (DepIterator D = V.DependsList(); D.end() != true; D++)
  620. {
  621. // Build the dependency state.
  622. unsigned char &State = DepState[D->ID];
  623. State = DependencyState(D);
  624. // Add to the group if we are within an or..
  625. Group |= State;
  626. State |= Group << 3;
  627. if ((D->CompareOp & Dep::Or) != Dep::Or)
  628. Group = 0;
  629. // Invert for Conflicts
  630. if (D->Type == Dep::Conflicts ||
  631. D->Type == Dep::DpkgBreaks ||
  632. D->Type == Dep::Obsoletes)
  633. State = ~State;
  634. }
  635. }
  636. // Compute the package dependency state and size additions
  637. AddSizes(I);
  638. UpdateVerState(I);
  639. AddStates(I);
  640. if (checkMultiArch != true || I->CurrentVer == 0)
  641. continue;
  642. VerIterator const V = I.CurrentVer();
  643. if (V->MultiArch != Version::All)
  644. continue;
  645. recheck.insert(I.Index());
  646. --Done; // no progress if we need to recheck the package
  647. }
  648. if (checkMultiArch == true) {
  649. /* FIXME: recheck breaks proper progress reporting as we don't know
  650. how many packages we need to recheck. To lower the effect
  651. a bit we increase with a kill, but we should do something more clever… */
  652. for(std::set<unsigned long>::const_iterator p = recheck.begin();
  653. p != recheck.end(); ++p) {
  654. if (Prog != 0 && Done%20 == 0)
  655. Prog->Progress(Done);
  656. PkgIterator P = PkgIterator(*Cache, Cache->PkgP + *p);
  657. if (RemovePseudoInstalledPkg(P, recheck) == true) {
  658. ++killed;
  659. ++Done;
  660. }
  661. recheck.erase(p);
  662. }
  663. }
  664. if (Prog != 0)
  665. Prog->Progress(Done);
  666. readStateFile(Prog);
  667. }
  668. /*}}}*/
  669. // DepCache::Update - Update the deps list of a package /*{{{*/
  670. // ---------------------------------------------------------------------
  671. /* This is a helper for update that only does the dep portion of the scan.
  672. It is mainly meant to scan reverse dependencies. */
  673. void pkgDepCache::Update(DepIterator D)
  674. {
  675. // Update the reverse deps
  676. for (;D.end() != true; D++)
  677. {
  678. unsigned char &State = DepState[D->ID];
  679. State = DependencyState(D);
  680. // Invert for Conflicts
  681. if (D->Type == Dep::Conflicts ||
  682. D->Type == Dep::DpkgBreaks ||
  683. D->Type == Dep::Obsoletes)
  684. State = ~State;
  685. RemoveStates(D.ParentPkg());
  686. BuildGroupOrs(D.ParentVer());
  687. UpdateVerState(D.ParentPkg());
  688. AddStates(D.ParentPkg());
  689. }
  690. }
  691. /*}}}*/
  692. // DepCache::Update - Update the related deps of a package /*{{{*/
  693. // ---------------------------------------------------------------------
  694. /* This is called whenever the state of a package changes. It updates
  695. all cached dependencies related to this package. */
  696. void pkgDepCache::Update(PkgIterator const &Pkg)
  697. {
  698. // Recompute the dep of the package
  699. RemoveStates(Pkg);
  700. UpdateVerState(Pkg);
  701. AddStates(Pkg);
  702. // Update the reverse deps
  703. Update(Pkg.RevDependsList());
  704. // Update the provides map for the current ver
  705. if (Pkg->CurrentVer != 0)
  706. for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
  707. P.end() != true; P++)
  708. Update(P.ParentPkg().RevDependsList());
  709. // Update the provides map for the candidate ver
  710. if (PkgState[Pkg->ID].CandidateVer != 0)
  711. for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
  712. P.end() != true; P++)
  713. Update(P.ParentPkg().RevDependsList());
  714. }
  715. /*}}}*/
  716. // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
  717. // ---------------------------------------------------------------------
  718. /* */
  719. void pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser,
  720. unsigned long Depth)
  721. {
  722. // Simplifies other routines.
  723. if (Pkg.end() == true)
  724. return;
  725. /* Reject an attempt to keep a non-source broken installed package, those
  726. must be upgraded */
  727. if (Pkg.State() == PkgIterator::NeedsUnpack &&
  728. Pkg.CurrentVer().Downloadable() == false)
  729. return;
  730. /** \todo Can this be moved later in the method? */
  731. ActionGroup group(*this);
  732. /* We changed the soft state all the time so the UI is a bit nicer
  733. to use */
  734. StateCache &P = PkgState[Pkg->ID];
  735. if (Soft == true)
  736. P.iFlags |= AutoKept;
  737. else
  738. P.iFlags &= ~AutoKept;
  739. // Check that it is not already kept
  740. if (P.Mode == ModeKeep)
  741. return;
  742. // We dont even try to keep virtual packages..
  743. if (Pkg->VersionList == 0)
  744. return;
  745. #if 0 // reseting the autoflag here means we lose the
  746. // auto-mark information if a user selects a package for removal
  747. // but changes his mind then and sets it for keep again
  748. // - this makes sense as default when all Garbage dependencies
  749. // are automatically marked for removal (as aptitude does).
  750. // setting a package for keep then makes it no longer autoinstalled
  751. // for all other use-case this action is rather suprising
  752. if(FromUser && !P.Marked)
  753. P.Flags &= ~Flag::Auto;
  754. #endif
  755. if (DebugMarker == true)
  756. std::clog << OutputInDepth(Depth) << "MarkKeep " << Pkg << " FU=" << FromUser << std::endl;
  757. RemoveSizes(Pkg);
  758. RemoveStates(Pkg);
  759. P.Mode = ModeKeep;
  760. if (Pkg->CurrentVer == 0)
  761. P.InstallVer = 0;
  762. else
  763. P.InstallVer = Pkg.CurrentVer();
  764. AddStates(Pkg);
  765. Update(Pkg);
  766. AddSizes(Pkg);
  767. }
  768. /*}}}*/
  769. // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
  770. // ---------------------------------------------------------------------
  771. /* */
  772. void pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge,
  773. unsigned long Depth, bool FromUser)
  774. {
  775. // Simplifies other routines.
  776. if (Pkg.end() == true)
  777. return;
  778. ActionGroup group(*this);
  779. // Check that it is not already marked for delete
  780. StateCache &P = PkgState[Pkg->ID];
  781. P.iFlags &= ~(AutoKept | Purge);
  782. if (rPurge == true)
  783. P.iFlags |= Purge;
  784. if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
  785. (Pkg.Purge() == true || rPurge == false))
  786. return;
  787. // We dont even try to delete virtual packages..
  788. if (Pkg->VersionList == 0)
  789. return;
  790. // check if we are allowed to install the package
  791. if (IsDeleteOk(Pkg,rPurge,Depth,FromUser) == false)
  792. return;
  793. if (DebugMarker == true)
  794. std::clog << OutputInDepth(Depth) << "MarkDelete " << Pkg << " FU=" << FromUser << std::endl;
  795. RemoveSizes(Pkg);
  796. RemoveStates(Pkg);
  797. if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
  798. P.Mode = ModeKeep;
  799. else
  800. P.Mode = ModeDelete;
  801. P.InstallVer = 0;
  802. AddStates(Pkg);
  803. Update(Pkg);
  804. AddSizes(Pkg);
  805. // if we remove the pseudo package, we also need to remove the "real"
  806. if (Pkg->CurrentVer != 0 && Pkg.CurrentVer().Pseudo() == true)
  807. MarkDelete(Pkg.Group().FindPkg("all"), rPurge, Depth+1, FromUser);
  808. }
  809. /*}}}*/
  810. // DepCache::IsDeleteOk - check if it is ok to remove this package /*{{{*/
  811. // ---------------------------------------------------------------------
  812. /* The default implementation just honors dpkg hold
  813. But an application using this library can override this method
  814. to control the MarkDelete behaviour */
  815. bool pkgDepCache::IsDeleteOk(PkgIterator const &Pkg,bool rPurge,
  816. unsigned long Depth, bool FromUser)
  817. {
  818. if (FromUser == false && Pkg->SelectedState == pkgCache::State::Hold && _config->FindB("APT::Ignore-Hold",false) == false)
  819. {
  820. if (DebugMarker == true)
  821. std::clog << OutputInDepth(Depth) << "Hold prevents MarkDelete of " << Pkg << " FU=" << FromUser << std::endl;
  822. return false;
  823. }
  824. return true;
  825. }
  826. /*}}}*/
  827. // DepCache::MarkInstall - Put the package in the install state /*{{{*/
  828. // ---------------------------------------------------------------------
  829. /* */
  830. void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
  831. unsigned long Depth, bool FromUser,
  832. bool ForceImportantDeps)
  833. {
  834. if (Depth > 100)
  835. return;
  836. // Simplifies other routines.
  837. if (Pkg.end() == true)
  838. return;
  839. ActionGroup group(*this);
  840. /* Check that it is not already marked for install and that it can be
  841. installed */
  842. StateCache &P = PkgState[Pkg->ID];
  843. P.iFlags &= ~AutoKept;
  844. if ((P.InstPolicyBroken() == false && P.InstBroken() == false) &&
  845. (P.Mode == ModeInstall ||
  846. P.CandidateVer == (Version *)Pkg.CurrentVer()))
  847. {
  848. if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
  849. MarkKeep(Pkg, false, FromUser, Depth+1);
  850. return;
  851. }
  852. // See if there is even any possible instalation candidate
  853. if (P.CandidateVer == 0)
  854. return;
  855. // We dont even try to install virtual packages..
  856. if (Pkg->VersionList == 0)
  857. return;
  858. // check if we are allowed to install the package
  859. if (IsInstallOk(Pkg,AutoInst,Depth,FromUser) == false)
  860. return;
  861. /* Target the candidate version and remove the autoflag. We reset the
  862. autoflag below if this was called recursively. Otherwise the user
  863. should have the ability to de-auto a package by changing its state */
  864. RemoveSizes(Pkg);
  865. RemoveStates(Pkg);
  866. P.Mode = ModeInstall;
  867. P.InstallVer = P.CandidateVer;
  868. if(FromUser)
  869. {
  870. // Set it to manual if it's a new install or cancelling the
  871. // removal of a garbage package.
  872. if(P.Status == 2 || (!Pkg.CurrentVer().end() && !P.Marked))
  873. P.Flags &= ~Flag::Auto;
  874. }
  875. else
  876. {
  877. // Set it to auto if this is a new install.
  878. if(P.Status == 2)
  879. P.Flags |= Flag::Auto;
  880. }
  881. if (P.CandidateVer == (Version *)Pkg.CurrentVer())
  882. P.Mode = ModeKeep;
  883. AddStates(Pkg);
  884. Update(Pkg);
  885. AddSizes(Pkg);
  886. if (AutoInst == false)
  887. return;
  888. if (DebugMarker == true)
  889. std::clog << OutputInDepth(Depth) << "MarkInstall " << Pkg << " FU=" << FromUser << std::endl;
  890. DepIterator Dep = P.InstVerIter(*this).DependsList();
  891. for (; Dep.end() != true;)
  892. {
  893. // Grok or groups
  894. DepIterator Start = Dep;
  895. bool Result = true;
  896. unsigned Ors = 0;
  897. for (bool LastOR = true; Dep.end() == false && LastOR == true; Dep++,Ors++)
  898. {
  899. LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
  900. if ((DepState[Dep->ID] & DepInstall) == DepInstall)
  901. Result = false;
  902. }
  903. // Dep is satisfied okay.
  904. if (Result == false)
  905. continue;
  906. /* Check if this dep should be consider for install. If it is a user
  907. defined important dep and we are installed a new package then
  908. it will be installed. Otherwise we only check for important
  909. deps that have changed from the installed version
  910. */
  911. if (IsImportantDep(Start) == false)
  912. continue;
  913. /* Check if any ImportantDep() (but not Critical) were added
  914. * since we installed the package. Also check for deps that
  915. * were satisfied in the past: for instance, if a version
  916. * restriction in a Recommends was tightened, upgrading the
  917. * package should follow that Recommends rather than causing the
  918. * dependency to be removed. (bug #470115)
  919. */
  920. bool isNewImportantDep = false;
  921. bool isPreviouslySatisfiedImportantDep = false;
  922. if(!ForceImportantDeps && !Start.IsCritical())
  923. {
  924. bool found=false;
  925. VerIterator instVer = Pkg.CurrentVer();
  926. if(!instVer.end())
  927. {
  928. for (DepIterator D = instVer.DependsList(); D.end() != true; D++)
  929. {
  930. //FIXME: deal better with or-groups(?)
  931. DepIterator LocalStart = D;
  932. if(IsImportantDep(D) && !D.IsCritical() &&
  933. Start.TargetPkg() == D.TargetPkg())
  934. {
  935. if(!isPreviouslySatisfiedImportantDep)
  936. {
  937. DepIterator D2 = D;
  938. while((D2->CompareOp & Dep::Or) != 0)
  939. ++D2;
  940. isPreviouslySatisfiedImportantDep =
  941. (((*this)[D2] & DepGNow) != 0);
  942. }
  943. found=true;
  944. }
  945. }
  946. // this is a new dep if it was not found to be already
  947. // a important dep of the installed pacakge
  948. isNewImportantDep = !found;
  949. }
  950. }
  951. if(isNewImportantDep)
  952. if(DebugAutoInstall == true)
  953. std::clog << OutputInDepth(Depth) << "new important dependency: "
  954. << Start.TargetPkg().Name() << std::endl;
  955. if(isPreviouslySatisfiedImportantDep)
  956. if(DebugAutoInstall == true)
  957. std::clog << OutputInDepth(Depth) << "previously satisfied important dependency on "
  958. << Start.TargetPkg().Name() << std::endl;
  959. // skip important deps if the package is already installed
  960. if (Pkg->CurrentVer != 0 && Start.IsCritical() == false
  961. && !isNewImportantDep && !isPreviouslySatisfiedImportantDep
  962. && !ForceImportantDeps)
  963. continue;
  964. /* If we are in an or group locate the first or that can
  965. succeed. We have already cached this.. */
  966. for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; Ors--)
  967. Start++;
  968. /* This bit is for processing the possibilty of an install/upgrade
  969. fixing the problem */
  970. SPtrArray<Version *> List = Start.AllTargets();
  971. if (Start->Type != Dep::DpkgBreaks &&
  972. (DepState[Start->ID] & DepCVer) == DepCVer)
  973. {
  974. // Right, find the best version to install..
  975. Version **Cur = List;
  976. PkgIterator P = Start.TargetPkg();
  977. PkgIterator InstPkg(*Cache,0);
  978. // See if there are direct matches (at the start of the list)
  979. for (; *Cur != 0 && (*Cur)->ParentPkg == P.Index(); Cur++)
  980. {
  981. PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
  982. if (PkgState[Pkg->ID].CandidateVer != *Cur)
  983. continue;
  984. InstPkg = Pkg;
  985. break;
  986. }
  987. // Select the highest priority providing package
  988. if (InstPkg.end() == true)
  989. {
  990. pkgPrioSortList(*Cache,Cur);
  991. for (; *Cur != 0; Cur++)
  992. {
  993. PkgIterator Pkg(*Cache,Cache->PkgP + (*Cur)->ParentPkg);
  994. if (PkgState[Pkg->ID].CandidateVer != *Cur)
  995. continue;
  996. InstPkg = Pkg;
  997. break;
  998. }
  999. }
  1000. if (InstPkg.end() == false)
  1001. {
  1002. if(DebugAutoInstall == true)
  1003. std::clog << OutputInDepth(Depth) << "Installing " << InstPkg.Name()
  1004. << " as " << Start.DepType() << " of " << Pkg.Name()
  1005. << std::endl;
  1006. // now check if we should consider it a automatic dependency or not
  1007. if(Pkg.Section() && ConfigValueInSubTree("APT::Never-MarkAuto-Sections", Pkg.Section()))
  1008. {
  1009. if(DebugAutoInstall == true)
  1010. std::clog << OutputInDepth(Depth) << "Setting NOT as auto-installed (direct "
  1011. << Start.DepType() << " of pkg in APT::Never-MarkAuto-Sections)" << std::endl;
  1012. MarkInstall(InstPkg,true,Depth + 1, true);
  1013. }
  1014. else
  1015. {
  1016. // mark automatic dependency
  1017. MarkInstall(InstPkg,true,Depth + 1, false, ForceImportantDeps);
  1018. // Set the autoflag, after MarkInstall because MarkInstall unsets it
  1019. if (P->CurrentVer == 0)
  1020. PkgState[InstPkg->ID].Flags |= Flag::Auto;
  1021. }
  1022. }
  1023. continue;
  1024. }
  1025. /* For conflicts we just de-install the package and mark as auto,
  1026. Conflicts may not have or groups. For dpkg's Breaks we try to
  1027. upgrade the package. */
  1028. if (Start->Type == Dep::Conflicts || Start->Type == Dep::Obsoletes ||
  1029. Start->Type == Dep::DpkgBreaks)
  1030. {
  1031. for (Version **I = List; *I != 0; I++)
  1032. {
  1033. VerIterator Ver(*this,*I);
  1034. PkgIterator Pkg = Ver.ParentPkg();
  1035. if (Start->Type != Dep::DpkgBreaks)
  1036. MarkDelete(Pkg,false,Depth + 1, false);
  1037. else if (PkgState[Pkg->ID].CandidateVer != *I)
  1038. MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps);
  1039. }
  1040. continue;
  1041. }
  1042. }
  1043. }
  1044. /*}}}*/
  1045. // DepCache::IsInstallOk - check if it is ok to install this package /*{{{*/
  1046. // ---------------------------------------------------------------------
  1047. /* The default implementation just honors dpkg hold
  1048. But an application using this library can override this method
  1049. to control the MarkInstall behaviour */
  1050. bool pkgDepCache::IsInstallOk(PkgIterator const &Pkg,bool AutoInst,
  1051. unsigned long Depth, bool FromUser)
  1052. {
  1053. if (FromUser == false && Pkg->SelectedState == pkgCache::State::Hold && _config->FindB("APT::Ignore-Hold",false) == false)
  1054. {
  1055. if (DebugMarker == true)
  1056. std::clog << OutputInDepth(Depth) << "Hold prevents MarkInstall of " << Pkg << " FU=" << FromUser << std::endl;
  1057. return false;
  1058. }
  1059. return true;
  1060. }
  1061. /*}}}*/
  1062. // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
  1063. // ---------------------------------------------------------------------
  1064. /* */
  1065. void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
  1066. {
  1067. ActionGroup group(*this);
  1068. RemoveSizes(Pkg);
  1069. RemoveStates(Pkg);
  1070. StateCache &P = PkgState[Pkg->ID];
  1071. if (To == true)
  1072. P.iFlags |= ReInstall;
  1073. else
  1074. P.iFlags &= ~ReInstall;
  1075. AddStates(Pkg);
  1076. AddSizes(Pkg);
  1077. }
  1078. /*}}}*/
  1079. // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
  1080. // ---------------------------------------------------------------------
  1081. /* */
  1082. void pkgDepCache::SetCandidateVersion(VerIterator TargetVer)
  1083. {
  1084. ActionGroup group(*this);
  1085. pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
  1086. StateCache &P = PkgState[Pkg->ID];
  1087. RemoveSizes(Pkg);
  1088. RemoveStates(Pkg);
  1089. if (P.CandidateVer == P.InstallVer)
  1090. P.InstallVer = (Version *)TargetVer;
  1091. P.CandidateVer = (Version *)TargetVer;
  1092. P.Update(Pkg,*this);
  1093. AddStates(Pkg);
  1094. Update(Pkg);
  1095. AddSizes(Pkg);
  1096. }
  1097. void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
  1098. {
  1099. StateCache &state = PkgState[Pkg->ID];
  1100. ActionGroup group(*this);
  1101. if(Auto)
  1102. state.Flags |= Flag::Auto;
  1103. else
  1104. state.Flags &= ~Flag::Auto;
  1105. }
  1106. /*}}}*/
  1107. // StateCache::Update - Compute the various static display things /*{{{*/
  1108. // ---------------------------------------------------------------------
  1109. /* This is called whenever the Candidate version changes. */
  1110. void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
  1111. {
  1112. // Some info
  1113. VerIterator Ver = CandidateVerIter(Cache);
  1114. // Use a null string or the version string
  1115. if (Ver.end() == true)
  1116. CandVersion = "";
  1117. else
  1118. CandVersion = Ver.VerStr();
  1119. // Find the current version
  1120. CurVersion = "";
  1121. if (Pkg->CurrentVer != 0)
  1122. CurVersion = Pkg.CurrentVer().VerStr();
  1123. // Strip off the epochs for display
  1124. CurVersion = StripEpoch(CurVersion);
  1125. CandVersion = StripEpoch(CandVersion);
  1126. // Figure out if its up or down or equal
  1127. Status = Ver.CompareVer(Pkg.CurrentVer());
  1128. if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
  1129. Status = 2;
  1130. }
  1131. /*}}}*/
  1132. // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
  1133. // ---------------------------------------------------------------------
  1134. /* */
  1135. const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
  1136. {
  1137. if (Ver == 0)
  1138. return 0;
  1139. // Strip any epoch
  1140. for (const char *I = Ver; *I != 0; I++)
  1141. if (*I == ':')
  1142. return I + 1;
  1143. return Ver;
  1144. }
  1145. /*}}}*/
  1146. // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
  1147. // ---------------------------------------------------------------------
  1148. /* The default just returns the highest available version that is not
  1149. a source and automatic. */
  1150. pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator Pkg)
  1151. {
  1152. /* Not source/not automatic versions cannot be a candidate version
  1153. unless they are already installed */
  1154. VerIterator Last(*(pkgCache *)this,0);
  1155. for (VerIterator I = Pkg.VersionList(); I.end() == false; I++)
  1156. {
  1157. if (Pkg.CurrentVer() == I)
  1158. return I;
  1159. for (VerFileIterator J = I.FileList(); J.end() == false; J++)
  1160. {
  1161. if ((J.File()->Flags & Flag::NotSource) != 0)
  1162. continue;
  1163. /* Stash the highest version of a not-automatic source, we use it
  1164. if there is nothing better */
  1165. if ((J.File()->Flags & Flag::NotAutomatic) != 0)
  1166. {
  1167. if (Last.end() == true)
  1168. Last = I;
  1169. continue;
  1170. }
  1171. return I;
  1172. }
  1173. }
  1174. return Last;
  1175. }
  1176. /*}}}*/
  1177. // Policy::IsImportantDep - True if the dependency is important /*{{{*/
  1178. // ---------------------------------------------------------------------
  1179. /* */
  1180. bool pkgDepCache::Policy::IsImportantDep(DepIterator Dep)
  1181. {
  1182. if(Dep.IsCritical())
  1183. return true;
  1184. else if(Dep->Type == pkgCache::Dep::Recommends)
  1185. {
  1186. if ( _config->FindB("APT::Install-Recommends", false))
  1187. return true;
  1188. // we suport a special mode to only install-recommends for certain
  1189. // sections
  1190. // FIXME: this is a meant as a temporarly solution until the
  1191. // recommends are cleaned up
  1192. const char *sec = Dep.ParentVer().Section();
  1193. if (sec && ConfigValueInSubTree("APT::Install-Recommends-Sections", sec))
  1194. return true;
  1195. }
  1196. else if(Dep->Type == pkgCache::Dep::Suggests)
  1197. return _config->FindB("APT::Install-Suggests", false);
  1198. return false;
  1199. }
  1200. /*}}}*/
  1201. pkgDepCache::DefaultRootSetFunc::DefaultRootSetFunc() /*{{{*/
  1202. : constructedSuccessfully(false)
  1203. {
  1204. Configuration::Item const *Opts;
  1205. Opts = _config->Tree("APT::NeverAutoRemove");
  1206. if (Opts != 0 && Opts->Child != 0)
  1207. {
  1208. Opts = Opts->Child;
  1209. for (; Opts != 0; Opts = Opts->Next)
  1210. {
  1211. if (Opts->Value.empty() == true)
  1212. continue;
  1213. regex_t *p = new regex_t;
  1214. if(regcomp(p,Opts->Value.c_str(),
  1215. REG_EXTENDED | REG_ICASE | REG_NOSUB) != 0)
  1216. {
  1217. regfree(p);
  1218. delete p;
  1219. _error->Error("Regex compilation error for APT::NeverAutoRemove");
  1220. return;
  1221. }
  1222. rootSetRegexp.push_back(p);
  1223. }
  1224. }
  1225. constructedSuccessfully = true;
  1226. }
  1227. /*}}}*/
  1228. pkgDepCache::DefaultRootSetFunc::~DefaultRootSetFunc() /*{{{*/
  1229. {
  1230. for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
  1231. {
  1232. regfree(rootSetRegexp[i]);
  1233. delete rootSetRegexp[i];
  1234. }
  1235. }
  1236. /*}}}*/
  1237. bool pkgDepCache::DefaultRootSetFunc::InRootSet(const pkgCache::PkgIterator &pkg) /*{{{*/
  1238. {
  1239. for(unsigned int i = 0; i < rootSetRegexp.size(); i++)
  1240. if (regexec(rootSetRegexp[i], pkg.Name(), 0, 0, 0) == 0)
  1241. return true;
  1242. return false;
  1243. }
  1244. /*}}}*/
  1245. pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc() /*{{{*/
  1246. {
  1247. DefaultRootSetFunc *f = new DefaultRootSetFunc;
  1248. if(f->wasConstructedSuccessfully())
  1249. return f;
  1250. else
  1251. {
  1252. delete f;
  1253. return NULL;
  1254. }
  1255. }
  1256. /*}}}*/
  1257. bool pkgDepCache::MarkFollowsRecommends()
  1258. {
  1259. return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
  1260. }
  1261. bool pkgDepCache::MarkFollowsSuggests()
  1262. {
  1263. return _config->FindB("APT::AutoRemove::SuggestsImportant", false);
  1264. }
  1265. // pkgDepCache::MarkRequired - the main mark algorithm /*{{{*/
  1266. bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
  1267. {
  1268. bool follow_recommends;
  1269. bool follow_suggests;
  1270. bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  1271. // init the states
  1272. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1273. {
  1274. PkgState[p->ID].Marked = false;
  1275. PkgState[p->ID].Garbage = false;
  1276. // debug output
  1277. if(debug_autoremove && PkgState[p->ID].Flags & Flag::Auto)
  1278. std::clog << "AutoDep: " << p.FullName() << std::endl;
  1279. }
  1280. // init vars
  1281. follow_recommends = MarkFollowsRecommends();
  1282. follow_suggests = MarkFollowsSuggests();
  1283. // do the mark part, this is the core bit of the algorithm
  1284. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1285. {
  1286. if(!(PkgState[p->ID].Flags & Flag::Auto) ||
  1287. (p->Flags & Flag::Essential) ||
  1288. userFunc.InRootSet(p))
  1289. {
  1290. // the package is installed (and set to keep)
  1291. if(PkgState[p->ID].Keep() && !p.CurrentVer().end())
  1292. MarkPackage(p, p.CurrentVer(),
  1293. follow_recommends, follow_suggests);
  1294. // the package is to be installed
  1295. else if(PkgState[p->ID].Install())
  1296. MarkPackage(p, PkgState[p->ID].InstVerIter(*this),
  1297. follow_recommends, follow_suggests);
  1298. }
  1299. }
  1300. return true;
  1301. }
  1302. /*}}}*/
  1303. // MarkPackage - mark a single package in Mark-and-Sweep /*{{{*/
  1304. void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
  1305. const pkgCache::VerIterator &ver,
  1306. bool const &follow_recommends,
  1307. bool const &follow_suggests)
  1308. {
  1309. pkgDepCache::StateCache &state = PkgState[pkg->ID];
  1310. // if we are marked already we are done
  1311. if(state.Marked)
  1312. return;
  1313. VerIterator const currver = pkg.CurrentVer();
  1314. VerIterator const candver = state.CandidateVerIter(*this);
  1315. VerIterator const instver = state.InstVerIter(*this);
  1316. #if 0
  1317. // If a package was garbage-collected but is now being marked, we
  1318. // should re-select it
  1319. // For cases when a pkg is set to upgrade and this trigger the
  1320. // removal of a no-longer used dependency. if the pkg is set to
  1321. // keep again later it will result in broken deps
  1322. if(state.Delete() && state.RemoveReason = Unused)
  1323. {
  1324. if(ver==candver)
  1325. mark_install(pkg, false, false, NULL);
  1326. else if(ver==pkg.CurrentVer())
  1327. MarkKeep(pkg, false, false);
  1328. instver=state.InstVerIter(*this);
  1329. }
  1330. #endif
  1331. // For packages that are not going to be removed, ignore versions
  1332. // other than the InstVer. For packages that are going to be
  1333. // removed, ignore versions other than the current version.
  1334. if(!(ver == instver && !instver.end()) &&
  1335. !(ver == currver && instver.end() && !ver.end()))
  1336. return;
  1337. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove", false);
  1338. if(debug_autoremove)
  1339. {
  1340. std::clog << "Marking: " << pkg.FullName();
  1341. if(!ver.end())
  1342. std::clog << " " << ver.VerStr();
  1343. if(!currver.end())
  1344. std::clog << ", Curr=" << currver.VerStr();
  1345. if(!instver.end())
  1346. std::clog << ", Inst=" << instver.VerStr();
  1347. std::clog << std::endl;
  1348. }
  1349. state.Marked=true;
  1350. if(ver.end() == true)
  1351. return;
  1352. // If the version belongs to a Multi-Arch all package
  1353. // we will mark all others in this Group with this version also
  1354. // Beware: We compare versions here the lazy way: string comparision
  1355. // this is bad if multiple repositories provide different versions
  1356. // of the package with an identical version number - but even in this
  1357. // case the dependencies are likely the same.
  1358. if (ver->MultiArch == pkgCache::Version::All &&
  1359. strcmp(ver.Arch(true), "all") == 0)
  1360. {
  1361. GrpIterator G = pkg.Group();
  1362. const char* const VerStr = ver.VerStr();
  1363. for (PkgIterator P = G.FindPkg("any");
  1364. P.end() != true; P = G.NextPkg(P))
  1365. {
  1366. for (VerIterator V = P.VersionList();
  1367. V.end() != true; ++V)
  1368. {
  1369. if (strcmp(VerStr, V.VerStr()) != 0)
  1370. continue;
  1371. MarkPackage(P, V, follow_recommends, follow_suggests);
  1372. break;
  1373. }
  1374. }
  1375. }
  1376. for(DepIterator d = ver.DependsList(); !d.end(); ++d)
  1377. {
  1378. if(d->Type == Dep::Depends ||
  1379. d->Type == Dep::PreDepends ||
  1380. (follow_recommends &&
  1381. d->Type == Dep::Recommends) ||
  1382. (follow_suggests &&
  1383. d->Type == Dep::Suggests))
  1384. {
  1385. // Try all versions of this package.
  1386. for(VerIterator V = d.TargetPkg().VersionList();
  1387. !V.end(); ++V)
  1388. {
  1389. if(_system->VS->CheckDep(V.VerStr(), d->CompareOp, d.TargetVer()))
  1390. {
  1391. if(debug_autoremove)
  1392. {
  1393. std::clog << "Following dep: " << d.ParentPkg().FullName()
  1394. << " " << d.ParentVer().VerStr() << " "
  1395. << d.DepType() << " " << d.TargetPkg().FullName();
  1396. if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
  1397. {
  1398. std::clog << " (" << d.CompType() << " "
  1399. << d.TargetVer() << ")";
  1400. }
  1401. std::clog << std::endl;
  1402. }
  1403. MarkPackage(V.ParentPkg(), V,
  1404. follow_recommends, follow_suggests);
  1405. }
  1406. }
  1407. // Now try virtual packages
  1408. for(PrvIterator prv=d.TargetPkg().ProvidesList();
  1409. !prv.end(); ++prv)
  1410. {
  1411. if(_system->VS->CheckDep(prv.ProvideVersion(), d->CompareOp,
  1412. d.TargetVer()))
  1413. {
  1414. if(debug_autoremove)
  1415. {
  1416. std::clog << "Following dep: " << d.ParentPkg().FullName() << " "
  1417. << d.ParentVer().VerStr() << " "
  1418. << d.DepType() << " " << d.TargetPkg().FullName() << " ";
  1419. if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
  1420. {
  1421. std::clog << " (" << d.CompType() << " "
  1422. << d.TargetVer() << ")";
  1423. }
  1424. std::clog << ", provided by "
  1425. << prv.OwnerPkg().FullName() << " "
  1426. << prv.OwnerVer().VerStr()
  1427. << std::endl;
  1428. }
  1429. MarkPackage(prv.OwnerPkg(), prv.OwnerVer(),
  1430. follow_recommends, follow_suggests);
  1431. }
  1432. }
  1433. }
  1434. }
  1435. }
  1436. /*}}}*/
  1437. bool pkgDepCache::Sweep() /*{{{*/
  1438. {
  1439. bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  1440. // do the sweep
  1441. for(PkgIterator p=PkgBegin(); !p.end(); ++p)
  1442. {
  1443. StateCache &state=PkgState[p->ID];
  1444. // skip required packages
  1445. if (!p.CurrentVer().end() &&
  1446. (p.CurrentVer()->Priority == pkgCache::State::Required))
  1447. continue;
  1448. // if it is not marked and it is installed, it's garbage
  1449. if(!state.Marked && (!p.CurrentVer().end() || state.Install()))
  1450. {
  1451. state.Garbage=true;
  1452. if(debug_autoremove)
  1453. std::cout << "Garbage: " << p.FullName() << std::endl;
  1454. }
  1455. }
  1456. return true;
  1457. }
  1458. /*}}}*/