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