depcache.cc 45 KB

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