policy.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: policy.cc,v 1.7 2001/05/27 05:36:04 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. #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. if (Pref.end() == true)
  149. return Pkg.VersionList();
  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. // tausq:g++v3 begin
  166. ;
  167. // P = Defaults.insert(Defaults.end());
  168. // tausq:g++v3 end
  169. else
  170. {
  171. // Get a spot to put the pin
  172. if (Pkg.end() == true)
  173. {
  174. // Check the unmatched table
  175. for (vector<PkgPin>::iterator I = Unmatched.begin();
  176. I != Unmatched.end() && P == 0; I++)
  177. if (I->Pkg == Name)
  178. // tausq:g++-v3 begin
  179. P = &(*I);
  180. // P = I;
  181. // tausq:g++-v3 end
  182. if (P == 0)
  183. // tausq:g++v3 begin
  184. ;
  185. // P = Unmatched.insert(Unmatched.end());
  186. // tausq:g++v3 end
  187. }
  188. else
  189. {
  190. P = Pins + Pkg->ID;
  191. }
  192. }
  193. // Set..
  194. P->Type = Type;
  195. P->Priority = Priority;
  196. P->Data = Data;
  197. }
  198. /*}}}*/
  199. // Policy::GetMatch - Get the matching version for a package pin /*{{{*/
  200. // ---------------------------------------------------------------------
  201. /* */
  202. pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator Pkg)
  203. {
  204. const Pin &PPkg = Pins[Pkg->ID];
  205. if (PPkg.Type != pkgVersionMatch::None)
  206. {
  207. pkgVersionMatch Match(PPkg.Data,PPkg.Type);
  208. return Match.Find(Pkg);
  209. }
  210. return pkgCache::VerIterator(*Pkg.Cache());
  211. }
  212. /*}}}*/
  213. // Policy::GetPriority - Get the priority of the package pin /*{{{*/
  214. // ---------------------------------------------------------------------
  215. /* */
  216. signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg)
  217. {
  218. if (Pins[Pkg->ID].Type != pkgVersionMatch::None)
  219. {
  220. // In this case 0 means default priority
  221. if (Pins[Pkg->ID].Priority == 0)
  222. return 989;
  223. return Pins[Pkg->ID].Priority;
  224. }
  225. return 0;
  226. }
  227. /*}}}*/
  228. // ReadPinFile - Load the pin file into a Policy /*{{{*/
  229. // ---------------------------------------------------------------------
  230. /* I'd like to see the preferences file store more than just pin information
  231. but right now that is the only stuff I have to store. Later there will
  232. have to be some kind of combined super parser to get the data into all
  233. the right classes.. */
  234. bool ReadPinFile(pkgPolicy &Plcy,string File)
  235. {
  236. if (File.empty() == true)
  237. File = _config->FindFile("Dir::Etc::Preferences");
  238. if (FileExists(File) == false)
  239. return true;
  240. FileFd Fd(File,FileFd::ReadOnly);
  241. pkgTagFile TF(&Fd);
  242. if (_error->PendingError() == true)
  243. return false;
  244. pkgTagSection Tags;
  245. while (TF.Step(Tags) == true)
  246. {
  247. string Name = Tags.FindS("Package");
  248. if (Name.empty() == true)
  249. return _error->Error(_("Invalid record in the preferences file, no Package header"));
  250. if (Name == "*")
  251. Name = string();
  252. const char *Start;
  253. const char *End;
  254. if (Tags.Find("Pin",Start,End) == false)
  255. continue;
  256. const char *Word = Start;
  257. for (; Word != End && isspace(*Word) == 0; Word++);
  258. // Parse the type..
  259. pkgVersionMatch::MatchType Type;
  260. if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
  261. Type = pkgVersionMatch::Version;
  262. else if (stringcasecmp(Start,Word,"release") == 0)
  263. Type = pkgVersionMatch::Release;
  264. else if (stringcasecmp(Start,Word,"origin") == 0)
  265. Type = pkgVersionMatch::Origin;
  266. else
  267. {
  268. _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
  269. continue;
  270. }
  271. for (; Word != End && isspace(*Word) != 0; Word++);
  272. Plcy.CreatePin(Type,Name,string(Word,End),
  273. Tags.FindI("Pin-Priority"));
  274. }
  275. Plcy.InitDefaults();
  276. return true;
  277. }
  278. /*}}}*/