aptconfiguration.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 <stdio.h>
  18. #include <algorithm>
  19. #include <string>
  20. #include <vector>
  21. /*}}}*/
  22. namespace APT {
  23. // getCompressionTypes - Return Vector of usbale compressiontypes /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* return a vector of compression types in the prefered order. */
  26. std::vector<std::string>
  27. const Configuration::getCompressionTypes(bool const &Cached) {
  28. static std::vector<std::string> types;
  29. if (types.empty() == false) {
  30. if (Cached == true)
  31. return types;
  32. else
  33. types.clear();
  34. }
  35. // setup the defaults for the compressiontypes => method mapping
  36. _config->CndSet("Acquire::CompressionTypes::bz2","bzip2");
  37. _config->CndSet("Acquire::CompressionTypes::xz","xz");
  38. _config->CndSet("Acquire::CompressionTypes::lzma","lzma");
  39. _config->CndSet("Acquire::CompressionTypes::gz","gzip");
  40. setDefaultConfigurationForCompressors();
  41. // accept non-list order as override setting for config settings on commandline
  42. std::string const overrideOrder = _config->Find("Acquire::CompressionTypes::Order","");
  43. if (overrideOrder.empty() == false)
  44. types.push_back(overrideOrder);
  45. // load the order setting into our vector
  46. std::vector<std::string> const order = _config->FindVector("Acquire::CompressionTypes::Order");
  47. for (std::vector<std::string>::const_iterator o = order.begin();
  48. o != order.end(); ++o) {
  49. if ((*o).empty() == true)
  50. continue;
  51. // ignore types we have no method ready to use
  52. if (_config->Exists(string("Acquire::CompressionTypes::").append(*o)) == false)
  53. continue;
  54. // ignore types we have no app ready to use
  55. string const appsetting = string("Dir::Bin::").append(*o);
  56. if (_config->Exists(appsetting) == true) {
  57. std::string const app = _config->FindFile(appsetting.c_str(), "");
  58. if (app.empty() == false && FileExists(app) == false)
  59. continue;
  60. }
  61. types.push_back(*o);
  62. }
  63. // move again over the option tree to add all missing compression types
  64. ::Configuration::Item const *Types = _config->Tree("Acquire::CompressionTypes");
  65. if (Types != 0)
  66. Types = Types->Child;
  67. for (; Types != 0; Types = Types->Next) {
  68. if (Types->Tag == "Order" || Types->Tag.empty() == true)
  69. continue;
  70. // ignore types we already have in the vector
  71. if (std::find(types.begin(),types.end(),Types->Tag) != types.end())
  72. continue;
  73. // ignore types we have no app ready to use
  74. string const appsetting = string("Dir::Bin::").append(Types->Value);
  75. if (appsetting.empty() == false && _config->Exists(appsetting) == true) {
  76. std::string const app = _config->FindFile(appsetting.c_str(), "");
  77. if (app.empty() == false && FileExists(app) == false)
  78. continue;
  79. }
  80. types.push_back(Types->Tag);
  81. }
  82. // add the special "uncompressed" type
  83. if (std::find(types.begin(), types.end(), "uncompressed") == types.end())
  84. {
  85. string const uncompr = _config->FindFile("Dir::Bin::uncompressed", "");
  86. if (uncompr.empty() == true || FileExists(uncompr) == true)
  87. types.push_back("uncompressed");
  88. }
  89. return types;
  90. }
  91. /*}}}*/
  92. // GetLanguages - Return Vector of Language Codes /*{{{*/
  93. // ---------------------------------------------------------------------
  94. /* return a vector of language codes in the prefered order.
  95. the special word "environment" will be replaced with the long and the short
  96. code of the local settings and it will be insured that this will not add
  97. duplicates. So in an german local the setting "environment, de_DE, en, de"
  98. will result in "de_DE, de, en".
  99. The special word "none" is the stopcode for the not-All code vector */
  100. std::vector<std::string> const Configuration::getLanguages(bool const &All,
  101. bool const &Cached, char const ** const Locale) {
  102. using std::string;
  103. // The detection is boring and has a lot of cornercases,
  104. // so we cache the results to calculated it only once.
  105. std::vector<string> static allCodes;
  106. std::vector<string> static codes;
  107. // we have something in the cache
  108. if (codes.empty() == false || allCodes.empty() == false) {
  109. if (Cached == true) {
  110. if(All == true && allCodes.empty() == false)
  111. return allCodes;
  112. else
  113. return codes;
  114. } else {
  115. allCodes.clear();
  116. codes.clear();
  117. }
  118. }
  119. // Include all Language codes we have a Translation file for in /var/lib/apt/lists
  120. // so they will be all included in the Cache.
  121. std::vector<string> builtin;
  122. DIR *D = opendir(_config->FindDir("Dir::State::lists").c_str());
  123. if (D != 0) {
  124. builtin.push_back("none");
  125. for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D)) {
  126. string const name = Ent->d_name;
  127. size_t const foundDash = name.rfind("-");
  128. size_t const foundUnderscore = name.rfind("_", foundDash);
  129. if (foundDash == string::npos || foundUnderscore == string::npos ||
  130. foundDash <= foundUnderscore ||
  131. name.substr(foundUnderscore+1, foundDash-(foundUnderscore+1)) != "Translation")
  132. continue;
  133. string const c = name.substr(foundDash+1);
  134. if (unlikely(c.empty() == true) || c == "en")
  135. continue;
  136. // Skip unusual files, like backups or that alike
  137. string::const_iterator s = c.begin();
  138. for (;s != c.end(); ++s) {
  139. if (isalpha(*s) == 0 && *s != '_')
  140. break;
  141. }
  142. if (s != c.end())
  143. continue;
  144. if (std::find(builtin.begin(), builtin.end(), c) != builtin.end())
  145. continue;
  146. builtin.push_back(c);
  147. }
  148. }
  149. closedir(D);
  150. // FIXME: Remove support for the old APT::Acquire::Translation
  151. // it was undocumented and so it should be not very widthly used
  152. string const oldAcquire = _config->Find("APT::Acquire::Translation","");
  153. if (oldAcquire.empty() == false && oldAcquire != "environment") {
  154. // TRANSLATORS: the two %s are APT configuration options
  155. _error->Notice("Option '%s' is deprecated. Please use '%s' instead, see 'man 5 apt.conf' for details.",
  156. "APT::Acquire::Translation", "Acquire::Languages");
  157. if (oldAcquire != "none")
  158. codes.push_back(oldAcquire);
  159. codes.push_back("en");
  160. allCodes = codes;
  161. for (std::vector<string>::const_iterator b = builtin.begin();
  162. b != builtin.end(); ++b)
  163. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  164. allCodes.push_back(*b);
  165. if (All == true)
  166. return allCodes;
  167. else
  168. return codes;
  169. }
  170. // get the environment language codes: LC_MESSAGES (and later LANGUAGE)
  171. // we extract both, a long and a short code and then we will
  172. // check if we actually need both (rare) or if the short is enough
  173. string const envMsg = string(Locale == 0 ? std::setlocale(LC_MESSAGES, NULL) : *Locale);
  174. size_t const lenShort = (envMsg.find('_') != string::npos) ? envMsg.find('_') : 2;
  175. size_t const lenLong = (envMsg.find_first_of(".@") != string::npos) ? envMsg.find_first_of(".@") : (lenShort + 3);
  176. string const envLong = envMsg.substr(0,lenLong);
  177. string const envShort = envLong.substr(0,lenShort);
  178. // It is very likely we will need the environment codes later,
  179. // so let us generate them now from LC_MESSAGES and LANGUAGE
  180. std::vector<string> environment;
  181. if (envShort != "C") {
  182. // take care of LC_MESSAGES
  183. if (envLong != envShort)
  184. environment.push_back(envLong);
  185. environment.push_back(envShort);
  186. // take care of LANGUAGE
  187. const char *language_env = getenv("LANGUAGE") == 0 ? "" : getenv("LANGUAGE");
  188. string envLang = Locale == 0 ? language_env : *(Locale+1);
  189. if (envLang.empty() == false) {
  190. std::vector<string> env = VectorizeString(envLang,':');
  191. short addedLangs = 0; // add a maximum of 3 fallbacks from the environment
  192. for (std::vector<string>::const_iterator e = env.begin();
  193. e != env.end() && addedLangs < 3; ++e) {
  194. if (unlikely(e->empty() == true) || *e == "en")
  195. continue;
  196. if (*e == envLong || *e == envShort)
  197. continue;
  198. if (std::find(environment.begin(), environment.end(), *e) != environment.end())
  199. continue;
  200. ++addedLangs;
  201. environment.push_back(*e);
  202. }
  203. }
  204. } else {
  205. environment.push_back("en");
  206. }
  207. // Support settings like Acquire::Languages=none on the command line to
  208. // override the configuration settings vector of languages.
  209. string const forceLang = _config->Find("Acquire::Languages","");
  210. if (forceLang.empty() == false) {
  211. if (forceLang == "environment") {
  212. codes = environment;
  213. } else if (forceLang != "none")
  214. codes.push_back(forceLang);
  215. else //if (forceLang == "none")
  216. builtin.clear();
  217. allCodes = codes;
  218. for (std::vector<string>::const_iterator b = builtin.begin();
  219. b != builtin.end(); ++b)
  220. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  221. allCodes.push_back(*b);
  222. if (All == true)
  223. return allCodes;
  224. else
  225. return codes;
  226. }
  227. // cornercase: LANG=C, so we use only "en" Translation
  228. if (envShort == "C") {
  229. allCodes = codes = environment;
  230. allCodes.insert(allCodes.end(), builtin.begin(), builtin.end());
  231. if (All == true)
  232. return allCodes;
  233. else
  234. return codes;
  235. }
  236. std::vector<string> const lang = _config->FindVector("Acquire::Languages");
  237. // the default setting -> "environment, en"
  238. if (lang.empty() == true) {
  239. codes = environment;
  240. if (envShort != "en")
  241. codes.push_back("en");
  242. allCodes = codes;
  243. for (std::vector<string>::const_iterator b = builtin.begin();
  244. b != builtin.end(); ++b)
  245. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  246. allCodes.push_back(*b);
  247. if (All == true)
  248. return allCodes;
  249. else
  250. return codes;
  251. }
  252. // the configs define the order, so add the environment
  253. // then needed and ensure the codes are not listed twice.
  254. bool noneSeen = false;
  255. for (std::vector<string>::const_iterator l = lang.begin();
  256. l != lang.end(); ++l) {
  257. if (*l == "environment") {
  258. for (std::vector<string>::const_iterator e = environment.begin();
  259. e != environment.end(); ++e) {
  260. if (std::find(allCodes.begin(), allCodes.end(), *e) != allCodes.end())
  261. continue;
  262. if (noneSeen == false)
  263. codes.push_back(*e);
  264. allCodes.push_back(*e);
  265. }
  266. continue;
  267. } else if (*l == "none") {
  268. noneSeen = true;
  269. continue;
  270. } else if (std::find(allCodes.begin(), allCodes.end(), *l) != allCodes.end())
  271. continue;
  272. if (noneSeen == false)
  273. codes.push_back(*l);
  274. allCodes.push_back(*l);
  275. }
  276. for (std::vector<string>::const_iterator b = builtin.begin();
  277. b != builtin.end(); ++b)
  278. if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
  279. allCodes.push_back(*b);
  280. if (All == true)
  281. return allCodes;
  282. else
  283. return codes;
  284. }
  285. /*}}}*/
  286. // getArchitectures - Return Vector of prefered Architectures /*{{{*/
  287. std::vector<std::string> const Configuration::getArchitectures(bool const &Cached) {
  288. using std::string;
  289. std::vector<string> static archs;
  290. if (likely(Cached == true) && archs.empty() == false)
  291. return archs;
  292. string const arch = _config->Find("APT::Architecture");
  293. archs = _config->FindVector("APT::Architectures");
  294. if (unlikely(arch.empty() == true))
  295. return archs;
  296. // FIXME: It is a bit unclean to have debian specific code here…
  297. if (archs.empty() == true) {
  298. archs.push_back(arch);
  299. string dpkgcall = _config->Find("Dir::Bin::dpkg", "dpkg");
  300. std::vector<string> const dpkgoptions = _config->FindVector("DPkg::options");
  301. for (std::vector<string>::const_iterator o = dpkgoptions.begin();
  302. o != dpkgoptions.end(); ++o)
  303. dpkgcall.append(" ").append(*o);
  304. dpkgcall.append(" --print-foreign-architectures 2> /dev/null");
  305. FILE *dpkg = popen(dpkgcall.c_str(), "r");
  306. char buf[1024];
  307. if(dpkg != NULL) {
  308. while (fgets(buf, sizeof(buf), dpkg) != NULL) {
  309. char* arch = strtok(buf, " ");
  310. while (arch != NULL) {
  311. for (; isspace(*arch) != 0; ++arch);
  312. if (arch[0] != '\0') {
  313. char const* archend = arch;
  314. for (; isspace(*archend) == 0 && *archend != '\0'; ++archend);
  315. archs.push_back(string(arch, (archend - arch)));
  316. }
  317. arch = strtok(NULL, " ");
  318. }
  319. }
  320. pclose(dpkg);
  321. }
  322. return archs;
  323. }
  324. if (archs.empty() == true ||
  325. std::find(archs.begin(), archs.end(), arch) == archs.end())
  326. archs.insert(archs.begin(), arch);
  327. // erase duplicates and empty strings
  328. for (std::vector<string>::reverse_iterator a = archs.rbegin();
  329. a != archs.rend(); ++a) {
  330. if (a->empty() == true || std::find(a + 1, archs.rend(), *a) != archs.rend())
  331. archs.erase(a.base()-1);
  332. if (a == archs.rend())
  333. break;
  334. }
  335. return archs;
  336. }
  337. /*}}}*/
  338. // checkArchitecture - are we interested in the given Architecture? /*{{{*/
  339. bool const Configuration::checkArchitecture(std::string const &Arch) {
  340. if (Arch == "all")
  341. return true;
  342. std::vector<std::string> const archs = getArchitectures(true);
  343. return (std::find(archs.begin(), archs.end(), Arch) != archs.end());
  344. }
  345. /*}}}*/
  346. // setDefaultConfigurationForCompressors /*{{{*/
  347. void Configuration::setDefaultConfigurationForCompressors() {
  348. // Set default application paths to check for optional compression types
  349. _config->CndSet("Dir::Bin::lzma", "/usr/bin/lzma");
  350. _config->CndSet("Dir::Bin::xz", "/usr/bin/xz");
  351. _config->CndSet("Dir::Bin::bzip2", "/bin/bzip2");
  352. }
  353. /*}}}*/
  354. // getCompressors - Return Vector of usbale compressors /*{{{*/
  355. // ---------------------------------------------------------------------
  356. /* return a vector of compressors used by apt-ftparchive in the
  357. multicompress functionality or to detect data.tar files */
  358. std::vector<APT::Configuration::Compressor>
  359. const Configuration::getCompressors(bool const Cached) {
  360. static std::vector<APT::Configuration::Compressor> compressors;
  361. if (compressors.empty() == false) {
  362. if (Cached == true)
  363. return compressors;
  364. else
  365. compressors.clear();
  366. }
  367. setDefaultConfigurationForCompressors();
  368. compressors.push_back(Compressor(".", "", "", "", "", 1));
  369. if (_config->Exists("Dir::Bin::gzip") == false || FileExists(_config->FindFile("Dir::Bin::gzip")) == true)
  370. compressors.push_back(Compressor("gzip",".gz","gzip","-9n","-d",2));
  371. if (_config->Exists("Dir::Bin::bzip2") == false || FileExists(_config->FindFile("Dir::Bin::bzip2")) == true)
  372. compressors.push_back(Compressor("bzip2",".bz2","bzip2","-9","-d",3));
  373. if (_config->Exists("Dir::Bin::lzma") == false || FileExists(_config->FindFile("Dir::Bin::lzma")) == true)
  374. compressors.push_back(Compressor("lzma",".lzma","lzma","-9","-d",4));
  375. if (_config->Exists("Dir::Bin::xz") == false || FileExists(_config->FindFile("Dir::Bin::xz")) == true)
  376. compressors.push_back(Compressor("xz",".xz","xz","-6","-d",5));
  377. std::vector<std::string> const comp = _config->FindVector("APT::Compressor");
  378. for (std::vector<std::string>::const_iterator c = comp.begin();
  379. c != comp.end(); ++c) {
  380. if (*c == "." || *c == "gzip" || *c == "bzip2" || *c == "lzma" || *c == "xz")
  381. continue;
  382. compressors.push_back(Compressor(c->c_str(), std::string(".").append(*c).c_str(), c->c_str(), "-9", "-d", 100));
  383. }
  384. return compressors;
  385. }
  386. /*}}}*/
  387. // getCompressorExtensions - supported data.tar extensions /*{{{*/
  388. // ---------------------------------------------------------------------
  389. /* */
  390. std::vector<std::string> const Configuration::getCompressorExtensions() {
  391. std::vector<APT::Configuration::Compressor> const compressors = getCompressors();
  392. std::vector<std::string> ext;
  393. for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressors.begin();
  394. c != compressors.end(); ++c)
  395. if (c->Extension.empty() == false && c->Extension != ".")
  396. ext.push_back(c->Extension);
  397. return ext;
  398. }
  399. /*}}}*/
  400. // Compressor constructor /*{{{*/
  401. // ---------------------------------------------------------------------
  402. /* */
  403. Configuration::Compressor::Compressor(char const *name, char const *extension,
  404. char const *binary,
  405. char const *compressArg, char const *uncompressArg,
  406. unsigned short const cost) {
  407. std::string const config = string("APT:Compressor::").append(name).append("::");
  408. Name = _config->Find(std::string(config).append("Name"), name);
  409. Extension = _config->Find(std::string(config).append("Extension"), extension);
  410. Binary = _config->Find(std::string(config).append("Binary"), binary);
  411. Cost = _config->FindI(std::string(config).append("Cost"), cost);
  412. std::string const compConf = std::string(config).append("CompressArg");
  413. if (_config->Exists(compConf) == true)
  414. CompressArgs = _config->FindVector(compConf);
  415. else if (compressArg != NULL)
  416. CompressArgs.push_back(compressArg);
  417. std::string const uncompConf = std::string(config).append("UncompressArg");
  418. if (_config->Exists(uncompConf) == true)
  419. UncompressArgs = _config->FindVector(uncompConf);
  420. else if (uncompressArg != NULL)
  421. UncompressArgs.push_back(uncompressArg);
  422. }
  423. /*}}}*/
  424. }