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