]>
git.saurik.com Git - apt.git/blob - apt-pkg/aptconfiguration.cc
9ded431e8c22c01c66628dc83d5c1bef0376b35c
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::xz","xz");
42 _config
->CndSet("Acquire::CompressionTypes::lzma","lzma");
43 _config
->CndSet("Acquire::CompressionTypes::gz","gzip");
45 // Set default application paths to check for optional compression types
46 _config
->CndSet("Dir::Bin::lzma", "/usr/bin/lzma");
47 _config
->CndSet("Dir::Bin::xz", "/usr/bin/xz");
48 _config
->CndSet("Dir::Bin::bzip2", "/bin/bzip2");
50 // accept non-list order as override setting for config settings on commandline
51 std::string
const overrideOrder
= _config
->Find("Acquire::CompressionTypes::Order","");
52 if (overrideOrder
.empty() == false)
53 types
.push_back(overrideOrder
);
55 // load the order setting into our vector
56 std::vector
<std::string
> const order
= _config
->FindVector("Acquire::CompressionTypes::Order");
57 for (std::vector
<std::string
>::const_iterator o
= order
.begin();
58 o
!= order
.end(); o
++) {
59 if ((*o
).empty() == true)
61 // ignore types we have no method ready to use
62 if (_config
->Exists(string("Acquire::CompressionTypes::").append(*o
)) == false)
64 // ignore types we have no app ready to use
65 string
const appsetting
= string("Dir::Bin::").append(*o
);
66 if (_config
->Exists(appsetting
) == true) {
67 std::string
const app
= _config
->FindFile(appsetting
.c_str(), "");
68 if (app
.empty() == false && FileExists(app
) == false)
74 // move again over the option tree to add all missing compression types
75 ::Configuration::Item
const *Types
= _config
->Tree("Acquire::CompressionTypes");
79 for (; Types
!= 0; Types
= Types
->Next
) {
80 if (Types
->Tag
== "Order" || Types
->Tag
.empty() == true)
82 // ignore types we already have in the vector
83 if (std::find(types
.begin(),types
.end(),Types
->Tag
) != types
.end())
85 // ignore types we have no app ready to use
86 string
const appsetting
= string("Dir::Bin::").append(Types
->Value
);
87 if (appsetting
.empty() == false && _config
->Exists(appsetting
) == true) {
88 std::string
const app
= _config
->FindFile(appsetting
.c_str(), "");
89 if (app
.empty() == false && FileExists(app
) == false)
92 types
.push_back(Types
->Tag
);
95 // add the special "uncompressed" type
96 if (std::find(types
.begin(), types
.end(), "uncompressed") == types
.end())
98 string
const uncompr
= _config
->FindFile("Dir::Bin::uncompressed", "");
99 if (uncompr
.empty() == true || FileExists(uncompr
) == true)
100 types
.push_back("uncompressed");
106 // GetLanguages - Return Vector of Language Codes /*{{{*/
107 // ---------------------------------------------------------------------
108 /* return a vector of language codes in the prefered order.
109 the special word "environment" will be replaced with the long and the short
110 code of the local settings and it will be insured that this will not add
111 duplicates. So in an german local the setting "environment, de_DE, en, de"
112 will result in "de_DE, de, en".
113 The special word "none" is the stopcode for the not-All code vector */
114 std::vector
<std::string
> const Configuration::getLanguages(bool const &All
,
115 bool const &Cached
, char const ** const Locale
) {
118 // The detection is boring and has a lot of cornercases,
119 // so we cache the results to calculated it only once.
120 std::vector
<string
> static allCodes
;
121 std::vector
<string
> static codes
;
123 // we have something in the cache
124 if (codes
.empty() == false || allCodes
.empty() == false) {
125 if (Cached
== true) {
126 if(All
== true && allCodes
.empty() == false)
136 // Include all Language codes we have a Translation file for in /var/lib/apt/lists
137 // so they will be all included in the Cache.
138 std::vector
<string
> builtin
;
139 DIR *D
= opendir(_config
->FindDir("Dir::State::lists").c_str());
141 builtin
.push_back("none");
142 for (struct dirent
*Ent
= readdir(D
); Ent
!= 0; Ent
= readdir(D
)) {
143 string
const name
= Ent
->d_name
;
144 size_t const foundDash
= name
.rfind("-");
145 size_t const foundUnderscore
= name
.rfind("_");
146 if (foundDash
== string::npos
|| foundUnderscore
== string::npos
||
147 foundDash
<= foundUnderscore
||
148 name
.substr(foundUnderscore
+1, foundDash
-(foundUnderscore
+1)) != "Translation")
150 string
const c
= name
.substr(foundDash
+1);
151 if (unlikely(c
.empty() == true) || c
== "en")
153 // Skip unusual files, like backups or that alike
154 string::const_iterator s
= c
.begin();
155 for (;s
!= c
.end(); ++s
) {
156 if (isalpha(*s
) == 0)
161 if (std::find(builtin
.begin(), builtin
.end(), c
) != builtin
.end())
163 builtin
.push_back(c
);
168 // FIXME: Remove support for the old APT::Acquire::Translation
169 // it was undocumented and so it should be not very widthly used
170 string
const oldAcquire
= _config
->Find("APT::Acquire::Translation","");
171 if (oldAcquire
.empty() == false && oldAcquire
!= "environment") {
172 // TRANSLATORS: the two %s are APT configuration options
173 _error
->Notice("Option '%s' is deprecated. Please use '%s' instead, see 'man 5 apt.conf' for details.",
174 "APT::Acquire::Translation", "Acquire::Languages");
175 if (oldAcquire
!= "none")
176 codes
.push_back(oldAcquire
);
177 codes
.push_back("en");
179 for (std::vector
<string
>::const_iterator b
= builtin
.begin();
180 b
!= builtin
.end(); ++b
)
181 if (std::find(allCodes
.begin(), allCodes
.end(), *b
) == allCodes
.end())
182 allCodes
.push_back(*b
);
189 // get the environment language codes: LC_MESSAGES (and later LANGUAGE)
190 // we extract both, a long and a short code and then we will
191 // check if we actually need both (rare) or if the short is enough
192 string
const envMsg
= string(Locale
== 0 ? std::setlocale(LC_MESSAGES
, NULL
) : *Locale
);
193 size_t const lenShort
= (envMsg
.find('_') != string::npos
) ? envMsg
.find('_') : 2;
194 size_t const lenLong
= (envMsg
.find_first_of(".@") != string::npos
) ? envMsg
.find_first_of(".@") : (lenShort
+ 3);
196 string
const envLong
= envMsg
.substr(0,lenLong
);
197 string
const envShort
= envLong
.substr(0,lenShort
);
199 // It is very likely we will need the environment codes later,
200 // so let us generate them now from LC_MESSAGES and LANGUAGE
201 std::vector
<string
> environment
;
202 if (envShort
!= "C") {
203 // take care of LC_MESSAGES
204 if (envLong
!= envShort
)
205 environment
.push_back(envLong
);
206 environment
.push_back(envShort
);
207 // take care of LANGUAGE
208 const char *language_env
= getenv("LANGUAGE") == 0 ? "" : getenv("LANGUAGE");
209 string envLang
= Locale
== 0 ? language_env
: *(Locale
+1);
210 if (envLang
.empty() == false) {
211 std::vector
<string
> env
= VectorizeString(envLang
,':');
212 short addedLangs
= 0; // add a maximum of 3 fallbacks from the environment
213 for (std::vector
<string
>::const_iterator e
= env
.begin();
214 e
!= env
.end() && addedLangs
< 3; ++e
) {
215 if (unlikely(e
->empty() == true) || *e
== "en")
217 if (*e
== envLong
|| *e
== envShort
)
219 if (std::find(environment
.begin(), environment
.end(), *e
) != environment
.end())
222 environment
.push_back(*e
);
226 environment
.push_back("en");
229 // Support settings like Acquire::Translation=none on the command line to
230 // override the configuration settings vector of languages.
231 string
const forceLang
= _config
->Find("Acquire::Languages","");
232 if (forceLang
.empty() == false) {
233 if (forceLang
== "environment") {
235 } else if (forceLang
!= "none")
236 codes
.push_back(forceLang
);
238 for (std::vector
<string
>::const_iterator b
= builtin
.begin();
239 b
!= builtin
.end(); ++b
)
240 if (std::find(allCodes
.begin(), allCodes
.end(), *b
) == allCodes
.end())
241 allCodes
.push_back(*b
);
248 // cornercase: LANG=C, so we use only "en" Translation
249 if (envShort
== "C") {
250 allCodes
= codes
= environment
;
251 allCodes
.insert(allCodes
.end(), builtin
.begin(), builtin
.end());
258 std::vector
<string
> const lang
= _config
->FindVector("Acquire::Languages");
259 // the default setting -> "environment, en"
260 if (lang
.empty() == true) {
262 if (envShort
!= "en")
263 codes
.push_back("en");
265 for (std::vector
<string
>::const_iterator b
= builtin
.begin();
266 b
!= builtin
.end(); ++b
)
267 if (std::find(allCodes
.begin(), allCodes
.end(), *b
) == allCodes
.end())
268 allCodes
.push_back(*b
);
275 // the configs define the order, so add the environment
276 // then needed and ensure the codes are not listed twice.
277 bool noneSeen
= false;
278 for (std::vector
<string
>::const_iterator l
= lang
.begin();
279 l
!= lang
.end(); l
++) {
280 if (*l
== "environment") {
281 for (std::vector
<string
>::const_iterator e
= environment
.begin();
282 e
!= environment
.end(); ++e
) {
283 if (std::find(allCodes
.begin(), allCodes
.end(), *e
) != allCodes
.end())
285 if (noneSeen
== false)
287 allCodes
.push_back(*e
);
290 } else if (*l
== "none") {
293 } else if (std::find(allCodes
.begin(), allCodes
.end(), *l
) != allCodes
.end())
296 if (noneSeen
== false)
298 allCodes
.push_back(*l
);
301 for (std::vector
<string
>::const_iterator b
= builtin
.begin();
302 b
!= builtin
.end(); ++b
)
303 if (std::find(allCodes
.begin(), allCodes
.end(), *b
) == allCodes
.end())
304 allCodes
.push_back(*b
);
312 // getArchitectures - Return Vector of prefered Architectures /*{{{*/
313 std::vector
<std::string
> const Configuration::getArchitectures(bool const &Cached
) {
316 std::vector
<string
> static archs
;
317 if (likely(Cached
== true) && archs
.empty() == false)
320 archs
= _config
->FindVector("APT::Architectures");
321 string
const arch
= _config
->Find("APT::Architecture");
322 if (unlikely(arch
.empty() == true))
325 if (archs
.empty() == true ||
326 std::find(archs
.begin(), archs
.end(), arch
) == archs
.end())
327 archs
.push_back(arch
);
329 // erase duplicates and empty strings
330 for (std::vector
<string
>::reverse_iterator a
= archs
.rbegin();
331 a
!= archs
.rend(); ++a
) {
332 if (a
->empty() == true || std::find(a
+ 1, archs
.rend(), *a
) != archs
.rend())
333 archs
.erase(a
.base()-1);
334 if (a
== archs
.rend())
341 // checkArchitecture - are we interested in the given Architecture? /*{{{*/
342 bool const Configuration::checkArchitecture(std::string
const &Arch
) {
345 std::vector
<std::string
> const archs
= getArchitectures(true);
346 return (std::find(archs
.begin(), archs
.end(), Arch
) != archs
.end());