policy.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: policy.cc,v 1.3 2001/03/03 23:27:24 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. /* This is a side effect of the condition below, and is added for
  130. completeness, though it should not be necessary */
  131. if (Ver->Arch == 0)
  132. continue;
  133. for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
  134. {
  135. /* If this is the status file, and the current version is not the
  136. version in the status file (ie it is not installed, or somesuch)
  137. then it is not a candidate for installation, ever. This weeds
  138. out bogus entries that may be due to config-file states, or
  139. other. */
  140. if ((VF.File()->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource &&
  141. Pkg.CurrentVer() != Ver)
  142. continue;
  143. signed Prio = PFPriority[VF.File()->ID];
  144. if (Prio > Max)
  145. {
  146. Pref = Ver;
  147. Max = Prio;
  148. }
  149. }
  150. if (Pkg.CurrentVer() == Ver && Max < 1000)
  151. {
  152. /* Elevate our current selection (or the status file itself)
  153. to the Pseudo-status priority. */
  154. if (Pref.end() == true)
  155. Pref = Ver;
  156. Max = 1000;
  157. // Fast path optimize.
  158. if (StatusOverride == false)
  159. break;
  160. }
  161. }
  162. return Pref;
  163. }
  164. /*}}}*/
  165. // Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
  166. // ---------------------------------------------------------------------
  167. /* For performance we have 3 tables, the default table, the main cache
  168. table (hashed to the cache). A blank package name indicates the pin
  169. belongs to the default table. Order of insertion matters here, the
  170. earlier defaults override later ones. */
  171. void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name,
  172. string Data,signed short Priority)
  173. {
  174. pkgCache::PkgIterator Pkg = Cache->FindPkg(Name);
  175. Pin *P = 0;
  176. if (Name.empty() == true)
  177. P = Defaults.insert(Defaults.end());
  178. else
  179. {
  180. // Get a spot to put the pin
  181. if (Pkg.end() == true)
  182. {
  183. // Check the unmatched table
  184. for (vector<PkgPin>::iterator I = Unmatched.begin();
  185. I != Unmatched.end() && P == 0; I++)
  186. if (I->Pkg == Name)
  187. P = I;
  188. if (P == 0)
  189. P = Unmatched.insert(Unmatched.end());
  190. }
  191. else
  192. {
  193. P = Pins + Pkg->ID;
  194. }
  195. }
  196. // Set..
  197. P->Type = Type;
  198. P->Priority = Priority;
  199. P->Data = Data;
  200. }
  201. /*}}}*/
  202. // ReadPinFile - Load the pin file into a Policy /*{{{*/
  203. // ---------------------------------------------------------------------
  204. /* I'd like to see the preferences file store more than just pin information
  205. but right now that is the only stuff I have to store. Later there will
  206. have to be some kind of combined super parser to get the data into all
  207. the right classes.. */
  208. bool ReadPinFile(pkgPolicy &Plcy,string File)
  209. {
  210. if (File.empty() == true)
  211. File = _config->FindFile("Dir::Etc::Preferences");
  212. if (FileExists(File) == false)
  213. return true;
  214. FileFd Fd(File,FileFd::ReadOnly);
  215. pkgTagFile TF(&Fd);
  216. if (_error->PendingError() == true)
  217. return false;
  218. pkgTagSection Tags;
  219. while (TF.Step(Tags) == true)
  220. {
  221. string Name = Tags.FindS("Package");
  222. if (Name.empty() == true)
  223. return _error->Error(_("Invalid record in the preferences file, no Package header"));
  224. if (Name == "*")
  225. Name = string();
  226. const char *Start;
  227. const char *End;
  228. if (Tags.Find("Pin",Start,End) == false)
  229. continue;
  230. const char *Word = Start;
  231. for (; Word != End && isspace(*Word) == 0; Word++);
  232. // Parse the type..
  233. pkgVersionMatch::MatchType Type;
  234. if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
  235. Type = pkgVersionMatch::Version;
  236. else if (stringcasecmp(Start,Word,"release") == 0)
  237. Type = pkgVersionMatch::Release;
  238. else if (stringcasecmp(Start,Word,"origin") == 0)
  239. Type = pkgVersionMatch::Origin;
  240. else
  241. {
  242. _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
  243. continue;
  244. }
  245. for (; Word != End && isspace(*Word) != 0; Word++);
  246. Plcy.CreatePin(Type,Name,string(Word,End),
  247. Tags.FindI("Pin-Priority"));
  248. }
  249. Plcy.InitDefaults();
  250. return true;
  251. }
  252. /*}}}*/