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