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