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