aptconfiguration.cc 8.5 KB

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