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