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