depcache.cc 36 KB

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