You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

390 lines
13 KiB

  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. std::clog << "Prio of " << F.FileName() << ' ' << PFPriority[F->ID] << std::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 const &Pkg)
  101. {
  102. // Look for a package pin and evaluate it.
  103. signed Max = GetPriority(Pkg);
  104. pkgCache::VerIterator Pref = GetMatch(Pkg);
  105. // Alternatives in case we can not find our package pin (Bug#512318).
  106. signed MaxAlt = 0;
  107. pkgCache::VerIterator PrefAlt;
  108. // no package = no candidate version
  109. if (Pkg.end() == true)
  110. return Pref;
  111. // packages with a pin lower than 0 have no newer candidate than the current version
  112. if (Max < 0)
  113. return Pkg.CurrentVer();
  114. /* Falling through to the default version.. Setting Max to zero
  115. effectively excludes everything <= 0 which are the non-automatic
  116. priorities.. The status file is given a prio of 100 which will exclude
  117. not-automatic sources, except in a single shot not-installed mode.
  118. The second pseduo-status file is at prio 1000, above which will permit
  119. the user to force-downgrade things.
  120. The user pin is subject to the same priority rules as default
  121. selections. Thus there are two ways to create a pin - a pin that
  122. tracks the default when the default is taken away, and a permanent
  123. pin that stays at that setting.
  124. */
  125. for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() == false; Ver++)
  126. {
  127. /* Lets see if this version is the installed version */
  128. bool instVer = (Pkg.CurrentVer() == Ver);
  129. if (Ver.Pseudo() == true && instVer == false)
  130. {
  131. pkgCache::PkgIterator const allPkg = Ver.ParentPkg().Group().FindPkg("all");
  132. if (allPkg->CurrentVer != 0 && allPkg.CurrentVer()->Hash == Ver->Hash &&
  133. strcmp(allPkg.CurVersion(), Ver.VerStr()) == 0)
  134. instVer = true;
  135. }
  136. for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
  137. {
  138. /* If this is the status file, and the current version is not the
  139. version in the status file (ie it is not installed, or somesuch)
  140. then it is not a candidate for installation, ever. This weeds
  141. out bogus entries that may be due to config-file states, or
  142. other. */
  143. if ((VF.File()->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource &&
  144. instVer == false)
  145. continue;
  146. signed Prio = PFPriority[VF.File()->ID];
  147. if (Prio > Max)
  148. {
  149. Pref = Ver;
  150. Max = Prio;
  151. }
  152. if (Prio > MaxAlt)
  153. {
  154. PrefAlt = Ver;
  155. MaxAlt = Prio;
  156. }
  157. }
  158. if (instVer == true && Max < 1000)
  159. {
  160. /* Elevate our current selection (or the status file itself)
  161. to the Pseudo-status priority. */
  162. if (Pref.end() == true)
  163. Pref = Ver;
  164. Max = 1000;
  165. // Fast path optimize.
  166. if (StatusOverride == false)
  167. break;
  168. }
  169. }
  170. // If we do not find our candidate, use the one with the highest pin.
  171. // This means that if there is a version available with pin > 0; there
  172. // will always be a candidate (Closes: #512318)
  173. if (!Pref.IsGood() && MaxAlt > 0)
  174. Pref = PrefAlt;
  175. return Pref;
  176. }
  177. /*}}}*/
  178. // Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
  179. // ---------------------------------------------------------------------
  180. /* For performance we have 3 tables, the default table, the main cache
  181. table (hashed to the cache). A blank package name indicates the pin
  182. belongs to the default table. Order of insertion matters here, the
  183. earlier defaults override later ones. */
  184. void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name,
  185. string Data,signed short Priority)
  186. {
  187. if (Name.empty() == true)
  188. {
  189. Pin *P = &*Defaults.insert(Defaults.end(),PkgPin());
  190. P->Type = Type;
  191. P->Priority = Priority;
  192. P->Data = Data;
  193. return;
  194. }
  195. // Get a spot to put the pin
  196. pkgCache::GrpIterator Grp = Cache->FindGrp(Name);
  197. for (pkgCache::PkgIterator Pkg = Grp.FindPkg("any");
  198. Pkg.end() != true; Pkg = Grp.NextPkg(Pkg))
  199. {
  200. Pin *P = 0;
  201. if (Pkg.end() == false)
  202. P = Pins + Pkg->ID;
  203. else
  204. {
  205. // Check the unmatched table
  206. for (vector<PkgPin>::iterator I = Unmatched.begin();
  207. I != Unmatched.end() && P == 0; I++)
  208. if (I->Pkg == Name)
  209. P = &*I;
  210. if (P == 0)
  211. P = &*Unmatched.insert(Unmatched.end(),PkgPin());
  212. }
  213. P->Type = Type;
  214. P->Priority = Priority;
  215. P->Data = Data;
  216. }
  217. }
  218. /*}}}*/
  219. // Policy::GetMatch - Get the matching version for a package pin /*{{{*/
  220. // ---------------------------------------------------------------------
  221. /* */
  222. pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator const &Pkg)
  223. {
  224. const Pin &PPkg = Pins[Pkg->ID];
  225. if (PPkg.Type == pkgVersionMatch::None)
  226. return pkgCache::VerIterator(*Pkg.Cache());
  227. pkgVersionMatch Match(PPkg.Data,PPkg.Type);
  228. return Match.Find(Pkg);
  229. }
  230. /*}}}*/
  231. // Policy::GetPriority - Get the priority of the package pin /*{{{*/
  232. // ---------------------------------------------------------------------
  233. /* */
  234. signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg)
  235. {
  236. if (Pins[Pkg->ID].Type != pkgVersionMatch::None)
  237. {
  238. // In this case 0 means default priority
  239. if (Pins[Pkg->ID].Priority == 0)
  240. return 989;
  241. return Pins[Pkg->ID].Priority;
  242. }
  243. return 0;
  244. }
  245. /*}}}*/
  246. // PreferenceSection class - Overriding the default TrimRecord method /*{{{*/
  247. // ---------------------------------------------------------------------
  248. /* The preference file is a user generated file so the parser should
  249. therefore be a bit more friendly by allowing comments and new lines
  250. all over the place rather than forcing a special format */
  251. class PreferenceSection : public pkgTagSection
  252. {
  253. void TrimRecord(bool BeforeRecord, const char* &End)
  254. {
  255. for (; Stop < End && (Stop[0] == '\n' || Stop[0] == '\r' || Stop[0] == '#'); Stop++)
  256. if (Stop[0] == '#')
  257. Stop = (const char*) memchr(Stop,'\n',End-Stop);
  258. }
  259. };
  260. /*}}}*/
  261. // ReadPinDir - Load the pin files from this dir into a Policy /*{{{*/
  262. // ---------------------------------------------------------------------
  263. /* This will load each pin file in the given dir into a Policy. If the
  264. given dir is empty the dir set in Dir::Etc::PreferencesParts is used.
  265. Note also that this method will issue a warning if the dir does not
  266. exists but it will return true in this case! */
  267. bool ReadPinDir(pkgPolicy &Plcy,string Dir)
  268. {
  269. if (Dir.empty() == true)
  270. Dir = _config->FindDir("Dir::Etc::PreferencesParts");
  271. if (DirectoryExists(Dir) == false)
  272. {
  273. _error->WarningE("DirectoryExists",_("Unable to read %s"),Dir.c_str());
  274. return true;
  275. }
  276. vector<string> const List = GetListOfFilesInDir(Dir, "pref", true, true);
  277. // Read the files
  278. for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
  279. if (ReadPinFile(Plcy, *I) == false)
  280. return false;
  281. return true;
  282. }
  283. /*}}}*/
  284. // ReadPinFile - Load the pin file into a Policy /*{{{*/
  285. // ---------------------------------------------------------------------
  286. /* I'd like to see the preferences file store more than just pin information
  287. but right now that is the only stuff I have to store. Later there will
  288. have to be some kind of combined super parser to get the data into all
  289. the right classes.. */
  290. bool ReadPinFile(pkgPolicy &Plcy,string File)
  291. {
  292. if (File.empty() == true)
  293. File = _config->FindFile("Dir::Etc::Preferences");
  294. if (FileExists(File) == false)
  295. return true;
  296. FileFd Fd(File,FileFd::ReadOnly);
  297. pkgTagFile TF(&Fd);
  298. if (_error->PendingError() == true)
  299. return false;
  300. PreferenceSection Tags;
  301. while (TF.Step(Tags) == true)
  302. {
  303. string Name = Tags.FindS("Package");
  304. if (Name.empty() == true)
  305. return _error->Error(_("Invalid record in the preferences file %s, no Package header"), File.c_str());
  306. if (Name == "*")
  307. Name = string();
  308. const char *Start;
  309. const char *End;
  310. if (Tags.Find("Pin",Start,End) == false)
  311. continue;
  312. const char *Word = Start;
  313. for (; Word != End && isspace(*Word) == 0; Word++);
  314. // Parse the type..
  315. pkgVersionMatch::MatchType Type;
  316. if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
  317. Type = pkgVersionMatch::Version;
  318. else if (stringcasecmp(Start,Word,"release") == 0)
  319. Type = pkgVersionMatch::Release;
  320. else if (stringcasecmp(Start,Word,"origin") == 0)
  321. Type = pkgVersionMatch::Origin;
  322. else
  323. {
  324. _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
  325. continue;
  326. }
  327. for (; Word != End && isspace(*Word) != 0; Word++);
  328. short int priority = Tags.FindI("Pin-Priority", 0);
  329. if (priority == 0)
  330. {
  331. _error->Warning(_("No priority (or zero) specified for pin"));
  332. continue;
  333. }
  334. istringstream s(Name);
  335. string pkg;
  336. while(!s.eof())
  337. {
  338. s >> pkg;
  339. Plcy.CreatePin(Type, pkg, string(Word,End),priority);
  340. };
  341. }
  342. Plcy.InitDefaults();
  343. return true;
  344. }
  345. /*}}}*/