depcache.cc 44 KB

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