depcache.cc 36 KB

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