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