policy.cc 9.9 KB

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