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