depcache.cc 48 KB

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