depcache.cc 36 KB

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