depcache.cc 46 KB

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