aptconfiguration.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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 <apt-pkg/aptconfiguration.h>
  10. #include <apt-pkg/configuration.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/fileutl.h>
  13. #include <apt-pkg/macros.h>
  14. #include <apt-pkg/strutl.h>
  15. #include <sys/types.h>
  16. #include <dirent.h>
  17. #include <algorithm>
  18. #include <string>
  19. #include <vector>
  20. /*}}}*/
  21. namespace APT {
  22. // getCompressionTypes - Return Vector of usbale compressiontypes /*{{{*/
  23. // ---------------------------------------------------------------------
  24. /* return a vector of compression types in the prefered order. */
  25. std::vector<std::string>
  26. const Configuration::getCompressionTypes(bool const &Cached) {
  27. static std::vector<std::string> types;
  28. if (types.empty() == false) {
  29. if (Cached == true)
  30. return types;
  31. else
  32. types.clear();
  33. }
  34. // setup the defaults for the compressiontypes => method mapping
  35. _config->CndSet("Acquire::CompressionTypes::bz2","bzip2");
  36. _config->CndSet("Acquire::CompressionTypes::lzma","lzma");
  37. _config->CndSet("Acquire::CompressionTypes::gz","gzip");
  38. // Set default application paths to check for optional compression types
  39. _config->CndSet("Dir::Bin::lzma", "/usr/bin/lzma");
  40. _config->CndSet("Dir::Bin::bzip2", "/bin/bzip2");
  41. // accept non-list order as override setting for config settings on commandline
  42. std::string const overrideOrder = _config->Find("Acquire::CompressionTypes::Order","");
  43. if (overrideOrder.empty() == false)
  44. types.push_back(overrideOrder);
  45. // load the order setting into our vector
  46. std::vector<std::string> const order = _config->FindVector("Acquire::CompressionTypes::Order");
  47. for (std::vector<std::string>::const_iterator o = order.begin();
  48. o != order.end(); o++) {
  49. if ((*o).empty() == true)
  50. continue;
  51. // ignore types we have no method ready to use
  52. if (_config->Exists(string("Acquire::CompressionTypes::").append(*o)) == false)
  53. continue;
  54. // ignore types we have no app ready to use
  55. string const appsetting = string("Dir::Bin::").append(*o);
  56. if (_config->Exists(appsetting) == true) {
  57. std::string const app = _config->FindFile(appsetting.c_str(), "");
  58. if (app.empty() == false && FileExists(app) == false)
  59. continue;
  60. }
  61. types.push_back(*o);
  62. }
  63. // move again over the option tree to add all missing compression types
  64. ::Configuration::Item const *Types = _config->Tree("Acquire::CompressionTypes");
  65. if (Types != 0)
  66. Types = Types->Child;
  67. for (; Types != 0; Types = Types->Next) {
  68. if (Types->Tag == "Order" || Types->Tag.empty() == true)
  69. continue;
  70. // ignore types we already have in the vector
  71. if (std::find(types.begin(),types.end(),Types->Tag) != types.end())
  72. continue;
  73. // ignore types we have no app ready to use
  74. string const appsetting = string("Dir::Bin::").append(Types->Value);
  75. if (appsetting.empty() == false && _config->Exists(appsetting) == true) {
  76. std::string const app = _config->FindFile(appsetting.c_str(), "");
  77. if (app.empty() == false && FileExists(app) == false)
  78. continue;
  79. }
  80. types.push_back(Types->Tag);
  81. }
  82. return types;
  83. }
  84. /*}}}*/
  85. // GetLanguages - Return Vector of Language Codes /*{{{*/
  86. // ---------------------------------------------------------------------
  87. /* return a vector of language codes in the prefered order.
  88. the special word "environment" will be replaced with the long and the short
  89. code of the local settings and it will be insured that this will not add
  90. duplicates. So in an german local the setting "environment, de_DE, en, de"
  91. will result in "de_DE, de, en".
  92. The special word "none" is the stopcode for the not-All code vector */
  93. std::vector<std::string> const Configuration::getLanguages(bool const &All,
  94. bool const &Cached, char const ** const Locale) {
  95. using std::string;
  96. // The detection is boring and has a lot of cornercases,
  97. // so we cache the results to calculated it only once.
  98. std::vector<string> static allCodes;
  99. std::vector<string> static codes;
  100. // we have something in the cache
  101. if (codes.empty() == false || allCodes.empty() == false) {
  102. if (Cached == true) {
  103. if(All == true && allCodes.empty() == false)
  104. return allCodes;
  105. else
  106. return codes;
  107. } else {
  108. allCodes.clear();
  109. codes.clear();
  110. }
  111. }
  112. // Include all Language codes we have a Translation file for in /var/lib/apt/lists
  113. // so they will be all included in the Cache.
  114. std::vector<string> builtin;
  115. DIR *D = opendir(_config->FindDir("Dir::State::lists").c_str());
  116. if (D != 0) {
  117. builtin.push_back("none");
  118. for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) {
  119. string const name = Ent->d_name;
  120. size_t const foundDash = name.rfind("-");
  121. size_t const foundUnderscore = name.rfind("_");
  122. if (foundDash == string::npos || foundUnderscore == string::npos ||
  123. foundDash <= foundUnderscore ||
  124. name.substr(foundUnderscore+1, foundDash-(foundUnderscore+1)) != "Translation")
  125. continue;
  126. string const c = name.substr(foundDash+1);
  127. if (unlikely(c.empty() == true) || c == "en")
  128. continue;
  129. // Skip unusual files, like backups or that alike
  130. string::const_iterator s = c.begin();
  131. for (;s != c.end(); ++s) {
  132. if (isalpha(*s) == 0)
  133. break;
  134. }
  135. if (s != c.end())
  136. continue;
  137. if (std::find(builtin.begin(), builtin.end(), c) != builtin.end())
  138. continue;
  139. builtin.push_back(c);
  140. }
  141. }
  142. closedir(D);
  143. // get the environment language codes: LC_MESSAGES (and later LANGUAGE)
  144. // we extract both, a long and a short code and then we will
  145. // check if we actually need both (rare) or if the short is enough
  146. string const envMsg = string(Locale == 0 ? std::setlocale(LC_MESSAGES, NULL) : *Locale);
  147. size_t const lenShort = (envMsg.find('_') != string::npos) ? envMsg.find('_') : 2;
  148. size_t const lenLong = (envMsg.find_first_of(".@") != string::npos) ? envMsg.find_first_of(".@") : (lenShort + 3);
  149. string envLong = envMsg.substr(0,lenLong);
  150. string const envShort = envLong.substr(0,lenShort);
  151. bool envLongIncluded = true;
  152. // to save the servers from unneeded queries, we only try also long codes
  153. // for languages it is realistic to have a long code translation file…
  154. // TODO: Improve translation acquire system to drop them dynamic
  155. char const *needLong[] = { "cs", "en", "pt", "sv", "zh", NULL };
  156. if (envLong != envShort) {
  157. for (char const **l = needLong; *l != NULL; l++)
  158. if (envShort.compare(*l) == 0) {
  159. envLongIncluded = false;
  160. break;
  161. }
  162. }
  163. // we don't add the long code, but we allow the user to do so
  164. if (envLongIncluded == true)
  165. envLong.clear();
  166. // FIXME: Remove support for the old APT::Acquire::Translation
  167. // it was undocumented and so it should be not very widthly used
  168. string const oldAcquire = _config->Find("APT::Acquire::Translation","");
  169. if (oldAcquire.empty() == false && oldAcquire != "environment") {
  170. // TRANSLATORS: the two %s are APT configuration options
  171. _error->Notice("Option '%s' is deprecated. Please use '%s' instead, see 'man 5 apt.conf' for details.",
  172. "APT::Acquire::Translation", "Acquire::Languages");
  173. if (oldAcquire != "none")
  174. codes.push_back(oldAcquire);
  175. codes.push_back("en");
  176. allCodes = codes;
  177. for (std::vector<string>::const_iterator b = builtin.begin();
  178. b != builtin.end(); ++b)
  179. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  180. allCodes.push_back(*b);
  181. if (All == true)
  182. return allCodes;
  183. else
  184. return codes;
  185. }
  186. // It is very likely we will need to environment codes later,
  187. // so let us generate them now from LC_MESSAGES and LANGUAGE
  188. std::vector<string> environment;
  189. if (envShort != "C") {
  190. // take care of LC_MESSAGES
  191. if (envLongIncluded == false)
  192. environment.push_back(envLong);
  193. environment.push_back(envShort);
  194. // take care of LANGUAGE
  195. const char *language_env = getenv("LANGUAGE") == 0 ? "" : getenv("LANGUAGE");
  196. string envLang = Locale == 0 ? language_env : *(Locale+1);
  197. if (envLang.empty() == false) {
  198. std::vector<string> env = VectorizeString(envLang,':');
  199. short addedLangs = 0; // add a maximum of 3 fallbacks from the environment
  200. for (std::vector<string>::const_iterator e = env.begin();
  201. e != env.end() && addedLangs < 3; ++e) {
  202. if (unlikely(e->empty() == true) || *e == "en")
  203. continue;
  204. if (*e == envLong || *e == envShort)
  205. continue;
  206. if (std::find(environment.begin(), environment.end(), *e) != environment.end())
  207. continue;
  208. if (e->find('_') != string::npos) {
  209. // Drop LongCodes here - ShortCodes are also included
  210. string const shorty = e->substr(0, e->find('_'));
  211. char const **n = needLong;
  212. for (; *n != NULL; ++n)
  213. if (shorty == *n)
  214. break;
  215. if (*n == NULL)
  216. continue;
  217. }
  218. ++addedLangs;
  219. environment.push_back(*e);
  220. }
  221. }
  222. } else {
  223. environment.push_back("en");
  224. }
  225. // Support settings like Acquire::Translation=none on the command line to
  226. // override the configuration settings vector of languages.
  227. string const forceLang = _config->Find("Acquire::Languages","");
  228. if (forceLang.empty() == false) {
  229. if (forceLang == "environment") {
  230. codes = environment;
  231. } else if (forceLang != "none")
  232. codes.push_back(forceLang);
  233. allCodes = codes;
  234. for (std::vector<string>::const_iterator b = builtin.begin();
  235. b != builtin.end(); ++b)
  236. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  237. allCodes.push_back(*b);
  238. if (All == true)
  239. return allCodes;
  240. else
  241. return codes;
  242. }
  243. // cornercase: LANG=C, so we use only "en" Translation
  244. if (envShort == "C") {
  245. allCodes = codes = environment;
  246. allCodes.insert(allCodes.end(), builtin.begin(), builtin.end());
  247. if (All == true)
  248. return allCodes;
  249. else
  250. return codes;
  251. }
  252. std::vector<string> const lang = _config->FindVector("Acquire::Languages");
  253. // the default setting -> "environment, en"
  254. if (lang.empty() == true) {
  255. codes = environment;
  256. if (envShort != "en")
  257. codes.push_back("en");
  258. allCodes = codes;
  259. for (std::vector<string>::const_iterator b = builtin.begin();
  260. b != builtin.end(); ++b)
  261. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  262. allCodes.push_back(*b);
  263. if (All == true)
  264. return allCodes;
  265. else
  266. return codes;
  267. }
  268. // the configs define the order, so add the environment
  269. // then needed and ensure the codes are not listed twice.
  270. bool noneSeen = false;
  271. for (std::vector<string>::const_iterator l = lang.begin();
  272. l != lang.end(); l++) {
  273. if (*l == "environment") {
  274. for (std::vector<string>::const_iterator e = environment.begin();
  275. e != environment.end(); ++e) {
  276. if (std::find(allCodes.begin(), allCodes.end(), *e) != allCodes.end())
  277. continue;
  278. if (noneSeen == false)
  279. codes.push_back(*e);
  280. allCodes.push_back(*e);
  281. }
  282. continue;
  283. } else if (*l == "none") {
  284. noneSeen = true;
  285. continue;
  286. } else if (std::find(allCodes.begin(), allCodes.end(), *l) != allCodes.end())
  287. continue;
  288. if (noneSeen == false)
  289. codes.push_back(*l);
  290. allCodes.push_back(*l);
  291. }
  292. for (std::vector<string>::const_iterator b = builtin.begin();
  293. b != builtin.end(); ++b)
  294. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  295. allCodes.push_back(*b);
  296. if (All == true)
  297. return allCodes;
  298. else
  299. return codes;
  300. }
  301. /*}}}*/
  302. // getArchitectures - Return Vector of prefered Architectures /*{{{*/
  303. std::vector<std::string> const Configuration::getArchitectures(bool const &Cached) {
  304. using std::string;
  305. std::vector<string> static archs;
  306. if (likely(Cached == true) && archs.empty() == false)
  307. return archs;
  308. archs = _config->FindVector("APT::Architectures");
  309. string const arch = _config->Find("APT::Architecture");
  310. if (unlikely(arch.empty() == true))
  311. return archs;
  312. if (archs.empty() == true ||
  313. std::find(archs.begin(), archs.end(), arch) == archs.end())
  314. archs.push_back(arch);
  315. // erase duplicates and empty strings
  316. for (std::vector<string>::reverse_iterator a = archs.rbegin();
  317. a != archs.rend(); ++a) {
  318. if (a->empty() == true || std::find(a + 1, archs.rend(), *a) != archs.rend())
  319. archs.erase(a.base()-1);
  320. if (a == archs.rend())
  321. break;
  322. }
  323. return archs;
  324. }
  325. /*}}}*/
  326. // checkArchitecture - are we interested in the given Architecture? /*{{{*/
  327. bool const Configuration::checkArchitecture(std::string const &Arch) {
  328. if (Arch == "all")
  329. return true;
  330. std::vector<std::string> const archs = getArchitectures(true);
  331. return (std::find(archs.begin(), archs.end(), Arch) != archs.end());
  332. }
  333. /*}}}*/
  334. }