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