policy.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: policy.cc,v 1.2 2001/02/20 07:03:17 jgg 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. /*}}}*/
  32. // Policy::Init - Startup and bind to a cache /*{{{*/
  33. // ---------------------------------------------------------------------
  34. /* Set the defaults for operation. The default mode with no loaded policy
  35. file matches the V0 policy engine. */
  36. pkgPolicy::pkgPolicy(pkgCache *Owner) : Pins(0), PFPriority(0), Cache(Owner)
  37. {
  38. PFPriority = new signed short[Owner->Head().PackageFileCount];
  39. Pins = new Pin[Owner->Head().PackageCount];
  40. for (unsigned long I = 0; I != Owner->Head().PackageCount; I++)
  41. Pins[I].Type = pkgVersionMatch::None;
  42. // The config file has a master override.
  43. string DefRel = _config->Find("APT::Default-Release");
  44. if (DefRel.empty() == false)
  45. CreatePin(pkgVersionMatch::Release,"",DefRel,990);
  46. InitDefaults();
  47. }
  48. /*}}}*/
  49. // Policy::InitDefaults - Compute the default selections /*{{{*/
  50. // ---------------------------------------------------------------------
  51. /* */
  52. bool pkgPolicy::InitDefaults()
  53. {
  54. // Initialize the priorities based on the status of the package file
  55. for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I != Cache->FileEnd(); I++)
  56. {
  57. PFPriority[I->ID] = 500;
  58. if ((I->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
  59. PFPriority[I->ID] = 100;
  60. else
  61. if ((I->Flags & pkgCache::Flag::NotAutomatic) == pkgCache::Flag::NotAutomatic)
  62. PFPriority[I->ID] = 1;
  63. }
  64. // Apply the defaults..
  65. SPtr<bool> Fixed = new bool[Cache->HeaderP->PackageFileCount];
  66. memset(Fixed,0,sizeof(*Fixed)*Cache->HeaderP->PackageFileCount);
  67. signed Cur = 989;
  68. StatusOverride = false;
  69. for (vector<Pin>::const_iterator I = Defaults.begin(); I != Defaults.end();
  70. I++, Cur--)
  71. {
  72. pkgVersionMatch Match(I->Data,I->Type);
  73. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); F++)
  74. {
  75. /* hmm?
  76. if ((F->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
  77. continue;*/
  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. const Pin &PPkg = Pins[Pkg->ID];
  105. // Look for a package pin and evaluate it.
  106. signed Max = 0;
  107. pkgCache::VerIterator Pref(*Cache);
  108. if (PPkg.Type != pkgVersionMatch::None)
  109. {
  110. pkgVersionMatch Match(PPkg.Data,PPkg.Type);
  111. Pref = Match.Find(Pkg);
  112. Max = PPkg.Priority;
  113. if (PPkg.Priority == 0)
  114. Max = 989;
  115. }
  116. /* Falling through to the default version.. Setting Max to zero
  117. effectively excludes everything <= 0 which are the non-automatic
  118. priorities.. The status file is given a prio of 100 which will exclude
  119. not-automatic sources, except in a single shot not-installed mode.
  120. The second pseduo-status file is at prio 1000, above which will permit
  121. the user to force-downgrade things.
  122. The user pin is subject to the same priority rules as default
  123. selections. Thus there are two ways to create a pin - a pin that
  124. tracks the default when the default is taken away, and a permanent
  125. pin that stays at that setting.
  126. */
  127. for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; Ver++)
  128. {
  129. for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
  130. {
  131. signed Prio = PFPriority[VF.File()->ID];
  132. if (Prio > Max)
  133. {
  134. Pref = Ver;
  135. Max = Prio;
  136. }
  137. }
  138. if (Pkg.CurrentVer() == Ver && Max < 1000)
  139. {
  140. /* Elevate our current selection (or the status file itself)
  141. to the Pseudo-status priority. */
  142. if (Pref.end() == true)
  143. Pref = Ver;
  144. Max = 1000;
  145. // Fast path optimize.
  146. if (StatusOverride == false)
  147. break;
  148. }
  149. }
  150. return Pref;
  151. }
  152. /*}}}*/
  153. // Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
  154. // ---------------------------------------------------------------------
  155. /* For performance we have 3 tables, the default table, the main cache
  156. table (hashed to the cache). A blank package name indicates the pin
  157. belongs to the default table. Order of insertion matters here, the
  158. earlier defaults override later ones. */
  159. void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name,
  160. string Data,signed short Priority)
  161. {
  162. pkgCache::PkgIterator Pkg = Cache->FindPkg(Name);
  163. Pin *P = 0;
  164. if (Name.empty() == true)
  165. P = Defaults.insert(Defaults.end());
  166. else
  167. {
  168. // Get a spot to put the pin
  169. if (Pkg.end() == true)
  170. {
  171. // Check the unmatched table
  172. for (vector<PkgPin>::iterator I = Unmatched.begin();
  173. I != Unmatched.end() && P == 0; I++)
  174. if (I->Pkg == Name)
  175. P = I;
  176. if (P == 0)
  177. P = Unmatched.insert(Unmatched.end());
  178. }
  179. else
  180. {
  181. P = Pins + Pkg->ID;
  182. }
  183. }
  184. // Set..
  185. P->Type = Type;
  186. P->Priority = Priority;
  187. P->Data = Data;
  188. }
  189. /*}}}*/
  190. // ReadPinFile - Load the pin file into a Policy /*{{{*/
  191. // ---------------------------------------------------------------------
  192. /* I'd like to see the preferences file store more than just pin information
  193. but right now that is the only stuff I have to store. Later there will
  194. have to be some kind of combined super parser to get the data into all
  195. the right classes.. */
  196. bool ReadPinFile(pkgPolicy &Plcy,string File)
  197. {
  198. if (File.empty() == true)
  199. File = _config->FindFile("Dir::Etc::Preferences");
  200. if (FileExists(File) == false)
  201. return true;
  202. FileFd Fd(File,FileFd::ReadOnly);
  203. pkgTagFile TF(&Fd);
  204. if (_error->PendingError() == true)
  205. return false;
  206. pkgTagSection Tags;
  207. while (TF.Step(Tags) == true)
  208. {
  209. string Name = Tags.FindS("Package");
  210. if (Name.empty() == true)
  211. return _error->Error(_("Invalid record in the preferences file, no Package header"));
  212. if (Name == "*")
  213. Name = string();
  214. const char *Start;
  215. const char *End;
  216. if (Tags.Find("Pin",Start,End) == false)
  217. continue;
  218. const char *Word = Start;
  219. for (; Word != End && isspace(*Word) == 0; Word++);
  220. // Parse the type..
  221. pkgVersionMatch::MatchType Type;
  222. if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
  223. Type = pkgVersionMatch::Version;
  224. else if (stringcasecmp(Start,Word,"release") == 0)
  225. Type = pkgVersionMatch::Release;
  226. else if (stringcasecmp(Start,Word,"origin") == 0)
  227. Type = pkgVersionMatch::Origin;
  228. else
  229. {
  230. _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
  231. continue;
  232. }
  233. for (; Word != End && isspace(*Word) != 0; Word++);
  234. Plcy.CreatePin(Type,Name,string(Word,End),
  235. Tags.FindI("Pin-Priority"));
  236. }
  237. Plcy.InitDefaults();
  238. return true;
  239. }
  240. /*}}}*/