policy.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: policy.cc,v 1.10 2003/08/12 00:17:37 mdz Exp $
  4. /* ######################################################################
  5. Package Version Policy implementation
  6. This is just a really simple wrapper around pkgVersionMatch with
  7. some added goodies to manage the list of things..
  8. Priority Table:
  9. 1000 -> inf = Downgradeable priorities
  10. 1000 = The 'no downgrade' pseduo-status file
  11. 100 -> 1000 = Standard priorities
  12. 990 = Config file override package files
  13. 989 = Start for preference auto-priorities
  14. 500 = Default package files
  15. 100 = The status file
  16. 0 -> 100 = NotAutomatic sources like experimental
  17. -inf -> 0 = Never selected
  18. ##################################################################### */
  19. /*}}}*/
  20. // Include Files /*{{{*/
  21. #ifdef __GNUG__
  22. #pragma implementation "apt-pkg/policy.h"
  23. #endif
  24. #include <apt-pkg/policy.h>
  25. #include <apt-pkg/configuration.h>
  26. #include <apt-pkg/tagfile.h>
  27. #include <apt-pkg/strutl.h>
  28. #include <apt-pkg/error.h>
  29. #include <apt-pkg/sptr.h>
  30. #include <apti18n.h>
  31. #include <iostream>
  32. #include <sstream>
  33. /*}}}*/
  34. using namespace std;
  35. // Policy::Init - Startup and bind to a cache /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* Set the defaults for operation. The default mode with no loaded policy
  38. file matches the V0 policy engine. */
  39. pkgPolicy::pkgPolicy(pkgCache *Owner) : Pins(0), PFPriority(0), Cache(Owner)
  40. {
  41. PFPriority = new signed short[Owner->Head().PackageFileCount];
  42. Pins = new Pin[Owner->Head().PackageCount];
  43. for (unsigned long I = 0; I != Owner->Head().PackageCount; I++)
  44. Pins[I].Type = pkgVersionMatch::None;
  45. // The config file has a master override.
  46. string DefRel = _config->Find("APT::Default-Release");
  47. if (DefRel.empty() == false)
  48. CreatePin(pkgVersionMatch::Release,"",DefRel,990);
  49. InitDefaults();
  50. }
  51. /*}}}*/
  52. // Policy::InitDefaults - Compute the default selections /*{{{*/
  53. // ---------------------------------------------------------------------
  54. /* */
  55. bool pkgPolicy::InitDefaults()
  56. {
  57. // Initialize the priorities based on the status of the package file
  58. for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I != Cache->FileEnd(); I++)
  59. {
  60. PFPriority[I->ID] = 500;
  61. if ((I->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
  62. PFPriority[I->ID] = 100;
  63. else
  64. if ((I->Flags & pkgCache::Flag::NotAutomatic) == pkgCache::Flag::NotAutomatic)
  65. PFPriority[I->ID] = 1;
  66. }
  67. // Apply the defaults..
  68. SPtrArray<bool> Fixed = new bool[Cache->HeaderP->PackageFileCount];
  69. memset(Fixed,0,sizeof(*Fixed)*Cache->HeaderP->PackageFileCount);
  70. signed Cur = 989;
  71. StatusOverride = false;
  72. for (vector<Pin>::const_iterator I = Defaults.begin(); I != Defaults.end();
  73. I++, Cur--)
  74. {
  75. pkgVersionMatch Match(I->Data,I->Type);
  76. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++)
  77. {
  78. if (Match.FileMatch(F) == true && Fixed[F->ID] == false)
  79. {
  80. if (I->Priority != 0 && I->Priority > 0)
  81. Cur = I->Priority;
  82. if (I->Priority < 0)
  83. PFPriority[F->ID] = I->Priority;
  84. else
  85. PFPriority[F->ID] = Cur;
  86. if (PFPriority[F->ID] > 1000)
  87. StatusOverride = true;
  88. Fixed[F->ID] = true;
  89. }
  90. }
  91. }
  92. if (_config->FindB("Debug::pkgPolicy",false) == true)
  93. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++)
  94. cout << "Prio of " << F.FileName() << ' ' << PFPriority[F->ID] << endl;
  95. return true;
  96. }
  97. /*}}}*/
  98. // Policy::GetCandidateVer - Get the candidate install version /*{{{*/
  99. // ---------------------------------------------------------------------
  100. /* Evaluate the package pins and the default list to deteremine what the
  101. best package is. */
  102. pkgCache::VerIterator pkgPolicy::GetCandidateVer(pkgCache::PkgIterator Pkg)
  103. {
  104. // Look for a package pin and evaluate it.
  105. signed Max = GetPriority(Pkg);
  106. pkgCache::VerIterator Pref = GetMatch(Pkg);
  107. /* Falling through to the default version.. Setting Max to zero
  108. effectively excludes everything <= 0 which are the non-automatic
  109. priorities.. The status file is given a prio of 100 which will exclude
  110. not-automatic sources, except in a single shot not-installed mode.
  111. The second pseduo-status file is at prio 1000, above which will permit
  112. the user to force-downgrade things.
  113. The user pin is subject to the same priority rules as default
  114. selections. Thus there are two ways to create a pin - a pin that
  115. tracks the default when the default is taken away, and a permanent
  116. pin that stays at that setting.
  117. */
  118. for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; Ver++)
  119. {
  120. for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
  121. {
  122. /* If this is the status file, and the current version is not the
  123. version in the status file (ie it is not installed, or somesuch)
  124. then it is not a candidate for installation, ever. This weeds
  125. out bogus entries that may be due to config-file states, or
  126. other. */
  127. if ((VF.File()->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource &&
  128. Pkg.CurrentVer() != Ver)
  129. continue;
  130. signed Prio = PFPriority[VF.File()->ID];
  131. if (Prio > Max)
  132. {
  133. Pref = Ver;
  134. Max = Prio;
  135. }
  136. }
  137. if (Pkg.CurrentVer() == Ver && Max < 1000)
  138. {
  139. /* Elevate our current selection (or the status file itself)
  140. to the Pseudo-status priority. */
  141. if (Pref.end() == true)
  142. Pref = Ver;
  143. Max = 1000;
  144. // Fast path optimize.
  145. if (StatusOverride == false)
  146. break;
  147. }
  148. }
  149. return Pref;
  150. }
  151. /*}}}*/
  152. // Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
  153. // ---------------------------------------------------------------------
  154. /* For performance we have 3 tables, the default table, the main cache
  155. table (hashed to the cache). A blank package name indicates the pin
  156. belongs to the default table. Order of insertion matters here, the
  157. earlier defaults override later ones. */
  158. void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name,
  159. string Data,signed short Priority)
  160. {
  161. Pin *P = 0;
  162. if (Name.empty() == true)
  163. P = &*Defaults.insert(Defaults.end(),PkgPin());
  164. else
  165. {
  166. // Get a spot to put the pin
  167. pkgCache::PkgIterator Pkg = Cache->FindPkg(Name);
  168. if (Pkg.end() == true)
  169. {
  170. // Check the unmatched table
  171. for (vector<PkgPin>::iterator I = Unmatched.begin();
  172. I != Unmatched.end() && P == 0; I++)
  173. if (I->Pkg == Name)
  174. P = &*I;
  175. if (P == 0)
  176. P = &*Unmatched.insert(Unmatched.end(),PkgPin());
  177. }
  178. else
  179. {
  180. P = Pins + Pkg->ID;
  181. }
  182. }
  183. // Set..
  184. P->Type = Type;
  185. P->Priority = Priority;
  186. P->Data = Data;
  187. }
  188. /*}}}*/
  189. // Policy::GetMatch - Get the matching version for a package pin /*{{{*/
  190. // ---------------------------------------------------------------------
  191. /* */
  192. pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator Pkg)
  193. {
  194. const Pin &PPkg = Pins[Pkg->ID];
  195. if (PPkg.Type != pkgVersionMatch::None)
  196. {
  197. pkgVersionMatch Match(PPkg.Data,PPkg.Type);
  198. return Match.Find(Pkg);
  199. }
  200. return pkgCache::VerIterator(*Pkg.Cache());
  201. }
  202. /*}}}*/
  203. // Policy::GetPriority - Get the priority of the package pin /*{{{*/
  204. // ---------------------------------------------------------------------
  205. /* */
  206. signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg)
  207. {
  208. if (Pins[Pkg->ID].Type != pkgVersionMatch::None)
  209. {
  210. // In this case 0 means default priority
  211. if (Pins[Pkg->ID].Priority == 0)
  212. return 989;
  213. return Pins[Pkg->ID].Priority;
  214. }
  215. return 0;
  216. }
  217. /*}}}*/
  218. // ReadPinFile - Load the pin file into a Policy /*{{{*/
  219. // ---------------------------------------------------------------------
  220. /* I'd like to see the preferences file store more than just pin information
  221. but right now that is the only stuff I have to store. Later there will
  222. have to be some kind of combined super parser to get the data into all
  223. the right classes.. */
  224. bool ReadPinFile(pkgPolicy &Plcy,string File)
  225. {
  226. if (File.empty() == true)
  227. File = _config->FindFile("Dir::Etc::Preferences");
  228. if (FileExists(File) == false)
  229. return true;
  230. FileFd Fd(File,FileFd::ReadOnly);
  231. pkgTagFile TF(&Fd);
  232. if (_error->PendingError() == true)
  233. return false;
  234. pkgTagSection Tags;
  235. while (TF.Step(Tags) == true)
  236. {
  237. string Name = Tags.FindS("Package");
  238. if (Name.empty() == true)
  239. return _error->Error(_("Invalid record in the preferences file, no Package header"));
  240. if (Name == "*")
  241. Name = string();
  242. const char *Start;
  243. const char *End;
  244. if (Tags.Find("Pin",Start,End) == false)
  245. continue;
  246. const char *Word = Start;
  247. for (; Word != End && isspace(*Word) == 0; Word++);
  248. // Parse the type..
  249. pkgVersionMatch::MatchType Type;
  250. if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
  251. Type = pkgVersionMatch::Version;
  252. else if (stringcasecmp(Start,Word,"release") == 0)
  253. Type = pkgVersionMatch::Release;
  254. else if (stringcasecmp(Start,Word,"origin") == 0)
  255. Type = pkgVersionMatch::Origin;
  256. else
  257. {
  258. _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
  259. continue;
  260. }
  261. for (; Word != End && isspace(*Word) != 0; Word++);
  262. short int priority = Tags.FindI("Pin-Priority", 0);
  263. if (priority == 0)
  264. {
  265. _error->Warning(_("No priority (or zero) specified for pin"));
  266. continue;
  267. }
  268. istringstream s(Name);
  269. string pkg;
  270. while(!s.eof())
  271. {
  272. s >> pkg;
  273. Plcy.CreatePin(Type, pkg, string(Word,End),priority);
  274. };
  275. }
  276. Plcy.InitDefaults();
  277. return true;
  278. }
  279. /*}}}*/