]> git.saurik.com Git - apt.git/blame_incremental - apt-pkg/deb/debmetaindex.cc
redirect which stderr to /dev/null for consistency
[apt.git] / apt-pkg / deb / debmetaindex.cc
... / ...
CommitLineData
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/sourcelist.h>
12#include <apt-pkg/hashes.h>
13#include <apt-pkg/metaindex.h>
14#include <apt-pkg/pkgcachegen.h>
15#include <apt-pkg/tagfile.h>
16#include <apt-pkg/gpgv.h>
17#include <apt-pkg/macros.h>
18
19#include <map>
20#include <string>
21#include <utility>
22#include <vector>
23#include <algorithm>
24#include <sstream>
25
26#include <sys/stat.h>
27#include <string.h>
28
29#include <apti18n.h>
30
31class APT_HIDDEN debReleaseIndexPrivate /*{{{*/
32{
33 public:
34 struct APT_HIDDEN debSectionEntry
35 {
36 std::string sourcesEntry;
37 std::string Name;
38 std::vector<std::string> Targets;
39 std::vector<std::string> Architectures;
40 std::vector<std::string> Languages;
41 bool UsePDiffs;
42 std::string UseByHash;
43 };
44
45 std::vector<debSectionEntry> DebEntries;
46 std::vector<debSectionEntry> DebSrcEntries;
47
48 metaIndex::TriState CheckValidUntil;
49 time_t ValidUntilMin;
50 time_t ValidUntilMax;
51
52 std::vector<std::string> Architectures;
53
54 debReleaseIndexPrivate() : CheckValidUntil(metaIndex::TRI_UNSET), ValidUntilMin(0), ValidUntilMax(0) {}
55};
56 /*}}}*/
57// ReleaseIndex::MetaIndex* - display helpers /*{{{*/
58std::string debReleaseIndex::MetaIndexInfo(const char *Type) const
59{
60 std::string Info = ::URI::ArchiveOnly(URI) + ' ';
61 if (Dist[Dist.size() - 1] == '/')
62 {
63 if (Dist != "/")
64 Info += Dist;
65 }
66 else
67 Info += Dist;
68 Info += " ";
69 Info += Type;
70 return Info;
71}
72std::string debReleaseIndex::Describe() const
73{
74 return MetaIndexInfo("Release");
75}
76
77std::string debReleaseIndex::MetaIndexFile(const char *Type) const
78{
79 return _config->FindDir("Dir::State::lists") +
80 URItoFileName(MetaIndexURI(Type));
81}
82
83std::string debReleaseIndex::MetaIndexURI(const char *Type) const
84{
85 std::string Res;
86
87 if (Dist == "/")
88 Res = URI;
89 else if (Dist[Dist.size()-1] == '/')
90 Res = URI + Dist;
91 else
92 Res = URI + "dists/" + Dist + "/";
93
94 Res += Type;
95 return Res;
96}
97 /*}}}*/
98// ReleaseIndex Con- and Destructors /*{{{*/
99debReleaseIndex::debReleaseIndex(std::string const &URI, std::string const &Dist) :
100 metaIndex(URI, Dist, "deb"), d(new debReleaseIndexPrivate())
101{}
102debReleaseIndex::debReleaseIndex(std::string const &URI, std::string const &Dist, bool const pTrusted) :
103 metaIndex(URI, Dist, "deb"), d(new debReleaseIndexPrivate())
104{
105 Trusted = pTrusted ? TRI_YES : TRI_NO;
106}
107debReleaseIndex::~debReleaseIndex() {
108 if (d != NULL)
109 delete d;
110}
111 /*}}}*/
112// ReleaseIndex::GetIndexTargets /*{{{*/
113static void GetIndexTargetsFor(char const * const Type, std::string const &URI, std::string const &Dist,
114 std::vector<debReleaseIndexPrivate::debSectionEntry> const &entries,
115 std::vector<IndexTarget> &IndexTargets)
116{
117 bool const flatArchive = (Dist[Dist.length() - 1] == '/');
118 std::string baseURI = URI;
119 if (flatArchive)
120 {
121 if (Dist != "/")
122 baseURI += Dist;
123 }
124 else
125 baseURI += "dists/" + Dist + "/";
126 std::string const Release = (Dist == "/") ? "" : Dist;
127 std::string const Site = ::URI::ArchiveOnly(URI);
128
129 std::string DefCompressionTypes;
130 {
131 std::vector<std::string> types = APT::Configuration::getCompressionTypes();
132 if (types.empty() == false)
133 {
134 std::ostringstream os;
135 std::copy(types.begin(), types.end()-1, std::ostream_iterator<std::string>(os, " "));
136 os << *types.rbegin();
137 DefCompressionTypes = os.str();
138 }
139 }
140 std::string const NativeArch = _config->Find("APT::Architecture");
141 bool const GzipIndex = _config->FindB("Acquire::GzipIndexes", false);
142 for (std::vector<debReleaseIndexPrivate::debSectionEntry>::const_iterator E = entries.begin(); E != entries.end(); ++E)
143 {
144 for (std::vector<std::string>::const_iterator T = E->Targets.begin(); T != E->Targets.end(); ++T)
145 {
146#define APT_T_CONFIG_STR(X, Y) _config->Find(std::string("Acquire::IndexTargets::") + Type + "::" + *T + "::" + (X), (Y))
147#define APT_T_CONFIG_BOOL(X, Y) _config->FindB(std::string("Acquire::IndexTargets::") + Type + "::" + *T + "::" + (X), (Y))
148 std::string const tplMetaKey = APT_T_CONFIG_STR(flatArchive ? "flatMetaKey" : "MetaKey", "");
149 std::string const tplShortDesc = APT_T_CONFIG_STR("ShortDescription", "");
150 std::string const tplLongDesc = "$(SITE) " + APT_T_CONFIG_STR(flatArchive ? "flatDescription" : "Description", "");
151 bool const IsOptional = APT_T_CONFIG_BOOL("Optional", true);
152 bool const KeepCompressed = APT_T_CONFIG_BOOL("KeepCompressed", GzipIndex);
153 bool const DefaultEnabled = APT_T_CONFIG_BOOL("DefaultEnabled", true);
154 bool const UsePDiffs = APT_T_CONFIG_BOOL("PDiffs", E->UsePDiffs);
155 std::string const UseByHash = APT_T_CONFIG_STR("By-Hash", E->UseByHash);
156 std::string const CompressionTypes = APT_T_CONFIG_STR("CompressionTypes", DefCompressionTypes);
157#undef APT_T_CONFIG_BOOL
158#undef APT_T_CONFIG_STR
159 if (tplMetaKey.empty())
160 continue;
161
162 for (std::vector<std::string>::const_iterator L = E->Languages.begin(); L != E->Languages.end(); ++L)
163 {
164 if (*L == "none" && tplMetaKey.find("$(LANGUAGE)") != std::string::npos)
165 continue;
166
167 for (std::vector<std::string>::const_iterator A = E->Architectures.begin(); A != E->Architectures.end(); ++A)
168 {
169 // available in templates
170 std::map<std::string, std::string> Options;
171 Options.insert(std::make_pair("SITE", Site));
172 Options.insert(std::make_pair("RELEASE", Release));
173 if (tplMetaKey.find("$(COMPONENT)") != std::string::npos)
174 Options.insert(std::make_pair("COMPONENT", E->Name));
175 if (tplMetaKey.find("$(LANGUAGE)") != std::string::npos)
176 Options.insert(std::make_pair("LANGUAGE", *L));
177 if (tplMetaKey.find("$(ARCHITECTURE)") != std::string::npos)
178 Options.insert(std::make_pair("ARCHITECTURE", *A));
179 else if (tplMetaKey.find("$(NATIVE_ARCHITECTURE)") != std::string::npos)
180 Options.insert(std::make_pair("ARCHITECTURE", NativeArch));
181 if (tplMetaKey.find("$(NATIVE_ARCHITECTURE)") != std::string::npos)
182 Options.insert(std::make_pair("NATIVE_ARCHITECTURE", NativeArch));
183
184 std::string MetaKey = tplMetaKey;
185 std::string ShortDesc = tplShortDesc;
186 std::string LongDesc = tplLongDesc;
187 for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
188 {
189 MetaKey = SubstVar(MetaKey, std::string("$(") + O->first + ")", O->second);
190 ShortDesc = SubstVar(ShortDesc, std::string("$(") + O->first + ")", O->second);
191 LongDesc = SubstVar(LongDesc, std::string("$(") + O->first + ")", O->second);
192 }
193
194 {
195 auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &IT) {
196 return MetaKey == IT.MetaKey && baseURI == IT.Option(IndexTarget::BASE_URI) &&
197 E->sourcesEntry == IT.Option(IndexTarget::SOURCESENTRY) && *T == IT.Option(IndexTarget::CREATED_BY);
198 });
199 if (dup != IndexTargets.end())
200 {
201 if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
202 break;
203 continue;
204 }
205 }
206
207 {
208 auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &IT) {
209 return MetaKey == IT.MetaKey && baseURI == IT.Option(IndexTarget::BASE_URI) &&
210 E->sourcesEntry == IT.Option(IndexTarget::SOURCESENTRY) && *T != IT.Option(IndexTarget::CREATED_BY);
211 });
212 if (dup != IndexTargets.end())
213 {
214 std::string const dupT = dup->Option(IndexTarget::CREATED_BY);
215 std::string const dupEntry = dup->Option(IndexTarget::SOURCESENTRY);
216 //TRANSLATOR: an identifier like Packages; Releasefile key indicating
217 // a file like main/binary-amd64/Packages; another identifier like Contents;
218 // filename and linenumber of the sources.list entry currently parsed
219 _error->Warning(_("Target %s wants to acquire the same file (%s) as %s from source %s"),
220 T->c_str(), MetaKey.c_str(), dupT.c_str(), dupEntry.c_str());
221 if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
222 break;
223 continue;
224 }
225 }
226
227 {
228 auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &T) {
229 return MetaKey == T.MetaKey && baseURI == T.Option(IndexTarget::BASE_URI) &&
230 E->sourcesEntry != T.Option(IndexTarget::SOURCESENTRY);
231 });
232 if (dup != IndexTargets.end())
233 {
234 std::string const dupEntry = dup->Option(IndexTarget::SOURCESENTRY);
235 //TRANSLATOR: an identifier like Packages; Releasefile key indicating
236 // a file like main/binary-amd64/Packages; filename and linenumber of
237 // two sources.list entries
238 _error->Warning(_("Target %s (%s) is configured multiple times in %s and %s"),
239 T->c_str(), MetaKey.c_str(), dupEntry.c_str(), E->sourcesEntry.c_str());
240 if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
241 break;
242 continue;
243 }
244 }
245
246 // not available in templates, but in the indextarget
247 Options.insert(std::make_pair("BASE_URI", baseURI));
248 Options.insert(std::make_pair("REPO_URI", URI));
249 Options.insert(std::make_pair("TARGET_OF", Type));
250 Options.insert(std::make_pair("CREATED_BY", *T));
251 Options.insert(std::make_pair("PDIFFS", UsePDiffs ? "yes" : "no"));
252 Options.insert(std::make_pair("BY_HASH", UseByHash));
253 Options.insert(std::make_pair("DEFAULTENABLED", DefaultEnabled ? "yes" : "no"));
254 Options.insert(std::make_pair("COMPRESSIONTYPES", CompressionTypes));
255 Options.insert(std::make_pair("SOURCESENTRY", E->sourcesEntry));
256
257 bool IsOpt = IsOptional;
258 if (IsOpt == false)
259 {
260 auto const arch = Options.find("ARCHITECTURE");
261 if (arch != Options.end() && arch->second == "all")
262 IsOpt = true;
263 }
264
265 IndexTarget Target(
266 MetaKey,
267 ShortDesc,
268 LongDesc,
269 Options.find("BASE_URI")->second + MetaKey,
270 IsOpt,
271 KeepCompressed,
272 Options
273 );
274 IndexTargets.push_back(Target);
275
276 if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos)
277 break;
278
279 }
280
281 if (tplMetaKey.find("$(LANGUAGE)") == std::string::npos)
282 break;
283
284 }
285
286 }
287 }
288}
289std::vector<IndexTarget> debReleaseIndex::GetIndexTargets() const
290{
291 std::vector<IndexTarget> IndexTargets;
292 GetIndexTargetsFor("deb-src", URI, Dist, d->DebSrcEntries, IndexTargets);
293 GetIndexTargetsFor("deb", URI, Dist, d->DebEntries, IndexTargets);
294 return IndexTargets;
295}
296 /*}}}*/
297void debReleaseIndex::AddComponent(std::string const &sourcesEntry, /*{{{*/
298 bool const isSrc, std::string const &Name,
299 std::vector<std::string> const &Targets,
300 std::vector<std::string> const &Architectures,
301 std::vector<std::string> Languages,
302 bool const usePDiffs, std::string const &useByHash)
303{
304 if (Languages.empty() == true)
305 Languages.push_back("none");
306 debReleaseIndexPrivate::debSectionEntry const entry = {
307 sourcesEntry, Name, Targets, Architectures, Languages, usePDiffs, useByHash
308 };
309 if (isSrc)
310 d->DebSrcEntries.push_back(entry);
311 else
312 d->DebEntries.push_back(entry);
313}
314 /*}}}*/
315
316bool debReleaseIndex::Load(std::string const &Filename, std::string * const ErrorText)/*{{{*/
317{
318 LoadedSuccessfully = TRI_NO;
319 FileFd Fd;
320 if (OpenMaybeClearSignedFile(Filename, Fd) == false)
321 return false;
322
323 pkgTagFile TagFile(&Fd, Fd.Size());
324 if (Fd.IsOpen() == false || Fd.Failed())
325 {
326 if (ErrorText != NULL)
327 strprintf(*ErrorText, _("Unable to parse Release file %s"),Filename.c_str());
328 return false;
329 }
330
331 pkgTagSection Section;
332 const char *Start, *End;
333 if (TagFile.Step(Section) == false)
334 {
335 if (ErrorText != NULL)
336 strprintf(*ErrorText, _("No sections in Release file %s"), Filename.c_str());
337 return false;
338 }
339 // FIXME: find better tag name
340 SupportsAcquireByHash = Section.FindB("Acquire-By-Hash", false);
341
342 Suite = Section.FindS("Suite");
343 Codename = Section.FindS("Codename");
344 {
345 std::string const archs = Section.FindS("Architectures");
346 if (archs.empty() == false)
347 d->Architectures = VectorizeString(archs, ' ');
348 }
349
350 bool FoundHashSum = false;
351 for (int i=0;HashString::SupportedHashes()[i] != NULL; i++)
352 {
353 if (!Section.Find(HashString::SupportedHashes()[i], Start, End))
354 continue;
355
356 std::string Name;
357 std::string Hash;
358 unsigned long long Size;
359 while (Start < End)
360 {
361 if (!parseSumData(Start, End, Name, Hash, Size))
362 return false;
363
364 if (Entries.find(Name) == Entries.end())
365 {
366 metaIndex::checkSum *Sum = new metaIndex::checkSum;
367 Sum->MetaKeyFilename = Name;
368 Sum->Size = Size;
369 Sum->Hashes.FileSize(Size);
370 APT_IGNORE_DEPRECATED(Sum->Hash = HashString(HashString::SupportedHashes()[i],Hash);)
371 Entries[Name] = Sum;
372 }
373 Entries[Name]->Hashes.push_back(HashString(HashString::SupportedHashes()[i],Hash));
374 FoundHashSum = true;
375 }
376 }
377
378 if(FoundHashSum == false)
379 {
380 if (ErrorText != NULL)
381 strprintf(*ErrorText, _("No Hash entry in Release file %s"), Filename.c_str());
382 return false;
383 }
384
385 std::string const StrDate = Section.FindS("Date");
386 if (RFC1123StrToTime(StrDate.c_str(), Date) == false)
387 {
388 if (ErrorText != NULL)
389 strprintf(*ErrorText, _("Invalid 'Date' entry in Release file %s"), Filename.c_str());
390 return false;
391 }
392
393 bool CheckValidUntil = _config->FindB("Acquire::Check-Valid-Until", true);
394 if (d->CheckValidUntil == metaIndex::TRI_NO)
395 CheckValidUntil = false;
396 else if (d->CheckValidUntil == metaIndex::TRI_YES)
397 CheckValidUntil = true;
398
399 if (CheckValidUntil == true)
400 {
401 std::string const Label = Section.FindS("Label");
402 std::string const StrValidUntil = Section.FindS("Valid-Until");
403
404 // if we have a Valid-Until header in the Release file, use it as default
405 if (StrValidUntil.empty() == false)
406 {
407 if(RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false)
408 {
409 if (ErrorText != NULL)
410 strprintf(*ErrorText, _("Invalid 'Valid-Until' entry in Release file %s"), Filename.c_str());
411 return false;
412 }
413 }
414 // get the user settings for this archive and use what expires earlier
415 time_t MaxAge = d->ValidUntilMax;
416 if (MaxAge == 0)
417 {
418 MaxAge = _config->FindI("Acquire::Max-ValidTime", 0);
419 if (Label.empty() == false)
420 MaxAge = _config->FindI(("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge);
421 }
422 time_t MinAge = d->ValidUntilMin;
423 if (MinAge == 0)
424 {
425 MinAge = _config->FindI("Acquire::Min-ValidTime", 0);
426 if (Label.empty() == false)
427 MinAge = _config->FindI(("Acquire::Min-ValidTime::" + Label).c_str(), MinAge);
428 }
429
430 if (MinAge != 0 && ValidUntil != 0) {
431 time_t const min_date = Date + MinAge;
432 if (ValidUntil < min_date)
433 ValidUntil = min_date;
434 }
435 if (MaxAge != 0) {
436 time_t const max_date = Date + MaxAge;
437 if (ValidUntil == 0 || ValidUntil > max_date)
438 ValidUntil = max_date;
439 }
440 }
441
442 LoadedSuccessfully = TRI_YES;
443 return true;
444}
445 /*}}}*/
446metaIndex * debReleaseIndex::UnloadedClone() const /*{{{*/
447{
448 if (Trusted == TRI_NO)
449 return new debReleaseIndex(URI, Dist, false);
450 else if (Trusted == TRI_YES)
451 return new debReleaseIndex(URI, Dist, true);
452 else
453 return new debReleaseIndex(URI, Dist);
454}
455 /*}}}*/
456bool debReleaseIndex::parseSumData(const char *&Start, const char *End, /*{{{*/
457 std::string &Name, std::string &Hash, unsigned long long &Size)
458{
459 Name = "";
460 Hash = "";
461 Size = 0;
462 /* Skip over the first blank */
463 while ((*Start == '\t' || *Start == ' ' || *Start == '\n' || *Start == '\r')
464 && Start < End)
465 Start++;
466 if (Start >= End)
467 return false;
468
469 /* Move EntryEnd to the end of the first entry (the hash) */
470 const char *EntryEnd = Start;
471 while ((*EntryEnd != '\t' && *EntryEnd != ' ')
472 && EntryEnd < End)
473 EntryEnd++;
474 if (EntryEnd == End)
475 return false;
476
477 Hash.append(Start, EntryEnd-Start);
478
479 /* Skip over intermediate blanks */
480 Start = EntryEnd;
481 while (*Start == '\t' || *Start == ' ')
482 Start++;
483 if (Start >= End)
484 return false;
485
486 EntryEnd = Start;
487 /* Find the end of the second entry (the size) */
488 while ((*EntryEnd != '\t' && *EntryEnd != ' ' )
489 && EntryEnd < End)
490 EntryEnd++;
491 if (EntryEnd == End)
492 return false;
493
494 Size = strtoull (Start, NULL, 10);
495
496 /* Skip over intermediate blanks */
497 Start = EntryEnd;
498 while (*Start == '\t' || *Start == ' ')
499 Start++;
500 if (Start >= End)
501 return false;
502
503 EntryEnd = Start;
504 /* Find the end of the third entry (the filename) */
505 while ((*EntryEnd != '\t' && *EntryEnd != ' ' &&
506 *EntryEnd != '\n' && *EntryEnd != '\r')
507 && EntryEnd < End)
508 EntryEnd++;
509
510 Name.append(Start, EntryEnd-Start);
511 Start = EntryEnd; //prepare for the next round
512 return true;
513}
514 /*}}}*/
515
516bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll)/*{{{*/
517{
518 std::vector<IndexTarget> const targets = GetIndexTargets();
519#define APT_TARGET(X) IndexTarget("", X, MetaIndexInfo(X), MetaIndexURI(X), false, false, std::map<std::string,std::string>())
520 pkgAcqMetaClearSig * const TransactionManager = new pkgAcqMetaClearSig(Owner,
521 APT_TARGET("InRelease"), APT_TARGET("Release"), APT_TARGET("Release.gpg"),
522 targets, this);
523#undef APT_TARGET
524 // special case for --print-uris
525 if (GetAll)
526 for (auto const &Target: targets)
527 new pkgAcqIndex(Owner, TransactionManager, Target);
528
529 return true;
530}
531 /*}}}*/
532// ReleaseIndex::Set* TriState options /*{{{*/
533bool debReleaseIndex::SetTrusted(TriState const pTrusted)
534{
535 if (Trusted == TRI_UNSET)
536 Trusted = pTrusted;
537 else if (Trusted != pTrusted)
538 // TRANSLATOR: The first is an option name from sources.list manpage, the other two URI and Suite
539 return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Trusted", URI.c_str(), Dist.c_str());
540 return true;
541}
542bool debReleaseIndex::SetCheckValidUntil(TriState const pCheckValidUntil)
543{
544 if (d->CheckValidUntil == TRI_UNSET)
545 d->CheckValidUntil = pCheckValidUntil;
546 else if (d->CheckValidUntil != pCheckValidUntil)
547 return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Check-Valid-Until", URI.c_str(), Dist.c_str());
548 return true;
549}
550bool debReleaseIndex::SetValidUntilMin(time_t const Valid)
551{
552 if (d->ValidUntilMin == 0)
553 d->ValidUntilMin = Valid;
554 else if (d->ValidUntilMin != Valid)
555 return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Min-ValidTime", URI.c_str(), Dist.c_str());
556 return true;
557}
558bool debReleaseIndex::SetValidUntilMax(time_t const Valid)
559{
560 if (d->ValidUntilMax == 0)
561 d->ValidUntilMax = Valid;
562 else if (d->ValidUntilMax != Valid)
563 return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Max-ValidTime", URI.c_str(), Dist.c_str());
564 return true;
565}
566bool debReleaseIndex::SetSignedBy(std::string const &pSignedBy)
567{
568 if (SignedBy.empty() == true && pSignedBy.empty() == false)
569 {
570 if (pSignedBy[0] == '/') // no check for existence as we could be chrooting later or such things
571 ; // absolute path to a keyring file
572 else
573 {
574 // we could go all fancy and allow short/long/string matches as gpgv/apt-key does,
575 // but fingerprints are harder to fake than the others and this option is set once,
576 // not interactively all the time so easy to type is not really a concern.
577 std::string finger = pSignedBy;
578 finger.erase(std::remove(finger.begin(), finger.end(), ' '), finger.end());
579 std::transform(finger.begin(), finger.end(), finger.begin(), ::toupper);
580 if (finger.length() != 40 || finger.find_first_not_of("0123456789ABCDEF") != std::string::npos)
581 return _error->Error(_("Invalid value set for option %s regarding source %s %s (%s)"), "Signed-By", URI.c_str(), Dist.c_str(), "not a fingerprint");
582 }
583 SignedBy = pSignedBy;
584 }
585 else if (SignedBy != pSignedBy)
586 return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Signed-By", URI.c_str(), Dist.c_str());
587 return true;
588}
589 /*}}}*/
590// ReleaseIndex::IsTrusted /*{{{*/
591bool debReleaseIndex::IsTrusted() const
592{
593 if (Trusted == TRI_YES)
594 return true;
595 else if (Trusted == TRI_NO)
596 return false;
597
598
599 if(_config->FindB("APT::Authentication::TrustCDROM", false))
600 if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
601 return true;
602
603 if (FileExists(MetaIndexFile("Release.gpg")))
604 return true;
605
606 return FileExists(MetaIndexFile("InRelease"));
607}
608 /*}}}*/
609bool debReleaseIndex::IsArchitectureSupported(std::string const &arch) const/*{{{*/
610{
611 if (d->Architectures.empty())
612 return true;
613 return std::find(d->Architectures.begin(), d->Architectures.end(), arch) != d->Architectures.end();
614}
615 /*}}}*/
616std::vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() /*{{{*/
617{
618 if (Indexes != NULL)
619 return Indexes;
620
621 Indexes = new std::vector<pkgIndexFile*>();
622 bool const istrusted = IsTrusted();
623 for (auto const &T: GetIndexTargets())
624 {
625 std::string const TargetName = T.Option(IndexTarget::CREATED_BY);
626 if (TargetName == "Packages")
627 Indexes->push_back(new debPackagesIndex(T, istrusted));
628 else if (TargetName == "Sources")
629 Indexes->push_back(new debSourcesIndex(T, istrusted));
630 else if (TargetName == "Translations")
631 Indexes->push_back(new debTranslationsIndex(T));
632 }
633 return Indexes;
634}
635 /*}}}*/
636
637static bool ReleaseFileName(debReleaseIndex const * const That, std::string &ReleaseFile)/*{{{*/
638{
639 ReleaseFile = That->MetaIndexFile("InRelease");
640 bool releaseExists = false;
641 if (FileExists(ReleaseFile) == true)
642 releaseExists = true;
643 else
644 {
645 ReleaseFile = That->MetaIndexFile("Release");
646 if (FileExists(ReleaseFile))
647 releaseExists = true;
648 }
649 return releaseExists;
650}
651 /*}}}*/
652bool debReleaseIndex::Merge(pkgCacheGenerator &Gen,OpProgress * /*Prog*/) const/*{{{*/
653{
654 std::string ReleaseFile;
655 bool const releaseExists = ReleaseFileName(this, ReleaseFile);
656
657 ::URI Tmp(URI);
658 if (Gen.SelectReleaseFile(ReleaseFile, Tmp.Host) == false)
659 return _error->Error("Problem with SelectReleaseFile %s", ReleaseFile.c_str());
660
661 if (releaseExists == false)
662 return true;
663
664 FileFd Rel;
665 // Beware: The 'Release' file might be clearsigned in case the
666 // signature for an 'InRelease' file couldn't be checked
667 if (OpenMaybeClearSignedFile(ReleaseFile, Rel) == false)
668 return false;
669
670 // Store the IMS information
671 pkgCache::RlsFileIterator File = Gen.GetCurRlsFile();
672 pkgCacheGenerator::Dynamic<pkgCache::RlsFileIterator> DynFile(File);
673 // Rel can't be used as this is potentially a temporary file
674 struct stat Buf;
675 if (stat(ReleaseFile.c_str(), &Buf) != 0)
676 return _error->Errno("fstat", "Unable to stat file %s", ReleaseFile.c_str());
677 File->Size = Buf.st_size;
678 File->mtime = Buf.st_mtime;
679
680 pkgTagFile TagFile(&Rel, Rel.Size());
681 pkgTagSection Section;
682 if (Rel.IsOpen() == false || Rel.Failed() || TagFile.Step(Section) == false)
683 return false;
684
685 std::string data;
686 #define APT_INRELEASE(TYPE, TAG, STORE) \
687 data = Section.FindS(TAG); \
688 if (data.empty() == false) \
689 { \
690 map_stringitem_t const storage = Gen.StoreString(pkgCacheGenerator::TYPE, data); \
691 if (storage == 0) return false; \
692 STORE = storage; \
693 }
694 APT_INRELEASE(MIXED, "Suite", File->Archive)
695 APT_INRELEASE(VERSIONNUMBER, "Version", File->Version)
696 APT_INRELEASE(MIXED, "Origin", File->Origin)
697 APT_INRELEASE(MIXED, "Codename", File->Codename)
698 APT_INRELEASE(MIXED, "Label", File->Label)
699 #undef APT_INRELEASE
700 Section.FindFlag("NotAutomatic", File->Flags, pkgCache::Flag::NotAutomatic);
701 Section.FindFlag("ButAutomaticUpgrades", File->Flags, pkgCache::Flag::ButAutomaticUpgrades);
702
703 return true;
704}
705 /*}}}*/
706// ReleaseIndex::FindInCache - Find this index /*{{{*/
707pkgCache::RlsFileIterator debReleaseIndex::FindInCache(pkgCache &Cache, bool const ModifyCheck) const
708{
709 std::string ReleaseFile;
710 bool const releaseExists = ReleaseFileName(this, ReleaseFile);
711
712 pkgCache::RlsFileIterator File = Cache.RlsFileBegin();
713 for (; File.end() == false; ++File)
714 {
715 if (File->FileName == 0 || ReleaseFile != File.FileName())
716 continue;
717
718 // empty means the file does not exist by "design"
719 if (ModifyCheck == false || (releaseExists == false && File->Size == 0))
720 return File;
721
722 struct stat St;
723 if (stat(File.FileName(),&St) != 0)
724 {
725 if (_config->FindB("Debug::pkgCacheGen", false))
726 std::clog << "ReleaseIndex::FindInCache - stat failed on " << File.FileName() << std::endl;
727 return pkgCache::RlsFileIterator(Cache);
728 }
729 if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime)
730 {
731 if (_config->FindB("Debug::pkgCacheGen", false))
732 std::clog << "ReleaseIndex::FindInCache - size (" << St.st_size << " <> " << File->Size
733 << ") or mtime (" << St.st_mtime << " <> " << File->mtime
734 << ") doesn't match for " << File.FileName() << std::endl;
735 return pkgCache::RlsFileIterator(Cache);
736 }
737 return File;
738 }
739
740 return File;
741}
742 /*}}}*/
743
744static std::vector<std::string> parsePlusMinusOptions(std::string const &Name, /*{{{*/
745 std::map<std::string, std::string> const &Options, std::vector<std::string> const &defaultValues)
746{
747 std::map<std::string, std::string>::const_iterator val = Options.find(Name);
748 std::vector<std::string> Values;
749 if (val != Options.end())
750 Values = VectorizeString(val->second, ',');
751 else
752 Values = defaultValues;
753
754 // all is a very special architecture users shouldn't be concerned with explicitly
755 if (Name == "arch" && std::find(Values.begin(), Values.end(), "all") == Values.end())
756 Values.push_back("all");
757
758 if ((val = Options.find(Name + "+")) != Options.end())
759 {
760 std::vector<std::string> const plus = VectorizeString(val->second, ',');
761 std::copy_if(plus.begin(), plus.end(), std::back_inserter(Values), [&Values](std::string const &v) {
762 return std::find(Values.begin(), Values.end(), v) == Values.end();
763 });
764 }
765 if ((val = Options.find(Name + "-")) != Options.end())
766 {
767 std::vector<std::string> const minus = VectorizeString(val->second, ',');
768 Values.erase(std::remove_if(Values.begin(), Values.end(), [&minus](std::string const &v) {
769 return std::find(minus.begin(), minus.end(), v) != minus.end();
770 }), Values.end());
771 }
772 return Values;
773}
774 /*}}}*/
775class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type /*{{{*/
776{
777 metaIndex::TriState GetTriStateOption(std::map<std::string, std::string>const &Options, char const * const name) const
778 {
779 std::map<std::string, std::string>::const_iterator const opt = Options.find(name);
780 if (opt != Options.end())
781 return StringToBool(opt->second, false) ? metaIndex::TRI_YES : metaIndex::TRI_NO;
782 return metaIndex::TRI_DONTCARE;
783 }
784
785 time_t GetTimeOption(std::map<std::string, std::string>const &Options, char const * const name) const
786 {
787 std::map<std::string, std::string>::const_iterator const opt = Options.find(name);
788 if (opt == Options.end())
789 return 0;
790 return strtoull(opt->second.c_str(), NULL, 10);
791 }
792
793 protected:
794
795 bool CreateItemInternal(std::vector<metaIndex *> &List, std::string const &URI,
796 std::string const &Dist, std::string const &Section,
797 bool const &IsSrc, std::map<std::string, std::string> const &Options) const
798 {
799 debReleaseIndex *Deb = NULL;
800 for (std::vector<metaIndex *>::const_iterator I = List.begin();
801 I != List.end(); ++I)
802 {
803 // We only worry about debian entries here
804 if (strcmp((*I)->GetType(), "deb") != 0)
805 continue;
806
807 /* This check insures that there will be only one Release file
808 queued for all the Packages files and Sources files it
809 corresponds to. */
810 if ((*I)->GetURI() == URI && (*I)->GetDist() == Dist)
811 {
812 Deb = dynamic_cast<debReleaseIndex*>(*I);
813 if (Deb != NULL)
814 break;
815 }
816 }
817
818 // No currently created Release file indexes this entry, so we create a new one.
819 if (Deb == NULL)
820 {
821 Deb = new debReleaseIndex(URI, Dist);
822 List.push_back(Deb);
823 }
824
825 std::vector<std::string> const alltargets = _config->FindVector(std::string("Acquire::IndexTargets::") + Name, "", true);
826 std::vector<std::string> deftargets;
827 deftargets.reserve(alltargets.size());
828 std::copy_if(alltargets.begin(), alltargets.end(), std::back_inserter(deftargets), [&](std::string const &t) {
829 std::string c = "Acquire::IndexTargets::";
830 c.append(Name).append("::").append(t).append("::DefaultEnabled");
831 return _config->FindB(c, true);
832 });
833 std::vector<std::string> mytargets = parsePlusMinusOptions("target", Options, deftargets);
834 for (auto const &target : alltargets)
835 {
836 std::map<std::string, std::string>::const_iterator const opt = Options.find(target);
837 if (opt == Options.end())
838 continue;
839 auto const tarItr = std::find(mytargets.begin(), mytargets.end(), target);
840 bool const optValue = StringToBool(opt->second);
841 if (optValue == true && tarItr == mytargets.end())
842 mytargets.push_back(target);
843 else if (optValue == false && tarItr != mytargets.end())
844 mytargets.erase(std::remove(mytargets.begin(), mytargets.end(), target), mytargets.end());
845 }
846
847 bool UsePDiffs = _config->FindB("Acquire::PDiffs", true);
848 {
849 std::map<std::string, std::string>::const_iterator const opt = Options.find("pdiffs");
850 if (opt != Options.end())
851 UsePDiffs = StringToBool(opt->second);
852 }
853
854 std::string UseByHash = _config->Find("APT::Acquire::By-Hash", "yes");
855 UseByHash = _config->Find("Acquire::By-Hash", UseByHash);
856 {
857 std::string const host = ::URI(URI).Host;
858 UseByHash = _config->Find("APT::Acquire::" + host + "::By-Hash", UseByHash);
859 UseByHash = _config->Find("Acquire::" + host + "::By-Hash", UseByHash);
860 std::map<std::string, std::string>::const_iterator const opt = Options.find("by-hash");
861 if (opt != Options.end())
862 UseByHash = opt->second;
863 }
864
865 auto const entry = Options.find("sourceslist-entry");
866 Deb->AddComponent(
867 entry->second,
868 IsSrc,
869 Section,
870 mytargets,
871 parsePlusMinusOptions("arch", Options, APT::Configuration::getArchitectures()),
872 parsePlusMinusOptions("lang", Options, APT::Configuration::getLanguages(true)),
873 UsePDiffs,
874 UseByHash
875 );
876
877 if (Deb->SetTrusted(GetTriStateOption(Options, "trusted")) == false ||
878 Deb->SetCheckValidUntil(GetTriStateOption(Options, "check-valid-until")) == false ||
879 Deb->SetValidUntilMax(GetTimeOption(Options, "valid-until-max")) == false ||
880 Deb->SetValidUntilMin(GetTimeOption(Options, "valid-until-min")) == false)
881 return false;
882
883 std::map<std::string, std::string>::const_iterator const signedby = Options.find("signed-by");
884 if (signedby == Options.end())
885 {
886 if (Deb->SetSignedBy("") == false)
887 return false;
888 }
889 else
890 {
891 if (Deb->SetSignedBy(signedby->second) == false)
892 return false;
893 }
894
895 return true;
896 }
897
898 debSLTypeDebian(char const * const Name, char const * const Label) : Type(Name, Label)
899 {
900 }
901};
902 /*}}}*/
903class APT_HIDDEN debSLTypeDeb : public debSLTypeDebian /*{{{*/
904{
905 public:
906
907 bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI,
908 std::string const &Dist, std::string const &Section,
909 std::map<std::string, std::string> const &Options) const APT_OVERRIDE
910 {
911 return CreateItemInternal(List, URI, Dist, Section, false, Options);
912 }
913
914 debSLTypeDeb() : debSLTypeDebian("deb", "Debian binary tree")
915 {
916 }
917};
918 /*}}}*/
919class APT_HIDDEN debSLTypeDebSrc : public debSLTypeDebian /*{{{*/
920{
921 public:
922
923 bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI,
924 std::string const &Dist, std::string const &Section,
925 std::map<std::string, std::string> const &Options) const APT_OVERRIDE
926 {
927 return CreateItemInternal(List, URI, Dist, Section, true, Options);
928 }
929
930 debSLTypeDebSrc() : debSLTypeDebian("deb-src", "Debian source tree")
931 {
932 }
933};
934 /*}}}*/
935
936APT_HIDDEN debSLTypeDeb _apt_DebType;
937APT_HIDDEN debSLTypeDebSrc _apt_DebSrcType;