]> git.saurik.com Git - apt.git/blame - apt-pkg/aptconfiguration.cc
* apt-pkg/aptconfiguration.cc:
[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
93 return types;
94}
95 /*}}}*/
45df0ad2
DK
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 */
104std::vector<std::string> const Configuration::getLanguages(bool const &All,
d7cf5923 105 bool const &Cached, char const ** const Locale) {
45df0ad2
DK
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
3f2d77b5
DK
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 }
cf0e078c 156 closedir(D);
3f2d77b5 157
d7cf5923 158 // get the environment language codes: LC_MESSAGES (and later LANGUAGE)
45df0ad2
DK
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
d7cf5923 161 string const envMsg = string(Locale == 0 ? std::setlocale(LC_MESSAGES, NULL) : *Locale);
45df0ad2 162 size_t const lenShort = (envMsg.find('_') != string::npos) ? envMsg.find('_') : 2;
d7cf5923 163 size_t const lenLong = (envMsg.find_first_of(".@") != string::npos) ? envMsg.find_first_of(".@") : (lenShort + 3);
45df0ad2
DK
164
165 string envLong = envMsg.substr(0,lenLong);
166 string const envShort = envLong.substr(0,lenShort);
d7cf5923 167 bool envLongIncluded = true;
45df0ad2
DK
168
169 // first cornercase: LANG=C, so we use only "en" Translation
170 if (envLong == "C") {
03745479
DK
171 if (_config->Find("Acquire::Languages","") != "none")
172 codes.push_back("en");
d7cf5923 173 allCodes = codes;
3f2d77b5
DK
174 allCodes.insert(allCodes.end(), builtin.begin(), builtin.end());
175 if (All == true)
176 return allCodes;
177 else
178 return codes;
45df0ad2
DK
179 }
180
d7cf5923
DK
181 // to save the servers from unneeded queries, we only try also long codes
182 // for languages it is realistic to have a long code translation fileā€¦
183 // TODO: Improve translation acquire system to drop them dynamic
184 char const *needLong[] = { "cs", "en", "pt", "sv", "zh", NULL };
45df0ad2 185 if (envLong != envShort) {
45df0ad2
DK
186 for (char const **l = needLong; *l != NULL; l++)
187 if (envShort.compare(*l) == 0) {
188 envLongIncluded = false;
189 break;
190 }
191 }
192
193 // we don't add the long code, but we allow the user to do so
194 if (envLongIncluded == true)
195 envLong.clear();
196
197 // FIXME: Remove support for the old APT::Acquire::Translation
198 // it was undocumented and so it should be not very widthly used
199 string const oldAcquire = _config->Find("APT::Acquire::Translation","");
200 if (oldAcquire.empty() == false && oldAcquire != "environment") {
9f9717fa
DK
201 // TRANSLATORS: the two %s are APT configuration options
202 _error->Notice("Option '%s' is deprecated. Please use '%s' instead, see 'man 5 apt.conf' for details.",
203 "APT::Acquire::Translation", "Acquire::Languages");
45df0ad2
DK
204 if (oldAcquire != "none")
205 codes.push_back(oldAcquire);
3f2d77b5 206 codes.push_back("en");
d7cf5923 207 allCodes = codes;
3f2d77b5
DK
208 for (std::vector<string>::const_iterator b = builtin.begin();
209 b != builtin.end(); ++b)
210 if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
211 allCodes.push_back(*b);
212 if (All == true)
213 return allCodes;
214 else
215 return codes;
45df0ad2
DK
216 }
217
d7cf5923
DK
218 // It is very likely we will need to environment codes later,
219 // so let us generate them now from LC_MESSAGES and LANGUAGE
220 std::vector<string> environment;
221 // take care of LC_MESSAGES
222 if (envLongIncluded == false)
223 environment.push_back(envLong);
224 environment.push_back(envShort);
225 // take care of LANGUAGE
a100bb98
MV
226 const char *language_env = getenv("LANGUAGE") == 0 ? "" : getenv("LANGUAGE");
227 string envLang = Locale == 0 ? language_env : *(Locale+1);
d7cf5923 228 if (envLang.empty() == false) {
3f42500d 229 std::vector<string> env = VectorizeString(envLang,':');
d7cf5923
DK
230 short addedLangs = 0; // add a maximum of 3 fallbacks from the environment
231 for (std::vector<string>::const_iterator e = env.begin();
232 e != env.end() && addedLangs < 3; ++e) {
233 if (unlikely(e->empty() == true) || *e == "en")
234 continue;
235 if (*e == envLong || *e == envShort)
236 continue;
237 if (std::find(environment.begin(), environment.end(), *e) != environment.end())
238 continue;
239 if (e->find('_') != string::npos) {
240 // Drop LongCodes here - ShortCodes are also included
241 string const shorty = e->substr(0, e->find('_'));
242 char const **n = needLong;
243 for (; *n != NULL; ++n)
244 if (shorty == *n)
245 break;
246 if (*n == NULL)
247 continue;
248 }
249 ++addedLangs;
250 environment.push_back(*e);
251 }
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
273 std::vector<string> const lang = _config->FindVector("Acquire::Languages");
274 // the default setting -> "environment, en"
275 if (lang.empty() == true) {
d7cf5923 276 codes = environment;
45df0ad2
DK
277 if (envShort != "en")
278 codes.push_back("en");
d7cf5923 279 allCodes = codes;
3f2d77b5
DK
280 for (std::vector<string>::const_iterator b = builtin.begin();
281 b != builtin.end(); ++b)
282 if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
283 allCodes.push_back(*b);
284 if (All == true)
285 return allCodes;
286 else
287 return codes;
45df0ad2
DK
288 }
289
290 // the configs define the order, so add the environment
291 // then needed and ensure the codes are not listed twice.
292 bool noneSeen = false;
293 for (std::vector<string>::const_iterator l = lang.begin();
294 l != lang.end(); l++) {
295 if (*l == "environment") {
d7cf5923
DK
296 for (std::vector<string>::const_iterator e = environment.begin();
297 e != environment.end(); ++e) {
298 if (std::find(allCodes.begin(), allCodes.end(), *e) != allCodes.end())
299 continue;
45df0ad2 300 if (noneSeen == false)
d7cf5923
DK
301 codes.push_back(*e);
302 allCodes.push_back(*e);
45df0ad2
DK
303 }
304 continue;
305 } else if (*l == "none") {
306 noneSeen = true;
307 continue;
d7cf5923 308 } else if (std::find(allCodes.begin(), allCodes.end(), *l) != allCodes.end())
45df0ad2
DK
309 continue;
310
311 if (noneSeen == false)
312 codes.push_back(*l);
313 allCodes.push_back(*l);
314 }
3f2d77b5
DK
315
316 for (std::vector<string>::const_iterator b = builtin.begin();
317 b != builtin.end(); ++b)
318 if (std::find(allCodes.begin(), allCodes.end(), *b) == allCodes.end())
319 allCodes.push_back(*b);
320
45df0ad2
DK
321 if (All == true)
322 return allCodes;
323 else
324 return codes;
325}
326 /*}}}*/
5dd4c8b8
DK
327// getArchitectures - Return Vector of prefered Architectures /*{{{*/
328std::vector<std::string> const Configuration::getArchitectures(bool const &Cached) {
329 using std::string;
330
331 std::vector<string> static archs;
332 if (likely(Cached == true) && archs.empty() == false)
333 return archs;
334
5dd4c8b8 335 archs = _config->FindVector("APT::Architectures");
3152f4aa
DK
336 string const arch = _config->Find("APT::Architecture");
337 if (unlikely(arch.empty() == true))
338 return archs;
339
5dd4c8b8
DK
340 if (archs.empty() == true ||
341 std::find(archs.begin(), archs.end(), arch) == archs.end())
342 archs.push_back(arch);
3152f4aa
DK
343
344 // erase duplicates and empty strings
345 for (std::vector<string>::reverse_iterator a = archs.rbegin();
346 a != archs.rend(); ++a) {
347 if (a->empty() == true || std::find(a + 1, archs.rend(), *a) != archs.rend())
348 archs.erase(a.base()-1);
349 if (a == archs.rend())
350 break;
351 }
352
5dd4c8b8
DK
353 return archs;
354}
355 /*}}}*/
356// checkArchitecture - are we interested in the given Architecture? /*{{{*/
357bool const Configuration::checkArchitecture(std::string const &Arch) {
358 if (Arch == "all")
359 return true;
360 std::vector<std::string> const archs = getArchitectures(true);
361 return (std::find(archs.begin(), archs.end(), Arch) != archs.end());
362}
363 /*}}}*/
e878aedb 364}