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.
 
 
 
 
 
 

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