1 // -*- mode: cpp; mode: fold -*-
3 // $Id: acquire-item.cc,v 1.46.2.9 2004/01/16 18:51:11 mdz Exp $
4 /* ######################################################################
6 Acquire Item - Item to acquire
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.
13 ##################################################################### */
15 // Include Files /*{{{*/
17 #pragma implementation "apt-pkg/acquire-item.h"
19 #include <apt-pkg/acquire-item.h>
20 #include <apt-pkg/configuration.h>
21 #include <apt-pkg/sourcelist.h>
22 #include <apt-pkg/vendorlist.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/strutl.h>
25 #include <apt-pkg/fileutl.h>
26 #include <apt-pkg/md5.h>
27 #include <apt-pkg/sha1.h>
28 #include <apt-pkg/tagfile.h>
42 // Acquire::Item::Item - Constructor /*{{{*/
43 // ---------------------------------------------------------------------
45 pkgAcquire::Item::Item(pkgAcquire
*Owner
) : Owner(Owner
), FileSize(0),
46 PartialSize(0), Mode(0), ID(0), Complete(false),
47 Local(false), QueueCounter(0)
53 // Acquire::Item::~Item - Destructor /*{{{*/
54 // ---------------------------------------------------------------------
56 pkgAcquire::Item::~Item()
61 // Acquire::Item::Failed - Item failed to download /*{{{*/
62 // ---------------------------------------------------------------------
63 /* We return to an idle state if there are still other queues that could
65 void pkgAcquire::Item::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
68 ErrorText
= LookupTag(Message
,"Message");
69 if (QueueCounter
<= 1)
71 /* This indicates that the file is not available right now but might
72 be sometime later. If we do a retry cycle then this should be
74 if (Cnf
->LocalOnly
== true &&
75 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
87 // Acquire::Item::Start - Item has begun to download /*{{{*/
88 // ---------------------------------------------------------------------
89 /* Stash status and the file size. Note that setting Complete means
90 sub-phases of the acquire process such as decompresion are operating */
91 void pkgAcquire::Item::Start(string
/*Message*/,unsigned long Size
)
93 Status
= StatFetching
;
94 if (FileSize
== 0 && Complete
== false)
98 // Acquire::Item::Done - Item downloaded OK /*{{{*/
99 // ---------------------------------------------------------------------
101 void pkgAcquire::Item::Done(string Message
,unsigned long Size
,string
,
102 pkgAcquire::MethodConfig
*Cnf
)
104 // We just downloaded something..
105 string FileName
= LookupTag(Message
,"Filename");
106 if (Complete
== false && FileName
== DestFile
)
109 Owner
->Log
->Fetched(Size
,atoi(LookupTag(Message
,"Resume-Point","0").c_str()));
116 ErrorText
= string();
117 Owner
->Dequeue(this);
120 // Acquire::Item::Rename - Rename a file /*{{{*/
121 // ---------------------------------------------------------------------
122 /* This helper function is used by alot of item methods as thier final
124 void pkgAcquire::Item::Rename(string From
,string To
)
126 if (rename(From
.c_str(),To
.c_str()) != 0)
129 snprintf(S
,sizeof(S
),_("rename failed, %s (%s -> %s)."),strerror(errno
),
130 From
.c_str(),To
.c_str());
137 // AcqIndexDiffs::AcqIndexDiffs - Constructor
138 // ---------------------------------------------------------------------
139 /* The package diff is added to the queue. one object is constructed
140 * for each diff and the index
142 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire
*Owner
,
143 string URI
,string URIDesc
,string ShortDesc
,
144 string ExpectedMD5
, vector
<DiffInfo
> diffs
)
145 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
),
146 available_patches(diffs
)
149 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
150 DestFile
+= URItoFileName(URI
);
152 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
154 Desc
.Description
= URIDesc
;
156 Desc
.ShortDesc
= ShortDesc
;
158 CurrentPackagesFile
= _config
->FindDir("Dir::State::lists");
159 CurrentPackagesFile
+= URItoFileName(RealURI
);
162 std::clog
<< "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
163 << CurrentPackagesFile
<< std::endl
;
166 if(!FileExists(CurrentPackagesFile
) ||
167 !_config
->FindB("Acquire::Diffs",true)) {
168 // we don't have a pkg file or we don't want to queue
170 std::clog
<< "No index file or canceld by user" << std::endl
;
175 if(available_patches
.size() == 0) {
176 State
= StateFetchIndex
;
179 State
= StateFetchDiff
;
184 void pkgAcqIndexDiffs::QueueDiffIndex(string URI
)
186 Desc
.URI
= URI
+ ".diff/Index";
187 Desc
.Description
= Description
+ "IndexDiff";
188 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
189 DestFile
+= URItoFileName(URI
) + string(".IndexDiff");
192 std::clog
<< "QueueDiffIndex: " << Desc
.URI
<< std::endl
;
197 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
198 // ---------------------------------------------------------------------
199 /* The only header we use is the last-modified header. */
200 string
pkgAcqIndexDiffs::Custom600Headers()
202 // we only care for the IndexDiff file
203 if(State
!= StateFetchIndex
)
206 string Final
= _config
->FindDir("Dir::State::lists");
207 Final
+= URItoFileName(RealURI
) + string(".IndexDiff");
210 std::clog
<< "Custom600Header-IMS: " << Final
<< std::endl
;
213 if (stat(Final
.c_str(),&Buf
) != 0)
214 return "\nIndex-File: true";
216 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
219 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
222 std::clog
<< "Failed(): " << Desc
.URI
<< std::endl
223 << "Falling back to big package file" << std::endl
;
224 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
230 // helper that cleans the item out of the fetcher queue
231 void pkgAcqIndexDiffs::Finish(bool allDone
)
233 // we restore the original name, this is required, otherwise
234 // the file will be cleaned
236 // this is for the "real" finish
237 DestFile
= _config
->FindDir("Dir::State::lists");
238 DestFile
+= URItoFileName(RealURI
);
242 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
248 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
257 bool pkgAcqIndexDiffs::QueueNextDiff()
259 // calc sha1 of the just patched file
260 string FinalFile
= _config
->FindDir("Dir::State::lists");
261 FinalFile
+= URItoFileName(RealURI
);
263 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
265 SHA1
.AddFD(fd
.Fd(), fd
.Size());
266 string local_sha1
= string(SHA1
.Result());
268 // remove all patches until the next matching patch is found
269 // this requires the Index file to be ordered
270 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
271 I
!= available_patches
.end() && (*I
).sha1
!= local_sha1
;
273 available_patches
.erase(I
);
275 // error checking and falling back if no patch was found
276 if(available_patches
.size() == 0) {
281 // queue the right diff
282 Desc
.URI
= string(RealURI
) + string(".diff/") + available_patches
[0].file
+ string(".gz");
283 Desc
.Description
= available_patches
[0].file
+ string(".pdiff");
285 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
286 DestFile
+= URItoFileName(RealURI
+ string(".diff/") + available_patches
[0].file
);
289 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
296 bool pkgAcqIndexDiffs::ParseIndexDiff(string IndexDiffFile
)
299 std::clog
<< "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
302 FileFd
Fd(IndexDiffFile
,FileFd::ReadOnly
);
304 if (_error
->PendingError() == true)
307 if(TF
.Step(Tags
) == true)
310 string tmp
= Tags
.FindS("SHA1-Current");
311 std::stringstream
ss(tmp
);
314 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnly
);
316 SHA1
.AddFD(fd
.Fd(), fd
.Size());
317 local_sha1
= string(SHA1
.Result());
319 if(local_sha1
== ServerSha1
) {
321 std::clog
<< "Package file is up-to-date" << std::endl
;
326 std::clog
<< "SHA1-Current: " << ServerSha1
<< std::endl
;
328 // check the historie and see what patches we need
329 string history
= Tags
.FindS("SHA1-History");
330 std::stringstream
hist(history
);
334 while(hist
>> d
.sha1
>> size
>> d
.file
) {
335 d
.size
= atoi(size
.c_str());
336 // read until the first match is found
337 if(d
.sha1
== local_sha1
)
339 // from that point on, we probably need all diffs
342 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
343 available_patches
.push_back(d
);
347 // no information how to get the patches, bail out
350 std::clog
<< "Can't find a patch in the index file" << std::endl
;
351 // Failed will queue a big package file
355 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
356 ExpectedMD5
, available_patches
);
366 void pkgAcqIndexDiffs::Done(string Message
,unsigned long Size
,string Md5Hash
,
367 pkgAcquire::MethodConfig
*Cnf
)
370 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
372 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
375 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
377 // sucess in downloading the index
378 if(State
== StateFetchIndex
)
381 FinalFile
+= string(".IndexDiff");
383 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
385 Rename(DestFile
,FinalFile
);
386 chmod(FinalFile
.c_str(),0644);
387 DestFile
= FinalFile
;
389 if(!ParseIndexDiff(DestFile
))
390 return Failed("", NULL
);
395 // sucess in downloading a diff, enter ApplyDiff state
396 if(State
== StateFetchDiff
)
400 std::clog
<< "Sending to gzip method: " << FinalFile
<< std::endl
;
402 string FileName
= LookupTag(Message
,"Filename");
403 State
= StateUnzipDiff
;
404 Desc
.URI
= "gzip:" + FileName
;
405 DestFile
+= ".decomp";
411 // sucess in downloading a diff, enter ApplyDiff state
412 if(State
== StateUnzipDiff
)
415 // rred excepts the patch as $FinalFile.ed
416 Rename(DestFile
,FinalFile
+".ed");
419 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
421 State
= StateApplyDiff
;
422 Desc
.URI
= "rred:" + FinalFile
;
429 // success in download/apply a diff, queue next (if needed)
430 if(State
== StateApplyDiff
)
432 // remove the just applied patch
433 available_patches
.erase(available_patches
.begin());
437 std::clog
<< "Moving patched file in place: " << std::endl
438 << DestFile
<< " -> " << FinalFile
<< std::endl
;
439 Rename(DestFile
,FinalFile
);
441 // see if there is more to download
442 if(available_patches
.size() > 0) {
443 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
444 ExpectedMD5
, available_patches
);
452 // AcqIndex::AcqIndex - Constructor /*{{{*/
453 // ---------------------------------------------------------------------
454 /* The package file is added to the queue and a second class is
455 instantiated to fetch the revision file */
456 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
457 string URI
,string URIDesc
,string ShortDesc
,
458 string ExpectedMD5
, string comprExt
)
459 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
)
461 Decompression
= false;
464 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
465 DestFile
+= URItoFileName(URI
);
470 if(FileExists("/usr/bin/bzip2"))
471 Desc
.URI
= URI
+ ".bz2";
473 Desc
.URI
= URI
+ ".gz";
475 Desc
.URI
= URI
+ comprExt
;
478 Desc
.Description
= URIDesc
;
480 Desc
.ShortDesc
= ShortDesc
;
485 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
486 // ---------------------------------------------------------------------
487 /* The only header we use is the last-modified header. */
488 string
pkgAcqIndex::Custom600Headers()
490 string Final
= _config
->FindDir("Dir::State::lists");
491 Final
+= URItoFileName(RealURI
);
494 if (stat(Final
.c_str(),&Buf
) != 0)
495 return "\nIndex-File: true";
497 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
501 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
503 // no .bz2 found, retry with .gz
504 if(Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1) == "bz2") {
505 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
507 // retry with a gzip one
508 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
509 ExpectedMD5
, string(".gz"));
517 Item::Failed(Message
,Cnf
);
521 // AcqIndex::Done - Finished a fetch /*{{{*/
522 // ---------------------------------------------------------------------
523 /* This goes through a number of states.. On the initial fetch the
524 method could possibly return an alternate filename which points
525 to the uncompressed version of the file. If this is so the file
526 is copied into the partial directory. In all other cases the file
527 is decompressed with a gzip uri. */
528 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
529 pkgAcquire::MethodConfig
*Cfg
)
531 Item::Done(Message
,Size
,MD5
,Cfg
);
533 if (Decompression
== true)
535 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
537 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
538 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
544 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
545 sum
.AddFD(Fd
.Fd(), Fd
.Size());
547 MD5
= (string
)sum
.Result();
550 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
552 Status
= StatAuthError
;
553 ErrorText
= _("MD5Sum mismatch");
554 Rename(DestFile
,DestFile
+ ".FAILED");
557 // Done, move it into position
558 string FinalFile
= _config
->FindDir("Dir::State::lists");
559 FinalFile
+= URItoFileName(RealURI
);
560 Rename(DestFile
,FinalFile
);
561 chmod(FinalFile
.c_str(),0644);
563 /* We restore the original name to DestFile so that the clean operation
565 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
566 DestFile
+= URItoFileName(RealURI
);
568 // Remove the compressed version.
570 unlink(DestFile
.c_str());
577 // Handle the unzipd case
578 string FileName
= LookupTag(Message
,"Alt-Filename");
579 if (FileName
.empty() == false)
581 // The files timestamp matches
582 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
585 Decompression
= true;
587 DestFile
+= ".decomp";
588 Desc
.URI
= "copy:" + FileName
;
594 FileName
= LookupTag(Message
,"Filename");
595 if (FileName
.empty() == true)
598 ErrorText
= "Method gave a blank filename";
601 // The files timestamp matches
602 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
605 if (FileName
== DestFile
)
610 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1);
613 decompProg
= "bzip2";
614 else if(compExt
== ".gz")
617 _error
->Error("Unsupported extension: %s", compExt
.c_str());
621 Decompression
= true;
622 DestFile
+= ".decomp";
623 Desc
.URI
= string(decompProg
) + ":" + FileName
;
628 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
629 string URI
,string URIDesc
,string ShortDesc
,
630 string MetaIndexURI
, string MetaIndexURIDesc
,
631 string MetaIndexShortDesc
,
632 const vector
<IndexTarget
*>* IndexTargets
,
633 indexRecords
* MetaIndexParser
) :
634 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
635 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
)
637 this->MetaIndexParser
= MetaIndexParser
;
638 this->IndexTargets
= IndexTargets
;
639 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
640 DestFile
+= URItoFileName(URI
);
642 // remove any partial downloaded sig-file. it may confuse proxies
643 // and is too small to warrant a partial download anyway
644 unlink(DestFile
.c_str());
647 Desc
.Description
= URIDesc
;
649 Desc
.ShortDesc
= ShortDesc
;
653 string Final
= _config
->FindDir("Dir::State::lists");
654 Final
+= URItoFileName(RealURI
);
656 if (stat(Final
.c_str(),&Buf
) == 0)
658 // File was already in place. It needs to be re-verified
659 // because Release might have changed, so Move it into partial
660 Rename(Final
,DestFile
);
661 // unlink the file and do not try to use I-M-S and Last-Modified
662 // if the users proxy is broken
663 if(_config
->FindB("Acquire::BrokenProxy", false) == true) {
664 std::cerr
<< "forcing re-get of the signature file as requested" << std::endl
;
665 unlink(DestFile
.c_str());
672 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
673 // ---------------------------------------------------------------------
674 /* The only header we use is the last-modified header. */
675 string
pkgAcqMetaSig::Custom600Headers()
678 if (stat(DestFile
.c_str(),&Buf
) != 0)
679 return "\nIndex-File: true";
681 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
684 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
685 pkgAcquire::MethodConfig
*Cfg
)
687 Item::Done(Message
,Size
,MD5
,Cfg
);
689 string FileName
= LookupTag(Message
,"Filename");
690 if (FileName
.empty() == true)
693 ErrorText
= "Method gave a blank filename";
697 if (FileName
!= DestFile
)
699 // We have to copy it into place
701 Desc
.URI
= "copy:" + FileName
;
708 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
709 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
710 DestFile
, IndexTargets
, MetaIndexParser
);
714 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
716 // Delete any existing sigfile, so that this source isn't
717 // mistakenly trusted
718 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
719 unlink(Final
.c_str());
721 // queue a pkgAcqMetaIndex with no sigfile
722 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
723 "", IndexTargets
, MetaIndexParser
);
725 if (Cnf
->LocalOnly
== true ||
726 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
735 Item::Failed(Message
,Cnf
);
738 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
739 string URI
,string URIDesc
,string ShortDesc
,
741 const vector
<struct IndexTarget
*>* IndexTargets
,
742 indexRecords
* MetaIndexParser
) :
743 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
745 this->AuthPass
= false;
746 this->MetaIndexParser
= MetaIndexParser
;
747 this->IndexTargets
= IndexTargets
;
748 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
749 DestFile
+= URItoFileName(URI
);
752 Desc
.Description
= URIDesc
;
754 Desc
.ShortDesc
= ShortDesc
;
761 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
762 // ---------------------------------------------------------------------
763 /* The only header we use is the last-modified header. */
764 string
pkgAcqMetaIndex::Custom600Headers()
766 string Final
= _config
->FindDir("Dir::State::lists");
767 Final
+= URItoFileName(RealURI
);
770 if (stat(Final
.c_str(),&Buf
) != 0)
771 return "\nIndex-File: true";
773 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
776 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
777 pkgAcquire::MethodConfig
*Cfg
)
779 Item::Done(Message
,Size
,MD5
,Cfg
);
781 // MetaIndexes are done in two passes: one to download the
782 // metaindex with an appropriate method, and a second to verify it
783 // with the gpgv method
785 if (AuthPass
== true)
791 RetrievalDone(Message
);
793 // Still more retrieving to do
798 // There was no signature file, so we are finished. Download
799 // the indexes without verification.
804 // There was a signature file, so pass it to gpgv for
807 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
808 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
809 << SigFile
<< "," << DestFile
<< ")\n";
811 Desc
.URI
= "gpgv:" + SigFile
;
818 void pkgAcqMetaIndex::RetrievalDone(string Message
)
820 // We have just finished downloading a Release file (it is not
823 string FileName
= LookupTag(Message
,"Filename");
824 if (FileName
.empty() == true)
827 ErrorText
= "Method gave a blank filename";
831 if (FileName
!= DestFile
)
834 Desc
.URI
= "copy:" + FileName
;
841 string FinalFile
= _config
->FindDir("Dir::State::lists");
842 FinalFile
+= URItoFileName(RealURI
);
844 // The files timestamp matches
845 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
847 // Move it into position
848 Rename(DestFile
,FinalFile
);
850 DestFile
= FinalFile
;
853 void pkgAcqMetaIndex::AuthDone(string Message
)
855 // At this point, the gpgv method has succeeded, so there is a
856 // valid signature from a key in the trusted keyring. We
857 // perform additional verification of its contents, and use them
858 // to verify the indexes we are about to download
860 if (!MetaIndexParser
->Load(DestFile
))
862 Status
= StatAuthError
;
863 ErrorText
= MetaIndexParser
->ErrorText
;
872 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
873 std::cerr
<< "Signature verification succeeded: "
874 << DestFile
<< std::endl
;
876 // Download further indexes with verification
879 // Done, move signature file into position
881 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
882 URItoFileName(RealURI
) + ".gpg";
883 Rename(SigFile
,VerifiedSigFile
);
884 chmod(VerifiedSigFile
.c_str(),0644);
887 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
889 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
890 Target
!= IndexTargets
->end();
893 string ExpectedIndexMD5
;
896 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
899 Status
= StatAuthError
;
900 ErrorText
= "Unable to find expected entry "
901 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
904 ExpectedIndexMD5
= Record
->MD5Hash
;
905 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
907 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
908 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
910 if (ExpectedIndexMD5
.empty())
912 Status
= StatAuthError
;
913 ErrorText
= "Unable to find MD5 sum for "
914 + (*Target
)->MetaKey
+ " in Meta-index file";
919 // Queue Packages file
920 new pkgAcqIndexDiffs(Owner
, (*Target
)->URI
, (*Target
)->Description
,
921 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
925 bool pkgAcqMetaIndex::VerifyVendor()
927 // // Maybe this should be made available from above so we don't have
928 // // to read and parse it every time?
929 // pkgVendorList List;
930 // List.ReadMainList();
932 // const Vendor* Vndr = NULL;
933 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
935 // string::size_type pos = (*I).find("VALIDSIG ");
936 // if (_config->FindB("Debug::Vendor", false))
937 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
939 // if (pos != std::string::npos)
941 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
942 // if (_config->FindB("Debug::Vendor", false))
943 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
945 // Vndr = List.FindVendor(Fingerprint) != "";
946 // if (Vndr != NULL);
951 string Transformed
= MetaIndexParser
->GetExpectedDist();
953 if (Transformed
== "../project/experimental")
955 Transformed
= "experimental";
958 string::size_type pos
= Transformed
.rfind('/');
959 if (pos
!= string::npos
)
961 Transformed
= Transformed
.substr(0, pos
);
964 if (Transformed
== ".")
969 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
971 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
972 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
973 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
976 if (MetaIndexParser
->CheckDist(Transformed
) == false)
978 // This might become fatal one day
979 // Status = StatAuthError;
980 // ErrorText = "Conflicting distribution; expected "
981 // + MetaIndexParser->GetExpectedDist() + " but got "
982 // + MetaIndexParser->GetDist();
984 if (!Transformed
.empty())
986 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
987 Desc
.Description
.c_str(),
989 MetaIndexParser
->GetDist().c_str());
996 // pkgAcqMetaIndex::Failed - no Release file present or no signature
997 // file present /*{{{*/
998 // ---------------------------------------------------------------------
1000 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1002 if (AuthPass
== true)
1004 // gpgv method failed
1005 _error
->Warning("GPG error: %s: %s",
1006 Desc
.Description
.c_str(),
1007 LookupTag(Message
,"Message").c_str());
1010 // No Release file was present, or verification failed, so fall
1011 // back to queueing Packages files without verification
1012 QueueIndexes(false);
1017 // AcqArchive::AcqArchive - Constructor /*{{{*/
1018 // ---------------------------------------------------------------------
1019 /* This just sets up the initial fetch environment and queues the first
1021 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
1022 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
1023 string
&StoreFilename
) :
1024 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
1025 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
1028 Retries
= _config
->FindI("Acquire::Retries",0);
1030 if (Version
.Arch() == 0)
1032 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1033 "This might mean you need to manually fix this package. "
1034 "(due to missing arch)"),
1035 Version
.ParentPkg().Name());
1039 /* We need to find a filename to determine the extension. We make the
1040 assumption here that all the available sources for this version share
1041 the same extension.. */
1042 // Skip not source sources, they do not have file fields.
1043 for (; Vf
.end() == false; Vf
++)
1045 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1050 // Does not really matter here.. we are going to fail out below
1051 if (Vf
.end() != true)
1053 // If this fails to get a file name we will bomb out below.
1054 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1055 if (_error
->PendingError() == true)
1058 // Generate the final file name as: package_version_arch.foo
1059 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1060 QuoteString(Version
.VerStr(),"_:") + '_' +
1061 QuoteString(Version
.Arch(),"_:.") +
1062 "." + flExtension(Parse
.FileName());
1065 // check if we have one trusted source for the package. if so, switch
1066 // to "TrustedOnly" mode
1067 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
1069 pkgIndexFile
*Index
;
1070 if (Sources
->FindIndex(i
.File(),Index
) == false)
1072 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1074 std::cerr
<< "Checking index: " << Index
->Describe()
1075 << "(Trusted=" << Index
->IsTrusted() << ")\n";
1077 if (Index
->IsTrusted()) {
1084 if (QueueNext() == false && _error
->PendingError() == false)
1085 _error
->Error(_("I wasn't able to locate file for the %s package. "
1086 "This might mean you need to manually fix this package."),
1087 Version
.ParentPkg().Name());
1090 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1091 // ---------------------------------------------------------------------
1092 /* This queues the next available file version for download. It checks if
1093 the archive is already available in the cache and stashs the MD5 for
1095 bool pkgAcqArchive::QueueNext()
1097 for (; Vf
.end() == false; Vf
++)
1099 // Ignore not source sources
1100 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1103 // Try to cross match against the source list
1104 pkgIndexFile
*Index
;
1105 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1108 // only try to get a trusted package from another source if that source
1110 if(Trusted
&& !Index
->IsTrusted())
1113 // Grab the text package record
1114 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1115 if (_error
->PendingError() == true)
1118 string PkgFile
= Parse
.FileName();
1119 MD5
= Parse
.MD5Hash();
1120 if (PkgFile
.empty() == true)
1121 return _error
->Error(_("The package index files are corrupted. No Filename: "
1122 "field for package %s."),
1123 Version
.ParentPkg().Name());
1125 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1126 Desc
.Description
= Index
->ArchiveInfo(Version
);
1128 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1130 // See if we already have the file. (Legacy filenames)
1131 FileSize
= Version
->Size
;
1132 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1134 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1136 // Make sure the size matches
1137 if ((unsigned)Buf
.st_size
== Version
->Size
)
1142 StoreFilename
= DestFile
= FinalFile
;
1146 /* Hmm, we have a file and its size does not match, this means it is
1147 an old style mismatched arch */
1148 unlink(FinalFile
.c_str());
1151 // Check it again using the new style output filenames
1152 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1153 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1155 // Make sure the size matches
1156 if ((unsigned)Buf
.st_size
== Version
->Size
)
1161 StoreFilename
= DestFile
= FinalFile
;
1165 /* Hmm, we have a file and its size does not match, this shouldnt
1167 unlink(FinalFile
.c_str());
1170 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1172 // Check the destination file
1173 if (stat(DestFile
.c_str(),&Buf
) == 0)
1175 // Hmm, the partial file is too big, erase it
1176 if ((unsigned)Buf
.st_size
> Version
->Size
)
1177 unlink(DestFile
.c_str());
1179 PartialSize
= Buf
.st_size
;
1184 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1185 Desc
.Description
= Index
->ArchiveInfo(Version
);
1187 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1196 // AcqArchive::Done - Finished fetching /*{{{*/
1197 // ---------------------------------------------------------------------
1199 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
1200 pkgAcquire::MethodConfig
*Cfg
)
1202 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
1205 if (Size
!= Version
->Size
)
1208 ErrorText
= _("Size mismatch");
1213 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1218 ErrorText
= _("MD5Sum mismatch");
1219 Rename(DestFile
,DestFile
+ ".FAILED");
1224 // Grab the output filename
1225 string FileName
= LookupTag(Message
,"Filename");
1226 if (FileName
.empty() == true)
1229 ErrorText
= "Method gave a blank filename";
1235 // Reference filename
1236 if (FileName
!= DestFile
)
1238 StoreFilename
= DestFile
= FileName
;
1243 // Done, move it into position
1244 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1245 FinalFile
+= flNotDir(StoreFilename
);
1246 Rename(DestFile
,FinalFile
);
1248 StoreFilename
= DestFile
= FinalFile
;
1252 // AcqArchive::Failed - Failure handler /*{{{*/
1253 // ---------------------------------------------------------------------
1254 /* Here we try other sources */
1255 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1257 ErrorText
= LookupTag(Message
,"Message");
1259 /* We don't really want to retry on failed media swaps, this prevents
1260 that. An interesting observation is that permanent failures are not
1262 if (Cnf
->Removable
== true &&
1263 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1265 // Vf = Version.FileList();
1266 while (Vf
.end() == false) Vf
++;
1267 StoreFilename
= string();
1268 Item::Failed(Message
,Cnf
);
1272 if (QueueNext() == false)
1274 // This is the retry counter
1276 Cnf
->LocalOnly
== false &&
1277 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1280 Vf
= Version
.FileList();
1281 if (QueueNext() == true)
1285 StoreFilename
= string();
1286 Item::Failed(Message
,Cnf
);
1290 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1291 // trusted source /*{{{*/
1292 // ---------------------------------------------------------------------
1293 bool pkgAcqArchive::IsTrusted()
1298 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1299 // ---------------------------------------------------------------------
1301 void pkgAcqArchive::Finished()
1303 if (Status
== pkgAcquire::Item::StatDone
&&
1306 StoreFilename
= string();
1310 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1311 // ---------------------------------------------------------------------
1312 /* The file is added to the queue */
1313 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1314 unsigned long Size
,string Dsc
,string ShortDesc
) :
1315 Item(Owner
), Md5Hash(MD5
)
1317 Retries
= _config
->FindI("Acquire::Retries",0);
1319 DestFile
= flNotDir(URI
);
1323 Desc
.Description
= Dsc
;
1326 // Set the short description to the archive component
1327 Desc
.ShortDesc
= ShortDesc
;
1329 // Get the transfer sizes
1332 if (stat(DestFile
.c_str(),&Buf
) == 0)
1334 // Hmm, the partial file is too big, erase it
1335 if ((unsigned)Buf
.st_size
> Size
)
1336 unlink(DestFile
.c_str());
1338 PartialSize
= Buf
.st_size
;
1344 // AcqFile::Done - Item downloaded OK /*{{{*/
1345 // ---------------------------------------------------------------------
1347 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1348 pkgAcquire::MethodConfig
*Cnf
)
1351 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1356 ErrorText
= "MD5Sum mismatch";
1357 Rename(DestFile
,DestFile
+ ".FAILED");
1362 Item::Done(Message
,Size
,MD5
,Cnf
);
1364 string FileName
= LookupTag(Message
,"Filename");
1365 if (FileName
.empty() == true)
1368 ErrorText
= "Method gave a blank filename";
1374 // The files timestamp matches
1375 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1378 // We have to copy it into place
1379 if (FileName
!= DestFile
)
1382 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1383 Cnf
->Removable
== true)
1385 Desc
.URI
= "copy:" + FileName
;
1390 // Erase the file if it is a symlink so we can overwrite it
1392 if (lstat(DestFile
.c_str(),&St
) == 0)
1394 if (S_ISLNK(St
.st_mode
) != 0)
1395 unlink(DestFile
.c_str());
1399 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1401 ErrorText
= "Link to " + DestFile
+ " failure ";
1408 // AcqFile::Failed - Failure handler /*{{{*/
1409 // ---------------------------------------------------------------------
1410 /* Here we try other sources */
1411 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1413 ErrorText
= LookupTag(Message
,"Message");
1415 // This is the retry counter
1417 Cnf
->LocalOnly
== false &&
1418 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1425 Item::Failed(Message
,Cnf
);