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