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());
138 // AcqDiffIndex::AcqDiffIndex - Constructor
139 // ---------------------------------------------------------------------
140 /* Get the DiffIndex file first and see if there are patches availabe
141 * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
142 * patches. If anything goes wrong in that process, it will fall back to
143 * the original packages file
145 pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire
*Owner
,
146 string URI
,string URIDesc
,string ShortDesc
,
148 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
), Description(URIDesc
)
151 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
153 Desc
.Description
= URIDesc
+ "/DiffIndex";
155 Desc
.ShortDesc
= ShortDesc
;
156 Desc
.URI
= URI
+ ".diff/Index";
158 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
159 DestFile
+= URItoFileName(URI
) + string(".DiffIndex");
162 std::clog
<< "pkgAcqDiffIndex: " << Desc
.URI
<< std::endl
;
164 // look for the current package file
165 CurrentPackagesFile
= _config
->FindDir("Dir::State::lists");
166 CurrentPackagesFile
+= URItoFileName(RealURI
);
168 if(!FileExists(CurrentPackagesFile
) ||
169 !_config
->FindB("Acquire::Diffs",true)) {
170 // we don't have a pkg file or we don't want to queue
172 std::clog
<< "No index file or canceld by user" << std::endl
;
178 std::clog
<< "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
179 << CurrentPackagesFile
<< std::endl
;
186 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
187 // ---------------------------------------------------------------------
188 /* The only header we use is the last-modified header. */
189 string
pkgAcqDiffIndex::Custom600Headers()
191 string Final
= _config
->FindDir("Dir::State::lists");
192 Final
+= URItoFileName(RealURI
) + string(".IndexDiff");
195 std::clog
<< "Custom600Header-IMS: " << Final
<< std::endl
;
198 if (stat(Final
.c_str(),&Buf
) != 0)
199 return "\nIndex-File: true";
201 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
205 bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile
)
208 std::clog
<< "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
213 vector
<DiffInfo
> available_patches
;
215 FileFd
Fd(IndexDiffFile
,FileFd::ReadOnly
);
217 if (_error
->PendingError() == true)
220 if(TF
.Step(Tags
) == true)
223 string tmp
= Tags
.FindS("SHA1-Current");
224 std::stringstream
ss(tmp
);
227 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnly
);
229 SHA1
.AddFD(fd
.Fd(), fd
.Size());
230 local_sha1
= string(SHA1
.Result());
232 if(local_sha1
== ServerSha1
) {
234 std::clog
<< "Package file is up-to-date" << std::endl
;
235 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
236 ExpectedMD5
, available_patches
);
243 std::clog
<< "SHA1-Current: " << ServerSha1
<< std::endl
;
245 // check the historie and see what patches we need
246 string history
= Tags
.FindS("SHA1-History");
247 std::stringstream
hist(history
);
251 while(hist
>> d
.sha1
>> size
>> d
.file
) {
252 d
.size
= atoi(size
.c_str());
253 // read until the first match is found
254 if(d
.sha1
== local_sha1
)
256 // from that point on, we probably need all diffs
259 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
260 available_patches
.push_back(d
);
264 // no information how to get the patches, bail out
267 std::clog
<< "Can't find a patch in the index file" << std::endl
;
268 // Failed will queue a big package file
272 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
273 ExpectedMD5
, available_patches
);
284 void pkgAcqDiffIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
287 std::clog
<< "pkgAcqDiffIndex failed: " << Desc
.URI
<< std::endl
288 << "Falling back to normal index file aquire" << std::endl
;
290 new pkgAcqIndex(Owner
, RealURI
, Description
,Desc
.ShortDesc
,
298 void pkgAcqDiffIndex::Done(string Message
,unsigned long Size
,string Md5Hash
,
299 pkgAcquire::MethodConfig
*Cnf
)
302 std::clog
<< "pkgAcqDiffIndex::Done(): " << Desc
.URI
<< std::endl
;
304 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
307 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
309 // sucess in downloading the index
311 FinalFile
+= string(".IndexDiff");
313 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
315 Rename(DestFile
,FinalFile
);
316 chmod(FinalFile
.c_str(),0644);
317 DestFile
= FinalFile
;
319 if(!ParseDiffIndex(DestFile
))
320 return Failed("", NULL
);
330 // AcqIndexDiffs::AcqIndexDiffs - Constructor
331 // ---------------------------------------------------------------------
332 /* The package diff is added to the queue. one object is constructed
333 * for each diff and the index
335 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire
*Owner
,
336 string URI
,string URIDesc
,string ShortDesc
,
337 string ExpectedMD5
, vector
<DiffInfo
> diffs
)
338 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
),
339 available_patches(diffs
)
342 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
343 DestFile
+= URItoFileName(URI
);
345 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
347 Desc
.Description
= URIDesc
;
349 Desc
.ShortDesc
= ShortDesc
;
351 if(available_patches
.size() == 0) {
352 // we are done (yeah!)
356 State
= StateFetchDiff
;
362 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
365 std::clog
<< "pkgAcqIndexDiffs failed: " << Desc
.URI
<< std::endl
366 << "Falling back to normal index file aquire" << std::endl
;
367 new pkgAcqIndex(Owner
, RealURI
, Description
,Desc
.ShortDesc
,
373 // helper that cleans the item out of the fetcher queue
374 void pkgAcqIndexDiffs::Finish(bool allDone
)
376 // we restore the original name, this is required, otherwise
377 // the file will be cleaned
379 // this is for the "real" finish
380 DestFile
= _config
->FindDir("Dir::State::lists");
381 DestFile
+= URItoFileName(RealURI
);
385 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
390 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
399 bool pkgAcqIndexDiffs::QueueNextDiff()
402 // calc sha1 of the just patched file
403 string FinalFile
= _config
->FindDir("Dir::State::lists");
404 FinalFile
+= URItoFileName(RealURI
);
406 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
408 SHA1
.AddFD(fd
.Fd(), fd
.Size());
409 string local_sha1
= string(SHA1
.Result());
411 std::clog
<< "QueueNextDiff: "
412 << FinalFile
<< " (" << local_sha1
<< ")"<<std::endl
;
414 // remove all patches until the next matching patch is found
415 // this requires the Index file to be ordered
416 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
417 available_patches
.size() > 0 && I
!= available_patches
.end()
418 && (*I
).sha1
!= local_sha1
;
420 available_patches
.erase(I
);
423 // error checking and falling back if no patch was found
424 if(available_patches
.size() == 0) {
429 // queue the right diff
430 Desc
.URI
= string(RealURI
) + ".diff/" + available_patches
[0].file
+ ".gz";
431 Desc
.Description
= available_patches
[0].file
+ string(".pdiff");
433 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
434 DestFile
+= URItoFileName(RealURI
+ ".diff/" + available_patches
[0].file
);
437 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
446 void pkgAcqIndexDiffs::Done(string Message
,unsigned long Size
,string Md5Hash
,
447 pkgAcquire::MethodConfig
*Cnf
)
450 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
452 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
455 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
457 // sucess in downloading a diff, enter ApplyDiff state
458 if(State
== StateFetchDiff
)
462 std::clog
<< "Sending to gzip method: " << FinalFile
<< std::endl
;
464 string FileName
= LookupTag(Message
,"Filename");
465 State
= StateUnzipDiff
;
466 Desc
.URI
= "gzip:" + FileName
;
467 DestFile
+= ".decomp";
473 // sucess in downloading a diff, enter ApplyDiff state
474 if(State
== StateUnzipDiff
)
477 // rred excepts the patch as $FinalFile.ed
478 Rename(DestFile
,FinalFile
+".ed");
481 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
483 State
= StateApplyDiff
;
484 Desc
.URI
= "rred:" + FinalFile
;
491 // success in download/apply a diff, queue next (if needed)
492 if(State
== StateApplyDiff
)
494 // remove the just applied patch
495 available_patches
.erase(available_patches
.begin());
500 std::clog
<< "Moving patched file in place: " << std::endl
501 << DestFile
<< " -> " << FinalFile
<< std::endl
;
503 Rename(DestFile
,FinalFile
);
505 // see if there is more to download
506 if(available_patches
.size() > 0) {
507 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
508 ExpectedMD5
, available_patches
);
516 // AcqIndex::AcqIndex - Constructor /*{{{*/
517 // ---------------------------------------------------------------------
518 /* The package file is added to the queue and a second class is
519 instantiated to fetch the revision file */
520 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
521 string URI
,string URIDesc
,string ShortDesc
,
522 string ExpectedMD5
, string comprExt
)
523 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
)
525 Decompression
= false;
528 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
529 DestFile
+= URItoFileName(URI
);
534 if(FileExists("/usr/bin/bzip2"))
535 Desc
.URI
= URI
+ ".bz2";
537 Desc
.URI
= URI
+ ".gz";
539 Desc
.URI
= URI
+ comprExt
;
542 Desc
.Description
= URIDesc
;
544 Desc
.ShortDesc
= ShortDesc
;
549 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
550 // ---------------------------------------------------------------------
551 /* The only header we use is the last-modified header. */
552 string
pkgAcqIndex::Custom600Headers()
554 string Final
= _config
->FindDir("Dir::State::lists");
555 Final
+= URItoFileName(RealURI
);
558 if (stat(Final
.c_str(),&Buf
) != 0)
559 return "\nIndex-File: true";
561 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
565 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
567 // no .bz2 found, retry with .gz
568 if(Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1) == "bz2") {
569 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
571 // retry with a gzip one
572 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
573 ExpectedMD5
, string(".gz"));
581 Item::Failed(Message
,Cnf
);
585 // AcqIndex::Done - Finished a fetch /*{{{*/
586 // ---------------------------------------------------------------------
587 /* This goes through a number of states.. On the initial fetch the
588 method could possibly return an alternate filename which points
589 to the uncompressed version of the file. If this is so the file
590 is copied into the partial directory. In all other cases the file
591 is decompressed with a gzip uri. */
592 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
593 pkgAcquire::MethodConfig
*Cfg
)
595 Item::Done(Message
,Size
,MD5
,Cfg
);
597 if (Decompression
== true)
599 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
601 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
602 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
608 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
609 sum
.AddFD(Fd
.Fd(), Fd
.Size());
611 MD5
= (string
)sum
.Result();
614 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
616 Status
= StatAuthError
;
617 ErrorText
= _("MD5Sum mismatch");
618 Rename(DestFile
,DestFile
+ ".FAILED");
621 // Done, move it into position
622 string FinalFile
= _config
->FindDir("Dir::State::lists");
623 FinalFile
+= URItoFileName(RealURI
);
624 Rename(DestFile
,FinalFile
);
625 chmod(FinalFile
.c_str(),0644);
627 /* We restore the original name to DestFile so that the clean operation
629 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
630 DestFile
+= URItoFileName(RealURI
);
632 // Remove the compressed version.
634 unlink(DestFile
.c_str());
641 // Handle the unzipd case
642 string FileName
= LookupTag(Message
,"Alt-Filename");
643 if (FileName
.empty() == false)
645 // The files timestamp matches
646 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
649 Decompression
= true;
651 DestFile
+= ".decomp";
652 Desc
.URI
= "copy:" + FileName
;
658 FileName
= LookupTag(Message
,"Filename");
659 if (FileName
.empty() == true)
662 ErrorText
= "Method gave a blank filename";
665 // The files timestamp matches
666 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
669 if (FileName
== DestFile
)
674 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1);
677 decompProg
= "bzip2";
678 else if(compExt
== ".gz")
681 _error
->Error("Unsupported extension: %s", compExt
.c_str());
685 Decompression
= true;
686 DestFile
+= ".decomp";
687 Desc
.URI
= string(decompProg
) + ":" + FileName
;
692 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
693 string URI
,string URIDesc
,string ShortDesc
,
694 string MetaIndexURI
, string MetaIndexURIDesc
,
695 string MetaIndexShortDesc
,
696 const vector
<IndexTarget
*>* IndexTargets
,
697 indexRecords
* MetaIndexParser
) :
698 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
699 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
)
701 this->MetaIndexParser
= MetaIndexParser
;
702 this->IndexTargets
= IndexTargets
;
703 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
704 DestFile
+= URItoFileName(URI
);
706 // remove any partial downloaded sig-file. it may confuse proxies
707 // and is too small to warrant a partial download anyway
708 unlink(DestFile
.c_str());
711 Desc
.Description
= URIDesc
;
713 Desc
.ShortDesc
= ShortDesc
;
717 string Final
= _config
->FindDir("Dir::State::lists");
718 Final
+= URItoFileName(RealURI
);
720 if (stat(Final
.c_str(),&Buf
) == 0)
722 // File was already in place. It needs to be re-verified
723 // because Release might have changed, so Move it into partial
724 Rename(Final
,DestFile
);
725 // unlink the file and do not try to use I-M-S and Last-Modified
726 // if the users proxy is broken
727 if(_config
->FindB("Acquire::BrokenProxy", false) == true) {
728 std::cerr
<< "forcing re-get of the signature file as requested" << std::endl
;
729 unlink(DestFile
.c_str());
736 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
737 // ---------------------------------------------------------------------
738 /* The only header we use is the last-modified header. */
739 string
pkgAcqMetaSig::Custom600Headers()
742 if (stat(DestFile
.c_str(),&Buf
) != 0)
743 return "\nIndex-File: true";
745 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
748 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
749 pkgAcquire::MethodConfig
*Cfg
)
751 Item::Done(Message
,Size
,MD5
,Cfg
);
753 string FileName
= LookupTag(Message
,"Filename");
754 if (FileName
.empty() == true)
757 ErrorText
= "Method gave a blank filename";
761 if (FileName
!= DestFile
)
763 // We have to copy it into place
765 Desc
.URI
= "copy:" + FileName
;
772 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
773 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
774 DestFile
, IndexTargets
, MetaIndexParser
);
778 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
780 // Delete any existing sigfile, so that this source isn't
781 // mistakenly trusted
782 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
783 unlink(Final
.c_str());
785 // queue a pkgAcqMetaIndex with no sigfile
786 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
787 "", IndexTargets
, MetaIndexParser
);
789 if (Cnf
->LocalOnly
== true ||
790 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
799 Item::Failed(Message
,Cnf
);
802 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
803 string URI
,string URIDesc
,string ShortDesc
,
805 const vector
<struct IndexTarget
*>* IndexTargets
,
806 indexRecords
* MetaIndexParser
) :
807 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
809 this->AuthPass
= false;
810 this->MetaIndexParser
= MetaIndexParser
;
811 this->IndexTargets
= IndexTargets
;
812 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
813 DestFile
+= URItoFileName(URI
);
816 Desc
.Description
= URIDesc
;
818 Desc
.ShortDesc
= ShortDesc
;
825 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
826 // ---------------------------------------------------------------------
827 /* The only header we use is the last-modified header. */
828 string
pkgAcqMetaIndex::Custom600Headers()
830 string Final
= _config
->FindDir("Dir::State::lists");
831 Final
+= URItoFileName(RealURI
);
834 if (stat(Final
.c_str(),&Buf
) != 0)
835 return "\nIndex-File: true";
837 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
840 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
841 pkgAcquire::MethodConfig
*Cfg
)
843 Item::Done(Message
,Size
,MD5
,Cfg
);
845 // MetaIndexes are done in two passes: one to download the
846 // metaindex with an appropriate method, and a second to verify it
847 // with the gpgv method
849 if (AuthPass
== true)
855 RetrievalDone(Message
);
857 // Still more retrieving to do
862 // There was no signature file, so we are finished. Download
863 // the indexes without verification.
868 // There was a signature file, so pass it to gpgv for
871 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
872 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
873 << SigFile
<< "," << DestFile
<< ")\n";
875 Desc
.URI
= "gpgv:" + SigFile
;
882 void pkgAcqMetaIndex::RetrievalDone(string Message
)
884 // We have just finished downloading a Release file (it is not
887 string FileName
= LookupTag(Message
,"Filename");
888 if (FileName
.empty() == true)
891 ErrorText
= "Method gave a blank filename";
895 if (FileName
!= DestFile
)
898 Desc
.URI
= "copy:" + FileName
;
905 string FinalFile
= _config
->FindDir("Dir::State::lists");
906 FinalFile
+= URItoFileName(RealURI
);
908 // The files timestamp matches
909 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
911 // Move it into position
912 Rename(DestFile
,FinalFile
);
914 DestFile
= FinalFile
;
917 void pkgAcqMetaIndex::AuthDone(string Message
)
919 // At this point, the gpgv method has succeeded, so there is a
920 // valid signature from a key in the trusted keyring. We
921 // perform additional verification of its contents, and use them
922 // to verify the indexes we are about to download
924 if (!MetaIndexParser
->Load(DestFile
))
926 Status
= StatAuthError
;
927 ErrorText
= MetaIndexParser
->ErrorText
;
936 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
937 std::cerr
<< "Signature verification succeeded: "
938 << DestFile
<< std::endl
;
940 // Download further indexes with verification
943 // Done, move signature file into position
945 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
946 URItoFileName(RealURI
) + ".gpg";
947 Rename(SigFile
,VerifiedSigFile
);
948 chmod(VerifiedSigFile
.c_str(),0644);
951 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
953 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
954 Target
!= IndexTargets
->end();
957 string ExpectedIndexMD5
;
960 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
963 Status
= StatAuthError
;
964 ErrorText
= "Unable to find expected entry "
965 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
968 ExpectedIndexMD5
= Record
->MD5Hash
;
969 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
971 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
972 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
974 if (ExpectedIndexMD5
.empty())
976 Status
= StatAuthError
;
977 ErrorText
= "Unable to find MD5 sum for "
978 + (*Target
)->MetaKey
+ " in Meta-index file";
983 // Queue Packages file
984 new pkgAcqDiffIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
985 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
989 bool pkgAcqMetaIndex::VerifyVendor()
991 // // Maybe this should be made available from above so we don't have
992 // // to read and parse it every time?
993 // pkgVendorList List;
994 // List.ReadMainList();
996 // const Vendor* Vndr = NULL;
997 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
999 // string::size_type pos = (*I).find("VALIDSIG ");
1000 // if (_config->FindB("Debug::Vendor", false))
1001 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
1003 // if (pos != std::string::npos)
1005 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
1006 // if (_config->FindB("Debug::Vendor", false))
1007 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
1009 // Vndr = List.FindVendor(Fingerprint) != "";
1010 // if (Vndr != NULL);
1015 string Transformed
= MetaIndexParser
->GetExpectedDist();
1017 if (Transformed
== "../project/experimental")
1019 Transformed
= "experimental";
1022 string::size_type pos
= Transformed
.rfind('/');
1023 if (pos
!= string::npos
)
1025 Transformed
= Transformed
.substr(0, pos
);
1028 if (Transformed
== ".")
1033 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1035 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1036 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1037 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1040 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1042 // This might become fatal one day
1043 // Status = StatAuthError;
1044 // ErrorText = "Conflicting distribution; expected "
1045 // + MetaIndexParser->GetExpectedDist() + " but got "
1046 // + MetaIndexParser->GetDist();
1048 if (!Transformed
.empty())
1050 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
1051 Desc
.Description
.c_str(),
1052 Transformed
.c_str(),
1053 MetaIndexParser
->GetDist().c_str());
1060 // pkgAcqMetaIndex::Failed - no Release file present or no signature
1061 // file present /*{{{*/
1062 // ---------------------------------------------------------------------
1064 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1066 if (AuthPass
== true)
1068 // gpgv method failed
1069 _error
->Warning("GPG error: %s: %s",
1070 Desc
.Description
.c_str(),
1071 LookupTag(Message
,"Message").c_str());
1074 // No Release file was present, or verification failed, so fall
1075 // back to queueing Packages files without verification
1076 QueueIndexes(false);
1081 // AcqArchive::AcqArchive - Constructor /*{{{*/
1082 // ---------------------------------------------------------------------
1083 /* This just sets up the initial fetch environment and queues the first
1085 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
1086 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
1087 string
&StoreFilename
) :
1088 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
1089 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
1092 Retries
= _config
->FindI("Acquire::Retries",0);
1094 if (Version
.Arch() == 0)
1096 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1097 "This might mean you need to manually fix this package. "
1098 "(due to missing arch)"),
1099 Version
.ParentPkg().Name());
1103 /* We need to find a filename to determine the extension. We make the
1104 assumption here that all the available sources for this version share
1105 the same extension.. */
1106 // Skip not source sources, they do not have file fields.
1107 for (; Vf
.end() == false; Vf
++)
1109 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1114 // Does not really matter here.. we are going to fail out below
1115 if (Vf
.end() != true)
1117 // If this fails to get a file name we will bomb out below.
1118 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1119 if (_error
->PendingError() == true)
1122 // Generate the final file name as: package_version_arch.foo
1123 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1124 QuoteString(Version
.VerStr(),"_:") + '_' +
1125 QuoteString(Version
.Arch(),"_:.") +
1126 "." + flExtension(Parse
.FileName());
1129 // check if we have one trusted source for the package. if so, switch
1130 // to "TrustedOnly" mode
1131 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
1133 pkgIndexFile
*Index
;
1134 if (Sources
->FindIndex(i
.File(),Index
) == false)
1136 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1138 std::cerr
<< "Checking index: " << Index
->Describe()
1139 << "(Trusted=" << Index
->IsTrusted() << ")\n";
1141 if (Index
->IsTrusted()) {
1148 if (QueueNext() == false && _error
->PendingError() == false)
1149 _error
->Error(_("I wasn't able to locate file for the %s package. "
1150 "This might mean you need to manually fix this package."),
1151 Version
.ParentPkg().Name());
1154 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1155 // ---------------------------------------------------------------------
1156 /* This queues the next available file version for download. It checks if
1157 the archive is already available in the cache and stashs the MD5 for
1159 bool pkgAcqArchive::QueueNext()
1161 for (; Vf
.end() == false; Vf
++)
1163 // Ignore not source sources
1164 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1167 // Try to cross match against the source list
1168 pkgIndexFile
*Index
;
1169 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1172 // only try to get a trusted package from another source if that source
1174 if(Trusted
&& !Index
->IsTrusted())
1177 // Grab the text package record
1178 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1179 if (_error
->PendingError() == true)
1182 string PkgFile
= Parse
.FileName();
1183 MD5
= Parse
.MD5Hash();
1184 if (PkgFile
.empty() == true)
1185 return _error
->Error(_("The package index files are corrupted. No Filename: "
1186 "field for package %s."),
1187 Version
.ParentPkg().Name());
1189 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1190 Desc
.Description
= Index
->ArchiveInfo(Version
);
1192 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1194 // See if we already have the file. (Legacy filenames)
1195 FileSize
= Version
->Size
;
1196 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1198 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1200 // Make sure the size matches
1201 if ((unsigned)Buf
.st_size
== Version
->Size
)
1206 StoreFilename
= DestFile
= FinalFile
;
1210 /* Hmm, we have a file and its size does not match, this means it is
1211 an old style mismatched arch */
1212 unlink(FinalFile
.c_str());
1215 // Check it again using the new style output filenames
1216 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1217 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1219 // Make sure the size matches
1220 if ((unsigned)Buf
.st_size
== Version
->Size
)
1225 StoreFilename
= DestFile
= FinalFile
;
1229 /* Hmm, we have a file and its size does not match, this shouldnt
1231 unlink(FinalFile
.c_str());
1234 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1236 // Check the destination file
1237 if (stat(DestFile
.c_str(),&Buf
) == 0)
1239 // Hmm, the partial file is too big, erase it
1240 if ((unsigned)Buf
.st_size
> Version
->Size
)
1241 unlink(DestFile
.c_str());
1243 PartialSize
= Buf
.st_size
;
1248 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1249 Desc
.Description
= Index
->ArchiveInfo(Version
);
1251 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1260 // AcqArchive::Done - Finished fetching /*{{{*/
1261 // ---------------------------------------------------------------------
1263 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
1264 pkgAcquire::MethodConfig
*Cfg
)
1266 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
1269 if (Size
!= Version
->Size
)
1272 ErrorText
= _("Size mismatch");
1277 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1282 ErrorText
= _("MD5Sum mismatch");
1283 Rename(DestFile
,DestFile
+ ".FAILED");
1288 // Grab the output filename
1289 string FileName
= LookupTag(Message
,"Filename");
1290 if (FileName
.empty() == true)
1293 ErrorText
= "Method gave a blank filename";
1299 // Reference filename
1300 if (FileName
!= DestFile
)
1302 StoreFilename
= DestFile
= FileName
;
1307 // Done, move it into position
1308 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1309 FinalFile
+= flNotDir(StoreFilename
);
1310 Rename(DestFile
,FinalFile
);
1312 StoreFilename
= DestFile
= FinalFile
;
1316 // AcqArchive::Failed - Failure handler /*{{{*/
1317 // ---------------------------------------------------------------------
1318 /* Here we try other sources */
1319 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1321 ErrorText
= LookupTag(Message
,"Message");
1323 /* We don't really want to retry on failed media swaps, this prevents
1324 that. An interesting observation is that permanent failures are not
1326 if (Cnf
->Removable
== true &&
1327 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1329 // Vf = Version.FileList();
1330 while (Vf
.end() == false) Vf
++;
1331 StoreFilename
= string();
1332 Item::Failed(Message
,Cnf
);
1336 if (QueueNext() == false)
1338 // This is the retry counter
1340 Cnf
->LocalOnly
== false &&
1341 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1344 Vf
= Version
.FileList();
1345 if (QueueNext() == true)
1349 StoreFilename
= string();
1350 Item::Failed(Message
,Cnf
);
1354 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1355 // trusted source /*{{{*/
1356 // ---------------------------------------------------------------------
1357 bool pkgAcqArchive::IsTrusted()
1362 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1363 // ---------------------------------------------------------------------
1365 void pkgAcqArchive::Finished()
1367 if (Status
== pkgAcquire::Item::StatDone
&&
1370 StoreFilename
= string();
1374 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1375 // ---------------------------------------------------------------------
1376 /* The file is added to the queue */
1377 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1378 unsigned long Size
,string Dsc
,string ShortDesc
) :
1379 Item(Owner
), Md5Hash(MD5
)
1381 Retries
= _config
->FindI("Acquire::Retries",0);
1383 DestFile
= flNotDir(URI
);
1387 Desc
.Description
= Dsc
;
1390 // Set the short description to the archive component
1391 Desc
.ShortDesc
= ShortDesc
;
1393 // Get the transfer sizes
1396 if (stat(DestFile
.c_str(),&Buf
) == 0)
1398 // Hmm, the partial file is too big, erase it
1399 if ((unsigned)Buf
.st_size
> Size
)
1400 unlink(DestFile
.c_str());
1402 PartialSize
= Buf
.st_size
;
1408 // AcqFile::Done - Item downloaded OK /*{{{*/
1409 // ---------------------------------------------------------------------
1411 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1412 pkgAcquire::MethodConfig
*Cnf
)
1415 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1420 ErrorText
= "MD5Sum mismatch";
1421 Rename(DestFile
,DestFile
+ ".FAILED");
1426 Item::Done(Message
,Size
,MD5
,Cnf
);
1428 string FileName
= LookupTag(Message
,"Filename");
1429 if (FileName
.empty() == true)
1432 ErrorText
= "Method gave a blank filename";
1438 // The files timestamp matches
1439 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1442 // We have to copy it into place
1443 if (FileName
!= DestFile
)
1446 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1447 Cnf
->Removable
== true)
1449 Desc
.URI
= "copy:" + FileName
;
1454 // Erase the file if it is a symlink so we can overwrite it
1456 if (lstat(DestFile
.c_str(),&St
) == 0)
1458 if (S_ISLNK(St
.st_mode
) != 0)
1459 unlink(DestFile
.c_str());
1463 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1465 ErrorText
= "Link to " + DestFile
+ " failure ";
1472 // AcqFile::Failed - Failure handler /*{{{*/
1473 // ---------------------------------------------------------------------
1474 /* Here we try other sources */
1475 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1477 ErrorText
= LookupTag(Message
,"Message");
1479 // This is the retry counter
1481 Cnf
->LocalOnly
== false &&
1482 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1489 Item::Failed(Message
,Cnf
);