add a note about the uncomplete toolchain to sound a bit more scary
[ntk/apt.git] / apt-pkg / aptconfiguration.cc
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 /*{{{*/
11 #include <apt-pkg/aptconfiguration.h>
12 #include <apt-pkg/configuration.h>
13 #include <apt-pkg/fileutl.h>
14 #include <apt-pkg/macros.h>
15 #include <apt-pkg/strutl.h>
16
17 #include <sys/types.h>
18 #include <dirent.h>
19
20 #include <algorithm>
21 #include <string>
22 #include <vector>
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
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
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
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);
51
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);
69 }
70
71 // move again over the option tree to add all missing compression types
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) {
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
83 string const appsetting = string("Dir::Bin::").append(Types->Value);
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 /*}}}*/
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,
104 bool const &Cached, char const ** const Locale) {
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
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 }
155
156 // get the environment language codes: LC_MESSAGES (and later LANGUAGE)
157 // we extract both, a long and a short code and then we will
158 // check if we actually need both (rare) or if the short is enough
159 string const envMsg = string(Locale == 0 ? std::setlocale(LC_MESSAGES, NULL) : *Locale);
160 size_t const lenShort = (envMsg.find('_') != string::npos) ? envMsg.find('_') : 2;
161 size_t const lenLong = (envMsg.find_first_of(".@") != string::npos) ? envMsg.find_first_of(".@") : (lenShort + 3);
162
163 string envLong = envMsg.substr(0,lenLong);
164 string const envShort = envLong.substr(0,lenShort);
165 bool envLongIncluded = true;
166
167 // first cornercase: LANG=C, so we use only "en" Translation
168 if (envLong == "C") {
169 codes.push_back("en");
170 allCodes = codes;
171 allCodes.insert(allCodes.end(), builtin.begin(), builtin.end());
172 if (All == true)
173 return allCodes;
174 else
175 return codes;
176 }
177
178 // to save the servers from unneeded queries, we only try also long codes
179 // for languages it is realistic to have a long code translation fileā€¦
180 // TODO: Improve translation acquire system to drop them dynamic
181 char const *needLong[] = { "cs", "en", "pt", "sv", "zh", NULL };
182 if (envLong != envShort) {
183 for (char const **l = needLong; *l != NULL; l++)
184 if (envShort.compare(*l) == 0) {
185 envLongIncluded = false;
186 break;
187 }
188 }
189
190 // we don't add the long code, but we allow the user to do so
191 if (envLongIncluded == true)
192 envLong.clear();
193
194 // FIXME: Remove support for the old APT::Acquire::Translation
195 // it was undocumented and so it should be not very widthly used
196 string const oldAcquire = _config->Find("APT::Acquire::Translation","");
197 if (oldAcquire.empty() == false && oldAcquire != "environment") {
198 if (oldAcquire != "none")
199 codes.push_back(oldAcquire);
200 codes.push_back("en");
201 allCodes = codes;
202 for (std::vector<string>::const_iterator b = builtin.begin();
203 b != builtin.end(); ++b)
204 if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
205 allCodes.push_back(*b);
206 if (All == true)
207 return allCodes;
208 else
209 return codes;
210 }
211
212 // It is very likely we will need to environment codes later,
213 // so let us generate them now from LC_MESSAGES and LANGUAGE
214 std::vector<string> environment;
215 // take care of LC_MESSAGES
216 if (envLongIncluded == false)
217 environment.push_back(envLong);
218 environment.push_back(envShort);
219 // take care of LANGUAGE
220 string envLang = Locale == 0 ? getenv("LANGUAGE") : *(Locale+1);
221 if (envLang.empty() == false) {
222 std::vector<string> env = ExplodeString(envLang,':');
223 short addedLangs = 0; // add a maximum of 3 fallbacks from the environment
224 for (std::vector<string>::const_iterator e = env.begin();
225 e != env.end() && addedLangs < 3; ++e) {
226 if (unlikely(e->empty() == true) || *e == "en")
227 continue;
228 if (*e == envLong || *e == envShort)
229 continue;
230 if (std::find(environment.begin(), environment.end(), *e) != environment.end())
231 continue;
232 if (e->find('_') != string::npos) {
233 // Drop LongCodes here - ShortCodes are also included
234 string const shorty = e->substr(0, e->find('_'));
235 char const **n = needLong;
236 for (; *n != NULL; ++n)
237 if (shorty == *n)
238 break;
239 if (*n == NULL)
240 continue;
241 }
242 ++addedLangs;
243 environment.push_back(*e);
244 }
245 }
246
247 // Support settings like Acquire::Translation=none on the command line to
248 // override the configuration settings vector of languages.
249 string const forceLang = _config->Find("Acquire::Languages","");
250 if (forceLang.empty() == false) {
251 if (forceLang == "environment") {
252 codes = environment;
253 } else if (forceLang != "none")
254 codes.push_back(forceLang);
255 allCodes = codes;
256 for (std::vector<string>::const_iterator b = builtin.begin();
257 b != builtin.end(); ++b)
258 if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
259 allCodes.push_back(*b);
260 if (All == true)
261 return allCodes;
262 else
263 return codes;
264 }
265
266 std::vector<string> const lang = _config->FindVector("Acquire::Languages");
267 // the default setting -> "environment, en"
268 if (lang.empty() == true) {
269 codes = environment;
270 if (envShort != "en")
271 codes.push_back("en");
272 allCodes = codes;
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 // the configs define the order, so add the environment
284 // then needed and ensure the codes are not listed twice.
285 bool noneSeen = false;
286 for (std::vector<string>::const_iterator l = lang.begin();
287 l != lang.end(); l++) {
288 if (*l == "environment") {
289 for (std::vector<string>::const_iterator e = environment.begin();
290 e != environment.end(); ++e) {
291 if (std::find(allCodes.begin(), allCodes.end(), *e) != allCodes.end())
292 continue;
293 if (noneSeen == false)
294 codes.push_back(*e);
295 allCodes.push_back(*e);
296 }
297 continue;
298 } else if (*l == "none") {
299 noneSeen = true;
300 continue;
301 } else if (std::find(allCodes.begin(), allCodes.end(), *l) != allCodes.end())
302 continue;
303
304 if (noneSeen == false)
305 codes.push_back(*l);
306 allCodes.push_back(*l);
307 }
308
309 for (std::vector<string>::const_iterator b = builtin.begin();
310 b != builtin.end(); ++b)
311 if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
312 allCodes.push_back(*b);
313
314 if (All == true)
315 return allCodes;
316 else
317 return codes;
318 }
319 /*}}}*/
320 // getArchitectures - Return Vector of prefered Architectures /*{{{*/
321 std::vector<std::string> const Configuration::getArchitectures(bool const &Cached) {
322 using std::string;
323
324 std::vector<string> static archs;
325 if (likely(Cached == true) && archs.empty() == false)
326 return archs;
327
328 string const arch = _config->Find("APT::Architecture");
329 archs = _config->FindVector("APT::Architectures");
330 if (archs.empty() == true ||
331 std::find(archs.begin(), archs.end(), arch) == archs.end())
332 archs.push_back(arch);
333 return archs;
334 }
335 /*}}}*/
336 // checkArchitecture - are we interested in the given Architecture? /*{{{*/
337 bool const Configuration::checkArchitecture(std::string const &Arch) {
338 if (Arch == "all")
339 return true;
340 std::vector<std::string> const archs = getArchitectures(true);
341 return (std::find(archs.begin(), archs.end(), Arch) != archs.end());
342 }
343 /*}}}*/
344 }