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.
 
 
 
 
 
 

2041 lines
66 KiB

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