]>
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/fileutl.h>
12 #include <apt-pkg/aptconfiguration.h>
13 #include <apt-pkg/configuration.h>
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) {
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");
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");
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
);
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)
54 // ignore types we have no method ready to use
55 if (_config
->Exists(string("Acquire::CompressionTypes::").append(*o
)) == false)
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)
67 // move again over the option tree to add all missing compression types
68 ::Configuration::Item
const *Types
= _config
->Tree("Acquire::CompressionTypes");
72 for (; Types
!= 0; Types
= Types
->Next
) {
73 if (Types
->Tag
== "Order" || Types
->Tag
.empty() == true)
75 // ignore types we already have in the vector
76 if (std::find(types
.begin(),types
.end(),Types
->Tag
) != types
.end())
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)
85 types
.push_back(Types
->Tag
);
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
) {
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
;
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)
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);
128 string envLong
= envMsg
.substr(0,lenLong
);
129 string
const envShort
= envLong
.substr(0,lenShort
);
130 bool envLongIncluded
= true, envShortIncluded
= false;
132 // first cornercase: LANG=C, so we use only "en" Translation
133 if (envLong
== "C") {
134 codes
.push_back("en");
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;
149 // we don't add the long code, but we allow the user to do so
150 if (envLongIncluded
== true)
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
);
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
);
172 } else if (forceLang
!= "none")
173 codes
.push_back(forceLang
);
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");
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)
197 if (envLongIncluded
== false) {
198 envLongIncluded
= true;
199 if (noneSeen
== false)
200 codes
.push_back(envLong
);
201 allCodes
.push_back(envLong
);
203 if (envShortIncluded
== false) {
204 envShortIncluded
= true;
205 if (noneSeen
== false)
206 codes
.push_back(envShort
);
207 allCodes
.push_back(envShort
);
210 } else if (*l
== "none") {
213 } else if ((envLongIncluded
== true && *l
== envLong
) ||
214 (envShortIncluded
== true && *l
== envShort
))
217 if (noneSeen
== false)
219 allCodes
.push_back(*l
);
227 // getArchitectures - Return Vector of prefered Architectures /*{{{*/
228 std::vector
<std::string
> const Configuration::getArchitectures(bool const &Cached
) {
231 std::vector
<string
> static archs
;
232 if (likely(Cached
== true) && archs
.empty() == false)
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
);
243 // checkArchitecture - are we interested in the given Architecture? /*{{{*/
244 bool const Configuration::checkArchitecture(std::string
const &Arch
) {
247 std::vector
<std::string
> const archs
= getArchitectures(true);
248 return (std::find(archs
.begin(), archs
.end(), Arch
) != archs
.end());