aptconfiguration.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Provide access methods to various configuration settings,
  5. setup defaults and returns validate settings.
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #include <config.h>
  10. #include <apt-pkg/aptconfiguration.h>
  11. #include <apt-pkg/configuration.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/fileutl.h>
  14. #include <apt-pkg/macros.h>
  15. #include <apt-pkg/strutl.h>
  16. #include <apt-pkg/pkgsystem.h>
  17. #include <dirent.h>
  18. #include <stdio.h>
  19. #include <fcntl.h>
  20. #include <ctype.h>
  21. #include <stddef.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <algorithm>
  26. #include <string>
  27. #include <vector>
  28. /*}}}*/
  29. namespace APT {
  30. // setDefaultConfigurationForCompressors /*{{{*/
  31. static void setDefaultConfigurationForCompressors() {
  32. // Set default application paths to check for optional compression types
  33. _config->CndSet("Dir::Bin::bzip2", "/bin/bzip2");
  34. _config->CndSet("Dir::Bin::xz", "/usr/bin/xz");
  35. if (FileExists(_config->FindFile("Dir::Bin::xz")) == true) {
  36. _config->Set("Dir::Bin::lzma", _config->FindFile("Dir::Bin::xz"));
  37. _config->Set("APT::Compressor::lzma::Binary", "xz");
  38. if (_config->Exists("APT::Compressor::lzma::CompressArg") == false) {
  39. _config->Set("APT::Compressor::lzma::CompressArg::", "--format=lzma");
  40. _config->Set("APT::Compressor::lzma::CompressArg::", "-6");
  41. }
  42. if (_config->Exists("APT::Compressor::lzma::UncompressArg") == false) {
  43. _config->Set("APT::Compressor::lzma::UncompressArg::", "--format=lzma");
  44. _config->Set("APT::Compressor::lzma::UncompressArg::", "-d");
  45. }
  46. } else {
  47. _config->CndSet("Dir::Bin::lzma", "/usr/bin/lzma");
  48. if (_config->Exists("APT::Compressor::lzma::CompressArg") == false) {
  49. _config->Set("APT::Compressor::lzma::CompressArg::", "--suffix=");
  50. _config->Set("APT::Compressor::lzma::CompressArg::", "-6");
  51. }
  52. if (_config->Exists("APT::Compressor::lzma::UncompressArg") == false) {
  53. _config->Set("APT::Compressor::lzma::UncompressArg::", "--suffix=");
  54. _config->Set("APT::Compressor::lzma::UncompressArg::", "-d");
  55. }
  56. }
  57. }
  58. /*}}}*/
  59. // getCompressionTypes - Return Vector of usable compressiontypes /*{{{*/
  60. // ---------------------------------------------------------------------
  61. /* return a vector of compression types in the preferred order. */
  62. std::vector<std::string>
  63. const Configuration::getCompressionTypes(bool const &Cached) {
  64. static std::vector<std::string> types;
  65. if (types.empty() == false) {
  66. if (Cached == true)
  67. return types;
  68. else
  69. types.clear();
  70. }
  71. // setup the defaults for the compressiontypes => method mapping
  72. _config->CndSet("Acquire::CompressionTypes::xz","xz");
  73. _config->CndSet("Acquire::CompressionTypes::bz2","bzip2");
  74. _config->CndSet("Acquire::CompressionTypes::lzma","lzma");
  75. _config->CndSet("Acquire::CompressionTypes::gz","gzip");
  76. setDefaultConfigurationForCompressors();
  77. std::vector<APT::Configuration::Compressor> const compressors = getCompressors();
  78. // load the order setting into our vector
  79. std::vector<std::string> const order = _config->FindVector("Acquire::CompressionTypes::Order");
  80. for (std::vector<std::string>::const_iterator o = order.begin();
  81. o != order.end(); ++o) {
  82. if ((*o).empty() == true)
  83. continue;
  84. // ignore types we have no method ready to use
  85. std::string const method = std::string("Acquire::CompressionTypes::").append(*o);
  86. if (_config->Exists(method) == false)
  87. continue;
  88. // ignore types we have no app ready to use
  89. std::string const app = _config->Find(method);
  90. if (std::find_if(compressors.begin(), compressors.end(), [&app](APT::Configuration::Compressor const &c) {
  91. return c.Name == app;
  92. }) == compressors.end())
  93. continue;
  94. types.push_back(*o);
  95. }
  96. // move again over the option tree to add all missing compression types
  97. ::Configuration::Item const *Types = _config->Tree("Acquire::CompressionTypes");
  98. if (Types != 0)
  99. Types = Types->Child;
  100. for (; Types != 0; Types = Types->Next) {
  101. if (Types->Tag == "Order" || Types->Tag.empty() == true)
  102. continue;
  103. // ignore types we already have in the vector
  104. if (std::find(types.begin(),types.end(),Types->Tag) != types.end())
  105. continue;
  106. // ignore types we have no app ready to use
  107. if (std::find_if(compressors.begin(), compressors.end(), [&Types](APT::Configuration::Compressor const &c) {
  108. return c.Name == Types->Value;
  109. }) == compressors.end())
  110. continue;
  111. types.push_back(Types->Tag);
  112. }
  113. // add the special "uncompressed" type
  114. if (std::find(types.begin(), types.end(), "uncompressed") == types.end())
  115. {
  116. std::string const uncompr = _config->FindFile("Dir::Bin::uncompressed", "");
  117. if (uncompr.empty() == true || FileExists(uncompr) == true)
  118. types.push_back("uncompressed");
  119. }
  120. return types;
  121. }
  122. /*}}}*/
  123. // GetLanguages - Return Vector of Language Codes /*{{{*/
  124. // ---------------------------------------------------------------------
  125. /* return a vector of language codes in the preferred order.
  126. the special word "environment" will be replaced with the long and the short
  127. code of the local settings and it will be insured that this will not add
  128. duplicates. So in an german local the setting "environment, de_DE, en, de"
  129. will result in "de_DE, de, en".
  130. The special word "none" is the stopcode for the not-All code vector */
  131. std::vector<std::string> const Configuration::getLanguages(bool const &All,
  132. bool const &Cached, char const ** const Locale) {
  133. using std::string;
  134. // The detection is boring and has a lot of cornercases,
  135. // so we cache the results to calculated it only once.
  136. std::vector<string> static allCodes;
  137. std::vector<string> static codes;
  138. // we have something in the cache
  139. if (codes.empty() == false || allCodes.empty() == false) {
  140. if (Cached == true) {
  141. if(All == true && allCodes.empty() == false)
  142. return allCodes;
  143. else
  144. return codes;
  145. } else {
  146. allCodes.clear();
  147. codes.clear();
  148. }
  149. }
  150. // Include all Language codes we have a Translation file for in /var/lib/apt/lists
  151. // so they will be all included in the Cache.
  152. std::vector<string> builtin;
  153. DIR *D = opendir(_config->FindDir("Dir::State::lists").c_str());
  154. if (D != NULL) {
  155. builtin.push_back("none");
  156. for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) {
  157. string const name = SubstVar(Ent->d_name, "%5f", "_");
  158. size_t const foundDash = name.rfind("-");
  159. size_t const foundUnderscore = name.rfind("_", foundDash);
  160. if (foundDash == string::npos || foundUnderscore == string::npos ||
  161. foundDash <= foundUnderscore ||
  162. name.substr(foundUnderscore+1, foundDash-(foundUnderscore+1)) != "Translation")
  163. continue;
  164. string const c = name.substr(foundDash+1);
  165. if (unlikely(c.empty() == true) || c == "en")
  166. continue;
  167. // Skip unusual files, like backups or that alike
  168. string::const_iterator s = c.begin();
  169. for (;s != c.end(); ++s) {
  170. if (isalpha(*s) == 0 && *s != '_')
  171. break;
  172. }
  173. if (s != c.end())
  174. continue;
  175. if (std::find(builtin.begin(), builtin.end(), c) != builtin.end())
  176. continue;
  177. builtin.push_back(c);
  178. }
  179. closedir(D);
  180. }
  181. // FIXME: Remove support for the old APT::Acquire::Translation
  182. // it was undocumented and so it should be not very widthly used
  183. string const oldAcquire = _config->Find("APT::Acquire::Translation","");
  184. if (oldAcquire.empty() == false && oldAcquire != "environment") {
  185. // TRANSLATORS: the two %s are APT configuration options
  186. _error->Notice("Option '%s' is deprecated. Please use '%s' instead, see 'man 5 apt.conf' for details.",
  187. "APT::Acquire::Translation", "Acquire::Languages");
  188. if (oldAcquire != "none")
  189. codes.push_back(oldAcquire);
  190. codes.push_back("en");
  191. allCodes = codes;
  192. for (std::vector<string>::const_iterator b = builtin.begin();
  193. b != builtin.end(); ++b)
  194. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  195. allCodes.push_back(*b);
  196. if (All == true)
  197. return allCodes;
  198. else
  199. return codes;
  200. }
  201. // get the environment language codes: LC_MESSAGES (and later LANGUAGE)
  202. // we extract both, a long and a short code and then we will
  203. // check if we actually need both (rare) or if the short is enough
  204. string const envMsg = string(Locale == 0 ? ::setlocale(LC_MESSAGES, NULL) : *Locale);
  205. size_t const lenShort = (envMsg.find('_') != string::npos) ? envMsg.find('_') : 2;
  206. size_t const lenLong = (envMsg.find_first_of(".@") != string::npos) ? envMsg.find_first_of(".@") : (lenShort + 3);
  207. string const envLong = envMsg.substr(0,lenLong);
  208. string const envShort = envLong.substr(0,lenShort);
  209. // It is very likely we will need the environment codes later,
  210. // so let us generate them now from LC_MESSAGES and LANGUAGE
  211. std::vector<string> environment;
  212. if (envShort != "C") {
  213. // take care of LC_MESSAGES
  214. if (envLong != envShort)
  215. environment.push_back(envLong);
  216. environment.push_back(envShort);
  217. // take care of LANGUAGE
  218. const char *language_env = getenv("LANGUAGE") == 0 ? "" : getenv("LANGUAGE");
  219. string envLang = Locale == 0 ? language_env : *(Locale+1);
  220. if (envLang.empty() == false) {
  221. std::vector<string> env = VectorizeString(envLang,':');
  222. short addedLangs = 0; // add a maximum of 3 fallbacks from the environment
  223. for (std::vector<string>::const_iterator e = env.begin();
  224. e != env.end() && addedLangs < 3; ++e) {
  225. if (unlikely(e->empty() == true) || *e == "en")
  226. continue;
  227. if (*e == envLong || *e == envShort)
  228. continue;
  229. if (std::find(environment.begin(), environment.end(), *e) != environment.end())
  230. continue;
  231. ++addedLangs;
  232. environment.push_back(*e);
  233. }
  234. }
  235. } else {
  236. // cornercase: LANG=C, so we use only "en" Translation
  237. environment.push_back("en");
  238. }
  239. std::vector<string> const lang = _config->FindVector("Acquire::Languages", "environment,en");
  240. // the configs define the order, so add the environment
  241. // then needed and ensure the codes are not listed twice.
  242. bool noneSeen = false;
  243. for (std::vector<string>::const_iterator l = lang.begin();
  244. l != lang.end(); ++l) {
  245. if (*l == "environment") {
  246. for (std::vector<string>::const_iterator e = environment.begin();
  247. e != environment.end(); ++e) {
  248. if (std::find(allCodes.begin(), allCodes.end(), *e) != allCodes.end())
  249. continue;
  250. if (noneSeen == false)
  251. codes.push_back(*e);
  252. allCodes.push_back(*e);
  253. }
  254. continue;
  255. } else if (*l == "none") {
  256. noneSeen = true;
  257. continue;
  258. } else if (std::find(allCodes.begin(), allCodes.end(), *l) != allCodes.end())
  259. continue;
  260. if (noneSeen == false)
  261. codes.push_back(*l);
  262. allCodes.push_back(*l);
  263. }
  264. if (allCodes.empty() == false) {
  265. for (std::vector<string>::const_iterator b = builtin.begin();
  266. b != builtin.end(); ++b)
  267. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  268. allCodes.push_back(*b);
  269. } else {
  270. // "none" was forced
  271. allCodes.push_back("none");
  272. }
  273. if (All == true)
  274. return allCodes;
  275. else
  276. return codes;
  277. }
  278. /*}}}*/
  279. // checkLanguage - are we interested in the given Language? /*{{{*/
  280. bool Configuration::checkLanguage(std::string Lang, bool const All) {
  281. // the empty Language is always interesting as it is the original
  282. if (Lang.empty() == true)
  283. return true;
  284. // filenames are encoded, so undo this
  285. Lang = SubstVar(Lang, "%5f", "_");
  286. std::vector<std::string> const langs = getLanguages(All, true);
  287. return (std::find(langs.begin(), langs.end(), Lang) != langs.end());
  288. }
  289. /*}}}*/
  290. // getArchitectures - Return Vector of preferred Architectures /*{{{*/
  291. std::vector<std::string> const Configuration::getArchitectures(bool const &Cached) {
  292. using std::string;
  293. std::vector<string> static archs;
  294. if (likely(Cached == true) && archs.empty() == false)
  295. return archs;
  296. string const arch = _config->Find("APT::Architecture");
  297. archs = _config->FindVector("APT::Architectures");
  298. if (archs.empty() == true)
  299. archs = _system->ArchitecturesSupported();
  300. if (archs.empty() == true ||
  301. std::find(archs.begin(), archs.end(), arch) == archs.end())
  302. archs.insert(archs.begin(), arch);
  303. // erase duplicates and empty strings
  304. for (std::vector<string>::reverse_iterator a = archs.rbegin();
  305. a != archs.rend(); ++a) {
  306. if (a->empty() == true || std::find(a + 1, archs.rend(), *a) != archs.rend())
  307. archs.erase(a.base()-1);
  308. if (a == archs.rend())
  309. break;
  310. }
  311. return archs;
  312. }
  313. /*}}}*/
  314. // checkArchitecture - are we interested in the given Architecture? /*{{{*/
  315. bool Configuration::checkArchitecture(std::string const &Arch) {
  316. if (Arch == "all")
  317. return true;
  318. std::vector<std::string> const archs = getArchitectures(true);
  319. return (std::find(archs.begin(), archs.end(), Arch) != archs.end());
  320. }
  321. /*}}}*/
  322. // getCompressors - Return Vector of usealbe compressors /*{{{*/
  323. // ---------------------------------------------------------------------
  324. /* return a vector of compressors used by apt-ftparchive in the
  325. multicompress functionality or to detect data.tar files */
  326. std::vector<APT::Configuration::Compressor>
  327. const Configuration::getCompressors(bool const Cached) {
  328. static std::vector<APT::Configuration::Compressor> compressors;
  329. if (compressors.empty() == false) {
  330. if (Cached == true)
  331. return compressors;
  332. else
  333. compressors.clear();
  334. }
  335. setDefaultConfigurationForCompressors();
  336. compressors.push_back(Compressor(".", "", "", NULL, NULL, 1));
  337. if (_config->Exists("Dir::Bin::gzip") == false || FileExists(_config->FindFile("Dir::Bin::gzip")) == true)
  338. compressors.push_back(Compressor("gzip",".gz","gzip","-6n","-d",2));
  339. #ifdef HAVE_ZLIB
  340. else
  341. compressors.push_back(Compressor("gzip",".gz","false", NULL, NULL, 2));
  342. #endif
  343. if (_config->Exists("Dir::Bin::xz") == false || FileExists(_config->FindFile("Dir::Bin::xz")) == true)
  344. compressors.push_back(Compressor("xz",".xz","xz","-6","-d",3));
  345. #ifdef HAVE_LZMA
  346. else
  347. compressors.push_back(Compressor("xz",".xz","false", NULL, NULL, 3));
  348. #endif
  349. if (_config->Exists("Dir::Bin::bzip2") == false || FileExists(_config->FindFile("Dir::Bin::bzip2")) == true)
  350. compressors.push_back(Compressor("bzip2",".bz2","bzip2","-6","-d",4));
  351. #ifdef HAVE_BZ2
  352. else
  353. compressors.push_back(Compressor("bzip2",".bz2","false", NULL, NULL, 4));
  354. #endif
  355. if (_config->Exists("Dir::Bin::lzma") == false || FileExists(_config->FindFile("Dir::Bin::lzma")) == true)
  356. compressors.push_back(Compressor("lzma",".lzma","lzma","-6","-d",5));
  357. #ifdef HAVE_LZMA
  358. else
  359. compressors.push_back(Compressor("lzma",".lzma","false", NULL, NULL, 5));
  360. #endif
  361. std::vector<std::string> const comp = _config->FindVector("APT::Compressor");
  362. for (std::vector<std::string>::const_iterator c = comp.begin();
  363. c != comp.end(); ++c) {
  364. if (c->empty() || *c == "." || *c == "gzip" || *c == "bzip2" || *c == "lzma" || *c == "xz")
  365. continue;
  366. compressors.push_back(Compressor(c->c_str(), std::string(".").append(*c).c_str(), c->c_str(), "-9", "-d", 100));
  367. }
  368. return compressors;
  369. }
  370. /*}}}*/
  371. // getCompressorExtensions - supported data.tar extensions /*{{{*/
  372. // ---------------------------------------------------------------------
  373. /* */
  374. std::vector<std::string> const Configuration::getCompressorExtensions() {
  375. std::vector<APT::Configuration::Compressor> const compressors = getCompressors();
  376. std::vector<std::string> ext;
  377. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin();
  378. c != compressors.end(); ++c)
  379. if (c->Extension.empty() == false && c->Extension != ".")
  380. ext.push_back(c->Extension);
  381. return ext;
  382. }
  383. /*}}}*/
  384. // Compressor constructor /*{{{*/
  385. // ---------------------------------------------------------------------
  386. /* */
  387. Configuration::Compressor::Compressor(char const *name, char const *extension,
  388. char const *binary,
  389. char const *compressArg, char const *uncompressArg,
  390. unsigned short const cost) {
  391. std::string const config = std::string("APT::Compressor::").append(name).append("::");
  392. Name = _config->Find(std::string(config).append("Name"), name);
  393. Extension = _config->Find(std::string(config).append("Extension"), extension);
  394. Binary = _config->Find(std::string(config).append("Binary"), binary);
  395. Cost = _config->FindI(std::string(config).append("Cost"), cost);
  396. std::string const compConf = std::string(config).append("CompressArg");
  397. if (_config->Exists(compConf) == true)
  398. CompressArgs = _config->FindVector(compConf);
  399. else if (compressArg != NULL)
  400. CompressArgs.push_back(compressArg);
  401. std::string const uncompConf = std::string(config).append("UncompressArg");
  402. if (_config->Exists(uncompConf) == true)
  403. UncompressArgs = _config->FindVector(uncompConf);
  404. else if (uncompressArg != NULL)
  405. UncompressArgs.push_back(uncompressArg);
  406. }
  407. /*}}}*/
  408. // getBuildProfiles - return a vector of enabled build profiles /*{{{*/
  409. std::vector<std::string> const Configuration::getBuildProfiles() {
  410. // order is: override value (~= commandline), environment variable, list (~= config file)
  411. std::string profiles_env = getenv("DEB_BUILD_PROFILES") == 0 ? "" : getenv("DEB_BUILD_PROFILES");
  412. if (profiles_env.empty() == false) {
  413. profiles_env = SubstVar(profiles_env, " ", ",");
  414. std::string const bp = _config->Find("APT::Build-Profiles");
  415. _config->Clear("APT::Build-Profiles");
  416. if (bp.empty() == false)
  417. _config->Set("APT::Build-Profiles", bp);
  418. }
  419. return _config->FindVector("APT::Build-Profiles", profiles_env);
  420. }
  421. std::string const Configuration::getBuildProfilesString() {
  422. std::vector<std::string> profiles = getBuildProfiles();
  423. if (profiles.empty() == true)
  424. return "";
  425. std::vector<std::string>::const_iterator p = profiles.begin();
  426. std::string list = *p;
  427. for (++p; p != profiles.end(); ++p)
  428. list.append(",").append(*p);
  429. return list;
  430. }
  431. /*}}}*/
  432. }