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