policy.cc 12 KB

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