merge Goswin Brederlow "support download of index files for different archs"
[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/fileutl.h>
12 #include <apt-pkg/aptconfiguration.h>
13 #include <apt-pkg/configuration.h>
14 #include <system.h>
15
16 #include <vector>
17 #include <string>
18 #include <algorithm>
19 /*}}}*/
20 namespace APT {
21 // getCompressionTypes - Return Vector of usbale compressiontypes /*{{{*/
22 // ---------------------------------------------------------------------
23 /* return a vector of compression types in the prefered order. */
24 std::vector<std::string>
25 const Configuration::getCompressionTypes(bool const &Cached) {
26 static std::vector<std::string> types;
27 if (types.empty() == false) {
28 if (Cached == true)
29 return types;
30 else
31 types.clear();
32 }
33
34 // setup the defaults for the compressiontypes => method mapping
35 _config->CndSet("Acquire::CompressionTypes::bz2","bzip2");
36 _config->CndSet("Acquire::CompressionTypes::lzma","lzma");
37 _config->CndSet("Acquire::CompressionTypes::gz","gzip");
38
39 // Set default application paths to check for optional compression types
40 _config->CndSet("Dir::Bin::lzma", "/usr/bin/lzma");
41 _config->CndSet("Dir::Bin::bzip2", "/bin/bzip2");
42
43 // accept non-list order as override setting for config settings on commandline
44 std::string const overrideOrder = _config->Find("Acquire::CompressionTypes::Order","");
45 if (overrideOrder.empty() == false)
46 types.push_back(overrideOrder);
47
48 // load the order setting into our vector
49 std::vector<std::string> const order = _config->FindVector("Acquire::CompressionTypes::Order");
50 for (std::vector<std::string>::const_iterator o = order.begin();
51 o != order.end(); o++) {
52 if ((*o).empty() == true)
53 continue;
54 // ignore types we have no method ready to use
55 if (_config->Exists(string("Acquire::CompressionTypes::").append(*o)) == false)
56 continue;
57 // ignore types we have no app ready to use
58 string const appsetting = string("Dir::Bin::").append(*o);
59 if (_config->Exists(appsetting) == true) {
60 std::string const app = _config->FindFile(appsetting.c_str(), "");
61 if (app.empty() == false && FileExists(app) == false)
62 continue;
63 }
64 types.push_back(*o);
65 }
66
67 // move again over the option tree to add all missing compression types
68 ::Configuration::Item const *Types = _config->Tree("Acquire::CompressionTypes");
69 if (Types != 0)
70 Types = Types->Child;
71
72 for (; Types != 0; Types = Types->Next) {
73 if (Types->Tag == "Order" || Types->Tag.empty() == true)
74 continue;
75 // ignore types we already have in the vector
76 if (std::find(types.begin(),types.end(),Types->Tag) != types.end())
77 continue;
78 // ignore types we have no app ready to use
79 string const appsetting = string("Dir::Bin::").append(Types->Value);
80 if (appsetting.empty() == false && _config->Exists(appsetting) == true) {
81 std::string const app = _config->FindFile(appsetting.c_str(), "");
82 if (app.empty() == false && FileExists(app) == false)
83 continue;
84 }
85 types.push_back(Types->Tag);
86 }
87
88 return types;
89 }
90 /*}}}*/
91 // GetLanguages - Return Vector of Language Codes /*{{{*/
92 // ---------------------------------------------------------------------
93 /* return a vector of language codes in the prefered order.
94 the special word "environment" will be replaced with the long and the short
95 code of the local settings and it will be insured that this will not add
96 duplicates. So in an german local the setting "environment, de_DE, en, de"
97 will result in "de_DE, de, en".
98 The special word "none" is the stopcode for the not-All code vector */
99 std::vector<std::string> const Configuration::getLanguages(bool const &All,
100 bool const &Cached, char const * const Locale) {
101 using std::string;
102
103 // The detection is boring and has a lot of cornercases,
104 // so we cache the results to calculated it only once.
105 std::vector<string> static allCodes;
106 std::vector<string> static codes;
107
108 // we have something in the cache
109 if (codes.empty() == false || allCodes.empty() == false) {
110 if (Cached == true) {
111 if(All == true && allCodes.empty() == false)
112 return allCodes;
113 else
114 return codes;
115 } else {
116 allCodes.clear();
117 codes.clear();
118 }
119 }
120
121 // get the environment language code
122 // we extract both, a long and a short code and then we will
123 // check if we actually need both (rare) or if the short is enough
124 string const envMsg = string(Locale == 0 ? std::setlocale(LC_MESSAGES, NULL) : Locale);
125 size_t const lenShort = (envMsg.find('_') != string::npos) ? envMsg.find('_') : 2;
126 size_t const lenLong = (envMsg.find('.') != string::npos) ? envMsg.find('.') : (lenShort + 3);
127
128 string envLong = envMsg.substr(0,lenLong);
129 string const envShort = envLong.substr(0,lenShort);
130 bool envLongIncluded = true, envShortIncluded = false;
131
132 // first cornercase: LANG=C, so we use only "en" Translation
133 if (envLong == "C") {
134 codes.push_back("en");
135 return codes;
136 }
137
138 if (envLong != envShort) {
139 // to save the servers from unneeded queries, we only try also long codes
140 // for languages it is realistic to have a long code translation file...
141 char const *needLong[] = { "cs", "en", "pt", "sv", "zh", NULL };
142 for (char const **l = needLong; *l != NULL; l++)
143 if (envShort.compare(*l) == 0) {
144 envLongIncluded = false;
145 break;
146 }
147 }
148
149 // we don't add the long code, but we allow the user to do so
150 if (envLongIncluded == true)
151 envLong.clear();
152
153 // FIXME: Remove support for the old APT::Acquire::Translation
154 // it was undocumented and so it should be not very widthly used
155 string const oldAcquire = _config->Find("APT::Acquire::Translation","");
156 if (oldAcquire.empty() == false && oldAcquire != "environment") {
157 if (oldAcquire != "none")
158 codes.push_back(oldAcquire);
159 return codes;
160 }
161
162 // Support settings like Acquire::Translation=none on the command line to
163 // override the configuration settings vector of languages.
164 string const forceLang = _config->Find("Acquire::Languages","");
165 if (forceLang.empty() == false) {
166 if (forceLang == "environment") {
167 if (envLongIncluded == false)
168 codes.push_back(envLong);
169 if (envShortIncluded == false)
170 codes.push_back(envShort);
171 return codes;
172 } else if (forceLang != "none")
173 codes.push_back(forceLang);
174 return codes;
175 }
176
177 std::vector<string> const lang = _config->FindVector("Acquire::Languages");
178 // the default setting -> "environment, en"
179 if (lang.empty() == true) {
180 if (envLongIncluded == false)
181 codes.push_back(envLong);
182 if (envShortIncluded == false)
183 codes.push_back(envShort);
184 if (envShort != "en")
185 codes.push_back("en");
186 return codes;
187 }
188
189 // the configs define the order, so add the environment
190 // then needed and ensure the codes are not listed twice.
191 bool noneSeen = false;
192 for (std::vector<string>::const_iterator l = lang.begin();
193 l != lang.end(); l++) {
194 if (*l == "environment") {
195 if (envLongIncluded == true && envShortIncluded == true)
196 continue;
197 if (envLongIncluded == false) {
198 envLongIncluded = true;
199 if (noneSeen == false)
200 codes.push_back(envLong);
201 allCodes.push_back(envLong);
202 }
203 if (envShortIncluded == false) {
204 envShortIncluded = true;
205 if (noneSeen == false)
206 codes.push_back(envShort);
207 allCodes.push_back(envShort);
208 }
209 continue;
210 } else if (*l == "none") {
211 noneSeen = true;
212 continue;
213 } else if ((envLongIncluded == true && *l == envLong) ||
214 (envShortIncluded == true && *l == envShort))
215 continue;
216
217 if (noneSeen == false)
218 codes.push_back(*l);
219 allCodes.push_back(*l);
220 }
221 if (All == true)
222 return allCodes;
223 else
224 return codes;
225 }
226 /*}}}*/
227 // getArchitectures - Return Vector of prefered Architectures /*{{{*/
228 std::vector<std::string> const Configuration::getArchitectures(bool const &Cached) {
229 using std::string;
230
231 std::vector<string> static archs;
232 if (likely(Cached == true) && archs.empty() == false)
233 return archs;
234
235 string const arch = _config->Find("APT::Architecture");
236 archs = _config->FindVector("APT::Architectures");
237 if (archs.empty() == true ||
238 std::find(archs.begin(), archs.end(), arch) == archs.end())
239 archs.push_back(arch);
240 return archs;
241 }
242 /*}}}*/
243 // checkArchitecture - are we interested in the given Architecture? /*{{{*/
244 bool const Configuration::checkArchitecture(std::string const &Arch) {
245 if (Arch == "all")
246 return true;
247 std::vector<std::string> const archs = getArchitectures(true);
248 return (std::find(archs.begin(), archs.end(), Arch) != archs.end());
249 }
250 /*}}}*/
251 }