policy.cc 9.8 KB

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