]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debmetaindex.cc
d99fd83932040c1d3ea6b26ed08c5ca44989171a
[apt.git] / apt-pkg / deb / debmetaindex.cc
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
24 using namespace std;
25
26 string 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
45 string 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
60 string debReleaseIndex::MetaIndexFile(const char *Type) const
61 {
62 return _config->FindDir("Dir::State::lists") +
63 URItoFileName(MetaIndexURI(Type));
64 }
65
66 string 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 std::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 }
94
95 string debReleaseIndex::IndexURISuffix(const char *Type, string const &Section, string const &Arch) const
96 {
97 string Res ="";
98 if (Dist[Dist.size() - 1] != '/')
99 {
100 if (Arch == "native")
101 Res += Section + "/binary-" + _config->Find("APT::Architecture") + '/';
102 else
103 Res += Section + "/binary-" + Arch + '/';
104 }
105 return Res + Type;
106 }
107
108
109 string debReleaseIndex::IndexURI(const char *Type, string const &Section, string const &Arch) const
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
121 return URI + "dists/" + Dist + '/' + IndexURISuffix(Type, Section, Arch);
122 }
123
124 string debReleaseIndex::SourceIndexURISuffix(const char *Type, const string &Section) const
125 {
126 string Res ="";
127 if (Dist[Dist.size() - 1] != '/')
128 Res += Section + "/source/";
129 return Res + Type;
130 }
131
132 string debReleaseIndex::SourceIndexURI(const char *Type, const string &Section) const
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
147 string debReleaseIndex::TranslationIndexURISuffix(const char *Type, const string &Section) const
148 {
149 string Res ="";
150 if (Dist[Dist.size() - 1] != '/')
151 Res += Section + "/i18n/Translation-";
152 return Res + Type;
153 }
154
155 string 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
170 debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist) :
171 metaIndex(URI, Dist, "deb"), Trusted(CHECK_TRUST)
172 {}
173
174 debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist, bool const Trusted) :
175 metaIndex(URI, Dist, "deb") {
176 SetTrusted(Trusted);
177 }
178
179 debReleaseIndex::~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;
185 }
186
187 vector <IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const {
188 vector <IndexTarget *>* IndexTargets = new vector <IndexTarget *>;
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
208 std::set<std::string> sections;
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);
221 sections.insert((*I)->Section);
222 }
223 }
224
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
230 if (lang.empty() == true)
231 return IndexTargets;
232
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);
242 Target->Description = Info (Target->ShortDesc.c_str(), *s);
243 IndexTargets->push_back(Target);
244 }
245 }
246
247 return IndexTargets;
248 }
249 /*}}}*/
250 bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
251 {
252 bool const tryInRelease = _config->FindB("Acquire::TryInRelease", true);
253
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
260 // special case for --print-uris
261 if (GetAll) {
262 vector <IndexTarget *> *targets = ComputeIndexTargets();
263 for (vector <IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); ++Target) {
264 new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
265 (*Target)->ShortDesc, HashStringList());
266 }
267 delete targets;
268
269 // this is normally created in pkgAcqMetaSig, but if we run
270 // in --print-uris mode, we add it here
271 if (tryInRelease == false)
272 new pkgAcqMetaIndex(Owner, NULL,
273 MetaIndexURI("Release"),
274 MetaIndexInfo("Release"), "Release",
275 MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
276 ComputeIndexTargets(),
277 iR);
278 }
279 if (tryInRelease == true)
280 new pkgAcqMetaClearSig(Owner,
281 MetaIndexURI("InRelease"), MetaIndexInfo("InRelease"), "InRelease",
282 MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
283 MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
284 ComputeIndexTargets(),
285 iR);
286 else
287 new pkgAcqMetaIndex(Owner, NULL,
288 MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
289 MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
290 ComputeIndexTargets(),
291 iR);
292
293 return true;
294 }
295
296 void debReleaseIndex::SetTrusted(bool const Trusted)
297 {
298 if (Trusted == true)
299 this->Trusted = ALWAYS_TRUSTED;
300 else
301 this->Trusted = NEVER_TRUSTED;
302 }
303
304 bool debReleaseIndex::IsTrusted() const
305 {
306 if (Trusted == ALWAYS_TRUSTED)
307 return true;
308 else if (Trusted == NEVER_TRUSTED)
309 return false;
310
311
312 if(_config->FindB("APT::Authentication::TrustCDROM", false))
313 if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
314 return true;
315
316 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
317 URItoFileName(MetaIndexURI("Release")) + ".gpg";
318
319 if (FileExists(VerifiedSigFile))
320 return true;
321
322 VerifiedSigFile = _config->FindDir("Dir::State::lists") +
323 URItoFileName(MetaIndexURI("InRelease"));
324
325 return FileExists(VerifiedSigFile);
326 }
327
328 vector <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();
337 I != SectionEntries.end(); ++I)
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();
352 I != a->second.end(); ++I) {
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();
361 l != s->second.end(); ++l) {
362 if (*l == "none") continue;
363 Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
364 }
365
366 return Indexes;
367 }
368
369 void 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;
374 }
375
376 void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
377 ArchEntries[Arch].push_back(Entry);
378 }
379
380 void 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 }
389 }
390
391 debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
392 bool const &IsSrc): Section(Section), IsSrc(IsSrc)
393 {}
394
395 class debSLTypeDebian : public pkgSourceList::Type
396 {
397 protected:
398
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
402 {
403 // parse arch=, arch+= and arch-= settings
404 map<string, string>::const_iterator arch = Options.find("arch");
405 vector<string> Archs =
406 (arch != Options.end()) ? VectorizeString(arch->second, ',') :
407 APT::Configuration::getArchitectures();
408 if ((arch = Options.find("arch+")) != Options.end())
409 {
410 std::vector<std::string> const plusArch = VectorizeString(arch->second, ',');
411 for (std::vector<std::string>::const_iterator plus = plusArch.begin(); plus != plusArch.end(); ++plus)
412 if (std::find(Archs.begin(), Archs.end(), *plus) == Archs.end())
413 Archs.push_back(*plus);
414 }
415 if ((arch = Options.find("arch-")) != Options.end())
416 {
417 std::vector<std::string> const minusArch = VectorizeString(arch->second, ',');
418 for (std::vector<std::string>::const_iterator minus = minusArch.begin(); minus != minusArch.end(); ++minus)
419 {
420 std::vector<std::string>::iterator kill = std::find(Archs.begin(), Archs.end(), *minus);
421 if (kill != Archs.end())
422 Archs.erase(kill);
423 }
424 }
425
426 map<string, string>::const_iterator const trusted = Options.find("trusted");
427
428 for (vector<metaIndex *>::const_iterator I = List.begin();
429 I != List.end(); ++I)
430 {
431 // We only worry about debian entries here
432 if (strcmp((*I)->GetType(), "deb") != 0)
433 continue;
434
435 debReleaseIndex *Deb = (debReleaseIndex *) (*I);
436 if (trusted != Options.end())
437 Deb->SetTrusted(StringToBool(trusted->second, false));
438
439 /* This check insures that there will be only one Release file
440 queued for all the Packages files and Sources files it
441 corresponds to. */
442 if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
443 {
444 if (IsSrc == true)
445 Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
446 else
447 {
448 if (Dist[Dist.size() - 1] == '/')
449 Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
450 else
451 Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
452 }
453 return true;
454 }
455 }
456
457 // No currently created Release file indexes this entry, so we create a new one.
458 debReleaseIndex *Deb;
459 if (trusted != Options.end())
460 Deb = new debReleaseIndex(URI, Dist, StringToBool(trusted->second, false));
461 else
462 Deb = new debReleaseIndex(URI, Dist);
463
464 if (IsSrc == true)
465 Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
466 else
467 {
468 if (Dist[Dist.size() - 1] == '/')
469 Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
470 else
471 Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
472 }
473 List.push_back(Deb);
474 return true;
475 }
476 };
477
478 debDebFileMetaIndex::debDebFileMetaIndex(std::string const &DebFile)
479 : metaIndex(DebFile, "local-uri", "deb-dist"), DebFile(DebFile)
480 {
481 DebIndex = new debDebPkgFileIndex(DebFile);
482 Indexes = new vector<pkgIndexFile *>();
483 Indexes->push_back(DebIndex);
484 }
485
486
487 class debSLTypeDeb : public debSLTypeDebian
488 {
489 public:
490
491 bool CreateItem(vector<metaIndex *> &List, string const &URI,
492 string const &Dist, string const &Section,
493 std::map<string, string> const &Options) const
494 {
495 return CreateItemInternal(List, URI, Dist, Section, false, Options);
496 }
497
498 debSLTypeDeb()
499 {
500 Name = "deb";
501 Label = "Standard Debian binary tree";
502 }
503 };
504
505 class debSLTypeDebSrc : public debSLTypeDebian
506 {
507 public:
508
509 bool CreateItem(vector<metaIndex *> &List, string const &URI,
510 string const &Dist, string const &Section,
511 std::map<string, string> const &Options) const
512 {
513 return CreateItemInternal(List, URI, Dist, Section, true, Options);
514 }
515
516 debSLTypeDebSrc()
517 {
518 Name = "deb-src";
519 Label = "Standard Debian source tree";
520 }
521 };
522
523 class debSLTypeDebFile : public pkgSourceList::Type
524 {
525 public:
526
527 bool CreateItem(vector<metaIndex *> &List, string const &URI,
528 string const &/*Dist*/, string const &/*Section*/,
529 std::map<string, string> const &/*Options*/) const
530 {
531 metaIndex *mi = new debDebFileMetaIndex(URI);
532 List.push_back(mi);
533 return true;
534 }
535
536 debSLTypeDebFile()
537 {
538 Name = "deb-file";
539 Label = "Debian Deb File";
540 }
541 };
542 debSLTypeDeb _apt_DebType;
543 debSLTypeDebSrc _apt_DebSrcType;
544 debSLTypeDebFile _apt_DebFileType;