Merge remote-tracking branch 'donkult/debian/sid' into debian/sid
[ntk/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 #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
82 std::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
97 string 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
111 string 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
126 string 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
134 string 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
149 string 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
157 string 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
172 debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist) :
173 metaIndex(URI, Dist, "deb"), Trusted(CHECK_TRUST)
174 {}
175
176 debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist, bool const Trusted) :
177 metaIndex(URI, Dist, "deb") {
178 SetTrusted(Trusted);
179 }
180
181 debReleaseIndex::~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
189 vector <struct IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const {
190 vector <struct 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 /*}}}*/
252 bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
253 {
254 bool const tryInRelease = _config->FindB("Acquire::TryInRelease", true);
255
256 // special case for --print-uris
257 if (GetAll) {
258 vector <struct IndexTarget *> *targets = ComputeIndexTargets();
259 for (vector <struct IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); ++Target) {
260 new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
261 (*Target)->ShortDesc, HashString());
262 }
263 delete targets;
264
265 // this is normally created in pkgAcqMetaSig, but if we run
266 // in --print-uris mode, we add it here
267 if (tryInRelease == false)
268 new pkgAcqMetaIndex(Owner, MetaIndexURI("Release"),
269 MetaIndexInfo("Release"), "Release",
270 MetaIndexURI("Release.gpg"),
271 ComputeIndexTargets(),
272 new indexRecords (Dist));
273 }
274
275 if (tryInRelease == true)
276 new pkgAcqMetaClearSig(Owner, MetaIndexURI("InRelease"),
277 MetaIndexInfo("InRelease"), "InRelease",
278 MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
279 MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
280 ComputeIndexTargets(),
281 new indexRecords (Dist));
282 else
283 new pkgAcqMetaSig(Owner, MetaIndexURI("Release.gpg"),
284 MetaIndexInfo("Release.gpg"), "Release.gpg",
285 MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
286 ComputeIndexTargets(),
287 new indexRecords (Dist));
288
289 return true;
290 }
291
292 void debReleaseIndex::SetTrusted(bool const Trusted)
293 {
294 if (Trusted == true)
295 this->Trusted = ALWAYS_TRUSTED;
296 else
297 this->Trusted = NEVER_TRUSTED;
298 }
299
300 bool debReleaseIndex::IsTrusted() const
301 {
302 if (Trusted == ALWAYS_TRUSTED)
303 return true;
304 else if (Trusted == NEVER_TRUSTED)
305 return false;
306
307
308 if(_config->FindB("APT::Authentication::TrustCDROM", false))
309 if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
310 return true;
311
312 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
313 URItoFileName(MetaIndexURI("Release")) + ".gpg";
314
315 if (FileExists(VerifiedSigFile))
316 return true;
317
318 VerifiedSigFile = _config->FindDir("Dir::State::lists") +
319 URItoFileName(MetaIndexURI("InRelease"));
320
321 return FileExists(VerifiedSigFile);
322 }
323
324 vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
325 if (Indexes != NULL)
326 return Indexes;
327
328 Indexes = new vector <pkgIndexFile*>;
329 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
330 if (src != ArchEntries.end()) {
331 vector<debSectionEntry const*> const SectionEntries = src->second;
332 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
333 I != SectionEntries.end(); ++I)
334 Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
335 }
336
337 // Only source release
338 if (Indexes->empty() == false && ArchEntries.size() == 1)
339 return Indexes;
340
341 std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
342 map<string, set<string> > sections;
343 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
344 a != ArchEntries.end(); ++a) {
345 if (a->first == "source")
346 continue;
347 for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
348 I != a->second.end(); ++I) {
349 Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
350 sections[(*I)->Section].insert(lang.begin(), lang.end());
351 }
352 }
353
354 for (map<string, set<string> >::const_iterator s = sections.begin();
355 s != sections.end(); ++s)
356 for (set<string>::const_iterator l = s->second.begin();
357 l != s->second.end(); ++l) {
358 if (*l == "none") continue;
359 Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
360 }
361
362 return Indexes;
363 }
364
365 void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
366 for (vector<string>::const_iterator a = Archs.begin();
367 a != Archs.end(); ++a)
368 ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
369 delete Entry;
370 }
371
372 void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
373 ArchEntries[Arch].push_back(Entry);
374 }
375
376 void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
377 if (Entry->IsSrc == true)
378 PushSectionEntry("source", Entry);
379 else {
380 for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
381 a != ArchEntries.end(); ++a) {
382 a->second.push_back(Entry);
383 }
384 }
385 }
386
387 debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
388 bool const &IsSrc): Section(Section), IsSrc(IsSrc)
389 {}
390
391 class debSLTypeDebian : public pkgSourceList::Type
392 {
393 protected:
394
395 bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
396 string const &Dist, string const &Section,
397 bool const &IsSrc, map<string, string> const &Options) const
398 {
399 // parse arch=, arch+= and arch-= settings
400 map<string, string>::const_iterator arch = Options.find("arch");
401 vector<string> Archs =
402 (arch != Options.end()) ? VectorizeString(arch->second, ',') :
403 APT::Configuration::getArchitectures();
404 if ((arch = Options.find("arch+")) != Options.end())
405 {
406 std::vector<std::string> const plusArch = VectorizeString(arch->second, ',');
407 for (std::vector<std::string>::const_iterator plus = plusArch.begin(); plus != plusArch.end(); ++plus)
408 if (std::find(Archs.begin(), Archs.end(), *plus) == Archs.end())
409 Archs.push_back(*plus);
410 }
411 if ((arch = Options.find("arch-")) != Options.end())
412 {
413 std::vector<std::string> const minusArch = VectorizeString(arch->second, ',');
414 for (std::vector<std::string>::const_iterator minus = minusArch.begin(); minus != minusArch.end(); ++minus)
415 {
416 std::vector<std::string>::iterator kill = std::find(Archs.begin(), Archs.end(), *minus);
417 if (kill != Archs.end())
418 Archs.erase(kill);
419 }
420 }
421
422 map<string, string>::const_iterator const trusted = Options.find("trusted");
423
424 for (vector<metaIndex *>::const_iterator I = List.begin();
425 I != List.end(); ++I)
426 {
427 // We only worry about debian entries here
428 if (strcmp((*I)->GetType(), "deb") != 0)
429 continue;
430
431 debReleaseIndex *Deb = (debReleaseIndex *) (*I);
432 if (trusted != Options.end())
433 Deb->SetTrusted(StringToBool(trusted->second, false));
434
435 /* This check insures that there will be only one Release file
436 queued for all the Packages files and Sources files it
437 corresponds to. */
438 if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
439 {
440 if (IsSrc == true)
441 Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
442 else
443 {
444 if (Dist[Dist.size() - 1] == '/')
445 Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
446 else
447 Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
448 }
449 return true;
450 }
451 }
452
453 // No currently created Release file indexes this entry, so we create a new one.
454 debReleaseIndex *Deb;
455 if (trusted != Options.end())
456 Deb = new debReleaseIndex(URI, Dist, StringToBool(trusted->second, false));
457 else
458 Deb = new debReleaseIndex(URI, Dist);
459
460 if (IsSrc == true)
461 Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
462 else
463 {
464 if (Dist[Dist.size() - 1] == '/')
465 Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
466 else
467 Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
468 }
469 List.push_back(Deb);
470 return true;
471 }
472 };
473
474 class 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 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 debSLTypeDeb _apt_DebType;
511 debSLTypeDebSrc _apt_DebSrcType;