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