]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: acquire-item.cc,v 1.46.2.9 2004/01/16 18:51:11 mdz Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | Acquire Item - Item to acquire | |
7 | ||
8 | Each item can download to exactly one file at a time. This means you | |
9 | cannot create an item that fetches two uri's to two files at the same | |
10 | time. The pkgAcqIndex class creates a second class upon instantiation | |
11 | to fetch the other index files because of this. | |
12 | ||
13 | ##################################################################### */ | |
14 | /*}}}*/ | |
15 | // Include Files /*{{{*/ | |
16 | #include <config.h> | |
17 | ||
18 | #include <apt-pkg/acquire-item.h> | |
19 | #include <apt-pkg/configuration.h> | |
20 | #include <apt-pkg/aptconfiguration.h> | |
21 | #include <apt-pkg/sourcelist.h> | |
22 | #include <apt-pkg/error.h> | |
23 | #include <apt-pkg/strutl.h> | |
24 | #include <apt-pkg/fileutl.h> | |
25 | #include <apt-pkg/tagfile.h> | |
26 | #include <apt-pkg/metaindex.h> | |
27 | #include <apt-pkg/acquire.h> | |
28 | #include <apt-pkg/hashes.h> | |
29 | #include <apt-pkg/indexfile.h> | |
30 | #include <apt-pkg/pkgcache.h> | |
31 | #include <apt-pkg/cacheiterators.h> | |
32 | #include <apt-pkg/pkgrecords.h> | |
33 | #include <apt-pkg/gpgv.h> | |
34 | ||
35 | #include <algorithm> | |
36 | #include <stddef.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | #include <iostream> | |
40 | #include <vector> | |
41 | #include <sys/stat.h> | |
42 | #include <unistd.h> | |
43 | #include <errno.h> | |
44 | #include <string> | |
45 | #include <stdio.h> | |
46 | #include <ctime> | |
47 | #include <sstream> | |
48 | #include <numeric> | |
49 | #include <random> | |
50 | ||
51 | #include <apti18n.h> | |
52 | /*}}}*/ | |
53 | ||
54 | using namespace std; | |
55 | ||
56 | static void printHashSumComparison(std::string const &URI, HashStringList const &Expected, HashStringList const &Actual) /*{{{*/ | |
57 | { | |
58 | if (_config->FindB("Debug::Acquire::HashSumMismatch", false) == false) | |
59 | return; | |
60 | std::cerr << std::endl << URI << ":" << std::endl << " Expected Hash: " << std::endl; | |
61 | for (HashStringList::const_iterator hs = Expected.begin(); hs != Expected.end(); ++hs) | |
62 | std::cerr << "\t- " << hs->toStr() << std::endl; | |
63 | std::cerr << " Actual Hash: " << std::endl; | |
64 | for (HashStringList::const_iterator hs = Actual.begin(); hs != Actual.end(); ++hs) | |
65 | std::cerr << "\t- " << hs->toStr() << std::endl; | |
66 | } | |
67 | /*}}}*/ | |
68 | static std::string GetPartialFileName(std::string const &file) /*{{{*/ | |
69 | { | |
70 | std::string DestFile = _config->FindDir("Dir::State::lists") + "partial/"; | |
71 | DestFile += file; | |
72 | return DestFile; | |
73 | } | |
74 | /*}}}*/ | |
75 | static std::string GetPartialFileNameFromURI(std::string const &uri) /*{{{*/ | |
76 | { | |
77 | return GetPartialFileName(URItoFileName(uri)); | |
78 | } | |
79 | /*}}}*/ | |
80 | static std::string GetFinalFileNameFromURI(std::string const &uri) /*{{{*/ | |
81 | { | |
82 | return _config->FindDir("Dir::State::lists") + URItoFileName(uri); | |
83 | } | |
84 | /*}}}*/ | |
85 | static std::string GetKeepCompressedFileName(std::string file, IndexTarget const &Target)/*{{{*/ | |
86 | { | |
87 | if (Target.KeepCompressed == false) | |
88 | return file; | |
89 | ||
90 | std::string const KeepCompressedAs = Target.Option(IndexTarget::KEEPCOMPRESSEDAS); | |
91 | if (KeepCompressedAs.empty() == false) | |
92 | { | |
93 | std::string const ext = KeepCompressedAs.substr(0, KeepCompressedAs.find(' ')); | |
94 | if (ext != "uncompressed") | |
95 | file.append(".").append(ext); | |
96 | } | |
97 | return file; | |
98 | } | |
99 | /*}}}*/ | |
100 | static std::string GetMergeDiffsPatchFileName(std::string const &Final, std::string const &Patch)/*{{{*/ | |
101 | { | |
102 | // rred expects the patch as $FinalFile.ed.$patchname.gz | |
103 | return Final + ".ed." + Patch + ".gz"; | |
104 | } | |
105 | /*}}}*/ | |
106 | static std::string GetDiffsPatchFileName(std::string const &Final) /*{{{*/ | |
107 | { | |
108 | // rred expects the patch as $FinalFile.ed | |
109 | return Final + ".ed"; | |
110 | } | |
111 | /*}}}*/ | |
112 | static std::string GetExistingFilename(std::string const &File) /*{{{*/ | |
113 | { | |
114 | if (RealFileExists(File)) | |
115 | return File; | |
116 | for (auto const &type : APT::Configuration::getCompressorExtensions()) | |
117 | { | |
118 | std::string const Final = File + type; | |
119 | if (RealFileExists(Final)) | |
120 | return Final; | |
121 | } | |
122 | return ""; | |
123 | } | |
124 | /*}}}*/ | |
125 | static std::string GetDiffIndexFileName(std::string const &Name) /*{{{*/ | |
126 | { | |
127 | return Name + ".diff/Index"; | |
128 | } | |
129 | /*}}}*/ | |
130 | static std::string GetDiffIndexURI(IndexTarget const &Target) /*{{{*/ | |
131 | { | |
132 | return Target.URI + ".diff/Index"; | |
133 | } | |
134 | /*}}}*/ | |
135 | ||
136 | static void ReportMirrorFailureToCentral(pkgAcquire::Item const &I, std::string const &FailCode, std::string const &Details)/*{{{*/ | |
137 | { | |
138 | // we only act if a mirror was used at all | |
139 | if(I.UsedMirror.empty()) | |
140 | return; | |
141 | #if 0 | |
142 | std::cerr << "\nReportMirrorFailure: " | |
143 | << UsedMirror | |
144 | << " Uri: " << DescURI() | |
145 | << " FailCode: " | |
146 | << FailCode << std::endl; | |
147 | #endif | |
148 | string const report = _config->Find("Methods::Mirror::ProblemReporting", | |
149 | LIBEXEC_DIR "/apt-report-mirror-failure"); | |
150 | if(!FileExists(report)) | |
151 | return; | |
152 | ||
153 | std::vector<char const*> const Args = { | |
154 | report.c_str(), | |
155 | I.UsedMirror.c_str(), | |
156 | I.DescURI().c_str(), | |
157 | FailCode.c_str(), | |
158 | Details.c_str(), | |
159 | NULL | |
160 | }; | |
161 | ||
162 | pid_t pid = ExecFork(); | |
163 | if(pid < 0) | |
164 | { | |
165 | _error->Error("ReportMirrorFailure Fork failed"); | |
166 | return; | |
167 | } | |
168 | else if(pid == 0) | |
169 | { | |
170 | execvp(Args[0], (char**)Args.data()); | |
171 | std::cerr << "Could not exec " << Args[0] << std::endl; | |
172 | _exit(100); | |
173 | } | |
174 | if(!ExecWait(pid, "report-mirror-failure")) | |
175 | _error->Warning("Couldn't report problem to '%s'", report.c_str()); | |
176 | } | |
177 | /*}}}*/ | |
178 | ||
179 | static APT_NONNULL(2) bool MessageInsecureRepository(bool const isError, char const * const msg, std::string const &repo)/*{{{*/ | |
180 | { | |
181 | std::string m; | |
182 | strprintf(m, msg, repo.c_str()); | |
183 | if (isError) | |
184 | { | |
185 | _error->Error("%s", m.c_str()); | |
186 | _error->Notice("%s", _("Updating from such a repository can't be done securely, and is therefore disabled by default.")); | |
187 | } | |
188 | else | |
189 | { | |
190 | _error->Warning("%s", m.c_str()); | |
191 | _error->Notice("%s", _("Data from such a repository can't be authenticated and is therefore potentially dangerous to use.")); | |
192 | } | |
193 | _error->Notice("%s", _("See apt-secure(8) manpage for repository creation and user configuration details.")); | |
194 | return false; | |
195 | } | |
196 | /*}}}*/ | |
197 | // AllowInsecureRepositories /*{{{*/ | |
198 | enum class InsecureType { UNSIGNED, WEAK, NORELEASE }; | |
199 | static bool TargetIsAllowedToBe(IndexTarget const &Target, InsecureType const type) | |
200 | { | |
201 | if (_config->FindB("Acquire::AllowInsecureRepositories")) | |
202 | return true; | |
203 | ||
204 | if (Target.OptionBool(IndexTarget::ALLOW_INSECURE)) | |
205 | return true; | |
206 | ||
207 | switch (type) | |
208 | { | |
209 | case InsecureType::UNSIGNED: break; | |
210 | case InsecureType::NORELEASE: break; | |
211 | case InsecureType::WEAK: | |
212 | if (_config->FindB("Acquire::AllowWeakRepositories")) | |
213 | return true; | |
214 | if (Target.OptionBool(IndexTarget::ALLOW_WEAK)) | |
215 | return true; | |
216 | break; | |
217 | } | |
218 | return false; | |
219 | } | |
220 | static bool APT_NONNULL(3, 4, 5) AllowInsecureRepositories(InsecureType const msg, std::string const &repo, | |
221 | metaIndex const * const MetaIndexParser, pkgAcqMetaClearSig * const TransactionManager, pkgAcquire::Item * const I) | |
222 | { | |
223 | // we skip weak downgrades as its unlikely that a repository gets really weaker – | |
224 | // its more realistic that apt got pickier in a newer version | |
225 | if (msg != InsecureType::WEAK) | |
226 | { | |
227 | std::string const FinalInRelease = TransactionManager->GetFinalFilename(); | |
228 | std::string const FinalReleasegpg = FinalInRelease.substr(0, FinalInRelease.length() - strlen("InRelease")) + "Release.gpg"; | |
229 | if (RealFileExists(FinalReleasegpg) || RealFileExists(FinalInRelease)) | |
230 | { | |
231 | char const * msgstr = nullptr; | |
232 | switch (msg) | |
233 | { | |
234 | case InsecureType::UNSIGNED: msgstr = _("The repository '%s' is no longer signed."); break; | |
235 | case InsecureType::NORELEASE: msgstr = _("The repository '%s' does no longer have a Release file."); break; | |
236 | case InsecureType::WEAK: /* unreachable */ break; | |
237 | } | |
238 | if (_config->FindB("Acquire::AllowDowngradeToInsecureRepositories") || | |
239 | TransactionManager->Target.OptionBool(IndexTarget::ALLOW_DOWNGRADE_TO_INSECURE)) | |
240 | { | |
241 | // meh, the users wants to take risks (we still mark the packages | |
242 | // from this repository as unauthenticated) | |
243 | _error->Warning(msgstr, repo.c_str()); | |
244 | _error->Warning(_("This is normally not allowed, but the option " | |
245 | "Acquire::AllowDowngradeToInsecureRepositories was " | |
246 | "given to override it.")); | |
247 | } else { | |
248 | MessageInsecureRepository(true, msgstr, repo); | |
249 | TransactionManager->AbortTransaction(); | |
250 | I->Status = pkgAcquire::Item::StatError; | |
251 | return false; | |
252 | } | |
253 | } | |
254 | } | |
255 | ||
256 | if(MetaIndexParser->GetTrusted() == metaIndex::TRI_YES) | |
257 | return true; | |
258 | ||
259 | char const * msgstr = nullptr; | |
260 | switch (msg) | |
261 | { | |
262 | case InsecureType::UNSIGNED: msgstr = _("The repository '%s' is not signed."); break; | |
263 | case InsecureType::NORELEASE: msgstr = _("The repository '%s' does not have a Release file."); break; | |
264 | case InsecureType::WEAK: msgstr = _("The repository '%s' provides only weak security information."); break; | |
265 | } | |
266 | ||
267 | if (TargetIsAllowedToBe(TransactionManager->Target, msg) == true) | |
268 | { | |
269 | MessageInsecureRepository(false, msgstr, repo); | |
270 | return true; | |
271 | } | |
272 | ||
273 | MessageInsecureRepository(true, msgstr, repo); | |
274 | TransactionManager->AbortTransaction(); | |
275 | I->Status = pkgAcquire::Item::StatError; | |
276 | return false; | |
277 | } | |
278 | /*}}}*/ | |
279 | static HashStringList GetExpectedHashesFromFor(metaIndex * const Parser, std::string const &MetaKey)/*{{{*/ | |
280 | { | |
281 | if (Parser == NULL) | |
282 | return HashStringList(); | |
283 | metaIndex::checkSum * const R = Parser->Lookup(MetaKey); | |
284 | if (R == NULL) | |
285 | return HashStringList(); | |
286 | return R->Hashes; | |
287 | } | |
288 | /*}}}*/ | |
289 | ||
290 | // all ::HashesRequired and ::GetExpectedHashes implementations /*{{{*/ | |
291 | /* ::GetExpectedHashes is abstract and has to be implemented by all subclasses. | |
292 | It is best to implement it as broadly as possible, while ::HashesRequired defaults | |
293 | to true and should be as restrictive as possible for false cases. Note that if | |
294 | a hash is returned by ::GetExpectedHashes it must match. Only if it doesn't | |
295 | ::HashesRequired is called to evaluate if its okay to have no hashes. */ | |
296 | APT_CONST bool pkgAcqTransactionItem::HashesRequired() const | |
297 | { | |
298 | /* signed repositories obviously have a parser and good hashes. | |
299 | unsigned repositories, too, as even if we can't trust them for security, | |
300 | we can at least trust them for integrity of the download itself. | |
301 | Only repositories without a Release file can (obviously) not have | |
302 | hashes – and they are very uncommon and strongly discouraged */ | |
303 | if (TransactionManager->MetaIndexParser->GetLoadedSuccessfully() != metaIndex::TRI_YES) | |
304 | return false; | |
305 | if (TargetIsAllowedToBe(Target, InsecureType::WEAK)) | |
306 | { | |
307 | /* If we allow weak hashes, we check that we have some (weak) and then | |
308 | declare hashes not needed. That will tip us in the right direction | |
309 | as if hashes exist, they will be used, even if not required */ | |
310 | auto const hsl = GetExpectedHashes(); | |
311 | if (hsl.usable()) | |
312 | return true; | |
313 | if (hsl.empty() == false) | |
314 | return false; | |
315 | } | |
316 | return true; | |
317 | } | |
318 | HashStringList pkgAcqTransactionItem::GetExpectedHashes() const | |
319 | { | |
320 | return GetExpectedHashesFor(GetMetaKey()); | |
321 | } | |
322 | ||
323 | APT_CONST bool pkgAcqMetaBase::HashesRequired() const | |
324 | { | |
325 | // Release and co have no hashes 'by design'. | |
326 | return false; | |
327 | } | |
328 | HashStringList pkgAcqMetaBase::GetExpectedHashes() const | |
329 | { | |
330 | return HashStringList(); | |
331 | } | |
332 | ||
333 | APT_CONST bool pkgAcqIndexDiffs::HashesRequired() const | |
334 | { | |
335 | /* We can't check hashes of rred result as we don't know what the | |
336 | hash of the file will be. We just know the hash of the patch(es), | |
337 | the hash of the file they will apply on and the hash of the resulting | |
338 | file. */ | |
339 | if (State == StateFetchDiff) | |
340 | return true; | |
341 | return false; | |
342 | } | |
343 | HashStringList pkgAcqIndexDiffs::GetExpectedHashes() const | |
344 | { | |
345 | if (State == StateFetchDiff) | |
346 | return available_patches[0].download_hashes; | |
347 | return HashStringList(); | |
348 | } | |
349 | ||
350 | APT_CONST bool pkgAcqIndexMergeDiffs::HashesRequired() const | |
351 | { | |
352 | /* @see #pkgAcqIndexDiffs::HashesRequired, with the difference that | |
353 | we can check the rred result after all patches are applied as | |
354 | we know the expected result rather than potentially apply more patches */ | |
355 | if (State == StateFetchDiff) | |
356 | return true; | |
357 | return State == StateApplyDiff; | |
358 | } | |
359 | HashStringList pkgAcqIndexMergeDiffs::GetExpectedHashes() const | |
360 | { | |
361 | if (State == StateFetchDiff) | |
362 | return patch.download_hashes; | |
363 | else if (State == StateApplyDiff) | |
364 | return GetExpectedHashesFor(Target.MetaKey); | |
365 | return HashStringList(); | |
366 | } | |
367 | ||
368 | APT_CONST bool pkgAcqArchive::HashesRequired() const | |
369 | { | |
370 | return LocalSource == false; | |
371 | } | |
372 | HashStringList pkgAcqArchive::GetExpectedHashes() const | |
373 | { | |
374 | // figured out while parsing the records | |
375 | return ExpectedHashes; | |
376 | } | |
377 | ||
378 | APT_CONST bool pkgAcqFile::HashesRequired() const | |
379 | { | |
380 | // supplied as parameter at creation time, so the caller decides | |
381 | return ExpectedHashes.usable(); | |
382 | } | |
383 | HashStringList pkgAcqFile::GetExpectedHashes() const | |
384 | { | |
385 | return ExpectedHashes; | |
386 | } | |
387 | /*}}}*/ | |
388 | // Acquire::Item::QueueURI and specialisations from child classes /*{{{*/ | |
389 | bool pkgAcquire::Item::QueueURI(pkgAcquire::ItemDesc &Item) | |
390 | { | |
391 | Owner->Enqueue(Item); | |
392 | return true; | |
393 | } | |
394 | /* The idea here is that an item isn't queued if it exists on disk and the | |
395 | transition manager was a hit as this means that the files it contains | |
396 | the checksums for can't be updated either (or they are and we are asking | |
397 | for a hashsum mismatch to happen which helps nobody) */ | |
398 | bool pkgAcqTransactionItem::QueueURI(pkgAcquire::ItemDesc &Item) | |
399 | { | |
400 | if (TransactionManager->State != TransactionStarted) | |
401 | { | |
402 | if (_config->FindB("Debug::Acquire::Transaction", false)) | |
403 | std::clog << "Skip " << Target.URI << " as transaction was already dealt with!" << std::endl; | |
404 | return false; | |
405 | } | |
406 | std::string const FinalFile = GetFinalFilename(); | |
407 | if (TransactionManager->IMSHit == true && FileExists(FinalFile) == true) | |
408 | { | |
409 | PartialFile = DestFile = FinalFile; | |
410 | Status = StatDone; | |
411 | return false; | |
412 | } | |
413 | // If we got the InRelease file via a mirror, pick all indexes directly from this mirror, too | |
414 | if (TransactionManager->BaseURI.empty() == false && UsedMirror.empty() && | |
415 | URI::SiteOnly(Item.URI) != URI::SiteOnly(TransactionManager->BaseURI)) | |
416 | { | |
417 | // this ensures we rewrite only once and only the first step | |
418 | auto const OldBaseURI = Target.Option(IndexTarget::BASE_URI); | |
419 | if (OldBaseURI.empty() == false && APT::String::Startswith(Item.URI, OldBaseURI)) | |
420 | { | |
421 | auto const ExtraPath = Item.URI.substr(OldBaseURI.length()); | |
422 | Item.URI = flCombine(TransactionManager->BaseURI, ExtraPath); | |
423 | UsedMirror = TransactionManager->UsedMirror; | |
424 | if (Item.Description.find(" ") != string::npos) | |
425 | Item.Description.replace(0, Item.Description.find(" "), UsedMirror); | |
426 | } | |
427 | } | |
428 | return pkgAcquire::Item::QueueURI(Item); | |
429 | } | |
430 | /* The transition manager InRelease itself (or its older sisters-in-law | |
431 | Release & Release.gpg) is always queued as this allows us to rerun gpgv | |
432 | on it to verify that we aren't stalled with old files */ | |
433 | bool pkgAcqMetaBase::QueueURI(pkgAcquire::ItemDesc &Item) | |
434 | { | |
435 | return pkgAcquire::Item::QueueURI(Item); | |
436 | } | |
437 | /* the Diff/Index needs to queue also the up-to-date complete index file | |
438 | to ensure that the list cleaner isn't eating it */ | |
439 | bool pkgAcqDiffIndex::QueueURI(pkgAcquire::ItemDesc &Item) | |
440 | { | |
441 | if (pkgAcqTransactionItem::QueueURI(Item) == true) | |
442 | return true; | |
443 | QueueOnIMSHit(); | |
444 | return false; | |
445 | } | |
446 | /*}}}*/ | |
447 | // Acquire::Item::GetFinalFilename and specialisations for child classes /*{{{*/ | |
448 | std::string pkgAcquire::Item::GetFinalFilename() const | |
449 | { | |
450 | // Beware: Desc.URI is modified by redirections | |
451 | return GetFinalFileNameFromURI(Desc.URI); | |
452 | } | |
453 | std::string pkgAcqDiffIndex::GetFinalFilename() const | |
454 | { | |
455 | std::string const FinalFile = GetFinalFileNameFromURI(GetDiffIndexURI(Target)); | |
456 | // we don't want recompress, so lets keep whatever we got | |
457 | if (CurrentCompressionExtension == "uncompressed") | |
458 | return FinalFile; | |
459 | return FinalFile + "." + CurrentCompressionExtension; | |
460 | } | |
461 | std::string pkgAcqIndex::GetFinalFilename() const | |
462 | { | |
463 | std::string const FinalFile = GetFinalFileNameFromURI(Target.URI); | |
464 | return GetKeepCompressedFileName(FinalFile, Target); | |
465 | } | |
466 | std::string pkgAcqMetaSig::GetFinalFilename() const | |
467 | { | |
468 | return GetFinalFileNameFromURI(Target.URI); | |
469 | } | |
470 | std::string pkgAcqBaseIndex::GetFinalFilename() const | |
471 | { | |
472 | return GetFinalFileNameFromURI(Target.URI); | |
473 | } | |
474 | std::string pkgAcqMetaBase::GetFinalFilename() const | |
475 | { | |
476 | return GetFinalFileNameFromURI(Target.URI); | |
477 | } | |
478 | std::string pkgAcqArchive::GetFinalFilename() const | |
479 | { | |
480 | return _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename); | |
481 | } | |
482 | /*}}}*/ | |
483 | // pkgAcqTransactionItem::GetMetaKey and specialisations for child classes /*{{{*/ | |
484 | std::string pkgAcqTransactionItem::GetMetaKey() const | |
485 | { | |
486 | return Target.MetaKey; | |
487 | } | |
488 | std::string pkgAcqIndex::GetMetaKey() const | |
489 | { | |
490 | if (Stage == STAGE_DECOMPRESS_AND_VERIFY || CurrentCompressionExtension == "uncompressed") | |
491 | return Target.MetaKey; | |
492 | return Target.MetaKey + "." + CurrentCompressionExtension; | |
493 | } | |
494 | std::string pkgAcqDiffIndex::GetMetaKey() const | |
495 | { | |
496 | auto const metakey = GetDiffIndexFileName(Target.MetaKey); | |
497 | if (CurrentCompressionExtension == "uncompressed") | |
498 | return metakey; | |
499 | return metakey + "." + CurrentCompressionExtension; | |
500 | } | |
501 | /*}}}*/ | |
502 | //pkgAcqTransactionItem::TransactionState and specialisations for child classes /*{{{*/ | |
503 | bool pkgAcqTransactionItem::TransactionState(TransactionStates const state) | |
504 | { | |
505 | bool const Debug = _config->FindB("Debug::Acquire::Transaction", false); | |
506 | switch(state) | |
507 | { | |
508 | case TransactionStarted: _error->Fatal("Item %s changed to invalid transaction start state!", Target.URI.c_str()); break; | |
509 | case TransactionAbort: | |
510 | if(Debug == true) | |
511 | std::clog << " Cancel: " << DestFile << std::endl; | |
512 | if (Status == pkgAcquire::Item::StatIdle) | |
513 | { | |
514 | Status = pkgAcquire::Item::StatDone; | |
515 | Dequeue(); | |
516 | } | |
517 | break; | |
518 | case TransactionCommit: | |
519 | if(PartialFile.empty() == false) | |
520 | { | |
521 | bool sameFile = (PartialFile == DestFile); | |
522 | // we use symlinks on IMS-Hit to avoid copies | |
523 | if (RealFileExists(DestFile)) | |
524 | { | |
525 | struct stat Buf; | |
526 | if (lstat(PartialFile.c_str(), &Buf) != -1) | |
527 | { | |
528 | if (S_ISLNK(Buf.st_mode) && Buf.st_size > 0) | |
529 | { | |
530 | char partial[Buf.st_size + 1]; | |
531 | ssize_t const sp = readlink(PartialFile.c_str(), partial, Buf.st_size); | |
532 | if (sp == -1) | |
533 | _error->Errno("pkgAcqTransactionItem::TransactionState-sp", _("Failed to readlink %s"), PartialFile.c_str()); | |
534 | else | |
535 | { | |
536 | partial[sp] = '\0'; | |
537 | sameFile = (DestFile == partial); | |
538 | } | |
539 | } | |
540 | } | |
541 | else | |
542 | _error->Errno("pkgAcqTransactionItem::TransactionState-stat", _("Failed to stat %s"), PartialFile.c_str()); | |
543 | } | |
544 | if (sameFile == false) | |
545 | { | |
546 | // ensure that even without lists-cleanup all compressions are nuked | |
547 | std::string FinalFile = GetFinalFileNameFromURI(Target.URI); | |
548 | if (FileExists(FinalFile)) | |
549 | { | |
550 | if(Debug == true) | |
551 | std::clog << "rm " << FinalFile << " # " << DescURI() << std::endl; | |
552 | if (RemoveFile("TransactionStates-Cleanup", FinalFile) == false) | |
553 | return false; | |
554 | } | |
555 | for (auto const &ext: APT::Configuration::getCompressorExtensions()) | |
556 | { | |
557 | auto const Final = FinalFile + ext; | |
558 | if (FileExists(Final)) | |
559 | { | |
560 | if(Debug == true) | |
561 | std::clog << "rm " << Final << " # " << DescURI() << std::endl; | |
562 | if (RemoveFile("TransactionStates-Cleanup", Final) == false) | |
563 | return false; | |
564 | } | |
565 | } | |
566 | if(Debug == true) | |
567 | std::clog << "mv " << PartialFile << " -> "<< DestFile << " # " << DescURI() << std::endl; | |
568 | if (Rename(PartialFile, DestFile) == false) | |
569 | return false; | |
570 | } | |
571 | else if(Debug == true) | |
572 | std::clog << "keep " << PartialFile << " # " << DescURI() << std::endl; | |
573 | ||
574 | } else { | |
575 | if(Debug == true) | |
576 | std::clog << "rm " << DestFile << " # " << DescURI() << std::endl; | |
577 | if (RemoveFile("TransItem::TransactionCommit", DestFile) == false) | |
578 | return false; | |
579 | } | |
580 | break; | |
581 | } | |
582 | return true; | |
583 | } | |
584 | bool pkgAcqMetaBase::TransactionState(TransactionStates const state) | |
585 | { | |
586 | // Do not remove InRelease on IMSHit of Release.gpg [yes, this is very edgecasey] | |
587 | if (TransactionManager->IMSHit == false) | |
588 | return pkgAcqTransactionItem::TransactionState(state); | |
589 | return true; | |
590 | } | |
591 | bool pkgAcqIndex::TransactionState(TransactionStates const state) | |
592 | { | |
593 | if (pkgAcqTransactionItem::TransactionState(state) == false) | |
594 | return false; | |
595 | ||
596 | switch (state) | |
597 | { | |
598 | case TransactionStarted: _error->Fatal("AcqIndex %s changed to invalid transaction start state!", Target.URI.c_str()); break; | |
599 | case TransactionAbort: | |
600 | if (Stage == STAGE_DECOMPRESS_AND_VERIFY) | |
601 | { | |
602 | // keep the compressed file, but drop the decompressed | |
603 | EraseFileName.clear(); | |
604 | if (PartialFile.empty() == false && flExtension(PartialFile) != CurrentCompressionExtension) | |
605 | RemoveFile("TransactionAbort", PartialFile); | |
606 | } | |
607 | break; | |
608 | case TransactionCommit: | |
609 | if (EraseFileName.empty() == false) | |
610 | RemoveFile("AcqIndex::TransactionCommit", EraseFileName); | |
611 | break; | |
612 | } | |
613 | return true; | |
614 | } | |
615 | bool pkgAcqDiffIndex::TransactionState(TransactionStates const state) | |
616 | { | |
617 | if (pkgAcqTransactionItem::TransactionState(state) == false) | |
618 | return false; | |
619 | ||
620 | switch (state) | |
621 | { | |
622 | case TransactionStarted: _error->Fatal("Item %s changed to invalid transaction start state!", Target.URI.c_str()); break; | |
623 | case TransactionCommit: | |
624 | break; | |
625 | case TransactionAbort: | |
626 | std::string const Partial = GetPartialFileNameFromURI(Target.URI); | |
627 | RemoveFile("TransactionAbort", Partial); | |
628 | break; | |
629 | } | |
630 | ||
631 | return true; | |
632 | } | |
633 | /*}}}*/ | |
634 | ||
635 | class APT_HIDDEN NoActionItem : public pkgAcquire::Item /*{{{*/ | |
636 | /* The sole purpose of this class is having an item which does nothing to | |
637 | reach its done state to prevent cleanup deleting the mentioned file. | |
638 | Handy in cases in which we know we have the file already, like IMS-Hits. */ | |
639 | { | |
640 | IndexTarget const Target; | |
641 | public: | |
642 | virtual std::string DescURI() const APT_OVERRIDE {return Target.URI;}; | |
643 | virtual HashStringList GetExpectedHashes() const APT_OVERRIDE {return HashStringList();}; | |
644 | ||
645 | NoActionItem(pkgAcquire * const Owner, IndexTarget const &Target) : | |
646 | pkgAcquire::Item(Owner), Target(Target) | |
647 | { | |
648 | Status = StatDone; | |
649 | DestFile = GetFinalFileNameFromURI(Target.URI); | |
650 | } | |
651 | NoActionItem(pkgAcquire * const Owner, IndexTarget const &Target, std::string const &FinalFile) : | |
652 | pkgAcquire::Item(Owner), Target(Target) | |
653 | { | |
654 | Status = StatDone; | |
655 | DestFile = FinalFile; | |
656 | } | |
657 | }; | |
658 | /*}}}*/ | |
659 | class APT_HIDDEN CleanupItem : public pkgAcqTransactionItem /*{{{*/ | |
660 | /* This class ensures that a file which was configured but isn't downloaded | |
661 | for various reasons isn't kept in an old version in the lists directory. | |
662 | In a way its the reverse of NoActionItem as it helps with removing files | |
663 | even if the lists-cleanup is deactivated. */ | |
664 | { | |
665 | public: | |
666 | virtual std::string DescURI() const APT_OVERRIDE {return Target.URI;}; | |
667 | virtual HashStringList GetExpectedHashes() const APT_OVERRIDE {return HashStringList();}; | |
668 | ||
669 | CleanupItem(pkgAcquire * const Owner, pkgAcqMetaClearSig * const TransactionManager, IndexTarget const &Target) : | |
670 | pkgAcqTransactionItem(Owner, TransactionManager, Target) | |
671 | { | |
672 | Status = StatDone; | |
673 | DestFile = GetFinalFileNameFromURI(Target.URI); | |
674 | } | |
675 | bool TransactionState(TransactionStates const state) APT_OVERRIDE | |
676 | { | |
677 | switch (state) | |
678 | { | |
679 | case TransactionStarted: | |
680 | break; | |
681 | case TransactionAbort: | |
682 | break; | |
683 | case TransactionCommit: | |
684 | if (_config->FindB("Debug::Acquire::Transaction", false) == true) | |
685 | std::clog << "rm " << DestFile << " # " << DescURI() << std::endl; | |
686 | if (RemoveFile("TransItem::TransactionCommit", DestFile) == false) | |
687 | return false; | |
688 | break; | |
689 | } | |
690 | return true; | |
691 | } | |
692 | }; | |
693 | /*}}}*/ | |
694 | ||
695 | // Acquire::Item::Item - Constructor /*{{{*/ | |
696 | class pkgAcquire::Item::Private | |
697 | { | |
698 | public: | |
699 | std::vector<std::string> PastRedirections; | |
700 | }; | |
701 | APT_IGNORE_DEPRECATED_PUSH | |
702 | pkgAcquire::Item::Item(pkgAcquire * const owner) : | |
703 | FileSize(0), PartialSize(0), Mode(0), ID(0), Complete(false), Local(false), | |
704 | QueueCounter(0), ExpectedAdditionalItems(0), Owner(owner), d(new Private()) | |
705 | { | |
706 | Owner->Add(this); | |
707 | Status = StatIdle; | |
708 | } | |
709 | APT_IGNORE_DEPRECATED_POP | |
710 | /*}}}*/ | |
711 | // Acquire::Item::~Item - Destructor /*{{{*/ | |
712 | pkgAcquire::Item::~Item() | |
713 | { | |
714 | Owner->Remove(this); | |
715 | delete d; | |
716 | } | |
717 | /*}}}*/ | |
718 | std::string pkgAcquire::Item::Custom600Headers() const /*{{{*/ | |
719 | { | |
720 | return std::string(); | |
721 | } | |
722 | /*}}}*/ | |
723 | std::string pkgAcquire::Item::ShortDesc() const /*{{{*/ | |
724 | { | |
725 | return DescURI(); | |
726 | } | |
727 | /*}}}*/ | |
728 | APT_CONST void pkgAcquire::Item::Finished() /*{{{*/ | |
729 | { | |
730 | } | |
731 | /*}}}*/ | |
732 | APT_PURE pkgAcquire * pkgAcquire::Item::GetOwner() const /*{{{*/ | |
733 | { | |
734 | return Owner; | |
735 | } | |
736 | /*}}}*/ | |
737 | APT_CONST pkgAcquire::ItemDesc &pkgAcquire::Item::GetItemDesc() /*{{{*/ | |
738 | { | |
739 | return Desc; | |
740 | } | |
741 | /*}}}*/ | |
742 | APT_CONST bool pkgAcquire::Item::IsTrusted() const /*{{{*/ | |
743 | { | |
744 | return false; | |
745 | } | |
746 | /*}}}*/ | |
747 | // Acquire::Item::Failed - Item failed to download /*{{{*/ | |
748 | // --------------------------------------------------------------------- | |
749 | /* We return to an idle state if there are still other queues that could | |
750 | fetch this object */ | |
751 | static void formatHashsum(std::ostream &out, HashString const &hs) | |
752 | { | |
753 | auto const type = hs.HashType(); | |
754 | if (type == "Checksum-FileSize") | |
755 | out << " - Filesize"; | |
756 | else | |
757 | out << " - " << type; | |
758 | out << ':' << hs.HashValue(); | |
759 | if (hs.usable() == false) | |
760 | out << " [weak]"; | |
761 | out << std::endl; | |
762 | } | |
763 | void pkgAcquire::Item::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf) | |
764 | { | |
765 | if (QueueCounter <= 1) | |
766 | { | |
767 | /* This indicates that the file is not available right now but might | |
768 | be sometime later. If we do a retry cycle then this should be | |
769 | retried [CDROMs] */ | |
770 | if (Cnf != NULL && Cnf->LocalOnly == true && | |
771 | StringToBool(LookupTag(Message,"Transient-Failure"),false) == true) | |
772 | { | |
773 | Status = StatIdle; | |
774 | Dequeue(); | |
775 | return; | |
776 | } | |
777 | ||
778 | switch (Status) | |
779 | { | |
780 | case StatIdle: | |
781 | case StatFetching: | |
782 | case StatDone: | |
783 | Status = StatError; | |
784 | break; | |
785 | case StatAuthError: | |
786 | case StatError: | |
787 | case StatTransientNetworkError: | |
788 | break; | |
789 | } | |
790 | Complete = false; | |
791 | Dequeue(); | |
792 | } | |
793 | ||
794 | string const FailReason = LookupTag(Message, "FailReason"); | |
795 | enum { MAXIMUM_SIZE_EXCEEDED, HASHSUM_MISMATCH, WEAK_HASHSUMS, REDIRECTION_LOOP, OTHER } failreason = OTHER; | |
796 | if ( FailReason == "MaximumSizeExceeded") | |
797 | failreason = MAXIMUM_SIZE_EXCEEDED; | |
798 | else if ( FailReason == "WeakHashSums") | |
799 | failreason = WEAK_HASHSUMS; | |
800 | else if (FailReason == "RedirectionLoop") | |
801 | failreason = REDIRECTION_LOOP; | |
802 | else if (Status == StatAuthError) | |
803 | failreason = HASHSUM_MISMATCH; | |
804 | ||
805 | if(ErrorText.empty()) | |
806 | { | |
807 | std::ostringstream out; | |
808 | switch (failreason) | |
809 | { | |
810 | case HASHSUM_MISMATCH: | |
811 | out << _("Hash Sum mismatch") << std::endl; | |
812 | break; | |
813 | case WEAK_HASHSUMS: | |
814 | out << _("Insufficient information available to perform this download securely") << std::endl; | |
815 | break; | |
816 | case REDIRECTION_LOOP: | |
817 | out << "Redirection loop encountered" << std::endl; | |
818 | break; | |
819 | case MAXIMUM_SIZE_EXCEEDED: | |
820 | out << LookupTag(Message, "Message") << std::endl; | |
821 | break; | |
822 | case OTHER: | |
823 | out << LookupTag(Message, "Message"); | |
824 | break; | |
825 | } | |
826 | ||
827 | if (Status == StatAuthError) | |
828 | { | |
829 | auto const ExpectedHashes = GetExpectedHashes(); | |
830 | if (ExpectedHashes.empty() == false) | |
831 | { | |
832 | out << "Hashes of expected file:" << std::endl; | |
833 | for (auto const &hs: ExpectedHashes) | |
834 | formatHashsum(out, hs); | |
835 | } | |
836 | if (failreason == HASHSUM_MISMATCH) | |
837 | { | |
838 | out << "Hashes of received file:" << std::endl; | |
839 | for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type) | |
840 | { | |
841 | std::string const tagname = std::string(*type) + "-Hash"; | |
842 | std::string const hashsum = LookupTag(Message, tagname.c_str()); | |
843 | if (hashsum.empty() == false) | |
844 | formatHashsum(out, HashString(*type, hashsum)); | |
845 | } | |
846 | } | |
847 | auto const lastmod = LookupTag(Message, "Last-Modified", ""); | |
848 | if (lastmod.empty() == false) | |
849 | out << "Last modification reported: " << lastmod << std::endl; | |
850 | } | |
851 | ErrorText = out.str(); | |
852 | } | |
853 | ||
854 | switch (failreason) | |
855 | { | |
856 | case MAXIMUM_SIZE_EXCEEDED: RenameOnError(MaximumSizeExceeded); break; | |
857 | case HASHSUM_MISMATCH: RenameOnError(HashSumMismatch); break; | |
858 | case WEAK_HASHSUMS: break; | |
859 | case REDIRECTION_LOOP: break; | |
860 | case OTHER: break; | |
861 | } | |
862 | ||
863 | if (FailReason.empty() == false) | |
864 | ReportMirrorFailureToCentral(*this, FailReason, ErrorText); | |
865 | else | |
866 | ReportMirrorFailureToCentral(*this, ErrorText, ErrorText); | |
867 | ||
868 | if (QueueCounter > 1) | |
869 | Status = StatIdle; | |
870 | } | |
871 | /*}}}*/ | |
872 | // Acquire::Item::Start - Item has begun to download /*{{{*/ | |
873 | // --------------------------------------------------------------------- | |
874 | /* Stash status and the file size. Note that setting Complete means | |
875 | sub-phases of the acquire process such as decompresion are operating */ | |
876 | void pkgAcquire::Item::Start(string const &/*Message*/, unsigned long long const Size) | |
877 | { | |
878 | Status = StatFetching; | |
879 | ErrorText.clear(); | |
880 | if (FileSize == 0 && Complete == false) | |
881 | FileSize = Size; | |
882 | } | |
883 | /*}}}*/ | |
884 | // Acquire::Item::VerifyDone - check if Item was downloaded OK /*{{{*/ | |
885 | /* Note that hash-verification is 'hardcoded' in acquire-worker and has | |
886 | * already passed if this method is called. */ | |
887 | bool pkgAcquire::Item::VerifyDone(std::string const &Message, | |
888 | pkgAcquire::MethodConfig const * const /*Cnf*/) | |
889 | { | |
890 | std::string const FileName = LookupTag(Message,"Filename"); | |
891 | if (FileName.empty() == true) | |
892 | { | |
893 | Status = StatError; | |
894 | ErrorText = "Method gave a blank filename"; | |
895 | return false; | |
896 | } | |
897 | ||
898 | return true; | |
899 | } | |
900 | /*}}}*/ | |
901 | // Acquire::Item::Done - Item downloaded OK /*{{{*/ | |
902 | void pkgAcquire::Item::Done(string const &/*Message*/, HashStringList const &Hashes, | |
903 | pkgAcquire::MethodConfig const * const /*Cnf*/) | |
904 | { | |
905 | // We just downloaded something.. | |
906 | if (FileSize == 0) | |
907 | { | |
908 | unsigned long long const downloadedSize = Hashes.FileSize(); | |
909 | if (downloadedSize != 0) | |
910 | { | |
911 | FileSize = downloadedSize; | |
912 | } | |
913 | } | |
914 | Status = StatDone; | |
915 | ErrorText = string(); | |
916 | Owner->Dequeue(this); | |
917 | } | |
918 | /*}}}*/ | |
919 | // Acquire::Item::Rename - Rename a file /*{{{*/ | |
920 | // --------------------------------------------------------------------- | |
921 | /* This helper function is used by a lot of item methods as their final | |
922 | step */ | |
923 | bool pkgAcquire::Item::Rename(string const &From,string const &To) | |
924 | { | |
925 | if (From == To || rename(From.c_str(),To.c_str()) == 0) | |
926 | return true; | |
927 | ||
928 | std::string S; | |
929 | strprintf(S, _("rename failed, %s (%s -> %s)."), strerror(errno), | |
930 | From.c_str(),To.c_str()); | |
931 | Status = StatError; | |
932 | if (ErrorText.empty()) | |
933 | ErrorText = S; | |
934 | else | |
935 | ErrorText = ErrorText + ": " + S; | |
936 | return false; | |
937 | } | |
938 | /*}}}*/ | |
939 | void pkgAcquire::Item::Dequeue() /*{{{*/ | |
940 | { | |
941 | Owner->Dequeue(this); | |
942 | } | |
943 | /*}}}*/ | |
944 | bool pkgAcquire::Item::RenameOnError(pkgAcquire::Item::RenameOnErrorState const error)/*{{{*/ | |
945 | { | |
946 | if (RealFileExists(DestFile)) | |
947 | Rename(DestFile, DestFile + ".FAILED"); | |
948 | ||
949 | std::string errtext; | |
950 | switch (error) | |
951 | { | |
952 | case HashSumMismatch: | |
953 | errtext = _("Hash Sum mismatch"); | |
954 | break; | |
955 | case SizeMismatch: | |
956 | errtext = _("Size mismatch"); | |
957 | Status = StatAuthError; | |
958 | break; | |
959 | case InvalidFormat: | |
960 | errtext = _("Invalid file format"); | |
961 | Status = StatError; | |
962 | // do not report as usually its not the mirrors fault, but Portal/Proxy | |
963 | break; | |
964 | case SignatureError: | |
965 | errtext = _("Signature error"); | |
966 | Status = StatError; | |
967 | break; | |
968 | case NotClearsigned: | |
969 | strprintf(errtext, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NOSPLIT"); | |
970 | Status = StatAuthError; | |
971 | break; | |
972 | case MaximumSizeExceeded: | |
973 | // the method is expected to report a good error for this | |
974 | break; | |
975 | case PDiffError: | |
976 | // no handling here, done by callers | |
977 | break; | |
978 | } | |
979 | if (ErrorText.empty()) | |
980 | ErrorText = errtext; | |
981 | return false; | |
982 | } | |
983 | /*}}}*/ | |
984 | void pkgAcquire::Item::SetActiveSubprocess(const std::string &subprocess)/*{{{*/ | |
985 | { | |
986 | ActiveSubprocess = subprocess; | |
987 | APT_IGNORE_DEPRECATED(Mode = ActiveSubprocess.c_str();) | |
988 | } | |
989 | /*}}}*/ | |
990 | // Acquire::Item::ReportMirrorFailure /*{{{*/ | |
991 | void pkgAcquire::Item::ReportMirrorFailure(std::string const &FailCode) | |
992 | { | |
993 | ReportMirrorFailureToCentral(*this, FailCode, FailCode); | |
994 | } | |
995 | /*}}}*/ | |
996 | std::string pkgAcquire::Item::HashSum() const /*{{{*/ | |
997 | { | |
998 | HashStringList const hashes = GetExpectedHashes(); | |
999 | HashString const * const hs = hashes.find(NULL); | |
1000 | return hs != NULL ? hs->toStr() : ""; | |
1001 | } | |
1002 | /*}}}*/ | |
1003 | bool pkgAcquire::Item::IsRedirectionLoop(std::string const &NewURI) /*{{{*/ | |
1004 | { | |
1005 | // store can fail due to permission errors and the item will "loop" then | |
1006 | if (APT::String::Startswith(NewURI, "store:")) | |
1007 | return false; | |
1008 | if (d->PastRedirections.empty()) | |
1009 | { | |
1010 | d->PastRedirections.push_back(NewURI); | |
1011 | return false; | |
1012 | } | |
1013 | auto const LastURI = std::prev(d->PastRedirections.end()); | |
1014 | // redirections to the same file are a way of restarting/resheduling, | |
1015 | // individual methods will have to make sure that they aren't looping this way | |
1016 | if (*LastURI == NewURI) | |
1017 | return false; | |
1018 | if (std::find(d->PastRedirections.begin(), LastURI, NewURI) != LastURI) | |
1019 | return true; | |
1020 | d->PastRedirections.push_back(NewURI); | |
1021 | return false; | |
1022 | } | |
1023 | /*}}}*/ | |
1024 | ||
1025 | /*}}}*/ | |
1026 | int pkgAcquire::Item::Priority() /*{{{*/ | |
1027 | { | |
1028 | // Stage 1: Meta indices and diff indices | |
1029 | // - those need to be fetched first to have progress reporting working | |
1030 | // for the rest | |
1031 | if (dynamic_cast<pkgAcqMetaSig*>(this) != nullptr | |
1032 | || dynamic_cast<pkgAcqMetaBase*>(this) != nullptr | |
1033 | || dynamic_cast<pkgAcqDiffIndex*>(this) != nullptr) | |
1034 | return 1000; | |
1035 | // Stage 2: Diff files | |
1036 | // - fetch before complete indexes so we can apply the diffs while fetching | |
1037 | // larger files. | |
1038 | if (dynamic_cast<pkgAcqIndexDiffs*>(this) != nullptr || | |
1039 | dynamic_cast<pkgAcqIndexMergeDiffs*>(this) != nullptr) | |
1040 | return 800; | |
1041 | ||
1042 | // Stage 3: The rest - complete index files and other stuff | |
1043 | return 500; | |
1044 | } | |
1045 | /*}}}*/ | |
1046 | ||
1047 | pkgAcqTransactionItem::pkgAcqTransactionItem(pkgAcquire * const Owner, /*{{{*/ | |
1048 | pkgAcqMetaClearSig * const transactionManager, IndexTarget const &target) : | |
1049 | pkgAcquire::Item(Owner), d(NULL), Target(target), TransactionManager(transactionManager) | |
1050 | { | |
1051 | if (TransactionManager != this) | |
1052 | TransactionManager->Add(this); | |
1053 | } | |
1054 | /*}}}*/ | |
1055 | pkgAcqTransactionItem::~pkgAcqTransactionItem() /*{{{*/ | |
1056 | { | |
1057 | } | |
1058 | /*}}}*/ | |
1059 | HashStringList pkgAcqTransactionItem::GetExpectedHashesFor(std::string const &MetaKey) const /*{{{*/ | |
1060 | { | |
1061 | return GetExpectedHashesFromFor(TransactionManager->MetaIndexParser, MetaKey); | |
1062 | } | |
1063 | /*}}}*/ | |
1064 | ||
1065 | static void LoadLastMetaIndexParser(pkgAcqMetaClearSig * const TransactionManager, std::string const &FinalRelease, std::string const &FinalInRelease)/*{{{*/ | |
1066 | { | |
1067 | if (TransactionManager->IMSHit == true) | |
1068 | return; | |
1069 | if (RealFileExists(FinalInRelease) || RealFileExists(FinalRelease)) | |
1070 | { | |
1071 | TransactionManager->LastMetaIndexParser = TransactionManager->MetaIndexParser->UnloadedClone(); | |
1072 | if (TransactionManager->LastMetaIndexParser != NULL) | |
1073 | { | |
1074 | _error->PushToStack(); | |
1075 | if (RealFileExists(FinalInRelease)) | |
1076 | TransactionManager->LastMetaIndexParser->Load(FinalInRelease, NULL); | |
1077 | else | |
1078 | TransactionManager->LastMetaIndexParser->Load(FinalRelease, NULL); | |
1079 | // its unlikely to happen, but if what we have is bad ignore it | |
1080 | if (_error->PendingError()) | |
1081 | { | |
1082 | delete TransactionManager->LastMetaIndexParser; | |
1083 | TransactionManager->LastMetaIndexParser = NULL; | |
1084 | } | |
1085 | _error->RevertToStack(); | |
1086 | } | |
1087 | } | |
1088 | } | |
1089 | /*}}}*/ | |
1090 | ||
1091 | // AcqMetaBase - Constructor /*{{{*/ | |
1092 | pkgAcqMetaBase::pkgAcqMetaBase(pkgAcquire * const Owner, | |
1093 | pkgAcqMetaClearSig * const TransactionManager, | |
1094 | IndexTarget const &DataTarget) | |
1095 | : pkgAcqTransactionItem(Owner, TransactionManager, DataTarget), d(NULL), | |
1096 | AuthPass(false), IMSHit(false), State(TransactionStarted) | |
1097 | { | |
1098 | } | |
1099 | /*}}}*/ | |
1100 | // AcqMetaBase::Add - Add a item to the current Transaction /*{{{*/ | |
1101 | void pkgAcqMetaBase::Add(pkgAcqTransactionItem * const I) | |
1102 | { | |
1103 | Transaction.push_back(I); | |
1104 | } | |
1105 | /*}}}*/ | |
1106 | // AcqMetaBase::AbortTransaction - Abort the current Transaction /*{{{*/ | |
1107 | void pkgAcqMetaBase::AbortTransaction() | |
1108 | { | |
1109 | if(_config->FindB("Debug::Acquire::Transaction", false) == true) | |
1110 | std::clog << "AbortTransaction: " << TransactionManager << std::endl; | |
1111 | ||
1112 | switch (TransactionManager->State) | |
1113 | { | |
1114 | case TransactionStarted: break; | |
1115 | case TransactionAbort: _error->Fatal("Transaction %s was already aborted and is aborted again", TransactionManager->Target.URI.c_str()); return; | |
1116 | case TransactionCommit: _error->Fatal("Transaction %s was already aborted and is now committed", TransactionManager->Target.URI.c_str()); return; | |
1117 | } | |
1118 | TransactionManager->State = TransactionAbort; | |
1119 | ||
1120 | // ensure the toplevel is in error state too | |
1121 | for (std::vector<pkgAcqTransactionItem*>::iterator I = Transaction.begin(); | |
1122 | I != Transaction.end(); ++I) | |
1123 | { | |
1124 | if ((*I)->Status != pkgAcquire::Item::StatFetching) | |
1125 | Owner->Dequeue(*I); | |
1126 | (*I)->TransactionState(TransactionAbort); | |
1127 | } | |
1128 | Transaction.clear(); | |
1129 | } | |
1130 | /*}}}*/ | |
1131 | // AcqMetaBase::TransactionHasError - Check for errors in Transaction /*{{{*/ | |
1132 | APT_PURE bool pkgAcqMetaBase::TransactionHasError() const | |
1133 | { | |
1134 | for (std::vector<pkgAcqTransactionItem*>::const_iterator I = Transaction.begin(); | |
1135 | I != Transaction.end(); ++I) | |
1136 | { | |
1137 | switch((*I)->Status) { | |
1138 | case StatDone: break; | |
1139 | case StatIdle: break; | |
1140 | case StatAuthError: return true; | |
1141 | case StatError: return true; | |
1142 | case StatTransientNetworkError: return true; | |
1143 | case StatFetching: break; | |
1144 | } | |
1145 | } | |
1146 | return false; | |
1147 | } | |
1148 | /*}}}*/ | |
1149 | // AcqMetaBase::CommitTransaction - Commit a transaction /*{{{*/ | |
1150 | void pkgAcqMetaBase::CommitTransaction() | |
1151 | { | |
1152 | if(_config->FindB("Debug::Acquire::Transaction", false) == true) | |
1153 | std::clog << "CommitTransaction: " << this << std::endl; | |
1154 | ||
1155 | switch (TransactionManager->State) | |
1156 | { | |
1157 | case TransactionStarted: break; | |
1158 | case TransactionAbort: _error->Fatal("Transaction %s was already committed and is now aborted", TransactionManager->Target.URI.c_str()); return; | |
1159 | case TransactionCommit: _error->Fatal("Transaction %s was already committed and is again committed", TransactionManager->Target.URI.c_str()); return; | |
1160 | } | |
1161 | TransactionManager->State = TransactionCommit; | |
1162 | ||
1163 | // move new files into place *and* remove files that are not | |
1164 | // part of the transaction but are still on disk | |
1165 | for (std::vector<pkgAcqTransactionItem*>::iterator I = Transaction.begin(); | |
1166 | I != Transaction.end(); ++I) | |
1167 | { | |
1168 | (*I)->TransactionState(TransactionCommit); | |
1169 | } | |
1170 | Transaction.clear(); | |
1171 | } | |
1172 | /*}}}*/ | |
1173 | // AcqMetaBase::TransactionStageCopy - Stage a file for copying /*{{{*/ | |
1174 | void pkgAcqMetaBase::TransactionStageCopy(pkgAcqTransactionItem * const I, | |
1175 | const std::string &From, | |
1176 | const std::string &To) | |
1177 | { | |
1178 | I->PartialFile = From; | |
1179 | I->DestFile = To; | |
1180 | } | |
1181 | /*}}}*/ | |
1182 | // AcqMetaBase::TransactionStageRemoval - Stage a file for removal /*{{{*/ | |
1183 | void pkgAcqMetaBase::TransactionStageRemoval(pkgAcqTransactionItem * const I, | |
1184 | const std::string &FinalFile) | |
1185 | { | |
1186 | I->PartialFile = ""; | |
1187 | I->DestFile = FinalFile; | |
1188 | } | |
1189 | /*}}}*/ | |
1190 | // AcqMetaBase::GenerateAuthWarning - Check gpg authentication error /*{{{*/ | |
1191 | /* This method is called from ::Failed handlers. If it returns true, | |
1192 | no fallback to other files or modi is performed */ | |
1193 | bool pkgAcqMetaBase::CheckStopAuthentication(pkgAcquire::Item * const I, const std::string &Message) | |
1194 | { | |
1195 | string const Final = I->GetFinalFilename(); | |
1196 | std::string const GPGError = LookupTag(Message, "Message"); | |
1197 | if (FileExists(Final)) | |
1198 | { | |
1199 | I->Status = StatTransientNetworkError; | |
1200 | _error->Warning(_("An error occurred during the signature verification. " | |
1201 | "The repository is not updated and the previous index files will be used. " | |
1202 | "GPG error: %s: %s"), | |
1203 | Desc.Description.c_str(), | |
1204 | GPGError.c_str()); | |
1205 | RunScripts("APT::Update::Auth-Failure"); | |
1206 | return true; | |
1207 | } else if (LookupTag(Message,"Message").find("NODATA") != string::npos) { | |
1208 | /* Invalid signature file, reject (LP: #346386) (Closes: #627642) */ | |
1209 | _error->Error(_("GPG error: %s: %s"), | |
1210 | Desc.Description.c_str(), | |
1211 | GPGError.c_str()); | |
1212 | I->Status = StatAuthError; | |
1213 | return true; | |
1214 | } else { | |
1215 | _error->Warning(_("GPG error: %s: %s"), | |
1216 | Desc.Description.c_str(), | |
1217 | GPGError.c_str()); | |
1218 | } | |
1219 | // gpgv method failed | |
1220 | ReportMirrorFailureToCentral(*this, "GPGFailure", GPGError); | |
1221 | return false; | |
1222 | } | |
1223 | /*}}}*/ | |
1224 | // AcqMetaBase::Custom600Headers - Get header for AcqMetaBase /*{{{*/ | |
1225 | // --------------------------------------------------------------------- | |
1226 | string pkgAcqMetaBase::Custom600Headers() const | |
1227 | { | |
1228 | std::string Header = "\nIndex-File: true"; | |
1229 | std::string MaximumSize; | |
1230 | strprintf(MaximumSize, "\nMaximum-Size: %i", | |
1231 | _config->FindI("Acquire::MaxReleaseFileSize", 10*1000*1000)); | |
1232 | Header += MaximumSize; | |
1233 | ||
1234 | string const FinalFile = GetFinalFilename(); | |
1235 | struct stat Buf; | |
1236 | if (stat(FinalFile.c_str(),&Buf) == 0) | |
1237 | Header += "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime, false); | |
1238 | ||
1239 | return Header; | |
1240 | } | |
1241 | /*}}}*/ | |
1242 | // AcqMetaBase::QueueForSignatureVerify /*{{{*/ | |
1243 | void pkgAcqMetaBase::QueueForSignatureVerify(pkgAcqTransactionItem * const I, std::string const &File, std::string const &Signature) | |
1244 | { | |
1245 | AuthPass = true; | |
1246 | I->Desc.URI = "gpgv:" + Signature; | |
1247 | I->DestFile = File; | |
1248 | QueueURI(I->Desc); | |
1249 | I->SetActiveSubprocess("gpgv"); | |
1250 | } | |
1251 | /*}}}*/ | |
1252 | // AcqMetaBase::CheckDownloadDone /*{{{*/ | |
1253 | bool pkgAcqMetaBase::CheckDownloadDone(pkgAcqTransactionItem * const I, const std::string &Message, HashStringList const &Hashes) const | |
1254 | { | |
1255 | // We have just finished downloading a Release file (it is not | |
1256 | // verified yet) | |
1257 | ||
1258 | // Save the final base URI we got this Release file from | |
1259 | if (I->UsedMirror.empty() == false && _config->FindB("Acquire::SameMirrorForAllIndexes", true)) | |
1260 | { | |
1261 | if (APT::String::Endswith(I->Desc.URI, "InRelease")) | |
1262 | { | |
1263 | TransactionManager->BaseURI = I->Desc.URI.substr(0, I->Desc.URI.length() - strlen("InRelease")); | |
1264 | TransactionManager->UsedMirror = I->UsedMirror; | |
1265 | } | |
1266 | else if (APT::String::Endswith(I->Desc.URI, "Release")) | |
1267 | { | |
1268 | TransactionManager->BaseURI = I->Desc.URI.substr(0, I->Desc.URI.length() - strlen("Release")); | |
1269 | TransactionManager->UsedMirror = I->UsedMirror; | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | std::string const FileName = LookupTag(Message,"Filename"); | |
1274 | if (FileName != I->DestFile && RealFileExists(I->DestFile) == false) | |
1275 | { | |
1276 | I->Local = true; | |
1277 | I->Desc.URI = "copy:" + FileName; | |
1278 | I->QueueURI(I->Desc); | |
1279 | return false; | |
1280 | } | |
1281 | ||
1282 | // make sure to verify against the right file on I-M-S hit | |
1283 | bool IMSHit = StringToBool(LookupTag(Message,"IMS-Hit"), false); | |
1284 | if (IMSHit == false && Hashes.usable()) | |
1285 | { | |
1286 | // detect IMS-Hits servers haven't detected by Hash comparison | |
1287 | std::string const FinalFile = I->GetFinalFilename(); | |
1288 | if (RealFileExists(FinalFile) && Hashes.VerifyFile(FinalFile) == true) | |
1289 | { | |
1290 | IMSHit = true; | |
1291 | RemoveFile("CheckDownloadDone", I->DestFile); | |
1292 | } | |
1293 | } | |
1294 | ||
1295 | if(IMSHit == true) | |
1296 | { | |
1297 | // for simplicity, the transaction manager is always InRelease | |
1298 | // even if it doesn't exist. | |
1299 | TransactionManager->IMSHit = true; | |
1300 | I->PartialFile = I->DestFile = I->GetFinalFilename(); | |
1301 | } | |
1302 | ||
1303 | // set Item to complete as the remaining work is all local (verify etc) | |
1304 | I->Complete = true; | |
1305 | ||
1306 | return true; | |
1307 | } | |
1308 | /*}}}*/ | |
1309 | bool pkgAcqMetaBase::CheckAuthDone(string const &Message) /*{{{*/ | |
1310 | { | |
1311 | // At this point, the gpgv method has succeeded, so there is a | |
1312 | // valid signature from a key in the trusted keyring. We | |
1313 | // perform additional verification of its contents, and use them | |
1314 | // to verify the indexes we are about to download | |
1315 | if (_config->FindB("Debug::pkgAcquire::Auth", false)) | |
1316 | std::cerr << "Signature verification succeeded: " << DestFile << std::endl; | |
1317 | ||
1318 | if (TransactionManager->IMSHit == false) | |
1319 | { | |
1320 | // open the last (In)Release if we have it | |
1321 | std::string const FinalFile = GetFinalFilename(); | |
1322 | std::string FinalRelease; | |
1323 | std::string FinalInRelease; | |
1324 | if (APT::String::Endswith(FinalFile, "InRelease")) | |
1325 | { | |
1326 | FinalInRelease = FinalFile; | |
1327 | FinalRelease = FinalFile.substr(0, FinalFile.length() - strlen("InRelease")) + "Release"; | |
1328 | } | |
1329 | else | |
1330 | { | |
1331 | FinalInRelease = FinalFile.substr(0, FinalFile.length() - strlen("Release")) + "InRelease"; | |
1332 | FinalRelease = FinalFile; | |
1333 | } | |
1334 | LoadLastMetaIndexParser(TransactionManager, FinalRelease, FinalInRelease); | |
1335 | } | |
1336 | ||
1337 | bool const GoodAuth = TransactionManager->MetaIndexParser->Load(DestFile, &ErrorText); | |
1338 | if (GoodAuth == false && AllowInsecureRepositories(InsecureType::WEAK, Target.Description, TransactionManager->MetaIndexParser, TransactionManager, this) == false) | |
1339 | { | |
1340 | Status = StatAuthError; | |
1341 | return false; | |
1342 | } | |
1343 | ||
1344 | if (!VerifyVendor(Message)) | |
1345 | { | |
1346 | Status = StatAuthError; | |
1347 | return false; | |
1348 | } | |
1349 | ||
1350 | // Download further indexes with verification | |
1351 | TransactionManager->QueueIndexes(GoodAuth); | |
1352 | ||
1353 | return GoodAuth; | |
1354 | } | |
1355 | /*}}}*/ | |
1356 | void pkgAcqMetaClearSig::QueueIndexes(bool const verify) /*{{{*/ | |
1357 | { | |
1358 | // at this point the real Items are loaded in the fetcher | |
1359 | ExpectedAdditionalItems = 0; | |
1360 | ||
1361 | std::set<std::string> targetsSeen; | |
1362 | bool const hasReleaseFile = TransactionManager->MetaIndexParser != NULL; | |
1363 | bool const metaBaseSupportsByHash = hasReleaseFile && TransactionManager->MetaIndexParser->GetSupportsAcquireByHash(); | |
1364 | bool hasHashes = true; | |
1365 | auto IndexTargets = TransactionManager->MetaIndexParser->GetIndexTargets(); | |
1366 | if (hasReleaseFile && verify == false) | |
1367 | hasHashes = std::any_of(IndexTargets.begin(), IndexTargets.end(), | |
1368 | [&](IndexTarget const &Target) { return TransactionManager->MetaIndexParser->Exists(Target.MetaKey); }); | |
1369 | if (_config->FindB("Acquire::IndexTargets::Randomized", true) && likely(IndexTargets.empty() == false)) | |
1370 | { | |
1371 | /* For fallback handling and to have some reasonable progress information | |
1372 | we can't randomize everything, but at least the order in the same type | |
1373 | can be as we shouldn't be telling the mirrors (and everyone else watching) | |
1374 | which is native/foreign arch, specific order of preference of translations, … */ | |
1375 | auto range_start = IndexTargets.begin(); | |
1376 | std::random_device rd; | |
1377 | std::default_random_engine g(rd()); | |
1378 | do { | |
1379 | auto const type = range_start->Option(IndexTarget::CREATED_BY); | |
1380 | auto const range_end = std::find_if_not(range_start, IndexTargets.end(), | |
1381 | [&type](IndexTarget const &T) { return type == T.Option(IndexTarget::CREATED_BY); }); | |
1382 | std::shuffle(range_start, range_end, g); | |
1383 | range_start = range_end; | |
1384 | } while (range_start != IndexTargets.end()); | |
1385 | } | |
1386 | for (auto&& Target: IndexTargets) | |
1387 | { | |
1388 | // if we have seen a target which is created-by a target this one here is declared a | |
1389 | // fallback to, we skip acquiring the fallback (but we make sure we clean up) | |
1390 | if (targetsSeen.find(Target.Option(IndexTarget::FALLBACK_OF)) != targetsSeen.end()) | |
1391 | { | |
1392 | targetsSeen.emplace(Target.Option(IndexTarget::CREATED_BY)); | |
1393 | new CleanupItem(Owner, TransactionManager, Target); | |
1394 | continue; | |
1395 | } | |
1396 | // all is an implementation detail. Users shouldn't use this as arch | |
1397 | // We need this support trickery here as e.g. Debian has binary-all files already, | |
1398 | // but arch:all packages are still in the arch:any files, so we would waste precious | |
1399 | // download time, bandwidth and diskspace for nothing, BUT Debian doesn't feature all | |
1400 | // in the set of supported architectures, so we can filter based on this property rather | |
1401 | // than invent an entirely new flag we would need to carry for all of eternity. | |
1402 | if (hasReleaseFile && Target.Option(IndexTarget::ARCHITECTURE) == "all") | |
1403 | { | |
1404 | if (TransactionManager->MetaIndexParser->IsArchitectureAllSupportedFor(Target) == false) | |
1405 | { | |
1406 | new CleanupItem(Owner, TransactionManager, Target); | |
1407 | continue; | |
1408 | } | |
1409 | } | |
1410 | ||
1411 | bool trypdiff = Target.OptionBool(IndexTarget::PDIFFS); | |
1412 | if (hasReleaseFile == true) | |
1413 | { | |
1414 | if (TransactionManager->MetaIndexParser->Exists(Target.MetaKey) == false) | |
1415 | { | |
1416 | // optional targets that we do not have in the Release file are skipped | |
1417 | if (hasHashes == true && Target.IsOptional) | |
1418 | { | |
1419 | new CleanupItem(Owner, TransactionManager, Target); | |
1420 | continue; | |
1421 | } | |
1422 | ||
1423 | std::string const &arch = Target.Option(IndexTarget::ARCHITECTURE); | |
1424 | if (arch.empty() == false) | |
1425 | { | |
1426 | if (TransactionManager->MetaIndexParser->IsArchitectureSupported(arch) == false) | |
1427 | { | |
1428 | new CleanupItem(Owner, TransactionManager, Target); | |
1429 | _error->Notice(_("Skipping acquire of configured file '%s' as repository '%s' doesn't support architecture '%s'"), | |
1430 | Target.MetaKey.c_str(), TransactionManager->Target.Description.c_str(), arch.c_str()); | |
1431 | continue; | |
1432 | } | |
1433 | // if the architecture is officially supported but currently no packages for it available, | |
1434 | // ignore silently as this is pretty much the same as just shipping an empty file. | |
1435 | // if we don't know which architectures are supported, we do NOT ignore it to notify user about this | |
1436 | if (hasHashes == true && TransactionManager->MetaIndexParser->IsArchitectureSupported("*undefined*") == false) | |
1437 | { | |
1438 | new CleanupItem(Owner, TransactionManager, Target); | |
1439 | continue; | |
1440 | } | |
1441 | } | |
1442 | ||
1443 | if (hasHashes == true) | |
1444 | { | |
1445 | Status = StatAuthError; | |
1446 | strprintf(ErrorText, _("Unable to find expected entry '%s' in Release file (Wrong sources.list entry or malformed file)"), Target.MetaKey.c_str()); | |
1447 | return; | |
1448 | } | |
1449 | else | |
1450 | { | |
1451 | new pkgAcqIndex(Owner, TransactionManager, Target); | |
1452 | continue; | |
1453 | } | |
1454 | } | |
1455 | else if (verify) | |
1456 | { | |
1457 | auto const hashes = GetExpectedHashesFor(Target.MetaKey); | |
1458 | if (hashes.empty() == false) | |
1459 | { | |
1460 | if (hashes.usable() == false && TargetIsAllowedToBe(TransactionManager->Target, InsecureType::WEAK) == false) | |
1461 | { | |
1462 | new CleanupItem(Owner, TransactionManager, Target); | |
1463 | _error->Warning(_("Skipping acquire of configured file '%s' as repository '%s' provides only weak security information for it"), | |
1464 | Target.MetaKey.c_str(), TransactionManager->Target.Description.c_str()); | |
1465 | continue; | |
1466 | } | |
1467 | // empty files are skipped as acquiring the very small compressed files is a waste of time | |
1468 | else if (hashes.FileSize() == 0) | |
1469 | { | |
1470 | new CleanupItem(Owner, TransactionManager, Target); | |
1471 | targetsSeen.emplace(Target.Option(IndexTarget::CREATED_BY)); | |
1472 | continue; | |
1473 | } | |
1474 | } | |
1475 | } | |
1476 | ||
1477 | // autoselect the compression method | |
1478 | std::vector<std::string> types = VectorizeString(Target.Option(IndexTarget::COMPRESSIONTYPES), ' '); | |
1479 | types.erase(std::remove_if(types.begin(), types.end(), [&](std::string const &t) { | |
1480 | if (t == "uncompressed") | |
1481 | return TransactionManager->MetaIndexParser->Exists(Target.MetaKey) == false; | |
1482 | std::string const MetaKey = Target.MetaKey + "." + t; | |
1483 | return TransactionManager->MetaIndexParser->Exists(MetaKey) == false; | |
1484 | }), types.end()); | |
1485 | if (types.empty() == false) | |
1486 | { | |
1487 | std::ostringstream os; | |
1488 | // add the special compressiontype byhash first if supported | |
1489 | std::string const useByHashConf = Target.Option(IndexTarget::BY_HASH); | |
1490 | bool useByHash = false; | |
1491 | if(useByHashConf == "force") | |
1492 | useByHash = true; | |
1493 | else | |
1494 | useByHash = StringToBool(useByHashConf) == true && metaBaseSupportsByHash; | |
1495 | if (useByHash == true) | |
1496 | os << "by-hash "; | |
1497 | std::copy(types.begin(), types.end()-1, std::ostream_iterator<std::string>(os, " ")); | |
1498 | os << *types.rbegin(); | |
1499 | Target.Options["COMPRESSIONTYPES"] = os.str(); | |
1500 | } | |
1501 | else | |
1502 | Target.Options["COMPRESSIONTYPES"].clear(); | |
1503 | ||
1504 | std::string filename = GetExistingFilename(GetFinalFileNameFromURI(Target.URI)); | |
1505 | if (filename.empty() == false) | |
1506 | { | |
1507 | // if the Release file is a hit and we have an index it must be the current one | |
1508 | if (TransactionManager->IMSHit == true) | |
1509 | ; | |
1510 | else if (TransactionManager->LastMetaIndexParser != NULL) | |
1511 | { | |
1512 | // see if the file changed since the last Release file | |
1513 | // we use the uncompressed files as we might compress differently compared to the server, | |
1514 | // so the hashes might not match, even if they contain the same data. | |
1515 | HashStringList const newFile = GetExpectedHashesFromFor(TransactionManager->MetaIndexParser, Target.MetaKey); | |
1516 | HashStringList const oldFile = GetExpectedHashesFromFor(TransactionManager->LastMetaIndexParser, Target.MetaKey); | |
1517 | if (newFile != oldFile) | |
1518 | filename.clear(); | |
1519 | } | |
1520 | else | |
1521 | filename.clear(); | |
1522 | } | |
1523 | else | |
1524 | trypdiff = false; // no file to patch | |
1525 | ||
1526 | if (filename.empty() == false) | |
1527 | { | |
1528 | new NoActionItem(Owner, Target, filename); | |
1529 | std::string const idxfilename = GetFinalFileNameFromURI(GetDiffIndexURI(Target)); | |
1530 | if (FileExists(idxfilename)) | |
1531 | new NoActionItem(Owner, Target, idxfilename); | |
1532 | targetsSeen.emplace(Target.Option(IndexTarget::CREATED_BY)); | |
1533 | continue; | |
1534 | } | |
1535 | ||
1536 | // check if we have patches available | |
1537 | trypdiff &= TransactionManager->MetaIndexParser->Exists(GetDiffIndexFileName(Target.MetaKey)); | |
1538 | } | |
1539 | else | |
1540 | { | |
1541 | // if we have no file to patch, no point in trying | |
1542 | trypdiff &= (GetExistingFilename(GetFinalFileNameFromURI(Target.URI)).empty() == false); | |
1543 | } | |
1544 | ||
1545 | // no point in patching from local sources | |
1546 | if (trypdiff) | |
1547 | { | |
1548 | std::string const proto = Target.URI.substr(0, strlen("file:/")); | |
1549 | if (proto == "file:/" || proto == "copy:/" || proto == "cdrom:") | |
1550 | trypdiff = false; | |
1551 | } | |
1552 | ||
1553 | // Queue the Index file (Packages, Sources, Translation-$foo, …) | |
1554 | targetsSeen.emplace(Target.Option(IndexTarget::CREATED_BY)); | |
1555 | if (trypdiff) | |
1556 | new pkgAcqDiffIndex(Owner, TransactionManager, Target); | |
1557 | else | |
1558 | new pkgAcqIndex(Owner, TransactionManager, Target); | |
1559 | } | |
1560 | } | |
1561 | /*}}}*/ | |
1562 | bool pkgAcqMetaBase::VerifyVendor(string const &) /*{{{*/ | |
1563 | { | |
1564 | if (TransactionManager->MetaIndexParser->GetValidUntil() > 0) | |
1565 | { | |
1566 | time_t const invalid_since = time(NULL) - TransactionManager->MetaIndexParser->GetValidUntil(); | |
1567 | if (invalid_since > 0) | |
1568 | { | |
1569 | std::string errmsg; | |
1570 | strprintf(errmsg, | |
1571 | // TRANSLATOR: The first %s is the URL of the bad Release file, the second is | |
1572 | // the time since then the file is invalid - formatted in the same way as in | |
1573 | // the download progress display (e.g. 7d 3h 42min 1s) | |
1574 | _("Release file for %s is expired (invalid since %s). " | |
1575 | "Updates for this repository will not be applied."), | |
1576 | Target.URI.c_str(), TimeToStr(invalid_since).c_str()); | |
1577 | if (ErrorText.empty()) | |
1578 | ErrorText = errmsg; | |
1579 | return _error->Error("%s", errmsg.c_str()); | |
1580 | } | |
1581 | } | |
1582 | ||
1583 | /* Did we get a file older than what we have? This is a last minute IMS hit and doubles | |
1584 | as a prevention of downgrading us to older (still valid) files */ | |
1585 | if (TransactionManager->IMSHit == false && TransactionManager->LastMetaIndexParser != NULL && | |
1586 | TransactionManager->LastMetaIndexParser->GetDate() > TransactionManager->MetaIndexParser->GetDate()) | |
1587 | { | |
1588 | TransactionManager->IMSHit = true; | |
1589 | RemoveFile("VerifyVendor", DestFile); | |
1590 | PartialFile = DestFile = GetFinalFilename(); | |
1591 | // load the 'old' file in the 'new' one instead of flipping pointers as | |
1592 | // the new one isn't owned by us, while the old one is so cleanup would be confused. | |
1593 | TransactionManager->MetaIndexParser->swapLoad(TransactionManager->LastMetaIndexParser); | |
1594 | delete TransactionManager->LastMetaIndexParser; | |
1595 | TransactionManager->LastMetaIndexParser = NULL; | |
1596 | } | |
1597 | ||
1598 | if (_config->FindB("Debug::pkgAcquire::Auth", false)) | |
1599 | { | |
1600 | std::cerr << "Got Codename: " << TransactionManager->MetaIndexParser->GetCodename() << std::endl; | |
1601 | std::cerr << "Got Suite: " << TransactionManager->MetaIndexParser->GetSuite() << std::endl; | |
1602 | std::cerr << "Expecting Dist: " << TransactionManager->MetaIndexParser->GetExpectedDist() << std::endl; | |
1603 | } | |
1604 | ||
1605 | // One day that might become fatal… | |
1606 | auto const ExpectedDist = TransactionManager->MetaIndexParser->GetExpectedDist(); | |
1607 | auto const NowCodename = TransactionManager->MetaIndexParser->GetCodename(); | |
1608 | if (TransactionManager->MetaIndexParser->CheckDist(ExpectedDist) == false) | |
1609 | _error->Warning(_("Conflicting distribution: %s (expected %s but got %s)"), | |
1610 | Desc.Description.c_str(), ExpectedDist.c_str(), NowCodename.c_str()); | |
1611 | // might be okay, might be not | |
1612 | if (TransactionManager->LastMetaIndexParser != nullptr) | |
1613 | { | |
1614 | auto const LastCodename = TransactionManager->LastMetaIndexParser->GetCodename(); | |
1615 | if (LastCodename.empty() == false && NowCodename.empty() == false && LastCodename != NowCodename) | |
1616 | _error->Warning(_("Conflicting distribution: %s (expected %s but got %s)"), | |
1617 | Desc.Description.c_str(), LastCodename.c_str(), NowCodename.c_str()); | |
1618 | } | |
1619 | return true; | |
1620 | } | |
1621 | /*}}}*/ | |
1622 | pkgAcqMetaBase::~pkgAcqMetaBase() | |
1623 | { | |
1624 | } | |
1625 | ||
1626 | pkgAcqMetaClearSig::pkgAcqMetaClearSig(pkgAcquire * const Owner, /*{{{*/ | |
1627 | IndexTarget const &ClearsignedTarget, | |
1628 | IndexTarget const &DetachedDataTarget, IndexTarget const &DetachedSigTarget, | |
1629 | metaIndex * const MetaIndexParser) : | |
1630 | pkgAcqMetaIndex(Owner, this, ClearsignedTarget, DetachedSigTarget), | |
1631 | d(NULL), DetachedDataTarget(DetachedDataTarget), | |
1632 | MetaIndexParser(MetaIndexParser), LastMetaIndexParser(NULL) | |
1633 | { | |
1634 | // index targets + (worst case:) Release/Release.gpg | |
1635 | ExpectedAdditionalItems = std::numeric_limits<decltype(ExpectedAdditionalItems)>::max(); | |
1636 | TransactionManager->Add(this); | |
1637 | } | |
1638 | /*}}}*/ | |
1639 | pkgAcqMetaClearSig::~pkgAcqMetaClearSig() /*{{{*/ | |
1640 | { | |
1641 | if (LastMetaIndexParser != NULL) | |
1642 | delete LastMetaIndexParser; | |
1643 | } | |
1644 | /*}}}*/ | |
1645 | // pkgAcqMetaClearSig::Custom600Headers - Insert custom request headers /*{{{*/ | |
1646 | string pkgAcqMetaClearSig::Custom600Headers() const | |
1647 | { | |
1648 | string Header = pkgAcqMetaBase::Custom600Headers(); | |
1649 | Header += "\nFail-Ignore: true"; | |
1650 | std::string const key = TransactionManager->MetaIndexParser->GetSignedBy(); | |
1651 | if (key.empty() == false) | |
1652 | Header += "\nSigned-By: " + key; | |
1653 | ||
1654 | return Header; | |
1655 | } | |
1656 | /*}}}*/ | |
1657 | void pkgAcqMetaClearSig::Finished() /*{{{*/ | |
1658 | { | |
1659 | if(_config->FindB("Debug::Acquire::Transaction", false) == true) | |
1660 | std::clog << "Finished: " << DestFile <<std::endl; | |
1661 | if(TransactionManager->State == TransactionStarted && | |
1662 | TransactionManager->TransactionHasError() == false) | |
1663 | TransactionManager->CommitTransaction(); | |
1664 | } | |
1665 | /*}}}*/ | |
1666 | bool pkgAcqMetaClearSig::VerifyDone(std::string const &Message, /*{{{*/ | |
1667 | pkgAcquire::MethodConfig const * const Cnf) | |
1668 | { | |
1669 | Item::VerifyDone(Message, Cnf); | |
1670 | ||
1671 | if (FileExists(DestFile) && !StartsWithGPGClearTextSignature(DestFile)) | |
1672 | return RenameOnError(NotClearsigned); | |
1673 | ||
1674 | return true; | |
1675 | } | |
1676 | /*}}}*/ | |
1677 | // pkgAcqMetaClearSig::Done - We got a file /*{{{*/ | |
1678 | void pkgAcqMetaClearSig::Done(std::string const &Message, | |
1679 | HashStringList const &Hashes, | |
1680 | pkgAcquire::MethodConfig const * const Cnf) | |
1681 | { | |
1682 | Item::Done(Message, Hashes, Cnf); | |
1683 | ||
1684 | if(AuthPass == false) | |
1685 | { | |
1686 | if(CheckDownloadDone(this, Message, Hashes) == true) | |
1687 | QueueForSignatureVerify(this, DestFile, DestFile); | |
1688 | return; | |
1689 | } | |
1690 | else if(CheckAuthDone(Message) == true) | |
1691 | { | |
1692 | if (TransactionManager->IMSHit == false) | |
1693 | TransactionManager->TransactionStageCopy(this, DestFile, GetFinalFilename()); | |
1694 | else if (RealFileExists(GetFinalFilename()) == false) | |
1695 | { | |
1696 | // We got an InRelease file IMSHit, but we haven't one, which means | |
1697 | // we had a valid Release/Release.gpg combo stepping in, which we have | |
1698 | // to 'acquire' now to ensure list cleanup isn't removing them | |
1699 | new NoActionItem(Owner, DetachedDataTarget); | |
1700 | new NoActionItem(Owner, DetachedSigTarget); | |
1701 | } | |
1702 | } | |
1703 | else if (Status != StatAuthError) | |
1704 | { | |
1705 | string const FinalFile = GetFinalFileNameFromURI(DetachedDataTarget.URI); | |
1706 | string const OldFile = GetFinalFilename(); | |
1707 | if (TransactionManager->IMSHit == false) | |
1708 | TransactionManager->TransactionStageCopy(this, DestFile, FinalFile); | |
1709 | else if (RealFileExists(OldFile) == false) | |
1710 | new NoActionItem(Owner, DetachedDataTarget); | |
1711 | else | |
1712 | TransactionManager->TransactionStageCopy(this, OldFile, FinalFile); | |
1713 | } | |
1714 | } | |
1715 | /*}}}*/ | |
1716 | void pkgAcqMetaClearSig::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf) /*{{{*/ | |
1717 | { | |
1718 | Item::Failed(Message, Cnf); | |
1719 | ||
1720 | if (AuthPass == false) | |
1721 | { | |
1722 | if (Status == StatAuthError || Status == StatTransientNetworkError) | |
1723 | { | |
1724 | // if we expected a ClearTextSignature (InRelease) but got a network | |
1725 | // error or got a file, but it wasn't valid, we end up here (see VerifyDone). | |
1726 | // As these is usually called by web-portals we do not try Release/Release.gpg | |
1727 | // as this is gonna fail anyway and instead abort our try (LP#346386) | |
1728 | TransactionManager->AbortTransaction(); | |
1729 | return; | |
1730 | } | |
1731 | ||
1732 | // Queue the 'old' InRelease file for removal if we try Release.gpg | |
1733 | // as otherwise the file will stay around and gives a false-auth | |
1734 | // impression (CVE-2012-0214) | |
1735 | TransactionManager->TransactionStageRemoval(this, GetFinalFilename()); | |
1736 | Status = StatDone; | |
1737 | ||
1738 | new pkgAcqMetaIndex(Owner, TransactionManager, DetachedDataTarget, DetachedSigTarget); | |
1739 | } | |
1740 | else | |
1741 | { | |
1742 | if(CheckStopAuthentication(this, Message)) | |
1743 | return; | |
1744 | ||
1745 | if(AllowInsecureRepositories(InsecureType::UNSIGNED, Target.Description, TransactionManager->MetaIndexParser, TransactionManager, this) == true) | |
1746 | { | |
1747 | Status = StatDone; | |
1748 | ||
1749 | /* InRelease files become Release files, otherwise | |
1750 | * they would be considered as trusted later on */ | |
1751 | string const FinalRelease = GetFinalFileNameFromURI(DetachedDataTarget.URI); | |
1752 | string const PartialRelease = GetPartialFileNameFromURI(DetachedDataTarget.URI); | |
1753 | string const FinalReleasegpg = GetFinalFileNameFromURI(DetachedSigTarget.URI); | |
1754 | string const FinalInRelease = GetFinalFilename(); | |
1755 | Rename(DestFile, PartialRelease); | |
1756 | TransactionManager->TransactionStageCopy(this, PartialRelease, FinalRelease); | |
1757 | LoadLastMetaIndexParser(TransactionManager, FinalRelease, FinalInRelease); | |
1758 | ||
1759 | // we parse the indexes here because at this point the user wanted | |
1760 | // a repository that may potentially harm him | |
1761 | if (TransactionManager->MetaIndexParser->Load(PartialRelease, &ErrorText) == false || VerifyVendor(Message) == false) | |
1762 | /* expired Release files are still a problem you need extra force for */; | |
1763 | else | |
1764 | TransactionManager->QueueIndexes(true); | |
1765 | } | |
1766 | } | |
1767 | } | |
1768 | /*}}}*/ | |
1769 | ||
1770 | pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire * const Owner, /*{{{*/ | |
1771 | pkgAcqMetaClearSig * const TransactionManager, | |
1772 | IndexTarget const &DataTarget, | |
1773 | IndexTarget const &DetachedSigTarget) : | |
1774 | pkgAcqMetaBase(Owner, TransactionManager, DataTarget), d(NULL), | |
1775 | DetachedSigTarget(DetachedSigTarget) | |
1776 | { | |
1777 | if(_config->FindB("Debug::Acquire::Transaction", false) == true) | |
1778 | std::clog << "New pkgAcqMetaIndex with TransactionManager " | |
1779 | << this->TransactionManager << std::endl; | |
1780 | ||
1781 | DestFile = GetPartialFileNameFromURI(DataTarget.URI); | |
1782 | ||
1783 | // Create the item | |
1784 | Desc.Description = DataTarget.Description; | |
1785 | Desc.Owner = this; | |
1786 | Desc.ShortDesc = DataTarget.ShortDesc; | |
1787 | Desc.URI = DataTarget.URI; | |
1788 | QueueURI(Desc); | |
1789 | } | |
1790 | /*}}}*/ | |
1791 | void pkgAcqMetaIndex::Done(string const &Message, /*{{{*/ | |
1792 | HashStringList const &Hashes, | |
1793 | pkgAcquire::MethodConfig const * const Cfg) | |
1794 | { | |
1795 | Item::Done(Message,Hashes,Cfg); | |
1796 | ||
1797 | if(CheckDownloadDone(this, Message, Hashes)) | |
1798 | { | |
1799 | // we have a Release file, now download the Signature, all further | |
1800 | // verify/queue for additional downloads will be done in the | |
1801 | // pkgAcqMetaSig::Done() code | |
1802 | new pkgAcqMetaSig(Owner, TransactionManager, DetachedSigTarget, this); | |
1803 | } | |
1804 | } | |
1805 | /*}}}*/ | |
1806 | // pkgAcqMetaIndex::Failed - no Release file present /*{{{*/ | |
1807 | void pkgAcqMetaIndex::Failed(string const &Message, | |
1808 | pkgAcquire::MethodConfig const * const Cnf) | |
1809 | { | |
1810 | pkgAcquire::Item::Failed(Message, Cnf); | |
1811 | Status = StatDone; | |
1812 | ||
1813 | // No Release file was present so fall | |
1814 | // back to queueing Packages files without verification | |
1815 | // only allow going further if the user explicitly wants it | |
1816 | if(AllowInsecureRepositories(InsecureType::NORELEASE, Target.Description, TransactionManager->MetaIndexParser, TransactionManager, this) == true) | |
1817 | { | |
1818 | // ensure old Release files are removed | |
1819 | TransactionManager->TransactionStageRemoval(this, GetFinalFilename()); | |
1820 | ||
1821 | // queue without any kind of hashsum support | |
1822 | TransactionManager->QueueIndexes(false); | |
1823 | } | |
1824 | } | |
1825 | /*}}}*/ | |
1826 | std::string pkgAcqMetaIndex::DescURI() const /*{{{*/ | |
1827 | { | |
1828 | return Target.URI; | |
1829 | } | |
1830 | /*}}}*/ | |
1831 | pkgAcqMetaIndex::~pkgAcqMetaIndex() {} | |
1832 | ||
1833 | // AcqMetaSig::AcqMetaSig - Constructor /*{{{*/ | |
1834 | pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire * const Owner, | |
1835 | pkgAcqMetaClearSig * const TransactionManager, | |
1836 | IndexTarget const &Target, | |
1837 | pkgAcqMetaIndex * const MetaIndex) : | |
1838 | pkgAcqTransactionItem(Owner, TransactionManager, Target), d(NULL), MetaIndex(MetaIndex) | |
1839 | { | |
1840 | DestFile = GetPartialFileNameFromURI(Target.URI); | |
1841 | ||
1842 | // remove any partial downloaded sig-file in partial/. | |
1843 | // it may confuse proxies and is too small to warrant a | |
1844 | // partial download anyway | |
1845 | RemoveFile("pkgAcqMetaSig", DestFile); | |
1846 | ||
1847 | // set the TransactionManager | |
1848 | if(_config->FindB("Debug::Acquire::Transaction", false) == true) | |
1849 | std::clog << "New pkgAcqMetaSig with TransactionManager " | |
1850 | << TransactionManager << std::endl; | |
1851 | ||
1852 | // Create the item | |
1853 | Desc.Description = Target.Description; | |
1854 | Desc.Owner = this; | |
1855 | Desc.ShortDesc = Target.ShortDesc; | |
1856 | Desc.URI = Target.URI; | |
1857 | ||
1858 | // If we got a hit for Release, we will get one for Release.gpg too (or obscure errors), | |
1859 | // so we skip the download step and go instantly to verification | |
1860 | if (TransactionManager->IMSHit == true && RealFileExists(GetFinalFilename())) | |
1861 | { | |
1862 | Complete = true; | |
1863 | Status = StatDone; | |
1864 | PartialFile = DestFile = GetFinalFilename(); | |
1865 | MetaIndexFileSignature = DestFile; | |
1866 | MetaIndex->QueueForSignatureVerify(this, MetaIndex->DestFile, DestFile); | |
1867 | } | |
1868 | else | |
1869 | QueueURI(Desc); | |
1870 | } | |
1871 | /*}}}*/ | |
1872 | pkgAcqMetaSig::~pkgAcqMetaSig() /*{{{*/ | |
1873 | { | |
1874 | } | |
1875 | /*}}}*/ | |
1876 | // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/ | |
1877 | std::string pkgAcqMetaSig::Custom600Headers() const | |
1878 | { | |
1879 | std::string Header = pkgAcqTransactionItem::Custom600Headers(); | |
1880 | std::string const key = TransactionManager->MetaIndexParser->GetSignedBy(); | |
1881 | if (key.empty() == false) | |
1882 | Header += "\nSigned-By: " + key; | |
1883 | return Header; | |
1884 | } | |
1885 | /*}}}*/ | |
1886 | // AcqMetaSig::Done - The signature was downloaded/verified /*{{{*/ | |
1887 | void pkgAcqMetaSig::Done(string const &Message, HashStringList const &Hashes, | |
1888 | pkgAcquire::MethodConfig const * const Cfg) | |
1889 | { | |
1890 | if (MetaIndexFileSignature.empty() == false) | |
1891 | { | |
1892 | DestFile = MetaIndexFileSignature; | |
1893 | MetaIndexFileSignature.clear(); | |
1894 | } | |
1895 | Item::Done(Message, Hashes, Cfg); | |
1896 | ||
1897 | if(MetaIndex->AuthPass == false) | |
1898 | { | |
1899 | if(MetaIndex->CheckDownloadDone(this, Message, Hashes) == true) | |
1900 | { | |
1901 | // destfile will be modified to point to MetaIndexFile for the | |
1902 | // gpgv method, so we need to save it here | |
1903 | MetaIndexFileSignature = DestFile; | |
1904 | MetaIndex->QueueForSignatureVerify(this, MetaIndex->DestFile, DestFile); | |
1905 | } | |
1906 | return; | |
1907 | } | |
1908 | else if(MetaIndex->CheckAuthDone(Message) == true) | |
1909 | { | |
1910 | auto const Releasegpg = GetFinalFilename(); | |
1911 | auto const Release = MetaIndex->GetFinalFilename(); | |
1912 | // if this is an IMS-Hit on Release ensure we also have the the Release.gpg file stored | |
1913 | // (previously an unknown pubkey) – but only if the Release file exists locally (unlikely | |
1914 | // event of InRelease removed from the mirror causing fallback but still an IMS-Hit) | |
1915 | if (TransactionManager->IMSHit == false || | |
1916 | (FileExists(Releasegpg) == false && FileExists(Release) == true)) | |
1917 | { | |
1918 | TransactionManager->TransactionStageCopy(this, DestFile, Releasegpg); | |
1919 | TransactionManager->TransactionStageCopy(MetaIndex, MetaIndex->DestFile, Release); | |
1920 | } | |
1921 | } | |
1922 | else if (MetaIndex->Status != StatAuthError) | |
1923 | { | |
1924 | std::string const FinalFile = MetaIndex->GetFinalFilename(); | |
1925 | if (TransactionManager->IMSHit == false) | |
1926 | TransactionManager->TransactionStageCopy(MetaIndex, MetaIndex->DestFile, FinalFile); | |
1927 | else | |
1928 | TransactionManager->TransactionStageCopy(MetaIndex, FinalFile, FinalFile); | |
1929 | } | |
1930 | } | |
1931 | /*}}}*/ | |
1932 | void pkgAcqMetaSig::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)/*{{{*/ | |
1933 | { | |
1934 | Item::Failed(Message,Cnf); | |
1935 | ||
1936 | // check if we need to fail at this point | |
1937 | if (MetaIndex->AuthPass == true && MetaIndex->CheckStopAuthentication(this, Message)) | |
1938 | return; | |
1939 | ||
1940 | // ensures that a Release.gpg file in the lists/ is removed by the transaction | |
1941 | TransactionManager->TransactionStageRemoval(this, DestFile); | |
1942 | ||
1943 | // only allow going further if the user explicitly wants it | |
1944 | if (AllowInsecureRepositories(InsecureType::UNSIGNED, MetaIndex->Target.Description, TransactionManager->MetaIndexParser, TransactionManager, this) == true) | |
1945 | { | |
1946 | string const FinalRelease = MetaIndex->GetFinalFilename(); | |
1947 | string const FinalInRelease = TransactionManager->GetFinalFilename(); | |
1948 | LoadLastMetaIndexParser(TransactionManager, FinalRelease, FinalInRelease); | |
1949 | ||
1950 | // we parse the indexes here because at this point the user wanted | |
1951 | // a repository that may potentially harm him | |
1952 | bool const GoodLoad = TransactionManager->MetaIndexParser->Load(MetaIndex->DestFile, &ErrorText); | |
1953 | if (MetaIndex->VerifyVendor(Message) == false) | |
1954 | /* expired Release files are still a problem you need extra force for */; | |
1955 | else | |
1956 | TransactionManager->QueueIndexes(GoodLoad); | |
1957 | ||
1958 | TransactionManager->TransactionStageCopy(MetaIndex, MetaIndex->DestFile, FinalRelease); | |
1959 | } | |
1960 | else if (TransactionManager->IMSHit == false) | |
1961 | Rename(MetaIndex->DestFile, MetaIndex->DestFile + ".FAILED"); | |
1962 | ||
1963 | // FIXME: this is used often (e.g. in pkgAcqIndexTrans) so refactor | |
1964 | if (Cnf->LocalOnly == true || | |
1965 | StringToBool(LookupTag(Message,"Transient-Failure"),false) == false) | |
1966 | { | |
1967 | // Ignore this | |
1968 | Status = StatDone; | |
1969 | } | |
1970 | } | |
1971 | /*}}}*/ | |
1972 | ||
1973 | ||
1974 | // AcqBaseIndex - Constructor /*{{{*/ | |
1975 | pkgAcqBaseIndex::pkgAcqBaseIndex(pkgAcquire * const Owner, | |
1976 | pkgAcqMetaClearSig * const TransactionManager, | |
1977 | IndexTarget const &Target) | |
1978 | : pkgAcqTransactionItem(Owner, TransactionManager, Target), d(NULL) | |
1979 | { | |
1980 | } | |
1981 | /*}}}*/ | |
1982 | void pkgAcqBaseIndex::Failed(std::string const &Message,pkgAcquire::MethodConfig const * const Cnf)/*{{{*/ | |
1983 | { | |
1984 | pkgAcquire::Item::Failed(Message, Cnf); | |
1985 | if (Status != StatAuthError) | |
1986 | return; | |
1987 | ||
1988 | ErrorText.append("Release file created at: "); | |
1989 | auto const timespec = TransactionManager->MetaIndexParser->GetDate(); | |
1990 | if (timespec == 0) | |
1991 | ErrorText.append("<unknown>"); | |
1992 | else | |
1993 | ErrorText.append(TimeRFC1123(timespec, true)); | |
1994 | ErrorText.append("\n"); | |
1995 | } | |
1996 | /*}}}*/ | |
1997 | pkgAcqBaseIndex::~pkgAcqBaseIndex() {} | |
1998 | ||
1999 | // AcqDiffIndex::AcqDiffIndex - Constructor /*{{{*/ | |
2000 | // --------------------------------------------------------------------- | |
2001 | /* Get the DiffIndex file first and see if there are patches available | |
2002 | * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the | |
2003 | * patches. If anything goes wrong in that process, it will fall back to | |
2004 | * the original packages file | |
2005 | */ | |
2006 | pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire * const Owner, | |
2007 | pkgAcqMetaClearSig * const TransactionManager, | |
2008 | IndexTarget const &Target) | |
2009 | : pkgAcqIndex(Owner, TransactionManager, Target, true), d(NULL), diffs(NULL) | |
2010 | { | |
2011 | // FIXME: Magic number as an upper bound on pdiffs we will reasonably acquire | |
2012 | ExpectedAdditionalItems = 40; | |
2013 | Debug = _config->FindB("Debug::pkgAcquire::Diffs",false); | |
2014 | ||
2015 | CompressionExtensions.clear(); | |
2016 | { | |
2017 | std::vector<std::string> types = APT::Configuration::getCompressionTypes(); | |
2018 | if (types.empty() == false) | |
2019 | { | |
2020 | std::ostringstream os; | |
2021 | std::copy_if(types.begin(), types.end()-1, std::ostream_iterator<std::string>(os, " "), [&](std::string const type) { | |
2022 | if (type == "uncompressed") | |
2023 | return true; | |
2024 | return TransactionManager->MetaIndexParser->Exists(GetDiffIndexFileName(Target.MetaKey) + '.' + type); | |
2025 | }); | |
2026 | os << *types.rbegin(); | |
2027 | CompressionExtensions = os.str(); | |
2028 | } | |
2029 | } | |
2030 | if (Target.Option(IndexTarget::COMPRESSIONTYPES).find("by-hash") != std::string::npos) | |
2031 | CompressionExtensions = "by-hash " + CompressionExtensions; | |
2032 | Init(GetDiffIndexURI(Target), GetDiffIndexFileName(Target.Description), Target.ShortDesc); | |
2033 | ||
2034 | if(Debug) | |
2035 | std::clog << "pkgAcqDiffIndex: " << Desc.URI << std::endl; | |
2036 | } | |
2037 | /*}}}*/ | |
2038 | void pkgAcqDiffIndex::QueueOnIMSHit() const /*{{{*/ | |
2039 | { | |
2040 | // list cleanup needs to know that this file as well as the already | |
2041 | // present index is ours, so we create an empty diff to save it for us | |
2042 | new pkgAcqIndexDiffs(Owner, TransactionManager, Target); | |
2043 | } | |
2044 | /*}}}*/ | |
2045 | static bool RemoveFileForBootstrapLinking(bool const Debug, std::string const &For, std::string const &Boot)/*{{{*/ | |
2046 | { | |
2047 | if (FileExists(Boot) && RemoveFile("Bootstrap-linking", Boot) == false) | |
2048 | { | |
2049 | if (Debug) | |
2050 | std::clog << "Bootstrap-linking for patching " << For | |
2051 | << " by removing stale " << Boot << " failed!" << std::endl; | |
2052 | return false; | |
2053 | } | |
2054 | return true; | |
2055 | } | |
2056 | /*}}}*/ | |
2057 | bool pkgAcqDiffIndex::ParseDiffIndex(string const &IndexDiffFile) /*{{{*/ | |
2058 | { | |
2059 | ExpectedAdditionalItems = 0; | |
2060 | // failing here is fine: our caller will take care of trying to | |
2061 | // get the complete file if patching fails | |
2062 | if(Debug) | |
2063 | std::clog << "pkgAcqDiffIndex::ParseIndexDiff() " << IndexDiffFile | |
2064 | << std::endl; | |
2065 | ||
2066 | FileFd Fd(IndexDiffFile, FileFd::ReadOnly, FileFd::Extension); | |
2067 | pkgTagFile TF(&Fd); | |
2068 | if (Fd.IsOpen() == false || Fd.Failed()) | |
2069 | return false; | |
2070 | ||
2071 | pkgTagSection Tags; | |
2072 | if(unlikely(TF.Step(Tags) == false)) | |
2073 | return false; | |
2074 | ||
2075 | HashStringList ServerHashes; | |
2076 | unsigned long long ServerSize = 0; | |
2077 | ||
2078 | auto const &posix = std::locale::classic(); | |
2079 | for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type) | |
2080 | { | |
2081 | std::string tagname = *type; | |
2082 | tagname.append("-Current"); | |
2083 | std::string const tmp = Tags.FindS(tagname.c_str()); | |
2084 | if (tmp.empty() == true) | |
2085 | continue; | |
2086 | ||
2087 | string hash; | |
2088 | unsigned long long size; | |
2089 | std::stringstream ss(tmp); | |
2090 | ss.imbue(posix); | |
2091 | ss >> hash >> size; | |
2092 | if (unlikely(hash.empty() == true)) | |
2093 | continue; | |
2094 | if (unlikely(ServerSize != 0 && ServerSize != size)) | |
2095 | continue; | |
2096 | ServerHashes.push_back(HashString(*type, hash)); | |
2097 | ServerSize = size; | |
2098 | } | |
2099 | ||
2100 | if (ServerHashes.usable() == false) | |
2101 | { | |
2102 | if (Debug == true) | |
2103 | std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": Did not find a good hashsum in the index" << std::endl; | |
2104 | return false; | |
2105 | } | |
2106 | ||
2107 | std::string const CurrentPackagesFile = GetFinalFileNameFromURI(Target.URI); | |
2108 | HashStringList const TargetFileHashes = GetExpectedHashesFor(Target.MetaKey); | |
2109 | if (TargetFileHashes.usable() == false || ServerHashes != TargetFileHashes) | |
2110 | { | |
2111 | if (Debug == true) | |
2112 | { | |
2113 | std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": Index has different hashes than parser, probably older, so fail pdiffing" << std::endl; | |
2114 | printHashSumComparison(CurrentPackagesFile, ServerHashes, TargetFileHashes); | |
2115 | } | |
2116 | return false; | |
2117 | } | |
2118 | ||
2119 | HashStringList LocalHashes; | |
2120 | // try avoiding calculating the hash here as this is costly | |
2121 | if (TransactionManager->LastMetaIndexParser != NULL) | |
2122 | LocalHashes = GetExpectedHashesFromFor(TransactionManager->LastMetaIndexParser, Target.MetaKey); | |
2123 | if (LocalHashes.usable() == false) | |
2124 | { | |
2125 | FileFd fd(CurrentPackagesFile, FileFd::ReadOnly, FileFd::Auto); | |
2126 | Hashes LocalHashesCalc(ServerHashes); | |
2127 | LocalHashesCalc.AddFD(fd); | |
2128 | LocalHashes = LocalHashesCalc.GetHashStringList(); | |
2129 | } | |
2130 | ||
2131 | if (ServerHashes == LocalHashes) | |
2132 | { | |
2133 | // we have the same sha1 as the server so we are done here | |
2134 | if(Debug) | |
2135 | std::clog << "pkgAcqDiffIndex: Package file " << CurrentPackagesFile << " is up-to-date" << std::endl; | |
2136 | QueueOnIMSHit(); | |
2137 | return true; | |
2138 | } | |
2139 | ||
2140 | if(Debug) | |
2141 | std::clog << "Server-Current: " << ServerHashes.find(NULL)->toStr() << " and we start at " | |
2142 | << CurrentPackagesFile << " " << LocalHashes.FileSize() << " " << LocalHashes.find(NULL)->toStr() << std::endl; | |
2143 | ||
2144 | // historically, older hashes have more info than newer ones, so start | |
2145 | // collecting with older ones first to avoid implementing complicated | |
2146 | // information merging techniques… a failure is after all always | |
2147 | // recoverable with a complete file and hashes aren't changed that often. | |
2148 | std::vector<char const *> types; | |
2149 | for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type) | |
2150 | types.push_back(*type); | |
2151 | ||
2152 | // parse all of (provided) history | |
2153 | vector<DiffInfo> available_patches; | |
2154 | bool firstAcceptedHashes = true; | |
2155 | for (auto type = types.crbegin(); type != types.crend(); ++type) | |
2156 | { | |
2157 | if (LocalHashes.find(*type) == NULL) | |
2158 | continue; | |
2159 | ||
2160 | std::string tagname = *type; | |
2161 | tagname.append("-History"); | |
2162 | std::string const tmp = Tags.FindS(tagname.c_str()); | |
2163 | if (tmp.empty() == true) | |
2164 | continue; | |
2165 | ||
2166 | string hash, filename; | |
2167 | unsigned long long size; | |
2168 | std::stringstream ss(tmp); | |
2169 | ss.imbue(posix); | |
2170 | ||
2171 | while (ss >> hash >> size >> filename) | |
2172 | { | |
2173 | if (unlikely(hash.empty() == true || filename.empty() == true)) | |
2174 | continue; | |
2175 | ||
2176 | // see if we have a record for this file already | |
2177 | std::vector<DiffInfo>::iterator cur = available_patches.begin(); | |
2178 | for (; cur != available_patches.end(); ++cur) | |
2179 | { | |
2180 | if (cur->file != filename) | |
2181 | continue; | |
2182 | cur->result_hashes.push_back(HashString(*type, hash)); | |
2183 | break; | |
2184 | } | |
2185 | if (cur != available_patches.end()) | |
2186 | continue; | |
2187 | if (firstAcceptedHashes == true) | |
2188 | { | |
2189 | DiffInfo next; | |
2190 | next.file = filename; | |
2191 | next.result_hashes.push_back(HashString(*type, hash)); | |
2192 | next.result_hashes.FileSize(size); | |
2193 | available_patches.push_back(next); | |
2194 | } | |
2195 | else | |
2196 | { | |
2197 | if (Debug == true) | |
2198 | std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": File " << filename | |
2199 | << " wasn't in the list for the first parsed hash! (history)" << std::endl; | |
2200 | break; | |
2201 | } | |
2202 | } | |
2203 | firstAcceptedHashes = false; | |
2204 | } | |
2205 | ||
2206 | if (unlikely(available_patches.empty() == true)) | |
2207 | { | |
2208 | if (Debug) | |
2209 | std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": " | |
2210 | << "Couldn't find any patches for the patch series." << std::endl; | |
2211 | return false; | |
2212 | } | |
2213 | ||
2214 | for (auto type = types.crbegin(); type != types.crend(); ++type) | |
2215 | { | |
2216 | if (LocalHashes.find(*type) == NULL) | |
2217 | continue; | |
2218 | ||
2219 | std::string tagname = *type; | |
2220 | tagname.append("-Patches"); | |
2221 | std::string const tmp = Tags.FindS(tagname.c_str()); | |
2222 | if (tmp.empty() == true) | |
2223 | continue; | |
2224 | ||
2225 | string hash, filename; | |
2226 | unsigned long long size; | |
2227 | std::stringstream ss(tmp); | |
2228 | ss.imbue(posix); | |
2229 | ||
2230 | while (ss >> hash >> size >> filename) | |
2231 | { | |
2232 | if (unlikely(hash.empty() == true || filename.empty() == true)) | |
2233 | continue; | |
2234 | ||
2235 | // see if we have a record for this file already | |
2236 | std::vector<DiffInfo>::iterator cur = available_patches.begin(); | |
2237 | for (; cur != available_patches.end(); ++cur) | |
2238 | { | |
2239 | if (cur->file != filename) | |
2240 | continue; | |
2241 | if (cur->patch_hashes.empty()) | |
2242 | cur->patch_hashes.FileSize(size); | |
2243 | cur->patch_hashes.push_back(HashString(*type, hash)); | |
2244 | break; | |
2245 | } | |
2246 | if (cur != available_patches.end()) | |
2247 | continue; | |
2248 | if (Debug == true) | |
2249 | std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": File " << filename | |
2250 | << " wasn't in the list for the first parsed hash! (patches)" << std::endl; | |
2251 | break; | |
2252 | } | |
2253 | } | |
2254 | ||
2255 | for (auto type = types.crbegin(); type != types.crend(); ++type) | |
2256 | { | |
2257 | std::string tagname = *type; | |
2258 | tagname.append("-Download"); | |
2259 | std::string const tmp = Tags.FindS(tagname.c_str()); | |
2260 | if (tmp.empty() == true) | |
2261 | continue; | |
2262 | ||
2263 | string hash, filename; | |
2264 | unsigned long long size; | |
2265 | std::stringstream ss(tmp); | |
2266 | ss.imbue(posix); | |
2267 | ||
2268 | // FIXME: all of pdiff supports only .gz compressed patches | |
2269 | while (ss >> hash >> size >> filename) | |
2270 | { | |
2271 | if (unlikely(hash.empty() == true || filename.empty() == true)) | |
2272 | continue; | |
2273 | if (unlikely(APT::String::Endswith(filename, ".gz") == false)) | |
2274 | continue; | |
2275 | filename.erase(filename.length() - 3); | |
2276 | ||
2277 | // see if we have a record for this file already | |
2278 | std::vector<DiffInfo>::iterator cur = available_patches.begin(); | |
2279 | for (; cur != available_patches.end(); ++cur) | |
2280 | { | |
2281 | if (cur->file != filename) | |
2282 | continue; | |
2283 | if (cur->download_hashes.empty()) | |
2284 | cur->download_hashes.FileSize(size); | |
2285 | cur->download_hashes.push_back(HashString(*type, hash)); | |
2286 | break; | |
2287 | } | |
2288 | if (cur != available_patches.end()) | |
2289 | continue; | |
2290 | if (Debug == true) | |
2291 | std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": File " << filename | |
2292 | << " wasn't in the list for the first parsed hash! (download)" << std::endl; | |
2293 | break; | |
2294 | } | |
2295 | } | |
2296 | ||
2297 | ||
2298 | bool foundStart = false; | |
2299 | for (std::vector<DiffInfo>::iterator cur = available_patches.begin(); | |
2300 | cur != available_patches.end(); ++cur) | |
2301 | { | |
2302 | if (LocalHashes != cur->result_hashes) | |
2303 | continue; | |
2304 | ||
2305 | available_patches.erase(available_patches.begin(), cur); | |
2306 | foundStart = true; | |
2307 | break; | |
2308 | } | |
2309 | ||
2310 | if (foundStart == false || unlikely(available_patches.empty() == true)) | |
2311 | { | |
2312 | if (Debug) | |
2313 | std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": " | |
2314 | << "Couldn't find the start of the patch series." << std::endl; | |
2315 | return false; | |
2316 | } | |
2317 | ||
2318 | for (auto const &patch: available_patches) | |
2319 | if (patch.result_hashes.usable() == false || | |
2320 | patch.patch_hashes.usable() == false || | |
2321 | patch.download_hashes.usable() == false) | |
2322 | { | |
2323 | if (Debug) | |
2324 | std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": provides no usable hashes for " << patch.file | |
2325 | << " so fallback to complete download" << std::endl; | |
2326 | return false; | |
2327 | } | |
2328 | ||
2329 | // patching with too many files is rather slow compared to a fast download | |
2330 | unsigned long const fileLimit = _config->FindI("Acquire::PDiffs::FileLimit", 0); | |
2331 | if (fileLimit != 0 && fileLimit < available_patches.size()) | |
2332 | { | |
2333 | if (Debug) | |
2334 | std::clog << "Need " << available_patches.size() << " diffs (Limit is " << fileLimit | |
2335 | << ") so fallback to complete download" << std::endl; | |
2336 | return false; | |
2337 | } | |
2338 | ||
2339 | // calculate the size of all patches we have to get | |
2340 | unsigned short const sizeLimitPercent = _config->FindI("Acquire::PDiffs::SizeLimit", 100); | |
2341 | if (sizeLimitPercent > 0) | |
2342 | { | |
2343 | unsigned long long downloadSize = std::accumulate(available_patches.begin(), | |
2344 | available_patches.end(), 0llu, [](unsigned long long const T, DiffInfo const &I) { | |
2345 | return T + I.download_hashes.FileSize(); | |
2346 | }); | |
2347 | if (downloadSize != 0) | |
2348 | { | |
2349 | unsigned long long downloadSizeIdx = 0; | |
2350 | auto const types = VectorizeString(Target.Option(IndexTarget::COMPRESSIONTYPES), ' '); | |
2351 | for (auto const &t : types) | |
2352 | { | |
2353 | std::string MetaKey = Target.MetaKey; | |
2354 | if (t != "uncompressed") | |
2355 | MetaKey += '.' + t; | |
2356 | HashStringList const hsl = GetExpectedHashesFor(MetaKey); | |
2357 | if (unlikely(hsl.usable() == false)) | |
2358 | continue; | |
2359 | downloadSizeIdx = hsl.FileSize(); | |
2360 | break; | |
2361 | } | |
2362 | unsigned long long const sizeLimit = downloadSizeIdx * sizeLimitPercent; | |
2363 | if ((sizeLimit/100) < downloadSize) | |
2364 | { | |
2365 | if (Debug) | |
2366 | std::clog << "Need " << downloadSize << " compressed bytes (Limit is " << (sizeLimit/100) << ", " | |
2367 | << "original is " << downloadSizeIdx << ") so fallback to complete download" << std::endl; | |
2368 | return false; | |
2369 | } | |
2370 | } | |
2371 | } | |
2372 | ||
2373 | // we have something, queue the diffs | |
2374 | string::size_type const last_space = Description.rfind(" "); | |
2375 | if(last_space != string::npos) | |
2376 | Description.erase(last_space, Description.size()-last_space); | |
2377 | ||
2378 | /* decide if we should download patches one by one or in one go: | |
2379 | The first is good if the server merges patches, but many don't so client | |
2380 | based merging can be attempt in which case the second is better. | |
2381 | "bad things" will happen if patches are merged on the server, | |
2382 | but client side merging is attempt as well */ | |
2383 | bool pdiff_merge = _config->FindB("Acquire::PDiffs::Merge", true); | |
2384 | if (pdiff_merge == true) | |
2385 | { | |
2386 | // reprepro adds this flag if it has merged patches on the server | |
2387 | std::string const precedence = Tags.FindS("X-Patch-Precedence"); | |
2388 | pdiff_merge = (precedence != "merged"); | |
2389 | } | |
2390 | ||
2391 | // clean the plate | |
2392 | { | |
2393 | std::string const Final = GetExistingFilename(CurrentPackagesFile); | |
2394 | if (unlikely(Final.empty())) // because we wouldn't be called in such a case | |
2395 | return false; | |
2396 | std::string const PartialFile = GetPartialFileNameFromURI(Target.URI); | |
2397 | std::string const PatchedFile = GetKeepCompressedFileName(PartialFile + "-patched", Target); | |
2398 | if (RemoveFileForBootstrapLinking(Debug, CurrentPackagesFile, PartialFile) == false || | |
2399 | RemoveFileForBootstrapLinking(Debug, CurrentPackagesFile, PatchedFile) == false) | |
2400 | return false; | |
2401 | for (auto const &ext : APT::Configuration::getCompressorExtensions()) | |
2402 | { | |
2403 | if (RemoveFileForBootstrapLinking(Debug, CurrentPackagesFile, PartialFile + ext) == false || | |
2404 | RemoveFileForBootstrapLinking(Debug, CurrentPackagesFile, PatchedFile + ext) == false) | |
2405 | return false; | |
2406 | } | |
2407 | std::string const Ext = Final.substr(CurrentPackagesFile.length()); | |
2408 | std::string const Partial = PartialFile + Ext; | |
2409 | if (symlink(Final.c_str(), Partial.c_str()) != 0) | |
2410 | { | |
2411 | if (Debug) | |
2412 | std::clog << "Bootstrap-linking for patching " << CurrentPackagesFile | |
2413 | << " by linking " << Final << " to " << Partial << " failed!" << std::endl; | |
2414 | return false; | |
2415 | } | |
2416 | } | |
2417 | ||
2418 | if (pdiff_merge == false) | |
2419 | new pkgAcqIndexDiffs(Owner, TransactionManager, Target, available_patches); | |
2420 | else | |
2421 | { | |
2422 | diffs = new std::vector<pkgAcqIndexMergeDiffs*>(available_patches.size()); | |
2423 | for(size_t i = 0; i < available_patches.size(); ++i) | |
2424 | (*diffs)[i] = new pkgAcqIndexMergeDiffs(Owner, TransactionManager, | |
2425 | Target, | |
2426 | available_patches[i], | |
2427 | diffs); | |
2428 | } | |
2429 | ||
2430 | Complete = false; | |
2431 | Status = StatDone; | |
2432 | Dequeue(); | |
2433 | return true; | |
2434 | } | |
2435 | /*}}}*/ | |
2436 | void pkgAcqDiffIndex::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)/*{{{*/ | |
2437 | { | |
2438 | if (CommonFailed(GetDiffIndexURI(Target), GetDiffIndexFileName(Target.Description), Message, Cnf)) | |
2439 | return; | |
2440 | ||
2441 | Status = StatDone; | |
2442 | ExpectedAdditionalItems = 0; | |
2443 | ||
2444 | if(Debug) | |
2445 | std::clog << "pkgAcqDiffIndex failed: " << Desc.URI << " with " << Message << std::endl | |
2446 | << "Falling back to normal index file acquire" << std::endl; | |
2447 | ||
2448 | new pkgAcqIndex(Owner, TransactionManager, Target); | |
2449 | } | |
2450 | /*}}}*/ | |
2451 | void pkgAcqDiffIndex::Done(string const &Message,HashStringList const &Hashes, /*{{{*/ | |
2452 | pkgAcquire::MethodConfig const * const Cnf) | |
2453 | { | |
2454 | if(Debug) | |
2455 | std::clog << "pkgAcqDiffIndex::Done(): " << Desc.URI << std::endl; | |
2456 | ||
2457 | Item::Done(Message, Hashes, Cnf); | |
2458 | ||
2459 | string const FinalFile = GetFinalFilename(); | |
2460 | if(StringToBool(LookupTag(Message,"IMS-Hit"),false)) | |
2461 | DestFile = FinalFile; | |
2462 | ||
2463 | if(ParseDiffIndex(DestFile) == false) | |
2464 | { | |
2465 | Failed("Message: Couldn't parse pdiff index", Cnf); | |
2466 | // queue for final move - this should happen even if we fail | |
2467 | // while parsing (e.g. on sizelimit) and download the complete file. | |
2468 | TransactionManager->TransactionStageCopy(this, DestFile, FinalFile); | |
2469 | return; | |
2470 | } | |
2471 | ||
2472 | TransactionManager->TransactionStageCopy(this, DestFile, FinalFile); | |
2473 | ||
2474 | Complete = true; | |
2475 | Status = StatDone; | |
2476 | Dequeue(); | |
2477 | ||
2478 | return; | |
2479 | } | |
2480 | /*}}}*/ | |
2481 | pkgAcqDiffIndex::~pkgAcqDiffIndex() | |
2482 | { | |
2483 | if (diffs != NULL) | |
2484 | delete diffs; | |
2485 | } | |
2486 | ||
2487 | // AcqIndexDiffs::AcqIndexDiffs - Constructor /*{{{*/ | |
2488 | // --------------------------------------------------------------------- | |
2489 | /* The package diff is added to the queue. one object is constructed | |
2490 | * for each diff and the index | |
2491 | */ | |
2492 | pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire * const Owner, | |
2493 | pkgAcqMetaClearSig * const TransactionManager, | |
2494 | IndexTarget const &Target, | |
2495 | vector<DiffInfo> const &diffs) | |
2496 | : pkgAcqBaseIndex(Owner, TransactionManager, Target), d(NULL), | |
2497 | available_patches(diffs) | |
2498 | { | |
2499 | DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target); | |
2500 | ||
2501 | Debug = _config->FindB("Debug::pkgAcquire::Diffs",false); | |
2502 | ||
2503 | Desc.Owner = this; | |
2504 | Description = Target.Description; | |
2505 | Desc.ShortDesc = Target.ShortDesc; | |
2506 | ||
2507 | if(available_patches.empty() == true) | |
2508 | { | |
2509 | // we are done (yeah!), check hashes against the final file | |
2510 | DestFile = GetKeepCompressedFileName(GetFinalFileNameFromURI(Target.URI), Target); | |
2511 | Finish(true); | |
2512 | } | |
2513 | else | |
2514 | { | |
2515 | State = StateFetchDiff; | |
2516 | QueueNextDiff(); | |
2517 | } | |
2518 | } | |
2519 | /*}}}*/ | |
2520 | void pkgAcqIndexDiffs::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)/*{{{*/ | |
2521 | { | |
2522 | pkgAcqBaseIndex::Failed(Message,Cnf); | |
2523 | Status = StatDone; | |
2524 | ||
2525 | DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target); | |
2526 | if(Debug) | |
2527 | std::clog << "pkgAcqIndexDiffs failed: " << Desc.URI << " with " << Message << std::endl | |
2528 | << "Falling back to normal index file acquire " << std::endl; | |
2529 | RenameOnError(PDiffError); | |
2530 | std::string const patchname = GetDiffsPatchFileName(DestFile); | |
2531 | if (RealFileExists(patchname)) | |
2532 | Rename(patchname, patchname + ".FAILED"); | |
2533 | std::string const UnpatchedFile = GetExistingFilename(GetPartialFileNameFromURI(Target.URI)); | |
2534 | if (UnpatchedFile.empty() == false && FileExists(UnpatchedFile)) | |
2535 | Rename(UnpatchedFile, UnpatchedFile + ".FAILED"); | |
2536 | new pkgAcqIndex(Owner, TransactionManager, Target); | |
2537 | Finish(); | |
2538 | } | |
2539 | /*}}}*/ | |
2540 | // Finish - helper that cleans the item out of the fetcher queue /*{{{*/ | |
2541 | void pkgAcqIndexDiffs::Finish(bool allDone) | |
2542 | { | |
2543 | if(Debug) | |
2544 | std::clog << "pkgAcqIndexDiffs::Finish(): " | |
2545 | << allDone << " " | |
2546 | << Desc.URI << std::endl; | |
2547 | ||
2548 | // we restore the original name, this is required, otherwise | |
2549 | // the file will be cleaned | |
2550 | if(allDone) | |
2551 | { | |
2552 | std::string const Final = GetKeepCompressedFileName(GetFinalFilename(), Target); | |
2553 | TransactionManager->TransactionStageCopy(this, DestFile, Final); | |
2554 | ||
2555 | // this is for the "real" finish | |
2556 | Complete = true; | |
2557 | Status = StatDone; | |
2558 | Dequeue(); | |
2559 | if(Debug) | |
2560 | std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl; | |
2561 | return; | |
2562 | } | |
2563 | else | |
2564 | DestFile.clear(); | |
2565 | ||
2566 | if(Debug) | |
2567 | std::clog << "Finishing: " << Desc.URI << std::endl; | |
2568 | Complete = false; | |
2569 | Status = StatDone; | |
2570 | Dequeue(); | |
2571 | return; | |
2572 | } | |
2573 | /*}}}*/ | |
2574 | bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/ | |
2575 | { | |
2576 | // calc sha1 of the just patched file | |
2577 | std::string const PartialFile = GetExistingFilename(GetPartialFileNameFromURI(Target.URI)); | |
2578 | if(unlikely(PartialFile.empty())) | |
2579 | { | |
2580 | Failed("Message: The file " + GetPartialFileNameFromURI(Target.URI) + " isn't available", NULL); | |
2581 | return false; | |
2582 | } | |
2583 | ||
2584 | FileFd fd(PartialFile, FileFd::ReadOnly, FileFd::Extension); | |
2585 | Hashes LocalHashesCalc; | |
2586 | LocalHashesCalc.AddFD(fd); | |
2587 | HashStringList const LocalHashes = LocalHashesCalc.GetHashStringList(); | |
2588 | ||
2589 | if(Debug) | |
2590 | std::clog << "QueueNextDiff: " << PartialFile << " (" << LocalHashes.find(NULL)->toStr() << ")" << std::endl; | |
2591 | ||
2592 | HashStringList const TargetFileHashes = GetExpectedHashesFor(Target.MetaKey); | |
2593 | if (unlikely(LocalHashes.usable() == false || TargetFileHashes.usable() == false)) | |
2594 | { | |
2595 | Failed("Local/Expected hashes are not usable for " + PartialFile, NULL); | |
2596 | return false; | |
2597 | } | |
2598 | ||
2599 | // final file reached before all patches are applied | |
2600 | if(LocalHashes == TargetFileHashes) | |
2601 | { | |
2602 | Finish(true); | |
2603 | return true; | |
2604 | } | |
2605 | ||
2606 | // remove all patches until the next matching patch is found | |
2607 | // this requires the Index file to be ordered | |
2608 | available_patches.erase(available_patches.begin(), | |
2609 | std::find_if(available_patches.begin(), available_patches.end(), [&](DiffInfo const &I) { | |
2610 | return I.result_hashes == LocalHashes; | |
2611 | })); | |
2612 | ||
2613 | // error checking and falling back if no patch was found | |
2614 | if(available_patches.empty() == true) | |
2615 | { | |
2616 | Failed("No patches left to reach target for " + PartialFile, NULL); | |
2617 | return false; | |
2618 | } | |
2619 | ||
2620 | // queue the right diff | |
2621 | Desc.URI = Target.URI + ".diff/" + available_patches[0].file + ".gz"; | |
2622 | Desc.Description = Description + " " + available_patches[0].file + string(".pdiff"); | |
2623 | DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI + ".diff/" + available_patches[0].file), Target); | |
2624 | ||
2625 | if(Debug) | |
2626 | std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl; | |
2627 | ||
2628 | QueueURI(Desc); | |
2629 | ||
2630 | return true; | |
2631 | } | |
2632 | /*}}}*/ | |
2633 | void pkgAcqIndexDiffs::Done(string const &Message, HashStringList const &Hashes, /*{{{*/ | |
2634 | pkgAcquire::MethodConfig const * const Cnf) | |
2635 | { | |
2636 | if (Debug) | |
2637 | std::clog << "pkgAcqIndexDiffs::Done(): " << Desc.URI << std::endl; | |
2638 | ||
2639 | Item::Done(Message, Hashes, Cnf); | |
2640 | ||
2641 | std::string const UncompressedUnpatchedFile = GetPartialFileNameFromURI(Target.URI); | |
2642 | std::string const UnpatchedFile = GetExistingFilename(UncompressedUnpatchedFile); | |
2643 | std::string const PatchFile = GetDiffsPatchFileName(UnpatchedFile); | |
2644 | std::string const PatchedFile = GetKeepCompressedFileName(UncompressedUnpatchedFile, Target); | |
2645 | ||
2646 | switch (State) | |
2647 | { | |
2648 | // success in downloading a diff, enter ApplyDiff state | |
2649 | case StateFetchDiff: | |
2650 | Rename(DestFile, PatchFile); | |
2651 | DestFile = GetKeepCompressedFileName(UncompressedUnpatchedFile + "-patched", Target); | |
2652 | if(Debug) | |
2653 | std::clog << "Sending to rred method: " << UnpatchedFile << std::endl; | |
2654 | State = StateApplyDiff; | |
2655 | Local = true; | |
2656 | Desc.URI = "rred:" + UnpatchedFile; | |
2657 | QueueURI(Desc); | |
2658 | SetActiveSubprocess("rred"); | |
2659 | return; | |
2660 | // success in download/apply a diff, queue next (if needed) | |
2661 | case StateApplyDiff: | |
2662 | // remove the just applied patch and base file | |
2663 | available_patches.erase(available_patches.begin()); | |
2664 | RemoveFile("pkgAcqIndexDiffs::Done", PatchFile); | |
2665 | RemoveFile("pkgAcqIndexDiffs::Done", UnpatchedFile); | |
2666 | if(Debug) | |
2667 | std::clog << "Moving patched file in place: " << std::endl | |
2668 | << DestFile << " -> " << PatchedFile << std::endl; | |
2669 | Rename(DestFile, PatchedFile); | |
2670 | ||
2671 | // see if there is more to download | |
2672 | if(available_patches.empty() == false) | |
2673 | { | |
2674 | new pkgAcqIndexDiffs(Owner, TransactionManager, Target, available_patches); | |
2675 | Finish(); | |
2676 | } else { | |
2677 | DestFile = PatchedFile; | |
2678 | Finish(true); | |
2679 | } | |
2680 | return; | |
2681 | } | |
2682 | } | |
2683 | /*}}}*/ | |
2684 | std::string pkgAcqIndexDiffs::Custom600Headers() const /*{{{*/ | |
2685 | { | |
2686 | if(State != StateApplyDiff) | |
2687 | return pkgAcqBaseIndex::Custom600Headers(); | |
2688 | std::ostringstream patchhashes; | |
2689 | for (auto && hs : available_patches[0].result_hashes) | |
2690 | patchhashes << "\nStart-" << hs.HashType() << "-Hash: " << hs.HashValue(); | |
2691 | for (auto && hs : available_patches[0].patch_hashes) | |
2692 | patchhashes << "\nPatch-0-" << hs.HashType() << "-Hash: " << hs.HashValue(); | |
2693 | patchhashes << pkgAcqBaseIndex::Custom600Headers(); | |
2694 | return patchhashes.str(); | |
2695 | } | |
2696 | /*}}}*/ | |
2697 | pkgAcqIndexDiffs::~pkgAcqIndexDiffs() {} | |
2698 | ||
2699 | // AcqIndexMergeDiffs::AcqIndexMergeDiffs - Constructor /*{{{*/ | |
2700 | pkgAcqIndexMergeDiffs::pkgAcqIndexMergeDiffs(pkgAcquire * const Owner, | |
2701 | pkgAcqMetaClearSig * const TransactionManager, | |
2702 | IndexTarget const &Target, | |
2703 | DiffInfo const &patch, | |
2704 | std::vector<pkgAcqIndexMergeDiffs*> const * const allPatches) | |
2705 | : pkgAcqBaseIndex(Owner, TransactionManager, Target), d(NULL), | |
2706 | patch(patch), allPatches(allPatches), State(StateFetchDiff) | |
2707 | { | |
2708 | Debug = _config->FindB("Debug::pkgAcquire::Diffs",false); | |
2709 | ||
2710 | Desc.Owner = this; | |
2711 | Description = Target.Description; | |
2712 | Desc.ShortDesc = Target.ShortDesc; | |
2713 | Desc.URI = Target.URI + ".diff/" + patch.file + ".gz"; | |
2714 | Desc.Description = Description + " " + patch.file + ".pdiff"; | |
2715 | DestFile = GetPartialFileNameFromURI(Desc.URI); | |
2716 | ||
2717 | if(Debug) | |
2718 | std::clog << "pkgAcqIndexMergeDiffs: " << Desc.URI << std::endl; | |
2719 | ||
2720 | QueueURI(Desc); | |
2721 | } | |
2722 | /*}}}*/ | |
2723 | void pkgAcqIndexMergeDiffs::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)/*{{{*/ | |
2724 | { | |
2725 | if(Debug) | |
2726 | std::clog << "pkgAcqIndexMergeDiffs failed: " << Desc.URI << " with " << Message << std::endl; | |
2727 | ||
2728 | pkgAcqBaseIndex::Failed(Message,Cnf); | |
2729 | Status = StatDone; | |
2730 | ||
2731 | // check if we are the first to fail, otherwise we are done here | |
2732 | State = StateDoneDiff; | |
2733 | for (std::vector<pkgAcqIndexMergeDiffs *>::const_iterator I = allPatches->begin(); | |
2734 | I != allPatches->end(); ++I) | |
2735 | if ((*I)->State == StateErrorDiff) | |
2736 | { | |
2737 | State = StateErrorDiff; | |
2738 | return; | |
2739 | } | |
2740 | ||
2741 | // first failure means we should fallback | |
2742 | State = StateErrorDiff; | |
2743 | if (Debug) | |
2744 | std::clog << "Falling back to normal index file acquire" << std::endl; | |
2745 | RenameOnError(PDiffError); | |
2746 | if (RealFileExists(DestFile)) | |
2747 | Rename(DestFile, DestFile + ".FAILED"); | |
2748 | std::string const UnpatchedFile = GetExistingFilename(GetPartialFileNameFromURI(Target.URI)); | |
2749 | if (UnpatchedFile.empty() == false && FileExists(UnpatchedFile)) | |
2750 | Rename(UnpatchedFile, UnpatchedFile + ".FAILED"); | |
2751 | DestFile.clear(); | |
2752 | new pkgAcqIndex(Owner, TransactionManager, Target); | |
2753 | } | |
2754 | /*}}}*/ | |
2755 | void pkgAcqIndexMergeDiffs::Done(string const &Message, HashStringList const &Hashes, /*{{{*/ | |
2756 | pkgAcquire::MethodConfig const * const Cnf) | |
2757 | { | |
2758 | if(Debug) | |
2759 | std::clog << "pkgAcqIndexMergeDiffs::Done(): " << Desc.URI << std::endl; | |
2760 | ||
2761 | Item::Done(Message, Hashes, Cnf); | |
2762 | ||
2763 | if (std::any_of(allPatches->begin(), allPatches->end(), | |
2764 | [](pkgAcqIndexMergeDiffs const * const P) { return P->State == StateErrorDiff; })) | |
2765 | { | |
2766 | if(Debug) | |
2767 | std::clog << "Another patch failed already, no point in processing this one." << std::endl; | |
2768 | State = StateErrorDiff; | |
2769 | return; | |
2770 | } | |
2771 | ||
2772 | std::string const UncompressedUnpatchedFile = GetPartialFileNameFromURI(Target.URI); | |
2773 | std::string const UnpatchedFile = GetExistingFilename(UncompressedUnpatchedFile); | |
2774 | if (UnpatchedFile.empty()) | |
2775 | { | |
2776 | _error->Fatal("Unpatched file %s doesn't exist (anymore)!", UncompressedUnpatchedFile.c_str()); | |
2777 | State = StateErrorDiff; | |
2778 | return; | |
2779 | } | |
2780 | std::string const PatchFile = GetMergeDiffsPatchFileName(UnpatchedFile, patch.file); | |
2781 | std::string const PatchedFile = GetKeepCompressedFileName(UncompressedUnpatchedFile, Target); | |
2782 | ||
2783 | switch (State) | |
2784 | { | |
2785 | case StateFetchDiff: | |
2786 | Rename(DestFile, PatchFile); | |
2787 | ||
2788 | // check if this is the last completed diff | |
2789 | State = StateDoneDiff; | |
2790 | for (std::vector<pkgAcqIndexMergeDiffs *>::const_iterator I = allPatches->begin(); | |
2791 | I != allPatches->end(); ++I) | |
2792 | if ((*I)->State != StateDoneDiff) | |
2793 | { | |
2794 | if(Debug) | |
2795 | std::clog << "Not the last done diff in the batch: " << Desc.URI << std::endl; | |
2796 | return; | |
2797 | } | |
2798 | // this is the last completed diff, so we are ready to apply now | |
2799 | DestFile = GetKeepCompressedFileName(UncompressedUnpatchedFile + "-patched", Target); | |
2800 | if(Debug) | |
2801 | std::clog << "Sending to rred method: " << UnpatchedFile << std::endl; | |
2802 | State = StateApplyDiff; | |
2803 | Local = true; | |
2804 | Desc.URI = "rred:" + UnpatchedFile; | |
2805 | QueueURI(Desc); | |
2806 | SetActiveSubprocess("rred"); | |
2807 | return; | |
2808 | case StateApplyDiff: | |
2809 | // success in download & apply all diffs, finialize and clean up | |
2810 | if(Debug) | |
2811 | std::clog << "Queue patched file in place: " << std::endl | |
2812 | << DestFile << " -> " << PatchedFile << std::endl; | |
2813 | ||
2814 | // queue for copy by the transaction manager | |
2815 | TransactionManager->TransactionStageCopy(this, DestFile, GetKeepCompressedFileName(GetFinalFilename(), Target)); | |
2816 | ||
2817 | // ensure the ed's are gone regardless of list-cleanup | |
2818 | for (std::vector<pkgAcqIndexMergeDiffs *>::const_iterator I = allPatches->begin(); | |
2819 | I != allPatches->end(); ++I) | |
2820 | RemoveFile("pkgAcqIndexMergeDiffs::Done", GetMergeDiffsPatchFileName(UnpatchedFile, (*I)->patch.file)); | |
2821 | RemoveFile("pkgAcqIndexMergeDiffs::Done", UnpatchedFile); | |
2822 | ||
2823 | // all set and done | |
2824 | Complete = true; | |
2825 | if(Debug) | |
2826 | std::clog << "allDone: " << DestFile << "\n" << std::endl; | |
2827 | return; | |
2828 | case StateDoneDiff: _error->Fatal("Done called for %s which is in an invalid Done state", PatchFile.c_str()); break; | |
2829 | case StateErrorDiff: _error->Fatal("Done called for %s which is in an invalid Error state", PatchFile.c_str()); break; | |
2830 | } | |
2831 | } | |
2832 | /*}}}*/ | |
2833 | std::string pkgAcqIndexMergeDiffs::Custom600Headers() const /*{{{*/ | |
2834 | { | |
2835 | if(State != StateApplyDiff) | |
2836 | return pkgAcqBaseIndex::Custom600Headers(); | |
2837 | std::ostringstream patchhashes; | |
2838 | unsigned int seen_patches = 0; | |
2839 | for (auto && hs : (*allPatches)[0]->patch.result_hashes) | |
2840 | patchhashes << "\nStart-" << hs.HashType() << "-Hash: " << hs.HashValue(); | |
2841 | for (std::vector<pkgAcqIndexMergeDiffs *>::const_iterator I = allPatches->begin(); | |
2842 | I != allPatches->end(); ++I) | |
2843 | { | |
2844 | HashStringList const ExpectedHashes = (*I)->patch.patch_hashes; | |
2845 | for (HashStringList::const_iterator hs = ExpectedHashes.begin(); hs != ExpectedHashes.end(); ++hs) | |
2846 | patchhashes << "\nPatch-" << std::to_string(seen_patches) << "-" << hs->HashType() << "-Hash: " << hs->HashValue(); | |
2847 | ++seen_patches; | |
2848 | } | |
2849 | patchhashes << pkgAcqBaseIndex::Custom600Headers(); | |
2850 | return patchhashes.str(); | |
2851 | } | |
2852 | /*}}}*/ | |
2853 | pkgAcqIndexMergeDiffs::~pkgAcqIndexMergeDiffs() {} | |
2854 | ||
2855 | // AcqIndex::AcqIndex - Constructor /*{{{*/ | |
2856 | pkgAcqIndex::pkgAcqIndex(pkgAcquire * const Owner, | |
2857 | pkgAcqMetaClearSig * const TransactionManager, | |
2858 | IndexTarget const &Target, bool const Derived) | |
2859 | : pkgAcqBaseIndex(Owner, TransactionManager, Target), d(NULL), Stage(STAGE_DOWNLOAD), | |
2860 | CompressionExtensions(Target.Option(IndexTarget::COMPRESSIONTYPES)) | |
2861 | { | |
2862 | if (Derived) | |
2863 | return; | |
2864 | Init(Target.URI, Target.Description, Target.ShortDesc); | |
2865 | ||
2866 | if(_config->FindB("Debug::Acquire::Transaction", false) == true) | |
2867 | std::clog << "New pkgIndex with TransactionManager " | |
2868 | << TransactionManager << std::endl; | |
2869 | } | |
2870 | /*}}}*/ | |
2871 | // AcqIndex::Init - defered Constructor /*{{{*/ | |
2872 | static void NextCompressionExtension(std::string &CurrentCompressionExtension, std::string &CompressionExtensions, bool const preview) | |
2873 | { | |
2874 | size_t const nextExt = CompressionExtensions.find(' '); | |
2875 | if (nextExt == std::string::npos) | |
2876 | { | |
2877 | CurrentCompressionExtension = CompressionExtensions; | |
2878 | if (preview == false) | |
2879 | CompressionExtensions.clear(); | |
2880 | } | |
2881 | else | |
2882 | { | |
2883 | CurrentCompressionExtension = CompressionExtensions.substr(0, nextExt); | |
2884 | if (preview == false) | |
2885 | CompressionExtensions = CompressionExtensions.substr(nextExt+1); | |
2886 | } | |
2887 | } | |
2888 | void pkgAcqIndex::Init(string const &URI, string const &URIDesc, | |
2889 | string const &ShortDesc) | |
2890 | { | |
2891 | Stage = STAGE_DOWNLOAD; | |
2892 | ||
2893 | DestFile = GetPartialFileNameFromURI(URI); | |
2894 | NextCompressionExtension(CurrentCompressionExtension, CompressionExtensions, false); | |
2895 | ||
2896 | if (CurrentCompressionExtension == "uncompressed") | |
2897 | { | |
2898 | Desc.URI = URI; | |
2899 | } | |
2900 | else if (CurrentCompressionExtension == "by-hash") | |
2901 | { | |
2902 | NextCompressionExtension(CurrentCompressionExtension, CompressionExtensions, true); | |
2903 | if(unlikely(CurrentCompressionExtension.empty())) | |
2904 | return; | |
2905 | if (CurrentCompressionExtension != "uncompressed") | |
2906 | { | |
2907 | Desc.URI = URI + '.' + CurrentCompressionExtension; | |
2908 | DestFile = DestFile + '.' + CurrentCompressionExtension; | |
2909 | } | |
2910 | else | |
2911 | Desc.URI = URI; | |
2912 | ||
2913 | HashStringList const Hashes = GetExpectedHashes(); | |
2914 | HashString const * const TargetHash = Hashes.find(NULL); | |
2915 | if (unlikely(TargetHash == nullptr)) | |
2916 | return; | |
2917 | std::string const ByHash = "/by-hash/" + TargetHash->HashType() + "/" + TargetHash->HashValue(); | |
2918 | size_t const trailing_slash = Desc.URI.find_last_of("/"); | |
2919 | if (unlikely(trailing_slash == std::string::npos)) | |
2920 | return; | |
2921 | Desc.URI = Desc.URI.replace( | |
2922 | trailing_slash, | |
2923 | Desc.URI.substr(trailing_slash+1).size()+1, | |
2924 | ByHash); | |
2925 | } | |
2926 | else if (unlikely(CurrentCompressionExtension.empty())) | |
2927 | return; | |
2928 | else | |
2929 | { | |
2930 | Desc.URI = URI + '.' + CurrentCompressionExtension; | |
2931 | DestFile = DestFile + '.' + CurrentCompressionExtension; | |
2932 | } | |
2933 | ||
2934 | // store file size of the download to ensure the fetcher gives | |
2935 | // accurate progress reporting | |
2936 | FileSize = GetExpectedHashes().FileSize(); | |
2937 | ||
2938 | Desc.Description = URIDesc; | |
2939 | Desc.Owner = this; | |
2940 | Desc.ShortDesc = ShortDesc; | |
2941 | ||
2942 | QueueURI(Desc); | |
2943 | } | |
2944 | /*}}}*/ | |
2945 | // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/ | |
2946 | // --------------------------------------------------------------------- | |
2947 | /* The only header we use is the last-modified header. */ | |
2948 | string pkgAcqIndex::Custom600Headers() const | |
2949 | { | |
2950 | ||
2951 | string msg = "\nIndex-File: true"; | |
2952 | ||
2953 | if (TransactionManager->LastMetaIndexParser == NULL) | |
2954 | { | |
2955 | std::string const Final = GetFinalFilename(); | |
2956 | ||
2957 | struct stat Buf; | |
2958 | if (stat(Final.c_str(),&Buf) == 0) | |
2959 | msg += "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime, false); | |
2960 | } | |
2961 | ||
2962 | if(Target.IsOptional) | |
2963 | msg += "\nFail-Ignore: true"; | |
2964 | ||
2965 | return msg; | |
2966 | } | |
2967 | /*}}}*/ | |
2968 | // AcqIndex::Failed - getting the indexfile failed /*{{{*/ | |
2969 | bool pkgAcqIndex::CommonFailed(std::string const &TargetURI, std::string const TargetDesc, | |
2970 | std::string const &Message, pkgAcquire::MethodConfig const * const Cnf) | |
2971 | { | |
2972 | pkgAcqBaseIndex::Failed(Message,Cnf); | |
2973 | ||
2974 | if (UsedMirror.empty() == false && UsedMirror != "DIRECT" && | |
2975 | LookupTag(Message, "FailReason") == "HttpError404") | |
2976 | { | |
2977 | UsedMirror = "DIRECT"; | |
2978 | if (Desc.URI.find("/by-hash/") != std::string::npos) | |
2979 | CompressionExtensions = "by-hash " + CompressionExtensions; | |
2980 | else | |
2981 | CompressionExtensions = CurrentCompressionExtension + ' ' + CompressionExtensions; | |
2982 | Init(TargetURI, TargetDesc, Desc.ShortDesc); | |
2983 | Status = StatIdle; | |
2984 | return true; | |
2985 | } | |
2986 | ||
2987 | // authorisation matches will not be fixed by other compression types | |
2988 | if (Status != StatAuthError) | |
2989 | { | |
2990 | if (CompressionExtensions.empty() == false) | |
2991 | { | |
2992 | Init(TargetURI, Desc.Description, Desc.ShortDesc); | |
2993 | Status = StatIdle; | |
2994 | return true; | |
2995 | } | |
2996 | } | |
2997 | return false; | |
2998 | } | |
2999 | void pkgAcqIndex::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf) | |
3000 | { | |
3001 | if (CommonFailed(Target.URI, Target.Description, Message, Cnf)) | |
3002 | return; | |
3003 | ||
3004 | if(Target.IsOptional && GetExpectedHashes().empty() && Stage == STAGE_DOWNLOAD) | |
3005 | Status = StatDone; | |
3006 | else | |
3007 | TransactionManager->AbortTransaction(); | |
3008 | } | |
3009 | /*}}}*/ | |
3010 | // AcqIndex::Done - Finished a fetch /*{{{*/ | |
3011 | // --------------------------------------------------------------------- | |
3012 | /* This goes through a number of states.. On the initial fetch the | |
3013 | method could possibly return an alternate filename which points | |
3014 | to the uncompressed version of the file. If this is so the file | |
3015 | is copied into the partial directory. In all other cases the file | |
3016 | is decompressed with a compressed uri. */ | |
3017 | void pkgAcqIndex::Done(string const &Message, | |
3018 | HashStringList const &Hashes, | |
3019 | pkgAcquire::MethodConfig const * const Cfg) | |
3020 | { | |
3021 | Item::Done(Message,Hashes,Cfg); | |
3022 | ||
3023 | switch(Stage) | |
3024 | { | |
3025 | case STAGE_DOWNLOAD: | |
3026 | StageDownloadDone(Message); | |
3027 | break; | |
3028 | case STAGE_DECOMPRESS_AND_VERIFY: | |
3029 | StageDecompressDone(); | |
3030 | break; | |
3031 | } | |
3032 | } | |
3033 | /*}}}*/ | |
3034 | // AcqIndex::StageDownloadDone - Queue for decompress and verify /*{{{*/ | |
3035 | void pkgAcqIndex::StageDownloadDone(string const &Message) | |
3036 | { | |
3037 | Local = true; | |
3038 | Complete = true; | |
3039 | ||
3040 | std::string const AltFilename = LookupTag(Message,"Alt-Filename"); | |
3041 | std::string Filename = LookupTag(Message,"Filename"); | |
3042 | ||
3043 | // we need to verify the file against the current Release file again | |
3044 | // on if-modfied-since hit to avoid a stale attack against us | |
3045 | if(StringToBool(LookupTag(Message,"IMS-Hit"),false) == true) | |
3046 | { | |
3047 | // copy FinalFile into partial/ so that we check the hash again | |
3048 | string const FinalFile = GetExistingFilename(GetFinalFileNameFromURI(Target.URI)); | |
3049 | if (symlink(FinalFile.c_str(), DestFile.c_str()) != 0) | |
3050 | _error->WarningE("pkgAcqIndex::StageDownloadDone", "Symlinking final file %s back to %s failed", FinalFile.c_str(), DestFile.c_str()); | |
3051 | else | |
3052 | { | |
3053 | EraseFileName = DestFile; | |
3054 | Filename = DestFile; | |
3055 | } | |
3056 | Stage = STAGE_DECOMPRESS_AND_VERIFY; | |
3057 | Desc.URI = "store:" + Filename; | |
3058 | QueueURI(Desc); | |
3059 | SetActiveSubprocess(::URI(Desc.URI).Access); | |
3060 | return; | |
3061 | } | |
3062 | // methods like file:// give us an alternative (uncompressed) file | |
3063 | else if (Target.KeepCompressed == false && AltFilename.empty() == false) | |
3064 | { | |
3065 | Filename = AltFilename; | |
3066 | EraseFileName.clear(); | |
3067 | } | |
3068 | // Methods like e.g. "file:" will give us a (compressed) FileName that is | |
3069 | // not the "DestFile" we set, in this case we uncompress from the local file | |
3070 | else if (Filename != DestFile && RealFileExists(DestFile) == false) | |
3071 | { | |
3072 | // symlinking ensures that the filename can be used for compression detection | |
3073 | // that is e.g. needed for by-hash which has no extension over file | |
3074 | if (symlink(Filename.c_str(),DestFile.c_str()) != 0) | |
3075 | _error->WarningE("pkgAcqIndex::StageDownloadDone", "Symlinking file %s to %s failed", Filename.c_str(), DestFile.c_str()); | |
3076 | else | |
3077 | { | |
3078 | EraseFileName = DestFile; | |
3079 | Filename = DestFile; | |
3080 | } | |
3081 | } | |
3082 | ||
3083 | Stage = STAGE_DECOMPRESS_AND_VERIFY; | |
3084 | DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target); | |
3085 | if (Filename != DestFile && flExtension(Filename) == flExtension(DestFile)) | |
3086 | Desc.URI = "copy:" + Filename; | |
3087 | else | |
3088 | Desc.URI = "store:" + Filename; | |
3089 | if (DestFile == Filename) | |
3090 | { | |
3091 | if (CurrentCompressionExtension == "uncompressed") | |
3092 | return StageDecompressDone(); | |
3093 | DestFile = "/dev/null"; | |
3094 | } | |
3095 | ||
3096 | if (EraseFileName.empty() && Filename != AltFilename) | |
3097 | EraseFileName = Filename; | |
3098 | ||
3099 | // queue uri for the next stage | |
3100 | QueueURI(Desc); | |
3101 | SetActiveSubprocess(::URI(Desc.URI).Access); | |
3102 | } | |
3103 | /*}}}*/ | |
3104 | // AcqIndex::StageDecompressDone - Final verification /*{{{*/ | |
3105 | void pkgAcqIndex::StageDecompressDone() | |
3106 | { | |
3107 | if (DestFile == "/dev/null") | |
3108 | DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target); | |
3109 | ||
3110 | // Done, queue for rename on transaction finished | |
3111 | TransactionManager->TransactionStageCopy(this, DestFile, GetFinalFilename()); | |
3112 | } | |
3113 | /*}}}*/ | |
3114 | pkgAcqIndex::~pkgAcqIndex() {} | |
3115 | ||
3116 | ||
3117 | // AcqArchive::AcqArchive - Constructor /*{{{*/ | |
3118 | // --------------------------------------------------------------------- | |
3119 | /* This just sets up the initial fetch environment and queues the first | |
3120 | possibilitiy */ | |
3121 | pkgAcqArchive::pkgAcqArchive(pkgAcquire * const Owner,pkgSourceList * const Sources, | |
3122 | pkgRecords * const Recs,pkgCache::VerIterator const &Version, | |
3123 | string &StoreFilename) : | |
3124 | Item(Owner), d(NULL), LocalSource(false), Version(Version), Sources(Sources), Recs(Recs), | |
3125 | StoreFilename(StoreFilename), Vf(Version.FileList()), | |
3126 | Trusted(false) | |
3127 | { | |
3128 | Retries = _config->FindI("Acquire::Retries",0); | |
3129 | ||
3130 | if (Version.Arch() == 0) | |
3131 | { | |
3132 | _error->Error(_("I wasn't able to locate a file for the %s package. " | |
3133 | "This might mean you need to manually fix this package. " | |
3134 | "(due to missing arch)"), | |
3135 | Version.ParentPkg().FullName().c_str()); | |
3136 | return; | |
3137 | } | |
3138 | ||
3139 | /* We need to find a filename to determine the extension. We make the | |
3140 | assumption here that all the available sources for this version share | |
3141 | the same extension.. */ | |
3142 | // Skip not source sources, they do not have file fields. | |
3143 | for (; Vf.end() == false; ++Vf) | |
3144 | { | |
3145 | if (Vf.File().Flagged(pkgCache::Flag::NotSource)) | |
3146 | continue; | |
3147 | break; | |
3148 | } | |
3149 | ||
3150 | // Does not really matter here.. we are going to fail out below | |
3151 | if (Vf.end() != true) | |
3152 | { | |
3153 | // If this fails to get a file name we will bomb out below. | |
3154 | pkgRecords::Parser &Parse = Recs->Lookup(Vf); | |
3155 | if (_error->PendingError() == true) | |
3156 | return; | |
3157 | ||
3158 | // Generate the final file name as: package_version_arch.foo | |
3159 | StoreFilename = QuoteString(Version.ParentPkg().Name(),"_:") + '_' + | |
3160 | QuoteString(Version.VerStr(),"_:") + '_' + | |
3161 | QuoteString(Version.Arch(),"_:.") + | |
3162 | "." + flExtension(Parse.FileName()); | |
3163 | } | |
3164 | ||
3165 | // check if we have one trusted source for the package. if so, switch | |
3166 | // to "TrustedOnly" mode - but only if not in AllowUnauthenticated mode | |
3167 | bool const allowUnauth = _config->FindB("APT::Get::AllowUnauthenticated", false); | |
3168 | bool const debugAuth = _config->FindB("Debug::pkgAcquire::Auth", false); | |
3169 | bool seenUntrusted = false; | |
3170 | for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; ++i) | |
3171 | { | |
3172 | pkgIndexFile *Index; | |
3173 | if (Sources->FindIndex(i.File(),Index) == false) | |
3174 | continue; | |
3175 | ||
3176 | if (debugAuth == true) | |
3177 | std::cerr << "Checking index: " << Index->Describe() | |
3178 | << "(Trusted=" << Index->IsTrusted() << ")" << std::endl; | |
3179 | ||
3180 | if (Index->IsTrusted() == true) | |
3181 | { | |
3182 | Trusted = true; | |
3183 | if (allowUnauth == false) | |
3184 | break; | |
3185 | } | |
3186 | else | |
3187 | seenUntrusted = true; | |
3188 | } | |
3189 | ||
3190 | // "allow-unauthenticated" restores apts old fetching behaviour | |
3191 | // that means that e.g. unauthenticated file:// uris are higher | |
3192 | // priority than authenticated http:// uris | |
3193 | if (allowUnauth == true && seenUntrusted == true) | |
3194 | Trusted = false; | |
3195 | ||
3196 | // Select a source | |
3197 | if (QueueNext() == false && _error->PendingError() == false) | |
3198 | _error->Error(_("Can't find a source to download version '%s' of '%s'"), | |
3199 | Version.VerStr(), Version.ParentPkg().FullName(false).c_str()); | |
3200 | } | |
3201 | /*}}}*/ | |
3202 | // AcqArchive::QueueNext - Queue the next file source /*{{{*/ | |
3203 | // --------------------------------------------------------------------- | |
3204 | /* This queues the next available file version for download. It checks if | |
3205 | the archive is already available in the cache and stashs the MD5 for | |
3206 | checking later. */ | |
3207 | bool pkgAcqArchive::QueueNext() | |
3208 | { | |
3209 | for (; Vf.end() == false; ++Vf) | |
3210 | { | |
3211 | pkgCache::PkgFileIterator const PkgF = Vf.File(); | |
3212 | // Ignore not source sources | |
3213 | if (PkgF.Flagged(pkgCache::Flag::NotSource)) | |
3214 | continue; | |
3215 | ||
3216 | // Try to cross match against the source list | |
3217 | pkgIndexFile *Index; | |
3218 | if (Sources->FindIndex(PkgF, Index) == false) | |
3219 | continue; | |
3220 | LocalSource = PkgF.Flagged(pkgCache::Flag::LocalSource); | |
3221 | ||
3222 | // only try to get a trusted package from another source if that source | |
3223 | // is also trusted | |
3224 | if(Trusted && !Index->IsTrusted()) | |
3225 | continue; | |
3226 | ||
3227 | // Grab the text package record | |
3228 | pkgRecords::Parser &Parse = Recs->Lookup(Vf); | |
3229 | if (_error->PendingError() == true) | |
3230 | return false; | |
3231 | ||
3232 | string PkgFile = Parse.FileName(); | |
3233 | ExpectedHashes = Parse.Hashes(); | |
3234 | ||
3235 | if (PkgFile.empty() == true) | |
3236 | return _error->Error(_("The package index files are corrupted. No Filename: " | |
3237 | "field for package %s."), | |
3238 | Version.ParentPkg().Name()); | |
3239 | ||
3240 | Desc.URI = Index->ArchiveURI(PkgFile); | |
3241 | Desc.Description = Index->ArchiveInfo(Version); | |
3242 | Desc.Owner = this; | |
3243 | Desc.ShortDesc = Version.ParentPkg().FullName(true); | |
3244 | ||
3245 | // See if we already have the file. (Legacy filenames) | |
3246 | FileSize = Version->Size; | |
3247 | string FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile); | |
3248 | struct stat Buf; | |
3249 | if (stat(FinalFile.c_str(),&Buf) == 0) | |
3250 | { | |
3251 | // Make sure the size matches | |
3252 | if ((unsigned long long)Buf.st_size == Version->Size) | |
3253 | { | |
3254 | Complete = true; | |
3255 | Local = true; | |
3256 | Status = StatDone; | |
3257 | StoreFilename = DestFile = FinalFile; | |
3258 | return true; | |
3259 | } | |
3260 | ||
3261 | /* Hmm, we have a file and its size does not match, this means it is | |
3262 | an old style mismatched arch */ | |
3263 | RemoveFile("pkgAcqArchive::QueueNext", FinalFile); | |
3264 | } | |
3265 | ||
3266 | // Check it again using the new style output filenames | |
3267 | FinalFile = _config->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename); | |
3268 | if (stat(FinalFile.c_str(),&Buf) == 0) | |
3269 | { | |
3270 | // Make sure the size matches | |
3271 | if ((unsigned long long)Buf.st_size == Version->Size) | |
3272 | { | |
3273 | Complete = true; | |
3274 | Local = true; | |
3275 | Status = StatDone; | |
3276 | StoreFilename = DestFile = FinalFile; | |
3277 | return true; | |
3278 | } | |
3279 | ||
3280 | /* Hmm, we have a file and its size does not match, this shouldn't | |
3281 | happen.. */ | |
3282 | RemoveFile("pkgAcqArchive::QueueNext", FinalFile); | |
3283 | } | |
3284 | ||
3285 | DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename); | |
3286 | ||
3287 | // Check the destination file | |
3288 | if (stat(DestFile.c_str(),&Buf) == 0) | |
3289 | { | |
3290 | // Hmm, the partial file is too big, erase it | |
3291 | if ((unsigned long long)Buf.st_size > Version->Size) | |
3292 | RemoveFile("pkgAcqArchive::QueueNext", DestFile); | |
3293 | else | |
3294 | PartialSize = Buf.st_size; | |
3295 | } | |
3296 | ||
3297 | // Disables download of archives - useful if no real installation follows, | |
3298 | // e.g. if we are just interested in proposed installation order | |
3299 | if (_config->FindB("Debug::pkgAcqArchive::NoQueue", false) == true) | |
3300 | { | |
3301 | Complete = true; | |
3302 | Local = true; | |
3303 | Status = StatDone; | |
3304 | StoreFilename = DestFile = FinalFile; | |
3305 | return true; | |
3306 | } | |
3307 | ||
3308 | // Create the item | |
3309 | Local = false; | |
3310 | ++Vf; | |
3311 | QueueURI(Desc); | |
3312 | return true; | |
3313 | } | |
3314 | return false; | |
3315 | } | |
3316 | /*}}}*/ | |
3317 | // AcqArchive::Done - Finished fetching /*{{{*/ | |
3318 | // --------------------------------------------------------------------- | |
3319 | /* */ | |
3320 | void pkgAcqArchive::Done(string const &Message, HashStringList const &Hashes, | |
3321 | pkgAcquire::MethodConfig const * const Cfg) | |
3322 | { | |
3323 | Item::Done(Message, Hashes, Cfg); | |
3324 | ||
3325 | // Grab the output filename | |
3326 | std::string const FileName = LookupTag(Message,"Filename"); | |
3327 | if (DestFile != FileName && RealFileExists(DestFile) == false) | |
3328 | { | |
3329 | StoreFilename = DestFile = FileName; | |
3330 | Local = true; | |
3331 | Complete = true; | |
3332 | return; | |
3333 | } | |
3334 | ||
3335 | // Done, move it into position | |
3336 | string const FinalFile = GetFinalFilename(); | |
3337 | Rename(DestFile,FinalFile); | |
3338 | StoreFilename = DestFile = FinalFile; | |
3339 | Complete = true; | |
3340 | } | |
3341 | /*}}}*/ | |
3342 | // AcqArchive::Failed - Failure handler /*{{{*/ | |
3343 | // --------------------------------------------------------------------- | |
3344 | /* Here we try other sources */ | |
3345 | void pkgAcqArchive::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf) | |
3346 | { | |
3347 | Item::Failed(Message,Cnf); | |
3348 | ||
3349 | /* We don't really want to retry on failed media swaps, this prevents | |
3350 | that. An interesting observation is that permanent failures are not | |
3351 | recorded. */ | |
3352 | if (Cnf->Removable == true && | |
3353 | StringToBool(LookupTag(Message,"Transient-Failure"),false) == true) | |
3354 | { | |
3355 | // Vf = Version.FileList(); | |
3356 | while (Vf.end() == false) ++Vf; | |
3357 | StoreFilename = string(); | |
3358 | return; | |
3359 | } | |
3360 | ||
3361 | Status = StatIdle; | |
3362 | if (QueueNext() == false) | |
3363 | { | |
3364 | // This is the retry counter | |
3365 | if (Retries != 0 && | |
3366 | Cnf->LocalOnly == false && | |
3367 | StringToBool(LookupTag(Message,"Transient-Failure"),false) == true) | |
3368 | { | |
3369 | Retries--; | |
3370 | Vf = Version.FileList(); | |
3371 | if (QueueNext() == true) | |
3372 | return; | |
3373 | } | |
3374 | ||
3375 | StoreFilename = string(); | |
3376 | Status = StatError; | |
3377 | } | |
3378 | } | |
3379 | /*}}}*/ | |
3380 | APT_PURE bool pkgAcqArchive::IsTrusted() const /*{{{*/ | |
3381 | { | |
3382 | return Trusted; | |
3383 | } | |
3384 | /*}}}*/ | |
3385 | void pkgAcqArchive::Finished() /*{{{*/ | |
3386 | { | |
3387 | if (Status == pkgAcquire::Item::StatDone && | |
3388 | Complete == true) | |
3389 | return; | |
3390 | StoreFilename = string(); | |
3391 | } | |
3392 | /*}}}*/ | |
3393 | std::string pkgAcqArchive::DescURI() const /*{{{*/ | |
3394 | { | |
3395 | return Desc.URI; | |
3396 | } | |
3397 | /*}}}*/ | |
3398 | std::string pkgAcqArchive::ShortDesc() const /*{{{*/ | |
3399 | { | |
3400 | return Desc.ShortDesc; | |
3401 | } | |
3402 | /*}}}*/ | |
3403 | pkgAcqArchive::~pkgAcqArchive() {} | |
3404 | ||
3405 | // AcqChangelog::pkgAcqChangelog - Constructors /*{{{*/ | |
3406 | class pkgAcqChangelog::Private | |
3407 | { | |
3408 | public: | |
3409 | std::string FinalFile; | |
3410 | }; | |
3411 | pkgAcqChangelog::pkgAcqChangelog(pkgAcquire * const Owner, pkgCache::VerIterator const &Ver, | |
3412 | std::string const &DestDir, std::string const &DestFilename) : | |
3413 | pkgAcquire::Item(Owner), d(new pkgAcqChangelog::Private()), SrcName(Ver.SourcePkgName()), SrcVersion(Ver.SourceVerStr()) | |
3414 | { | |
3415 | Desc.URI = URI(Ver); | |
3416 | Init(DestDir, DestFilename); | |
3417 | } | |
3418 | // some parameters are char* here as they come likely from char* interfaces – which can also return NULL | |
3419 | pkgAcqChangelog::pkgAcqChangelog(pkgAcquire * const Owner, pkgCache::RlsFileIterator const &RlsFile, | |
3420 | char const * const Component, char const * const SrcName, char const * const SrcVersion, | |
3421 | const string &DestDir, const string &DestFilename) : | |
3422 | pkgAcquire::Item(Owner), d(new pkgAcqChangelog::Private()), SrcName(SrcName), SrcVersion(SrcVersion) | |
3423 | { | |
3424 | Desc.URI = URI(RlsFile, Component, SrcName, SrcVersion); | |
3425 | Init(DestDir, DestFilename); | |
3426 | } | |
3427 | pkgAcqChangelog::pkgAcqChangelog(pkgAcquire * const Owner, | |
3428 | std::string const &URI, char const * const SrcName, char const * const SrcVersion, | |
3429 | const string &DestDir, const string &DestFilename) : | |
3430 | pkgAcquire::Item(Owner), d(new pkgAcqChangelog::Private()), SrcName(SrcName), SrcVersion(SrcVersion) | |
3431 | { | |
3432 | Desc.URI = URI; | |
3433 | Init(DestDir, DestFilename); | |
3434 | } | |
3435 | void pkgAcqChangelog::Init(std::string const &DestDir, std::string const &DestFilename) | |
3436 | { | |
3437 | if (Desc.URI.empty()) | |
3438 | { | |
3439 | Status = StatError; | |
3440 | // TRANSLATOR: %s=%s is sourcename=sourceversion, e.g. apt=1.1 | |
3441 | strprintf(ErrorText, _("Changelog unavailable for %s=%s"), SrcName.c_str(), SrcVersion.c_str()); | |
3442 | // Let the error message print something sensible rather than "Failed to fetch /" | |
3443 | if (DestFilename.empty()) | |
3444 | DestFile = SrcName + ".changelog"; | |
3445 | else | |
3446 | DestFile = DestFilename; | |
3447 | Desc.URI = "changelog:/" + DestFile; | |
3448 | return; | |
3449 | } | |
3450 | ||
3451 | std::string DestFileName; | |
3452 | if (DestFilename.empty()) | |
3453 | DestFileName = flCombine(DestFile, SrcName + ".changelog"); | |
3454 | else | |
3455 | DestFileName = flCombine(DestFile, DestFilename); | |
3456 | ||
3457 | std::string const SandboxUser = _config->Find("APT::Sandbox::User"); | |
3458 | std::string const systemTemp = GetTempDir(SandboxUser); | |
3459 | char tmpname[1000]; | |
3460 | snprintf(tmpname, sizeof(tmpname), "%s/apt-changelog-XXXXXX", systemTemp.c_str()); | |
3461 | if (NULL == mkdtemp(tmpname)) | |
3462 | { | |
3463 | _error->Errno("mkdtemp", "mkdtemp failed in changelog acquire of %s %s", SrcName.c_str(), SrcVersion.c_str()); | |
3464 | Status = StatError; | |
3465 | return; | |
3466 | } | |
3467 | TemporaryDirectory = tmpname; | |
3468 | ||
3469 | ChangeOwnerAndPermissionOfFile("Item::QueueURI", TemporaryDirectory.c_str(), | |
3470 | SandboxUser.c_str(), ROOT_GROUP, 0700); | |
3471 | ||
3472 | DestFile = flCombine(TemporaryDirectory, DestFileName); | |
3473 | if (DestDir.empty() == false) | |
3474 | { | |
3475 | d->FinalFile = flCombine(DestDir, DestFileName); | |
3476 | if (RealFileExists(d->FinalFile)) | |
3477 | { | |
3478 | FileFd file1, file2; | |
3479 | if (file1.Open(DestFile, FileFd::WriteOnly | FileFd::Create | FileFd::Exclusive) && | |
3480 | file2.Open(d->FinalFile, FileFd::ReadOnly) && CopyFile(file2, file1)) | |
3481 | { | |
3482 | struct timeval times[2]; | |
3483 | times[0].tv_sec = times[1].tv_sec = file2.ModificationTime(); | |
3484 | times[0].tv_usec = times[1].tv_usec = 0; | |
3485 | utimes(DestFile.c_str(), times); | |
3486 | } | |
3487 | } | |
3488 | } | |
3489 | ||
3490 | Desc.ShortDesc = "Changelog"; | |
3491 | strprintf(Desc.Description, "%s %s %s Changelog", URI::SiteOnly(Desc.URI).c_str(), SrcName.c_str(), SrcVersion.c_str()); | |
3492 | Desc.Owner = this; | |
3493 | QueueURI(Desc); | |
3494 | } | |
3495 | /*}}}*/ | |
3496 | std::string pkgAcqChangelog::URI(pkgCache::VerIterator const &Ver) /*{{{*/ | |
3497 | { | |
3498 | std::string const confOnline = "Acquire::Changelogs::AlwaysOnline"; | |
3499 | bool AlwaysOnline = _config->FindB(confOnline, false); | |
3500 | if (AlwaysOnline == false) | |
3501 | for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; ++VF) | |
3502 | { | |
3503 | pkgCache::PkgFileIterator const PF = VF.File(); | |
3504 | if (PF.Flagged(pkgCache::Flag::NotSource) || PF->Release == 0) | |
3505 | continue; | |
3506 | pkgCache::RlsFileIterator const RF = PF.ReleaseFile(); | |
3507 | if (RF->Origin != 0 && _config->FindB(confOnline + "::Origin::" + RF.Origin(), false)) | |
3508 | { | |
3509 | AlwaysOnline = true; | |
3510 | break; | |
3511 | } | |
3512 | } | |
3513 | if (AlwaysOnline == false) | |
3514 | { | |
3515 | pkgCache::PkgIterator const Pkg = Ver.ParentPkg(); | |
3516 | if (Pkg->CurrentVer != 0 && Pkg.CurrentVer() == Ver) | |
3517 | { | |
3518 | std::string const root = _config->FindDir("Dir"); | |
3519 | std::string const basename = root + std::string("usr/share/doc/") + Pkg.Name() + "/changelog"; | |
3520 | std::string const debianname = basename + ".Debian"; | |
3521 | if (FileExists(debianname)) | |
3522 | return "copy://" + debianname; | |
3523 | else if (FileExists(debianname + ".gz")) | |
3524 | return "gzip://" + debianname + ".gz"; | |
3525 | else if (FileExists(basename)) | |
3526 | return "copy://" + basename; | |
3527 | else if (FileExists(basename + ".gz")) | |
3528 | return "gzip://" + basename + ".gz"; | |
3529 | } | |
3530 | } | |
3531 | ||
3532 | char const * const SrcName = Ver.SourcePkgName(); | |
3533 | char const * const SrcVersion = Ver.SourceVerStr(); | |
3534 | // find the first source for this version which promises a changelog | |
3535 | for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; ++VF) | |
3536 | { | |
3537 | pkgCache::PkgFileIterator const PF = VF.File(); | |
3538 | if (PF.Flagged(pkgCache::Flag::NotSource) || PF->Release == 0) | |
3539 | continue; | |
3540 | pkgCache::RlsFileIterator const RF = PF.ReleaseFile(); | |
3541 | std::string const uri = URI(RF, PF.Component(), SrcName, SrcVersion); | |
3542 | if (uri.empty()) | |
3543 | continue; | |
3544 | return uri; | |
3545 | } | |
3546 | return ""; | |
3547 | } | |
3548 | std::string pkgAcqChangelog::URITemplate(pkgCache::RlsFileIterator const &Rls) | |
3549 | { | |
3550 | if (Rls.end() == true || (Rls->Label == 0 && Rls->Origin == 0)) | |
3551 | return ""; | |
3552 | std::string const serverConfig = "Acquire::Changelogs::URI"; | |
3553 | std::string server; | |
3554 | #define APT_EMPTY_SERVER \ | |
3555 | if (server.empty() == false) \ | |
3556 | { \ | |
3557 | if (server != "no") \ | |
3558 | return server; \ | |
3559 | return ""; \ | |
3560 | } | |
3561 | #define APT_CHECK_SERVER(X, Y) \ | |
3562 | if (Rls->X != 0) \ | |
3563 | { \ | |
3564 | std::string const specialServerConfig = serverConfig + "::" + Y + #X + "::" + Rls.X(); \ | |
3565 | server = _config->Find(specialServerConfig); \ | |
3566 | APT_EMPTY_SERVER \ | |
3567 | } | |
3568 | // this way e.g. Debian-Security can fallback to Debian | |
3569 | APT_CHECK_SERVER(Label, "Override::") | |
3570 | APT_CHECK_SERVER(Origin, "Override::") | |
3571 | ||
3572 | if (RealFileExists(Rls.FileName())) | |
3573 | { | |
3574 | _error->PushToStack(); | |
3575 | FileFd rf; | |
3576 | /* This can be costly. A caller wanting to get millions of URIs might | |
3577 | want to do this on its own once and use Override settings. | |
3578 | We don't do this here as Origin/Label are not as unique as they | |
3579 | should be so this could produce request order-dependent anomalies */ | |
3580 | if (OpenMaybeClearSignedFile(Rls.FileName(), rf) == true) | |
3581 | { | |
3582 | pkgTagFile TagFile(&rf, rf.Size()); | |
3583 | pkgTagSection Section; | |
3584 | if (TagFile.Step(Section) == true) | |
3585 | server = Section.FindS("Changelogs"); | |
3586 | } | |
3587 | _error->RevertToStack(); | |
3588 | APT_EMPTY_SERVER | |
3589 | } | |
3590 | ||
3591 | APT_CHECK_SERVER(Label, "") | |
3592 | APT_CHECK_SERVER(Origin, "") | |
3593 | #undef APT_CHECK_SERVER | |
3594 | #undef APT_EMPTY_SERVER | |
3595 | return ""; | |
3596 | } | |
3597 | std::string pkgAcqChangelog::URI(pkgCache::RlsFileIterator const &Rls, | |
3598 | char const * const Component, char const * const SrcName, | |
3599 | char const * const SrcVersion) | |
3600 | { | |
3601 | return URI(URITemplate(Rls), Component, SrcName, SrcVersion); | |
3602 | } | |
3603 | std::string pkgAcqChangelog::URI(std::string const &Template, | |
3604 | char const * const Component, char const * const SrcName, | |
3605 | char const * const SrcVersion) | |
3606 | { | |
3607 | if (Template.find("@CHANGEPATH@") == std::string::npos) | |
3608 | return ""; | |
3609 | ||
3610 | // the path is: COMPONENT/SRC/SRCNAME/SRCNAME_SRCVER, e.g. main/a/apt/1.1 or contrib/liba/libapt/2.0 | |
3611 | std::string Src = SrcName; | |
3612 | std::string path = APT::String::Startswith(SrcName, "lib") ? Src.substr(0, 4) : Src.substr(0,1); | |
3613 | path.append("/").append(Src).append("/"); | |
3614 | path.append(Src).append("_").append(StripEpoch(SrcVersion)); | |
3615 | // we omit component for releases without one (= flat-style repositories) | |
3616 | if (Component != NULL && strlen(Component) != 0) | |
3617 | path = std::string(Component) + "/" + path; | |
3618 | ||
3619 | return SubstVar(Template, "@CHANGEPATH@", path); | |
3620 | } | |
3621 | /*}}}*/ | |
3622 | // AcqChangelog::Failed - Failure handler /*{{{*/ | |
3623 | void pkgAcqChangelog::Failed(string const &Message, pkgAcquire::MethodConfig const * const Cnf) | |
3624 | { | |
3625 | Item::Failed(Message,Cnf); | |
3626 | ||
3627 | std::string errText; | |
3628 | // TRANSLATOR: %s=%s is sourcename=sourceversion, e.g. apt=1.1 | |
3629 | strprintf(errText, _("Changelog unavailable for %s=%s"), SrcName.c_str(), SrcVersion.c_str()); | |
3630 | ||
3631 | // Error is probably something techy like 404 Not Found | |
3632 | if (ErrorText.empty()) | |
3633 | ErrorText = errText; | |
3634 | else | |
3635 | ErrorText = errText + " (" + ErrorText + ")"; | |
3636 | } | |
3637 | /*}}}*/ | |
3638 | // AcqChangelog::Done - Item downloaded OK /*{{{*/ | |
3639 | void pkgAcqChangelog::Done(string const &Message,HashStringList const &CalcHashes, | |
3640 | pkgAcquire::MethodConfig const * const Cnf) | |
3641 | { | |
3642 | Item::Done(Message,CalcHashes,Cnf); | |
3643 | if (d->FinalFile.empty() == false) | |
3644 | { | |
3645 | if (RemoveFile("pkgAcqChangelog::Done", d->FinalFile) == false || | |
3646 | Rename(DestFile, d->FinalFile) == false) | |
3647 | Status = StatError; | |
3648 | } | |
3649 | ||
3650 | Complete = true; | |
3651 | } | |
3652 | /*}}}*/ | |
3653 | pkgAcqChangelog::~pkgAcqChangelog() /*{{{*/ | |
3654 | { | |
3655 | if (TemporaryDirectory.empty() == false) | |
3656 | { | |
3657 | RemoveFile("~pkgAcqChangelog", DestFile); | |
3658 | rmdir(TemporaryDirectory.c_str()); | |
3659 | } | |
3660 | delete d; | |
3661 | } | |
3662 | /*}}}*/ | |
3663 | ||
3664 | // AcqFile::pkgAcqFile - Constructor /*{{{*/ | |
3665 | pkgAcqFile::pkgAcqFile(pkgAcquire * const Owner,string const &URI, HashStringList const &Hashes, | |
3666 | unsigned long long const Size,string const &Dsc,string const &ShortDesc, | |
3667 | const string &DestDir, const string &DestFilename, | |
3668 | bool const IsIndexFile) : | |
3669 | Item(Owner), d(NULL), IsIndexFile(IsIndexFile), ExpectedHashes(Hashes) | |
3670 | { | |
3671 | Retries = _config->FindI("Acquire::Retries",0); | |
3672 | ||
3673 | if(!DestFilename.empty()) | |
3674 | DestFile = DestFilename; | |
3675 | else if(!DestDir.empty()) | |
3676 | DestFile = DestDir + "/" + flNotDir(URI); | |
3677 | else | |
3678 | DestFile = flNotDir(URI); | |
3679 | ||
3680 | // Create the item | |
3681 | Desc.URI = URI; | |
3682 | Desc.Description = Dsc; | |
3683 | Desc.Owner = this; | |
3684 | ||
3685 | // Set the short description to the archive component | |
3686 | Desc.ShortDesc = ShortDesc; | |
3687 | ||
3688 | // Get the transfer sizes | |
3689 | FileSize = Size; | |
3690 | struct stat Buf; | |
3691 | if (stat(DestFile.c_str(),&Buf) == 0) | |
3692 | { | |
3693 | // Hmm, the partial file is too big, erase it | |
3694 | if ((Size > 0) && (unsigned long long)Buf.st_size > Size) | |
3695 | RemoveFile("pkgAcqFile", DestFile); | |
3696 | else | |
3697 | PartialSize = Buf.st_size; | |
3698 | } | |
3699 | ||
3700 | QueueURI(Desc); | |
3701 | } | |
3702 | /*}}}*/ | |
3703 | // AcqFile::Done - Item downloaded OK /*{{{*/ | |
3704 | void pkgAcqFile::Done(string const &Message,HashStringList const &CalcHashes, | |
3705 | pkgAcquire::MethodConfig const * const Cnf) | |
3706 | { | |
3707 | Item::Done(Message,CalcHashes,Cnf); | |
3708 | ||
3709 | std::string const FileName = LookupTag(Message,"Filename"); | |
3710 | Complete = true; | |
3711 | ||
3712 | // The files timestamp matches | |
3713 | if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true) | |
3714 | return; | |
3715 | ||
3716 | // We have to copy it into place | |
3717 | if (RealFileExists(DestFile.c_str()) == false) | |
3718 | { | |
3719 | Local = true; | |
3720 | if (_config->FindB("Acquire::Source-Symlinks",true) == false || | |
3721 | Cnf->Removable == true) | |
3722 | { | |
3723 | Desc.URI = "copy:" + FileName; | |
3724 | QueueURI(Desc); | |
3725 | return; | |
3726 | } | |
3727 | ||
3728 | // Erase the file if it is a symlink so we can overwrite it | |
3729 | struct stat St; | |
3730 | if (lstat(DestFile.c_str(),&St) == 0) | |
3731 | { | |
3732 | if (S_ISLNK(St.st_mode) != 0) | |
3733 | RemoveFile("pkgAcqFile::Done", DestFile); | |
3734 | } | |
3735 | ||
3736 | // Symlink the file | |
3737 | if (symlink(FileName.c_str(),DestFile.c_str()) != 0) | |
3738 | { | |
3739 | _error->PushToStack(); | |
3740 | _error->Errno("pkgAcqFile::Done", "Symlinking file %s failed", DestFile.c_str()); | |
3741 | std::stringstream msg; | |
3742 | _error->DumpErrors(msg, GlobalError::DEBUG, false); | |
3743 | _error->RevertToStack(); | |
3744 | ErrorText = msg.str(); | |
3745 | Status = StatError; | |
3746 | Complete = false; | |
3747 | } | |
3748 | } | |
3749 | } | |
3750 | /*}}}*/ | |
3751 | // AcqFile::Failed - Failure handler /*{{{*/ | |
3752 | // --------------------------------------------------------------------- | |
3753 | /* Here we try other sources */ | |
3754 | void pkgAcqFile::Failed(string const &Message, pkgAcquire::MethodConfig const * const Cnf) | |
3755 | { | |
3756 | Item::Failed(Message,Cnf); | |
3757 | ||
3758 | // This is the retry counter | |
3759 | if (Retries != 0 && | |
3760 | Cnf->LocalOnly == false && | |
3761 | StringToBool(LookupTag(Message,"Transient-Failure"),false) == true) | |
3762 | { | |
3763 | --Retries; | |
3764 | QueueURI(Desc); | |
3765 | Status = StatIdle; | |
3766 | return; | |
3767 | } | |
3768 | ||
3769 | } | |
3770 | /*}}}*/ | |
3771 | string pkgAcqFile::Custom600Headers() const /*{{{*/ | |
3772 | { | |
3773 | if (IsIndexFile) | |
3774 | return "\nIndex-File: true"; | |
3775 | return ""; | |
3776 | } | |
3777 | /*}}}*/ | |
3778 | pkgAcqFile::~pkgAcqFile() {} |