]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debmetaindex.cc
store Release files data in the Cache
[apt.git] / apt-pkg / deb / debmetaindex.cc
1 #include <config.h>
2
3 #include <apt-pkg/error.h>
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/hashes.h>
14 #include <apt-pkg/metaindex.h>
15 #include <apt-pkg/pkgcachegen.h>
16 #include <apt-pkg/tagfile.h>
17 #include <apt-pkg/gpgv.h>
18 #include <apt-pkg/macros.h>
19
20 #include <map>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 #include <set>
25 #include <algorithm>
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <string.h>
31
32 using namespace std;
33
34 string debReleaseIndex::MetaIndexInfo(const char *Type) const
35 {
36 string Info = ::URI::ArchiveOnly(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 std::string debReleaseIndex::Describe() const
49 {
50 return MetaIndexInfo("Release");
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 std::string debReleaseIndex::LocalFileName() const
75 {
76 // see if we have a InRelease file
77 std::string PathInRelease = MetaIndexFile("InRelease");
78 if (FileExists(PathInRelease))
79 return PathInRelease;
80
81 // and if not return the normal one
82 if (FileExists(PathInRelease))
83 return MetaIndexFile("Release");
84
85 return "";
86 }
87
88 debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist) :
89 metaIndex(URI, Dist, "deb"), Trusted(CHECK_TRUST)
90 {}
91
92 debReleaseIndex::debReleaseIndex(string const &URI, string const &Dist, bool const Trusted) :
93 metaIndex(URI, Dist, "deb") {
94 SetTrusted(Trusted);
95 }
96
97 debReleaseIndex::~debReleaseIndex() {
98 for (map<string, vector<debSectionEntry const*> >::const_iterator A = ArchEntries.begin();
99 A != ArchEntries.end(); ++A)
100 for (vector<const debSectionEntry *>::const_iterator S = A->second.begin();
101 S != A->second.end(); ++S)
102 delete *S;
103 }
104
105 template<typename CallC>
106 void foreachTarget(std::string const URI, std::string const Dist,
107 std::map<std::string, std::vector<debReleaseIndex::debSectionEntry const *> > const &ArchEntries,
108 CallC &Call)
109 {
110 bool const flatArchive = (Dist[Dist.length() - 1] == '/');
111 std::string baseURI = URI;
112 if (flatArchive)
113 {
114 if (Dist != "/")
115 baseURI += Dist;
116 }
117 else
118 baseURI += "dists/" + Dist + "/";
119 std::string const Release = (Dist == "/") ? "" : Dist;
120 std::string const Site = ::URI::ArchiveOnly(URI);
121 std::vector<std::string> lang = APT::Configuration::getLanguages(true);
122 if (lang.empty())
123 lang.push_back("none");
124 map<string, vector<debReleaseIndex::debSectionEntry const*> >::const_iterator const src = ArchEntries.find("source");
125 if (src != ArchEntries.end())
126 {
127 std::vector<std::string> const targets = _config->FindVector("APT::Acquire::Targets::deb-src", "", true);
128 for (std::vector<std::string>::const_iterator T = targets.begin(); T != targets.end(); ++T)
129 {
130 #define APT_T_CONFIG(X) _config->Find(std::string("APT::Acquire::Targets::deb-src::") + *T + "::" + (X))
131 std::string const MetaKey = APT_T_CONFIG(flatArchive ? "flatMetaKey" : "MetaKey");
132 std::string const ShortDesc = APT_T_CONFIG("ShortDescription");
133 std::string const LongDesc = APT_T_CONFIG(flatArchive ? "flatDescription" : "Description");
134 bool const IsOptional = _config->FindB(std::string("APT::Acquire::Targets::deb-src::") + *T + "::Optional", true);
135 #undef APT_T_CONFIG
136 if (MetaKey.empty())
137 continue;
138
139 vector<debReleaseIndex::debSectionEntry const*> const SectionEntries = src->second;
140 for (vector<debReleaseIndex::debSectionEntry const*>::const_iterator I = SectionEntries.begin();
141 I != SectionEntries.end(); ++I)
142 {
143 for (vector<std::string>::const_iterator l = lang.begin(); l != lang.end(); ++l)
144 {
145 if (*l == "none" && MetaKey.find("$(LANGUAGE)") != std::string::npos)
146 continue;
147
148 std::map<std::string, std::string> Options;
149 Options.insert(std::make_pair("SITE", Site));
150 Options.insert(std::make_pair("RELEASE", Release));
151 if (MetaKey.find("$(COMPONENT)") != std::string::npos)
152 Options.insert(std::make_pair("COMPONENT", (*I)->Section));
153 if (MetaKey.find("$(LANGUAGE)") != std::string::npos)
154 Options.insert(std::make_pair("LANGUAGE", *l));
155 Options.insert(std::make_pair("ARCHITECTURE", "source"));
156 Options.insert(std::make_pair("BASE_URI", baseURI));
157 Options.insert(std::make_pair("REPO_URI", URI));
158 Options.insert(std::make_pair("TARGET_OF", "deb-src"));
159 Options.insert(std::make_pair("CREATED_BY", *T));
160 Call(MetaKey, ShortDesc, LongDesc, IsOptional, Options);
161
162 if (MetaKey.find("$(LANGUAGE)") == std::string::npos)
163 break;
164 }
165
166 if (MetaKey.find("$(COMPONENT)") == std::string::npos)
167 break;
168 }
169 }
170 }
171
172 std::vector<std::string> const targets = _config->FindVector("APT::Acquire::Targets::deb", "", true);
173 for (std::vector<std::string>::const_iterator T = targets.begin(); T != targets.end(); ++T)
174 {
175 #define APT_T_CONFIG(X) _config->Find(std::string("APT::Acquire::Targets::deb::") + *T + "::" + (X))
176 std::string const MetaKey = APT_T_CONFIG(flatArchive ? "flatMetaKey" : "MetaKey");
177 std::string const ShortDesc = APT_T_CONFIG("ShortDescription");
178 std::string const LongDesc = APT_T_CONFIG(flatArchive ? "flatDescription" : "Description");
179 bool const IsOptional = _config->FindB(std::string("APT::Acquire::Targets::deb::") + *T + "::Optional", true);
180 #undef APT_T_CONFIG
181 if (MetaKey.empty())
182 continue;
183
184 for (map<string, vector<debReleaseIndex::debSectionEntry const*> >::const_iterator a = ArchEntries.begin();
185 a != ArchEntries.end(); ++a)
186 {
187 if (a->first == "source")
188 continue;
189
190 for (vector <const debReleaseIndex::debSectionEntry *>::const_iterator I = a->second.begin();
191 I != a->second.end(); ++I) {
192
193 for (vector<std::string>::const_iterator l = lang.begin(); l != lang.end(); ++l)
194 {
195 if (*l == "none" && MetaKey.find("$(LANGUAGE)") != std::string::npos)
196 continue;
197
198 std::map<std::string, std::string> Options;
199 Options.insert(std::make_pair("SITE", Site));
200 Options.insert(std::make_pair("RELEASE", Release));
201 if (MetaKey.find("$(COMPONENT)") != std::string::npos)
202 Options.insert(std::make_pair("COMPONENT", (*I)->Section));
203 if (MetaKey.find("$(LANGUAGE)") != std::string::npos)
204 Options.insert(std::make_pair("LANGUAGE", *l));
205 if (MetaKey.find("$(ARCHITECTURE)") != std::string::npos)
206 Options.insert(std::make_pair("ARCHITECTURE", a->first));
207 Options.insert(std::make_pair("BASE_URI", baseURI));
208 Options.insert(std::make_pair("REPO_URI", URI));
209 Options.insert(std::make_pair("TARGET_OF", "deb"));
210 Options.insert(std::make_pair("CREATED_BY", *T));
211 Call(MetaKey, ShortDesc, LongDesc, IsOptional, Options);
212
213 if (MetaKey.find("$(LANGUAGE)") == std::string::npos)
214 break;
215 }
216
217 if (MetaKey.find("$(COMPONENT)") == std::string::npos)
218 break;
219 }
220
221 if (MetaKey.find("$(ARCHITECTURE)") == std::string::npos)
222 break;
223 }
224 }
225 }
226
227
228 struct ComputeIndexTargetsClass
229 {
230 vector <IndexTarget> IndexTargets;
231
232 void operator()(std::string MetaKey, std::string ShortDesc, std::string LongDesc,
233 bool const IsOptional, std::map<std::string, std::string> Options)
234 {
235 for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
236 {
237 MetaKey = SubstVar(MetaKey, std::string("$(") + O->first + ")", O->second);
238 ShortDesc = SubstVar(ShortDesc, std::string("$(") + O->first + ")", O->second);
239 LongDesc = SubstVar(LongDesc, std::string("$(") + O->first + ")", O->second);
240 }
241 IndexTarget Target(
242 MetaKey,
243 ShortDesc,
244 LongDesc,
245 Options.find("BASE_URI")->second + MetaKey,
246 IsOptional,
247 Options
248 );
249 IndexTargets.push_back(Target);
250 }
251 };
252
253 std::vector<IndexTarget> debReleaseIndex::GetIndexTargets() const
254 {
255 ComputeIndexTargetsClass comp;
256 foreachTarget(URI, Dist, ArchEntries, comp);
257 return comp.IndexTargets;
258 }
259
260
261 /*}}}*/
262 bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll) const
263 {
264 indexRecords * const iR = new indexRecords(Dist);
265 if (Trusted == ALWAYS_TRUSTED)
266 iR->SetTrusted(true);
267 else if (Trusted == NEVER_TRUSTED)
268 iR->SetTrusted(false);
269
270 // special case for --print-uris
271 std::vector<IndexTarget> const targets = GetIndexTargets();
272 #define APT_TARGET(X) IndexTarget("", X, MetaIndexInfo(X), MetaIndexURI(X), false, std::map<std::string,std::string>())
273 pkgAcqMetaBase * const TransactionManager = new pkgAcqMetaClearSig(Owner,
274 APT_TARGET("InRelease"), APT_TARGET("Release"), APT_TARGET("Release.gpg"),
275 targets, iR);
276 #undef APT_TARGET
277 if (GetAll)
278 {
279 for (std::vector<IndexTarget>::const_iterator Target = targets.begin(); Target != targets.end(); ++Target)
280 new pkgAcqIndex(Owner, TransactionManager, *Target);
281 }
282
283 return true;
284 }
285
286 void debReleaseIndex::SetTrusted(bool const Trusted)
287 {
288 if (Trusted == true)
289 this->Trusted = ALWAYS_TRUSTED;
290 else
291 this->Trusted = NEVER_TRUSTED;
292 }
293
294 bool debReleaseIndex::IsTrusted() const
295 {
296 if (Trusted == ALWAYS_TRUSTED)
297 return true;
298 else if (Trusted == NEVER_TRUSTED)
299 return false;
300
301
302 if(_config->FindB("APT::Authentication::TrustCDROM", false))
303 if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
304 return true;
305
306 string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
307 URItoFileName(MetaIndexURI("Release")) + ".gpg";
308
309 if (FileExists(VerifiedSigFile))
310 return true;
311
312 VerifiedSigFile = _config->FindDir("Dir::State::lists") +
313 URItoFileName(MetaIndexURI("InRelease"));
314
315 return FileExists(VerifiedSigFile);
316 }
317
318 std::vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles()
319 {
320 if (Indexes != NULL)
321 return Indexes;
322
323 Indexes = new std::vector<pkgIndexFile*>();
324 std::vector<IndexTarget> const Targets = GetIndexTargets();
325 bool const istrusted = IsTrusted();
326 for (std::vector<IndexTarget>::const_iterator T = Targets.begin(); T != Targets.end(); ++T)
327 {
328 std::string const TargetName = T->Option(IndexTarget::CREATED_BY);
329 if (TargetName == "Packages")
330 Indexes->push_back(new debPackagesIndex(*T, istrusted));
331 else if (TargetName == "Sources")
332 Indexes->push_back(new debSourcesIndex(*T, istrusted));
333 else if (TargetName == "Translations")
334 Indexes->push_back(new debTranslationsIndex(*T));
335 }
336 return Indexes;
337 }
338
339 void debReleaseIndex::PushSectionEntry(vector<string> const &Archs, const debSectionEntry *Entry) {
340 for (vector<string>::const_iterator a = Archs.begin();
341 a != Archs.end(); ++a)
342 ArchEntries[*a].push_back(new debSectionEntry(Entry->Section, Entry->IsSrc));
343 delete Entry;
344 }
345
346 void debReleaseIndex::PushSectionEntry(string const &Arch, const debSectionEntry *Entry) {
347 ArchEntries[Arch].push_back(Entry);
348 }
349
350 debReleaseIndex::debSectionEntry::debSectionEntry (string const &Section,
351 bool const &IsSrc): Section(Section), IsSrc(IsSrc)
352 {}
353
354 static bool ReleaseFileName(debReleaseIndex const * const That, std::string &ReleaseFile)
355 {
356 ReleaseFile = That->MetaIndexFile("InRelease");
357 bool releaseExists = false;
358 if (FileExists(ReleaseFile) == true)
359 releaseExists = true;
360 else
361 {
362 ReleaseFile = That->MetaIndexFile("Release");
363 if (FileExists(ReleaseFile))
364 releaseExists = true;
365 }
366 return releaseExists;
367 }
368
369 bool debReleaseIndex::Merge(pkgCacheGenerator &Gen,OpProgress * /*Prog*/) const/*{{{*/
370 {
371 std::string ReleaseFile;
372 bool const releaseExists = ReleaseFileName(this, ReleaseFile);
373
374 ::URI Tmp(URI);
375 if (Gen.SelectReleaseFile(ReleaseFile, Tmp.Host) == false)
376 return _error->Error("Problem with SelectReleaseFile %s", ReleaseFile.c_str());
377
378 if (releaseExists == false)
379 return true;
380
381 FileFd Rel;
382 // Beware: The 'Release' file might be clearsigned in case the
383 // signature for an 'InRelease' file couldn't be checked
384 if (OpenMaybeClearSignedFile(ReleaseFile, Rel) == false)
385 return false;
386 if (_error->PendingError() == true)
387 return false;
388
389 // Store the IMS information
390 pkgCache::RlsFileIterator File = Gen.GetCurRlsFile();
391 pkgCacheGenerator::Dynamic<pkgCache::RlsFileIterator> DynFile(File);
392 // Rel can't be used as this is potentially a temporary file
393 struct stat Buf;
394 if (stat(ReleaseFile.c_str(), &Buf) != 0)
395 return _error->Errno("fstat", "Unable to stat file %s", ReleaseFile.c_str());
396 File->Size = Buf.st_size;
397 File->mtime = Buf.st_mtime;
398
399 pkgTagFile TagFile(&Rel, Rel.Size());
400 pkgTagSection Section;
401 if (_error->PendingError() == true || TagFile.Step(Section) == false)
402 return false;
403
404 std::string data;
405 #define APT_INRELEASE(TYPE, TAG, STORE) \
406 data = Section.FindS(TAG); \
407 if (data.empty() == false) \
408 { \
409 map_stringitem_t const storage = Gen.StoreString(pkgCacheGenerator::TYPE, data); \
410 STORE = storage; \
411 }
412 APT_INRELEASE(MIXED, "Suite", File->Archive)
413 APT_INRELEASE(VERSIONNUMBER, "Version", File->Version)
414 APT_INRELEASE(MIXED, "Origin", File->Origin)
415 APT_INRELEASE(MIXED, "Codename", File->Codename)
416 APT_INRELEASE(MIXED, "Label", File->Label)
417 #undef APT_INRELEASE
418 Section.FindFlag("NotAutomatic", File->Flags, pkgCache::Flag::NotAutomatic);
419 Section.FindFlag("ButAutomaticUpgrades", File->Flags, pkgCache::Flag::ButAutomaticUpgrades);
420
421 return !_error->PendingError();
422 }
423 /*}}}*/
424 // ReleaseIndex::FindInCache - Find this index /*{{{*/
425 pkgCache::RlsFileIterator debReleaseIndex::FindInCache(pkgCache &Cache) const
426 {
427 std::string ReleaseFile;
428 bool const releaseExists = ReleaseFileName(this, ReleaseFile);
429
430 pkgCache::RlsFileIterator File = Cache.RlsFileBegin();
431 for (; File.end() == false; ++File)
432 {
433 if (File->FileName == 0 || ReleaseFile != File.FileName())
434 continue;
435
436 // empty means the file does not exist by "design"
437 if (releaseExists == false && File->Size == 0)
438 return File;
439
440 struct stat St;
441 if (stat(File.FileName(),&St) != 0)
442 {
443 if (_config->FindB("Debug::pkgCacheGen", false))
444 std::clog << "ReleaseIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
445 return pkgCache::RlsFileIterator(Cache);
446 }
447 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
448 {
449 if (_config->FindB("Debug::pkgCacheGen", false))
450 std::clog << "ReleaseIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
451 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
452 << ") doesn't match for " << File.FileName() << std::endl;
453 return pkgCache::RlsFileIterator(Cache);
454 }
455 return File;
456 }
457
458 return File;
459 }
460 /*}}}*/
461
462 class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type
463 {
464 protected:
465
466 bool CreateItemInternal(vector<metaIndex *> &List, string const &URI,
467 string const &Dist, string const &Section,
468 bool const &IsSrc, map<string, string> const &Options) const
469 {
470 // parse arch=, arch+= and arch-= settings
471 map<string, string>::const_iterator arch = Options.find("arch");
472 vector<string> Archs;
473 if (arch != Options.end())
474 Archs = VectorizeString(arch->second, ',');
475 else
476 Archs = APT::Configuration::getArchitectures();
477
478 if ((arch = Options.find("arch+")) != Options.end())
479 {
480 std::vector<std::string> const plusArch = VectorizeString(arch->second, ',');
481 for (std::vector<std::string>::const_iterator plus = plusArch.begin(); plus != plusArch.end(); ++plus)
482 if (std::find(Archs.begin(), Archs.end(), *plus) == Archs.end())
483 Archs.push_back(*plus);
484 }
485 if ((arch = Options.find("arch-")) != Options.end())
486 {
487 std::vector<std::string> const minusArch = VectorizeString(arch->second, ',');
488 for (std::vector<std::string>::const_iterator minus = minusArch.begin(); minus != minusArch.end(); ++minus)
489 {
490 std::vector<std::string>::iterator kill = std::find(Archs.begin(), Archs.end(), *minus);
491 if (kill != Archs.end())
492 Archs.erase(kill);
493 }
494 }
495
496 map<string, string>::const_iterator const trusted = Options.find("trusted");
497
498 debReleaseIndex *Deb = NULL;
499 for (vector<metaIndex *>::const_iterator I = List.begin();
500 I != List.end(); ++I)
501 {
502 // We only worry about debian entries here
503 if (strcmp((*I)->GetType(), "deb") != 0)
504 continue;
505
506 /* This check insures that there will be only one Release file
507 queued for all the Packages files and Sources files it
508 corresponds to. */
509 if ((*I)->GetURI() == URI && (*I)->GetDist() == Dist)
510 {
511 Deb = dynamic_cast<debReleaseIndex*>(*I);
512 if (Deb != NULL)
513 break;
514 }
515 }
516
517 // No currently created Release file indexes this entry, so we create a new one.
518 if (Deb == NULL)
519 {
520 Deb = new debReleaseIndex(URI, Dist);
521 List.push_back(Deb);
522 }
523
524 if (IsSrc == true)
525 Deb->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section, IsSrc));
526 else
527 {
528 if (Dist[Dist.size() - 1] == '/')
529 Deb->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section, IsSrc));
530 else
531 Deb->PushSectionEntry (Archs, new debReleaseIndex::debSectionEntry(Section, IsSrc));
532 }
533
534 if (trusted != Options.end())
535 Deb->SetTrusted(StringToBool(trusted->second, false));
536
537 return true;
538 }
539 };
540
541 debDebFileMetaIndex::debDebFileMetaIndex(std::string const &DebFile)
542 : metaIndex(DebFile, "local-uri", "deb-dist"), DebFile(DebFile)
543 {
544 DebIndex = new debDebPkgFileIndex(DebFile);
545 Indexes = new vector<pkgIndexFile *>();
546 Indexes->push_back(DebIndex);
547 }
548
549
550 class APT_HIDDEN debSLTypeDeb : public debSLTypeDebian
551 {
552 public:
553
554 bool CreateItem(vector<metaIndex *> &List, string const &URI,
555 string const &Dist, string const &Section,
556 std::map<string, string> const &Options) const
557 {
558 return CreateItemInternal(List, URI, Dist, Section, false, Options);
559 }
560
561 debSLTypeDeb()
562 {
563 Name = "deb";
564 Label = "Standard Debian binary tree";
565 }
566 };
567
568 class APT_HIDDEN debSLTypeDebSrc : public debSLTypeDebian
569 {
570 public:
571
572 bool CreateItem(vector<metaIndex *> &List, string const &URI,
573 string const &Dist, string const &Section,
574 std::map<string, string> const &Options) const
575 {
576 return CreateItemInternal(List, URI, Dist, Section, true, Options);
577 }
578
579 debSLTypeDebSrc()
580 {
581 Name = "deb-src";
582 Label = "Standard Debian source tree";
583 }
584 };
585
586 class APT_HIDDEN debSLTypeDebFile : public pkgSourceList::Type
587 {
588 public:
589
590 bool CreateItem(vector<metaIndex *> &List, string const &URI,
591 string const &/*Dist*/, string const &/*Section*/,
592 std::map<string, string> const &/*Options*/) const
593 {
594 metaIndex *mi = new debDebFileMetaIndex(URI);
595 List.push_back(mi);
596 return true;
597 }
598
599 debSLTypeDebFile()
600 {
601 Name = "deb-file";
602 Label = "Debian Deb File";
603 }
604 };
605
606 APT_HIDDEN debSLTypeDeb _apt_DebType;
607 APT_HIDDEN debSLTypeDebSrc _apt_DebSrcType;
608 APT_HIDDEN debSLTypeDebFile _apt_DebFileType;