]>
git.saurik.com Git - apt.git/blob - apt-pkg/aptconfiguration.cc
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Provide access methods to various configuration settings,
6 setup defaults and returns validate settings.
8 ##################################################################### */
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>
18 #include <sys/types.h>
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) {
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");
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");
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
);
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)
59 // ignore types we have no method ready to use
60 if (_config
->Exists(string("Acquire::CompressionTypes::").append(*o
)) == false)
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)
72 // move again over the option tree to add all missing compression types
73 ::Configuration::Item
const *Types
= _config
->Tree("Acquire::CompressionTypes");
77 for (; Types
!= 0; Types
= Types
->Next
) {
78 if (Types
->Tag
== "Order" || Types
->Tag
.empty() == true)
80 // ignore types we already have in the vector
81 if (std::find(types
.begin(),types
.end(),Types
->Tag
) != types
.end())
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)
90 types
.push_back(Types
->Tag
);
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
) {
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
;
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)
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());
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")
140 string
const c
= name
.substr(foundDash
+1);
141 if (unlikely(c
.empty() == true) || c
== "en")
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)
151 if (std::find(builtin
.begin(), builtin
.end(), c
) != builtin
.end())
153 builtin
.push_back(c
);
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);
165 string envLong
= envMsg
.substr(0,lenLong
);
166 string
const envShort
= envLong
.substr(0,lenShort
);
167 bool envLongIncluded
= true;
169 // to save the servers from unneeded queries, we only try also long codes
170 // for languages it is realistic to have a long code translation fileā¦
171 // TODO: Improve translation acquire system to drop them dynamic
172 char const *needLong
[] = { "cs", "en", "pt", "sv", "zh", NULL
};
173 if (envLong
!= envShort
) {
174 for (char const **l
= needLong
; *l
!= NULL
; l
++)
175 if (envShort
.compare(*l
) == 0) {
176 envLongIncluded
= false;
181 // we don't add the long code, but we allow the user to do so
182 if (envLongIncluded
== true)
185 // FIXME: Remove support for the old APT::Acquire::Translation
186 // it was undocumented and so it should be not very widthly used
187 string
const oldAcquire
= _config
->Find("APT::Acquire::Translation","");
188 if (oldAcquire
.empty() == false && oldAcquire
!= "environment") {
189 // TRANSLATORS: the two %s are APT configuration options
190 _error
->Notice("Option '%s' is deprecated. Please use '%s' instead, see 'man 5 apt.conf' for details.",
191 "APT::Acquire::Translation", "Acquire::Languages");
192 if (oldAcquire
!= "none")
193 codes
.push_back(oldAcquire
);
194 codes
.push_back("en");
196 for (std::vector
<string
>::const_iterator b
= builtin
.begin();
197 b
!= builtin
.end(); ++b
)
198 if (std::find(allCodes
.begin(), allCodes
.end(), *b
) == allCodes
.end())
199 allCodes
.push_back(*b
);
206 // It is very likely we will need to environment codes later,
207 // so let us generate them now from LC_MESSAGES and LANGUAGE
208 std::vector
<string
> environment
;
209 if (envShort
!= "C") {
210 // take care of LC_MESSAGES
211 if (envLongIncluded
== false)
212 environment
.push_back(envLong
);
213 environment
.push_back(envShort
);
214 // take care of LANGUAGE
215 const char *language_env
= getenv("LANGUAGE") == 0 ? "" : getenv("LANGUAGE");
216 string envLang
= Locale
== 0 ? language_env
: *(Locale
+1);
217 if (envLang
.empty() == false) {
218 std::vector
<string
> env
= VectorizeString(envLang
,':');
219 short addedLangs
= 0; // add a maximum of 3 fallbacks from the environment
220 for (std::vector
<string
>::const_iterator e
= env
.begin();
221 e
!= env
.end() && addedLangs
< 3; ++e
) {
222 if (unlikely(e
->empty() == true) || *e
== "en")
224 if (*e
== envLong
|| *e
== envShort
)
226 if (std::find(environment
.begin(), environment
.end(), *e
) != environment
.end())
228 if (e
->find('_') != string::npos
) {
229 // Drop LongCodes here - ShortCodes are also included
230 string
const shorty
= e
->substr(0, e
->find('_'));
231 char const **n
= needLong
;
232 for (; *n
!= NULL
; ++n
)
239 environment
.push_back(*e
);
243 environment
.push_back("en");
246 // Support settings like Acquire::Translation=none on the command line to
247 // override the configuration settings vector of languages.
248 string
const forceLang
= _config
->Find("Acquire::Languages","");
249 if (forceLang
.empty() == false) {
250 if (forceLang
== "environment") {
252 } else if (forceLang
!= "none")
253 codes
.push_back(forceLang
);
255 for (std::vector
<string
>::const_iterator b
= builtin
.begin();
256 b
!= builtin
.end(); ++b
)
257 if (std::find(allCodes
.begin(), allCodes
.end(), *b
) == allCodes
.end())
258 allCodes
.push_back(*b
);
265 // cornercase: LANG=C, so we use only "en" Translation
266 if (envShort
== "C") {
267 allCodes
= codes
= environment
;
268 allCodes
.insert(allCodes
.end(), builtin
.begin(), builtin
.end());
275 std::vector
<string
> const lang
= _config
->FindVector("Acquire::Languages");
276 // the default setting -> "environment, en"
277 if (lang
.empty() == true) {
279 if (envShort
!= "en")
280 codes
.push_back("en");
282 for (std::vector
<string
>::const_iterator b
= builtin
.begin();
283 b
!= builtin
.end(); ++b
)
284 if (std::find(allCodes
.begin(), allCodes
.end(), *b
) == allCodes
.end())
285 allCodes
.push_back(*b
);
292 // the configs define the order, so add the environment
293 // then needed and ensure the codes are not listed twice.
294 bool noneSeen
= false;
295 for (std::vector
<string
>::const_iterator l
= lang
.begin();
296 l
!= lang
.end(); l
++) {
297 if (*l
== "environment") {
298 for (std::vector
<string
>::const_iterator e
= environment
.begin();
299 e
!= environment
.end(); ++e
) {
300 if (std::find(allCodes
.begin(), allCodes
.end(), *e
) != allCodes
.end())
302 if (noneSeen
== false)
304 allCodes
.push_back(*e
);
307 } else if (*l
== "none") {
310 } else if (std::find(allCodes
.begin(), allCodes
.end(), *l
) != allCodes
.end())
313 if (noneSeen
== false)
315 allCodes
.push_back(*l
);
318 for (std::vector
<string
>::const_iterator b
= builtin
.begin();
319 b
!= builtin
.end(); ++b
)
320 if (std::find(allCodes
.begin(), allCodes
.end(), *b
) == allCodes
.end())
321 allCodes
.push_back(*b
);
329 // getArchitectures - Return Vector of prefered Architectures /*{{{*/
330 std::vector
<std::string
> const Configuration::getArchitectures(bool const &Cached
) {
333 std::vector
<string
> static archs
;
334 if (likely(Cached
== true) && archs
.empty() == false)
337 archs
= _config
->FindVector("APT::Architectures");
338 string
const arch
= _config
->Find("APT::Architecture");
339 if (unlikely(arch
.empty() == true))
342 if (archs
.empty() == true ||
343 std::find(archs
.begin(), archs
.end(), arch
) == archs
.end())
344 archs
.push_back(arch
);
346 // erase duplicates and empty strings
347 for (std::vector
<string
>::reverse_iterator a
= archs
.rbegin();
348 a
!= archs
.rend(); ++a
) {
349 if (a
->empty() == true || std::find(a
+ 1, archs
.rend(), *a
) != archs
.rend())
350 archs
.erase(a
.base()-1);
351 if (a
== archs
.rend())
358 // checkArchitecture - are we interested in the given Architecture? /*{{{*/
359 bool const Configuration::checkArchitecture(std::string
const &Arch
) {
362 std::vector
<std::string
> const archs
= getArchitectures(true);
363 return (std::find(archs
.begin(), archs
.end(), Arch
) != archs
.end());