reorder includes: add <config.h> if needed and include it at first
[ntk/apt.git] / apt-pkg / deb / debmetaindex.cc
CommitLineData
7db98ffc 1// ijones, walters
ea542140 2#include <config.h>
7db98ffc 3
7db98ffc
MZ
4#include <apt-pkg/debmetaindex.h>
5#include <apt-pkg/debindexfile.h>
6#include <apt-pkg/strutl.h>
7#include <apt-pkg/acquire-item.h>
8#include <apt-pkg/configuration.h>
45df0ad2 9#include <apt-pkg/aptconfiguration.h>
7db98ffc
MZ
10#include <apt-pkg/error.h>
11
5dd4c8b8
DK
12#include <set>
13
7db98ffc
MZ
14using namespace std;
15
5dd4c8b8 16string debReleaseIndex::Info(const char *Type, string const &Section, string const &Arch) const
7db98ffc
MZ
17{
18 string Info = ::URI::SiteOnly(URI) + ' ';
19 if (Dist[Dist.size() - 1] == '/')
20 {
21 if (Dist != "/")
22 Info += Dist;
23 }
24 else
5dd4c8b8
DK
25 {
26 Info += Dist + '/' + Section;
ff1ad6fd 27 if (Arch.empty() != true)
5dd4c8b8
DK
28 Info += " " + Arch;
29 }
7db98ffc
MZ
30 Info += " ";
31 Info += Type;
32 return Info;
33}
34
35string debReleaseIndex::MetaIndexInfo(const char *Type) const
36{
37 string Info = ::URI::SiteOnly(URI) + ' ';
38 if (Dist[Dist.size() - 1] == '/')
39 {
40 if (Dist != "/")
41 Info += Dist;
42 }
43 else
44 Info += Dist;
45 Info += " ";
46 Info += Type;
47 return Info;
48}
49
50string debReleaseIndex::MetaIndexFile(const char *Type) const
51{
52 return _config->FindDir("Dir::State::lists") +
53 URItoFileName(MetaIndexURI(Type));
54}
55
56string debReleaseIndex::MetaIndexURI(const char *Type) const
57{
58 string Res;
59
60 if (Dist == "/")
61 Res = URI;
62 else if (Dist[Dist.size()-1] == '/')
63 Res = URI + Dist;
64 else
65 Res = URI + "dists/" + Dist + "/";
66
67 Res += Type;
68 return Res;
69}
70
5dd4c8b8 71string debReleaseIndex::IndexURISuffix(const char *Type, string const &Section, string const &Arch) const
7db98ffc
MZ
72{
73 string Res ="";
74 if (Dist[Dist.size() - 1] != '/')
5dd4c8b8
DK
75 {
76 if (Arch == "native")
77 Res += Section + "/binary-" + _config->Find("APT::Architecture") + '/';
78 else
79 Res += Section + "/binary-" + Arch + '/';
80 }
7db98ffc
MZ
81 return Res + Type;
82}
83
84
5dd4c8b8 85string debReleaseIndex::IndexURI(const char *Type, string const &Section, string const &Arch) const
7db98ffc
MZ
86{
87 if (Dist[Dist.size() - 1] == '/')
88 {
89 string Res;
90 if (Dist != "/")
91 Res = URI + Dist;
92 else
93 Res = URI;
94 return Res + Type;
95 }
96 else
5dd4c8b8 97 return URI + "dists/" + Dist + '/' + IndexURISuffix(Type, Section, Arch);
7db98ffc
MZ
98 }
99
5dd4c8b8 100string debReleaseIndex::SourceIndexURISuffix(const char *Type, const string &Section) const
7db98ffc
MZ
101{
102 string Res ="";
103 if (Dist[Dist.size() - 1] != '/')
104 Res += Section + "/source/";
105 return Res + Type;
106}
107
5dd4c8b8 108string debReleaseIndex::SourceIndexURI(const char *Type, const string &Section) const
7db98ffc
MZ
109{
110 string Res;
111 if (Dist[Dist.size() - 1] == '/')
112 {
113 if (Dist != "/")
114 Res = URI + Dist;
115 else
116 Res = URI;
117 return Res + Type;
118 }
119 else
120 return URI + "dists/" + Dist + "/" + SourceIndexURISuffix(Type, Section);
121}
122
ab53c018
DK
123string debReleaseIndex::TranslationIndexURISuffix(const char *Type, const string &Section) const
124{
125 string Res ="";
126 if (Dist[Dist.size() - 1] != '/')
127 Res += Section + "/i18n/";
128 return Res + Type;
129}
130
131string debReleaseIndex::TranslationIndexURI(const char *Type, const string &Section) const
132{
133 string Res;
134 if (Dist[Dist.size() - 1] == '/')
135 {
136 if (Dist != "/")
137 Res = URI + Dist;
138 else
139 Res = URI;
140 return Res + Type;
141 }
142 else
143 return URI + "dists/" + Dist + "/" + TranslationIndexURISuffix(Type, Section);
144}
145
4b42f43b
DK
146debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist) :
147 metaIndex(URI, Dist, "deb"), Trusted(CHECK_TRUST)
148{}
149
150debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist, bool const Trusted) :
151 metaIndex(URI, Dist, "deb") {
152 SetTrusted(Trusted);
7db98ffc
MZ
153}
154
5dd4c8b8
DK
155debReleaseIndex::~debReleaseIndex() {
156 for (map<string, vector<debSectionEntry const*> >::const_iterator A = ArchEntries.begin();
157 A != ArchEntries.end(); ++A)
158 for (vector<const debSectionEntry *>::const_iterator S = A->second.begin();
159 S != A->second.end(); ++S)
160 delete *S;
7a9f09bd
MV
161}
162
5dd4c8b8
DK
163vector <struct IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const {
164 vector <struct IndexTarget *>* IndexTargets = new vector <IndexTarget *>;
165
166 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
167 if (src != ArchEntries.end()) {
168 vector<debSectionEntry const*> const SectionEntries = src->second;
169 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
170 I != SectionEntries.end(); ++I) {
171 IndexTarget * Target = new IndexTarget();
172 Target->ShortDesc = "Sources";
173 Target->MetaKey = SourceIndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section);
174 Target->URI = SourceIndexURI(Target->ShortDesc.c_str(), (*I)->Section);
175 Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section);
176 IndexTargets->push_back (Target);
177 }
178 }
179
180 // Only source release
181 if (IndexTargets->empty() == false && ArchEntries.size() == 1)
182 return IndexTargets;
183
ab53c018 184 std::set<std::string> sections;
5dd4c8b8
DK
185 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
186 a != ArchEntries.end(); ++a) {
187 if (a->first == "source")
188 continue;
189 for (vector <const debSectionEntry *>::const_iterator I = a->second.begin();
190 I != a->second.end(); ++I) {
191 IndexTarget * Target = new IndexTarget();
192 Target->ShortDesc = "Packages";
193 Target->MetaKey = IndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section, a->first);
194 Target->URI = IndexURI(Target->ShortDesc.c_str(), (*I)->Section, a->first);
195 Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section, a->first);
196 IndexTargets->push_back (Target);
ab53c018
DK
197 sections.insert((*I)->Section);
198 }
199 }
200
05bb1e5d
DK
201 std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
202 if (lang.empty() == true)
203 return IndexTargets;
204
ab53c018
DK
205 // get the Translations:
206 // - if its a dists-style repository get the i18n/Index first
207 // - if its flat try to acquire files by guessing
208 if (Dist[Dist.size() - 1] == '/') {
ab53c018
DK
209 for (std::set<std::string>::const_iterator s = sections.begin();
210 s != sections.end(); ++s) {
211 for (std::vector<std::string>::const_iterator l = lang.begin();
212 l != lang.end(); l++) {
213 if (*l == "none") continue;
214 IndexTarget * Target = new OptionalIndexTarget();
215 Target->ShortDesc = "Translation-" + *l;
216 Target->MetaKey = TranslationIndexURISuffix(l->c_str(), *s);
217 Target->URI = TranslationIndexURI(l->c_str(), *s);
218 Target->Description = Info (Target->ShortDesc.c_str(), *s);
219 IndexTargets->push_back(Target);
220 }
221 }
222 } else {
223 for (std::set<std::string>::const_iterator s = sections.begin();
224 s != sections.end(); ++s) {
225 IndexTarget * Target = new OptionalIndexTarget();
226 Target->ShortDesc = "TranslationIndex";
227 Target->MetaKey = TranslationIndexURISuffix("Index", *s);
228 Target->URI = TranslationIndexURI("Index", *s);
229 Target->Description = Info (Target->ShortDesc.c_str(), *s);
230 IndexTargets->push_back (Target);
5dd4c8b8
DK
231 }
232 }
233
234 return IndexTargets;
7db98ffc
MZ
235}
236 /*}}}*/
5dd4c8b8 237bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
7db98ffc
MZ
238{
239 // special case for --print-uris
240 if (GetAll) {
241 vector <struct IndexTarget *> *targets = ComputeIndexTargets();
242 for (vector <struct IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); Target++) {
243 new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
495e5cb2 244 (*Target)->ShortDesc, HashString());
7db98ffc
MZ
245 }
246 }
8f9b141f 247
fe0f7911
DK
248 new pkgAcqMetaClearSig(Owner, MetaIndexURI("InRelease"),
249 MetaIndexInfo("InRelease"), "InRelease",
250 MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
251 MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
252 ComputeIndexTargets(),
253 new indexRecords (Dist));
254
5dd4c8b8 255 return true;
7db98ffc
MZ
256}
257
4b42f43b
DK
258void debReleaseIndex::SetTrusted(bool const Trusted)
259{
260 if (Trusted == true)
261 this->Trusted = ALWAYS_TRUSTED;
262 else
263 this->Trusted = NEVER_TRUSTED;
264}
265
7db98ffc
MZ
266bool debReleaseIndex::IsTrusted() const
267{
4b42f43b
DK
268 if (Trusted == ALWAYS_TRUSTED)
269 return true;
270 else if (Trusted == NEVER_TRUSTED)
271 return false;
272
273
4e0ad446 274 if(_config->FindB("APT::Authentication::TrustCDROM", false))
e8cdc56a
MV
275 if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
276 return true;
fe0f7911
DK
277
278 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
279 URItoFileName(MetaIndexURI("Release")) + ".gpg";
280
7db98ffc
MZ
281 if (FileExists(VerifiedSigFile))
282 return true;
fe0f7911
DK
283
284 VerifiedSigFile = _config->FindDir("Dir::State::lists") +
285 URItoFileName(MetaIndexURI("InRelease"));
286
287 return FileExists(VerifiedSigFile);
7db98ffc
MZ
288}
289
5dd4c8b8
DK
290vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
291 if (Indexes != NULL)
292 return Indexes;
293
294 Indexes = new vector <pkgIndexFile*>;
295 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
296 if (src != ArchEntries.end()) {
297 vector<debSectionEntry const*> const SectionEntries = src->second;
298 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
299 I != SectionEntries.end(); I++)
300 Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
301 }
302
303 // Only source release
304 if (Indexes->empty() == false && ArchEntries.size() == 1)
305 return Indexes;
306
307 std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
308 map<string, set<string> > sections;
309 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
310 a != ArchEntries.end(); ++a) {
311 if (a->first == "source")
312 continue;
313 for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
314 I != a->second.end(); I++) {
315 Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
316 sections[(*I)->Section].insert(lang.begin(), lang.end());
317 }
318 }
319
320 for (map<string, set<string> >::const_iterator s = sections.begin();
321 s != sections.end(); ++s)
322 for (set<string>::const_iterator l = s->second.begin();
1a31359b
DK
323 l != s->second.end(); l++) {
324 if (*l == "none") continue;
5dd4c8b8 325 Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
1a31359b 326 }
5dd4c8b8
DK
327
328 return Indexes;
329}
a7a5b0d9 330
5dd4c8b8
DK
331void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
332 for (vector<string>::const_iterator a = Archs.begin();
333 a != Archs.end(); ++a)
334 ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
335 delete Entry;
7db98ffc
MZ
336}
337
5dd4c8b8
DK
338void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
339 ArchEntries[Arch].push_back(Entry);
7db98ffc
MZ
340}
341
5dd4c8b8
DK
342void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
343 if (Entry->IsSrc == true)
344 PushSectionEntry("source", Entry);
345 else {
346 for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
347 a != ArchEntries.end(); ++a) {
348 a->second.push_back(Entry);
349 }
350 }
7db98ffc
MZ
351}
352
5dd4c8b8
DK
353debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
354 bool const &IsSrc): Section(Section), IsSrc(IsSrc)
355{}
356
7db98ffc
MZ
357class debSLTypeDebian : public pkgSourceList::Type
358{
359 protected:
360
5dd4c8b8
DK
361 bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
362 string const &Dist, string const &Section,
363 bool const &IsSrc, map<string, string> const &Options) const
7db98ffc 364 {
5dd4c8b8
DK
365 map<string, string>::const_iterator const arch = Options.find("arch");
366 vector<string> const Archs =
3f42500d 367 (arch != Options.end()) ? VectorizeString(arch->second, ',') :
5dd4c8b8 368 APT::Configuration::getArchitectures();
4b42f43b 369 map<string, string>::const_iterator const trusted = Options.find("trusted");
5dd4c8b8
DK
370
371 for (vector<metaIndex *>::const_iterator I = List.begin();
7db98ffc
MZ
372 I != List.end(); I++)
373 {
5dd4c8b8
DK
374 // We only worry about debian entries here
375 if (strcmp((*I)->GetType(), "deb") != 0)
376 continue;
377
378 debReleaseIndex *Deb = (debReleaseIndex *) (*I);
4b42f43b
DK
379 if (trusted != Options.end())
380 Deb->SetTrusted(StringToBool(trusted->second, false));
381
5dd4c8b8
DK
382 /* This check insures that there will be only one Release file
383 queued for all the Packages files and Sources files it
384 corresponds to. */
385 if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
7db98ffc 386 {
5dd4c8b8
DK
387 if (IsSrc == true)
388 Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
389 else
dd13742e
DK
390 {
391 if (Dist[Dist.size() - 1] == '/')
392 Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
393 else
394 Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
395 }
5dd4c8b8 396 return true;
7db98ffc
MZ
397 }
398 }
4b42f43b 399
7db98ffc 400 // No currently created Release file indexes this entry, so we create a new one.
4b42f43b
DK
401 debReleaseIndex *Deb;
402 if (trusted != Options.end())
403 Deb = new debReleaseIndex(URI, Dist, StringToBool(trusted->second, false));
404 else
405 Deb = new debReleaseIndex(URI, Dist);
406
5dd4c8b8
DK
407 if (IsSrc == true)
408 Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
409 else
dd13742e
DK
410 {
411 if (Dist[Dist.size() - 1] == '/')
412 Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
413 else
414 Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
415 }
7db98ffc
MZ
416 List.push_back(Deb);
417 return true;
418 }
419};
420
421class debSLTypeDeb : public debSLTypeDebian
422{
423 public:
424
5dd4c8b8
DK
425 bool CreateItem(vector<metaIndex *> &List, string const &URI,
426 string const &Dist, string const &Section,
427 std::map<string, string> const &Options) const
7db98ffc 428 {
5dd4c8b8 429 return CreateItemInternal(List, URI, Dist, Section, false, Options);
7db98ffc
MZ
430 }
431
432 debSLTypeDeb()
433 {
434 Name = "deb";
435 Label = "Standard Debian binary tree";
436 }
437};
438
439class debSLTypeDebSrc : public debSLTypeDebian
440{
441 public:
442
5dd4c8b8
DK
443 bool CreateItem(vector<metaIndex *> &List, string const &URI,
444 string const &Dist, string const &Section,
445 std::map<string, string> const &Options) const
7db98ffc 446 {
5dd4c8b8 447 return CreateItemInternal(List, URI, Dist, Section, true, Options);
7db98ffc
MZ
448 }
449
450 debSLTypeDebSrc()
451 {
452 Name = "deb-src";
453 Label = "Standard Debian source tree";
454 }
455};
456
457debSLTypeDeb _apt_DebType;
458debSLTypeDebSrc _apt_DebSrcType;