- change the internal handling of Extensions in pkgAcqIndex
[ntk/apt.git] / apt-pkg / aptconfiguration.cc
CommitLineData
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>
20
8bd02d8b 21#include <algorithm>
3f2d77b5
DK
22#include <string>
23#include <vector>
e878aedb
DK
24 /*}}}*/
25namespace APT {
26// getCompressionTypes - Return Vector of usbale compressiontypes /*{{{*/
27// ---------------------------------------------------------------------
28/* return a vector of compression types in the prefered order. */
29std::vector<std::string>
30const Configuration::getCompressionTypes(bool const &Cached) {
31 static std::vector<std::string> types;
32 if (types.empty() == false) {
33 if (Cached == true)
34 return types;
35 else
36 types.clear();
37 }
38
8bd02d8b
DK
39 // setup the defaults for the compressiontypes => method mapping
40 _config->CndSet("Acquire::CompressionTypes::bz2","bzip2");
41 _config->CndSet("Acquire::CompressionTypes::lzma","lzma");
42 _config->CndSet("Acquire::CompressionTypes::gz","gzip");
43
e878aedb
DK
44 // Set default application paths to check for optional compression types
45 _config->CndSet("Dir::Bin::lzma", "/usr/bin/lzma");
46 _config->CndSet("Dir::Bin::bzip2", "/bin/bzip2");
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 */
112std::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
d7cf5923 166 // get the environment language codes: LC_MESSAGES (and later LANGUAGE)
45df0ad2
DK
167 // we extract both, a long and a short code and then we will
168 // check if we actually need both (rare) or if the short is enough
d7cf5923 169 string const envMsg = string(Locale == 0 ? std::setlocale(LC_MESSAGES, NULL) : *Locale);
45df0ad2 170 size_t const lenShort = (envMsg.find('_') != string::npos) ? envMsg.find('_') : 2;
d7cf5923 171 size_t const lenLong = (envMsg.find_first_of(".@") != string::npos) ? envMsg.find_first_of(".@") : (lenShort + 3);
45df0ad2
DK
172
173 string envLong = envMsg.substr(0,lenLong);
174 string const envShort = envLong.substr(0,lenShort);
d7cf5923 175 bool envLongIncluded = true;
45df0ad2 176
d7cf5923
DK
177 // to save the servers from unneeded queries, we only try also long codes
178 // for languages it is realistic to have a long code translation fileā€¦
179 // TODO: Improve translation acquire system to drop them dynamic
180 char const *needLong[] = { "cs", "en", "pt", "sv", "zh", NULL };
45df0ad2 181 if (envLong != envShort) {
45df0ad2
DK
182 for (char const **l = needLong; *l != NULL; l++)
183 if (envShort.compare(*l) == 0) {
184 envLongIncluded = false;
185 break;
186 }
187 }
188
189 // we don't add the long code, but we allow the user to do so
190 if (envLongIncluded == true)
191 envLong.clear();
192
193 // FIXME: Remove support for the old APT::Acquire::Translation
194 // it was undocumented and so it should be not very widthly used
195 string const oldAcquire = _config->Find("APT::Acquire::Translation","");
196 if (oldAcquire.empty() == false && oldAcquire != "environment") {
9f9717fa
DK
197 // TRANSLATORS: the two %s are APT configuration options
198 _error->Notice("Option '%s' is deprecated. Please use '%s' instead, see 'man 5 apt.conf' for details.",
199 "APT::Acquire::Translation", "Acquire::Languages");
45df0ad2
DK
200 if (oldAcquire != "none")
201 codes.push_back(oldAcquire);
3f2d77b5 202 codes.push_back("en");
d7cf5923 203 allCodes = codes;
3f2d77b5
DK
204 for (std::vector<string>::const_iterator b = builtin.begin();
205 b != builtin.end(); ++b)
206 if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
207 allCodes.push_back(*b);
208 if (All == true)
209 return allCodes;
210 else
211 return codes;
45df0ad2
DK
212 }
213
d7cf5923
DK
214 // It is very likely we will need to environment codes later,
215 // so let us generate them now from LC_MESSAGES and LANGUAGE
216 std::vector<string> environment;
eb3947c6
DK
217 if (envShort != "C") {
218 // take care of LC_MESSAGES
219 if (envLongIncluded == false)
220 environment.push_back(envLong);
221 environment.push_back(envShort);
222 // take care of LANGUAGE
223 const char *language_env = getenv("LANGUAGE") == 0 ? "" : getenv("LANGUAGE");
224 string envLang = Locale == 0 ? language_env : *(Locale+1);
225 if (envLang.empty() == false) {
226 std::vector<string> env = VectorizeString(envLang,':');
227 short addedLangs = 0; // add a maximum of 3 fallbacks from the environment
228 for (std::vector<string>::const_iterator e = env.begin();
229 e != env.end() && addedLangs < 3; ++e) {
230 if (unlikely(e->empty() == true) || *e == "en")
231 continue;
232 if (*e == envLong || *e == envShort)
233 continue;
234 if (std::find(environment.begin(), environment.end(), *e) != environment.end())
d7cf5923 235 continue;
eb3947c6
DK
236 if (e->find('_') != string::npos) {
237 // Drop LongCodes here - ShortCodes are also included
238 string const shorty = e->substr(0, e->find('_'));
239 char const **n = needLong;
240 for (; *n != NULL; ++n)
241 if (shorty == *n)
242 break;
243 if (*n == NULL)
244 continue;
245 }
246 ++addedLangs;
247 environment.push_back(*e);
d7cf5923 248 }
d7cf5923 249 }
eb3947c6
DK
250 } else {
251 environment.push_back("en");
d7cf5923
DK
252 }
253
45df0ad2
DK
254 // Support settings like Acquire::Translation=none on the command line to
255 // override the configuration settings vector of languages.
256 string const forceLang = _config->Find("Acquire::Languages","");
257 if (forceLang.empty() == false) {
258 if (forceLang == "environment") {
d7cf5923 259 codes = environment;
45df0ad2
DK
260 } else if (forceLang != "none")
261 codes.push_back(forceLang);
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
eb3947c6
DK
273 // cornercase: LANG=C, so we use only "en" Translation
274 if (envShort == "C") {
275 allCodes = codes = environment;
276 allCodes.insert(allCodes.end(), builtin.begin(), builtin.end());
277 if (All == true)
278 return allCodes;
279 else
280 return codes;
281 }
282
45df0ad2
DK
283 std::vector<string> const lang = _config->FindVector("Acquire::Languages");
284 // the default setting -> "environment, en"
285 if (lang.empty() == true) {
d7cf5923 286 codes = environment;
45df0ad2
DK
287 if (envShort != "en")
288 codes.push_back("en");
d7cf5923 289 allCodes = codes;
3f2d77b5
DK
290 for (std::vector<string>::const_iterator b = builtin.begin();
291 b != builtin.end(); ++b)
292 if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
293 allCodes.push_back(*b);
294 if (All == true)
295 return allCodes;
296 else
297 return codes;
45df0ad2
DK
298 }
299
300 // the configs define the order, so add the environment
301 // then needed and ensure the codes are not listed twice.
302 bool noneSeen = false;
303 for (std::vector<string>::const_iterator l = lang.begin();
304 l != lang.end(); l++) {
305 if (*l == "environment") {
d7cf5923
DK
306 for (std::vector<string>::const_iterator e = environment.begin();
307 e != environment.end(); ++e) {
308 if (std::find(allCodes.begin(), allCodes.end(), *e) != allCodes.end())
309 continue;
45df0ad2 310 if (noneSeen == false)
d7cf5923
DK
311 codes.push_back(*e);
312 allCodes.push_back(*e);
45df0ad2
DK
313 }
314 continue;
315 } else if (*l == "none") {
316 noneSeen = true;
317 continue;
d7cf5923 318 } else if (std::find(allCodes.begin(), allCodes.end(), *l) != allCodes.end())
45df0ad2
DK
319 continue;
320
321 if (noneSeen == false)
322 codes.push_back(*l);
323 allCodes.push_back(*l);
324 }
3f2d77b5
DK
325
326 for (std::vector<string>::const_iterator b = builtin.begin();
327 b != builtin.end(); ++b)
328 if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
329 allCodes.push_back(*b);
330
45df0ad2
DK
331 if (All == true)
332 return allCodes;
333 else
334 return codes;
335}
336 /*}}}*/
5dd4c8b8
DK
337// getArchitectures - Return Vector of prefered Architectures /*{{{*/
338std::vector<std::string> const Configuration::getArchitectures(bool const &Cached) {
339 using std::string;
340
341 std::vector<string> static archs;
342 if (likely(Cached == true) && archs.empty() == false)
343 return archs;
344
5dd4c8b8 345 archs = _config->FindVector("APT::Architectures");
3152f4aa
DK
346 string const arch = _config->Find("APT::Architecture");
347 if (unlikely(arch.empty() == true))
348 return archs;
349
5dd4c8b8
DK
350 if (archs.empty() == true ||
351 std::find(archs.begin(), archs.end(), arch) == archs.end())
352 archs.push_back(arch);
3152f4aa
DK
353
354 // erase duplicates and empty strings
355 for (std::vector<string>::reverse_iterator a = archs.rbegin();
356 a != archs.rend(); ++a) {
357 if (a->empty() == true || std::find(a + 1, archs.rend(), *a) != archs.rend())
358 archs.erase(a.base()-1);
359 if (a == archs.rend())
360 break;
361 }
362
5dd4c8b8
DK
363 return archs;
364}
365 /*}}}*/
366// checkArchitecture - are we interested in the given Architecture? /*{{{*/
367bool const Configuration::checkArchitecture(std::string const &Arch) {
368 if (Arch == "all")
369 return true;
370 std::vector<std::string> const archs = getArchitectures(true);
371 return (std::find(archs.begin(), archs.end(), Arch) != archs.end());
372}
373 /*}}}*/
e878aedb 374}