]> git.saurik.com Git - apt.git/blame_incremental - apt-pkg/deb/debmetaindex.cc
Merge branch 'debian/sid' into debian/experimental
[apt.git] / apt-pkg / deb / debmetaindex.cc
... / ...
CommitLineData
1#include <config.h>
2
3#include <apt-pkg/debmetaindex.h>
4#include <apt-pkg/debindexfile.h>
5#include <apt-pkg/strutl.h>
6#include <apt-pkg/fileutl.h>
7#include <apt-pkg/acquire-item.h>
8#include <apt-pkg/configuration.h>
9#include <apt-pkg/aptconfiguration.h>
10#include <apt-pkg/indexrecords.h>
11#include <apt-pkg/sourcelist.h>
12#include <apt-pkg/hashes.h>
13#include <apt-pkg/macros.h>
14#include <apt-pkg/metaindex.h>
15
16#include <string.h>
17#include <map>
18#include <string>
19#include <utility>
20#include <vector>
21#include <set>
22#include <algorithm>
23
24using namespace std;
25
26string debReleaseIndex::Info(const char *Type, string const &Section, string const &Arch) const
27{
28 string Info = ::URI::SiteOnly(URI) + ' ';
29 if (Dist[Dist.size() - 1] == '/')
30 {
31 if (Dist != "/")
32 Info += Dist;
33 }
34 else
35 {
36 Info += Dist + '/' + Section;
37 if (Arch.empty() != true)
38 Info += " " + Arch;
39 }
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
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
97string debReleaseIndex::IndexURISuffix(const char *Type, string const &Section, string const &Arch) const
98{
99 string Res ="";
100 if (Dist[Dist.size() - 1] != '/')
101 {
102 if (Arch == "native")
103 Res += Section + "/binary-" + _config->Find("APT::Architecture") + '/';
104 else
105 Res += Section + "/binary-" + Arch + '/';
106 }
107 return Res + Type;
108}
109
110
111string debReleaseIndex::IndexURI(const char *Type, string const &Section, string const &Arch) const
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
123 return URI + "dists/" + Dist + '/' + IndexURISuffix(Type, Section, Arch);
124 }
125
126string debReleaseIndex::SourceIndexURISuffix(const char *Type, const string &Section) const
127{
128 string Res ="";
129 if (Dist[Dist.size() - 1] != '/')
130 Res += Section + "/source/";
131 return Res + Type;
132}
133
134string debReleaseIndex::SourceIndexURI(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 + "/" + SourceIndexURISuffix(Type, Section);
147}
148
149string debReleaseIndex::TranslationIndexURISuffix(const char *Type, const string &Section) const
150{
151 string Res ="";
152 if (Dist[Dist.size() - 1] != '/')
153 Res += Section + "/i18n/Translation-";
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
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);
179}
180
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;
187}
188
189vector <IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const {
190 vector <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
210 std::set<std::string> sections;
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);
223 sections.insert((*I)->Section);
224 }
225 }
226
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
232 if (lang.empty() == true)
233 return IndexTargets;
234
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);
244 Target->Description = Info (Target->ShortDesc.c_str(), *s);
245 IndexTargets->push_back(Target);
246 }
247 }
248
249 return IndexTargets;
250}
251 /*}}}*/
252bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
253{
254 bool const tryInRelease = _config->FindB("Acquire::TryInRelease", true);
255
256 indexRecords * const iR = new indexRecords(Dist);
257 if (Trusted == ALWAYS_TRUSTED)
258 iR->SetTrusted(true);
259 else if (Trusted == NEVER_TRUSTED)
260 iR->SetTrusted(false);
261
262 // special case for --print-uris
263 if (GetAll) {
264 vector <IndexTarget *> *targets = ComputeIndexTargets();
265 for (vector <IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); ++Target) {
266 new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
267 (*Target)->ShortDesc, HashStringList());
268 }
269 delete targets;
270
271 // this is normally created in pkgAcqMetaSig, but if we run
272 // in --print-uris mode, we add it here
273 if (tryInRelease == false)
274 new pkgAcqMetaIndex(Owner, NULL,
275 MetaIndexURI("Release"),
276 MetaIndexInfo("Release"), "Release",
277 MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
278 ComputeIndexTargets(),
279 iR);
280 }
281 if (tryInRelease == true)
282 new pkgAcqMetaClearSig(Owner,
283 MetaIndexURI("InRelease"), MetaIndexInfo("InRelease"), "InRelease",
284 MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
285 MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
286 ComputeIndexTargets(),
287 iR);
288 else
289 new pkgAcqMetaIndex(Owner, NULL,
290 MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
291 MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
292 ComputeIndexTargets(),
293 iR);
294
295 return true;
296}
297
298void debReleaseIndex::SetTrusted(bool const Trusted)
299{
300 if (Trusted == true)
301 this->Trusted = ALWAYS_TRUSTED;
302 else
303 this->Trusted = NEVER_TRUSTED;
304}
305
306bool debReleaseIndex::IsTrusted() const
307{
308 if (Trusted == ALWAYS_TRUSTED)
309 return true;
310 else if (Trusted == NEVER_TRUSTED)
311 return false;
312
313
314 if(_config->FindB("APT::Authentication::TrustCDROM", false))
315 if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
316 return true;
317
318 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
319 URItoFileName(MetaIndexURI("Release")) + ".gpg";
320
321 if (FileExists(VerifiedSigFile))
322 return true;
323
324 VerifiedSigFile = _config->FindDir("Dir::State::lists") +
325 URItoFileName(MetaIndexURI("InRelease"));
326
327 return FileExists(VerifiedSigFile);
328}
329
330vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
331 if (Indexes != NULL)
332 return Indexes;
333
334 Indexes = new vector <pkgIndexFile*>;
335 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
336 if (src != ArchEntries.end()) {
337 vector<debSectionEntry const*> const SectionEntries = src->second;
338 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
339 I != SectionEntries.end(); ++I)
340 Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
341 }
342
343 // Only source release
344 if (Indexes->empty() == false && ArchEntries.size() == 1)
345 return Indexes;
346
347 std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
348 map<string, set<string> > sections;
349 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
350 a != ArchEntries.end(); ++a) {
351 if (a->first == "source")
352 continue;
353 for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
354 I != a->second.end(); ++I) {
355 Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
356 sections[(*I)->Section].insert(lang.begin(), lang.end());
357 }
358 }
359
360 for (map<string, set<string> >::const_iterator s = sections.begin();
361 s != sections.end(); ++s)
362 for (set<string>::const_iterator l = s->second.begin();
363 l != s->second.end(); ++l) {
364 if (*l == "none") continue;
365 Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
366 }
367
368 return Indexes;
369}
370
371void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
372 for (vector<string>::const_iterator a = Archs.begin();
373 a != Archs.end(); ++a)
374 ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
375 delete Entry;
376}
377
378void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
379 ArchEntries[Arch].push_back(Entry);
380}
381
382void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
383 if (Entry->IsSrc == true)
384 PushSectionEntry("source", Entry);
385 else {
386 for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
387 a != ArchEntries.end(); ++a) {
388 a->second.push_back(Entry);
389 }
390 }
391}
392
393debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
394 bool const &IsSrc): Section(Section), IsSrc(IsSrc)
395{}
396
397class debSLTypeDebian : public pkgSourceList::Type
398{
399 protected:
400
401 bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
402 string const &Dist, string const &Section,
403 bool const &IsSrc, map<string, string> const &Options) const
404 {
405 // parse arch=, arch+= and arch-= settings
406 map<string, string>::const_iterator arch = Options.find("arch");
407 vector<string> Archs =
408 (arch != Options.end()) ? VectorizeString(arch->second, ',') :
409 APT::Configuration::getArchitectures();
410 if ((arch = Options.find("arch+")) != Options.end())
411 {
412 std::vector<std::string> const plusArch = VectorizeString(arch->second, ',');
413 for (std::vector<std::string>::const_iterator plus = plusArch.begin(); plus != plusArch.end(); ++plus)
414 if (std::find(Archs.begin(), Archs.end(), *plus) == Archs.end())
415 Archs.push_back(*plus);
416 }
417 if ((arch = Options.find("arch-")) != Options.end())
418 {
419 std::vector<std::string> const minusArch = VectorizeString(arch->second, ',');
420 for (std::vector<std::string>::const_iterator minus = minusArch.begin(); minus != minusArch.end(); ++minus)
421 {
422 std::vector<std::string>::iterator kill = std::find(Archs.begin(), Archs.end(), *minus);
423 if (kill != Archs.end())
424 Archs.erase(kill);
425 }
426 }
427
428 map<string, string>::const_iterator const trusted = Options.find("trusted");
429
430 for (vector<metaIndex *>::const_iterator I = List.begin();
431 I != List.end(); ++I)
432 {
433 // We only worry about debian entries here
434 if (strcmp((*I)->GetType(), "deb") != 0)
435 continue;
436
437 debReleaseIndex *Deb = (debReleaseIndex *) (*I);
438 if (trusted != Options.end())
439 Deb->SetTrusted(StringToBool(trusted->second, false));
440
441 /* This check insures that there will be only one Release file
442 queued for all the Packages files and Sources files it
443 corresponds to. */
444 if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
445 {
446 if (IsSrc == true)
447 Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
448 else
449 {
450 if (Dist[Dist.size() - 1] == '/')
451 Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
452 else
453 Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
454 }
455 return true;
456 }
457 }
458
459 // No currently created Release file indexes this entry, so we create a new one.
460 debReleaseIndex *Deb;
461 if (trusted != Options.end())
462 Deb = new debReleaseIndex(URI, Dist, StringToBool(trusted->second, false));
463 else
464 Deb = new debReleaseIndex(URI, Dist);
465
466 if (IsSrc == true)
467 Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
468 else
469 {
470 if (Dist[Dist.size() - 1] == '/')
471 Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
472 else
473 Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
474 }
475 List.push_back(Deb);
476 return true;
477 }
478};
479
480debDebFileMetaIndex::debDebFileMetaIndex(std::string const &DebFile)
481 : metaIndex(DebFile, "local-uri", "deb-dist"), DebFile(DebFile)
482{
483 DebIndex = new debDebPkgFileIndex(DebFile);
484 Indexes = new vector<pkgIndexFile *>();
485 Indexes->push_back(DebIndex);
486}
487
488
489class debSLTypeDeb : public debSLTypeDebian
490{
491 public:
492
493 bool CreateItem(vector<metaIndex *> &List, string const &URI,
494 string const &Dist, string const &Section,
495 std::map<string, string> const &Options) const
496 {
497 return CreateItemInternal(List, URI, Dist, Section, false, Options);
498 }
499
500 debSLTypeDeb()
501 {
502 Name = "deb";
503 Label = "Standard Debian binary tree";
504 }
505};
506
507class debSLTypeDebSrc : public debSLTypeDebian
508{
509 public:
510
511 bool CreateItem(vector<metaIndex *> &List, string const &URI,
512 string const &Dist, string const &Section,
513 std::map<string, string> const &Options) const
514 {
515 return CreateItemInternal(List, URI, Dist, Section, true, Options);
516 }
517
518 debSLTypeDebSrc()
519 {
520 Name = "deb-src";
521 Label = "Standard Debian source tree";
522 }
523};
524
525class debSLTypeDebFile : public pkgSourceList::Type
526{
527 public:
528
529 bool CreateItem(vector<metaIndex *> &List, string const &URI,
530 string const &/*Dist*/, string const &/*Section*/,
531 std::map<string, string> const &/*Options*/) const
532 {
533 metaIndex *mi = new debDebFileMetaIndex(URI);
534 List.push_back(mi);
535 return true;
536 }
537
538 debSLTypeDebFile()
539 {
540 Name = "deb-file";
541 Label = "Debian Deb File";
542 }
543};
544debSLTypeDeb _apt_DebType;
545debSLTypeDebSrc _apt_DebSrcType;
546debSLTypeDebFile _apt_DebFileType;