You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

429 lines
16 KiB

  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::xz","xz");
  37. _config->CndSet("Acquire::CompressionTypes::lzma","lzma");
  38. _config->CndSet("Acquire::CompressionTypes::gz","gzip");
  39. setDefaultConfigurationForCompressors();
  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. // add the special "uncompressed" type
  82. if (std::find(types.begin(), types.end(), "uncompressed") == types.end())
  83. {
  84. string const uncompr = _config->FindFile("Dir::Bin::uncompressed", "");
  85. if (uncompr.empty() == true || FileExists(uncompr) == true)
  86. types.push_back("uncompressed");
  87. }
  88. return types;
  89. }
  90. /*}}}*/
  91. // GetLanguages - Return Vector of Language Codes /*{{{*/
  92. // ---------------------------------------------------------------------
  93. /* return a vector of language codes in the prefered order.
  94. the special word "environment" will be replaced with the long and the short
  95. code of the local settings and it will be insured that this will not add
  96. duplicates. So in an german local the setting "environment, de_DE, en, de"
  97. will result in "de_DE, de, en".
  98. The special word "none" is the stopcode for the not-All code vector */
  99. std::vector<std::string> const Configuration::getLanguages(bool const &All,
  100. bool const &Cached, char const ** const Locale) {
  101. using std::string;
  102. // The detection is boring and has a lot of cornercases,
  103. // so we cache the results to calculated it only once.
  104. std::vector<string> static allCodes;
  105. std::vector<string> static codes;
  106. // we have something in the cache
  107. if (codes.empty() == false || allCodes.empty() == false) {
  108. if (Cached == true) {
  109. if(All == true && allCodes.empty() == false)
  110. return allCodes;
  111. else
  112. return codes;
  113. } else {
  114. allCodes.clear();
  115. codes.clear();
  116. }
  117. }
  118. // Include all Language codes we have a Translation file for in /var/lib/apt/lists
  119. // so they will be all included in the Cache.
  120. std::vector<string> builtin;
  121. DIR *D = opendir(_config->FindDir("Dir::State::lists").c_str());
  122. if (D != 0) {
  123. builtin.push_back("none");
  124. for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) {
  125. string const name = Ent->d_name;
  126. size_t const foundDash = name.rfind("-");
  127. size_t const foundUnderscore = name.rfind("_");
  128. if (foundDash == string::npos || foundUnderscore == string::npos ||
  129. foundDash <= foundUnderscore ||
  130. name.substr(foundUnderscore+1, foundDash-(foundUnderscore+1)) != "Translation")
  131. continue;
  132. string const c = name.substr(foundDash+1);
  133. if (unlikely(c.empty() == true) || c == "en")
  134. continue;
  135. // Skip unusual files, like backups or that alike
  136. string::const_iterator s = c.begin();
  137. for (;s != c.end(); ++s) {
  138. if (isalpha(*s) == 0)
  139. break;
  140. }
  141. if (s != c.end())
  142. continue;
  143. if (std::find(builtin.begin(), builtin.end(), c) != builtin.end())
  144. continue;
  145. builtin.push_back(c);
  146. }
  147. }
  148. closedir(D);
  149. // FIXME: Remove support for the old APT::Acquire::Translation
  150. // it was undocumented and so it should be not very widthly used
  151. string const oldAcquire = _config->Find("APT::Acquire::Translation","");
  152. if (oldAcquire.empty() == false && oldAcquire != "environment") {
  153. // TRANSLATORS: the two %s are APT configuration options
  154. _error->Notice("Option '%s' is deprecated. Please use '%s' instead, see 'man 5 apt.conf' for details.",
  155. "APT::Acquire::Translation", "Acquire::Languages");
  156. if (oldAcquire != "none")
  157. codes.push_back(oldAcquire);
  158. codes.push_back("en");
  159. allCodes = codes;
  160. for (std::vector<string>::const_iterator b = builtin.begin();
  161. b != builtin.end(); ++b)
  162. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  163. allCodes.push_back(*b);
  164. if (All == true)
  165. return allCodes;
  166. else
  167. return codes;
  168. }
  169. // get the environment language codes: LC_MESSAGES (and later LANGUAGE)
  170. // we extract both, a long and a short code and then we will
  171. // check if we actually need both (rare) or if the short is enough
  172. string const envMsg = string(Locale == 0 ? std::setlocale(LC_MESSAGES, NULL) : *Locale);
  173. size_t const lenShort = (envMsg.find('_') != string::npos) ? envMsg.find('_') : 2;
  174. size_t const lenLong = (envMsg.find_first_of(".@") != string::npos) ? envMsg.find_first_of(".@") : (lenShort + 3);
  175. string const envLong = envMsg.substr(0,lenLong);
  176. string const envShort = envLong.substr(0,lenShort);
  177. // It is very likely we will need the environment codes later,
  178. // so let us generate them now from LC_MESSAGES and LANGUAGE
  179. std::vector<string> environment;
  180. if (envShort != "C") {
  181. // take care of LC_MESSAGES
  182. if (envLong != envShort)
  183. environment.push_back(envLong);
  184. environment.push_back(envShort);
  185. // take care of LANGUAGE
  186. const char *language_env = getenv("LANGUAGE") == 0 ? "" : getenv("LANGUAGE");
  187. string envLang = Locale == 0 ? language_env : *(Locale+1);
  188. if (envLang.empty() == false) {
  189. std::vector<string> env = VectorizeString(envLang,':');
  190. short addedLangs = 0; // add a maximum of 3 fallbacks from the environment
  191. for (std::vector<string>::const_iterator e = env.begin();
  192. e != env.end() && addedLangs < 3; ++e) {
  193. if (unlikely(e->empty() == true) || *e == "en")
  194. continue;
  195. if (*e == envLong || *e == envShort)
  196. continue;
  197. if (std::find(environment.begin(), environment.end(), *e) != environment.end())
  198. continue;
  199. ++addedLangs;
  200. environment.push_back(*e);
  201. }
  202. }
  203. } else {
  204. environment.push_back("en");
  205. }
  206. // Support settings like Acquire::Translation=none on the command line to
  207. // override the configuration settings vector of languages.
  208. string const forceLang = _config->Find("Acquire::Languages","");
  209. if (forceLang.empty() == false) {
  210. if (forceLang == "environment") {
  211. codes = environment;
  212. } else if (forceLang != "none")
  213. codes.push_back(forceLang);
  214. allCodes = codes;
  215. for (std::vector<string>::const_iterator b = builtin.begin();
  216. b != builtin.end(); ++b)
  217. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  218. allCodes.push_back(*b);
  219. if (All == true)
  220. return allCodes;
  221. else
  222. return codes;
  223. }
  224. // cornercase: LANG=C, so we use only "en" Translation
  225. if (envShort == "C") {
  226. allCodes = codes = environment;
  227. allCodes.insert(allCodes.end(), builtin.begin(), builtin.end());
  228. if (All == true)
  229. return allCodes;
  230. else
  231. return codes;
  232. }
  233. std::vector<string> const lang = _config->FindVector("Acquire::Languages");
  234. // the default setting -> "environment, en"
  235. if (lang.empty() == true) {
  236. codes = environment;
  237. if (envShort != "en")
  238. codes.push_back("en");
  239. allCodes = codes;
  240. for (std::vector<string>::const_iterator b = builtin.begin();
  241. b != builtin.end(); ++b)
  242. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  243. allCodes.push_back(*b);
  244. if (All == true)
  245. return allCodes;
  246. else
  247. return codes;
  248. }
  249. // the configs define the order, so add the environment
  250. // then needed and ensure the codes are not listed twice.
  251. bool noneSeen = false;
  252. for (std::vector<string>::const_iterator l = lang.begin();
  253. l != lang.end(); l++) {
  254. if (*l == "environment") {
  255. for (std::vector<string>::const_iterator e = environment.begin();
  256. e != environment.end(); ++e) {
  257. if (std::find(allCodes.begin(), allCodes.end(), *e) != allCodes.end())
  258. continue;
  259. if (noneSeen == false)
  260. codes.push_back(*e);
  261. allCodes.push_back(*e);
  262. }
  263. continue;
  264. } else if (*l == "none") {
  265. noneSeen = true;
  266. continue;
  267. } else if (std::find(allCodes.begin(), allCodes.end(), *l) != allCodes.end())
  268. continue;
  269. if (noneSeen == false)
  270. codes.push_back(*l);
  271. allCodes.push_back(*l);
  272. }
  273. for (std::vector<string>::const_iterator b = builtin.begin();
  274. b != builtin.end(); ++b)
  275. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  276. allCodes.push_back(*b);
  277. if (All == true)
  278. return allCodes;
  279. else
  280. return codes;
  281. }
  282. /*}}}*/
  283. // getArchitectures - Return Vector of prefered Architectures /*{{{*/
  284. std::vector<std::string> const Configuration::getArchitectures(bool const &Cached) {
  285. using std::string;
  286. std::vector<string> static archs;
  287. if (likely(Cached == true) && archs.empty() == false)
  288. return archs;
  289. archs = _config->FindVector("APT::Architectures");
  290. string const arch = _config->Find("APT::Architecture");
  291. if (unlikely(arch.empty() == true))
  292. return archs;
  293. if (archs.empty() == true ||
  294. std::find(archs.begin(), archs.end(), arch) == archs.end())
  295. archs.push_back(arch);
  296. // erase duplicates and empty strings
  297. for (std::vector<string>::reverse_iterator a = archs.rbegin();
  298. a != archs.rend(); ++a) {
  299. if (a->empty() == true || std::find(a + 1, archs.rend(), *a) != archs.rend())
  300. archs.erase(a.base()-1);
  301. if (a == archs.rend())
  302. break;
  303. }
  304. return archs;
  305. }
  306. /*}}}*/
  307. // checkArchitecture - are we interested in the given Architecture? /*{{{*/
  308. bool const Configuration::checkArchitecture(std::string const &Arch) {
  309. if (Arch == "all")
  310. return true;
  311. std::vector<std::string> const archs = getArchitectures(true);
  312. return (std::find(archs.begin(), archs.end(), Arch) != archs.end());
  313. }
  314. /*}}}*/
  315. // setDefaultConfigurationForCompressors /*{{{*/
  316. void Configuration::setDefaultConfigurationForCompressors() {
  317. // Set default application paths to check for optional compression types
  318. _config->CndSet("Dir::Bin::lzma", "/usr/bin/lzma");
  319. _config->CndSet("Dir::Bin::xz", "/usr/bin/xz");
  320. _config->CndSet("Dir::Bin::bzip2", "/bin/bzip2");
  321. }
  322. /*}}}*/
  323. // getCompressors - Return Vector of usbale compressors /*{{{*/
  324. // ---------------------------------------------------------------------
  325. /* return a vector of compressors used by apt-ftparchive in the
  326. multicompress functionality or to detect data.tar files */
  327. std::vector<APT::Configuration::Compressor>
  328. const Configuration::getCompressors(bool const Cached) {
  329. static std::vector<APT::Configuration::Compressor> compressors;
  330. if (compressors.empty() == false) {
  331. if (Cached == true)
  332. return compressors;
  333. else
  334. compressors.clear();
  335. }
  336. setDefaultConfigurationForCompressors();
  337. compressors.push_back(Compressor(".", "", "", "", "", 1));
  338. if (_config->Exists("Dir::Bin::gzip") == false || FileExists(_config->FindFile("Dir::Bin::gzip")) == true)
  339. compressors.push_back(Compressor("gzip",".gz","gzip","-9n","-d",2));
  340. if (_config->Exists("Dir::Bin::bzip2") == false || FileExists(_config->FindFile("Dir::Bin::bzip2")) == true)
  341. compressors.push_back(Compressor("bzip2",".bz2","bzip2","-9","-d",3));
  342. if (_config->Exists("Dir::Bin::lzma") == false || FileExists(_config->FindFile("Dir::Bin::lzma")) == true)
  343. compressors.push_back(Compressor("lzma",".lzma","lzma","-9","-d",4));
  344. if (_config->Exists("Dir::Bin::xz") == false || FileExists(_config->FindFile("Dir::Bin::xz")) == true)
  345. compressors.push_back(Compressor("xz",".xz","xz","-6","-d",5));
  346. std::vector<std::string> const comp = _config->FindVector("APT::Compressor");
  347. for (std::vector<std::string>::const_iterator c = comp.begin();
  348. c != comp.end(); ++c) {
  349. if (*c == "." || *c == "gzip" || *c == "bzip2" || *c == "lzma" || *c == "xz")
  350. continue;
  351. compressors.push_back(Compressor(c->c_str(), std::string(".").append(*c).c_str(), c->c_str(), "-9", "-d", 100));
  352. }
  353. return compressors;
  354. }
  355. /*}}}*/
  356. // getCompressorExtensions - supported data.tar extensions /*{{{*/
  357. // ---------------------------------------------------------------------
  358. /* */
  359. std::vector<std::string> const Configuration::getCompressorExtensions() {
  360. std::vector<APT::Configuration::Compressor> const compressors = getCompressors();
  361. std::vector<std::string> ext;
  362. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin();
  363. c != compressors.end(); ++c)
  364. if (c->Extension.empty() == false && c->Extension != ".")
  365. ext.push_back(c->Extension);
  366. return ext;
  367. }
  368. /*}}}*/
  369. // Compressor constructor /*{{{*/
  370. // ---------------------------------------------------------------------
  371. /* */
  372. Configuration::Compressor::Compressor(char const *name, char const *extension,
  373. char const *binary,
  374. char const *compressArg, char const *uncompressArg,
  375. unsigned short const cost) {
  376. std::string const config = string("APT:Compressor::").append(name).append("::");
  377. Name = _config->Find(std::string(config).append("Name"), name);
  378. Extension = _config->Find(std::string(config).append("Extension"), extension);
  379. Binary = _config->Find(std::string(config).append("Binary"), binary);
  380. Cost = _config->FindI(std::string(config).append("Cost"), cost);
  381. std::string const compConf = std::string(config).append("CompressArg");
  382. if (_config->Exists(compConf) == true)
  383. CompressArgs = _config->FindVector(compConf);
  384. else if (compressArg != NULL)
  385. CompressArgs.push_back(compressArg);
  386. std::string const uncompConf = std::string(config).append("UncompressArg");
  387. if (_config->Exists(uncompConf) == true)
  388. UncompressArgs = _config->FindVector(uncompConf);
  389. else if (uncompressArg != NULL)
  390. UncompressArgs.push_back(uncompressArg);
  391. }
  392. /*}}}*/
  393. }