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