]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debmetaindex.cc
add sha512 support in the client now as well
[apt.git] / apt-pkg / deb / debmetaindex.cc
1 // ijones, walters
2
3 #include <apt-pkg/debmetaindex.h>
4 #include <apt-pkg/debindexfile.h>
5 #include <apt-pkg/strutl.h>
6 #include <apt-pkg/acquire-item.h>
7 #include <apt-pkg/configuration.h>
8 #include <apt-pkg/aptconfiguration.h>
9 #include <apt-pkg/error.h>
10
11 #include <set>
12
13 using namespace std;
14
15 string debReleaseIndex::Info(const char *Type, string const &Section, string const &Arch) const
16 {
17 string Info = ::URI::SiteOnly(URI) + ' ';
18 if (Dist[Dist.size() - 1] == '/')
19 {
20 if (Dist != "/")
21 Info += Dist;
22 }
23 else
24 {
25 Info += Dist + '/' + Section;
26 if (Arch.empty() != true)
27 Info += " " + Arch;
28 }
29 Info += " ";
30 Info += Type;
31 return Info;
32 }
33
34 string debReleaseIndex::MetaIndexInfo(const char *Type) const
35 {
36 string Info = ::URI::SiteOnly(URI) + ' ';
37 if (Dist[Dist.size() - 1] == '/')
38 {
39 if (Dist != "/")
40 Info += Dist;
41 }
42 else
43 Info += Dist;
44 Info += " ";
45 Info += Type;
46 return Info;
47 }
48
49 string debReleaseIndex::MetaIndexFile(const char *Type) const
50 {
51 return _config->FindDir("Dir::State::lists") +
52 URItoFileName(MetaIndexURI(Type));
53 }
54
55 string debReleaseIndex::MetaIndexURI(const char *Type) const
56 {
57 string Res;
58
59 if (Dist == "/")
60 Res = URI;
61 else if (Dist[Dist.size()-1] == '/')
62 Res = URI + Dist;
63 else
64 Res = URI + "dists/" + Dist + "/";
65
66 Res += Type;
67 return Res;
68 }
69
70 string debReleaseIndex::IndexURISuffix(const char *Type, string const &Section, string const &Arch) const
71 {
72 string Res ="";
73 if (Dist[Dist.size() - 1] != '/')
74 {
75 if (Arch == "native")
76 Res += Section + "/binary-" + _config->Find("APT::Architecture") + '/';
77 else
78 Res += Section + "/binary-" + Arch + '/';
79 }
80 return Res + Type;
81 }
82
83
84 string debReleaseIndex::IndexURI(const char *Type, string const &Section, string const &Arch) const
85 {
86 if (Dist[Dist.size() - 1] == '/')
87 {
88 string Res;
89 if (Dist != "/")
90 Res = URI + Dist;
91 else
92 Res = URI;
93 return Res + Type;
94 }
95 else
96 return URI + "dists/" + Dist + '/' + IndexURISuffix(Type, Section, Arch);
97 }
98
99 string debReleaseIndex::SourceIndexURISuffix(const char *Type, const string &Section) const
100 {
101 string Res ="";
102 if (Dist[Dist.size() - 1] != '/')
103 Res += Section + "/source/";
104 return Res + Type;
105 }
106
107 string debReleaseIndex::SourceIndexURI(const char *Type, const string &Section) const
108 {
109 string Res;
110 if (Dist[Dist.size() - 1] == '/')
111 {
112 if (Dist != "/")
113 Res = URI + Dist;
114 else
115 Res = URI;
116 return Res + Type;
117 }
118 else
119 return URI + "dists/" + Dist + "/" + SourceIndexURISuffix(Type, Section);
120 }
121
122 string debReleaseIndex::TranslationIndexURISuffix(const char *Type, const string &Section) const
123 {
124 string Res ="";
125 if (Dist[Dist.size() - 1] != '/')
126 Res += Section + "/i18n/";
127 return Res + Type;
128 }
129
130 string debReleaseIndex::TranslationIndexURI(const char *Type, const string &Section) const
131 {
132 string Res;
133 if (Dist[Dist.size() - 1] == '/')
134 {
135 if (Dist != "/")
136 Res = URI + Dist;
137 else
138 Res = URI;
139 return Res + Type;
140 }
141 else
142 return URI + "dists/" + Dist + "/" + TranslationIndexURISuffix(Type, Section);
143 }
144
145 debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist) {
146 this->URI = URI;
147 this->Dist = Dist;
148 this->Indexes = NULL;
149 this->Type = "deb";
150 }
151
152 debReleaseIndex::~debReleaseIndex() {
153 for (map<string, vector<debSectionEntry const*> >::const_iterator A = ArchEntries.begin();
154 A != ArchEntries.end(); ++A)
155 for (vector<const debSectionEntry *>::const_iterator S = A->second.begin();
156 S != A->second.end(); ++S)
157 delete *S;
158 }
159
160 vector <struct IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const {
161 vector <struct IndexTarget *>* IndexTargets = new vector <IndexTarget *>;
162
163 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
164 if (src != ArchEntries.end()) {
165 vector<debSectionEntry const*> const SectionEntries = src->second;
166 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
167 I != SectionEntries.end(); ++I) {
168 IndexTarget * Target = new IndexTarget();
169 Target->ShortDesc = "Sources";
170 Target->MetaKey = SourceIndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section);
171 Target->URI = SourceIndexURI(Target->ShortDesc.c_str(), (*I)->Section);
172 Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section);
173 IndexTargets->push_back (Target);
174 }
175 }
176
177 // Only source release
178 if (IndexTargets->empty() == false && ArchEntries.size() == 1)
179 return IndexTargets;
180
181 std::set<std::string> sections;
182 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
183 a != ArchEntries.end(); ++a) {
184 if (a->first == "source")
185 continue;
186 for (vector <const debSectionEntry *>::const_iterator I = a->second.begin();
187 I != a->second.end(); ++I) {
188 IndexTarget * Target = new IndexTarget();
189 Target->ShortDesc = "Packages";
190 Target->MetaKey = IndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section, a->first);
191 Target->URI = IndexURI(Target->ShortDesc.c_str(), (*I)->Section, a->first);
192 Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section, a->first);
193 IndexTargets->push_back (Target);
194 sections.insert((*I)->Section);
195 }
196 }
197
198 // get the Translations:
199 // - if its a dists-style repository get the i18n/Index first
200 // - if its flat try to acquire files by guessing
201 if (Dist[Dist.size() - 1] == '/') {
202 std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
203 for (std::set<std::string>::const_iterator s = sections.begin();
204 s != sections.end(); ++s) {
205 for (std::vector<std::string>::const_iterator l = lang.begin();
206 l != lang.end(); l++) {
207 if (*l == "none") continue;
208 IndexTarget * Target = new OptionalIndexTarget();
209 Target->ShortDesc = "Translation-" + *l;
210 Target->MetaKey = TranslationIndexURISuffix(l->c_str(), *s);
211 Target->URI = TranslationIndexURI(l->c_str(), *s);
212 Target->Description = Info (Target->ShortDesc.c_str(), *s);
213 IndexTargets->push_back(Target);
214 }
215 }
216 } else {
217 for (std::set<std::string>::const_iterator s = sections.begin();
218 s != sections.end(); ++s) {
219 IndexTarget * Target = new OptionalIndexTarget();
220 Target->ShortDesc = "TranslationIndex";
221 Target->MetaKey = TranslationIndexURISuffix("Index", *s);
222 Target->URI = TranslationIndexURI("Index", *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 // special case for --print-uris
234 if (GetAll) {
235 vector <struct IndexTarget *> *targets = ComputeIndexTargets();
236 for (vector <struct IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); Target++) {
237 new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
238 (*Target)->ShortDesc, HashString());
239 }
240 }
241
242 new pkgAcqMetaClearSig(Owner, MetaIndexURI("InRelease"),
243 MetaIndexInfo("InRelease"), "InRelease",
244 MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
245 MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
246 ComputeIndexTargets(),
247 new indexRecords (Dist));
248
249 return true;
250 }
251
252 bool debReleaseIndex::IsTrusted() const
253 {
254 if(_config->FindB("APT::Authentication::TrustCDROM", false))
255 if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
256 return true;
257
258 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
259 URItoFileName(MetaIndexURI("Release")) + ".gpg";
260
261 if (FileExists(VerifiedSigFile))
262 return true;
263
264 VerifiedSigFile = _config->FindDir("Dir::State::lists") +
265 URItoFileName(MetaIndexURI("InRelease"));
266
267 return FileExists(VerifiedSigFile);
268 }
269
270 vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
271 if (Indexes != NULL)
272 return Indexes;
273
274 Indexes = new vector <pkgIndexFile*>;
275 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
276 if (src != ArchEntries.end()) {
277 vector<debSectionEntry const*> const SectionEntries = src->second;
278 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
279 I != SectionEntries.end(); I++)
280 Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
281 }
282
283 // Only source release
284 if (Indexes->empty() == false && ArchEntries.size() == 1)
285 return Indexes;
286
287 std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
288 map<string, set<string> > sections;
289 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
290 a != ArchEntries.end(); ++a) {
291 if (a->first == "source")
292 continue;
293 for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
294 I != a->second.end(); I++) {
295 Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
296 sections[(*I)->Section].insert(lang.begin(), lang.end());
297 }
298 }
299
300 for (map<string, set<string> >::const_iterator s = sections.begin();
301 s != sections.end(); ++s)
302 for (set<string>::const_iterator l = s->second.begin();
303 l != s->second.end(); l++) {
304 if (*l == "none") continue;
305 Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
306 }
307
308 return Indexes;
309 }
310
311 void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
312 for (vector<string>::const_iterator a = Archs.begin();
313 a != Archs.end(); ++a)
314 ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
315 delete Entry;
316 }
317
318 void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
319 ArchEntries[Arch].push_back(Entry);
320 }
321
322 void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
323 if (Entry->IsSrc == true)
324 PushSectionEntry("source", Entry);
325 else {
326 for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
327 a != ArchEntries.end(); ++a) {
328 a->second.push_back(Entry);
329 }
330 }
331 }
332
333 debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
334 bool const &IsSrc): Section(Section), IsSrc(IsSrc)
335 {}
336
337 class debSLTypeDebian : public pkgSourceList::Type
338 {
339 protected:
340
341 bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
342 string const &Dist, string const &Section,
343 bool const &IsSrc, map<string, string> const &Options) const
344 {
345 map<string, string>::const_iterator const arch = Options.find("arch");
346 vector<string> const Archs =
347 (arch != Options.end()) ? VectorizeString(arch->second, ',') :
348 APT::Configuration::getArchitectures();
349
350 for (vector<metaIndex *>::const_iterator I = List.begin();
351 I != List.end(); I++)
352 {
353 // We only worry about debian entries here
354 if (strcmp((*I)->GetType(), "deb") != 0)
355 continue;
356
357 debReleaseIndex *Deb = (debReleaseIndex *) (*I);
358 /* This check insures that there will be only one Release file
359 queued for all the Packages files and Sources files it
360 corresponds to. */
361 if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
362 {
363 if (IsSrc == true)
364 Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
365 else
366 {
367 if (Dist[Dist.size() - 1] == '/')
368 Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
369 else
370 Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
371 }
372 return true;
373 }
374 }
375 // No currently created Release file indexes this entry, so we create a new one.
376 // XXX determine whether this release is trusted or not
377 debReleaseIndex *Deb = new debReleaseIndex(URI, Dist);
378 if (IsSrc == true)
379 Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
380 else
381 {
382 if (Dist[Dist.size() - 1] == '/')
383 Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
384 else
385 Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
386 }
387 List.push_back(Deb);
388 return true;
389 }
390 };
391
392 class debSLTypeDeb : public debSLTypeDebian
393 {
394 public:
395
396 bool CreateItem(vector<metaIndex *> &List, string const &URI,
397 string const &Dist, string const &Section,
398 std::map<string, string> const &Options) const
399 {
400 return CreateItemInternal(List, URI, Dist, Section, false, Options);
401 }
402
403 debSLTypeDeb()
404 {
405 Name = "deb";
406 Label = "Standard Debian binary tree";
407 }
408 };
409
410 class debSLTypeDebSrc : public debSLTypeDebian
411 {
412 public:
413
414 bool CreateItem(vector<metaIndex *> &List, string const &URI,
415 string const &Dist, string const &Section,
416 std::map<string, string> const &Options) const
417 {
418 return CreateItemInternal(List, URI, Dist, Section, true, Options);
419 }
420
421 debSLTypeDebSrc()
422 {
423 Name = "deb-src";
424 Label = "Standard Debian source tree";
425 }
426 };
427
428 debSLTypeDeb _apt_DebType;
429 debSLTypeDebSrc _apt_DebSrcType;