aptconfiguration.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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/fileutl.h>
  12. #include <apt-pkg/macros.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <vector>
  15. #include <string>
  16. #include <algorithm>
  17. /*}}}*/
  18. namespace APT {
  19. // getCompressionTypes - Return Vector of usbale compressiontypes /*{{{*/
  20. // ---------------------------------------------------------------------
  21. /* return a vector of compression types in the prefered order. */
  22. std::vector<std::string>
  23. const Configuration::getCompressionTypes(bool const &Cached) {
  24. static std::vector<std::string> types;
  25. if (types.empty() == false) {
  26. if (Cached == true)
  27. return types;
  28. else
  29. types.clear();
  30. }
  31. // setup the defaults for the compressiontypes => method mapping
  32. _config->CndSet("Acquire::CompressionTypes::bz2","bzip2");
  33. _config->CndSet("Acquire::CompressionTypes::lzma","lzma");
  34. _config->CndSet("Acquire::CompressionTypes::gz","gzip");
  35. // Set default application paths to check for optional compression types
  36. _config->CndSet("Dir::Bin::lzma", "/usr/bin/lzma");
  37. _config->CndSet("Dir::Bin::bzip2", "/bin/bzip2");
  38. // accept non-list order as override setting for config settings on commandline
  39. std::string const overrideOrder = _config->Find("Acquire::CompressionTypes::Order","");
  40. if (overrideOrder.empty() == false)
  41. types.push_back(overrideOrder);
  42. // load the order setting into our vector
  43. std::vector<std::string> const order = _config->FindVector("Acquire::CompressionTypes::Order");
  44. for (std::vector<std::string>::const_iterator o = order.begin();
  45. o != order.end(); o++) {
  46. if ((*o).empty() == true)
  47. continue;
  48. // ignore types we have no method ready to use
  49. if (_config->Exists(string("Acquire::CompressionTypes::").append(*o)) == false)
  50. continue;
  51. // ignore types we have no app ready to use
  52. string const appsetting = string("Dir::Bin::").append(*o);
  53. if (_config->Exists(appsetting) == true) {
  54. std::string const app = _config->FindFile(appsetting.c_str(), "");
  55. if (app.empty() == false && FileExists(app) == false)
  56. continue;
  57. }
  58. types.push_back(*o);
  59. }
  60. // move again over the option tree to add all missing compression types
  61. ::Configuration::Item const *Types = _config->Tree("Acquire::CompressionTypes");
  62. if (Types != 0)
  63. Types = Types->Child;
  64. for (; Types != 0; Types = Types->Next) {
  65. if (Types->Tag == "Order" || Types->Tag.empty() == true)
  66. continue;
  67. // ignore types we already have in the vector
  68. if (std::find(types.begin(),types.end(),Types->Tag) != types.end())
  69. continue;
  70. // ignore types we have no app ready to use
  71. string const appsetting = string("Dir::Bin::").append(Types->Value);
  72. if (appsetting.empty() == false && _config->Exists(appsetting) == true) {
  73. std::string const app = _config->FindFile(appsetting.c_str(), "");
  74. if (app.empty() == false && FileExists(app) == false)
  75. continue;
  76. }
  77. types.push_back(Types->Tag);
  78. }
  79. return types;
  80. }
  81. /*}}}*/
  82. // GetLanguages - Return Vector of Language Codes /*{{{*/
  83. // ---------------------------------------------------------------------
  84. /* return a vector of language codes in the prefered order.
  85. the special word "environment" will be replaced with the long and the short
  86. code of the local settings and it will be insured that this will not add
  87. duplicates. So in an german local the setting "environment, de_DE, en, de"
  88. will result in "de_DE, de, en".
  89. The special word "none" is the stopcode for the not-All code vector */
  90. std::vector<std::string> const Configuration::getLanguages(bool const &All,
  91. bool const &Cached, char const ** const Locale) {
  92. using std::string;
  93. // The detection is boring and has a lot of cornercases,
  94. // so we cache the results to calculated it only once.
  95. std::vector<string> static allCodes;
  96. std::vector<string> static codes;
  97. // we have something in the cache
  98. if (codes.empty() == false || allCodes.empty() == false) {
  99. if (Cached == true) {
  100. if(All == true && allCodes.empty() == false)
  101. return allCodes;
  102. else
  103. return codes;
  104. } else {
  105. allCodes.clear();
  106. codes.clear();
  107. }
  108. }
  109. // get the environment language codes: LC_MESSAGES (and later LANGUAGE)
  110. // we extract both, a long and a short code and then we will
  111. // check if we actually need both (rare) or if the short is enough
  112. string const envMsg = string(Locale == 0 ? std::setlocale(LC_MESSAGES, NULL) : *Locale);
  113. size_t const lenShort = (envMsg.find('_') != string::npos) ? envMsg.find('_') : 2;
  114. size_t const lenLong = (envMsg.find_first_of(".@") != string::npos) ? envMsg.find_first_of(".@") : (lenShort + 3);
  115. string envLong = envMsg.substr(0,lenLong);
  116. string const envShort = envLong.substr(0,lenShort);
  117. bool envLongIncluded = true;
  118. // first cornercase: LANG=C, so we use only "en" Translation
  119. if (envLong == "C") {
  120. codes.push_back("en");
  121. allCodes = codes;
  122. return codes;
  123. }
  124. // to save the servers from unneeded queries, we only try also long codes
  125. // for languages it is realistic to have a long code translation file…
  126. // TODO: Improve translation acquire system to drop them dynamic
  127. char const *needLong[] = { "cs", "en", "pt", "sv", "zh", NULL };
  128. if (envLong != envShort) {
  129. for (char const **l = needLong; *l != NULL; l++)
  130. if (envShort.compare(*l) == 0) {
  131. envLongIncluded = false;
  132. break;
  133. }
  134. }
  135. // we don't add the long code, but we allow the user to do so
  136. if (envLongIncluded == true)
  137. envLong.clear();
  138. // FIXME: Remove support for the old APT::Acquire::Translation
  139. // it was undocumented and so it should be not very widthly used
  140. string const oldAcquire = _config->Find("APT::Acquire::Translation","");
  141. if (oldAcquire.empty() == false && oldAcquire != "environment") {
  142. if (oldAcquire != "none")
  143. codes.push_back(oldAcquire);
  144. allCodes = codes;
  145. return codes;
  146. }
  147. // It is very likely we will need to environment codes later,
  148. // so let us generate them now from LC_MESSAGES and LANGUAGE
  149. std::vector<string> environment;
  150. // take care of LC_MESSAGES
  151. if (envLongIncluded == false)
  152. environment.push_back(envLong);
  153. environment.push_back(envShort);
  154. // take care of LANGUAGE
  155. string envLang = Locale == 0 ? getenv("LANGUAGE") : *(Locale+1);
  156. if (envLang.empty() == false) {
  157. std::vector<string> env = ExplodeString(envLang,':');
  158. short addedLangs = 0; // add a maximum of 3 fallbacks from the environment
  159. for (std::vector<string>::const_iterator e = env.begin();
  160. e != env.end() && addedLangs < 3; ++e) {
  161. if (unlikely(e->empty() == true) || *e == "en")
  162. continue;
  163. if (*e == envLong || *e == envShort)
  164. continue;
  165. if (std::find(environment.begin(), environment.end(), *e) != environment.end())
  166. continue;
  167. if (e->find('_') != string::npos) {
  168. // Drop LongCodes here - ShortCodes are also included
  169. string const shorty = e->substr(0, e->find('_'));
  170. char const **n = needLong;
  171. for (; *n != NULL; ++n)
  172. if (shorty == *n)
  173. break;
  174. if (*n == NULL)
  175. continue;
  176. }
  177. ++addedLangs;
  178. environment.push_back(*e);
  179. }
  180. }
  181. // Support settings like Acquire::Translation=none on the command line to
  182. // override the configuration settings vector of languages.
  183. string const forceLang = _config->Find("Acquire::Languages","");
  184. if (forceLang.empty() == false) {
  185. if (forceLang == "environment") {
  186. codes = environment;
  187. } else if (forceLang != "none")
  188. codes.push_back(forceLang);
  189. allCodes = codes;
  190. return codes;
  191. }
  192. std::vector<string> const lang = _config->FindVector("Acquire::Languages");
  193. // the default setting -> "environment, en"
  194. if (lang.empty() == true) {
  195. codes = environment;
  196. if (envShort != "en")
  197. codes.push_back("en");
  198. allCodes = codes;
  199. return codes;
  200. }
  201. // the configs define the order, so add the environment
  202. // then needed and ensure the codes are not listed twice.
  203. bool noneSeen = false;
  204. for (std::vector<string>::const_iterator l = lang.begin();
  205. l != lang.end(); l++) {
  206. if (*l == "environment") {
  207. for (std::vector<string>::const_iterator e = environment.begin();
  208. e != environment.end(); ++e) {
  209. if (std::find(allCodes.begin(), allCodes.end(), *e) != allCodes.end())
  210. continue;
  211. if (noneSeen == false)
  212. codes.push_back(*e);
  213. allCodes.push_back(*e);
  214. }
  215. continue;
  216. } else if (*l == "none") {
  217. noneSeen = true;
  218. continue;
  219. } else if (std::find(allCodes.begin(), allCodes.end(), *l) != allCodes.end())
  220. continue;
  221. if (noneSeen == false)
  222. codes.push_back(*l);
  223. allCodes.push_back(*l);
  224. }
  225. if (All == true)
  226. return allCodes;
  227. else
  228. return codes;
  229. }
  230. /*}}}*/
  231. }