aptconfiguration.cc 7.7 KB

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