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