policy.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. See man apt_preferences for what value means what.
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #include<config.h>
  13. #include <apt-pkg/policy.h>
  14. #include <apt-pkg/configuration.h>
  15. #include <apt-pkg/cachefilter.h>
  16. #include <apt-pkg/tagfile.h>
  17. #include <apt-pkg/strutl.h>
  18. #include <apt-pkg/fileutl.h>
  19. #include <apt-pkg/error.h>
  20. #include <apt-pkg/cacheiterators.h>
  21. #include <apt-pkg/pkgcache.h>
  22. #include <apt-pkg/versionmatch.h>
  23. #include <apt-pkg/version.h>
  24. #include <ctype.h>
  25. #include <stddef.h>
  26. #include <string.h>
  27. #include <string>
  28. #include <vector>
  29. #include <iostream>
  30. #include <sstream>
  31. #include <apti18n.h>
  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(nullptr), VerPins(nullptr),
  39. PFPriority(nullptr), Cache(Owner), d(NULL)
  40. {
  41. if (Owner == 0)
  42. return;
  43. PFPriority = new signed short[Owner->Head().PackageFileCount];
  44. Pins = new Pin[Owner->Head().PackageCount];
  45. VerPins = new Pin[Owner->Head().VersionCount];
  46. for (unsigned long I = 0; I != Owner->Head().PackageCount; I++)
  47. Pins[I].Type = pkgVersionMatch::None;
  48. for (unsigned long I = 0; I != Owner->Head().VersionCount; I++)
  49. VerPins[I].Type = pkgVersionMatch::None;
  50. // The config file has a master override.
  51. string DefRel = _config->Find("APT::Default-Release");
  52. if (DefRel.empty() == false)
  53. {
  54. bool found = false;
  55. // FIXME: make ExpressionMatches static to use it here easily
  56. pkgVersionMatch vm("", pkgVersionMatch::None);
  57. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); ++F)
  58. {
  59. if (vm.ExpressionMatches(DefRel, F.Archive()) ||
  60. vm.ExpressionMatches(DefRel, F.Codename()) ||
  61. vm.ExpressionMatches(DefRel, F.Version()) ||
  62. (DefRel.length() > 2 && DefRel[1] == '='))
  63. found = true;
  64. }
  65. if (found == false)
  66. _error->Error(_("The value '%s' is invalid for APT::Default-Release as such a release is not available in the sources"), DefRel.c_str());
  67. else
  68. CreatePin(pkgVersionMatch::Release,"",DefRel,990);
  69. }
  70. InitDefaults();
  71. }
  72. /*}}}*/
  73. // Policy::InitDefaults - Compute the default selections /*{{{*/
  74. // ---------------------------------------------------------------------
  75. /* */
  76. bool pkgPolicy::InitDefaults()
  77. {
  78. // Initialize the priorities based on the status of the package file
  79. for (pkgCache::PkgFileIterator I = Cache->FileBegin(); I != Cache->FileEnd(); ++I)
  80. {
  81. PFPriority[I->ID] = 500;
  82. if (I.Flagged(pkgCache::Flag::NotSource))
  83. PFPriority[I->ID] = 100;
  84. else if (I.Flagged(pkgCache::Flag::ButAutomaticUpgrades))
  85. PFPriority[I->ID] = 100;
  86. else if (I.Flagged(pkgCache::Flag::NotAutomatic))
  87. PFPriority[I->ID] = 1;
  88. }
  89. // Apply the defaults..
  90. std::unique_ptr<bool[]> Fixed(new bool[Cache->HeaderP->PackageFileCount]);
  91. memset(Fixed.get(),0,sizeof(Fixed[0])*Cache->HeaderP->PackageFileCount);
  92. StatusOverride = false;
  93. for (vector<Pin>::const_iterator I = Defaults.begin(); I != Defaults.end(); ++I)
  94. {
  95. pkgVersionMatch Match(I->Data,I->Type);
  96. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); ++F)
  97. {
  98. if (Fixed[F->ID] == false && Match.FileMatch(F) == true)
  99. {
  100. PFPriority[F->ID] = I->Priority;
  101. if (PFPriority[F->ID] >= 1000)
  102. StatusOverride = true;
  103. Fixed[F->ID] = true;
  104. }
  105. }
  106. }
  107. if (_config->FindB("Debug::pkgPolicy",false) == true)
  108. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); ++F)
  109. std::clog << "Prio of " << F.FileName() << ' ' << PFPriority[F->ID] << std::endl;
  110. return true;
  111. }
  112. /*}}}*/
  113. // Policy::GetCandidateVer - Get the candidate install version /*{{{*/
  114. // ---------------------------------------------------------------------
  115. /* Evaluate the package pins and the default list to deteremine what the
  116. best package is. */
  117. pkgCache::VerIterator pkgPolicy::GetCandidateVer(pkgCache::PkgIterator const &Pkg)
  118. {
  119. pkgCache::VerIterator cand;
  120. pkgCache::VerIterator cur = Pkg.CurrentVer();
  121. int candPriority = -1;
  122. pkgVersioningSystem *vs = Cache->VS;
  123. for (pkgCache::VerIterator ver = Pkg.VersionList(); ver.end() == false; ++ver) {
  124. int priority = GetPriority(ver, true);
  125. if (priority == 0 || priority <= candPriority)
  126. continue;
  127. // TODO: Maybe optimize to not compare versions
  128. if (!cur.end() && priority < 1000
  129. && (vs->CmpVersion(ver.VerStr(), cur.VerStr()) < 0))
  130. continue;
  131. candPriority = priority;
  132. cand = ver;
  133. }
  134. return cand;
  135. }
  136. /*}}}*/
  137. // Policy::CreatePin - Create an entry in the pin table.. /*{{{*/
  138. // ---------------------------------------------------------------------
  139. /* For performance we have 3 tables, the default table, the main cache
  140. table (hashed to the cache). A blank package name indicates the pin
  141. belongs to the default table. Order of insertion matters here, the
  142. earlier defaults override later ones. */
  143. void pkgPolicy::CreatePin(pkgVersionMatch::MatchType Type,string Name,
  144. string Data,signed short Priority)
  145. {
  146. if (Name.empty() == true)
  147. {
  148. Pin *P = &*Defaults.insert(Defaults.end(),Pin());
  149. P->Type = Type;
  150. P->Priority = Priority;
  151. P->Data = Data;
  152. return;
  153. }
  154. size_t found = Name.rfind(':');
  155. string Arch;
  156. if (found != string::npos) {
  157. Arch = Name.substr(found+1);
  158. Name.erase(found);
  159. }
  160. // Allow pinning by wildcards - beware of package names looking like wildcards!
  161. // TODO: Maybe we should always prefer specific pins over non-specific ones.
  162. if ((Name[0] == '/' && Name[Name.length() - 1] == '/') || Name.find_first_of("*[?") != string::npos)
  163. {
  164. pkgVersionMatch match(Data, Type);
  165. for (pkgCache::GrpIterator G = Cache->GrpBegin(); G.end() != true; ++G)
  166. if (Name != G.Name() && match.ExpressionMatches(Name, G.Name()))
  167. {
  168. if (Arch.empty() == false)
  169. CreatePin(Type, string(G.Name()).append(":").append(Arch), Data, Priority);
  170. else
  171. CreatePin(Type, G.Name(), Data, Priority);
  172. }
  173. return;
  174. }
  175. // find the package (group) this pin applies to
  176. pkgCache::GrpIterator Grp = Cache->FindGrp(Name);
  177. bool matched = false;
  178. if (Grp.end() == false)
  179. {
  180. std::string MatchingArch;
  181. if (Arch.empty() == true)
  182. MatchingArch = Cache->NativeArch();
  183. else
  184. MatchingArch = Arch;
  185. APT::CacheFilter::PackageArchitectureMatchesSpecification pams(MatchingArch);
  186. for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() != true; Pkg = Grp.NextPkg(Pkg))
  187. {
  188. if (pams(Pkg.Arch()) == false)
  189. continue;
  190. Pin *P = Pins + Pkg->ID;
  191. // the first specific stanza for a package is the ruler,
  192. // all others need to be ignored
  193. if (P->Type != pkgVersionMatch::None)
  194. P = &*Unmatched.insert(Unmatched.end(),PkgPin(Pkg.FullName()));
  195. P->Type = Type;
  196. P->Priority = Priority;
  197. P->Data = Data;
  198. matched = true;
  199. // Find matching version(s) and copy the pin into it
  200. pkgVersionMatch Match(P->Data,P->Type);
  201. for (pkgCache::VerIterator Ver = Pkg.VersionList(); Ver.end() != true; ++Ver)
  202. {
  203. if (Match.VersionMatches(Ver)) {
  204. Pin *VP = VerPins + Ver->ID;
  205. if (VP->Type == pkgVersionMatch::None)
  206. *VP = *P;
  207. }
  208. }
  209. }
  210. }
  211. if (matched == false)
  212. {
  213. PkgPin *P = &*Unmatched.insert(Unmatched.end(),PkgPin(Name));
  214. if (Arch.empty() == false)
  215. P->Pkg.append(":").append(Arch);
  216. P->Type = Type;
  217. P->Priority = Priority;
  218. P->Data = Data;
  219. return;
  220. }
  221. }
  222. /*}}}*/
  223. // Policy::GetMatch - Get the matching version for a package pin /*{{{*/
  224. // ---------------------------------------------------------------------
  225. /* */
  226. pkgCache::VerIterator pkgPolicy::GetMatch(pkgCache::PkgIterator const &Pkg)
  227. {
  228. const Pin &PPkg = Pins[Pkg->ID];
  229. if (PPkg.Type == pkgVersionMatch::None)
  230. return pkgCache::VerIterator(*Pkg.Cache());
  231. pkgVersionMatch Match(PPkg.Data,PPkg.Type);
  232. return Match.Find(Pkg);
  233. }
  234. /*}}}*/
  235. // Policy::GetPriority - Get the priority of the package pin /*{{{*/
  236. // ---------------------------------------------------------------------
  237. /* */
  238. APT_PURE signed short pkgPolicy::GetPriority(pkgCache::PkgIterator const &Pkg)
  239. {
  240. if (Pins[Pkg->ID].Type != pkgVersionMatch::None)
  241. return Pins[Pkg->ID].Priority;
  242. return 0;
  243. }
  244. APT_PURE signed short pkgPolicy::GetPriority(pkgCache::VerIterator const &Ver, bool ConsiderFiles)
  245. {
  246. if (VerPins[Ver->ID].Type != pkgVersionMatch::None)
  247. return VerPins[Ver->ID].Priority;
  248. if (!ConsiderFiles)
  249. return 0;
  250. // priorities are short ints, but we want to pick a value outside the valid range here
  251. auto priority = std::numeric_limits<signed int>::min();
  252. for (pkgCache::VerFileIterator file = Ver.FileList(); file.end() == false; file++)
  253. {
  254. /* If this is the status file, and the current version is not the
  255. version in the status file (ie it is not installed, or somesuch)
  256. then it is not a candidate for installation, ever. This weeds
  257. out bogus entries that may be due to config-file states, or
  258. other. */
  259. if (file.File().Flagged(pkgCache::Flag::NotSource) && Ver.ParentPkg().CurrentVer() != Ver)
  260. priority = std::max(priority, static_cast<decltype(priority)>(-1));
  261. else
  262. priority = std::max(priority, static_cast<decltype(priority)>(GetPriority(file.File())));
  263. }
  264. return priority == std::numeric_limits<decltype(priority)>::min() ? 0 : priority;
  265. }
  266. APT_PURE signed short pkgPolicy::GetPriority(pkgCache::PkgFileIterator const &File)
  267. {
  268. return PFPriority[File->ID];
  269. }
  270. /*}}}*/
  271. // ReadPinDir - Load the pin files from this dir into a Policy /*{{{*/
  272. // ---------------------------------------------------------------------
  273. /* This will load each pin file in the given dir into a Policy. If the
  274. given dir is empty the dir set in Dir::Etc::PreferencesParts is used.
  275. Note also that this method will issue a warning if the dir does not
  276. exists but it will return true in this case! */
  277. bool ReadPinDir(pkgPolicy &Plcy,string Dir)
  278. {
  279. if (Dir.empty() == true)
  280. Dir = _config->FindDir("Dir::Etc::PreferencesParts", "/dev/null");
  281. if (DirectoryExists(Dir) == false)
  282. {
  283. if (APT::String::Endswith(Dir, "/dev/null") == false)
  284. _error->WarningE("DirectoryExists",_("Unable to read %s"),Dir.c_str());
  285. return true;
  286. }
  287. _error->PushToStack();
  288. vector<string> const List = GetListOfFilesInDir(Dir, "pref", true, true);
  289. bool const PendingErrors = _error->PendingError();
  290. _error->MergeWithStack();
  291. if (PendingErrors)
  292. return _error->ReturnError();
  293. // Read the files
  294. for (vector<string>::const_iterator I = List.begin(); I != List.end(); ++I)
  295. if (ReadPinFile(Plcy, *I) == false)
  296. return false;
  297. return true;
  298. }
  299. /*}}}*/
  300. // ReadPinFile - Load the pin file into a Policy /*{{{*/
  301. // ---------------------------------------------------------------------
  302. /* I'd like to see the preferences file store more than just pin information
  303. but right now that is the only stuff I have to store. Later there will
  304. have to be some kind of combined super parser to get the data into all
  305. the right classes.. */
  306. bool ReadPinFile(pkgPolicy &Plcy,string File)
  307. {
  308. if (File.empty() == true)
  309. File = _config->FindFile("Dir::Etc::Preferences");
  310. if (RealFileExists(File) == false)
  311. return true;
  312. FileFd Fd(File,FileFd::ReadOnly);
  313. pkgTagFile TF(&Fd, pkgTagFile::SUPPORT_COMMENTS);
  314. if (Fd.IsOpen() == false || Fd.Failed())
  315. return false;
  316. pkgTagSection Tags;
  317. while (TF.Step(Tags) == true)
  318. {
  319. // can happen when there are only comments in a record
  320. if (Tags.Count() == 0)
  321. continue;
  322. string Name = Tags.FindS("Package");
  323. if (Name.empty() == true)
  324. return _error->Error(_("Invalid record in the preferences file %s, no Package header"), File.c_str());
  325. if (Name == "*")
  326. Name = string();
  327. const char *Start;
  328. const char *End;
  329. if (Tags.Find("Pin",Start,End) == false)
  330. continue;
  331. const char *Word = Start;
  332. for (; Word != End && isspace(*Word) == 0; Word++);
  333. // Parse the type..
  334. pkgVersionMatch::MatchType Type;
  335. if (stringcasecmp(Start,Word,"version") == 0 && Name.empty() == false)
  336. Type = pkgVersionMatch::Version;
  337. else if (stringcasecmp(Start,Word,"release") == 0)
  338. Type = pkgVersionMatch::Release;
  339. else if (stringcasecmp(Start,Word,"origin") == 0)
  340. Type = pkgVersionMatch::Origin;
  341. else
  342. {
  343. _error->Warning(_("Did not understand pin type %s"),string(Start,Word).c_str());
  344. continue;
  345. }
  346. for (; Word != End && isspace(*Word) != 0; Word++);
  347. _error->PushToStack();
  348. int const priority = Tags.FindI("Pin-Priority", 0);
  349. bool const newError = _error->PendingError();
  350. _error->MergeWithStack();
  351. if (priority < std::numeric_limits<short>::min() ||
  352. priority > std::numeric_limits<short>::max() ||
  353. newError) {
  354. _error->ReturnError();
  355. return _error->Error(_("%s: Value %s is outside the range of valid pin priorities (%d to %d)"),
  356. File.c_str(), Tags.FindS("Pin-Priority").c_str(),
  357. std::numeric_limits<short>::min(),
  358. std::numeric_limits<short>::max());
  359. }
  360. if (priority == 0)
  361. {
  362. return _error->Error(_("No priority (or zero) specified for pin"));
  363. }
  364. istringstream s(Name);
  365. string pkg;
  366. while(!s.eof())
  367. {
  368. s >> pkg;
  369. Plcy.CreatePin(Type, pkg, string(Word,End),priority);
  370. };
  371. }
  372. Plcy.InitDefaults();
  373. return true;
  374. }
  375. /*}}}*/
  376. pkgPolicy::~pkgPolicy() {delete [] PFPriority; delete [] Pins; delete [] VerPins; }