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.
 
 
 
 
 
 

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