You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2026 lines
65 KiB

  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<config.h>
  10. #include <apt-pkg/depcache.h>
  11. #include <apt-pkg/versionmatch.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/fileutl.h>
  14. #include <apt-pkg/strutl.h>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/aptconfiguration.h>
  17. #include <apt-pkg/tagfile.h>
  18. #include <apt-pkg/progress.h>
  19. #include <apt-pkg/cacheset.h>
  20. #include <apt-pkg/pkgcache.h>
  21. #include <apt-pkg/cacheiterators.h>
  22. #include <apt-pkg/cachefile.h>
  23. #include <apt-pkg/macros.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <list>
  27. #include <string>
  28. #include <utility>
  29. #include <vector>
  30. #include <algorithm>
  31. #include <iostream>
  32. #include <set>
  33. #include <sys/stat.h>
  34. #include <apti18n.h>
  35. /*}}}*/
  36. using std::string;
  37. // helper for Install-Recommends-Sections and Never-MarkAuto-Sections /*{{{*/
  38. static bool
  39. ConfigValueInSubTree(const char* SubTree, const char *needle)
  40. {
  41. Configuration::Item const *Opts;
  42. Opts = _config->Tree(SubTree);
  43. if (Opts != 0 && Opts->Child != 0)
  44. {
  45. Opts = Opts->Child;
  46. for (; Opts != 0; Opts = Opts->Next)
  47. {
  48. if (Opts->Value.empty() == true)
  49. continue;
  50. if (strcmp(needle, Opts->Value.c_str()) == 0)
  51. return true;
  52. }
  53. }
  54. return false;
  55. }
  56. /*}}}*/
  57. pkgDepCache::ActionGroup::ActionGroup(pkgDepCache &cache) : /*{{{*/
  58. d(NULL), cache(cache), released(false)
  59. {
  60. ++cache.group_level;
  61. }
  62. void pkgDepCache::ActionGroup::release()
  63. {
  64. if(!released)
  65. {
  66. if(cache.group_level == 0)
  67. std::cerr << "W: Unbalanced action groups, expect badness" << std::endl;
  68. else
  69. {
  70. --cache.group_level;
  71. if(cache.group_level == 0)
  72. cache.MarkAndSweep();
  73. }
  74. released = true;
  75. }
  76. }
  77. pkgDepCache::ActionGroup::~ActionGroup()
  78. {
  79. release();
  80. }
  81. /*}}}*/
  82. // DepCache::pkgDepCache - Constructors /*{{{*/
  83. // ---------------------------------------------------------------------
  84. /* */
  85. pkgDepCache::pkgDepCache(pkgCache * const pCache,Policy * const Plcy) :
  86. group_level(0), Cache(pCache), PkgState(0), DepState(0),
  87. iUsrSize(0), iDownloadSize(0), iInstCount(0), iDelCount(0), iKeepCount(0),
  88. iBrokenCount(0), iPolicyBrokenCount(0), iBadCount(0), d(NULL)
  89. {
  90. DebugMarker = _config->FindB("Debug::pkgDepCache::Marker", false);
  91. DebugAutoInstall = _config->FindB("Debug::pkgDepCache::AutoInstall", false);
  92. delLocalPolicy = 0;
  93. LocalPolicy = Plcy;
  94. if (LocalPolicy == 0)
  95. delLocalPolicy = LocalPolicy = new Policy;
  96. }
  97. /*}}}*/
  98. // DepCache::~pkgDepCache - Destructor /*{{{*/
  99. // ---------------------------------------------------------------------
  100. /* */
  101. pkgDepCache::~pkgDepCache()
  102. {
  103. delete [] PkgState;
  104. delete [] DepState;
  105. delete delLocalPolicy;
  106. }
  107. /*}}}*/
  108. // DepCache::Init - Generate the initial extra structures. /*{{{*/
  109. // ---------------------------------------------------------------------
  110. /* This allocats the extension buffers and initializes them. */
  111. bool pkgDepCache::Init(OpProgress * const Prog)
  112. {
  113. // Suppress mark updates during this operation (just in case) and
  114. // run a mark operation when Init terminates.
  115. ActionGroup actions(*this);
  116. delete [] PkgState;
  117. delete [] DepState;
  118. PkgState = new StateCache[Head().PackageCount];
  119. DepState = new unsigned char[Head().DependsCount];
  120. memset(PkgState,0,sizeof(*PkgState)*Head().PackageCount);
  121. memset(DepState,0,sizeof(*DepState)*Head().DependsCount);
  122. if (Prog != 0)
  123. {
  124. Prog->OverallProgress(0,2*Head().PackageCount,Head().PackageCount,
  125. _("Building dependency tree"));
  126. Prog->SubProgress(Head().PackageCount,_("Candidate versions"));
  127. }
  128. /* Set the current state of everything. In this state all of the
  129. packages are kept exactly as is. See AllUpgrade */
  130. int Done = 0;
  131. for (PkgIterator I = PkgBegin(); I.end() != true; ++I, ++Done)
  132. {
  133. if (Prog != 0 && Done%20 == 0)
  134. Prog->Progress(Done);
  135. // Find the proper cache slot
  136. StateCache &State = PkgState[I->ID];
  137. State.iFlags = 0;
  138. // Figure out the install version
  139. State.CandidateVer = LocalPolicy->GetCandidateVer(I);
  140. State.InstallVer = I.CurrentVer();
  141. State.Mode = ModeKeep;
  142. State.Update(I,*this);
  143. }
  144. if (Prog != 0)
  145. {
  146. Prog->OverallProgress(Head().PackageCount,2*Head().PackageCount,
  147. Head().PackageCount,
  148. _("Building dependency tree"));
  149. Prog->SubProgress(Head().PackageCount,_("Dependency generation"));
  150. }
  151. Update(Prog);
  152. if(Prog != 0)
  153. Prog->Done();
  154. return true;
  155. }
  156. /*}}}*/
  157. bool pkgDepCache::readStateFile(OpProgress * const Prog) /*{{{*/
  158. {
  159. FileFd state_file;
  160. string const state = _config->FindFile("Dir::State::extended_states");
  161. if(RealFileExists(state)) {
  162. state_file.Open(state, FileFd::ReadOnly);
  163. off_t const file_size = state_file.Size();
  164. if(Prog != NULL)
  165. Prog->OverallProgress(0, file_size, 1,
  166. _("Reading state information"));
  167. pkgTagFile tagfile(&state_file);
  168. pkgTagSection section;
  169. off_t amt = 0;
  170. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  171. while(tagfile.Step(section)) {
  172. string const pkgname = section.FindS("Package");
  173. string pkgarch = section.FindS("Architecture");
  174. if (pkgarch.empty() == true)
  175. pkgarch = "any";
  176. pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
  177. // Silently ignore unknown packages and packages with no actual version.
  178. if(pkg.end() == true || pkg->VersionList == 0)
  179. continue;
  180. short const reason = section.FindI("Auto-Installed", 0);
  181. if(reason > 0)
  182. {
  183. PkgState[pkg->ID].Flags |= Flag::Auto;
  184. if (unlikely(debug_autoremove))
  185. std::clog << "Auto-Installed : " << pkg.FullName() << std::endl;
  186. if (pkgarch == "any")
  187. {
  188. pkgCache::GrpIterator G = pkg.Group();
  189. for (pkg = G.NextPkg(pkg); pkg.end() != true; pkg = G.NextPkg(pkg))
  190. if (pkg->VersionList != 0)
  191. PkgState[pkg->ID].Flags |= Flag::Auto;
  192. }
  193. }
  194. amt += section.size();
  195. if(Prog != NULL)
  196. Prog->OverallProgress(amt, file_size, 1,
  197. _("Reading state information"));
  198. }
  199. if(Prog != NULL)
  200. Prog->OverallProgress(file_size, file_size, 1,
  201. _("Reading state information"));
  202. }
  203. return true;
  204. }
  205. /*}}}*/
  206. bool pkgDepCache::writeStateFile(OpProgress * const /*prog*/, bool const InstalledOnly) /*{{{*/
  207. {
  208. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  209. if(debug_autoremove)
  210. std::clog << "pkgDepCache::writeStateFile()" << std::endl;
  211. FileFd StateFile;
  212. string const state = _config->FindFile("Dir::State::extended_states");
  213. if (CreateAPTDirectoryIfNeeded(_config->FindDir("Dir::State"), flNotFile(state)) == false)
  214. return false;
  215. // if it does not exist, create a empty one
  216. if(!RealFileExists(state))
  217. {
  218. StateFile.Open(state, FileFd::WriteAtomic);
  219. StateFile.Close();
  220. }
  221. // open it
  222. if(!StateFile.Open(state, FileFd::ReadOnly))
  223. return _error->Error(_("Failed to open StateFile %s"),
  224. state.c_str());
  225. FileFd OutFile(state, FileFd::ReadWrite | FileFd::Atomic);
  226. if (OutFile.IsOpen() == false || OutFile.Failed() == true)
  227. return _error->Error(_("Failed to write temporary StateFile %s"), state.c_str());
  228. // first merge with the existing sections
  229. pkgTagFile tagfile(&StateFile);
  230. pkgTagSection section;
  231. std::set<string> pkgs_seen;
  232. while(tagfile.Step(section)) {
  233. string const pkgname = section.FindS("Package");
  234. string pkgarch = section.FindS("Architecture");
  235. if (pkgarch.empty() == true)
  236. pkgarch = "native";
  237. // Silently ignore unknown packages and packages with no actual
  238. // version.
  239. pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
  240. if(pkg.end() || pkg.VersionList().end())
  241. continue;
  242. StateCache const &P = PkgState[pkg->ID];
  243. bool newAuto = (P.Flags & Flag::Auto);
  244. // skip not installed or now-removed ones if requested
  245. if (InstalledOnly && (
  246. (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
  247. (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
  248. {
  249. // The section is obsolete if it contains no other tag
  250. unsigned int const count = section.Count();
  251. if (count < 2 ||
  252. (count == 2 && section.Exists("Auto-Installed")) ||
  253. (count == 3 && section.Exists("Auto-Installed") && section.Exists("Architecture")))
  254. continue;
  255. else
  256. newAuto = false;
  257. }
  258. if(_config->FindB("Debug::pkgAutoRemove",false))
  259. std::clog << "Update existing AutoInstall info: "
  260. << pkg.FullName() << std::endl;
  261. std::vector<pkgTagSection::Tag> rewrite;
  262. rewrite.push_back(pkgTagSection::Tag::Rewrite("Architecture", pkg.Arch()));
  263. rewrite.push_back(pkgTagSection::Tag::Rewrite("Auto-Installed", newAuto ? "1" : "0"));
  264. section.Write(OutFile, NULL, rewrite);
  265. if (OutFile.Write("\n", 1) == false)
  266. return false;
  267. pkgs_seen.insert(pkg.FullName());
  268. }
  269. // then write the ones we have not seen yet
  270. for(pkgCache::PkgIterator pkg=Cache->PkgBegin(); !pkg.end(); ++pkg) {
  271. StateCache const &P = PkgState[pkg->ID];
  272. if(P.Flags & Flag::Auto) {
  273. if (pkgs_seen.find(pkg.FullName()) != pkgs_seen.end()) {
  274. if(debug_autoremove)
  275. std::clog << "Skipping already written " << pkg.FullName() << std::endl;
  276. continue;
  277. }
  278. // skip not installed ones if requested
  279. if (InstalledOnly && (
  280. (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
  281. (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
  282. continue;
  283. const char* const pkgarch = pkg.Arch();
  284. if (strcmp(pkgarch, "all") == 0)
  285. continue;
  286. if(debug_autoremove)
  287. std::clog << "Writing new AutoInstall: " << pkg.FullName() << std::endl;
  288. std::string stanza = "Package: ";
  289. stanza.append(pkg.Name())
  290. .append("\nArchitecture: ").append(pkgarch)
  291. .append("\nAuto-Installed: 1\n\n");
  292. if (OutFile.Write(stanza.c_str(), stanza.length()) == false)
  293. return false;
  294. }
  295. }
  296. if (OutFile.Close() == false)
  297. return false;
  298. chmod(state.c_str(), 0644);
  299. return true;
  300. }
  301. /*}}}*/
  302. // DepCache::CheckDep - Checks a single dependency /*{{{*/
  303. // ---------------------------------------------------------------------
  304. /* This first checks the dependency against the main target package and
  305. then walks along the package provides list and checks if each provides
  306. will be installed then checks the provides against the dep. Res will be
  307. set to the package which was used to satisfy the dep. */
  308. bool pkgDepCache::CheckDep(DepIterator const &Dep,int const Type,PkgIterator &Res)
  309. {
  310. Res = Dep.TargetPkg();
  311. /* Check simple depends. A depends -should- never self match but
  312. we allow it anyhow because dpkg does. Technically it is a packaging
  313. bug. Conflicts may never self match */
  314. if (Dep.IsIgnorable(Res) == false)
  315. {
  316. // Check the base package
  317. if (Type == NowVersion)
  318. {
  319. if (Res->CurrentVer != 0 && Dep.IsSatisfied(Res.CurrentVer()) == true)
  320. return true;
  321. }
  322. else if (Type == InstallVersion)
  323. {
  324. if (PkgState[Res->ID].InstallVer != 0 &&
  325. Dep.IsSatisfied(PkgState[Res->ID].InstVerIter(*this)) == true)
  326. return true;
  327. }
  328. else if (Type == CandidateVersion)
  329. if (PkgState[Res->ID].CandidateVer != 0 &&
  330. Dep.IsSatisfied(PkgState[Res->ID].CandidateVerIter(*this)) == true)
  331. return true;
  332. }
  333. if (Dep->Type == Dep::Obsoletes)
  334. return false;
  335. // Check the providing packages
  336. PrvIterator P = Dep.TargetPkg().ProvidesList();
  337. for (; P.end() != true; ++P)
  338. {
  339. if (Dep.IsIgnorable(P) == true)
  340. continue;
  341. // Check if the provides is a hit
  342. if (Type == NowVersion)
  343. {
  344. if (P.OwnerPkg().CurrentVer() != P.OwnerVer())
  345. continue;
  346. }
  347. else if (Type == InstallVersion)
  348. {
  349. StateCache &State = PkgState[P.OwnerPkg()->ID];
  350. if (State.InstallVer != (Version *)P.OwnerVer())
  351. continue;
  352. }
  353. else if (Type == CandidateVersion)
  354. {
  355. StateCache &State = PkgState[P.OwnerPkg()->ID];
  356. if (State.CandidateVer != (Version *)P.OwnerVer())
  357. continue;
  358. }
  359. // Compare the versions.
  360. if (Dep.IsSatisfied(P) == true)
  361. {
  362. Res = P.OwnerPkg();
  363. return true;
  364. }
  365. }
  366. return false;
  367. }
  368. /*}}}*/
  369. // DepCache::AddSizes - Add the packages sizes to the counters /*{{{*/
  370. // ---------------------------------------------------------------------
  371. /* Call with Inverse = true to preform the inverse opration */
  372. void pkgDepCache::AddSizes(const PkgIterator &Pkg, bool const Inverse)
  373. {
  374. StateCache &P = PkgState[Pkg->ID];
  375. if (Pkg->VersionList == 0)
  376. return;
  377. if (Pkg.State() == pkgCache::PkgIterator::NeedsConfigure &&
  378. P.Keep() == true)
  379. return;
  380. // Compute the size data
  381. if (P.NewInstall() == true)
  382. {
  383. if (Inverse == false) {
  384. iUsrSize += P.InstVerIter(*this)->InstalledSize;
  385. iDownloadSize += P.InstVerIter(*this)->Size;
  386. } else {
  387. iUsrSize -= P.InstVerIter(*this)->InstalledSize;
  388. iDownloadSize -= P.InstVerIter(*this)->Size;
  389. }
  390. return;
  391. }
  392. // Upgrading
  393. if (Pkg->CurrentVer != 0 &&
  394. (P.InstallVer != (Version *)Pkg.CurrentVer() ||
  395. (P.iFlags & ReInstall) == ReInstall) && P.InstallVer != 0)
  396. {
  397. if (Inverse == false) {
  398. iUsrSize -= Pkg.CurrentVer()->InstalledSize;
  399. iUsrSize += P.InstVerIter(*this)->InstalledSize;
  400. iDownloadSize += P.InstVerIter(*this)->Size;
  401. } else {
  402. iUsrSize -= P.InstVerIter(*this)->InstalledSize;
  403. iUsrSize += Pkg.CurrentVer()->InstalledSize;
  404. iDownloadSize -= P.InstVerIter(*this)->Size;
  405. }
  406. return;
  407. }
  408. // Reinstall
  409. if (Pkg.State() == pkgCache::PkgIterator::NeedsUnpack &&
  410. P.Delete() == false)
  411. {
  412. if (Inverse == false)
  413. iDownloadSize += P.InstVerIter(*this)->Size;
  414. else
  415. iDownloadSize -= P.InstVerIter(*this)->Size;
  416. return;
  417. }
  418. // Removing
  419. if (Pkg->CurrentVer != 0 && P.InstallVer == 0)
  420. {
  421. if (Inverse == false)
  422. iUsrSize -= Pkg.CurrentVer()->InstalledSize;
  423. else
  424. iUsrSize += Pkg.CurrentVer()->InstalledSize;
  425. return;
  426. }
  427. }
  428. /*}}}*/
  429. // DepCache::AddStates - Add the package to the state counter /*{{{*/
  430. // ---------------------------------------------------------------------
  431. /* This routine is tricky to use, you must make sure that it is never
  432. called twice for the same package. This means the Remove/Add section
  433. should be as short as possible and not encompass any code that will
  434. calld Remove/Add itself. Remember, dependencies can be circular so
  435. while processing a dep for Pkg it is possible that Add/Remove
  436. will be called on Pkg */
  437. void pkgDepCache::AddStates(const PkgIterator &Pkg, bool const Invert)
  438. {
  439. signed char const Add = (Invert == false) ? 1 : -1;
  440. StateCache &State = PkgState[Pkg->ID];
  441. // The Package is broken (either minimal dep or policy dep)
  442. if ((State.DepState & DepInstMin) != DepInstMin)
  443. iBrokenCount += Add;
  444. if ((State.DepState & DepInstPolicy) != DepInstPolicy)
  445. iPolicyBrokenCount += Add;
  446. // Bad state
  447. if (Pkg.State() != PkgIterator::NeedsNothing)
  448. iBadCount += Add;
  449. // Not installed
  450. if (Pkg->CurrentVer == 0)
  451. {
  452. if (State.Mode == ModeDelete &&
  453. (State.iFlags & Purge) == Purge && Pkg.Purge() == false)
  454. iDelCount += Add;
  455. if (State.Mode == ModeInstall)
  456. iInstCount += Add;
  457. return;
  458. }
  459. // Installed, no upgrade
  460. if (State.Status == 0)
  461. {
  462. if (State.Mode == ModeDelete)
  463. iDelCount += Add;
  464. else
  465. if ((State.iFlags & ReInstall) == ReInstall)
  466. iInstCount += Add;
  467. return;
  468. }
  469. // Alll 3 are possible
  470. if (State.Mode == ModeDelete)
  471. iDelCount += Add;
  472. else if (State.Mode == ModeKeep)
  473. iKeepCount += Add;
  474. else if (State.Mode == ModeInstall)
  475. iInstCount += Add;
  476. }
  477. /*}}}*/
  478. // DepCache::BuildGroupOrs - Generate the Or group dep data /*{{{*/
  479. // ---------------------------------------------------------------------
  480. /* The or group results are stored in the last item of the or group. This
  481. allows easy detection of the state of a whole or'd group. */
  482. void pkgDepCache::BuildGroupOrs(VerIterator const &V)
  483. {
  484. unsigned char Group = 0;
  485. for (DepIterator D = V.DependsList(); D.end() != true; ++D)
  486. {
  487. // Build the dependency state.
  488. unsigned char &State = DepState[D->ID];
  489. /* Invert for Conflicts. We have to do this twice to get the
  490. right sense for a conflicts group */
  491. if (D.IsNegative() == true)
  492. State = ~State;
  493. // Add to the group if we are within an or..
  494. State &= 0x7;
  495. Group |= State;
  496. State |= Group << 3;
  497. if ((D->CompareOp & Dep::Or) != Dep::Or)
  498. Group = 0;
  499. // Invert for Conflicts
  500. if (D.IsNegative() == true)
  501. State = ~State;
  502. }
  503. }
  504. /*}}}*/
  505. // DepCache::VersionState - Perform a pass over a dependency list /*{{{*/
  506. // ---------------------------------------------------------------------
  507. /* This is used to run over a dependency list and determine the dep
  508. state of the list, filtering it through both a Min check and a Policy
  509. check. The return result will have SetMin/SetPolicy low if a check
  510. fails. It uses the DepState cache for it's computations. */
  511. unsigned char pkgDepCache::VersionState(DepIterator D, unsigned char const Check,
  512. unsigned char const SetMin,
  513. unsigned char const SetPolicy) const
  514. {
  515. unsigned char Dep = 0xFF;
  516. while (D.end() != true)
  517. {
  518. // the last or-dependency has the state of all previous or'ed
  519. DepIterator Start, End;
  520. D.GlobOr(Start, End);
  521. // ignore if we are called with Dep{Install,…} or DepG{Install,…}
  522. // the later would be more correct, but the first is what we get
  523. unsigned char const State = DepState[End->ID] | (DepState[End->ID] >> 3);
  524. // Minimum deps that must be satisfied to have a working package
  525. if (Start.IsCritical() == true)
  526. {
  527. if ((State & Check) != Check)
  528. return Dep &= ~(SetMin | SetPolicy);
  529. }
  530. // Policy deps that must be satisfied to install the package
  531. else if (IsImportantDep(Start) == true &&
  532. (State & Check) != Check)
  533. Dep &= ~SetPolicy;
  534. }
  535. return Dep;
  536. }
  537. /*}}}*/
  538. // DepCache::DependencyState - Compute the 3 results for a dep /*{{{*/
  539. // ---------------------------------------------------------------------
  540. /* This is the main dependency computation bit. It computes the 3 main
  541. results for a dependencys, Now, Install and Candidate. Callers must
  542. invert the result if dealing with conflicts. */
  543. unsigned char pkgDepCache::DependencyState(DepIterator const &D)
  544. {
  545. unsigned char State = 0;
  546. if (CheckDep(D,NowVersion) == true)
  547. State |= DepNow;
  548. if (CheckDep(D,InstallVersion) == true)
  549. State |= DepInstall;
  550. if (CheckDep(D,CandidateVersion) == true)
  551. State |= DepCVer;
  552. return State;
  553. }
  554. /*}}}*/
  555. // DepCache::UpdateVerState - Compute the Dep member of the state /*{{{*/
  556. // ---------------------------------------------------------------------
  557. /* This determines the combined dependency representation of a package
  558. for its two states now and install. This is done by using the pre-generated
  559. dependency information. */
  560. void pkgDepCache::UpdateVerState(PkgIterator const &Pkg)
  561. {
  562. // Empty deps are always true
  563. StateCache &State = PkgState[Pkg->ID];
  564. State.DepState = 0xFF;
  565. // Check the Current state
  566. if (Pkg->CurrentVer != 0)
  567. {
  568. DepIterator D = Pkg.CurrentVer().DependsList();
  569. State.DepState &= VersionState(D,DepNow,DepNowMin,DepNowPolicy);
  570. }
  571. /* Check the candidate state. We do not compare against the whole as
  572. a candidate state but check the candidate version against the
  573. install states */
  574. if (State.CandidateVer != 0)
  575. {
  576. DepIterator D = State.CandidateVerIter(*this).DependsList();
  577. State.DepState &= VersionState(D,DepInstall,DepCandMin,DepCandPolicy);
  578. }
  579. // Check target state which can only be current or installed
  580. if (State.InstallVer != 0)
  581. {
  582. DepIterator D = State.InstVerIter(*this).DependsList();
  583. State.DepState &= VersionState(D,DepInstall,DepInstMin,DepInstPolicy);
  584. }
  585. }
  586. /*}}}*/
  587. // DepCache::Update - Figure out all the state information /*{{{*/
  588. // ---------------------------------------------------------------------
  589. /* This will figure out the state of all the packages and all the
  590. dependencies based on the current policy. */
  591. void pkgDepCache::Update(OpProgress * const Prog)
  592. {
  593. iUsrSize = 0;
  594. iDownloadSize = 0;
  595. iInstCount = 0;
  596. iDelCount = 0;
  597. iKeepCount = 0;
  598. iBrokenCount = 0;
  599. iPolicyBrokenCount = 0;
  600. iBadCount = 0;
  601. // Perform the depends pass
  602. int Done = 0;
  603. for (PkgIterator I = PkgBegin(); I.end() != true; ++I, ++Done)
  604. {
  605. if (Prog != 0 && Done%20 == 0)
  606. Prog->Progress(Done);
  607. for (VerIterator V = I.VersionList(); V.end() != true; ++V)
  608. {
  609. unsigned char Group = 0;
  610. for (DepIterator D = V.DependsList(); D.end() != true; ++D)
  611. {
  612. // Build the dependency state.
  613. unsigned char &State = DepState[D->ID];
  614. State = DependencyState(D);
  615. // Add to the group if we are within an or..
  616. Group |= State;
  617. State |= Group << 3;
  618. if ((D->CompareOp & Dep::Or) != Dep::Or)
  619. Group = 0;
  620. // Invert for Conflicts
  621. if (D.IsNegative() == true)
  622. State = ~State;
  623. }
  624. }
  625. // Compute the package dependency state and size additions
  626. AddSizes(I);
  627. UpdateVerState(I);
  628. AddStates(I);
  629. }
  630. if (Prog != 0)
  631. Prog->Progress(Done);
  632. readStateFile(Prog);
  633. }
  634. /*}}}*/
  635. // DepCache::Update - Update the deps list of a package /*{{{*/
  636. // ---------------------------------------------------------------------
  637. /* This is a helper for update that only does the dep portion of the scan.
  638. It is mainly meant to scan reverse dependencies. */
  639. void pkgDepCache::Update(DepIterator D)
  640. {
  641. // Update the reverse deps
  642. for (;D.end() != true; ++D)
  643. {
  644. unsigned char &State = DepState[D->ID];
  645. State = DependencyState(D);
  646. // Invert for Conflicts
  647. if (D.IsNegative() == true)
  648. State = ~State;
  649. RemoveStates(D.ParentPkg());
  650. BuildGroupOrs(D.ParentVer());
  651. UpdateVerState(D.ParentPkg());
  652. AddStates(D.ParentPkg());
  653. }
  654. }
  655. /*}}}*/
  656. // DepCache::Update - Update the related deps of a package /*{{{*/
  657. // ---------------------------------------------------------------------
  658. /* This is called whenever the state of a package changes. It updates
  659. all cached dependencies related to this package. */
  660. void pkgDepCache::Update(PkgIterator const &Pkg)
  661. {
  662. // Recompute the dep of the package
  663. RemoveStates(Pkg);
  664. UpdateVerState(Pkg);
  665. AddStates(Pkg);
  666. // Update the reverse deps
  667. Update(Pkg.RevDependsList());
  668. // Update the provides map for the current ver
  669. if (Pkg->CurrentVer != 0)
  670. for (PrvIterator P = Pkg.CurrentVer().ProvidesList();
  671. P.end() != true; ++P)
  672. Update(P.ParentPkg().RevDependsList());
  673. // Update the provides map for the candidate ver
  674. if (PkgState[Pkg->ID].CandidateVer != 0)
  675. for (PrvIterator P = PkgState[Pkg->ID].CandidateVerIter(*this).ProvidesList();
  676. P.end() != true; ++P)
  677. Update(P.ParentPkg().RevDependsList());
  678. }
  679. /*}}}*/
  680. // DepCache::MarkKeep - Put the package in the keep state /*{{{*/
  681. // ---------------------------------------------------------------------
  682. /* */
  683. bool pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser,
  684. unsigned long Depth)
  685. {
  686. if (IsModeChangeOk(ModeKeep, Pkg, Depth, FromUser) == false)
  687. return false;
  688. /* Reject an attempt to keep a non-source broken installed package, those
  689. must be upgraded */
  690. if (Pkg.State() == PkgIterator::NeedsUnpack &&
  691. Pkg.CurrentVer().Downloadable() == false)
  692. return false;
  693. /* We changed the soft state all the time so the UI is a bit nicer
  694. to use */
  695. StateCache &P = PkgState[Pkg->ID];
  696. // Check that it is not already kept
  697. if (P.Mode == ModeKeep)
  698. return true;
  699. if (Soft == true)
  700. P.iFlags |= AutoKept;
  701. else
  702. P.iFlags &= ~AutoKept;
  703. ActionGroup group(*this);
  704. #if 0 // reseting the autoflag here means we lose the
  705. // auto-mark information if a user selects a package for removal
  706. // but changes his mind then and sets it for keep again
  707. // - this makes sense as default when all Garbage dependencies
  708. // are automatically marked for removal (as aptitude does).
  709. // setting a package for keep then makes it no longer autoinstalled
  710. // for all other use-case this action is rather surprising
  711. if(FromUser && !P.Marked)
  712. P.Flags &= ~Flag::Auto;
  713. #endif
  714. if (DebugMarker == true)
  715. std::clog << OutputInDepth(Depth) << "MarkKeep " << Pkg << " FU=" << FromUser << std::endl;
  716. RemoveSizes(Pkg);
  717. RemoveStates(Pkg);
  718. P.Mode = ModeKeep;
  719. if (Pkg->CurrentVer == 0)
  720. P.InstallVer = 0;
  721. else
  722. P.InstallVer = Pkg.CurrentVer();
  723. AddStates(Pkg);
  724. Update(Pkg);
  725. AddSizes(Pkg);
  726. return true;
  727. }
  728. /*}}}*/
  729. // DepCache::MarkDelete - Put the package in the delete state /*{{{*/
  730. // ---------------------------------------------------------------------
  731. /* */
  732. bool pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge,
  733. unsigned long Depth, bool FromUser)
  734. {
  735. if (IsModeChangeOk(ModeDelete, Pkg, Depth, FromUser) == false)
  736. return false;
  737. StateCache &P = PkgState[Pkg->ID];
  738. // Check that it is not already marked for delete
  739. if ((P.Mode == ModeDelete || P.InstallVer == 0) &&
  740. (Pkg.Purge() == true || rPurge == false))
  741. return true;
  742. // check if we are allowed to remove the package
  743. if (IsDeleteOk(Pkg,rPurge,Depth,FromUser) == false)
  744. return false;
  745. P.iFlags &= ~(AutoKept | Purge);
  746. if (rPurge == true)
  747. P.iFlags |= Purge;
  748. ActionGroup group(*this);
  749. if (FromUser == false)
  750. {
  751. VerIterator const PV = P.InstVerIter(*this);
  752. if (PV.end() == false)
  753. {
  754. // removed metapackages mark their dependencies as manual to prevent in "desktop depends browser, texteditor"
  755. // the removal of browser to suggest the removal of desktop and texteditor.
  756. // We ignore the auto-bit here as we can't deal with metapackage cascardes otherwise.
  757. // We do not check for or-groups here as we don't know which package takes care of
  758. // providing the feature the user likes e.g.: browser1 | browser2 | browser3
  759. // Temporary removals are effected by this as well, which is bad, but unlikely in practice
  760. bool const PinNeverMarkAutoSection = (PV->Section != 0 && ConfigValueInSubTree("APT::Never-MarkAuto-Sections", PV.Section()));
  761. if (PinNeverMarkAutoSection)
  762. {
  763. for (DepIterator D = PV.DependsList(); D.end() != true; ++D)
  764. {
  765. if (D.IsMultiArchImplicit() == true || D.IsNegative() == true || IsImportantDep(D) == false)
  766. continue;
  767. pkgCacheFile CacheFile(this);
  768. APT::VersionList verlist = APT::VersionList::FromDependency(CacheFile, D, APT::CacheSetHelper::INSTALLED);
  769. for (auto const &V : verlist)
  770. {
  771. PkgIterator const DP = V.ParentPkg();
  772. if(DebugAutoInstall == true)
  773. std::clog << OutputInDepth(Depth) << "Setting " << DP.FullName(false) << " NOT as auto-installed (direct "
  774. << D.DepType() << " of " << Pkg.FullName(false) << " which is in APT::Never-MarkAuto-Sections)" << std::endl;
  775. MarkAuto(DP, false);
  776. }
  777. }
  778. }
  779. }
  780. }
  781. if (DebugMarker == true)
  782. std::clog << OutputInDepth(Depth) << (rPurge ? "MarkPurge " : "MarkDelete ") << Pkg << " FU=" << FromUser << std::endl;
  783. RemoveSizes(Pkg);
  784. RemoveStates(Pkg);
  785. if (Pkg->CurrentVer == 0 && (Pkg.Purge() == true || rPurge == false))
  786. P.Mode = ModeKeep;
  787. else
  788. P.Mode = ModeDelete;
  789. P.InstallVer = 0;
  790. AddStates(Pkg);
  791. Update(Pkg);
  792. AddSizes(Pkg);
  793. return true;
  794. }
  795. /*}}}*/
  796. // DepCache::IsDeleteOk - check if it is ok to remove this package /*{{{*/
  797. // ---------------------------------------------------------------------
  798. /* The default implementation tries to prevent deletion of install requests.
  799. dpkg holds are enforced by the private IsModeChangeOk */
  800. bool pkgDepCache::IsDeleteOk(PkgIterator const &Pkg,bool rPurge,
  801. unsigned long Depth, bool FromUser)
  802. {
  803. return IsDeleteOkProtectInstallRequests(Pkg, rPurge, Depth, FromUser);
  804. }
  805. bool pkgDepCache::IsDeleteOkProtectInstallRequests(PkgIterator const &Pkg,
  806. bool const /*rPurge*/, unsigned long const Depth, bool const FromUser)
  807. {
  808. if (FromUser == false && Pkg->CurrentVer == 0)
  809. {
  810. StateCache &P = PkgState[Pkg->ID];
  811. if (P.InstallVer != 0 && P.Status == 2 && (P.Flags & Flag::Auto) != Flag::Auto)
  812. {
  813. if (DebugMarker == true)
  814. std::clog << OutputInDepth(Depth) << "Manual install request prevents MarkDelete of " << Pkg << std::endl;
  815. return false;
  816. }
  817. }
  818. return true;
  819. }
  820. /*}}}*/
  821. // DepCache::IsModeChangeOk - check if it is ok to change the mode /*{{{*/
  822. // ---------------------------------------------------------------------
  823. /* this is used by all Mark methods on the very first line to check sanity
  824. and prevents mode changes for packages on hold for example.
  825. If you want to check Mode specific stuff you can use the virtual public
  826. Is<Mode>Ok methods instead */
  827. static char const* PrintMode(char const mode)
  828. {
  829. switch (mode)
  830. {
  831. case pkgDepCache::ModeInstall: return "Install";
  832. case pkgDepCache::ModeKeep: return "Keep";
  833. case pkgDepCache::ModeDelete: return "Delete";
  834. case pkgDepCache::ModeGarbage: return "Garbage";
  835. default: return "UNKNOWN";
  836. }
  837. }
  838. bool pkgDepCache::IsModeChangeOk(ModeList const mode, PkgIterator const &Pkg,
  839. unsigned long const Depth, bool const FromUser)
  840. {
  841. // we are not trying to hard…
  842. if (unlikely(Depth > 100))
  843. return false;
  844. // general sanity
  845. if (unlikely(Pkg.end() == true || Pkg->VersionList == 0))
  846. return false;
  847. // the user is always right
  848. if (FromUser == true)
  849. return true;
  850. StateCache &P = PkgState[Pkg->ID];
  851. // not changing the mode is obviously also fine as we might want to call
  852. // e.g. MarkInstall multiple times with different arguments for the same package
  853. if (P.Mode == mode)
  854. return true;
  855. // if previous state was set by user only user can reset it
  856. if ((P.iFlags & Protected) == Protected)
  857. {
  858. if (unlikely(DebugMarker == true))
  859. std::clog << OutputInDepth(Depth) << "Ignore Mark" << PrintMode(mode)
  860. << " of " << Pkg << " as its mode (" << PrintMode(P.Mode)
  861. << ") is protected" << std::endl;
  862. return false;
  863. }
  864. // enforce dpkg holds
  865. else if (mode != ModeKeep && Pkg->SelectedState == pkgCache::State::Hold &&
  866. _config->FindB("APT::Ignore-Hold",false) == false)
  867. {
  868. if (unlikely(DebugMarker == true))
  869. std::clog << OutputInDepth(Depth) << "Hold prevents Mark" << PrintMode(mode)
  870. << " of " << Pkg << std::endl;
  871. return false;
  872. }
  873. return true;
  874. }
  875. /*}}}*/
  876. // DepCache::MarkInstall - Put the package in the install state /*{{{*/
  877. // ---------------------------------------------------------------------
  878. /* */
  879. struct CompareProviders {
  880. pkgCache::PkgIterator const Pkg;
  881. explicit CompareProviders(pkgCache::DepIterator const &Dep) : Pkg(Dep.TargetPkg()) {};
  882. //bool operator() (APT::VersionList::iterator const &AV, APT::VersionList::iterator const &BV)
  883. bool operator() (pkgCache::VerIterator const &AV, pkgCache::VerIterator const &BV)
  884. {
  885. pkgCache::PkgIterator const A = AV.ParentPkg();
  886. pkgCache::PkgIterator const B = BV.ParentPkg();
  887. // Prefer MA:same packages if other architectures for it are installed
  888. if ((AV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same ||
  889. (BV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  890. {
  891. bool instA = false;
  892. if ((AV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  893. {
  894. pkgCache::GrpIterator Grp = A.Group();
  895. for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
  896. if (P->CurrentVer != 0)
  897. {
  898. instA = true;
  899. break;
  900. }
  901. }
  902. bool instB = false;
  903. if ((BV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  904. {
  905. pkgCache::GrpIterator Grp = B.Group();
  906. for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
  907. {
  908. if (P->CurrentVer != 0)
  909. {
  910. instB = true;
  911. break;
  912. }
  913. }
  914. }
  915. if (instA != instB)
  916. return instA == false;
  917. }
  918. // Prefer packages in the same group as the target; e.g. foo:i386, foo:amd64
  919. if (A->Group != B->Group)
  920. {
  921. if (A->Group == Pkg->Group && B->Group != Pkg->Group)
  922. return false;
  923. else if (B->Group == Pkg->Group && A->Group != Pkg->Group)
  924. return true;
  925. }
  926. // we like essentials
  927. if ((A->Flags & pkgCache::Flag::Essential) != (B->Flags & pkgCache::Flag::Essential))
  928. {
  929. if ((A->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  930. return false;
  931. else if ((B->Flags & pkgCache::Flag::Essential) == pkgCache::Flag::Essential)
  932. return true;
  933. }
  934. if ((A->Flags & pkgCache::Flag::Important) != (B->Flags & pkgCache::Flag::Important))
  935. {
  936. if ((A->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
  937. return false;
  938. else if ((B->Flags & pkgCache::Flag::Important) == pkgCache::Flag::Important)
  939. return true;
  940. }
  941. // prefer native architecture
  942. if (strcmp(A.Arch(), B.Arch()) != 0)
  943. {
  944. if (strcmp(A.Arch(), A.Cache()->NativeArch()) == 0)
  945. return false;
  946. else if (strcmp(B.Arch(), B.Cache()->NativeArch()) == 0)
  947. return true;
  948. std::vector<std::string> archs = APT::Configuration::getArchitectures();
  949. for (std::vector<std::string>::const_iterator a = archs.begin(); a != archs.end(); ++a)
  950. if (*a == A.Arch())
  951. return false;
  952. else if (*a == B.Arch())
  953. return true;
  954. }
  955. // higher priority seems like a good idea
  956. if (AV->Priority != BV->Priority)
  957. return AV->Priority > BV->Priority;
  958. // unable to decide…
  959. return A->ID < B->ID;
  960. }
  961. };
  962. bool pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
  963. unsigned long Depth, bool FromUser,
  964. bool ForceImportantDeps)
  965. {
  966. if (IsModeChangeOk(ModeInstall, Pkg, Depth, FromUser) == false)
  967. return false;
  968. StateCache &P = PkgState[Pkg->ID];
  969. // See if there is even any possible instalation candidate
  970. if (P.CandidateVer == 0)
  971. return false;
  972. /* Check that it is not already marked for install and that it can be
  973. installed */
  974. if ((P.InstPolicyBroken() == false && P.InstBroken() == false) &&
  975. (P.Mode == ModeInstall ||
  976. P.CandidateVer == (Version *)Pkg.CurrentVer()))
  977. {
  978. if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
  979. return MarkKeep(Pkg, false, FromUser, Depth+1);
  980. return true;
  981. }
  982. // check if we are allowed to install the package
  983. if (IsInstallOk(Pkg,AutoInst,Depth,FromUser) == false)
  984. return false;
  985. ActionGroup group(*this);
  986. P.iFlags &= ~AutoKept;
  987. /* Target the candidate version and remove the autoflag. We reset the
  988. autoflag below if this was called recursively. Otherwise the user
  989. should have the ability to de-auto a package by changing its state */
  990. RemoveSizes(Pkg);
  991. RemoveStates(Pkg);
  992. P.Mode = ModeInstall;
  993. P.InstallVer = P.CandidateVer;
  994. if(FromUser)
  995. {
  996. // Set it to manual if it's a new install or already installed,
  997. // but only if its not marked by the autoremover (aptitude depend on this behavior)
  998. // or if we do automatic installation (aptitude never does it)
  999. if(P.Status == 2 || (Pkg->CurrentVer != 0 && (AutoInst == true || P.Marked == false)))
  1000. P.Flags &= ~Flag::Auto;
  1001. }
  1002. else
  1003. {
  1004. // Set it to auto if this is a new install.
  1005. if(P.Status == 2)
  1006. P.Flags |= Flag::Auto;
  1007. }
  1008. if (P.CandidateVer == (Version *)Pkg.CurrentVer())
  1009. P.Mode = ModeKeep;
  1010. AddStates(Pkg);
  1011. Update(Pkg);
  1012. AddSizes(Pkg);
  1013. if (AutoInst == false || _config->Find("APT::Solver", "internal") != "internal")
  1014. return true;
  1015. if (DebugMarker == true)
  1016. std::clog << OutputInDepth(Depth) << "MarkInstall " << Pkg << " FU=" << FromUser << std::endl;
  1017. bool MoveAutoBitToDependencies = false;
  1018. VerIterator const PV = P.InstVerIter(*this);
  1019. if (unlikely(PV.end() == true))
  1020. return false;
  1021. else if (PV->Section != 0 && (P.Flags & Flag::Auto) != Flag::Auto)
  1022. {
  1023. VerIterator const CurVer = Pkg.CurrentVer();
  1024. if (CurVer.end() == false && CurVer->Section != 0 && strcmp(CurVer.Section(), PV.Section()) != 0)
  1025. {
  1026. bool const CurVerInMoveSection = ConfigValueInSubTree("APT::Move-Autobit-Sections", CurVer.Section());
  1027. bool const InstVerInMoveSection = ConfigValueInSubTree("APT::Move-Autobit-Sections", PV.Section());
  1028. MoveAutoBitToDependencies = (CurVerInMoveSection == false && InstVerInMoveSection == true);
  1029. if (MoveAutoBitToDependencies == true)
  1030. {
  1031. if(DebugAutoInstall == true)
  1032. std::clog << OutputInDepth(Depth) << "Setting " << Pkg.FullName(false) << " as auto-installed, moving manual to its dependencies" << std::endl;
  1033. MarkAuto(Pkg, true);
  1034. }
  1035. }
  1036. }
  1037. DepIterator Dep = PV.DependsList();
  1038. for (; Dep.end() != true;)
  1039. {
  1040. // Grok or groups
  1041. DepIterator Start = Dep;
  1042. bool Result = true;
  1043. unsigned Ors = 0;
  1044. for (bool LastOR = true; Dep.end() == false && LastOR == true; ++Dep, ++Ors)
  1045. {
  1046. LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
  1047. if ((DepState[Dep->ID] & DepInstall) == DepInstall)
  1048. Result = false;
  1049. }
  1050. // Dep is satisfied okay.
  1051. if (Result == false)
  1052. continue;
  1053. /* Check if this dep should be consider for install. If it is a user
  1054. defined important dep and we are installed a new package then
  1055. it will be installed. Otherwise we only check for important
  1056. deps that have changed from the installed version */
  1057. if (IsImportantDep(Start) == false)
  1058. continue;
  1059. /* If we are in an or group locate the first or that can
  1060. succeed. We have already cached this… */
  1061. for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; --Ors)
  1062. ++Start;
  1063. /* unsatisfiable dependency: IsInstallOkDependenciesSatisfiableByCandidates
  1064. would have prevented us to get here if not overridden, so just skip
  1065. over the problem here as the front-end will know what it is doing */
  1066. if (Ors == 1 && (DepState[Start->ID] &DepCVer) != DepCVer && Start.IsNegative() == false)
  1067. continue;
  1068. /* Check if any ImportantDep() (but not Critical) were added
  1069. * since we installed the package. Also check for deps that
  1070. * were satisfied in the past: for instance, if a version
  1071. * restriction in a Recommends was tightened, upgrading the
  1072. * package should follow that Recommends rather than causing the
  1073. * dependency to be removed. (bug #470115)
  1074. */
  1075. if (Pkg->CurrentVer != 0 && ForceImportantDeps == false && Start.IsCritical() == false)
  1076. {
  1077. bool isNewImportantDep = true;
  1078. bool isPreviouslySatisfiedImportantDep = false;
  1079. for (DepIterator D = Pkg.CurrentVer().DependsList(); D.end() != true; ++D)
  1080. {
  1081. //FIXME: Should we handle or-group better here?
  1082. // We do not check if the package we look for is part of the same or-group
  1083. // we might find while searching, but could that really be a problem?
  1084. if (D.IsCritical() == true || IsImportantDep(D) == false ||
  1085. Start.TargetPkg() != D.TargetPkg())
  1086. continue;
  1087. isNewImportantDep = false;
  1088. while ((D->CompareOp & Dep::Or) != 0)
  1089. ++D;
  1090. isPreviouslySatisfiedImportantDep = (((*this)[D] & DepGNow) != 0);
  1091. if (isPreviouslySatisfiedImportantDep == true)
  1092. break;
  1093. }
  1094. if(isNewImportantDep == true)
  1095. {
  1096. if (DebugAutoInstall == true)
  1097. std::clog << OutputInDepth(Depth) << "new important dependency: "
  1098. << Start.TargetPkg().FullName() << std::endl;
  1099. }
  1100. else if(isPreviouslySatisfiedImportantDep == true)
  1101. {
  1102. if (DebugAutoInstall == true)
  1103. std::clog << OutputInDepth(Depth) << "previously satisfied important dependency on "
  1104. << Start.TargetPkg().FullName() << std::endl;
  1105. }
  1106. else
  1107. {
  1108. if (DebugAutoInstall == true)
  1109. std::clog << OutputInDepth(Depth) << "ignore old unsatisfied important dependency on "
  1110. << Start.TargetPkg().FullName() << std::endl;
  1111. continue;
  1112. }
  1113. }
  1114. /* This bit is for processing the possibility of an install/upgrade
  1115. fixing the problem for "positive" dependencies */
  1116. if (Start.IsNegative() == false && (DepState[Start->ID] & DepCVer) == DepCVer)
  1117. {
  1118. pkgCacheFile CacheFile(this);
  1119. APT::VersionList verlist = APT::VersionList::FromDependency(CacheFile, Start, APT::CacheSetHelper::CANDIDATE);
  1120. CompareProviders comp(Start);
  1121. do {
  1122. APT::VersionList::iterator InstVer = std::max_element(verlist.begin(), verlist.end(), comp);
  1123. if (InstVer == verlist.end())
  1124. break;
  1125. pkgCache::PkgIterator InstPkg = InstVer.ParentPkg();
  1126. if(DebugAutoInstall == true)
  1127. std::clog << OutputInDepth(Depth) << "Installing " << InstPkg.Name()
  1128. << " as " << Start.DepType() << " of " << Pkg.Name()
  1129. << std::endl;
  1130. if (MarkInstall(InstPkg, true, Depth + 1, false, ForceImportantDeps) == false)
  1131. {
  1132. verlist.erase(InstVer);
  1133. continue;
  1134. }
  1135. // now check if we should consider it a automatic dependency or not
  1136. if(InstPkg->CurrentVer == 0 && MoveAutoBitToDependencies)
  1137. {
  1138. if(DebugAutoInstall == true)
  1139. std::clog << OutputInDepth(Depth) << "Setting " << InstPkg.FullName(false) << " NOT as auto-installed (direct "
  1140. << Start.DepType() << " of " << Pkg.FullName(false) << " which is manual and in APT::Move-Autobit-Sections)" << std::endl;
  1141. MarkAuto(InstPkg, false);
  1142. }
  1143. break;
  1144. } while(true);
  1145. continue;
  1146. }
  1147. /* Negative dependencies have no or-group
  1148. If the dependency isn't versioned, we try if an upgrade might solve the problem.
  1149. Otherwise we remove the offender if needed */
  1150. else if (Start.IsNegative() == true && Start->Type != pkgCache::Dep::Obsoletes)
  1151. {
  1152. std::unique_ptr<Version *[]> List(Start.AllTargets());
  1153. pkgCache::PkgIterator TrgPkg = Start.TargetPkg();
  1154. for (Version **I = List.get(); *I != 0; I++)
  1155. {
  1156. VerIterator Ver(*this,*I);
  1157. PkgIterator Pkg = Ver.ParentPkg();
  1158. /* The List includes all packages providing this dependency,
  1159. even providers which are not installed, so skip them. */
  1160. if (PkgState[Pkg->ID].InstallVer == 0)
  1161. continue;
  1162. /* Ignore negative dependencies that we are not going to
  1163. get installed */
  1164. if (PkgState[Pkg->ID].InstallVer != *I)
  1165. continue;
  1166. if ((Start->Version != 0 || TrgPkg != Pkg) &&
  1167. PkgState[Pkg->ID].CandidateVer != PkgState[Pkg->ID].InstallVer &&
  1168. PkgState[Pkg->ID].CandidateVer != *I &&
  1169. MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps) == true)
  1170. continue;
  1171. else if (Start->Type == pkgCache::Dep::Conflicts ||
  1172. Start->Type == pkgCache::Dep::DpkgBreaks)
  1173. {
  1174. if(DebugAutoInstall == true)
  1175. std::clog << OutputInDepth(Depth)
  1176. << " Removing: " << Pkg.Name()
  1177. << std::endl;
  1178. if (MarkDelete(Pkg,false,Depth + 1, false) == false)
  1179. break;
  1180. }
  1181. }
  1182. continue;
  1183. }
  1184. }
  1185. return Dep.end() == true;
  1186. }
  1187. /*}}}*/
  1188. // DepCache::IsInstallOk - check if it is ok to install this package /*{{{*/
  1189. // ---------------------------------------------------------------------
  1190. /* The default implementation checks if the installation of an M-A:same
  1191. package would lead us into a version-screw and if so forbids it.
  1192. dpkg holds are enforced by the private IsModeChangeOk */
  1193. bool pkgDepCache::IsInstallOk(PkgIterator const &Pkg,bool AutoInst,
  1194. unsigned long Depth, bool FromUser)
  1195. {
  1196. return IsInstallOkMultiArchSameVersionSynced(Pkg,AutoInst, Depth, FromUser) &&
  1197. IsInstallOkDependenciesSatisfiableByCandidates(Pkg,AutoInst, Depth, FromUser);
  1198. }
  1199. bool pkgDepCache::IsInstallOkMultiArchSameVersionSynced(PkgIterator const &Pkg,
  1200. bool const /*AutoInst*/, unsigned long const Depth, bool const FromUser)
  1201. {
  1202. if (FromUser == true) // as always: user is always right
  1203. return true;
  1204. // if we have checked before and it was okay, it will still be okay
  1205. if (PkgState[Pkg->ID].Mode == ModeInstall &&
  1206. PkgState[Pkg->ID].InstallVer == PkgState[Pkg->ID].CandidateVer)
  1207. return true;
  1208. // ignore packages with none-M-A:same candidates
  1209. VerIterator const CandVer = PkgState[Pkg->ID].CandidateVerIter(*this);
  1210. if (unlikely(CandVer.end() == true) || CandVer == Pkg.CurrentVer() ||
  1211. (CandVer->MultiArch & pkgCache::Version::Same) != pkgCache::Version::Same)
  1212. return true;
  1213. GrpIterator const Grp = Pkg.Group();
  1214. for (PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
  1215. {
  1216. // not installed or self-check: fine by definition
  1217. if (P->CurrentVer == 0 || P == Pkg)
  1218. continue;
  1219. // not having a candidate or being in sync
  1220. // (simple string-compare as stuff like '1' == '0:1-0' can't happen here)
  1221. VerIterator CV = PkgState[P->ID].CandidateVerIter(*this);
  1222. if (CV.end() == true || strcmp(Pkg.CandVersion(), CV.VerStr()) == 0)
  1223. continue;
  1224. // packages losing M-A:same can be out-of-sync
  1225. if ((CV->MultiArch & pkgCache::Version::Same) != pkgCache::Version::Same)
  1226. continue;
  1227. // not downloadable means the package is obsolete, so allow out-of-sync
  1228. if (CV.Downloadable() == false)
  1229. continue;
  1230. PkgState[Pkg->ID].iFlags |= AutoKept;
  1231. if (unlikely(DebugMarker == true))
  1232. std::clog << OutputInDepth(Depth) << "Ignore MarkInstall of " << Pkg
  1233. << " as it is not in sync with its M-A:same sibling " << P
  1234. << " (" << Pkg.CandVersion() << " != " << CV.VerStr() << ")" << std::endl;
  1235. return false;
  1236. }
  1237. return true;
  1238. }
  1239. bool pkgDepCache::IsInstallOkDependenciesSatisfiableByCandidates(PkgIterator const &Pkg,
  1240. bool const AutoInst, unsigned long const Depth, bool const /*FromUser*/)
  1241. {
  1242. if (AutoInst == false)
  1243. return true;
  1244. VerIterator const CandVer = PkgState[Pkg->ID].CandidateVerIter(*this);
  1245. if (unlikely(CandVer.end() == true) || CandVer == Pkg.CurrentVer())
  1246. return true;
  1247. for (DepIterator Dep = CandVer.DependsList(); Dep.end() != true;)
  1248. {
  1249. // Grok or groups
  1250. DepIterator Start = Dep;
  1251. bool Result = true;
  1252. unsigned Ors = 0;
  1253. for (bool LastOR = true; Dep.end() == false && LastOR == true; ++Dep, ++Ors)
  1254. {
  1255. LastOR = (Dep->CompareOp & Dep::Or) == Dep::Or;
  1256. if ((DepState[Dep->ID] & DepInstall) == DepInstall)
  1257. Result = false;
  1258. }
  1259. if (Start.IsCritical() == false || Start.IsNegative() == true || Result == false)
  1260. continue;
  1261. /* If we are in an or group locate the first or that can succeed.
  1262. We have already cached this… */
  1263. for (; Ors > 1 && (DepState[Start->ID] & DepCVer) != DepCVer; --Ors)
  1264. ++Start;
  1265. if (Ors == 1 && (DepState[Start->ID] &DepCVer) != DepCVer)
  1266. {
  1267. if (DebugAutoInstall == true)
  1268. std::clog << OutputInDepth(Depth) << Start << " can't be satisfied!" << std::endl;
  1269. // the dependency is critical, but can't be installed, so discard the candidate
  1270. // as the problemresolver will trip over it otherwise trying to install it (#735967)
  1271. if (Pkg->CurrentVer != 0 && (PkgState[Pkg->ID].iFlags & Protected) != Protected)
  1272. SetCandidateVersion(Pkg.CurrentVer());
  1273. return false;
  1274. }
  1275. }
  1276. return true;
  1277. }
  1278. /*}}}*/
  1279. // DepCache::SetReInstall - Set the reinstallation flag /*{{{*/
  1280. // ---------------------------------------------------------------------
  1281. /* */
  1282. void pkgDepCache::SetReInstall(PkgIterator const &Pkg,bool To)
  1283. {
  1284. if (unlikely(Pkg.end() == true))
  1285. return;
  1286. APT::PackageList pkglist;
  1287. if (Pkg->CurrentVer != 0 &&
  1288. (Pkg.CurrentVer()-> MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
  1289. {
  1290. pkgCache::GrpIterator Grp = Pkg.Group();
  1291. for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false; P = Grp.NextPkg(P))
  1292. {
  1293. if (P->CurrentVer != 0)
  1294. pkglist.insert(P);
  1295. }
  1296. }
  1297. else
  1298. pkglist.insert(Pkg);
  1299. ActionGroup group(*this);
  1300. for (APT::PackageList::const_iterator Pkg = pkglist.begin(); Pkg != pkglist.end(); ++Pkg)
  1301. {
  1302. RemoveSizes(Pkg);
  1303. RemoveStates(Pkg);
  1304. StateCache &P = PkgState[Pkg->ID];
  1305. if (To == true)
  1306. P.iFlags |= ReInstall;
  1307. else
  1308. P.iFlags &= ~ReInstall;
  1309. AddStates(Pkg);
  1310. AddSizes(Pkg);
  1311. }
  1312. }
  1313. /*}}}*/
  1314. pkgCache::VerIterator pkgDepCache::GetCandidateVersion(PkgIterator const &Pkg)/*{{{*/
  1315. {
  1316. return PkgState[Pkg->ID].CandidateVerIter(*this);
  1317. }
  1318. /*}}}*/
  1319. // DepCache::SetCandidateVersion - Change the candidate version /*{{{*/
  1320. // ---------------------------------------------------------------------
  1321. /* */
  1322. void pkgDepCache::SetCandidateVersion(VerIterator TargetVer)
  1323. {
  1324. pkgCache::PkgIterator Pkg = TargetVer.ParentPkg();
  1325. StateCache &P = PkgState[Pkg->ID];
  1326. if (P.CandidateVer == TargetVer)
  1327. return;
  1328. ActionGroup group(*this);
  1329. RemoveSizes(Pkg);
  1330. RemoveStates(Pkg);
  1331. if (P.CandidateVer == P.InstallVer && P.Install() == true)
  1332. P.InstallVer = (Version *)TargetVer;
  1333. P.CandidateVer = (Version *)TargetVer;
  1334. P.Update(Pkg,*this);
  1335. AddStates(Pkg);
  1336. Update(Pkg);
  1337. AddSizes(Pkg);
  1338. }
  1339. /*}}}*/
  1340. // DepCache::SetCandidateRelease - Change the candidate version /*{{{*/
  1341. // ---------------------------------------------------------------------
  1342. /* changes the candidate of a package and walks over all its dependencies
  1343. to check if it needs to change the candidate of the dependency, too,
  1344. to reach a installable versionstate */
  1345. bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer,
  1346. std::string const &TargetRel)
  1347. {
  1348. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > Changed;
  1349. return SetCandidateRelease(TargetVer, TargetRel, Changed);
  1350. }
  1351. bool pkgDepCache::SetCandidateRelease(pkgCache::VerIterator TargetVer,
  1352. std::string const &TargetRel,
  1353. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> > &Changed)
  1354. {
  1355. ActionGroup group(*this);
  1356. SetCandidateVersion(TargetVer);
  1357. if (TargetRel == "installed" || TargetRel == "candidate") // both doesn't make sense in this context
  1358. return true;
  1359. pkgVersionMatch Match(TargetRel, pkgVersionMatch::Release);
  1360. // save the position of the last element we will not undo - if we have to
  1361. std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::iterator newChanged = --(Changed.end());
  1362. for (pkgCache::DepIterator D = TargetVer.DependsList(); D.end() == false; ++D)
  1363. {
  1364. if (D->Type != pkgCache::Dep::PreDepends && D->Type != pkgCache::Dep::Depends &&
  1365. ((D->Type != pkgCache::Dep::Recommends && D->Type != pkgCache::Dep::Suggests) ||
  1366. IsImportantDep(D) == false))
  1367. continue;
  1368. // walk over an or-group and check if we need to do anything
  1369. // for simpilicity no or-group is handled as a or-group including one dependency
  1370. pkgCache::DepIterator Start = D;
  1371. bool itsFine = false;
  1372. for (bool stillOr = true; stillOr == true; ++Start)
  1373. {
  1374. stillOr = (Start->CompareOp & Dep::Or) == Dep::Or;
  1375. pkgCache::PkgIterator const P = Start.TargetPkg();
  1376. // virtual packages can't be a solution
  1377. if (P.end() == true || (P->ProvidesList == 0 && P->VersionList == 0))
  1378. continue;
  1379. pkgCache::VerIterator const Cand = PkgState[P->ID].CandidateVerIter(*this);
  1380. // no versioned dependency - but is it installable?
  1381. if (Start.TargetVer() == 0 || Start.TargetVer()[0] == '\0')
  1382. {
  1383. // Check if one of the providers is installable
  1384. if (P->ProvidesList != 0)
  1385. {
  1386. pkgCache::PrvIterator Prv = P.ProvidesList();
  1387. for (; Prv.end() == false; ++Prv)
  1388. {
  1389. pkgCache::VerIterator const C = PkgState[Prv.OwnerPkg()->ID].CandidateVerIter(*this);
  1390. if (C.end() == true || C != Prv.OwnerVer() ||
  1391. (VersionState(C.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) != DepCandMin)
  1392. continue;
  1393. break;
  1394. }
  1395. if (Prv.end() == true)
  1396. continue;
  1397. }
  1398. // no providers, so check if we have an installable candidate version
  1399. else if (Cand.end() == true ||
  1400. (VersionState(Cand.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) != DepCandMin)
  1401. continue;
  1402. itsFine = true;
  1403. break;
  1404. }
  1405. if (Cand.end() == true)
  1406. continue;
  1407. // check if the current candidate is enough for the versioned dependency - and installable?
  1408. if (Start.IsSatisfied(Cand) == true &&
  1409. (VersionState(Cand.DependsList(), DepInstall, DepCandMin, DepCandPolicy) & DepCandMin) == DepCandMin)
  1410. {
  1411. itsFine = true;
  1412. break;
  1413. }
  1414. }
  1415. if (itsFine == true) {
  1416. // something in the or-group was fine, skip all other members
  1417. for (; (D->CompareOp & Dep::Or) == Dep::Or; ++D);
  1418. continue;
  1419. }
  1420. // walk again over the or-group and check each if a candidate switch would help
  1421. itsFine = false;
  1422. for (bool stillOr = true; stillOr == true; ++D)
  1423. {
  1424. stillOr = (D->CompareOp & Dep::Or) == Dep::Or;
  1425. // changing candidate will not help if the dependency is not versioned
  1426. if (D.TargetVer() == 0 || D.TargetVer()[0] == '\0')
  1427. {
  1428. if (stillOr == true)
  1429. continue;
  1430. break;
  1431. }
  1432. pkgCache::VerIterator V;
  1433. if (TargetRel == "newest")
  1434. V = D.TargetPkg().VersionList();
  1435. else
  1436. V = Match.Find(D.TargetPkg());
  1437. // check if the version from this release could satisfy the dependency
  1438. if (V.end() == true || D.IsSatisfied(V) == false)
  1439. {
  1440. if (stillOr == true)
  1441. continue;
  1442. break;
  1443. }
  1444. pkgCache::VerIterator oldCand = PkgState[D.TargetPkg()->ID].CandidateVerIter(*this);
  1445. if (V == oldCand)
  1446. {
  1447. // Do we already touched this Version? If so, their versioned dependencies are okay, no need to check again
  1448. for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = Changed.begin();
  1449. c != Changed.end(); ++c)
  1450. {
  1451. if (c->first->ParentPkg != V->ParentPkg)
  1452. continue;
  1453. itsFine = true;
  1454. break;
  1455. }
  1456. }
  1457. if (itsFine == false)
  1458. {
  1459. // change the candidate
  1460. Changed.push_back(make_pair(V, TargetVer));
  1461. if (SetCandidateRelease(V, TargetRel, Changed) == false)
  1462. {
  1463. if (stillOr == false)
  1464. break;
  1465. // undo the candidate changing
  1466. SetCandidateVersion(oldCand);
  1467. Changed.pop_back();
  1468. continue;
  1469. }
  1470. itsFine = true;
  1471. }
  1472. // something in the or-group was fine, skip all other members
  1473. for (; (D->CompareOp & Dep::Or) == Dep::Or; ++D);
  1474. break;
  1475. }
  1476. if (itsFine == false && (D->Type == pkgCache::Dep::PreDepends || D->Type == pkgCache::Dep::Depends))
  1477. {
  1478. // undo all changes which aren't lead to a solution
  1479. for (std::list<std::pair<pkgCache::VerIterator, pkgCache::VerIterator> >::const_iterator c = ++newChanged;
  1480. c != Changed.end(); ++c)
  1481. SetCandidateVersion(c->first);
  1482. Changed.erase(newChanged, Changed.end());
  1483. return false;
  1484. }
  1485. }
  1486. return true;
  1487. }
  1488. /*}}}*/
  1489. // DepCache::MarkAuto - set the Auto flag for a package /*{{{*/
  1490. // ---------------------------------------------------------------------
  1491. /* */
  1492. void pkgDepCache::MarkAuto(const PkgIterator &Pkg, bool Auto)
  1493. {
  1494. StateCache &state = PkgState[Pkg->ID];
  1495. ActionGroup group(*this);
  1496. if(Auto)
  1497. state.Flags |= Flag::Auto;
  1498. else
  1499. state.Flags &= ~Flag::Auto;
  1500. }
  1501. /*}}}*/
  1502. // StateCache::Update - Compute the various static display things /*{{{*/
  1503. // ---------------------------------------------------------------------
  1504. /* This is called whenever the Candidate version changes. */
  1505. void pkgDepCache::StateCache::Update(PkgIterator Pkg,pkgCache &Cache)
  1506. {
  1507. // Some info
  1508. VerIterator Ver = CandidateVerIter(Cache);
  1509. // Use a null string or the version string
  1510. if (Ver.end() == true)
  1511. CandVersion = "";
  1512. else
  1513. CandVersion = Ver.VerStr();
  1514. // Find the current version
  1515. CurVersion = "";
  1516. if (Pkg->CurrentVer != 0)
  1517. CurVersion = Pkg.CurrentVer().VerStr();
  1518. // Strip off the epochs for display
  1519. CurVersion = StripEpoch(CurVersion);
  1520. CandVersion = StripEpoch(CandVersion);
  1521. // Figure out if its up or down or equal
  1522. Status = Ver.CompareVer(Pkg.CurrentVer());
  1523. if (Pkg->CurrentVer == 0 || Pkg->VersionList == 0 || CandidateVer == 0)
  1524. Status = 2;
  1525. }
  1526. /*}}}*/
  1527. // StateCache::StripEpoch - Remove the epoch specifier from the version /*{{{*/
  1528. // ---------------------------------------------------------------------
  1529. /* */
  1530. const char *pkgDepCache::StateCache::StripEpoch(const char *Ver)
  1531. {
  1532. if (Ver == 0)
  1533. return 0;
  1534. // Strip any epoch
  1535. char const * const I = strchr(Ver, ':');
  1536. if (I == nullptr)
  1537. return Ver;
  1538. return I + 1;
  1539. }
  1540. /*}}}*/
  1541. // Policy::GetCandidateVer - Returns the Candidate install version /*{{{*/
  1542. // ---------------------------------------------------------------------
  1543. /* The default just returns the highest available version that is not
  1544. a source and automatic. */
  1545. pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator const &Pkg)
  1546. {
  1547. /* Not source/not automatic versions cannot be a candidate version
  1548. unless they are already installed */
  1549. VerIterator Last;
  1550. for (VerIterator I = Pkg.VersionList(); I.end() == false; ++I)
  1551. {
  1552. if (Pkg.CurrentVer() == I)
  1553. return I;
  1554. for (VerFileIterator J = I.FileList(); J.end() == false; ++J)
  1555. {
  1556. if (J.File().Flagged(Flag::NotSource))
  1557. continue;
  1558. /* Stash the highest version of a not-automatic source, we use it
  1559. if there is nothing better */
  1560. if (J.File().Flagged(Flag::NotAutomatic) ||
  1561. J.File().Flagged(Flag::ButAutomaticUpgrades))
  1562. {
  1563. if (Last.end() == true)
  1564. Last = I;
  1565. continue;
  1566. }
  1567. return I;
  1568. }
  1569. }
  1570. return Last;
  1571. }
  1572. /*}}}*/
  1573. // Policy::IsImportantDep - True if the dependency is important /*{{{*/
  1574. // ---------------------------------------------------------------------
  1575. /* */
  1576. bool pkgDepCache::Policy::IsImportantDep(DepIterator const &Dep) const
  1577. {
  1578. if(Dep.IsCritical())
  1579. return true;
  1580. else if(Dep->Type == pkgCache::Dep::Recommends)
  1581. {
  1582. if (InstallRecommends)
  1583. return true;
  1584. // we suport a special mode to only install-recommends for certain
  1585. // sections
  1586. // FIXME: this is a meant as a temporarly solution until the
  1587. // recommends are cleaned up
  1588. const char *sec = Dep.ParentVer().Section();
  1589. if (sec && ConfigValueInSubTree("APT::Install-Recommends-Sections", sec))
  1590. return true;
  1591. }
  1592. else if(Dep->Type == pkgCache::Dep::Suggests)
  1593. return InstallSuggests;
  1594. return false;
  1595. }
  1596. /*}}}*/
  1597. // Policy::GetPriority - Get the priority of the package pin /*{{{*/
  1598. APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgIterator const &/*Pkg*/)
  1599. { return 0; }
  1600. APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::VerIterator const &/*Ver*/, bool /*ConsiderFiles*/)
  1601. { return 0; }
  1602. APT_CONST signed short pkgDepCache::Policy::GetPriority(pkgCache::PkgFileIterator const &/*File*/)
  1603. { return 0; }
  1604. /*}}}*/
  1605. pkgDepCache::InRootSetFunc *pkgDepCache::GetRootSetFunc() /*{{{*/
  1606. {
  1607. DefaultRootSetFunc *f = new DefaultRootSetFunc;
  1608. if(f->wasConstructedSuccessfully())
  1609. return f;
  1610. else
  1611. {
  1612. delete f;
  1613. return NULL;
  1614. }
  1615. }
  1616. /*}}}*/
  1617. bool pkgDepCache::MarkFollowsRecommends()
  1618. {
  1619. return _config->FindB("APT::AutoRemove::RecommendsImportant", true);
  1620. }
  1621. bool pkgDepCache::MarkFollowsSuggests()
  1622. {
  1623. return _config->FindB("APT::AutoRemove::SuggestsImportant", true);
  1624. }
  1625. // pkgDepCache::MarkRequired - the main mark algorithm /*{{{*/
  1626. bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
  1627. {
  1628. if (_config->Find("APT::Solver", "internal") != "internal")
  1629. return true;
  1630. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  1631. // init the states
  1632. map_id_t const PackagesCount = Head().PackageCount;
  1633. for(map_id_t i = 0; i < PackagesCount; ++i)
  1634. {
  1635. PkgState[i].Marked = false;
  1636. PkgState[i].Garbage = false;
  1637. }
  1638. if (debug_autoremove)
  1639. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1640. if(PkgState[p->ID].Flags & Flag::Auto)
  1641. std::clog << "AutoDep: " << p.FullName() << std::endl;
  1642. bool const follow_recommends = MarkFollowsRecommends();
  1643. bool const follow_suggests = MarkFollowsSuggests();
  1644. // do the mark part, this is the core bit of the algorithm
  1645. for(PkgIterator p = PkgBegin(); !p.end(); ++p)
  1646. {
  1647. if(!(PkgState[p->ID].Flags & Flag::Auto) ||
  1648. (p->Flags & Flag::Essential) ||
  1649. (p->Flags & Flag::Important) ||
  1650. userFunc.InRootSet(p) ||
  1651. // be nice even then a required package violates the policy (#583517)
  1652. // and do the full mark process also for required packages
  1653. (p.CurrentVer().end() != true &&
  1654. p.CurrentVer()->Priority == pkgCache::State::Required) ||
  1655. // packages which can't be changed (like holds) can't be garbage
  1656. (IsModeChangeOk(ModeGarbage, p, 0, false) == false))
  1657. {
  1658. // the package is installed (and set to keep)
  1659. if(PkgState[p->ID].Keep() && !p.CurrentVer().end())
  1660. MarkPackage(p, p.CurrentVer(),
  1661. follow_recommends, follow_suggests);
  1662. // the package is to be installed
  1663. else if(PkgState[p->ID].Install())
  1664. MarkPackage(p, PkgState[p->ID].InstVerIter(*this),
  1665. follow_recommends, follow_suggests);
  1666. }
  1667. }
  1668. return true;
  1669. }
  1670. /*}}}*/
  1671. // MarkPackage - mark a single package in Mark-and-Sweep /*{{{*/
  1672. void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
  1673. const pkgCache::VerIterator &ver,
  1674. bool const &follow_recommends,
  1675. bool const &follow_suggests)
  1676. {
  1677. pkgDepCache::StateCache &state = PkgState[pkg->ID];
  1678. // if we are marked already we are done
  1679. if(state.Marked)
  1680. return;
  1681. VerIterator const currver = pkg.CurrentVer();
  1682. VerIterator const instver = state.InstVerIter(*this);
  1683. #if 0
  1684. VerIterator const candver = state.CandidateVerIter(*this);
  1685. // If a package was garbage-collected but is now being marked, we
  1686. // should re-select it
  1687. // For cases when a pkg is set to upgrade and this trigger the
  1688. // removal of a no-longer used dependency. if the pkg is set to
  1689. // keep again later it will result in broken deps
  1690. if(state.Delete() && state.RemoveReason = Unused)
  1691. {
  1692. if(ver==candver)
  1693. mark_install(pkg, false, false, NULL);
  1694. else if(ver==pkg.CurrentVer())
  1695. MarkKeep(pkg, false, false);
  1696. instver=state.InstVerIter(*this);
  1697. }
  1698. #endif
  1699. // For packages that are not going to be removed, ignore versions
  1700. // other than the InstVer. For packages that are going to be
  1701. // removed, ignore versions other than the current version.
  1702. if(!(ver == instver && !instver.end()) &&
  1703. !(ver == currver && instver.end() && !ver.end()))
  1704. return;
  1705. bool const debug_autoremove = _config->FindB("Debug::pkgAutoRemove", false);
  1706. if(debug_autoremove)
  1707. {
  1708. std::clog << "Marking: " << pkg.FullName();
  1709. if(!ver.end())
  1710. std::clog << " " << ver.VerStr();
  1711. if(!currver.end())
  1712. std::clog << ", Curr=" << currver.VerStr();
  1713. if(!instver.end())
  1714. std::clog << ", Inst=" << instver.VerStr();
  1715. std::clog << std::endl;
  1716. }
  1717. state.Marked=true;
  1718. if(ver.end() == true)
  1719. return;
  1720. for(DepIterator d = ver.DependsList(); !d.end(); ++d)
  1721. {
  1722. if(d->Type == Dep::Depends ||
  1723. d->Type == Dep::PreDepends ||
  1724. (follow_recommends &&
  1725. d->Type == Dep::Recommends) ||
  1726. (follow_suggests &&
  1727. d->Type == Dep::Suggests))
  1728. {
  1729. // Try all versions of this package.
  1730. for(VerIterator V = d.TargetPkg().VersionList();
  1731. !V.end(); ++V)
  1732. {
  1733. if(d.IsSatisfied(V))
  1734. {
  1735. if(debug_autoremove)
  1736. {
  1737. std::clog << "Following dep: " << d.ParentPkg().FullName()
  1738. << " " << d.ParentVer().VerStr() << " "
  1739. << d.DepType() << " " << d.TargetPkg().FullName();
  1740. if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
  1741. {
  1742. std::clog << " (" << d.CompType() << " "
  1743. << d.TargetVer() << ")";
  1744. }
  1745. std::clog << std::endl;
  1746. }
  1747. MarkPackage(V.ParentPkg(), V,
  1748. follow_recommends, follow_suggests);
  1749. }
  1750. }
  1751. // Now try virtual packages
  1752. for(PrvIterator prv=d.TargetPkg().ProvidesList();
  1753. !prv.end(); ++prv)
  1754. {
  1755. if(d.IsSatisfied(prv))
  1756. {
  1757. if(debug_autoremove)
  1758. {
  1759. std::clog << "Following dep: " << d.ParentPkg().FullName() << " "
  1760. << d.ParentVer().VerStr() << " "
  1761. << d.DepType() << " " << d.TargetPkg().FullName() << " ";
  1762. if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
  1763. {
  1764. std::clog << " (" << d.CompType() << " "
  1765. << d.TargetVer() << ")";
  1766. }
  1767. std::clog << ", provided by "
  1768. << prv.OwnerPkg().FullName() << " "
  1769. << prv.OwnerVer().VerStr()
  1770. << std::endl;
  1771. }
  1772. MarkPackage(prv.OwnerPkg(), prv.OwnerVer(),
  1773. follow_recommends, follow_suggests);
  1774. }
  1775. }
  1776. }
  1777. }
  1778. }
  1779. /*}}}*/
  1780. bool pkgDepCache::Sweep() /*{{{*/
  1781. {
  1782. bool debug_autoremove = _config->FindB("Debug::pkgAutoRemove",false);
  1783. // do the sweep
  1784. for(PkgIterator p=PkgBegin(); !p.end(); ++p)
  1785. {
  1786. StateCache &state=PkgState[p->ID];
  1787. // skip required packages
  1788. if (!p.CurrentVer().end() &&
  1789. (p.CurrentVer()->Priority == pkgCache::State::Required))
  1790. continue;
  1791. // if it is not marked and it is installed, it's garbage
  1792. if(!state.Marked && (!p.CurrentVer().end() || state.Install()))
  1793. {
  1794. state.Garbage=true;
  1795. if(debug_autoremove)
  1796. std::clog << "Garbage: " << p.FullName() << std::endl;
  1797. }
  1798. }
  1799. return true;
  1800. }
  1801. /*}}}*/
  1802. // DepCache::MarkAndSweep /*{{{*/
  1803. bool pkgDepCache::MarkAndSweep(InRootSetFunc &rootFunc)
  1804. {
  1805. return MarkRequired(rootFunc) && Sweep();
  1806. }
  1807. bool pkgDepCache::MarkAndSweep()
  1808. {
  1809. std::unique_ptr<InRootSetFunc> f(GetRootSetFunc());
  1810. if(f.get() != NULL)
  1811. return MarkAndSweep(*f.get());
  1812. else
  1813. return false;
  1814. }
  1815. /*}}}*/