]>
Commit | Line | Data |
---|---|---|
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 |
31 | class APT_HIDDEN debReleaseIndexPrivate /*{{{*/ |
32 | { | |
33 | public: | |
34 | struct APT_HIDDEN debSectionEntry | |
35 | { | |
7f2d1eef DK |
36 | std::string const sourcesEntry; |
37 | std::string const Name; | |
38 | std::vector<std::string> const Targets; | |
39 | std::vector<std::string> const Architectures; | |
40 | std::vector<std::string> const Languages; | |
41 | bool const UsePDiffs; | |
42 | std::string const 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; |
d03b947b | 54 | std::map<std::string, std::string> const ReleaseOptions; |
1dd20368 | 55 | |
d03b947b | 56 | debReleaseIndexPrivate(std::map<std::string, std::string> const &Options) : CheckValidUntil(metaIndex::TRI_UNSET), ValidUntilMin(0), ValidUntilMax(0), ReleaseOptions(Options) {} |
463c8d80 DK |
57 | }; |
58 | /*}}}*/ | |
59 | // ReleaseIndex::MetaIndex* - display helpers /*{{{*/ | |
60 | std::string debReleaseIndex::MetaIndexInfo(const char *Type) const | |
7db98ffc | 61 | { |
463c8d80 | 62 | std::string Info = ::URI::ArchiveOnly(URI) + ' '; |
7db98ffc MZ |
63 | if (Dist[Dist.size() - 1] == '/') |
64 | { | |
65 | if (Dist != "/") | |
66 | Info += Dist; | |
67 | } | |
68 | else | |
69 | Info += Dist; | |
70 | Info += " "; | |
71 | Info += Type; | |
72 | return Info; | |
73 | } | |
b07aeb1a DK |
74 | std::string debReleaseIndex::Describe() const |
75 | { | |
76 | return MetaIndexInfo("Release"); | |
77 | } | |
7db98ffc | 78 | |
463c8d80 | 79 | std::string debReleaseIndex::MetaIndexFile(const char *Type) const |
7db98ffc MZ |
80 | { |
81 | return _config->FindDir("Dir::State::lists") + | |
82 | URItoFileName(MetaIndexURI(Type)); | |
83 | } | |
b90faf24 | 84 | static std::string constructMetaIndexURI(std::string URI, std::string const &Dist, char const * const Type) |
7db98ffc | 85 | { |
7db98ffc | 86 | if (Dist == "/") |
b90faf24 | 87 | ; |
7db98ffc | 88 | else if (Dist[Dist.size()-1] == '/') |
b90faf24 | 89 | URI += Dist; |
7db98ffc | 90 | else |
b90faf24 DK |
91 | URI += "dists/" + Dist + "/"; |
92 | return URI + Type; | |
93 | } | |
94 | std::string debReleaseIndex::MetaIndexURI(const char *Type) const | |
95 | { | |
96 | return constructMetaIndexURI(URI, Dist, Type); | |
7db98ffc | 97 | } |
463c8d80 | 98 | /*}}}*/ |
463c8d80 | 99 | // ReleaseIndex Con- and Destructors /*{{{*/ |
d03b947b DK |
100 | debReleaseIndex::debReleaseIndex(std::string const &URI, std::string const &Dist, std::map<std::string, std::string> const &Options) : |
101 | metaIndex(URI, Dist, "deb"), d(new debReleaseIndexPrivate(Options)) | |
4b42f43b | 102 | {} |
d03b947b DK |
103 | debReleaseIndex::debReleaseIndex(std::string const &URI, std::string const &Dist, bool const pTrusted, std::map<std::string, std::string> const &Options) : |
104 | metaIndex(URI, Dist, "deb"), d(new debReleaseIndexPrivate(Options)) | |
5ad0096a DK |
105 | { |
106 | Trusted = pTrusted ? TRI_YES : TRI_NO; | |
107 | } | |
5dd4c8b8 | 108 | debReleaseIndex::~debReleaseIndex() { |
463c8d80 DK |
109 | if (d != NULL) |
110 | delete d; | |
7a9f09bd | 111 | } |
463c8d80 DK |
112 | /*}}}*/ |
113 | // ReleaseIndex::GetIndexTargets /*{{{*/ | |
114 | static void GetIndexTargetsFor(char const * const Type, std::string const &URI, std::string const &Dist, | |
115 | std::vector<debReleaseIndexPrivate::debSectionEntry> const &entries, | |
d03b947b | 116 | std::vector<IndexTarget> &IndexTargets, std::map<std::string, std::string> const &ReleaseOptions) |
1e0f0f28 | 117 | { |
1e0f0f28 | 118 | bool const flatArchive = (Dist[Dist.length() - 1] == '/'); |
d03b947b | 119 | std::string const baseURI = constructMetaIndexURI(URI, Dist, ""); |
1e0f0f28 | 120 | std::string const Release = (Dist == "/") ? "" : Dist; |
1da3b7b8 | 121 | std::string const Site = ::URI::ArchiveOnly(URI); |
463c8d80 | 122 | |
d7a51997 DK |
123 | std::string DefCompressionTypes; |
124 | { | |
125 | std::vector<std::string> types = APT::Configuration::getCompressionTypes(); | |
126 | if (types.empty() == false) | |
127 | { | |
128 | std::ostringstream os; | |
129 | std::copy(types.begin(), types.end()-1, std::ostream_iterator<std::string>(os, " ")); | |
130 | os << *types.rbegin(); | |
131 | DefCompressionTypes = os.str(); | |
132 | } | |
133 | } | |
0179cfa8 DK |
134 | std::string DefKeepCompressedAs; |
135 | { | |
136 | std::vector<APT::Configuration::Compressor> comps = APT::Configuration::getCompressors(); | |
137 | if (comps.empty() == false) | |
138 | { | |
139 | std::sort(comps.begin(), comps.end(), | |
140 | [](APT::Configuration::Compressor const &a, APT::Configuration::Compressor const &b) { return a.Cost < b.Cost; }); | |
141 | std::ostringstream os; | |
142 | for (auto const &c : comps) | |
143 | if (c.Cost != 0) | |
144 | os << c.Extension.substr(1) << ' '; | |
145 | DefKeepCompressedAs = os.str(); | |
146 | } | |
147 | DefKeepCompressedAs += "uncompressed"; | |
148 | } | |
7c1dca14 | 149 | |
e8e5d464 | 150 | std::vector<std::string> const NativeArchs = { _config->Find("APT::Architecture"), "implicit:all" }; |
653ef26c | 151 | bool const GzipIndex = _config->FindB("Acquire::GzipIndexes", false); |
463c8d80 | 152 | for (std::vector<debReleaseIndexPrivate::debSectionEntry>::const_iterator E = entries.begin(); E != entries.end(); ++E) |
1e0f0f28 | 153 | { |
463c8d80 | 154 | for (std::vector<std::string>::const_iterator T = E->Targets.begin(); T != E->Targets.end(); ++T) |
1e0f0f28 | 155 | { |
d7a51997 DK |
156 | #define APT_T_CONFIG_STR(X, Y) _config->Find(std::string("Acquire::IndexTargets::") + Type + "::" + *T + "::" + (X), (Y)) |
157 | #define APT_T_CONFIG_BOOL(X, Y) _config->FindB(std::string("Acquire::IndexTargets::") + Type + "::" + *T + "::" + (X), (Y)) | |
158 | std::string const tplMetaKey = APT_T_CONFIG_STR(flatArchive ? "flatMetaKey" : "MetaKey", ""); | |
159 | std::string const tplShortDesc = APT_T_CONFIG_STR("ShortDescription", ""); | |
160 | std::string const tplLongDesc = "$(SITE) " + APT_T_CONFIG_STR(flatArchive ? "flatDescription" : "Description", ""); | |
39c724b4 | 161 | std::string const tplIdentifier = APT_T_CONFIG_STR("Identifier", *T); |
d7a51997 DK |
162 | bool const IsOptional = APT_T_CONFIG_BOOL("Optional", true); |
163 | bool const KeepCompressed = APT_T_CONFIG_BOOL("KeepCompressed", GzipIndex); | |
9adb9778 | 164 | bool const DefaultEnabled = APT_T_CONFIG_BOOL("DefaultEnabled", true); |
d7a51997 | 165 | bool const UsePDiffs = APT_T_CONFIG_BOOL("PDiffs", E->UsePDiffs); |
24e8f24e | 166 | std::string const UseByHash = APT_T_CONFIG_STR("By-Hash", E->UseByHash); |
d7a51997 | 167 | std::string const CompressionTypes = APT_T_CONFIG_STR("CompressionTypes", DefCompressionTypes); |
0179cfa8 | 168 | std::string KeepCompressedAs = APT_T_CONFIG_STR("KeepCompressedAs", ""); |
7f2d1eef | 169 | std::string const FallbackOf = APT_T_CONFIG_STR("Fallback-Of", ""); |
d7a51997 DK |
170 | #undef APT_T_CONFIG_BOOL |
171 | #undef APT_T_CONFIG_STR | |
463c8d80 | 172 | if (tplMetaKey.empty()) |
1e0f0f28 DK |
173 | continue; |
174 | ||
0179cfa8 DK |
175 | if (KeepCompressedAs.empty()) |
176 | KeepCompressedAs = DefKeepCompressedAs; | |
177 | else | |
178 | { | |
179 | std::vector<std::string> const defKeep = VectorizeString(DefKeepCompressedAs, ' '); | |
180 | std::vector<std::string> const valKeep = VectorizeString(KeepCompressedAs, ' '); | |
181 | std::vector<std::string> keep; | |
182 | for (auto const &val : valKeep) | |
183 | { | |
184 | if (val.empty()) | |
185 | continue; | |
186 | if (std::find(defKeep.begin(), defKeep.end(), val) == defKeep.end()) | |
187 | continue; | |
188 | keep.push_back(val); | |
189 | } | |
190 | if (std::find(keep.begin(), keep.end(), "uncompressed") == keep.end()) | |
191 | keep.push_back("uncompressed"); | |
192 | std::ostringstream os; | |
193 | std::copy(keep.begin(), keep.end()-1, std::ostream_iterator<std::string>(os, " ")); | |
194 | os << *keep.rbegin(); | |
195 | KeepCompressedAs = os.str(); | |
196 | } | |
197 | ||
463c8d80 | 198 | for (std::vector<std::string>::const_iterator L = E->Languages.begin(); L != E->Languages.end(); ++L) |
1e0f0f28 | 199 | { |
463c8d80 DK |
200 | if (*L == "none" && tplMetaKey.find("$(LANGUAGE)") != std::string::npos) |
201 | continue; | |
202 | ||
203 | for (std::vector<std::string>::const_iterator A = E->Architectures.begin(); A != E->Architectures.end(); ++A) | |
1e0f0f28 | 204 | { |
7c1dca14 | 205 | for (auto const &NativeArch: NativeArchs) |
463c8d80 | 206 | { |
7c1dca14 DK |
207 | constexpr static auto BreakPoint = "$(NATIVE_ARCHITECTURE)"; |
208 | // available in templates | |
209 | std::map<std::string, std::string> Options; | |
210 | Options.insert(std::make_pair("SITE", Site)); | |
211 | Options.insert(std::make_pair("RELEASE", Release)); | |
212 | if (tplMetaKey.find("$(COMPONENT)") != std::string::npos) | |
e8e5d464 | 213 | Options.emplace("COMPONENT", E->Name); |
7c1dca14 | 214 | if (tplMetaKey.find("$(LANGUAGE)") != std::string::npos) |
e8e5d464 | 215 | Options.emplace("LANGUAGE", *L); |
7c1dca14 | 216 | if (tplMetaKey.find("$(ARCHITECTURE)") != std::string::npos) |
e8e5d464 | 217 | Options.emplace("ARCHITECTURE", (*A == "implicit:all") ? "all" : *A); |
7c1dca14 | 218 | else if (tplMetaKey.find("$(NATIVE_ARCHITECTURE)") != std::string::npos) |
e8e5d464 | 219 | Options.emplace("ARCHITECTURE", (NativeArch == "implicit:all") ? "all" : NativeArch); |
7c1dca14 | 220 | if (tplMetaKey.find("$(NATIVE_ARCHITECTURE)") != std::string::npos) |
e8e5d464 | 221 | Options.emplace("NATIVE_ARCHITECTURE", (NativeArch == "implicit:all") ? "all" : NativeArch); |
d7a51997 | 222 | |
7c1dca14 DK |
223 | std::string MetaKey = tplMetaKey; |
224 | std::string ShortDesc = tplShortDesc; | |
225 | std::string LongDesc = tplLongDesc; | |
39c724b4 | 226 | std::string Identifier = tplIdentifier; |
7c1dca14 | 227 | for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O) |
3090ae69 | 228 | { |
39c724b4 DK |
229 | std::string const varname = "$(" + O->first + ")"; |
230 | MetaKey = SubstVar(MetaKey, varname, O->second); | |
231 | ShortDesc = SubstVar(ShortDesc, varname, O->second); | |
232 | LongDesc = SubstVar(LongDesc, varname, O->second); | |
233 | Identifier = SubstVar(Identifier, varname, O->second); | |
3090ae69 | 234 | } |
3090ae69 | 235 | |
3090ae69 | 236 | { |
7c1dca14 DK |
237 | auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &IT) { |
238 | return MetaKey == IT.MetaKey && baseURI == IT.Option(IndexTarget::BASE_URI) && | |
239 | E->sourcesEntry == IT.Option(IndexTarget::SOURCESENTRY) && *T == IT.Option(IndexTarget::CREATED_BY); | |
240 | }); | |
241 | if (dup != IndexTargets.end()) | |
242 | { | |
243 | if (tplMetaKey.find(BreakPoint) == std::string::npos) | |
244 | break; | |
245 | continue; | |
246 | } | |
3090ae69 | 247 | } |
3090ae69 | 248 | |
3090ae69 | 249 | { |
7c1dca14 DK |
250 | auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &IT) { |
251 | return MetaKey == IT.MetaKey && baseURI == IT.Option(IndexTarget::BASE_URI) && | |
252 | E->sourcesEntry == IT.Option(IndexTarget::SOURCESENTRY) && *T != IT.Option(IndexTarget::CREATED_BY); | |
253 | }); | |
254 | if (dup != IndexTargets.end()) | |
255 | { | |
256 | std::string const dupT = dup->Option(IndexTarget::CREATED_BY); | |
257 | std::string const dupEntry = dup->Option(IndexTarget::SOURCESENTRY); | |
258 | //TRANSLATOR: an identifier like Packages; Releasefile key indicating | |
259 | // a file like main/binary-amd64/Packages; another identifier like Contents; | |
260 | // filename and linenumber of the sources.list entry currently parsed | |
261 | _error->Warning(_("Target %s wants to acquire the same file (%s) as %s from source %s"), | |
262 | T->c_str(), MetaKey.c_str(), dupT.c_str(), dupEntry.c_str()); | |
263 | if (tplMetaKey.find(BreakPoint) == std::string::npos) | |
264 | break; | |
265 | continue; | |
266 | } | |
3090ae69 | 267 | } |
3090ae69 | 268 | |
7c1dca14 DK |
269 | { |
270 | auto const dup = std::find_if(IndexTargets.begin(), IndexTargets.end(), [&](IndexTarget const &T) { | |
271 | return MetaKey == T.MetaKey && baseURI == T.Option(IndexTarget::BASE_URI) && | |
272 | E->sourcesEntry != T.Option(IndexTarget::SOURCESENTRY); | |
273 | }); | |
274 | if (dup != IndexTargets.end()) | |
275 | { | |
276 | std::string const dupEntry = dup->Option(IndexTarget::SOURCESENTRY); | |
277 | //TRANSLATOR: an identifier like Packages; Releasefile key indicating | |
278 | // a file like main/binary-amd64/Packages; filename and linenumber of | |
279 | // two sources.list entries | |
280 | _error->Warning(_("Target %s (%s) is configured multiple times in %s and %s"), | |
281 | T->c_str(), MetaKey.c_str(), dupEntry.c_str(), E->sourcesEntry.c_str()); | |
282 | if (tplMetaKey.find(BreakPoint) == std::string::npos) | |
283 | break; | |
284 | continue; | |
285 | } | |
286 | } | |
d7a51997 | 287 | |
7c1dca14 | 288 | // not available in templates, but in the indextarget |
d03b947b | 289 | Options.insert(ReleaseOptions.begin(), ReleaseOptions.end()); |
39c724b4 | 290 | Options.insert(std::make_pair("IDENTIFIER", Identifier)); |
7c1dca14 DK |
291 | Options.insert(std::make_pair("TARGET_OF", Type)); |
292 | Options.insert(std::make_pair("CREATED_BY", *T)); | |
7f2d1eef | 293 | Options.insert(std::make_pair("FALLBACK_OF", FallbackOf)); |
7c1dca14 DK |
294 | Options.insert(std::make_pair("PDIFFS", UsePDiffs ? "yes" : "no")); |
295 | Options.insert(std::make_pair("BY_HASH", UseByHash)); | |
296 | Options.insert(std::make_pair("DEFAULTENABLED", DefaultEnabled ? "yes" : "no")); | |
297 | Options.insert(std::make_pair("COMPRESSIONTYPES", CompressionTypes)); | |
298 | Options.insert(std::make_pair("KEEPCOMPRESSEDAS", KeepCompressedAs)); | |
299 | Options.insert(std::make_pair("SOURCESENTRY", E->sourcesEntry)); | |
1dd20368 | 300 | |
7c1dca14 | 301 | bool IsOpt = IsOptional; |
7c1dca14 DK |
302 | { |
303 | auto const arch = Options.find("ARCHITECTURE"); | |
304 | if (arch != Options.end() && arch->second == "all") | |
e8e5d464 DK |
305 | { |
306 | // one of them must be implicit:all then | |
307 | if (*A != "all" && NativeArch != "all") | |
308 | IsOpt = true; | |
309 | else // user used arch=all explicitly | |
310 | Options.emplace("Force-Support-For-All", "yes"); | |
311 | } | |
7c1dca14 DK |
312 | } |
313 | ||
314 | IndexTarget Target( | |
315 | MetaKey, | |
316 | ShortDesc, | |
317 | LongDesc, | |
d03b947b | 318 | baseURI + MetaKey, |
7c1dca14 DK |
319 | IsOpt, |
320 | KeepCompressed, | |
321 | Options | |
322 | ); | |
323 | IndexTargets.push_back(Target); | |
324 | ||
325 | if (tplMetaKey.find(BreakPoint) == std::string::npos) | |
326 | break; | |
327 | } | |
463c8d80 DK |
328 | |
329 | if (tplMetaKey.find("$(ARCHITECTURE)") == std::string::npos) | |
1e0f0f28 | 330 | break; |
d3a869e3 | 331 | |
1e0f0f28 DK |
332 | } |
333 | ||
463c8d80 | 334 | if (tplMetaKey.find("$(LANGUAGE)") == std::string::npos) |
1e0f0f28 | 335 | break; |
463c8d80 | 336 | |
1e0f0f28 DK |
337 | } |
338 | ||
1e0f0f28 DK |
339 | } |
340 | } | |
59148d96 | 341 | } |
261727f0 | 342 | std::vector<IndexTarget> debReleaseIndex::GetIndexTargets() const |
59148d96 | 343 | { |
463c8d80 | 344 | std::vector<IndexTarget> IndexTargets; |
d03b947b DK |
345 | GetIndexTargetsFor("deb-src", URI, Dist, d->DebSrcEntries, IndexTargets, d->ReleaseOptions); |
346 | GetIndexTargetsFor("deb", URI, Dist, d->DebEntries, IndexTargets, d->ReleaseOptions); | |
463c8d80 | 347 | return IndexTargets; |
7db98ffc | 348 | } |
463c8d80 | 349 | /*}}}*/ |
3090ae69 DK |
350 | void debReleaseIndex::AddComponent(std::string const &sourcesEntry, /*{{{*/ |
351 | bool const isSrc, std::string const &Name, | |
463c8d80 DK |
352 | std::vector<std::string> const &Targets, |
353 | std::vector<std::string> const &Architectures, | |
1a3a14ac | 354 | std::vector<std::string> Languages, |
24e8f24e | 355 | bool const usePDiffs, std::string const &useByHash) |
463c8d80 DK |
356 | { |
357 | if (Languages.empty() == true) | |
358 | Languages.push_back("none"); | |
359 | debReleaseIndexPrivate::debSectionEntry const entry = { | |
24e8f24e | 360 | sourcesEntry, Name, Targets, Architectures, Languages, usePDiffs, useByHash |
463c8d80 DK |
361 | }; |
362 | if (isSrc) | |
363 | d->DebSrcEntries.push_back(entry); | |
364 | else | |
365 | d->DebEntries.push_back(entry); | |
366 | } | |
367 | /*}}}*/ | |
59148d96 | 368 | |
5ad0096a DK |
369 | bool debReleaseIndex::Load(std::string const &Filename, std::string * const ErrorText)/*{{{*/ |
370 | { | |
371 | LoadedSuccessfully = TRI_NO; | |
372 | FileFd Fd; | |
373 | if (OpenMaybeClearSignedFile(Filename, Fd) == false) | |
374 | return false; | |
375 | ||
376 | pkgTagFile TagFile(&Fd, Fd.Size()); | |
95278287 | 377 | if (Fd.IsOpen() == false || Fd.Failed()) |
5ad0096a DK |
378 | { |
379 | if (ErrorText != NULL) | |
380 | strprintf(*ErrorText, _("Unable to parse Release file %s"),Filename.c_str()); | |
381 | return false; | |
382 | } | |
383 | ||
384 | pkgTagSection Section; | |
385 | const char *Start, *End; | |
386 | if (TagFile.Step(Section) == false) | |
387 | { | |
388 | if (ErrorText != NULL) | |
389 | strprintf(*ErrorText, _("No sections in Release file %s"), Filename.c_str()); | |
390 | return false; | |
391 | } | |
392 | // FIXME: find better tag name | |
393 | SupportsAcquireByHash = Section.FindB("Acquire-By-Hash", false); | |
394 | ||
395 | Suite = Section.FindS("Suite"); | |
396 | Codename = Section.FindS("Codename"); | |
1dd20368 DK |
397 | { |
398 | std::string const archs = Section.FindS("Architectures"); | |
399 | if (archs.empty() == false) | |
400 | d->Architectures = VectorizeString(archs, ' '); | |
401 | } | |
a628ca52 DK |
402 | { |
403 | std::string const targets = Section.FindS("No-Support-for-Architecture-all"); | |
404 | if (targets.empty() == false) | |
405 | d->NoSupportForAll = VectorizeString(targets, ' '); | |
406 | } | |
5ad0096a DK |
407 | |
408 | bool FoundHashSum = false; | |
bd4a8f51 DK |
409 | bool FoundStrongHashSum = false; |
410 | auto const SupportedHashes = HashString::SupportedHashes(); | |
411 | for (int i=0; SupportedHashes[i] != NULL; i++) | |
5ad0096a | 412 | { |
bd4a8f51 | 413 | if (!Section.Find(SupportedHashes[i], Start, End)) |
5ad0096a DK |
414 | continue; |
415 | ||
416 | std::string Name; | |
417 | std::string Hash; | |
418 | unsigned long long Size; | |
419 | while (Start < End) | |
420 | { | |
421 | if (!parseSumData(Start, End, Name, Hash, Size)) | |
422 | return false; | |
423 | ||
bd4a8f51 | 424 | HashString const hs(SupportedHashes[i], Hash); |
5ad0096a DK |
425 | if (Entries.find(Name) == Entries.end()) |
426 | { | |
427 | metaIndex::checkSum *Sum = new metaIndex::checkSum; | |
428 | Sum->MetaKeyFilename = Name; | |
429 | Sum->Size = Size; | |
430 | Sum->Hashes.FileSize(Size); | |
bd4a8f51 | 431 | APT_IGNORE_DEPRECATED(Sum->Hash = hs;) |
5ad0096a DK |
432 | Entries[Name] = Sum; |
433 | } | |
bd4a8f51 | 434 | Entries[Name]->Hashes.push_back(hs); |
5ad0096a | 435 | FoundHashSum = true; |
bd4a8f51 DK |
436 | if (FoundStrongHashSum == false && hs.usable() == true) |
437 | FoundStrongHashSum = true; | |
5ad0096a DK |
438 | } |
439 | } | |
440 | ||
ab94dcec | 441 | bool AuthPossible = false; |
5ad0096a | 442 | if(FoundHashSum == false) |
ab94dcec DK |
443 | _error->Warning(_("No Hash entry in Release file %s"), Filename.c_str()); |
444 | else if(FoundStrongHashSum == false) | |
445 | _error->Warning(_("No Hash entry in Release file %s which is considered strong enough for security purposes"), Filename.c_str()); | |
446 | else | |
447 | AuthPossible = true; | |
5ad0096a DK |
448 | |
449 | std::string const StrDate = Section.FindS("Date"); | |
450 | if (RFC1123StrToTime(StrDate.c_str(), Date) == false) | |
451 | { | |
89901946 | 452 | _error->Warning( _("Invalid '%s' entry in Release file %s"), "Date", Filename.c_str()); |
6fc2e030 | 453 | Date = 0; |
5ad0096a DK |
454 | } |
455 | ||
0741daeb DK |
456 | bool CheckValidUntil = _config->FindB("Acquire::Check-Valid-Until", true); |
457 | if (d->CheckValidUntil == metaIndex::TRI_NO) | |
458 | CheckValidUntil = false; | |
459 | else if (d->CheckValidUntil == metaIndex::TRI_YES) | |
460 | CheckValidUntil = true; | |
5ad0096a | 461 | |
0741daeb | 462 | if (CheckValidUntil == true) |
5ad0096a | 463 | { |
0741daeb DK |
464 | std::string const Label = Section.FindS("Label"); |
465 | std::string const StrValidUntil = Section.FindS("Valid-Until"); | |
466 | ||
467 | // if we have a Valid-Until header in the Release file, use it as default | |
468 | if (StrValidUntil.empty() == false) | |
5ad0096a | 469 | { |
0741daeb DK |
470 | if(RFC1123StrToTime(StrValidUntil.c_str(), ValidUntil) == false) |
471 | { | |
472 | if (ErrorText != NULL) | |
89901946 | 473 | strprintf(*ErrorText, _("Invalid '%s' entry in Release file %s"), "Valid-Until", Filename.c_str()); |
0741daeb DK |
474 | return false; |
475 | } | |
476 | } | |
477 | // get the user settings for this archive and use what expires earlier | |
478 | time_t MaxAge = d->ValidUntilMax; | |
479 | if (MaxAge == 0) | |
480 | { | |
481 | MaxAge = _config->FindI("Acquire::Max-ValidTime", 0); | |
482 | if (Label.empty() == false) | |
483 | MaxAge = _config->FindI(("Acquire::Max-ValidTime::" + Label).c_str(), MaxAge); | |
484 | } | |
485 | time_t MinAge = d->ValidUntilMin; | |
486 | if (MinAge == 0) | |
487 | { | |
488 | MinAge = _config->FindI("Acquire::Min-ValidTime", 0); | |
489 | if (Label.empty() == false) | |
490 | MinAge = _config->FindI(("Acquire::Min-ValidTime::" + Label).c_str(), MinAge); | |
5ad0096a | 491 | } |
5ad0096a | 492 | |
6fc2e030 DK |
493 | if (MinAge != 0 || ValidUntil != 0 || MaxAge != 0) |
494 | { | |
495 | if (MinAge != 0 && ValidUntil != 0) { | |
496 | time_t const min_date = Date + MinAge; | |
497 | if (ValidUntil < min_date) | |
498 | ValidUntil = min_date; | |
499 | } | |
500 | if (MaxAge != 0 && Date != 0) { | |
501 | time_t const max_date = Date + MaxAge; | |
502 | if (ValidUntil == 0 || ValidUntil > max_date) | |
503 | ValidUntil = max_date; | |
504 | } | |
0741daeb | 505 | } |
5ad0096a | 506 | } |
59148d96 | 507 | |
89901946 DK |
508 | /* as the Release file is parsed only after it was verified, the Signed-By field |
509 | does not effect the current, but the "next" Release file */ | |
510 | auto Sign = Section.FindS("Signed-By"); | |
511 | if (Sign.empty() == false) | |
512 | { | |
513 | std::transform(Sign.begin(), Sign.end(), Sign.begin(), [&](char const c) { | |
514 | return (isspace(c) == 0) ? c : ','; | |
515 | }); | |
516 | auto fingers = VectorizeString(Sign, ','); | |
517 | std::transform(fingers.begin(), fingers.end(), fingers.begin(), [&](std::string finger) { | |
518 | std::transform(finger.begin(), finger.end(), finger.begin(), ::toupper); | |
519 | if (finger.length() != 40 || finger.find_first_not_of("0123456789ABCDEF") != std::string::npos) | |
520 | { | |
521 | if (ErrorText != NULL) | |
522 | strprintf(*ErrorText, _("Invalid '%s' entry in Release file %s"), "Signed-By", Filename.c_str()); | |
523 | return std::string(); | |
524 | } | |
525 | return finger; | |
526 | }); | |
527 | if (fingers.empty() == false && std::find(fingers.begin(), fingers.end(), "") == fingers.end()) | |
528 | { | |
529 | std::stringstream os; | |
530 | std::copy(fingers.begin(), fingers.end(), std::ostream_iterator<std::string>(os, ",")); | |
531 | SignedBy = os.str(); | |
532 | } | |
533 | } | |
534 | ||
ab94dcec DK |
535 | if (AuthPossible) |
536 | LoadedSuccessfully = TRI_YES; | |
537 | return AuthPossible; | |
5ad0096a DK |
538 | } |
539 | /*}}}*/ | |
540 | metaIndex * debReleaseIndex::UnloadedClone() const /*{{{*/ | |
541 | { | |
542 | if (Trusted == TRI_NO) | |
d03b947b | 543 | return new debReleaseIndex(URI, Dist, false, d->ReleaseOptions); |
5ad0096a | 544 | else if (Trusted == TRI_YES) |
d03b947b | 545 | return new debReleaseIndex(URI, Dist, true, d->ReleaseOptions); |
5ad0096a | 546 | else |
d03b947b | 547 | return new debReleaseIndex(URI, Dist, d->ReleaseOptions); |
5ad0096a DK |
548 | } |
549 | /*}}}*/ | |
550 | bool debReleaseIndex::parseSumData(const char *&Start, const char *End, /*{{{*/ | |
551 | std::string &Name, std::string &Hash, unsigned long long &Size) | |
7db98ffc | 552 | { |
5ad0096a DK |
553 | Name = ""; |
554 | Hash = ""; | |
555 | Size = 0; | |
556 | /* Skip over the first blank */ | |
557 | while ((*Start == '\t' || *Start == ' ' || *Start == '\n' || *Start == '\r') | |
558 | && Start < End) | |
559 | Start++; | |
560 | if (Start >= End) | |
561 | return false; | |
07cb47e7 | 562 | |
5ad0096a DK |
563 | /* Move EntryEnd to the end of the first entry (the hash) */ |
564 | const char *EntryEnd = Start; | |
565 | while ((*EntryEnd != '\t' && *EntryEnd != ' ') | |
566 | && EntryEnd < End) | |
567 | EntryEnd++; | |
568 | if (EntryEnd == End) | |
569 | return false; | |
570 | ||
571 | Hash.append(Start, EntryEnd-Start); | |
572 | ||
573 | /* Skip over intermediate blanks */ | |
574 | Start = EntryEnd; | |
575 | while (*Start == '\t' || *Start == ' ') | |
576 | Start++; | |
577 | if (Start >= End) | |
578 | return false; | |
579 | ||
580 | EntryEnd = Start; | |
581 | /* Find the end of the second entry (the size) */ | |
582 | while ((*EntryEnd != '\t' && *EntryEnd != ' ' ) | |
583 | && EntryEnd < End) | |
584 | EntryEnd++; | |
585 | if (EntryEnd == End) | |
586 | return false; | |
587 | ||
588 | Size = strtoull (Start, NULL, 10); | |
589 | ||
590 | /* Skip over intermediate blanks */ | |
591 | Start = EntryEnd; | |
592 | while (*Start == '\t' || *Start == ' ') | |
593 | Start++; | |
594 | if (Start >= End) | |
595 | return false; | |
596 | ||
597 | EntryEnd = Start; | |
598 | /* Find the end of the third entry (the filename) */ | |
599 | while ((*EntryEnd != '\t' && *EntryEnd != ' ' && | |
600 | *EntryEnd != '\n' && *EntryEnd != '\r') | |
601 | && EntryEnd < End) | |
602 | EntryEnd++; | |
603 | ||
604 | Name.append(Start, EntryEnd-Start); | |
605 | Start = EntryEnd; //prepare for the next round | |
606 | return true; | |
607 | } | |
608 | /*}}}*/ | |
609 | ||
610 | bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool const &GetAll)/*{{{*/ | |
611 | { | |
d03b947b | 612 | #define APT_TARGET(X) IndexTarget("", X, MetaIndexInfo(X), MetaIndexURI(X), false, false, d->ReleaseOptions) |
3d8232bf | 613 | pkgAcqMetaClearSig * const TransactionManager = new pkgAcqMetaClearSig(Owner, |
a8f565d3 | 614 | APT_TARGET("InRelease"), APT_TARGET("Release"), APT_TARGET("Release.gpg"), this); |
448c38bd | 615 | #undef APT_TARGET |
5ad0096a | 616 | // special case for --print-uris |
448c38bd | 617 | if (GetAll) |
a8f565d3 | 618 | for (auto const &Target: GetIndexTargets()) |
7f2d1eef DK |
619 | if (Target.Option(IndexTarget::FALLBACK_OF).empty()) |
620 | new pkgAcqIndex(Owner, TransactionManager, Target); | |
fe0f7911 | 621 | |
55971004 | 622 | return true; |
7db98ffc | 623 | } |
463c8d80 | 624 | /*}}}*/ |
0741daeb | 625 | // ReleaseIndex::Set* TriState options /*{{{*/ |
5ad0096a | 626 | bool debReleaseIndex::SetTrusted(TriState const pTrusted) |
4b42f43b | 627 | { |
5ad0096a DK |
628 | if (Trusted == TRI_UNSET) |
629 | Trusted = pTrusted; | |
630 | else if (Trusted != pTrusted) | |
268ffceb | 631 | // TRANSLATOR: The first is an option name from sources.list manpage, the other two URI and Suite |
d04e44ac | 632 | return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Trusted", URI.c_str(), Dist.c_str()); |
268ffceb | 633 | return true; |
4b42f43b | 634 | } |
0741daeb DK |
635 | bool debReleaseIndex::SetCheckValidUntil(TriState const pCheckValidUntil) |
636 | { | |
637 | if (d->CheckValidUntil == TRI_UNSET) | |
638 | d->CheckValidUntil = pCheckValidUntil; | |
639 | else if (d->CheckValidUntil != pCheckValidUntil) | |
d04e44ac | 640 | return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Check-Valid-Until", URI.c_str(), Dist.c_str()); |
0741daeb DK |
641 | return true; |
642 | } | |
643 | bool debReleaseIndex::SetValidUntilMin(time_t const Valid) | |
644 | { | |
645 | if (d->ValidUntilMin == 0) | |
646 | d->ValidUntilMin = Valid; | |
647 | else if (d->ValidUntilMin != Valid) | |
d04e44ac | 648 | return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Min-ValidTime", URI.c_str(), Dist.c_str()); |
0741daeb DK |
649 | return true; |
650 | } | |
651 | bool debReleaseIndex::SetValidUntilMax(time_t const Valid) | |
652 | { | |
653 | if (d->ValidUntilMax == 0) | |
654 | d->ValidUntilMax = Valid; | |
655 | else if (d->ValidUntilMax != Valid) | |
d04e44ac | 656 | return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), "Max-ValidTime", URI.c_str(), Dist.c_str()); |
0741daeb | 657 | return true; |
b0d40854 DK |
658 | } |
659 | bool debReleaseIndex::SetSignedBy(std::string const &pSignedBy) | |
660 | { | |
661 | if (SignedBy.empty() == true && pSignedBy.empty() == false) | |
662 | { | |
663 | if (pSignedBy[0] == '/') // no check for existence as we could be chrooting later or such things | |
46e00c90 | 664 | SignedBy = pSignedBy; // absolute path to a keyring file |
b0d40854 DK |
665 | else |
666 | { | |
667 | // we could go all fancy and allow short/long/string matches as gpgv/apt-key does, | |
668 | // but fingerprints are harder to fake than the others and this option is set once, | |
669 | // not interactively all the time so easy to type is not really a concern. | |
46e00c90 DK |
670 | auto fingers = VectorizeString(pSignedBy, ','); |
671 | std::transform(fingers.begin(), fingers.end(), fingers.begin(), [&](std::string finger) { | |
672 | std::transform(finger.begin(), finger.end(), finger.begin(), ::toupper); | |
673 | if (finger.length() != 40 || finger.find_first_not_of("0123456789ABCDEF") != std::string::npos) | |
674 | { | |
675 | _error->Error(_("Invalid value set for option %s regarding source %s %s (%s)"), "Signed-By", URI.c_str(), Dist.c_str(), "not a fingerprint"); | |
676 | return std::string(); | |
677 | } | |
678 | return finger; | |
679 | }); | |
680 | std::stringstream os; | |
681 | std::copy(fingers.begin(), fingers.end(), std::ostream_iterator<std::string>(os, ",")); | |
682 | SignedBy = os.str(); | |
b0d40854 | 683 | } |
71203dbf JAK |
684 | // Normalize the string: Remove trailing commas |
685 | while (SignedBy[SignedBy.size() - 1] == ',') | |
686 | SignedBy.resize(SignedBy.size() - 1); | |
687 | } | |
688 | else { | |
689 | // Only compare normalized strings | |
690 | auto pSignedByView = APT::StringView(pSignedBy); | |
691 | while (pSignedByView[pSignedByView.size() - 1] == ',') | |
692 | pSignedByView = pSignedByView.substr(0, pSignedByView.size() - 1); | |
693 | if (pSignedByView != SignedBy) | |
694 | return _error->Error(_("Conflicting values set for option %s regarding source %s %s: %s != %s"), "Signed-By", URI.c_str(), Dist.c_str(), SignedBy.c_str(), pSignedByView.to_string().c_str()); | |
b0d40854 | 695 | } |
b0d40854 | 696 | return true; |
0741daeb DK |
697 | } |
698 | /*}}}*/ | |
699 | // ReleaseIndex::IsTrusted /*{{{*/ | |
7db98ffc MZ |
700 | bool debReleaseIndex::IsTrusted() const |
701 | { | |
5ad0096a | 702 | if (Trusted == TRI_YES) |
4b42f43b | 703 | return true; |
5ad0096a | 704 | else if (Trusted == TRI_NO) |
4b42f43b DK |
705 | return false; |
706 | ||
707 | ||
4e0ad446 | 708 | if(_config->FindB("APT::Authentication::TrustCDROM", false)) |
e8cdc56a MV |
709 | if(URI.substr(0,strlen("cdrom:")) == "cdrom:") |
710 | return true; | |
fe0f7911 | 711 | |
463c8d80 | 712 | if (FileExists(MetaIndexFile("Release.gpg"))) |
7db98ffc | 713 | return true; |
fe0f7911 | 714 | |
463c8d80 | 715 | return FileExists(MetaIndexFile("InRelease")); |
7db98ffc | 716 | } |
463c8d80 | 717 | /*}}}*/ |
1dd20368 DK |
718 | bool debReleaseIndex::IsArchitectureSupported(std::string const &arch) const/*{{{*/ |
719 | { | |
720 | if (d->Architectures.empty()) | |
721 | return true; | |
722 | return std::find(d->Architectures.begin(), d->Architectures.end(), arch) != d->Architectures.end(); | |
723 | } | |
724 | /*}}}*/ | |
a628ca52 DK |
725 | bool debReleaseIndex::IsArchitectureAllSupportedFor(IndexTarget const &target) const/*{{{*/ |
726 | { | |
e8e5d464 DK |
727 | if (target.Options.find("Force-Support-For-All") != target.Options.end()) |
728 | return true; | |
729 | if (IsArchitectureSupported("all") == false) | |
730 | return false; | |
a628ca52 DK |
731 | if (d->NoSupportForAll.empty()) |
732 | return true; | |
733 | return std::find(d->NoSupportForAll.begin(), d->NoSupportForAll.end(), target.Option(IndexTarget::CREATED_BY)) == d->NoSupportForAll.end(); | |
734 | } | |
735 | /*}}}*/ | |
463c8d80 | 736 | std::vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles() /*{{{*/ |
59148d96 | 737 | { |
e3c1cfc7 DK |
738 | if (Indexes != NULL) |
739 | return Indexes; | |
59148d96 | 740 | |
e3c1cfc7 | 741 | Indexes = new std::vector<pkgIndexFile*>(); |
e3c1cfc7 | 742 | bool const istrusted = IsTrusted(); |
8dd562a8 | 743 | for (auto const &T: GetIndexTargets()) |
59148d96 | 744 | { |
8dd562a8 | 745 | std::string const TargetName = T.Option(IndexTarget::CREATED_BY); |
59148d96 | 746 | if (TargetName == "Packages") |
8dd562a8 | 747 | Indexes->push_back(new debPackagesIndex(T, istrusted)); |
59148d96 | 748 | else if (TargetName == "Sources") |
8dd562a8 | 749 | Indexes->push_back(new debSourcesIndex(T, istrusted)); |
59148d96 | 750 | else if (TargetName == "Translations") |
8dd562a8 | 751 | Indexes->push_back(new debTranslationsIndex(T)); |
59148d96 | 752 | } |
e3c1cfc7 | 753 | return Indexes; |
5dd4c8b8 | 754 | } |
463c8d80 | 755 | /*}}}*/ |
d03b947b DK |
756 | std::map<std::string, std::string> debReleaseIndex::GetReleaseOptions() |
757 | { | |
758 | return d->ReleaseOptions; | |
759 | } | |
a7a5b0d9 | 760 | |
463c8d80 | 761 | static bool ReleaseFileName(debReleaseIndex const * const That, std::string &ReleaseFile)/*{{{*/ |
b07aeb1a DK |
762 | { |
763 | ReleaseFile = That->MetaIndexFile("InRelease"); | |
764 | bool releaseExists = false; | |
765 | if (FileExists(ReleaseFile) == true) | |
766 | releaseExists = true; | |
767 | else | |
768 | { | |
769 | ReleaseFile = That->MetaIndexFile("Release"); | |
770 | if (FileExists(ReleaseFile)) | |
771 | releaseExists = true; | |
772 | } | |
773 | return releaseExists; | |
774 | } | |
463c8d80 | 775 | /*}}}*/ |
b07aeb1a DK |
776 | bool debReleaseIndex::Merge(pkgCacheGenerator &Gen,OpProgress * /*Prog*/) const/*{{{*/ |
777 | { | |
778 | std::string ReleaseFile; | |
779 | bool const releaseExists = ReleaseFileName(this, ReleaseFile); | |
780 | ||
781 | ::URI Tmp(URI); | |
782 | if (Gen.SelectReleaseFile(ReleaseFile, Tmp.Host) == false) | |
783 | return _error->Error("Problem with SelectReleaseFile %s", ReleaseFile.c_str()); | |
784 | ||
785 | if (releaseExists == false) | |
786 | return true; | |
787 | ||
788 | FileFd Rel; | |
789 | // Beware: The 'Release' file might be clearsigned in case the | |
790 | // signature for an 'InRelease' file couldn't be checked | |
791 | if (OpenMaybeClearSignedFile(ReleaseFile, Rel) == false) | |
792 | return false; | |
b07aeb1a DK |
793 | |
794 | // Store the IMS information | |
795 | pkgCache::RlsFileIterator File = Gen.GetCurRlsFile(); | |
796 | pkgCacheGenerator::Dynamic<pkgCache::RlsFileIterator> DynFile(File); | |
797 | // Rel can't be used as this is potentially a temporary file | |
798 | struct stat Buf; | |
799 | if (stat(ReleaseFile.c_str(), &Buf) != 0) | |
800 | return _error->Errno("fstat", "Unable to stat file %s", ReleaseFile.c_str()); | |
801 | File->Size = Buf.st_size; | |
802 | File->mtime = Buf.st_mtime; | |
803 | ||
804 | pkgTagFile TagFile(&Rel, Rel.Size()); | |
805 | pkgTagSection Section; | |
95278287 | 806 | if (Rel.IsOpen() == false || Rel.Failed() || TagFile.Step(Section) == false) |
b07aeb1a DK |
807 | return false; |
808 | ||
809 | std::string data; | |
810 | #define APT_INRELEASE(TYPE, TAG, STORE) \ | |
811 | data = Section.FindS(TAG); \ | |
812 | if (data.empty() == false) \ | |
813 | { \ | |
814 | map_stringitem_t const storage = Gen.StoreString(pkgCacheGenerator::TYPE, data); \ | |
95278287 | 815 | if (storage == 0) return false; \ |
b07aeb1a DK |
816 | STORE = storage; \ |
817 | } | |
818 | APT_INRELEASE(MIXED, "Suite", File->Archive) | |
819 | APT_INRELEASE(VERSIONNUMBER, "Version", File->Version) | |
820 | APT_INRELEASE(MIXED, "Origin", File->Origin) | |
821 | APT_INRELEASE(MIXED, "Codename", File->Codename) | |
822 | APT_INRELEASE(MIXED, "Label", File->Label) | |
823 | #undef APT_INRELEASE | |
824 | Section.FindFlag("NotAutomatic", File->Flags, pkgCache::Flag::NotAutomatic); | |
825 | Section.FindFlag("ButAutomaticUpgrades", File->Flags, pkgCache::Flag::ButAutomaticUpgrades); | |
826 | ||
95278287 | 827 | return true; |
b07aeb1a DK |
828 | } |
829 | /*}}}*/ | |
830 | // ReleaseIndex::FindInCache - Find this index /*{{{*/ | |
3fd89e62 | 831 | pkgCache::RlsFileIterator debReleaseIndex::FindInCache(pkgCache &Cache, bool const ModifyCheck) const |
b07aeb1a DK |
832 | { |
833 | std::string ReleaseFile; | |
834 | bool const releaseExists = ReleaseFileName(this, ReleaseFile); | |
835 | ||
836 | pkgCache::RlsFileIterator File = Cache.RlsFileBegin(); | |
837 | for (; File.end() == false; ++File) | |
838 | { | |
839 | if (File->FileName == 0 || ReleaseFile != File.FileName()) | |
840 | continue; | |
841 | ||
842 | // empty means the file does not exist by "design" | |
3fd89e62 | 843 | if (ModifyCheck == false || (releaseExists == false && File->Size == 0)) |
b07aeb1a DK |
844 | return File; |
845 | ||
846 | struct stat St; | |
847 | if (stat(File.FileName(),&St) != 0) | |
848 | { | |
849 | if (_config->FindB("Debug::pkgCacheGen", false)) | |
850 | std::clog << "ReleaseIndex::FindInCache - stat failed on " << File.FileName() << std::endl; | |
851 | return pkgCache::RlsFileIterator(Cache); | |
852 | } | |
853 | if ((unsigned)St.st_size != File->Size || St.st_mtime != File->mtime) | |
854 | { | |
855 | if (_config->FindB("Debug::pkgCacheGen", false)) | |
856 | std::clog << "ReleaseIndex::FindInCache - size (" << St.st_size << " <> " << File->Size | |
857 | << ") or mtime (" << St.st_mtime << " <> " << File->mtime | |
858 | << ") doesn't match for " << File.FileName() << std::endl; | |
859 | return pkgCache::RlsFileIterator(Cache); | |
860 | } | |
861 | return File; | |
862 | } | |
863 | ||
864 | return File; | |
865 | } | |
866 | /*}}}*/ | |
867 | ||
36d5299a | 868 | class APT_HIDDEN debSLTypeDebian : public pkgSourceList::Type /*{{{*/ |
7db98ffc | 869 | { |
36d5299a DK |
870 | static std::vector<std::string> getDefaultSetOf(std::string const &Name, |
871 | std::map<std::string, std::string> const &Options, std::vector<std::string> const &defaultValues) | |
7db98ffc | 872 | { |
36d5299a DK |
873 | auto const val = Options.find(Name); |
874 | if (val != Options.end()) | |
875 | return VectorizeString(val->second, ','); | |
876 | return defaultValues; | |
463c8d80 | 877 | } |
36d5299a DK |
878 | static std::vector<std::string> applyPlusMinusOptions(std::string const &Name, |
879 | std::map<std::string, std::string> const &Options, std::vector<std::string> &&Values) | |
463c8d80 | 880 | { |
36d5299a DK |
881 | auto val = Options.find(Name + "+"); |
882 | if (val != Options.end()) | |
883 | { | |
884 | std::vector<std::string> const plus = VectorizeString(val->second, ','); | |
885 | std::copy_if(plus.begin(), plus.end(), std::back_inserter(Values), [&Values](std::string const &v) { | |
886 | return std::find(Values.begin(), Values.end(), v) == Values.end(); | |
887 | }); | |
888 | } | |
889 | if ((val = Options.find(Name + "-")) != Options.end()) | |
890 | { | |
891 | std::vector<std::string> const minus = VectorizeString(val->second, ','); | |
892 | Values.erase(std::remove_if(Values.begin(), Values.end(), [&minus](std::string const &v) { | |
893 | return std::find(minus.begin(), minus.end(), v) != minus.end(); | |
894 | }), Values.end()); | |
895 | } | |
896 | return Values; | |
463c8d80 | 897 | } |
36d5299a DK |
898 | static std::vector<std::string> parsePlusMinusOptions(std::string const &Name, |
899 | std::map<std::string, std::string> const &Options, std::vector<std::string> const &defaultValues) | |
900 | { | |
901 | return applyPlusMinusOptions(Name, Options, getDefaultSetOf(Name, Options, defaultValues)); | |
902 | } | |
903 | static std::vector<std::string> parsePlusMinusArchOptions(std::string const &Name, | |
904 | std::map<std::string, std::string> const &Options) | |
905 | { | |
906 | auto Values = getDefaultSetOf(Name, Options, APT::Configuration::getArchitectures()); | |
907 | // all is a very special architecture users shouldn't be concerned with explicitly | |
e8e5d464 DK |
908 | // but if the user does, do not override the choice |
909 | auto const val = Options.find(Name + "-"); | |
910 | if (val != Options.end()) | |
911 | { | |
912 | std::vector<std::string> const minus = VectorizeString(val->second, ','); | |
913 | if (std::find(minus.begin(), minus.end(), "all") != minus.end()) | |
914 | return applyPlusMinusOptions(Name, Options, std::move(Values)); | |
915 | } | |
916 | Values = applyPlusMinusOptions(Name, Options, std::move(Values)); | |
917 | if (std::find(Values.begin(), Values.end(), "all") == Values.end()) | |
918 | Values.push_back("implicit:all"); | |
919 | return Values; | |
36d5299a DK |
920 | } |
921 | static std::vector<std::string> parsePlusMinusTargetOptions(char const * const Name, | |
922 | std::map<std::string, std::string> const &Options) | |
923 | { | |
924 | std::vector<std::string> const alltargets = _config->FindVector(std::string("Acquire::IndexTargets::") + Name, "", true); | |
925 | std::vector<std::string> deftargets; | |
926 | deftargets.reserve(alltargets.size()); | |
927 | std::copy_if(alltargets.begin(), alltargets.end(), std::back_inserter(deftargets), [&](std::string const &t) { | |
928 | std::string c = "Acquire::IndexTargets::"; | |
929 | c.append(Name).append("::").append(t).append("::DefaultEnabled"); | |
930 | return _config->FindB(c, true); | |
931 | }); | |
932 | std::vector<std::string> mytargets = parsePlusMinusOptions("target", Options, deftargets); | |
933 | for (auto const &target : alltargets) | |
934 | { | |
935 | std::map<std::string, std::string>::const_iterator const opt = Options.find(target); | |
936 | if (opt == Options.end()) | |
937 | continue; | |
938 | auto const idMatch = [&](std::string const &t) { | |
939 | return target == _config->Find(std::string("Acquire::IndexTargets::") + Name + "::" + t + "::Identifier", t); | |
940 | }; | |
941 | if (StringToBool(opt->second)) | |
942 | std::copy_if(alltargets.begin(), alltargets.end(), std::back_inserter(mytargets), idMatch); | |
943 | else | |
944 | mytargets.erase(std::remove_if(mytargets.begin(), mytargets.end(), idMatch), mytargets.end()); | |
945 | } | |
946 | // if we can't order it in a 1000 steps we give up… probably a cycle | |
947 | for (auto i = 0; i < 1000; ++i) | |
948 | { | |
949 | bool Changed = false; | |
950 | for (auto t = mytargets.begin(); t != mytargets.end(); ++t) | |
951 | { | |
952 | std::string const fallback = _config->Find(std::string("Acquire::IndexTargets::") + Name + "::" + *t + "::Fallback-Of"); | |
953 | if (fallback.empty()) | |
954 | continue; | |
955 | auto const faller = std::find(mytargets.begin(), mytargets.end(), fallback); | |
956 | if (faller == mytargets.end() || faller < t) | |
957 | continue; | |
958 | Changed = true; | |
959 | auto const tv = *t; | |
960 | mytargets.erase(t); | |
961 | mytargets.emplace_back(tv); | |
962 | } | |
963 | if (Changed == false) | |
964 | break; | |
965 | } | |
966 | // remove duplicates without changing the order (in first appearance) | |
967 | { | |
968 | std::set<std::string> seenOnce; | |
969 | mytargets.erase(std::remove_if(mytargets.begin(), mytargets.end(), [&](std::string const &t) { | |
970 | return seenOnce.insert(t).second == false; | |
971 | }), mytargets.end()); | |
972 | } | |
973 | return mytargets; | |
974 | } | |
975 | ||
0741daeb DK |
976 | metaIndex::TriState GetTriStateOption(std::map<std::string, std::string>const &Options, char const * const name) const |
977 | { | |
978 | std::map<std::string, std::string>::const_iterator const opt = Options.find(name); | |
979 | if (opt != Options.end()) | |
980 | return StringToBool(opt->second, false) ? metaIndex::TRI_YES : metaIndex::TRI_NO; | |
981 | return metaIndex::TRI_DONTCARE; | |
982 | } | |
983 | ||
d03b947b | 984 | static time_t GetTimeOption(std::map<std::string, std::string>const &Options, char const * const name) |
0741daeb DK |
985 | { |
986 | std::map<std::string, std::string>::const_iterator const opt = Options.find(name); | |
987 | if (opt == Options.end()) | |
988 | return 0; | |
989 | return strtoull(opt->second.c_str(), NULL, 10); | |
990 | } | |
991 | ||
d03b947b DK |
992 | static bool GetBoolOption(std::map<std::string, std::string> const &Options, char const * const name, bool const defVal) |
993 | { | |
994 | std::map<std::string, std::string>::const_iterator const opt = Options.find(name); | |
995 | if (opt == Options.end()) | |
996 | return defVal; | |
997 | return StringToBool(opt->second, defVal); | |
998 | } | |
999 | ||
1000 | static std::vector<std::string> GetMapKeys(std::map<std::string, std::string> const &Options) | |
1001 | { | |
1002 | std::vector<std::string> ret; | |
1003 | ret.reserve(Options.size()); | |
1004 | for (auto &&O: Options) | |
1005 | ret.emplace_back(O.first); | |
1006 | std::sort(ret.begin(), ret.end()); | |
1007 | return ret; | |
1008 | } | |
1009 | ||
1010 | static bool MapsAreEqual(std::map<std::string, std::string> const &OptionsA, | |
1011 | std::map<std::string, std::string> const &OptionsB, | |
1012 | std::string const &URI, std::string const &Dist) | |
1013 | { | |
1014 | auto const KeysA = GetMapKeys(OptionsA); | |
1015 | auto const KeysB = GetMapKeys(OptionsB); | |
1016 | auto const m = std::mismatch(KeysA.begin(), KeysA.end(), KeysB.begin()); | |
1017 | if (m.first != KeysA.end()) | |
1018 | { | |
1019 | if (std::find(KeysB.begin(), KeysB.end(), *m.first) == KeysB.end()) | |
1020 | return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), m.first->c_str(), "<set>", "<unset>"); | |
1021 | else | |
1022 | return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), m.second->c_str(), "<set>", "<unset>"); | |
1023 | } | |
1024 | if (m.second != KeysB.end()) | |
1025 | { | |
1026 | if (std::find(KeysA.begin(), KeysA.end(), *m.second) == KeysA.end()) | |
1027 | return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), m.first->c_str(), "<set>", "<unset>"); | |
1028 | else | |
1029 | return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), m.second->c_str(), "<set>", "<unset>"); | |
1030 | } | |
1031 | for (auto&& key: KeysA) | |
1032 | { | |
1033 | if (key == "BASE_URI" || key == "REPO_URI") | |
1034 | continue; | |
1035 | auto const a = OptionsA.find(key); | |
1036 | auto const b = OptionsB.find(key); | |
1037 | if (unlikely(a == OptionsA.end() || b == OptionsB.end()) || a->second != b->second) | |
1038 | return _error->Error(_("Conflicting values set for option %s regarding source %s %s"), key.c_str(), URI.c_str(), Dist.c_str()); | |
1039 | } | |
1040 | return true; | |
1041 | } | |
1042 | ||
36d5299a DK |
1043 | static debReleaseIndex * GetDebReleaseIndexBy(std::vector<metaIndex *> &List, std::string const &URI, |
1044 | std::string const &Dist, std::map<std::string, std::string> const &Options) | |
463c8d80 | 1045 | { |
d03b947b DK |
1046 | std::map<std::string,std::string> ReleaseOptions = {{ |
1047 | { "BASE_URI", constructMetaIndexURI(URI, Dist, "") }, | |
1048 | { "REPO_URI", URI }, | |
1049 | }}; | |
1050 | if (GetBoolOption(Options, "allow-insecure", _config->FindB("Acquire::AllowInsecureRepositories"))) | |
1051 | ReleaseOptions.emplace("ALLOW_INSECURE", "true"); | |
1052 | if (GetBoolOption(Options, "allow-weak", _config->FindB("Acquire::AllowWeakRepositories"))) | |
1053 | ReleaseOptions.emplace("ALLOW_WEAK", "true"); | |
1054 | if (GetBoolOption(Options, "allow-downgrade-to-insecure", _config->FindB("Acquire::AllowDowngradeToInsecureRepositories"))) | |
1055 | ReleaseOptions.emplace("ALLOW_DOWNGRADE_TO_INSECURE", "true"); | |
1056 | ||
b90faf24 DK |
1057 | debReleaseIndex * Deb = nullptr; |
1058 | std::string const FileName = URItoFileName(constructMetaIndexURI(URI, Dist, "Release")); | |
1059 | for (auto const &I: List) | |
7db98ffc | 1060 | { |
5dd4c8b8 | 1061 | // We only worry about debian entries here |
b90faf24 | 1062 | if (strcmp(I->GetType(), "deb") != 0) |
5dd4c8b8 DK |
1063 | continue; |
1064 | ||
b90faf24 DK |
1065 | auto const D = dynamic_cast<debReleaseIndex*>(I); |
1066 | if (unlikely(D == nullptr)) | |
1067 | continue; | |
1068 | ||
1069 | /* This check ensures that there will be only one Release file | |
5dd4c8b8 DK |
1070 | queued for all the Packages files and Sources files it |
1071 | corresponds to. */ | |
b90faf24 | 1072 | if (URItoFileName(D->MetaIndexURI("Release")) == FileName) |
7db98ffc | 1073 | { |
d03b947b | 1074 | if (MapsAreEqual(ReleaseOptions, D->GetReleaseOptions(), URI, Dist) == false) |
36d5299a | 1075 | return nullptr; |
b90faf24 DK |
1076 | Deb = D; |
1077 | break; | |
7db98ffc MZ |
1078 | } |
1079 | } | |
4b42f43b | 1080 | |
7db98ffc | 1081 | // No currently created Release file indexes this entry, so we create a new one. |
b90faf24 | 1082 | if (Deb == nullptr) |
261727f0 | 1083 | { |
d03b947b | 1084 | Deb = new debReleaseIndex(URI, Dist, ReleaseOptions); |
261727f0 DK |
1085 | List.push_back(Deb); |
1086 | } | |
36d5299a DK |
1087 | return Deb; |
1088 | } | |
4b42f43b | 1089 | |
36d5299a DK |
1090 | protected: |
1091 | ||
1092 | bool CreateItemInternal(std::vector<metaIndex *> &List, std::string const &URI, | |
1093 | std::string const &Dist, std::string const &Section, | |
1094 | bool const &IsSrc, std::map<std::string, std::string> const &Options) const | |
1095 | { | |
1096 | auto const Deb = GetDebReleaseIndexBy(List, URI, Dist, Options); | |
8dd562a8 | 1097 | |
d03b947b | 1098 | bool const UsePDiffs = GetBoolOption(Options, "pdiffs", _config->FindB("Acquire::PDiffs", true)); |
8dd562a8 | 1099 | |
24e8f24e DK |
1100 | std::string UseByHash = _config->Find("APT::Acquire::By-Hash", "yes"); |
1101 | UseByHash = _config->Find("Acquire::By-Hash", UseByHash); | |
1102 | { | |
1103 | std::string const host = ::URI(URI).Host; | |
1104 | UseByHash = _config->Find("APT::Acquire::" + host + "::By-Hash", UseByHash); | |
1105 | UseByHash = _config->Find("Acquire::" + host + "::By-Hash", UseByHash); | |
1106 | std::map<std::string, std::string>::const_iterator const opt = Options.find("by-hash"); | |
1107 | if (opt != Options.end()) | |
1108 | UseByHash = opt->second; | |
1109 | } | |
1110 | ||
3090ae69 | 1111 | auto const entry = Options.find("sourceslist-entry"); |
463c8d80 | 1112 | Deb->AddComponent( |
3090ae69 | 1113 | entry->second, |
463c8d80 DK |
1114 | IsSrc, |
1115 | Section, | |
36d5299a DK |
1116 | parsePlusMinusTargetOptions(Name, Options), |
1117 | parsePlusMinusArchOptions("arch", Options), | |
1a3a14ac | 1118 | parsePlusMinusOptions("lang", Options, APT::Configuration::getLanguages(true)), |
24e8f24e DK |
1119 | UsePDiffs, |
1120 | UseByHash | |
463c8d80 | 1121 | ); |
261727f0 | 1122 | |
0741daeb DK |
1123 | if (Deb->SetTrusted(GetTriStateOption(Options, "trusted")) == false || |
1124 | Deb->SetCheckValidUntil(GetTriStateOption(Options, "check-valid-until")) == false || | |
1125 | Deb->SetValidUntilMax(GetTimeOption(Options, "valid-until-max")) == false || | |
1126 | Deb->SetValidUntilMin(GetTimeOption(Options, "valid-until-min")) == false) | |
268ffceb | 1127 | return false; |
261727f0 | 1128 | |
b0d40854 DK |
1129 | std::map<std::string, std::string>::const_iterator const signedby = Options.find("signed-by"); |
1130 | if (signedby == Options.end()) | |
1131 | { | |
89901946 DK |
1132 | bool alreadySet = false; |
1133 | std::string filename; | |
1134 | if (ReleaseFileName(Deb, filename)) | |
1135 | { | |
1136 | auto OldDeb = Deb->UnloadedClone(); | |
1137 | _error->PushToStack(); | |
1138 | OldDeb->Load(filename, nullptr); | |
1139 | bool const goodLoad = _error->PendingError() == false; | |
1140 | _error->RevertToStack(); | |
1141 | if (goodLoad) | |
1142 | { | |
1143 | if (OldDeb->GetValidUntil() > 0) | |
1144 | { | |
1145 | time_t const invalid_since = time(NULL) - OldDeb->GetValidUntil(); | |
1146 | if (invalid_since <= 0) | |
1147 | { | |
1148 | Deb->SetSignedBy(OldDeb->GetSignedBy()); | |
1149 | alreadySet = true; | |
1150 | } | |
1151 | } | |
1152 | } | |
1153 | delete OldDeb; | |
1154 | } | |
1155 | if (alreadySet == false && Deb->SetSignedBy("") == false) | |
b0d40854 DK |
1156 | return false; |
1157 | } | |
1158 | else | |
1159 | { | |
1160 | if (Deb->SetSignedBy(signedby->second) == false) | |
1161 | return false; | |
1162 | } | |
1163 | ||
7db98ffc MZ |
1164 | return true; |
1165 | } | |
0d29b9d4 | 1166 | |
463c8d80 DK |
1167 | debSLTypeDebian(char const * const Name, char const * const Label) : Type(Name, Label) |
1168 | { | |
1169 | } | |
1170 | }; | |
1171 | /*}}}*/ | |
1172 | class APT_HIDDEN debSLTypeDeb : public debSLTypeDebian /*{{{*/ | |
7db98ffc MZ |
1173 | { |
1174 | public: | |
1175 | ||
463c8d80 DK |
1176 | bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI, |
1177 | std::string const &Dist, std::string const &Section, | |
3b302846 | 1178 | std::map<std::string, std::string> const &Options) const APT_OVERRIDE |
7db98ffc | 1179 | { |
5dd4c8b8 | 1180 | return CreateItemInternal(List, URI, Dist, Section, false, Options); |
7db98ffc MZ |
1181 | } |
1182 | ||
463c8d80 | 1183 | debSLTypeDeb() : debSLTypeDebian("deb", "Debian binary tree") |
7db98ffc | 1184 | { |
463c8d80 | 1185 | } |
7db98ffc | 1186 | }; |
463c8d80 DK |
1187 | /*}}}*/ |
1188 | class APT_HIDDEN debSLTypeDebSrc : public debSLTypeDebian /*{{{*/ | |
7db98ffc MZ |
1189 | { |
1190 | public: | |
1191 | ||
463c8d80 DK |
1192 | bool CreateItem(std::vector<metaIndex *> &List, std::string const &URI, |
1193 | std::string const &Dist, std::string const &Section, | |
3b302846 | 1194 | std::map<std::string, std::string> const &Options) const APT_OVERRIDE |
7db98ffc | 1195 | { |
5dd4c8b8 | 1196 | return CreateItemInternal(List, URI, Dist, Section, true, Options); |
7db98ffc | 1197 | } |
463c8d80 DK |
1198 | |
1199 | debSLTypeDebSrc() : debSLTypeDebian("deb-src", "Debian source tree") | |
7db98ffc | 1200 | { |
463c8d80 | 1201 | } |
7db98ffc | 1202 | }; |
463c8d80 | 1203 | /*}}}*/ |
7db98ffc | 1204 | |
dce45dbe DK |
1205 | APT_HIDDEN debSLTypeDeb _apt_DebType; |
1206 | APT_HIDDEN debSLTypeDebSrc _apt_DebSrcType; |