]> git.saurik.com Git - apt.git/blame - apt-pkg/deb/debmetaindex.cc
replace ULONG_MAX with c++ style std::numeric_limits
[apt.git] / apt-pkg / deb / debmetaindex.cc
CommitLineData
ea542140 1#include <config.h>
7db98ffc 2
7db98ffc
MZ
3#include <apt-pkg/debmetaindex.h>
4#include <apt-pkg/debindexfile.h>
5#include <apt-pkg/strutl.h>
472ff00e 6#include <apt-pkg/fileutl.h>
7db98ffc
MZ
7#include <apt-pkg/acquire-item.h>
8#include <apt-pkg/configuration.h>
45df0ad2 9#include <apt-pkg/aptconfiguration.h>
472ff00e
DK
10#include <apt-pkg/indexrecords.h>
11#include <apt-pkg/sourcelist.h>
453b82a3
DK
12#include <apt-pkg/hashes.h>
13#include <apt-pkg/macros.h>
14#include <apt-pkg/metaindex.h>
7db98ffc 15
453b82a3
DK
16#include <string.h>
17#include <map>
18#include <string>
19#include <utility>
20#include <vector>
5dd4c8b8 21#include <set>
7cb28948 22#include <algorithm>
5dd4c8b8 23
7db98ffc
MZ
24using namespace std;
25
7db98ffc
MZ
26string 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
41string debReleaseIndex::MetaIndexFile(const char *Type) const
42{
43 return _config->FindDir("Dir::State::lists") +
44 URItoFileName(MetaIndexURI(Type));
45}
46
47string 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
7014e148
MV
62std::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}
7014e148 75
4b42f43b
DK
76debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist) :
77 metaIndex(URI, Dist, "deb"), Trusted(CHECK_TRUST)
78{}
79
80debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist, bool const Trusted) :
81 metaIndex(URI, Dist, "deb") {
82 SetTrusted(Trusted);
7db98ffc
MZ
83}
84
5dd4c8b8
DK
85debReleaseIndex::~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;
7a9f09bd
MV
91}
92
1e0f0f28
DK
93vector <IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const
94{
95 vector <IndexTarget *>* IndexTargets = new vector <IndexTarget *>;
5dd4c8b8 96
1e0f0f28
DK
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 }
5dd4c8b8 173
1e0f0f28
DK
174 // Only source release
175 if (IndexTargets->empty() == false && ArchEntries.size() == 1)
176 return IndexTargets;
ab53c018 177
1e0f0f28
DK
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 }
5dd4c8b8 246
1e0f0f28 247 return IndexTargets;
7db98ffc
MZ
248}
249 /*}}}*/
5dd4c8b8 250bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
7db98ffc 251{
07cb47e7
DK
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
7db98ffc 258 // special case for --print-uris
448c38bd
DK
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);
7db98ffc 269 }
fe0f7911 270
55971004 271 return true;
7db98ffc
MZ
272}
273
4b42f43b
DK
274void debReleaseIndex::SetTrusted(bool const Trusted)
275{
276 if (Trusted == true)
277 this->Trusted = ALWAYS_TRUSTED;
278 else
279 this->Trusted = NEVER_TRUSTED;
280}
281
7db98ffc
MZ
282bool debReleaseIndex::IsTrusted() const
283{
4b42f43b
DK
284 if (Trusted == ALWAYS_TRUSTED)
285 return true;
286 else if (Trusted == NEVER_TRUSTED)
287 return false;
288
289
4e0ad446 290 if(_config->FindB("APT::Authentication::TrustCDROM", false))
e8cdc56a
MV
291 if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
292 return true;
fe0f7911
DK
293
294 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
295 URItoFileName(MetaIndexURI("Release")) + ".gpg";
296
7db98ffc
MZ
297 if (FileExists(VerifiedSigFile))
298 return true;
fe0f7911
DK
299
300 VerifiedSigFile = _config->FindDir("Dir::State::lists") +
301 URItoFileName(MetaIndexURI("InRelease"));
302
303 return FileExists(VerifiedSigFile);
7db98ffc
MZ
304}
305
5dd4c8b8
DK
306vector <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();
f7f0d6c7 315 I != SectionEntries.end(); ++I)
5dd4c8b8
DK
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();
f7f0d6c7 330 I != a->second.end(); ++I) {
5dd4c8b8
DK
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();
f7f0d6c7 339 l != s->second.end(); ++l) {
1a31359b 340 if (*l == "none") continue;
5dd4c8b8 341 Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
1a31359b 342 }
5dd4c8b8
DK
343
344 return Indexes;
345}
a7a5b0d9 346
5dd4c8b8
DK
347void 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;
7db98ffc
MZ
352}
353
5dd4c8b8
DK
354void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
355 ArchEntries[Arch].push_back(Entry);
7db98ffc
MZ
356}
357
5dd4c8b8
DK
358debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
359 bool const &IsSrc): Section(Section), IsSrc(IsSrc)
360{}
361
dce45dbe 362class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type
7db98ffc
MZ
363{
364 protected:
365
5dd4c8b8
DK
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
7db98ffc 369 {
3d1be93d
DK
370 // parse arch=, arch+= and arch-= settings
371 map<string, string>::const_iterator arch = Options.find("arch");
2b4cead3
DK
372 vector<string> Archs;
373 if (arch != Options.end())
374 Archs = VectorizeString(arch->second, ',');
375 else
376 Archs = APT::Configuration::getArchitectures();
377
3d1be93d
DK
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
4b42f43b 396 map<string, string>::const_iterator const trusted = Options.find("trusted");
5dd4c8b8
DK
397
398 for (vector<metaIndex *>::const_iterator I = List.begin();
f7f0d6c7 399 I != List.end(); ++I)
7db98ffc 400 {
5dd4c8b8
DK
401 // We only worry about debian entries here
402 if (strcmp((*I)->GetType(), "deb") != 0)
403 continue;
404
405 debReleaseIndex *Deb = (debReleaseIndex *) (*I);
4b42f43b
DK
406 if (trusted != Options.end())
407 Deb->SetTrusted(StringToBool(trusted->second, false));
408
5dd4c8b8
DK
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)
7db98ffc 413 {
5dd4c8b8
DK
414 if (IsSrc == true)
415 Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
416 else
dd13742e
DK
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 }
5dd4c8b8 423 return true;
7db98ffc
MZ
424 }
425 }
4b42f43b 426
7db98ffc 427 // No currently created Release file indexes this entry, so we create a new one.
4b42f43b
DK
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
5dd4c8b8
DK
434 if (IsSrc == true)
435 Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
436 else
dd13742e
DK
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 }
7db98ffc
MZ
443 List.push_back(Deb);
444 return true;
445 }
446};
447
0d29b9d4
MV
448debDebFileMetaIndex::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
dce45dbe 457class APT_HIDDEN debSLTypeDeb : public debSLTypeDebian
7db98ffc
MZ
458{
459 public:
460
5dd4c8b8
DK
461 bool CreateItem(vector<metaIndex *> &List, string const &URI,
462 string const &Dist, string const &Section,
463 std::map<string, string> const &Options) const
7db98ffc 464 {
5dd4c8b8 465 return CreateItemInternal(List, URI, Dist, Section, false, Options);
7db98ffc
MZ
466 }
467
468 debSLTypeDeb()
469 {
470 Name = "deb";
471 Label = "Standard Debian binary tree";
472 }
473};
474
dce45dbe 475class APT_HIDDEN debSLTypeDebSrc : public debSLTypeDebian
7db98ffc
MZ
476{
477 public:
478
5dd4c8b8
DK
479 bool CreateItem(vector<metaIndex *> &List, string const &URI,
480 string const &Dist, string const &Section,
481 std::map<string, string> const &Options) const
7db98ffc 482 {
5dd4c8b8 483 return CreateItemInternal(List, URI, Dist, Section, true, Options);
7db98ffc
MZ
484 }
485
486 debSLTypeDebSrc()
487 {
488 Name = "deb-src";
489 Label = "Standard Debian source tree";
490 }
491};
492
dce45dbe 493class APT_HIDDEN debSLTypeDebFile : public pkgSourceList::Type
eafc5435
MV
494{
495 public:
496
497 bool CreateItem(vector<metaIndex *> &List, string const &URI,
070536e6
MV
498 string const &/*Dist*/, string const &/*Section*/,
499 std::map<string, string> const &/*Options*/) const
eafc5435
MV
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};
dce45dbe
DK
512
513APT_HIDDEN debSLTypeDeb _apt_DebType;
514APT_HIDDEN debSLTypeDebSrc _apt_DebSrcType;
515APT_HIDDEN debSLTypeDebFile _apt_DebFileType;