]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debmetaindex.cc
rework hashsum verification in the acquire system
[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 char const * const ShortDesc = "Sources";
196 IndexTarget * const Target = new IndexTarget(
197 SourceIndexURISuffix(ShortDesc, (*I)->Section),
198 ShortDesc,
199 Info(ShortDesc, (*I)->Section),
200 SourceIndexURI(ShortDesc, (*I)->Section)
201 );
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 char const * const ShortDesc = "Packages";
218 IndexTarget * const Target = new IndexTarget(
219 IndexURISuffix(ShortDesc, (*I)->Section, a->first),
220 ShortDesc,
221 Info (ShortDesc, (*I)->Section, a->first),
222 IndexURI(ShortDesc, (*I)->Section, a->first)
223 );
224 IndexTargets->push_back (Target);
225 sections.insert((*I)->Section);
226 }
227 }
228
229 std::vector<std::string> lang = APT::Configuration::getLanguages(true);
230 std::vector<std::string>::iterator lend = std::remove(lang.begin(), lang.end(), "none");
231 if (lend != lang.end())
232 lang.erase(lend);
233
234 if (lang.empty() == true)
235 return IndexTargets;
236
237 // get the Translation-* files, later we will skip download of non-existent if we have an index
238 for (std::set<std::string>::const_iterator s = sections.begin();
239 s != sections.end(); ++s) {
240 for (std::vector<std::string>::const_iterator l = lang.begin();
241 l != lang.end(); ++l) {
242 std::string const ShortDesc = "Translation-" + *l;
243 IndexTarget * const Target = new OptionalIndexTarget(
244 TranslationIndexURISuffix(l->c_str(), *s),
245 ShortDesc,
246 Info (ShortDesc.c_str(), *s),
247 TranslationIndexURI(l->c_str(), *s)
248 );
249 IndexTargets->push_back(Target);
250 }
251 }
252
253 return IndexTargets;
254 }
255 /*}}}*/
256 bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
257 {
258 indexRecords * const iR = new indexRecords(Dist);
259 if (Trusted == ALWAYS_TRUSTED)
260 iR->SetTrusted(true);
261 else if (Trusted == NEVER_TRUSTED)
262 iR->SetTrusted(false);
263
264 // special case for --print-uris
265 vector <IndexTarget *> const * const targets = ComputeIndexTargets();
266 #define APT_TARGET(X) IndexTarget("", X, MetaIndexInfo(X), MetaIndexURI(X))
267 pkgAcqMetaBase * const TransactionManager = new pkgAcqMetaClearSig(Owner,
268 APT_TARGET("InRelease"), APT_TARGET("Release"), APT_TARGET("Release.gpg"),
269 targets, iR);
270 #undef APT_TARGET
271 if (GetAll)
272 {
273 for (vector <IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); ++Target)
274 new pkgAcqIndex(Owner, TransactionManager, *Target);
275 }
276
277 return true;
278 }
279
280 void debReleaseIndex::SetTrusted(bool const Trusted)
281 {
282 if (Trusted == true)
283 this->Trusted = ALWAYS_TRUSTED;
284 else
285 this->Trusted = NEVER_TRUSTED;
286 }
287
288 bool debReleaseIndex::IsTrusted() const
289 {
290 if (Trusted == ALWAYS_TRUSTED)
291 return true;
292 else if (Trusted == NEVER_TRUSTED)
293 return false;
294
295
296 if(_config->FindB("APT::Authentication::TrustCDROM", false))
297 if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
298 return true;
299
300 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
301 URItoFileName(MetaIndexURI("Release")) + ".gpg";
302
303 if (FileExists(VerifiedSigFile))
304 return true;
305
306 VerifiedSigFile = _config->FindDir("Dir::State::lists") +
307 URItoFileName(MetaIndexURI("InRelease"));
308
309 return FileExists(VerifiedSigFile);
310 }
311
312 vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
313 if (Indexes != NULL)
314 return Indexes;
315
316 Indexes = new vector <pkgIndexFile*>;
317 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
318 if (src != ArchEntries.end()) {
319 vector<debSectionEntry const*> const SectionEntries = src->second;
320 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
321 I != SectionEntries.end(); ++I)
322 Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
323 }
324
325 // Only source release
326 if (Indexes->empty() == false && ArchEntries.size() == 1)
327 return Indexes;
328
329 std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
330 map<string, set<string> > sections;
331 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
332 a != ArchEntries.end(); ++a) {
333 if (a->first == "source")
334 continue;
335 for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
336 I != a->second.end(); ++I) {
337 Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
338 sections[(*I)->Section].insert(lang.begin(), lang.end());
339 }
340 }
341
342 for (map<string, set<string> >::const_iterator s = sections.begin();
343 s != sections.end(); ++s)
344 for (set<string>::const_iterator l = s->second.begin();
345 l != s->second.end(); ++l) {
346 if (*l == "none") continue;
347 Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
348 }
349
350 return Indexes;
351 }
352
353 void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
354 for (vector<string>::const_iterator a = Archs.begin();
355 a != Archs.end(); ++a)
356 ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
357 delete Entry;
358 }
359
360 void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
361 ArchEntries[Arch].push_back(Entry);
362 }
363
364 void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
365 if (Entry->IsSrc == true)
366 PushSectionEntry("source", Entry);
367 else {
368 for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
369 a != ArchEntries.end(); ++a) {
370 a->second.push_back(Entry);
371 }
372 }
373 }
374
375 debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
376 bool const &IsSrc): Section(Section), IsSrc(IsSrc)
377 {}
378
379 class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type
380 {
381 protected:
382
383 bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
384 string const &Dist, string const &Section,
385 bool const &IsSrc, map<string, string> const &Options) const
386 {
387 // parse arch=, arch+= and arch-= settings
388 map<string, string>::const_iterator arch = Options.find("arch");
389 vector<string> Archs;
390 if (arch != Options.end())
391 Archs = VectorizeString(arch->second, ',');
392 else
393 Archs = APT::Configuration::getArchitectures();
394
395 if ((arch = Options.find("arch+")) != Options.end())
396 {
397 std::vector<std::string> const plusArch = VectorizeString(arch->second, ',');
398 for (std::vector<std::string>::const_iterator plus = plusArch.begin(); plus != plusArch.end(); ++plus)
399 if (std::find(Archs.begin(), Archs.end(), *plus) == Archs.end())
400 Archs.push_back(*plus);
401 }
402 if ((arch = Options.find("arch-")) != Options.end())
403 {
404 std::vector<std::string> const minusArch = VectorizeString(arch->second, ',');
405 for (std::vector<std::string>::const_iterator minus = minusArch.begin(); minus != minusArch.end(); ++minus)
406 {
407 std::vector<std::string>::iterator kill = std::find(Archs.begin(), Archs.end(), *minus);
408 if (kill != Archs.end())
409 Archs.erase(kill);
410 }
411 }
412
413 map<string, string>::const_iterator const trusted = Options.find("trusted");
414
415 for (vector<metaIndex *>::const_iterator I = List.begin();
416 I != List.end(); ++I)
417 {
418 // We only worry about debian entries here
419 if (strcmp((*I)->GetType(), "deb") != 0)
420 continue;
421
422 debReleaseIndex *Deb = (debReleaseIndex *) (*I);
423 if (trusted != Options.end())
424 Deb->SetTrusted(StringToBool(trusted->second, false));
425
426 /* This check insures that there will be only one Release file
427 queued for all the Packages files and Sources files it
428 corresponds to. */
429 if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
430 {
431 if (IsSrc == true)
432 Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
433 else
434 {
435 if (Dist[Dist.size() - 1] == '/')
436 Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
437 else
438 Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
439 }
440 return true;
441 }
442 }
443
444 // No currently created Release file indexes this entry, so we create a new one.
445 debReleaseIndex *Deb;
446 if (trusted != Options.end())
447 Deb = new debReleaseIndex(URI, Dist, StringToBool(trusted->second, false));
448 else
449 Deb = new debReleaseIndex(URI, Dist);
450
451 if (IsSrc == true)
452 Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
453 else
454 {
455 if (Dist[Dist.size() - 1] == '/')
456 Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
457 else
458 Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
459 }
460 List.push_back(Deb);
461 return true;
462 }
463 };
464
465 debDebFileMetaIndex::debDebFileMetaIndex(std::string const &DebFile)
466 : metaIndex(DebFile, "local-uri", "deb-dist"), DebFile(DebFile)
467 {
468 DebIndex = new debDebPkgFileIndex(DebFile);
469 Indexes = new vector<pkgIndexFile *>();
470 Indexes->push_back(DebIndex);
471 }
472
473
474 class APT_HIDDEN debSLTypeDeb : public debSLTypeDebian
475 {
476 public:
477
478 bool CreateItem(vector<metaIndex *> &List, string const &URI,
479 string const &Dist, string const &Section,
480 std::map<string, string> const &Options) const
481 {
482 return CreateItemInternal(List, URI, Dist, Section, false, Options);
483 }
484
485 debSLTypeDeb()
486 {
487 Name = "deb";
488 Label = "Standard Debian binary tree";
489 }
490 };
491
492 class APT_HIDDEN debSLTypeDebSrc : public debSLTypeDebian
493 {
494 public:
495
496 bool CreateItem(vector<metaIndex *> &List, string const &URI,
497 string const &Dist, string const &Section,
498 std::map<string, string> const &Options) const
499 {
500 return CreateItemInternal(List, URI, Dist, Section, true, Options);
501 }
502
503 debSLTypeDebSrc()
504 {
505 Name = "deb-src";
506 Label = "Standard Debian source tree";
507 }
508 };
509
510 class APT_HIDDEN debSLTypeDebFile : public pkgSourceList::Type
511 {
512 public:
513
514 bool CreateItem(vector<metaIndex *> &List, string const &URI,
515 string const &/*Dist*/, string const &/*Section*/,
516 std::map<string, string> const &/*Options*/) const
517 {
518 metaIndex *mi = new debDebFileMetaIndex(URI);
519 List.push_back(mi);
520 return true;
521 }
522
523 debSLTypeDebFile()
524 {
525 Name = "deb-file";
526 Label = "Debian Deb File";
527 }
528 };
529
530 APT_HIDDEN debSLTypeDeb _apt_DebType;
531 APT_HIDDEN debSLTypeDebSrc _apt_DebSrcType;
532 APT_HIDDEN debSLTypeDebFile _apt_DebFileType;