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