]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debmetaindex.cc
merged from debian-sid
[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 debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist) {
123 this->URI = URI;
124 this->Dist = Dist;
125 this->Indexes = NULL;
126 this->Type = "deb";
127 }
128
129 debReleaseIndex::~debReleaseIndex() {
130 for (map<string, vector<debSectionEntry const*> >::const_iterator A = ArchEntries.begin();
131 A != ArchEntries.end(); ++A)
132 for (vector<const debSectionEntry *>::const_iterator S = A->second.begin();
133 S != A->second.end(); ++S)
134 delete *S;
135 }
136
137 vector <struct IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const {
138 vector <struct IndexTarget *>* IndexTargets = new vector <IndexTarget *>;
139
140 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
141 if (src != ArchEntries.end()) {
142 vector<debSectionEntry const*> const SectionEntries = src->second;
143 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
144 I != SectionEntries.end(); ++I) {
145 IndexTarget * Target = new IndexTarget();
146 Target->ShortDesc = "Sources";
147 Target->MetaKey = SourceIndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section);
148 Target->URI = SourceIndexURI(Target->ShortDesc.c_str(), (*I)->Section);
149 Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section);
150 IndexTargets->push_back (Target);
151 }
152 }
153
154 // Only source release
155 if (IndexTargets->empty() == false && ArchEntries.size() == 1)
156 return IndexTargets;
157
158 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
159 a != ArchEntries.end(); ++a) {
160 if (a->first == "source")
161 continue;
162 for (vector <const debSectionEntry *>::const_iterator I = a->second.begin();
163 I != a->second.end(); ++I) {
164 IndexTarget * Target = new IndexTarget();
165 Target->ShortDesc = "Packages";
166 Target->MetaKey = IndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section, a->first);
167 Target->URI = IndexURI(Target->ShortDesc.c_str(), (*I)->Section, a->first);
168 Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section, a->first);
169 IndexTargets->push_back (Target);
170 }
171 }
172
173 return IndexTargets;
174 }
175 /*}}}*/
176 bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
177 {
178 // special case for --print-uris
179 if (GetAll) {
180 vector <struct IndexTarget *> *targets = ComputeIndexTargets();
181 for (vector <struct IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); Target++) {
182 new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
183 (*Target)->ShortDesc, HashString());
184 }
185 // this is normally created in pkgAcqMetaSig, but if we run
186 // in --print-uris mode, we add it here
187 new pkgAcqMetaIndex(Owner, MetaIndexURI("Release"),
188 MetaIndexInfo("Release"), "Release",
189 MetaIndexURI("Release.gpg"),
190 ComputeIndexTargets(),
191 new indexRecords (Dist));
192
193 }
194
195 new pkgAcqMetaSig(Owner, MetaIndexURI("Release.gpg"),
196 MetaIndexInfo("Release.gpg"), "Release.gpg",
197 MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
198 ComputeIndexTargets(),
199 new indexRecords (Dist));
200
201 // Queue the translations
202 std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
203 map<string, set<string> > sections;
204 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
205 a != ArchEntries.end(); ++a) {
206 if (a->first == "source")
207 continue;
208 for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
209 I != a->second.end(); I++)
210 sections[(*I)->Section].insert(lang.begin(), lang.end());
211 }
212
213 for (map<string, set<string> >::const_iterator s = sections.begin();
214 s != sections.end(); ++s)
215 for (set<string>::const_iterator l = s->second.begin();
216 l != s->second.end(); l++) {
217 if (*l == "none") continue;
218 debTranslationsIndex i = debTranslationsIndex(URI,Dist,s->first,(*l).c_str());
219 i.GetIndexes(Owner);
220 }
221
222 return true;
223 }
224
225 bool debReleaseIndex::IsTrusted() const
226 {
227 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
228 URItoFileName(MetaIndexURI("Release")) + ".gpg";
229
230 if(_config->FindB("APT::Authentication::TrustCDROM", false))
231 if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
232 return true;
233
234 if (FileExists(VerifiedSigFile))
235 return true;
236 return false;
237 }
238
239 vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() {
240 if (Indexes != NULL)
241 return Indexes;
242
243 Indexes = new vector <pkgIndexFile*>;
244 map<string, vector<debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
245 if (src != ArchEntries.end()) {
246 vector<debSectionEntry const*> const SectionEntries = src->second;
247 for (vector<debSectionEntry const*>::const_iterator I = SectionEntries.begin();
248 I != SectionEntries.end(); I++)
249 Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
250 }
251
252 // Only source release
253 if (Indexes->empty() == false && ArchEntries.size() == 1)
254 return Indexes;
255
256 std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
257 map<string, set<string> > sections;
258 for (map<string, vector<debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
259 a != ArchEntries.end(); ++a) {
260 if (a->first == "source")
261 continue;
262 for (vector<debSectionEntry const*>::const_iterator I = a->second.begin();
263 I != a->second.end(); I++) {
264 Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted(), a->first));
265 sections[(*I)->Section].insert(lang.begin(), lang.end());
266 }
267 }
268
269 for (map<string, set<string> >::const_iterator s = sections.begin();
270 s != sections.end(); ++s)
271 for (set<string>::const_iterator l = s->second.begin();
272 l != s->second.end(); l++) {
273 if (*l == "none") continue;
274 Indexes->push_back(new debTranslationsIndex(URI,Dist,s->first,(*l).c_str()));
275 }
276
277 return Indexes;
278 }
279
280 void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
281 for (vector<string>::const_iterator a = Archs.begin();
282 a != Archs.end(); ++a)
283 ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
284 delete Entry;
285 }
286
287 void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
288 ArchEntries[Arch].push_back(Entry);
289 }
290
291 void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry) {
292 if (Entry->IsSrc == true)
293 PushSectionEntry("source", Entry);
294 else {
295 for (map<string, vector<const debSectionEntry *> >::iterator a = ArchEntries.begin();
296 a != ArchEntries.end(); ++a) {
297 a->second.push_back(Entry);
298 }
299 }
300 }
301
302 debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
303 bool const &IsSrc): Section(Section), IsSrc(IsSrc)
304 {}
305
306 class debSLTypeDebian : public pkgSourceList::Type
307 {
308 protected:
309
310 bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
311 string const &Dist, string const &Section,
312 bool const &IsSrc, map<string, string> const &Options) const
313 {
314 map<string, string>::const_iterator const arch = Options.find("arch");
315 vector<string> const Archs =
316 (arch != Options.end()) ? VectorizeString(arch->second, ',') :
317 APT::Configuration::getArchitectures();
318
319 for (vector<metaIndex *>::const_iterator I = List.begin();
320 I != List.end(); I++)
321 {
322 // We only worry about debian entries here
323 if (strcmp((*I)->GetType(), "deb") != 0)
324 continue;
325
326 debReleaseIndex *Deb = (debReleaseIndex *) (*I);
327 /* This check insures that there will be only one Release file
328 queued for all the Packages files and Sources files it
329 corresponds to. */
330 if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
331 {
332 if (IsSrc == true)
333 Deb->PushSectionEntry("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
334 else
335 Deb->PushSectionEntry(Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
336 return true;
337 }
338 }
339 // No currently created Release file indexes this entry, so we create a new one.
340 // XXX determine whether this release is trusted or not
341 debReleaseIndex *Deb = new debReleaseIndex(URI, Dist);
342 if (IsSrc == true)
343 Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
344 else
345 Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
346 List.push_back(Deb);
347 return true;
348 }
349 };
350
351 class debSLTypeDeb : public debSLTypeDebian
352 {
353 public:
354
355 bool CreateItem(vector<metaIndex *> &List, string const &URI,
356 string const &Dist, string const &Section,
357 std::map<string, string> const &Options) const
358 {
359 return CreateItemInternal(List, URI, Dist, Section, false, Options);
360 }
361
362 debSLTypeDeb()
363 {
364 Name = "deb";
365 Label = "Standard Debian binary tree";
366 }
367 };
368
369 class debSLTypeDebSrc : public debSLTypeDebian
370 {
371 public:
372
373 bool CreateItem(vector<metaIndex *> &List, string const &URI,
374 string const &Dist, string const &Section,
375 std::map<string, string> const &Options) const
376 {
377 return CreateItemInternal(List, URI, Dist, Section, true, Options);
378 }
379
380 debSLTypeDebSrc()
381 {
382 Name = "deb-src";
383 Label = "Standard Debian source tree";
384 }
385 };
386
387 debSLTypeDeb _apt_DebType;
388 debSLTypeDebSrc _apt_DebSrcType;