]> git.saurik.com Git - apt.git/blame_incremental - apt-pkg/acquire-item.cc
allow pdiff bootstrap from all supported compressors
[apt.git] / apt-pkg / acquire-item.cc
... / ...
CommitLineData
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
3// $Id: acquire-item.cc,v 1.46.2.9 2004/01/16 18:51:11 mdz Exp $
4/* ######################################################################
5
6 Acquire Item - Item to acquire
7
8 Each item can download to exactly one file at a time. This means you
9 cannot create an item that fetches two uri's to two files at the same
10 time. The pkgAcqIndex class creates a second class upon instantiation
11 to fetch the other index files because of this.
12
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
16#include <config.h>
17
18#include <apt-pkg/acquire-item.h>
19#include <apt-pkg/configuration.h>
20#include <apt-pkg/aptconfiguration.h>
21#include <apt-pkg/sourcelist.h>
22#include <apt-pkg/error.h>
23#include <apt-pkg/strutl.h>
24#include <apt-pkg/fileutl.h>
25#include <apt-pkg/tagfile.h>
26#include <apt-pkg/metaindex.h>
27#include <apt-pkg/acquire.h>
28#include <apt-pkg/hashes.h>
29#include <apt-pkg/indexfile.h>
30#include <apt-pkg/pkgcache.h>
31#include <apt-pkg/cacheiterators.h>
32#include <apt-pkg/pkgrecords.h>
33#include <apt-pkg/gpgv.h>
34
35#include <algorithm>
36#include <stddef.h>
37#include <stdlib.h>
38#include <string.h>
39#include <iostream>
40#include <vector>
41#include <sys/stat.h>
42#include <unistd.h>
43#include <errno.h>
44#include <string>
45#include <stdio.h>
46#include <ctime>
47#include <sstream>
48
49#include <apti18n.h>
50 /*}}}*/
51
52using namespace std;
53
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 /*}}}*/
66static std::string GetPartialFileName(std::string const &file) /*{{{*/
67{
68 std::string DestFile = _config->FindDir("Dir::State::lists") + "partial/";
69 DestFile += file;
70 return DestFile;
71}
72 /*}}}*/
73static std::string GetPartialFileNameFromURI(std::string const &uri) /*{{{*/
74{
75 return GetPartialFileName(URItoFileName(uri));
76}
77 /*}}}*/
78static std::string GetFinalFileNameFromURI(std::string const &uri) /*{{{*/
79{
80 return _config->FindDir("Dir::State::lists") + URItoFileName(uri);
81}
82 /*}}}*/
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 /*}}}*/
98static std::string GetCompressedFileName(IndexTarget const &Target, std::string const &Name, std::string const &Ext) /*{{{*/
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
105 if (Target.URI.substr(0,6) == "cdrom:")
106 return Name;
107
108 // adjust DestFile if its compressed on disk
109 if (Target.KeepCompressed == true)
110 return Name + '.' + Ext;
111 return Name;
112}
113 /*}}}*/
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 /*}}}*/
126static std::string GetExistingFilename(std::string const &File) /*{{{*/
127{
128 if (RealFileExists(File))
129 return File;
130 for (auto const &type : APT::Configuration::getCompressorExtensions())
131 {
132 std::string const Final = File + type;
133 if (RealFileExists(Final))
134 return Final;
135 }
136 return "";
137}
138 /*}}}*/
139
140static bool MessageInsecureRepository(bool const isError, std::string const &msg)/*{{{*/
141{
142 if (isError)
143 {
144 _error->Error("%s", msg.c_str());
145 _error->Notice("%s", _("Updating from such a repository can't be done securely, and is therefore disabled by default."));
146 }
147 else
148 {
149 _error->Warning("%s", msg.c_str());
150 _error->Notice("%s", _("Data from such a repository can't be authenticated and is therefore potentially dangerous to use."));
151 }
152 _error->Notice("%s", _("See apt-secure(8) manpage for repository creation and user configuration details."));
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)
166 return true;
167
168 if (_config->FindB("Acquire::AllowInsecureRepositories") == true)
169 {
170 MessageInsecureRepository(false, msg, repo);
171 return true;
172 }
173
174 MessageInsecureRepository(true, msg, repo);
175 TransactionManager->AbortTransaction();
176 I->Status = pkgAcquire::Item::StatError;
177 return false;
178}
179 /*}}}*/
180static HashStringList GetExpectedHashesFromFor(metaIndex * const Parser, std::string const &MetaKey)/*{{{*/
181{
182 if (Parser == NULL)
183 return HashStringList();
184 metaIndex::checkSum * const R = Parser->Lookup(MetaKey);
185 if (R == NULL)
186 return HashStringList();
187 return R->Hashes;
188}
189 /*}}}*/
190
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 */
204 return TransactionManager->MetaIndexParser != NULL &&
205 TransactionManager->MetaIndexParser->GetLoadedSuccessfully() == metaIndex::TRI_YES;
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{
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;
230 return false;
231}
232HashStringList pkgAcqIndexDiffs::GetExpectedHashes() const
233{
234 if (State == StateFetchDiff)
235 return available_patches[0].download_hashes;
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 */
244 if (State == StateFetchDiff)
245 return patch.download_hashes.empty() == false;
246 return State == StateApplyDiff;
247}
248HashStringList pkgAcqIndexMergeDiffs::GetExpectedHashes() const
249{
250 if (State == StateFetchDiff)
251 return patch.download_hashes;
252 else if (State == StateApplyDiff)
253 return GetExpectedHashesFor(Target.MetaKey);
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{
328 std::string const FinalFile = GetFinalFileNameFromURI(Target.URI);
329 return GetCompressedFileName(Target, FinalFile, CurrentCompressionExtension);
330}
331std::string pkgAcqMetaSig::GetFinalFilename() const
332{
333 return GetFinalFileNameFromURI(Target.URI);
334}
335std::string pkgAcqBaseIndex::GetFinalFilename() const
336{
337 return GetFinalFileNameFromURI(Target.URI);
338}
339std::string pkgAcqMetaBase::GetFinalFilename() const
340{
341 return GetFinalFileNameFromURI(Target.URI);
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{
351 return Target.MetaKey;
352}
353std::string pkgAcqIndex::GetMetaKey() const
354{
355 if (Stage == STAGE_DECOMPRESS_AND_VERIFY || CurrentCompressionExtension == "uncompressed")
356 return Target.MetaKey;
357 return Target.MetaKey + "." + CurrentCompressionExtension;
358}
359std::string pkgAcqDiffIndex::GetMetaKey() const
360{
361 return Target.MetaKey + ".diff/Index";
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:
380 if(PartialFile.empty() == false)
381 {
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;
411
412 } else {
413 if(Debug == true)
414 std::clog << "rm " << DestFile << " # " << DescURI() << std::endl;
415 if (RemoveFile("TransactionCommit", DestFile) == false)
416 return false;
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();
441 if (PartialFile.empty() == false && flExtension(PartialFile) != CurrentCompressionExtension)
442 RemoveFile("TransactionAbort", PartialFile);
443 }
444 break;
445 case TransactionCommit:
446 if (EraseFileName.empty() == false)
447 RemoveFile("TransactionCommit", EraseFileName);
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:
462 std::string const Partial = GetPartialFileNameFromURI(Target.URI);
463 RemoveFile("TransactionAbort", Partial);
464 break;
465 }
466
467 return true;
468}
469 /*}}}*/
470
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{
476 IndexTarget const Target;
477 public:
478 virtual std::string DescURI() const APT_OVERRIDE {return Target.URI;};
479 virtual HashStringList GetExpectedHashes() const APT_OVERRIDE {return HashStringList();};
480
481 NoActionItem(pkgAcquire * const Owner, IndexTarget const &Target) :
482 pkgAcquire::Item(Owner), Target(Target)
483 {
484 Status = StatDone;
485 DestFile = GetFinalFileNameFromURI(Target.URI);
486 }
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 }
493};
494 /*}}}*/
495
496// Acquire::Item::Item - Constructor /*{{{*/
497APT_IGNORE_DEPRECATED_PUSH
498pkgAcquire::Item::Item(pkgAcquire * const owner) :
499 FileSize(0), PartialSize(0), Mode(0), ID(0), Complete(false), Local(false),
500 QueueCounter(0), ExpectedAdditionalItems(0), Owner(owner), d(NULL)
501{
502 Owner->Add(this);
503 Status = StatIdle;
504}
505APT_IGNORE_DEPRECATED_POP
506 /*}}}*/
507// Acquire::Item::~Item - Destructor /*{{{*/
508pkgAcquire::Item::~Item()
509{
510 Owner->Remove(this);
511}
512 /*}}}*/
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 /*}}}*/
532APT_CONST pkgAcquire::ItemDesc &pkgAcquire::Item::GetItemDesc() /*{{{*/
533{
534 return Desc;
535}
536 /*}}}*/
537APT_CONST bool pkgAcquire::Item::IsTrusted() const /*{{{*/
538{
539 return false;
540}
541 /*}}}*/
542// Acquire::Item::Failed - Item failed to download /*{{{*/
543// ---------------------------------------------------------------------
544/* We return to an idle state if there are still other queues that could
545 fetch this object */
546void pkgAcquire::Item::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)
547{
548 if(ErrorText.empty())
549 ErrorText = LookupTag(Message,"Message");
550 if (QueueCounter <= 1)
551 {
552 /* This indicates that the file is not available right now but might
553 be sometime later. If we do a retry cycle then this should be
554 retried [CDROMs] */
555 if (Cnf != NULL && Cnf->LocalOnly == true &&
556 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
557 {
558 Status = StatIdle;
559 Dequeue();
560 return;
561 }
562
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 }
575 Complete = false;
576 Dequeue();
577 }
578
579 string const FailReason = LookupTag(Message, "FailReason");
580 if (FailReason == "MaximumSizeExceeded")
581 RenameOnError(MaximumSizeExceeded);
582 else if (Status == StatAuthError)
583 RenameOnError(HashSumMismatch);
584
585 // report mirror failure back to LP if we actually use a mirror
586 if (FailReason.empty() == false)
587 ReportMirrorFailure(FailReason);
588 else
589 ReportMirrorFailure(ErrorText);
590
591 if (QueueCounter > 1)
592 Status = StatIdle;
593}
594 /*}}}*/
595// Acquire::Item::Start - Item has begun to download /*{{{*/
596// ---------------------------------------------------------------------
597/* Stash status and the file size. Note that setting Complete means
598 sub-phases of the acquire process such as decompresion are operating */
599void pkgAcquire::Item::Start(string const &/*Message*/, unsigned long long const Size)
600{
601 Status = StatFetching;
602 ErrorText.clear();
603 if (FileSize == 0 && Complete == false)
604 FileSize = Size;
605}
606 /*}}}*/
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 /*}}}*/
624// Acquire::Item::Done - Item downloaded OK /*{{{*/
625void pkgAcquire::Item::Done(string const &/*Message*/, HashStringList const &Hashes,
626 pkgAcquire::MethodConfig const * const /*Cnf*/)
627{
628 // We just downloaded something..
629 if (FileSize == 0)
630 {
631 unsigned long long const downloadedSize = Hashes.FileSize();
632 if (downloadedSize != 0)
633 {
634 FileSize = downloadedSize;
635 }
636 }
637 Status = StatDone;
638 ErrorText = string();
639 Owner->Dequeue(this);
640}
641 /*}}}*/
642// Acquire::Item::Rename - Rename a file /*{{{*/
643// ---------------------------------------------------------------------
644/* This helper function is used by a lot of item methods as their final
645 step */
646bool pkgAcquire::Item::Rename(string const &From,string const &To)
647{
648 if (From == To || rename(From.c_str(),To.c_str()) == 0)
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;
655 if (ErrorText.empty())
656 ErrorText = S;
657 else
658 ErrorText = ErrorText + ": " + S;
659 return false;
660}
661 /*}}}*/
662void pkgAcquire::Item::Dequeue() /*{{{*/
663{
664 Owner->Dequeue(this);
665}
666 /*}}}*/
667bool pkgAcquire::Item::RenameOnError(pkgAcquire::Item::RenameOnErrorState const error)/*{{{*/
668{
669 if (RealFileExists(DestFile))
670 Rename(DestFile, DestFile + ".FAILED");
671
672 std::string errtext;
673 switch (error)
674 {
675 case HashSumMismatch:
676 errtext = _("Hash Sum mismatch");
677 Status = StatAuthError;
678 ReportMirrorFailure("HashChecksumFailure");
679 break;
680 case SizeMismatch:
681 errtext = _("Size mismatch");
682 Status = StatAuthError;
683 ReportMirrorFailure("SizeFailure");
684 break;
685 case InvalidFormat:
686 errtext = _("Invalid file format");
687 Status = StatError;
688 // do not report as usually its not the mirrors fault, but Portal/Proxy
689 break;
690 case SignatureError:
691 errtext = _("Signature error");
692 Status = StatError;
693 break;
694 case NotClearsigned:
695 strprintf(errtext, _("Clearsigned file isn't valid, got '%s' (does the network require authentication?)"), "NOSPLIT");
696 Status = StatAuthError;
697 break;
698 case MaximumSizeExceeded:
699 // the method is expected to report a good error for this
700 Status = StatError;
701 break;
702 case PDiffError:
703 // no handling here, done by callers
704 break;
705 }
706 if (ErrorText.empty())
707 ErrorText = errtext;
708 return false;
709}
710 /*}}}*/
711void pkgAcquire::Item::SetActiveSubprocess(const std::string &subprocess)/*{{{*/
712{
713 ActiveSubprocess = subprocess;
714 APT_IGNORE_DEPRECATED(Mode = ActiveSubprocess.c_str();)
715}
716 /*}}}*/
717// Acquire::Item::ReportMirrorFailure /*{{{*/
718void pkgAcquire::Item::ReportMirrorFailure(string const &FailCode)
719{
720 // we only act if a mirror was used at all
721 if(UsedMirror.empty())
722 return;
723#if 0
724 std::cerr << "\nReportMirrorFailure: "
725 << UsedMirror
726 << " Uri: " << DescURI()
727 << " FailCode: "
728 << FailCode << std::endl;
729#endif
730 string report = _config->Find("Methods::Mirror::ProblemReporting",
731 "/usr/lib/apt/apt-report-mirror-failure");
732 if(!FileExists(report))
733 return;
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
742 pid_t pid = ExecFork();
743 if(pid < 0)
744 {
745 _error->Error("ReportMirrorFailure Fork failed");
746 return;
747 }
748 else if(pid == 0)
749 {
750 execvp(Args[0], (char**)Args.data());
751 std::cerr << "Could not exec " << Args[0] << std::endl;
752 _exit(100);
753 }
754 if(!ExecWait(pid, "report-mirror-failure"))
755 {
756 _error->Warning("Couldn't report problem to '%s'",
757 _config->Find("Methods::Mirror::ProblemReporting").c_str());
758 }
759}
760 /*}}}*/
761std::string pkgAcquire::Item::HashSum() const /*{{{*/
762{
763 HashStringList const hashes = GetExpectedHashes();
764 HashString const * const hs = hashes.find(NULL);
765 return hs != NULL ? hs->toStr() : "";
766}
767 /*}}}*/
768
769pkgAcqTransactionItem::pkgAcqTransactionItem(pkgAcquire * const Owner, /*{{{*/
770 pkgAcqMetaClearSig * const transactionManager, IndexTarget const &target) :
771 pkgAcquire::Item(Owner), d(NULL), Target(target), TransactionManager(transactionManager)
772{
773 if (TransactionManager != this)
774 TransactionManager->Add(this);
775}
776 /*}}}*/
777pkgAcqTransactionItem::~pkgAcqTransactionItem() /*{{{*/
778{
779}
780 /*}}}*/
781HashStringList pkgAcqTransactionItem::GetExpectedHashesFor(std::string const &MetaKey) const /*{{{*/
782{
783 return GetExpectedHashesFromFor(TransactionManager->MetaIndexParser, MetaKey);
784}
785 /*}}}*/
786
787// AcqMetaBase - Constructor /*{{{*/
788pkgAcqMetaBase::pkgAcqMetaBase(pkgAcquire * const Owner,
789 pkgAcqMetaClearSig * const TransactionManager,
790 std::vector<IndexTarget> const &IndexTargets,
791 IndexTarget const &DataTarget)
792: pkgAcqTransactionItem(Owner, TransactionManager, DataTarget), d(NULL),
793 IndexTargets(IndexTargets),
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;
809
810 // ensure the toplevel is in error state too
811 for (std::vector<pkgAcqTransactionItem*>::iterator I = Transaction.begin();
812 I != Transaction.end(); ++I)
813 {
814 (*I)->TransactionState(TransactionAbort);
815 }
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;
842
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();
851}
852 /*}}}*/
853// AcqMetaBase::TransactionStageCopy - Stage a file for copying /*{{{*/
854void pkgAcqMetaBase::TransactionStageCopy(pkgAcqTransactionItem * const I,
855 const std::string &From,
856 const std::string &To)
857{
858 I->PartialFile = From;
859 I->DestFile = To;
860}
861 /*}}}*/
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. "
883 "GPG error: %s: %s"),
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());
893 I->Status = StatAuthError;
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 /*{{{*/
906// ---------------------------------------------------------------------
907string pkgAcqMetaBase::Custom600Headers() const
908{
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;
914
915 string const FinalFile = GetFinalFilename();
916 struct stat Buf;
917 if (stat(FinalFile.c_str(),&Buf) == 0)
918 Header += "\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
919
920 return Header;
921}
922 /*}}}*/
923// AcqMetaBase::QueueForSignatureVerify /*{{{*/
924void pkgAcqMetaBase::QueueForSignatureVerify(pkgAcqTransactionItem * const I, std::string const &File, std::string const &Signature)
925{
926 AuthPass = true;
927 I->Desc.URI = "gpgv:" + Signature;
928 I->DestFile = File;
929 QueueURI(I->Desc);
930 I->SetActiveSubprocess("gpgv");
931}
932 /*}}}*/
933// AcqMetaBase::CheckDownloadDone /*{{{*/
934bool pkgAcqMetaBase::CheckDownloadDone(pkgAcqTransactionItem * const I, const std::string &Message, HashStringList const &Hashes) const
935{
936 // We have just finished downloading a Release file (it is not
937 // verified yet)
938
939 std::string const FileName = LookupTag(Message,"Filename");
940 if (FileName != I->DestFile && RealFileExists(I->DestFile) == false)
941 {
942 I->Local = true;
943 I->Desc.URI = "copy:" + FileName;
944 I->QueueURI(I->Desc);
945 return false;
946 }
947
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())
951 {
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)
955 {
956 IMSHit = true;
957 RemoveFile("CheckDownloadDone", I->DestFile);
958 }
959 }
960
961 if(IMSHit == true)
962 {
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();
968 }
969
970 // set Item to complete as the remaining work is all local (verify etc)
971 I->Complete = true;
972
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
982
983 if (TransactionManager->IMSHit == false)
984 {
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"))
990 {
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 {
1001 TransactionManager->LastMetaIndexParser = TransactionManager->MetaIndexParser->UnloadedClone();
1002 if (TransactionManager->LastMetaIndexParser != NULL)
1003 {
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();
1016 }
1017 }
1018 }
1019
1020 if (TransactionManager->MetaIndexParser->Load(DestFile, &ErrorText) == false)
1021 {
1022 Status = StatAuthError;
1023 return false;
1024 }
1025
1026 if (!VerifyVendor(Message))
1027 {
1028 Status = StatAuthError;
1029 return false;
1030 }
1031
1032 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1033 std::cerr << "Signature verification succeeded: "
1034 << DestFile << std::endl;
1035
1036 // Download further indexes with verification
1037 QueueIndexes(true);
1038
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
1047 bool metaBaseSupportsByHash = false;
1048 if (TransactionManager != NULL && TransactionManager->MetaIndexParser != NULL)
1049 metaBaseSupportsByHash = TransactionManager->MetaIndexParser->GetSupportsAcquireByHash();
1050
1051 for (std::vector <IndexTarget>::iterator Target = IndexTargets.begin();
1052 Target != IndexTargets.end();
1053 ++Target)
1054 {
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.
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 }
1068
1069 bool trypdiff = Target->OptionBool(IndexTarget::PDIFFS);
1070 if (verify == true)
1071 {
1072 if (TransactionManager->MetaIndexParser->Exists(Target->MetaKey) == false)
1073 {
1074 // optional targets that we do not have in the Release file are skipped
1075 if (Target->IsOptional)
1076 continue;
1077
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
1094 Status = StatAuthError;
1095 strprintf(ErrorText, _("Unable to find expected entry '%s' in Release file (Wrong sources.list entry or malformed file)"), Target->MetaKey.c_str());
1096 return;
1097 }
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 }
1108
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)
1118 {
1119 std::ostringstream os;
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 ";
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
1136 std::string filename = GetExistingFilename(GetFinalFileNameFromURI(Target->URI));
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)
1143 {
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();
1151 }
1152 else
1153 filename.clear();
1154 }
1155 else
1156 trypdiff = false; // no file to patch
1157
1158 if (filename.empty() == false)
1159 {
1160 new NoActionItem(Owner, *Target, filename);
1161 std::string const idxfilename = GetFinalFileNameFromURI(Target->URI + ".diff/Index");
1162 if (FileExists(idxfilename))
1163 new NoActionItem(Owner, *Target, idxfilename);
1164 continue;
1165 }
1166
1167 // check if we have patches available
1168 trypdiff &= TransactionManager->MetaIndexParser->Exists(Target->MetaKey + ".diff/Index");
1169 }
1170 else
1171 {
1172 // if we have no file to patch, no point in trying
1173 trypdiff &= (GetExistingFilename(GetFinalFileNameFromURI(Target->URI)).empty() == false);
1174 }
1175
1176 // no point in patching from local sources
1177 if (trypdiff)
1178 {
1179 std::string const proto = Target->URI.substr(0, strlen("file:/"));
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)
1186 new pkgAcqDiffIndex(Owner, TransactionManager, *Target);
1187 else
1188 new pkgAcqIndex(Owner, TransactionManager, *Target);
1189 }
1190}
1191 /*}}}*/
1192bool pkgAcqMetaBase::VerifyVendor(string const &Message) /*{{{*/
1193{
1194 string::size_type pos;
1195
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)
1203 {
1204 string::size_type start = pos+strlen("NO_PUBKEY ");
1205 string Fingerprint = Message.substr(start, Message.find("\n")-start);
1206 missingkeys += (Fingerprint);
1207 }
1208 if(!missingkeys.empty())
1209 _error->Warning("%s", (msg + missingkeys).c_str());
1210
1211 string Transformed = TransactionManager->MetaIndexParser->GetExpectedDist();
1212
1213 if (Transformed == "../project/experimental")
1214 {
1215 Transformed = "experimental";
1216 }
1217
1218 pos = Transformed.rfind('/');
1219 if (pos != string::npos)
1220 {
1221 Transformed = Transformed.substr(0, pos);
1222 }
1223
1224 if (Transformed == ".")
1225 {
1226 Transformed = "";
1227 }
1228
1229 if (TransactionManager->MetaIndexParser->GetValidUntil() > 0)
1230 {
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
1237 // the time since then the file is invalid - formatted in the same way as in
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."),
1241 Target.URI.c_str(), TimeToStr(invalid_since).c_str());
1242 if (ErrorText.empty())
1243 ErrorText = errmsg;
1244 return _error->Error("%s", errmsg.c_str());
1245 }
1246 }
1247
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())
1252 {
1253 TransactionManager->IMSHit = true;
1254 RemoveFile("VerifyVendor", DestFile);
1255 PartialFile = DestFile = GetFinalFilename();
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;
1260 TransactionManager->LastMetaIndexParser = NULL;
1261 }
1262
1263 if (_config->FindB("Debug::pkgAcquire::Auth", false))
1264 {
1265 std::cerr << "Got Codename: " << TransactionManager->MetaIndexParser->GetCodename() << std::endl;
1266 std::cerr << "Expecting Dist: " << TransactionManager->MetaIndexParser->GetExpectedDist() << std::endl;
1267 std::cerr << "Transformed Dist: " << Transformed << std::endl;
1268 }
1269
1270 if (TransactionManager->MetaIndexParser->CheckDist(Transformed) == false)
1271 {
1272 // This might become fatal one day
1273// Status = StatAuthError;
1274// ErrorText = "Conflicting distribution; expected "
1275// + MetaIndexParser->GetExpectedDist() + " but got "
1276// + MetaIndexParser->GetCodename();
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(),
1283 TransactionManager->MetaIndexParser->GetCodename().c_str());
1284 }
1285 }
1286
1287 return true;
1288}
1289 /*}}}*/
1290pkgAcqMetaBase::~pkgAcqMetaBase()
1291{
1292}
1293
1294pkgAcqMetaClearSig::pkgAcqMetaClearSig(pkgAcquire * const Owner, /*{{{*/
1295 IndexTarget const &ClearsignedTarget,
1296 IndexTarget const &DetachedDataTarget, IndexTarget const &DetachedSigTarget,
1297 std::vector<IndexTarget> const &IndexTargets,
1298 metaIndex * const MetaIndexParser) :
1299 pkgAcqMetaIndex(Owner, this, ClearsignedTarget, DetachedSigTarget, IndexTargets),
1300 d(NULL), ClearsignedTarget(ClearsignedTarget),
1301 DetachedDataTarget(DetachedDataTarget),
1302 MetaIndexParser(MetaIndexParser), LastMetaIndexParser(NULL)
1303{
1304 // index targets + (worst case:) Release/Release.gpg
1305 ExpectedAdditionalItems = IndexTargets.size() + 2;
1306 TransactionManager->Add(this);
1307}
1308 /*}}}*/
1309pkgAcqMetaClearSig::~pkgAcqMetaClearSig() /*{{{*/
1310{
1311 if (LastMetaIndexParser != NULL)
1312 delete LastMetaIndexParser;
1313}
1314 /*}}}*/
1315// pkgAcqMetaClearSig::Custom600Headers - Insert custom request headers /*{{{*/
1316string pkgAcqMetaClearSig::Custom600Headers() const
1317{
1318 string Header = pkgAcqMetaBase::Custom600Headers();
1319 Header += "\nFail-Ignore: true";
1320 std::string const key = TransactionManager->MetaIndexParser->GetSignedBy();
1321 if (key.empty() == false)
1322 Header += "\nSigned-By: " + key;
1323
1324 return Header;
1325}
1326 /*}}}*/
1327bool pkgAcqMetaClearSig::VerifyDone(std::string const &Message, /*{{{*/
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}
1337 /*}}}*/
1338// pkgAcqMetaClearSig::Done - We got a file /*{{{*/
1339void pkgAcqMetaClearSig::Done(std::string const &Message,
1340 HashStringList const &Hashes,
1341 pkgAcquire::MethodConfig const * const Cnf)
1342{
1343 Item::Done(Message, Hashes, Cnf);
1344
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
1360 new NoActionItem(Owner, DetachedDataTarget);
1361 new NoActionItem(Owner, DetachedSigTarget);
1362 }
1363 }
1364}
1365 /*}}}*/
1366void pkgAcqMetaClearSig::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf) /*{{{*/
1367{
1368 Item::Failed(Message, Cnf);
1369
1370 // we failed, we will not get additional items from this method
1371 ExpectedAdditionalItems = 0;
1372
1373 if (AuthPass == false)
1374 {
1375 if (Status == StatAuthError || Status == StatTransientNetworkError)
1376 {
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).
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
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
1391 new pkgAcqMetaIndex(Owner, TransactionManager, DetachedDataTarget, DetachedSigTarget, IndexTargets);
1392 }
1393 else
1394 {
1395 if(CheckStopAuthentication(this, Message))
1396 return;
1397
1398 // No Release file was present, or verification failed, so fall
1399 // back to queueing Packages files without verification
1400 // only allow going further if the user explicitly wants it
1401 if(AllowInsecureRepositories(_("The repository '%s' is not signed."), ClearsignedTarget.Description, TransactionManager->MetaIndexParser, TransactionManager, this) == true)
1402 {
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))
1415 {
1416 // open the last Release if we have it
1417 if (TransactionManager->IMSHit == false)
1418 {
1419 TransactionManager->LastMetaIndexParser = TransactionManager->MetaIndexParser->UnloadedClone();
1420 if (TransactionManager->LastMetaIndexParser != NULL)
1421 {
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();
1434 }
1435 }
1436 }
1437
1438 // we parse the indexes here because at this point the user wanted
1439 // a repository that may potentially harm him
1440 if (TransactionManager->MetaIndexParser->Load(PartialRelease, &ErrorText) == false || VerifyVendor(Message) == false)
1441 /* expired Release files are still a problem you need extra force for */;
1442 else
1443 QueueIndexes(true);
1444 }
1445 }
1446}
1447 /*}}}*/
1448
1449pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire * const Owner, /*{{{*/
1450 pkgAcqMetaClearSig * const TransactionManager,
1451 IndexTarget const &DataTarget,
1452 IndexTarget const &DetachedSigTarget,
1453 vector<IndexTarget> const &IndexTargets) :
1454 pkgAcqMetaBase(Owner, TransactionManager, IndexTargets, DataTarget), d(NULL),
1455 DetachedSigTarget(DetachedSigTarget)
1456{
1457 if(_config->FindB("Debug::Acquire::Transaction", false) == true)
1458 std::clog << "New pkgAcqMetaIndex with TransactionManager "
1459 << this->TransactionManager << std::endl;
1460
1461 DestFile = GetPartialFileNameFromURI(DataTarget.URI);
1462
1463 // Create the item
1464 Desc.Description = DataTarget.Description;
1465 Desc.Owner = this;
1466 Desc.ShortDesc = DataTarget.ShortDesc;
1467 Desc.URI = DataTarget.URI;
1468
1469 // we expect more item
1470 ExpectedAdditionalItems = IndexTargets.size();
1471 QueueURI(Desc);
1472}
1473 /*}}}*/
1474void pkgAcqMetaIndex::Done(string const &Message, /*{{{*/
1475 HashStringList const &Hashes,
1476 pkgAcquire::MethodConfig const * const Cfg)
1477{
1478 Item::Done(Message,Hashes,Cfg);
1479
1480 if(CheckDownloadDone(this, Message, Hashes))
1481 {
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
1485 new pkgAcqMetaSig(Owner, TransactionManager, DetachedSigTarget, this);
1486 }
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;
1495
1496 // No Release file was present so fall
1497 // back to queueing Packages files without verification
1498 // only allow going further if the user explicitly wants it
1499 if(AllowInsecureRepositories(_("The repository '%s' does not have a Release file."), Target.Description, TransactionManager->MetaIndexParser, TransactionManager, this) == true)
1500 {
1501 // ensure old Release files are removed
1502 TransactionManager->TransactionStageRemoval(this, GetFinalFilename());
1503
1504 // queue without any kind of hashsum support
1505 QueueIndexes(false);
1506 }
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{
1520 return Target.URI;
1521}
1522 /*}}}*/
1523pkgAcqMetaIndex::~pkgAcqMetaIndex() {}
1524
1525// AcqMetaSig::AcqMetaSig - Constructor /*{{{*/
1526pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire * const Owner,
1527 pkgAcqMetaClearSig * const TransactionManager,
1528 IndexTarget const &Target,
1529 pkgAcqMetaIndex * const MetaIndex) :
1530 pkgAcqTransactionItem(Owner, TransactionManager, Target), d(NULL), MetaIndex(MetaIndex)
1531{
1532 DestFile = GetPartialFileNameFromURI(Target.URI);
1533
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
1537 RemoveFile("pkgAcqMetaSig", DestFile);
1538
1539 // set the TransactionManager
1540 if(_config->FindB("Debug::Acquire::Transaction", false) == true)
1541 std::clog << "New pkgAcqMetaSig with TransactionManager "
1542 << TransactionManager << std::endl;
1543
1544 // Create the item
1545 Desc.Description = Target.Description;
1546 Desc.Owner = this;
1547 Desc.ShortDesc = Target.ShortDesc;
1548 Desc.URI = Target.URI;
1549
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);
1562}
1563 /*}}}*/
1564pkgAcqMetaSig::~pkgAcqMetaSig() /*{{{*/
1565{
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;
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)
1583 {
1584 DestFile = MetaIndexFileSignature;
1585 MetaIndexFileSignature.clear();
1586 }
1587 Item::Done(Message, Hashes, Cfg);
1588
1589 if(MetaIndex->AuthPass == false)
1590 {
1591 if(MetaIndex->CheckDownloadDone(this, Message, Hashes) == true)
1592 {
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());
1606 }
1607 }
1608}
1609 /*}}}*/
1610void pkgAcqMetaSig::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)/*{{{*/
1611{
1612 Item::Failed(Message,Cnf);
1613
1614 // check if we need to fail at this point
1615 if (MetaIndex->AuthPass == true && MetaIndex->CheckStopAuthentication(this, Message))
1616 return;
1617
1618 string const FinalRelease = MetaIndex->GetFinalFilename();
1619 string const FinalReleasegpg = GetFinalFilename();
1620 string const FinalInRelease = TransactionManager->GetFinalFilename();
1621
1622 if (RealFileExists(FinalReleasegpg) || RealFileExists(FinalInRelease))
1623 {
1624 std::string downgrade_msg;
1625 strprintf(downgrade_msg, _("The repository '%s' is no longer signed."),
1626 MetaIndex->Target.Description.c_str());
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 {
1637 MessageInsecureRepository(true, downgrade_msg);
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 }
1645
1646 // ensures that a Release.gpg file in the lists/ is removed by the transaction
1647 TransactionManager->TransactionStageRemoval(this, DestFile);
1648
1649 // only allow going further if the user explicitly wants it
1650 if (AllowInsecureRepositories(_("The repository '%s' is not signed."), MetaIndex->Target.Description, TransactionManager->MetaIndexParser, TransactionManager, this) == true)
1651 {
1652 if (RealFileExists(FinalReleasegpg) || RealFileExists(FinalInRelease))
1653 {
1654 // open the last Release if we have it
1655 if (TransactionManager->IMSHit == false)
1656 {
1657 TransactionManager->LastMetaIndexParser = TransactionManager->MetaIndexParser->UnloadedClone();
1658 if (TransactionManager->LastMetaIndexParser != NULL)
1659 {
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();
1672 }
1673 }
1674 }
1675
1676 // we parse the indexes here because at this point the user wanted
1677 // a repository that may potentially harm him
1678 bool const GoodLoad = TransactionManager->MetaIndexParser->Load(MetaIndex->DestFile, &ErrorText);
1679 if (MetaIndex->VerifyVendor(Message) == false)
1680 /* expired Release files are still a problem you need extra force for */;
1681 else
1682 MetaIndex->QueueIndexes(GoodLoad);
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;
1693 }
1694}
1695 /*}}}*/
1696
1697
1698// AcqBaseIndex - Constructor /*{{{*/
1699pkgAcqBaseIndex::pkgAcqBaseIndex(pkgAcquire * const Owner,
1700 pkgAcqMetaClearSig * const TransactionManager,
1701 IndexTarget const &Target)
1702: pkgAcqTransactionItem(Owner, TransactionManager, Target), d(NULL)
1703{
1704}
1705 /*}}}*/
1706pkgAcqBaseIndex::~pkgAcqBaseIndex() {}
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,
1716 pkgAcqMetaClearSig * const TransactionManager,
1717 IndexTarget const &Target)
1718 : pkgAcqBaseIndex(Owner, TransactionManager, Target), d(NULL), diffs(NULL)
1719{
1720 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
1721
1722 Desc.Owner = this;
1723 Desc.Description = Target.Description + ".diff/Index";
1724 Desc.ShortDesc = Target.ShortDesc;
1725 Desc.URI = Target.URI + ".diff/Index";
1726
1727 DestFile = GetPartialFileNameFromURI(Desc.URI);
1728
1729 if(Debug)
1730 std::clog << "pkgAcqDiffIndex: " << Desc.URI << std::endl;
1731
1732 QueueURI(Desc);
1733}
1734 /*}}}*/
1735// AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
1736// ---------------------------------------------------------------------
1737/* The only header we use is the last-modified header. */
1738string pkgAcqDiffIndex::Custom600Headers() const
1739{
1740 if (TransactionManager->LastMetaIndexParser != NULL)
1741 return "\nIndex-File: true";
1742
1743 string const Final = GetFinalFilename();
1744
1745 if(Debug)
1746 std::clog << "Custom600Header-IMS: " << Final << std::endl;
1747
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);
1760}
1761 /*}}}*/
1762bool pkgAcqDiffIndex::ParseDiffIndex(string const &IndexDiffFile) /*{{{*/
1763{
1764 // failing here is fine: our caller will take care of trying to
1765 // get the complete file if patching fails
1766 if(Debug)
1767 std::clog << "pkgAcqDiffIndex::ParseIndexDiff() " << IndexDiffFile
1768 << std::endl;
1769
1770 FileFd Fd(IndexDiffFile,FileFd::ReadOnly);
1771 pkgTagFile TF(&Fd);
1772 if (Fd.IsOpen() == false || Fd.Failed())
1773 return false;
1774
1775 pkgTagSection Tags;
1776 if(unlikely(TF.Step(Tags) == false))
1777 return false;
1778
1779 HashStringList ServerHashes;
1780 unsigned long long ServerSize = 0;
1781
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;
1789
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 }
1801
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;
1807 }
1808
1809 std::string const CurrentPackagesFile = GetFinalFileNameFromURI(Target.URI);
1810 HashStringList const TargetFileHashes = GetExpectedHashesFor(Target.MetaKey);
1811 if (TargetFileHashes.usable() == false || ServerHashes != TargetFileHashes)
1812 {
1813 if (Debug == true)
1814 {
1815 std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": Index has different hashes than parser, probably older, so fail pdiffing" << std::endl;
1816 printHashSumComparision(CurrentPackagesFile, ServerHashes, TargetFileHashes);
1817 }
1818 return false;
1819 }
1820
1821 HashStringList LocalHashes;
1822 // try avoiding calculating the hash here as this is costly
1823 if (TransactionManager->LastMetaIndexParser != NULL)
1824 LocalHashes = GetExpectedHashesFromFor(TransactionManager->LastMetaIndexParser, Target.MetaKey);
1825 if (LocalHashes.usable() == false)
1826 {
1827 FileFd fd(CurrentPackagesFile, FileFd::ReadOnly, FileFd::Auto);
1828 Hashes LocalHashesCalc(ServerHashes);
1829 LocalHashesCalc.AddFD(fd);
1830 LocalHashes = LocalHashesCalc.GetHashStringList();
1831 }
1832
1833 if (ServerHashes == LocalHashes)
1834 {
1835 // we have the same sha1 as the server so we are done here
1836 if(Debug)
1837 std::clog << "pkgAcqDiffIndex: Package file " << CurrentPackagesFile << " is up-to-date" << std::endl;
1838 QueueOnIMSHit();
1839 return true;
1840 }
1841
1842 if(Debug)
1843 std::clog << "Server-Current: " << ServerHashes.find(NULL)->toStr() << " and we start at "
1844 << CurrentPackagesFile << " " << LocalHashes.FileSize() << " " << LocalHashes.find(NULL)->toStr() << std::endl;
1845
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
1854 // parse all of (provided) history
1855 vector<DiffInfo> available_patches;
1856 bool firstAcceptedHashes = true;
1857 for (auto type = types.crbegin(); type != types.crend(); ++type)
1858 {
1859 if (LocalHashes.find(*type) == NULL)
1860 continue;
1861
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;
1867
1868 string hash, filename;
1869 unsigned long long size;
1870 std::stringstream ss(tmp);
1871
1872 while (ss >> hash >> size >> filename)
1873 {
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 {
1881 if (cur->file != filename)
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));
1893 next.result_hashes.FileSize(size);
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 }
1903 }
1904 firstAcceptedHashes = false;
1905 }
1906
1907 if (unlikely(available_patches.empty() == true))
1908 {
1909 if (Debug)
1910 std::clog << "pkgAcqDiffIndex: " << IndexDiffFile << ": "
1911 << "Couldn't find any patches for the patch series." << std::endl;
1912 return false;
1913 }
1914
1915 for (auto type = types.crbegin(); type != types.crend(); ++type)
1916 {
1917 if (LocalHashes.find(*type) == NULL)
1918 continue;
1919
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;
1925
1926 string hash, filename;
1927 unsigned long long size;
1928 std::stringstream ss(tmp);
1929
1930 while (ss >> hash >> size >> filename)
1931 {
1932 if (unlikely(hash.empty() == true || filename.empty() == true))
1933 continue;
1934
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)
1938 {
1939 if (cur->file != filename)
1940 continue;
1941 if (cur->patch_hashes.empty())
1942 cur->patch_hashes.FileSize(size);
1943 cur->patch_hashes.push_back(HashString(*type, hash));
1944 break;
1945 }
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;
1951 break;
1952 }
1953 }
1954
1955 for (auto type = types.crbegin(); type != types.crend(); ++type)
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
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;
2007 }
2008
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 }
2016
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 }
2026
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)
2032 patchesSize += cur->patch_hashes.FileSize();
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 }
2041
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)
2054 {
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");
2058 }
2059
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
2082 if (pdiff_merge == false)
2083 new pkgAcqIndexDiffs(Owner, TransactionManager, Target, available_patches);
2084 else
2085 {
2086 diffs = new std::vector<pkgAcqIndexMergeDiffs*>(available_patches.size());
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;
2098}
2099 /*}}}*/
2100void pkgAcqDiffIndex::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)/*{{{*/
2101{
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);
2110}
2111 /*}}}*/
2112void pkgAcqDiffIndex::Done(string const &Message,HashStringList const &Hashes, /*{{{*/
2113 pkgAcquire::MethodConfig const * const Cnf)
2114{
2115 if(Debug)
2116 std::clog << "pkgAcqDiffIndex::Done(): " << Desc.URI << std::endl;
2117
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)
2125 {
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);
2130 return;
2131 }
2132
2133 TransactionManager->TransactionStageCopy(this, DestFile, FinalFile);
2134
2135 Complete = true;
2136 Status = StatDone;
2137 Dequeue();
2138
2139 return;
2140}
2141 /*}}}*/
2142pkgAcqDiffIndex::~pkgAcqDiffIndex()
2143{
2144 if (diffs != NULL)
2145 delete diffs;
2146}
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,
2154 pkgAcqMetaClearSig * const TransactionManager,
2155 IndexTarget const &Target,
2156 vector<DiffInfo> const &diffs)
2157 : pkgAcqBaseIndex(Owner, TransactionManager, Target), d(NULL),
2158 available_patches(diffs)
2159{
2160 DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target);
2161
2162 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
2163
2164 Desc.Owner = this;
2165 Description = Target.Description;
2166 Desc.ShortDesc = Target.ShortDesc;
2167
2168 if(available_patches.empty() == true)
2169 {
2170 // we are done (yeah!), check hashes against the final file
2171 DestFile = GetKeepCompressedFileName(GetFinalFileNameFromURI(Target.URI), Target);
2172 Finish(true);
2173 }
2174 else
2175 {
2176 State = StateFetchDiff;
2177 QueueNextDiff();
2178 }
2179}
2180 /*}}}*/
2181void pkgAcqIndexDiffs::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)/*{{{*/
2182{
2183 Item::Failed(Message,Cnf);
2184 Status = StatDone;
2185
2186 DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI), Target);
2187 if(Debug)
2188 std::clog << "pkgAcqIndexDiffs failed: " << Desc.URI << " with " << Message << std::endl
2189 << "Falling back to normal index file acquire " << std::endl;
2190 RenameOnError(PDiffError);
2191 std::string const patchname = GetDiffsPatchFileName(DestFile);
2192 if (RealFileExists(patchname))
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");
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)
2212 {
2213 std::string const Final = GetKeepCompressedFileName(GetFinalFilename(), Target);
2214 TransactionManager->TransactionStageCopy(this, DestFile, Final);
2215
2216 // this is for the "real" finish
2217 Complete = true;
2218 Status = StatDone;
2219 Dequeue();
2220 if(Debug)
2221 std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl;
2222 return;
2223 }
2224 else
2225 DestFile.clear();
2226
2227 if(Debug)
2228 std::clog << "Finishing: " << Desc.URI << std::endl;
2229 Complete = false;
2230 Status = StatDone;
2231 Dequeue();
2232 return;
2233}
2234 /*}}}*/
2235bool pkgAcqIndexDiffs::QueueNextDiff() /*{{{*/
2236{
2237 // calc sha1 of the just patched file
2238 std::string const PartialFile = GetExistingFilename(GetPartialFileNameFromURI(Target.URI));
2239 if(unlikely(PartialFile.empty()))
2240 {
2241 Failed("Message: The file " + GetPartialFileNameFromURI(Target.URI) + " isn't available", NULL);
2242 return false;
2243 }
2244
2245 FileFd fd(PartialFile, FileFd::ReadOnly, FileFd::Extension);
2246 Hashes LocalHashesCalc;
2247 LocalHashesCalc.AddFD(fd);
2248 HashStringList const LocalHashes = LocalHashesCalc.GetHashStringList();
2249
2250 if(Debug)
2251 std::clog << "QueueNextDiff: " << PartialFile << " (" << LocalHashes.find(NULL)->toStr() << ")" << std::endl;
2252
2253 HashStringList const TargetFileHashes = GetExpectedHashesFor(Target.MetaKey);
2254 if (unlikely(LocalHashes.usable() == false || TargetFileHashes.usable() == false))
2255 {
2256 Failed("Local/Expected hashes are not usable for " + PartialFile, NULL);
2257 return false;
2258 }
2259
2260 // final file reached before all patches are applied
2261 if(LocalHashes == TargetFileHashes)
2262 {
2263 Finish(true);
2264 return true;
2265 }
2266
2267 // remove all patches until the next matching patch is found
2268 // this requires the Index file to be ordered
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 }));
2273
2274 // error checking and falling back if no patch was found
2275 if(available_patches.empty() == true)
2276 {
2277 Failed("No patches left to reach target for " + PartialFile, NULL);
2278 return false;
2279 }
2280
2281 // queue the right diff
2282 Desc.URI = Target.URI + ".diff/" + available_patches[0].file + ".gz";
2283 Desc.Description = Description + " " + available_patches[0].file + string(".pdiff");
2284 DestFile = GetKeepCompressedFileName(GetPartialFileNameFromURI(Target.URI + ".diff/" + available_patches[0].file), Target);
2285
2286 if(Debug)
2287 std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl;
2288
2289 QueueURI(Desc);
2290
2291 return true;
2292}
2293 /*}}}*/
2294void pkgAcqIndexDiffs::Done(string const &Message, HashStringList const &Hashes, /*{{{*/
2295 pkgAcquire::MethodConfig const * const Cnf)
2296{
2297 if (Debug)
2298 std::clog << "pkgAcqIndexDiffs::Done(): " << Desc.URI << std::endl;
2299
2300 Item::Done(Message, Hashes, Cnf);
2301
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;
2342 }
2343}
2344 /*}}}*/
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 /*}}}*/
2357pkgAcqIndexDiffs::~pkgAcqIndexDiffs() {}
2358
2359// AcqIndexMergeDiffs::AcqIndexMergeDiffs - Constructor /*{{{*/
2360pkgAcqIndexMergeDiffs::pkgAcqIndexMergeDiffs(pkgAcquire * const Owner,
2361 pkgAcqMetaClearSig * const TransactionManager,
2362 IndexTarget const &Target,
2363 DiffInfo const &patch,
2364 std::vector<pkgAcqIndexMergeDiffs*> const * const allPatches)
2365 : pkgAcqBaseIndex(Owner, TransactionManager, Target), d(NULL),
2366 patch(patch), allPatches(allPatches), State(StateFetchDiff)
2367{
2368 Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
2369
2370 Desc.Owner = this;
2371 Description = Target.Description;
2372 Desc.ShortDesc = Target.ShortDesc;
2373 Desc.URI = Target.URI + ".diff/" + patch.file + ".gz";
2374 Desc.Description = Description + " " + patch.file + ".pdiff";
2375 DestFile = GetPartialFileNameFromURI(Desc.URI);
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;
2387
2388 Item::Failed(Message,Cnf);
2389 Status = StatDone;
2390
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;
2402 RenameOnError(PDiffError);
2403 std::string const patchname = GetPartialFileNameFromURI(Desc.URI);
2404 if (RealFileExists(patchname))
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");
2409 DestFile.clear();
2410 new pkgAcqIndex(Owner, TransactionManager, Target);
2411}
2412 /*}}}*/
2413void pkgAcqIndexMergeDiffs::Done(string const &Message, HashStringList const &Hashes, /*{{{*/
2414 pkgAcquire::MethodConfig const * const Cnf)
2415{
2416 if(Debug)
2417 std::clog << "pkgAcqIndexMergeDiffs::Done(): " << Desc.URI << std::endl;
2418
2419 Item::Done(Message, Hashes, Cnf);
2420
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);
2425
2426 switch (State)
2427 {
2428 case StateFetchDiff:
2429 Rename(DestFile, PatchFile);
2430
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;
2473 }
2474}
2475 /*}}}*/
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 /*}}}*/
2494pkgAcqIndexMergeDiffs::~pkgAcqIndexMergeDiffs() {}
2495
2496// AcqIndex::AcqIndex - Constructor /*{{{*/
2497pkgAcqIndex::pkgAcqIndex(pkgAcquire * const Owner,
2498 pkgAcqMetaClearSig * const TransactionManager,
2499 IndexTarget const &Target)
2500 : pkgAcqBaseIndex(Owner, TransactionManager, Target), d(NULL), Stage(STAGE_DOWNLOAD),
2501 CompressionExtensions(Target.Option(IndexTarget::COMPRESSIONTYPES))
2502{
2503 Init(Target.URI, Target.Description, Target.ShortDesc);
2504
2505 if(_config->FindB("Debug::Acquire::Transaction", false) == true)
2506 std::clog << "New pkgIndex with TransactionManager "
2507 << TransactionManager << std::endl;
2508}
2509 /*}}}*/
2510// AcqIndex::Init - defered Constructor /*{{{*/
2511static void NextCompressionExtension(std::string &CurrentCompressionExtension, std::string &CompressionExtensions, bool const preview)
2512{
2513 size_t const nextExt = CompressionExtensions.find(' ');
2514 if (nextExt == std::string::npos)
2515 {
2516 CurrentCompressionExtension = CompressionExtensions;
2517 if (preview == false)
2518 CompressionExtensions.clear();
2519 }
2520 else
2521 {
2522 CurrentCompressionExtension = CompressionExtensions.substr(0, nextExt);
2523 if (preview == false)
2524 CompressionExtensions = CompressionExtensions.substr(nextExt+1);
2525 }
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);
2534
2535 if (CurrentCompressionExtension == "uncompressed")
2536 {
2537 Desc.URI = URI;
2538 }
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 }
2563 else if (unlikely(CurrentCompressionExtension.empty()))
2564 return;
2565 else
2566 {
2567 Desc.URI = URI + '.' + CurrentCompressionExtension;
2568 DestFile = DestFile + '.' + CurrentCompressionExtension;
2569 }
2570
2571
2572 Desc.Description = URIDesc;
2573 Desc.Owner = this;
2574 Desc.ShortDesc = ShortDesc;
2575
2576 QueueURI(Desc);
2577}
2578 /*}}}*/
2579// AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
2580// ---------------------------------------------------------------------
2581/* The only header we use is the last-modified header. */
2582string pkgAcqIndex::Custom600Headers() const
2583{
2584
2585 string msg = "\nIndex-File: true";
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 }
2595
2596 if(Target.IsOptional)
2597 msg += "\nFail-Ignore: true";
2598
2599 return msg;
2600}
2601 /*}}}*/
2602// AcqIndex::Failed - getting the indexfile failed /*{{{*/
2603void pkgAcqIndex::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)
2604{
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 {
2612 Init(Target.URI, Desc.Description, Desc.ShortDesc);
2613 Status = StatIdle;
2614 return;
2615 }
2616 }
2617
2618 if(Target.IsOptional && GetExpectedHashes().empty() && Stage == STAGE_DOWNLOAD)
2619 Status = StatDone;
2620 else
2621 TransactionManager->AbortTransaction();
2622}
2623 /*}}}*/
2624// AcqIndex::ReverifyAfterIMS - Reverify index after an ims-hit /*{{{*/
2625void pkgAcqIndex::ReverifyAfterIMS()
2626{
2627 // update destfile to *not* include the compression extension when doing
2628 // a reverify (as its uncompressed on disk already)
2629 DestFile = GetCompressedFileName(Target, GetPartialFileNameFromURI(Target.URI), CurrentCompressionExtension);
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);
2636}
2637 /*}}}*/
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)
2648{
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 }
2660}
2661 /*}}}*/
2662// AcqIndex::StageDownloadDone - Queue for decompress and verify /*{{{*/
2663void pkgAcqIndex::StageDownloadDone(string const &Message, HashStringList const &,
2664 pkgAcquire::MethodConfig const * const)
2665{
2666 Complete = true;
2667
2668 // Handle the unzipd case
2669 std::string FileName = LookupTag(Message,"Alt-Filename");
2670 if (FileName.empty() == false)
2671 {
2672 Stage = STAGE_DECOMPRESS_AND_VERIFY;
2673 Local = true;
2674 if (CurrentCompressionExtension != "uncompressed")
2675 DestFile.erase(DestFile.length() - (CurrentCompressionExtension.length() + 1));
2676 Desc.URI = "copy:" + FileName;
2677 QueueURI(Desc);
2678 SetActiveSubprocess("copy");
2679 return;
2680 }
2681 FileName = LookupTag(Message,"Filename");
2682
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
2685 if (FileName != DestFile && RealFileExists(DestFile) == false)
2686 {
2687 Local = true;
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 }
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 }
2709 }
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)
2716 {
2717 // The files timestamp matches, reverify by copy into partial/
2718 EraseFileName = "";
2719 ReverifyAfterIMS();
2720 return;
2721 }
2722
2723 string decompProg = "store";
2724 if (Target.KeepCompressed == true)
2725 {
2726 DestFile = "/dev/null";
2727 EraseFileName.clear();
2728 }
2729 else
2730 {
2731 if (CurrentCompressionExtension == "uncompressed")
2732 decompProg = "copy";
2733 else
2734 DestFile.erase(DestFile.length() - (CurrentCompressionExtension.length() + 1));
2735 }
2736
2737 // queue uri for the next stage
2738 Stage = STAGE_DECOMPRESS_AND_VERIFY;
2739 Desc.URI = decompProg + ":" + FileName;
2740 QueueURI(Desc);
2741 SetActiveSubprocess(decompProg);
2742}
2743 /*}}}*/
2744// AcqIndex::StageDecompressDone - Final verification /*{{{*/
2745void pkgAcqIndex::StageDecompressDone(string const &,
2746 HashStringList const &,
2747 pkgAcquire::MethodConfig const * const)
2748{
2749 if (Target.KeepCompressed == true && DestFile == "/dev/null")
2750 DestFile = GetPartialFileNameFromURI(Target.URI + '.' + CurrentCompressionExtension);
2751
2752 // Done, queue for rename on transaction finished
2753 TransactionManager->TransactionStageCopy(this, DestFile, GetFinalFilename());
2754 return;
2755}
2756 /*}}}*/
2757pkgAcqIndex::~pkgAcqIndex() {}
2758
2759
2760// AcqArchive::AcqArchive - Constructor /*{{{*/
2761// ---------------------------------------------------------------------
2762/* This just sets up the initial fetch environment and queues the first
2763 possibilitiy */
2764pkgAcqArchive::pkgAcqArchive(pkgAcquire * const Owner,pkgSourceList * const Sources,
2765 pkgRecords * const Recs,pkgCache::VerIterator const &Version,
2766 string &StoreFilename) :
2767 Item(Owner), d(NULL), LocalSource(false), Version(Version), Sources(Sources), Recs(Recs),
2768 StoreFilename(StoreFilename), Vf(Version.FileList()),
2769 Trusted(false)
2770{
2771 Retries = _config->FindI("Acquire::Retries",0);
2772
2773 if (Version.Arch() == 0)
2774 {
2775 _error->Error(_("I wasn't able to locate a file for the %s package. "
2776 "This might mean you need to manually fix this package. "
2777 "(due to missing arch)"),
2778 Version.ParentPkg().FullName().c_str());
2779 return;
2780 }
2781
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.
2786 for (; Vf.end() == false; ++Vf)
2787 {
2788 if (Vf.File().Flagged(pkgCache::Flag::NotSource))
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 }
2807
2808 // check if we have one trusted source for the package. if so, switch
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;
2813 for (pkgCache::VerFileIterator i = Version.FileList(); i.end() == false; ++i)
2814 {
2815 pkgIndexFile *Index;
2816 if (Sources->FindIndex(i.File(),Index) == false)
2817 continue;
2818
2819 if (debugAuth == true)
2820 std::cerr << "Checking index: " << Index->Describe()
2821 << "(Trusted=" << Index->IsTrusted() << ")" << std::endl;
2822
2823 if (Index->IsTrusted() == true)
2824 {
2825 Trusted = true;
2826 if (allowUnauth == false)
2827 break;
2828 }
2829 else
2830 seenUntrusted = true;
2831 }
2832
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
2836 if (allowUnauth == true && seenUntrusted == true)
2837 Trusted = false;
2838
2839 // Select a source
2840 if (QueueNext() == false && _error->PendingError() == false)
2841 _error->Error(_("Can't find a source to download version '%s' of '%s'"),
2842 Version.VerStr(), Version.ParentPkg().FullName(false).c_str());
2843}
2844 /*}}}*/
2845// AcqArchive::QueueNext - Queue the next file source /*{{{*/
2846// ---------------------------------------------------------------------
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. */
2850bool pkgAcqArchive::QueueNext()
2851{
2852 for (; Vf.end() == false; ++Vf)
2853 {
2854 pkgCache::PkgFileIterator const PkgF = Vf.File();
2855 // Ignore not source sources
2856 if (PkgF.Flagged(pkgCache::Flag::NotSource))
2857 continue;
2858
2859 // Try to cross match against the source list
2860 pkgIndexFile *Index;
2861 if (Sources->FindIndex(PkgF, Index) == false)
2862 continue;
2863 LocalSource = PkgF.Flagged(pkgCache::Flag::LocalSource);
2864
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
2870 // Grab the text package record
2871 pkgRecords::Parser &Parse = Recs->Lookup(Vf);
2872 if (_error->PendingError() == true)
2873 return false;
2874
2875 string PkgFile = Parse.FileName();
2876 ExpectedHashes = Parse.Hashes();
2877
2878 if (PkgFile.empty() == true)
2879 return _error->Error(_("The package index files are corrupted. No Filename: "
2880 "field for package %s."),
2881 Version.ParentPkg().Name());
2882
2883 Desc.URI = Index->ArchiveURI(PkgFile);
2884 Desc.Description = Index->ArchiveInfo(Version);
2885 Desc.Owner = this;
2886 Desc.ShortDesc = Version.ParentPkg().FullName(true);
2887
2888 // See if we already have the file. (Legacy filenames)
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
2895 if ((unsigned long long)Buf.st_size == Version->Size)
2896 {
2897 Complete = true;
2898 Local = true;
2899 Status = StatDone;
2900 StoreFilename = DestFile = FinalFile;
2901 return true;
2902 }
2903
2904 /* Hmm, we have a file and its size does not match, this means it is
2905 an old style mismatched arch */
2906 RemoveFile("pkgAcqArchive::QueueNext", FinalFile);
2907 }
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
2914 if ((unsigned long long)Buf.st_size == Version->Size)
2915 {
2916 Complete = true;
2917 Local = true;
2918 Status = StatDone;
2919 StoreFilename = DestFile = FinalFile;
2920 return true;
2921 }
2922
2923 /* Hmm, we have a file and its size does not match, this shouldn't
2924 happen.. */
2925 RemoveFile("pkgAcqArchive::QueueNext", FinalFile);
2926 }
2927
2928 DestFile = _config->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename);
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
2934 if ((unsigned long long)Buf.st_size > Version->Size)
2935 RemoveFile("pkgAcqArchive::QueueNext", DestFile);
2936 else
2937 PartialSize = Buf.st_size;
2938 }
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
2951 // Create the item
2952 Local = false;
2953 QueueURI(Desc);
2954
2955 ++Vf;
2956 return true;
2957 }
2958 return false;
2959}
2960 /*}}}*/
2961// AcqArchive::Done - Finished fetching /*{{{*/
2962// ---------------------------------------------------------------------
2963/* */
2964void pkgAcqArchive::Done(string const &Message, HashStringList const &Hashes,
2965 pkgAcquire::MethodConfig const * const Cfg)
2966{
2967 Item::Done(Message, Hashes, Cfg);
2968
2969 // Grab the output filename
2970 std::string const FileName = LookupTag(Message,"Filename");
2971 if (DestFile != FileName && RealFileExists(DestFile) == false)
2972 {
2973 StoreFilename = DestFile = FileName;
2974 Local = true;
2975 Complete = true;
2976 return;
2977 }
2978
2979 // Done, move it into position
2980 string const FinalFile = GetFinalFilename();
2981 Rename(DestFile,FinalFile);
2982 StoreFilename = DestFile = FinalFile;
2983 Complete = true;
2984}
2985 /*}}}*/
2986// AcqArchive::Failed - Failure handler /*{{{*/
2987// ---------------------------------------------------------------------
2988/* Here we try other sources */
2989void pkgAcqArchive::Failed(string const &Message,pkgAcquire::MethodConfig const * const Cnf)
2990{
2991 Item::Failed(Message,Cnf);
2992
2993 /* We don't really want to retry on failed media swaps, this prevents
2994 that. An interesting observation is that permanent failures are not
2995 recorded. */
2996 if (Cnf->Removable == true &&
2997 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
2998 {
2999 // Vf = Version.FileList();
3000 while (Vf.end() == false) ++Vf;
3001 StoreFilename = string();
3002 return;
3003 }
3004
3005 Status = StatIdle;
3006 if (QueueNext() == false)
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 }
3018
3019 StoreFilename = string();
3020 Status = StatError;
3021 }
3022}
3023 /*}}}*/
3024APT_PURE bool pkgAcqArchive::IsTrusted() const /*{{{*/
3025{
3026 return Trusted;
3027}
3028 /*}}}*/
3029void pkgAcqArchive::Finished() /*{{{*/
3030{
3031 if (Status == pkgAcquire::Item::StatDone &&
3032 Complete == true)
3033 return;
3034 StoreFilename = string();
3035}
3036 /*}}}*/
3037std::string pkgAcqArchive::DescURI() const /*{{{*/
3038{
3039 return Desc.URI;
3040}
3041 /*}}}*/
3042std::string pkgAcqArchive::ShortDesc() const /*{{{*/
3043{
3044 return Desc.ShortDesc;
3045}
3046 /*}}}*/
3047pkgAcqArchive::~pkgAcqArchive() {}
3048
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 {
3092 std::string const SandboxUser = _config->Find("APT::Sandbox::User");
3093 std::string const systemTemp = GetTempDir(SandboxUser);
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;
3103
3104 ChangeOwnerAndPermissionOfFile("Item::QueueURI", DestFile.c_str(),
3105 SandboxUser.c_str(), "root", 0700);
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);
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{
3200 if (Template.find("@CHANGEPATH@") == std::string::npos)
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
3212 return SubstVar(Template, "@CHANGEPATH@", path);
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 {
3245 RemoveFile("~pkgAcqChangelog", DestFile);
3246 rmdir(TemporaryDirectory.c_str());
3247 }
3248}
3249 /*}}}*/
3250
3251// AcqFile::pkgAcqFile - Constructor /*{{{*/
3252pkgAcqFile::pkgAcqFile(pkgAcquire * const Owner,string const &URI, HashStringList const &Hashes,
3253 unsigned long long const Size,string const &Dsc,string const &ShortDesc,
3254 const string &DestDir, const string &DestFilename,
3255 bool const IsIndexFile) :
3256 Item(Owner), d(NULL), IsIndexFile(IsIndexFile), ExpectedHashes(Hashes)
3257{
3258 Retries = _config->FindI("Acquire::Retries",0);
3259
3260 if(!DestFilename.empty())
3261 DestFile = DestFilename;
3262 else if(!DestDir.empty())
3263 DestFile = DestDir + "/" + flNotDir(URI);
3264 else
3265 DestFile = flNotDir(URI);
3266
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;
3274
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
3281 if ((Size > 0) && (unsigned long long)Buf.st_size > Size)
3282 RemoveFile("pkgAcqFile", DestFile);
3283 else
3284 PartialSize = Buf.st_size;
3285 }
3286
3287 QueueURI(Desc);
3288}
3289 /*}}}*/
3290// AcqFile::Done - Item downloaded OK /*{{{*/
3291void pkgAcqFile::Done(string const &Message,HashStringList const &CalcHashes,
3292 pkgAcquire::MethodConfig const * const Cnf)
3293{
3294 Item::Done(Message,CalcHashes,Cnf);
3295
3296 std::string const FileName = LookupTag(Message,"Filename");
3297 Complete = true;
3298
3299 // The files timestamp matches
3300 if (StringToBool(LookupTag(Message,"IMS-Hit"),false) == true)
3301 return;
3302
3303 // We have to copy it into place
3304 if (RealFileExists(DestFile.c_str()) == false)
3305 {
3306 Local = true;
3307 if (_config->FindB("Acquire::Source-Symlinks",true) == false ||
3308 Cnf->Removable == true)
3309 {
3310 Desc.URI = "copy:" + FileName;
3311 QueueURI(Desc);
3312 return;
3313 }
3314
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)
3320 RemoveFile("pkgAcqFile::Done", DestFile);
3321 }
3322
3323 // Symlink the file
3324 if (symlink(FileName.c_str(),DestFile.c_str()) != 0)
3325 {
3326 _error->PushToStack();
3327 _error->Errno("pkgAcqFile::Done", "Symlinking file %s failed", DestFile.c_str());
3328 std::stringstream msg;
3329 _error->DumpErrors(msg, GlobalError::DEBUG, false);
3330 _error->RevertToStack();
3331 ErrorText = msg.str();
3332 Status = StatError;
3333 Complete = false;
3334 }
3335 }
3336}
3337 /*}}}*/
3338// AcqFile::Failed - Failure handler /*{{{*/
3339// ---------------------------------------------------------------------
3340/* Here we try other sources */
3341void pkgAcqFile::Failed(string const &Message, pkgAcquire::MethodConfig const * const Cnf)
3342{
3343 Item::Failed(Message,Cnf);
3344
3345 // This is the retry counter
3346 if (Retries != 0 &&
3347 Cnf->LocalOnly == false &&
3348 StringToBool(LookupTag(Message,"Transient-Failure"),false) == true)
3349 {
3350 --Retries;
3351 QueueURI(Desc);
3352 Status = StatIdle;
3353 return;
3354 }
3355
3356}
3357 /*}}}*/
3358string pkgAcqFile::Custom600Headers() const /*{{{*/
3359{
3360 if (IsIndexFile)
3361 return "\nIndex-File: true";
3362 return "";
3363}
3364 /*}}}*/
3365pkgAcqFile::~pkgAcqFile() {}