aptconfiguration.cc 12 KB

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