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