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