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