Merge remote-tracking branch 'donkult/feature/clientmergepdiffs' into debian/experime...
[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/error.h>
13
14 #include <set>
15 #include <algorithm>
16
17 using namespace std;
18
19 string debReleaseIndex::Info(const char *Type, string const &Section, string const &Arch) const
20 {
21 string Info = ::URI::SiteOnly(URI) + ' ';
22 if (Dist[Dist.size() - 1] == '/')
23 {
24 if (Dist != "/")
25 Info += Dist;
26 }
27 else
28 {
29 Info += Dist + '/' + Section;
30 if (Arch.empty() != true)
31 Info += " " + Arch;
32 }
33 Info += " ";
34 Info += Type;
35 return Info;
36 }
37
38 string debReleaseIndex::MetaIndexInfo(const char *Type) const
39 {
40 string Info = ::URI::SiteOnly(URI) + ' ';
41 if (Dist[Dist.size() - 1] == '/')
42 {
43 if (Dist != "/")
44 Info += Dist;
45 }
46 else
47 Info += Dist;
48 Info += " ";
49 Info += Type;
50 return Info;
51 }
52
53 string debReleaseIndex::MetaIndexFile(const char *Type) const
54 {
55 return _config->FindDir("Dir::State::lists") +
56 URItoFileName(MetaIndexURI(Type));
57 }
58
59 string debReleaseIndex::MetaIndexURI(const char *Type) const
60 {
61 string Res;
62
63 if (Dist == "/")
64 Res = URI;
65 else if (Dist[Dist.size()-1] == '/')
66 Res = URI + Dist;
67 else
68 Res = URI + "dists/" + Dist + "/";
69
70 Res += Type;
71 return Res;
72 }
73
74 #if (APT_PKG_MAJOR >= 4 && APT_PKG_MINOR >= 13)
75 std::string debReleaseIndex::LocalFileName() const
76 {
77 // see if we have a InRelease file
78 std::string PathInRelease = MetaIndexFile("InRelease");
79 if (FileExists(PathInRelease))
80 return PathInRelease;
81
82 // and if not return the normal one
83 if (FileExists(PathInRelease))
84 return MetaIndexFile("Release");
85
86 return "";
87 }
88 #endif
89
90 string debReleaseIndex::IndexURISuffix(const char *Type, string const &Section, string const &Arch) const
91 {
92 string Res ="";
93 if (Dist[Dist.size() - 1] != '/')
94 {
95 if (Arch == "native")
96 Res += Section + "/binary-" + _config->Find("APT::Architecture") + '/';
97 else
98 Res += Section + "/binary-" + Arch + '/';
99 }
100 return Res + Type;
101 }
102
103
104 string debReleaseIndex::IndexURI(const char *Type, string const &Section, string const &Arch) const
105 {
106 if (Dist[Dist.size() - 1] == '/')
107 {
108 string Res;
109 if (Dist != "/")
110 Res = URI + Dist;
111 else
112 Res = URI;
113 return Res + Type;
114 }
115 else
116 return URI + "dists/" + Dist + '/' + IndexURISuffix(Type, Section, Arch);
117 }
118
119 string debReleaseIndex::SourceIndexURISuffix(const char *Type, const string &Section) const
120 {
121 string Res ="";
122 if (Dist[Dist.size() - 1] != '/')
123 Res += Section + "/source/";
124 return Res + Type;
125 }
126
127 string debReleaseIndex::SourceIndexURI(const char *Type, const string &Section) const
128 {
129 string Res;
130 if (Dist[Dist.size() - 1] == '/')
131 {
132 if (Dist != "/")
133 Res = URI + Dist;
134 else
135 Res = URI;
136 return Res + Type;
137 }
138 else
139 return URI + "dists/" + Dist + "/" + SourceIndexURISuffix(Type, Section);
140 }
141
142 string debReleaseIndex::TranslationIndexURISuffix(const char *Type, const string &Section) const
143 {
144 string Res ="";
145 if (Dist[Dist.size() - 1] != '/')
146 Res += Section + "/i18n/Translation-";
147 return Res + Type;
148 }
149
150 string debReleaseIndex::TranslationIndexURI(const char *Type, const string &Section) const
151 {
152 string Res;
153 if (Dist[Dist.size() - 1] == '/')
154 {
155 if (Dist != "/")
156 Res = URI + Dist;
157 else
158 Res = URI;
159 return Res + Type;
160 }
161 else
162 return URI + "dists/" + Dist + "/" + TranslationIndexURISuffix(Type, Section);
163 }
164
165 debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist) :
166 metaIndex(URI, Dist, "deb"), Trusted(CHECK_TRUST)
167 {}
168
169 debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist, bool const Trusted) :
170 metaIndex(URI, Dist, "deb") {
171 SetTrusted(Trusted);
172 }
173
174 debReleaseIndex::~debReleaseIndex() {
175 for (map<string, vector<debSectionEntry const*> >::const_iterator A = ArchEntries.begin();
176 A != ArchEntries.end(); ++A)
177 for (vector<const debSectionEntry *>::const_iterator S = A->second.begin();
178 S != A->second.end(); ++S)
179 delete *S;
180 }
181
182 vector <struct IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const {
183 vector <struct IndexTarget *>* IndexTargets = new vector <IndexTarget *>;
184
185 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
186 if (src != ArchEntries.end()) {
187 vector<debSectionEntry const*> const SectionEntries = src->second;
188 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
189 I != SectionEntries.end(); ++I) {
190 IndexTarget * Target = new IndexTarget();
191 Target->ShortDesc = "Sources";
192 Target->MetaKey = SourceIndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section);
193 Target->URI = SourceIndexURI(Target->ShortDesc.c_str(), (*I)->Section);
194 Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section);
195 IndexTargets->push_back (Target);
196 }
197 }
198
199 // Only source release
200 if (IndexTargets->empty() == false && ArchEntries.size() == 1)
201 return IndexTargets;
202
203 std::set<std::string> sections;
204 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
205 a != ArchEntries.end(); ++a) {
206 if (a->first == "source")
207 continue;
208 for (vector <const debSectionEntry *>::const_iterator I = a->second.begin();
209 I != a->second.end(); ++I) {
210 IndexTarget * Target = new IndexTarget();
211 Target->ShortDesc = "Packages";
212 Target->MetaKey = IndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section, a->first);
213 Target->URI = IndexURI(Target->ShortDesc.c_str(), (*I)->Section, a->first);
214 Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section, a->first);
215 IndexTargets->push_back (Target);
216 sections.insert((*I)->Section);
217 }
218 }
219
220 std::vector<std::string> lang = APT::Configuration::getLanguages(true);
221 std::vector<std::string>::iterator lend = std::remove(lang.begin(), lang.end(), "none");
222 if (lend != lang.end())
223 lang.erase(lend);
224
225 if (lang.empty() == true)
226 return IndexTargets;
227
228 // get the Translation-* files, later we will skip download of non-existent if we have an index
229 for (std::set<std::string>::const_iterator s = sections.begin();
230 s != sections.end(); ++s) {
231 for (std::vector<std::string>::const_iterator l = lang.begin();
232 l != lang.end(); ++l) {
233 IndexTarget * Target = new OptionalIndexTarget();
234 Target->ShortDesc = "Translation-" + *l;
235 Target->MetaKey = TranslationIndexURISuffix(l->c_str(), *s);
236 Target->URI = TranslationIndexURI(l->c_str(), *s);
237 Target->Description = Info (Target->ShortDesc.c_str(), *s);
238 IndexTargets->push_back(Target);
239 }
240 }
241
242 return IndexTargets;
243 }
244 /*}}}*/
245 bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
246 {
247 bool const tryInRelease = _config->FindB("Acquire::TryInRelease", true);
248
249 // special case for --print-uris
250 if (GetAll) {
251 vector <struct IndexTarget *> *targets = ComputeIndexTargets();
252 for (vector <struct IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); ++Target) {
253 new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
254 (*Target)->ShortDesc, HashString());
255 }
256 delete targets;
257
258 // this is normally created in pkgAcqMetaSig, but if we run
259 // in --print-uris mode, we add it here
260 if (tryInRelease == false)
261 new pkgAcqMetaIndex(Owner, MetaIndexURI("Release"),
262 MetaIndexInfo("Release"), "Release",
263 MetaIndexURI("Release.gpg"),
264 ComputeIndexTargets(),
265 new indexRecords (Dist));
266 }
267
268 if (tryInRelease == true)
269 new pkgAcqMetaClearSig(Owner, MetaIndexURI("InRelease"),
270 MetaIndexInfo("InRelease"), "InRelease",
271 MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
272 MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
273 ComputeIndexTargets(),
274 new indexRecords (Dist));
275 else
276 new pkgAcqMetaSig(Owner, MetaIndexURI("Release.gpg"),
277 MetaIndexInfo("Release.gpg"), "Release.gpg",
278 MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
279 ComputeIndexTargets(),
280 new indexRecords (Dist));
281
282 return true;
283 }
284
285 void debReleaseIndex::SetTrusted(bool const Trusted)
286 {
287 if (Trusted == true)
288 this->Trusted = ALWAYS_TRUSTED;
289 else
290 this->Trusted = NEVER_TRUSTED;
291 }
292
293 bool debReleaseIndex::IsTrusted() const
294 {
295 if (Trusted == ALWAYS_TRUSTED)
296 return true;
297 else if (Trusted == NEVER_TRUSTED)
298 return false;
299
300
301 if(_config->FindB("APT::Authentication::TrustCDROM", false))
302 if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
303 return true;
304
305 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
306 URItoFileName(MetaIndexURI("Release")) + ".gpg";
307
308 if (FileExists(VerifiedSigFile))
309 return true;
310
311 VerifiedSigFile = _config->FindDir("Dir::State::lists") +
312 URItoFileName(MetaIndexURI("InRelease"));
313
314 return FileExists(VerifiedSigFile);
315 }
316
317 vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
318 if (Indexes != NULL)
319 return Indexes;
320
321 Indexes = new vector <pkgIndexFile*>;
322 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
323 if (src != ArchEntries.end()) {
324 vector<debSectionEntry const*> const SectionEntries = src->second;
325 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
326 I != SectionEntries.end(); ++I)
327 Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
328 }
329
330 // Only source release
331 if (Indexes->empty() == false && ArchEntries.size() == 1)
332 return Indexes;
333
334 std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
335 map<string, set<string> > sections;
336 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
337 a != ArchEntries.end(); ++a) {
338 if (a->first == "source")
339 continue;
340 for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
341 I != a->second.end(); ++I) {
342 Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
343 sections[(*I)->Section].insert(lang.begin(), lang.end());
344 }
345 }
346
347 for (map<string, set<string> >::const_iterator s = sections.begin();
348 s != sections.end(); ++s)
349 for (set<string>::const_iterator l = s->second.begin();
350 l != s->second.end(); ++l) {
351 if (*l == "none") continue;
352 Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
353 }
354
355 return Indexes;
356 }
357
358 void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
359 for (vector<string>::const_iterator a = Archs.begin();
360 a != Archs.end(); ++a)
361 ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
362 delete Entry;
363 }
364
365 void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
366 ArchEntries[Arch].push_back(Entry);
367 }
368
369 void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
370 if (Entry->IsSrc == true)
371 PushSectionEntry("source", Entry);
372 else {
373 for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
374 a != ArchEntries.end(); ++a) {
375 a->second.push_back(Entry);
376 }
377 }
378 }
379
380 debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
381 bool const &IsSrc): Section(Section), IsSrc(IsSrc)
382 {}
383
384 class debSLTypeDebian : public pkgSourceList::Type
385 {
386 protected:
387
388 bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
389 string const &Dist, string const &Section,
390 bool const &IsSrc, map<string, string> const &Options) const
391 {
392 // parse arch=, arch+= and arch-= settings
393 map<string, string>::const_iterator arch = Options.find("arch");
394 vector<string> Archs =
395 (arch != Options.end()) ? VectorizeString(arch->second, ',') :
396 APT::Configuration::getArchitectures();
397 if ((arch = Options.find("arch+")) != Options.end())
398 {
399 std::vector<std::string> const plusArch = VectorizeString(arch->second, ',');
400 for (std::vector<std::string>::const_iterator plus = plusArch.begin(); plus != plusArch.end(); ++plus)
401 if (std::find(Archs.begin(), Archs.end(), *plus) == Archs.end())
402 Archs.push_back(*plus);
403 }
404 if ((arch = Options.find("arch-")) != Options.end())
405 {
406 std::vector<std::string> const minusArch = VectorizeString(arch->second, ',');
407 for (std::vector<std::string>::const_iterator minus = minusArch.begin(); minus != minusArch.end(); ++minus)
408 {
409 std::vector<std::string>::iterator kill = std::find(Archs.begin(), Archs.end(), *minus);
410 if (kill != Archs.end())
411 Archs.erase(kill);
412 }
413 }
414
415 map<string, string>::const_iterator const trusted = Options.find("trusted");
416
417 for (vector<metaIndex *>::const_iterator I = List.begin();
418 I != List.end(); ++I)
419 {
420 // We only worry about debian entries here
421 if (strcmp((*I)->GetType(), "deb") != 0)
422 continue;
423
424 debReleaseIndex *Deb = (debReleaseIndex *) (*I);
425 if (trusted != Options.end())
426 Deb->SetTrusted(StringToBool(trusted->second, false));
427
428 /* This check insures that there will be only one Release file
429 queued for all the Packages files and Sources files it
430 corresponds to. */
431 if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
432 {
433 if (IsSrc == true)
434 Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
435 else
436 {
437 if (Dist[Dist.size() - 1] == '/')
438 Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
439 else
440 Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
441 }
442 return true;
443 }
444 }
445
446 // No currently created Release file indexes this entry, so we create a new one.
447 debReleaseIndex *Deb;
448 if (trusted != Options.end())
449 Deb = new debReleaseIndex(URI, Dist, StringToBool(trusted->second, false));
450 else
451 Deb = new debReleaseIndex(URI, Dist);
452
453 if (IsSrc == true)
454 Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
455 else
456 {
457 if (Dist[Dist.size() - 1] == '/')
458 Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
459 else
460 Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
461 }
462 List.push_back(Deb);
463 return true;
464 }
465 };
466
467 class debSLTypeDeb : public debSLTypeDebian
468 {
469 public:
470
471 bool CreateItem(vector<metaIndex *> &List, string const &URI,
472 string const &Dist, string const &Section,
473 std::map<string, string> const &Options) const
474 {
475 return CreateItemInternal(List, URI, Dist, Section, false, Options);
476 }
477
478 debSLTypeDeb()
479 {
480 Name = "deb";
481 Label = "Standard Debian binary tree";
482 }
483 };
484
485 class debSLTypeDebSrc : public debSLTypeDebian
486 {
487 public:
488
489 bool CreateItem(vector<metaIndex *> &List, string const &URI,
490 string const &Dist, string const &Section,
491 std::map<string, string> const &Options) const
492 {
493 return CreateItemInternal(List, URI, Dist, Section, true, Options);
494 }
495
496 debSLTypeDebSrc()
497 {
498 Name = "deb-src";
499 Label = "Standard Debian source tree";
500 }
501 };
502
503 debSLTypeDeb _apt_DebType;
504 debSLTypeDebSrc _apt_DebSrcType;