merged from debian-apt
[ntk/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 metaIndex(URI, Dist, "deb"), Trusted(CHECK_TRUST)
147 {}
148
149 debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist, bool const Trusted) :
150 metaIndex(URI, Dist, "deb") {
151 SetTrusted(Trusted);
152 }
153
154 debReleaseIndex::~debReleaseIndex() {
155 for (map<string, vector<debSectionEntry const*> >::const_iterator A = ArchEntries.begin();
156 A != ArchEntries.end(); ++A)
157 for (vector<const debSectionEntry *>::const_iterator S = A->second.begin();
158 S != A->second.end(); ++S)
159 delete *S;
160 }
161
162 vector <struct IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const {
163 vector <struct IndexTarget *>* IndexTargets = new vector <IndexTarget *>;
164
165 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
166 if (src != ArchEntries.end()) {
167 vector<debSectionEntry const*> const SectionEntries = src->second;
168 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
169 I != SectionEntries.end(); ++I) {
170 IndexTarget * Target = new IndexTarget();
171 Target->ShortDesc = "Sources";
172 Target->MetaKey = SourceIndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section);
173 Target->URI = SourceIndexURI(Target->ShortDesc.c_str(), (*I)->Section);
174 Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section);
175 IndexTargets->push_back (Target);
176 }
177 }
178
179 // Only source release
180 if (IndexTargets->empty() == false && ArchEntries.size() == 1)
181 return IndexTargets;
182
183 std::set<std::string> sections;
184 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
185 a != ArchEntries.end(); ++a) {
186 if (a->first == "source")
187 continue;
188 for (vector <const debSectionEntry *>::const_iterator I = a->second.begin();
189 I != a->second.end(); ++I) {
190 IndexTarget * Target = new IndexTarget();
191 Target->ShortDesc = "Packages";
192 Target->MetaKey = IndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section, a->first);
193 Target->URI = IndexURI(Target->ShortDesc.c_str(), (*I)->Section, a->first);
194 Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section, a->first);
195 IndexTargets->push_back (Target);
196 sections.insert((*I)->Section);
197 }
198 }
199
200 std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
201 if (lang.empty() == true)
202 return IndexTargets;
203
204 // get the Translations:
205 // - if its a dists-style repository get the i18n/Index first
206 // - if its flat try to acquire files by guessing
207 if (Dist[Dist.size() - 1] == '/') {
208 for (std::set<std::string>::const_iterator s = sections.begin();
209 s != sections.end(); ++s) {
210 for (std::vector<std::string>::const_iterator l = lang.begin();
211 l != lang.end(); l++) {
212 if (*l == "none") continue;
213 IndexTarget * Target = new OptionalIndexTarget();
214 Target->ShortDesc = "Translation-" + *l;
215 Target->MetaKey = TranslationIndexURISuffix(l->c_str(), *s);
216 Target->URI = TranslationIndexURI(l->c_str(), *s);
217 Target->Description = Info (Target->ShortDesc.c_str(), *s);
218 IndexTargets->push_back(Target);
219 }
220 }
221 } else {
222 for (std::set<std::string>::const_iterator s = sections.begin();
223 s != sections.end(); ++s) {
224 IndexTarget * Target = new OptionalIndexTarget();
225 Target->ShortDesc = "TranslationIndex";
226 Target->MetaKey = TranslationIndexURISuffix("Index", *s);
227 Target->URI = TranslationIndexURI("Index", *s);
228 Target->Description = Info (Target->ShortDesc.c_str(), *s);
229 IndexTargets->push_back (Target);
230 }
231 }
232
233 return IndexTargets;
234 }
235 /*}}}*/
236 bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
237 {
238 // special case for --print-uris
239 if (GetAll) {
240 vector <struct IndexTarget *> *targets = ComputeIndexTargets();
241 for (vector <struct IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); Target++) {
242 new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
243 (*Target)->ShortDesc, HashString());
244 }
245 }
246
247 new pkgAcqMetaClearSig(Owner, MetaIndexURI("InRelease"),
248 MetaIndexInfo("InRelease"), "InRelease",
249 MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
250 MetaIndexURI("Release.gpg"), MetaIndexInfo("Release.gpg"), "Release.gpg",
251 ComputeIndexTargets(),
252 new indexRecords (Dist));
253
254 return true;
255 }
256
257 void debReleaseIndex::SetTrusted(bool const Trusted)
258 {
259 if (Trusted == true)
260 this->Trusted = ALWAYS_TRUSTED;
261 else
262 this->Trusted = NEVER_TRUSTED;
263 }
264
265 bool debReleaseIndex::IsTrusted() const
266 {
267 if (Trusted == ALWAYS_TRUSTED)
268 return true;
269 else if (Trusted == NEVER_TRUSTED)
270 return false;
271
272
273 if(_config->FindB("APT::Authentication::TrustCDROM", false))
274 if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
275 return true;
276
277 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
278 URItoFileName(MetaIndexURI("Release")) + ".gpg";
279
280 if (FileExists(VerifiedSigFile))
281 return true;
282
283 VerifiedSigFile = _config->FindDir("Dir::State::lists") +
284 URItoFileName(MetaIndexURI("InRelease"));
285
286 return FileExists(VerifiedSigFile);
287 }
288
289 vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
290 if (Indexes != NULL)
291 return Indexes;
292
293 Indexes = new vector <pkgIndexFile*>;
294 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
295 if (src != ArchEntries.end()) {
296 vector<debSectionEntry const*> const SectionEntries = src->second;
297 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
298 I != SectionEntries.end(); I++)
299 Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
300 }
301
302 // Only source release
303 if (Indexes->empty() == false && ArchEntries.size() == 1)
304 return Indexes;
305
306 std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
307 map<string, set<string> > sections;
308 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
309 a != ArchEntries.end(); ++a) {
310 if (a->first == "source")
311 continue;
312 for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
313 I != a->second.end(); I++) {
314 Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
315 sections[(*I)->Section].insert(lang.begin(), lang.end());
316 }
317 }
318
319 for (map<string, set<string> >::const_iterator s = sections.begin();
320 s != sections.end(); ++s)
321 for (set<string>::const_iterator l = s->second.begin();
322 l != s->second.end(); l++) {
323 if (*l == "none") continue;
324 Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
325 }
326
327 return Indexes;
328 }
329
330 void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
331 for (vector<string>::const_iterator a = Archs.begin();
332 a != Archs.end(); ++a)
333 ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
334 delete Entry;
335 }
336
337 void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
338 ArchEntries[Arch].push_back(Entry);
339 }
340
341 void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
342 if (Entry->IsSrc == true)
343 PushSectionEntry("source", Entry);
344 else {
345 for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
346 a != ArchEntries.end(); ++a) {
347 a->second.push_back(Entry);
348 }
349 }
350 }
351
352 debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
353 bool const &IsSrc): Section(Section), IsSrc(IsSrc)
354 {}
355
356 class debSLTypeDebian : public pkgSourceList::Type
357 {
358 protected:
359
360 bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
361 string const &Dist, string const &Section,
362 bool const &IsSrc, map<string, string> const &Options) const
363 {
364 map<string, string>::const_iterator const arch = Options.find("arch");
365 vector<string> const Archs =
366 (arch != Options.end()) ? VectorizeString(arch->second, ',') :
367 APT::Configuration::getArchitectures();
368 map<string, string>::const_iterator const trusted = Options.find("trusted");
369
370 for (vector<metaIndex *>::const_iterator I = List.begin();
371 I != List.end(); I++)
372 {
373 // We only worry about debian entries here
374 if (strcmp((*I)->GetType(), "deb") != 0)
375 continue;
376
377 debReleaseIndex *Deb = (debReleaseIndex *) (*I);
378 if (trusted != Options.end())
379 Deb->SetTrusted(StringToBool(trusted->second, false));
380
381 /* This check insures that there will be only one Release file
382 queued for all the Packages files and Sources files it
383 corresponds to. */
384 if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
385 {
386 if (IsSrc == true)
387 Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
388 else
389 {
390 if (Dist[Dist.size() - 1] == '/')
391 Deb->PushSectionEntry("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
392 else
393 Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
394 }
395 return true;
396 }
397 }
398
399 // No currently created Release file indexes this entry, so we create a new one.
400 debReleaseIndex *Deb;
401 if (trusted != Options.end())
402 Deb = new debReleaseIndex(URI, Dist, StringToBool(trusted->second, false));
403 else
404 Deb = new debReleaseIndex(URI, Dist);
405
406 if (IsSrc == true)
407 Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
408 else
409 {
410 if (Dist[Dist.size() - 1] == '/')
411 Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
412 else
413 Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
414 }
415 List.push_back(Deb);
416 return true;
417 }
418 };
419
420 class debSLTypeDeb : public debSLTypeDebian
421 {
422 public:
423
424 bool CreateItem(vector<metaIndex *> &List, string const &URI,
425 string const &Dist, string const &Section,
426 std::map<string, string> const &Options) const
427 {
428 return CreateItemInternal(List, URI, Dist, Section, false, Options);
429 }
430
431 debSLTypeDeb()
432 {
433 Name = "deb";
434 Label = "Standard Debian binary tree";
435 }
436 };
437
438 class debSLTypeDebSrc : public debSLTypeDebian
439 {
440 public:
441
442 bool CreateItem(vector<metaIndex *> &List, string const &URI,
443 string const &Dist, string const &Section,
444 std::map<string, string> const &Options) const
445 {
446 return CreateItemInternal(List, URI, Dist, Section, true, Options);
447 }
448
449 debSLTypeDebSrc()
450 {
451 Name = "deb-src";
452 Label = "Standard Debian source tree";
453 }
454 };
455
456 debSLTypeDeb _apt_DebType;
457 debSLTypeDebSrc _apt_DebSrcType;