depcache.cc 43 KB

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