aptconfiguration.cc 18 KB

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