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)
227 string tmp
= Tags
.FindS("SHA1-Current");
228 std::stringstream
ss(tmp
);
231 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnly
);
233 SHA1
.AddFD(fd
.Fd(), fd
.Size());
234 local_sha1
= string(SHA1
.Result());
236 if(local_sha1
== ServerSha1
) {
238 std::clog
<< "Package file is up-to-date" << std::endl
;
239 // set found to true, this will queue a pkgAcqIndexDiffs with
240 // a empty availabe_patches
244 std::clog
<< "SHA1-Current: " << ServerSha1
<< std::endl
;
246 // check the historie and see what patches we need
247 string history
= Tags
.FindS("SHA1-History");
248 std::stringstream
hist(history
);
249 while(hist
>> d
.sha1
>> size
>> d
.file
) {
250 d
.size
= atoi(size
.c_str());
251 // read until the first match is found
252 if(d
.sha1
== local_sha1
)
254 // from that point on, we probably need all diffs
257 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
258 available_patches
.push_back(d
);
263 // no information how to get the patches, bail out
266 std::clog
<< "Can't find a patch in the index file" << std::endl
;
267 // Failed will queue a big package file
271 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
272 ExpectedMD5
, available_patches
);
283 void pkgAcqDiffIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
286 std::clog
<< "pkgAcqDiffIndex failed: " << Desc
.URI
<< std::endl
287 << "Falling back to normal index file aquire" << std::endl
;
289 new pkgAcqIndex(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
297 void pkgAcqDiffIndex::Done(string Message
,unsigned long Size
,string Md5Hash
,
298 pkgAcquire::MethodConfig
*Cnf
)
301 std::clog
<< "pkgAcqDiffIndex::Done(): " << Desc
.URI
<< std::endl
;
303 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
306 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
308 // sucess in downloading the index
310 FinalFile
+= string(".IndexDiff");
312 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
314 Rename(DestFile
,FinalFile
);
315 chmod(FinalFile
.c_str(),0644);
316 DestFile
= FinalFile
;
318 if(!ParseDiffIndex(DestFile
))
319 return Failed("", NULL
);
329 // AcqIndexDiffs::AcqIndexDiffs - Constructor
330 // ---------------------------------------------------------------------
331 /* The package diff is added to the queue. one object is constructed
332 * for each diff and the index
334 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire
*Owner
,
335 string URI
,string URIDesc
,string ShortDesc
,
336 string ExpectedMD5
, vector
<DiffInfo
> diffs
)
337 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
),
338 available_patches(diffs
)
341 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
342 DestFile
+= URItoFileName(URI
);
344 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
346 Desc
.Description
= URIDesc
;
348 Desc
.ShortDesc
= ShortDesc
;
350 if(available_patches
.size() == 0) {
351 // we are done (yeah!)
355 State
= StateFetchDiff
;
361 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
364 std::clog
<< "pkgAcqIndexDiffs failed: " << Desc
.URI
<< std::endl
365 << "Falling back to normal index file aquire" << std::endl
;
366 new pkgAcqIndex(Owner
, RealURI
, Description
,Desc
.ShortDesc
,
372 // helper that cleans the item out of the fetcher queue
373 void pkgAcqIndexDiffs::Finish(bool allDone
)
375 // we restore the original name, this is required, otherwise
376 // the file will be cleaned
378 DestFile
= _config
->FindDir("Dir::State::lists");
379 DestFile
+= URItoFileName(RealURI
);
381 // do the final md5sum checking
383 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
384 sum
.AddFD(Fd
.Fd(), Fd
.Size());
386 string MD5
= (string
)sum
.Result();
388 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
390 Status
= StatAuthError
;
391 ErrorText
= _("MD5Sum mismatch");
392 Rename(DestFile
,DestFile
+ ".FAILED");
397 // this is for the "real" finish
402 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
407 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
416 bool pkgAcqIndexDiffs::QueueNextDiff()
419 // calc sha1 of the just patched file
420 string FinalFile
= _config
->FindDir("Dir::State::lists");
421 FinalFile
+= URItoFileName(RealURI
);
423 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
425 SHA1
.AddFD(fd
.Fd(), fd
.Size());
426 string local_sha1
= string(SHA1
.Result());
428 std::clog
<< "QueueNextDiff: "
429 << FinalFile
<< " (" << local_sha1
<< ")"<<std::endl
;
431 // remove all patches until the next matching patch is found
432 // this requires the Index file to be ordered
433 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
434 available_patches
.size() > 0 && I
!= available_patches
.end()
435 && (*I
).sha1
!= local_sha1
;
437 available_patches
.erase(I
);
440 // error checking and falling back if no patch was found
441 if(available_patches
.size() == 0) {
446 // queue the right diff
447 Desc
.URI
= string(RealURI
) + ".diff/" + available_patches
[0].file
+ ".gz";
448 Desc
.Description
= available_patches
[0].file
+ string(".pdiff");
450 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
451 DestFile
+= URItoFileName(RealURI
+ ".diff/" + available_patches
[0].file
);
454 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
463 void pkgAcqIndexDiffs::Done(string Message
,unsigned long Size
,string Md5Hash
,
464 pkgAcquire::MethodConfig
*Cnf
)
467 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
469 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
472 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
474 // sucess in downloading a diff, enter ApplyDiff state
475 if(State
== StateFetchDiff
)
479 std::clog
<< "Sending to gzip method: " << FinalFile
<< std::endl
;
481 string FileName
= LookupTag(Message
,"Filename");
482 State
= StateUnzipDiff
;
483 Desc
.URI
= "gzip:" + FileName
;
484 DestFile
+= ".decomp";
490 // sucess in downloading a diff, enter ApplyDiff state
491 if(State
== StateUnzipDiff
)
494 // rred excepts the patch as $FinalFile.ed
495 Rename(DestFile
,FinalFile
+".ed");
498 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
500 State
= StateApplyDiff
;
501 Desc
.URI
= "rred:" + FinalFile
;
508 // success in download/apply a diff, queue next (if needed)
509 if(State
== StateApplyDiff
)
511 // remove the just applied patch
512 available_patches
.erase(available_patches
.begin());
517 std::clog
<< "Moving patched file in place: " << std::endl
518 << DestFile
<< " -> " << FinalFile
<< std::endl
;
520 Rename(DestFile
,FinalFile
);
522 // see if there is more to download
523 if(available_patches
.size() > 0) {
524 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
525 ExpectedMD5
, available_patches
);
533 // AcqIndex::AcqIndex - Constructor /*{{{*/
534 // ---------------------------------------------------------------------
535 /* The package file is added to the queue and a second class is
536 instantiated to fetch the revision file */
537 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
538 string URI
,string URIDesc
,string ShortDesc
,
539 string ExpectedMD5
, string comprExt
)
540 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
)
542 Decompression
= false;
545 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
546 DestFile
+= URItoFileName(URI
);
550 // autoselect the compression method
551 if(FileExists("/usr/bin/bzip2"))
552 CompressionExtension
= ".bz2";
554 CompressionExtension
= ".gz";
556 CompressionExtension
= comprExt
;
558 Desc
.URI
= URI
+ CompressionExtension
;
560 Desc
.Description
= URIDesc
;
562 Desc
.ShortDesc
= ShortDesc
;
567 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
568 // ---------------------------------------------------------------------
569 /* The only header we use is the last-modified header. */
570 string
pkgAcqIndex::Custom600Headers()
572 string Final
= _config
->FindDir("Dir::State::lists");
573 Final
+= URItoFileName(RealURI
);
576 if (stat(Final
.c_str(),&Buf
) != 0)
577 return "\nIndex-File: true";
579 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
583 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
585 // no .bz2 found, retry with .gz
586 if(Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1) == "bz2") {
587 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
589 // retry with a gzip one
590 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
591 ExpectedMD5
, string(".gz"));
599 Item::Failed(Message
,Cnf
);
603 // AcqIndex::Done - Finished a fetch /*{{{*/
604 // ---------------------------------------------------------------------
605 /* This goes through a number of states.. On the initial fetch the
606 method could possibly return an alternate filename which points
607 to the uncompressed version of the file. If this is so the file
608 is copied into the partial directory. In all other cases the file
609 is decompressed with a gzip uri. */
610 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
611 pkgAcquire::MethodConfig
*Cfg
)
613 Item::Done(Message
,Size
,MD5
,Cfg
);
615 if (Decompression
== true)
617 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
619 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
620 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
626 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
627 sum
.AddFD(Fd
.Fd(), Fd
.Size());
629 MD5
= (string
)sum
.Result();
632 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
634 Status
= StatAuthError
;
635 ErrorText
= _("MD5Sum mismatch");
636 Rename(DestFile
,DestFile
+ ".FAILED");
639 // Done, move it into position
640 string FinalFile
= _config
->FindDir("Dir::State::lists");
641 FinalFile
+= URItoFileName(RealURI
);
642 Rename(DestFile
,FinalFile
);
643 chmod(FinalFile
.c_str(),0644);
645 /* We restore the original name to DestFile so that the clean operation
647 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
648 DestFile
+= URItoFileName(RealURI
);
650 // Remove the compressed version.
652 unlink(DestFile
.c_str());
659 // Handle the unzipd case
660 string FileName
= LookupTag(Message
,"Alt-Filename");
661 if (FileName
.empty() == false)
663 // The files timestamp matches
664 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
667 Decompression
= true;
669 DestFile
+= ".decomp";
670 Desc
.URI
= "copy:" + FileName
;
676 FileName
= LookupTag(Message
,"Filename");
677 if (FileName
.empty() == true)
680 ErrorText
= "Method gave a blank filename";
683 // The files timestamp matches
684 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
687 if (FileName
== DestFile
)
692 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1);
695 decompProg
= "bzip2";
696 else if(compExt
== ".gz")
699 _error
->Error("Unsupported extension: %s", compExt
.c_str());
703 Decompression
= true;
704 DestFile
+= ".decomp";
705 Desc
.URI
= string(decompProg
) + ":" + FileName
;
710 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
711 string URI
,string URIDesc
,string ShortDesc
,
712 string MetaIndexURI
, string MetaIndexURIDesc
,
713 string MetaIndexShortDesc
,
714 const vector
<IndexTarget
*>* IndexTargets
,
715 indexRecords
* MetaIndexParser
) :
716 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
717 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
)
719 this->MetaIndexParser
= MetaIndexParser
;
720 this->IndexTargets
= IndexTargets
;
721 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
722 DestFile
+= URItoFileName(URI
);
724 // remove any partial downloaded sig-file. it may confuse proxies
725 // and is too small to warrant a partial download anyway
726 unlink(DestFile
.c_str());
729 Desc
.Description
= URIDesc
;
731 Desc
.ShortDesc
= ShortDesc
;
735 string Final
= _config
->FindDir("Dir::State::lists");
736 Final
+= URItoFileName(RealURI
);
738 if (stat(Final
.c_str(),&Buf
) == 0)
740 // File was already in place. It needs to be re-verified
741 // because Release might have changed, so Move it into partial
742 Rename(Final
,DestFile
);
743 // unlink the file and do not try to use I-M-S and Last-Modified
744 // if the users proxy is broken
745 if(_config
->FindB("Acquire::BrokenProxy", false) == true) {
746 std::cerr
<< "forcing re-get of the signature file as requested" << std::endl
;
747 unlink(DestFile
.c_str());
754 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
755 // ---------------------------------------------------------------------
756 /* The only header we use is the last-modified header. */
757 string
pkgAcqMetaSig::Custom600Headers()
760 if (stat(DestFile
.c_str(),&Buf
) != 0)
761 return "\nIndex-File: true";
763 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
766 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
767 pkgAcquire::MethodConfig
*Cfg
)
769 Item::Done(Message
,Size
,MD5
,Cfg
);
771 string FileName
= LookupTag(Message
,"Filename");
772 if (FileName
.empty() == true)
775 ErrorText
= "Method gave a blank filename";
779 if (FileName
!= DestFile
)
781 // We have to copy it into place
783 Desc
.URI
= "copy:" + FileName
;
790 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
791 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
792 DestFile
, IndexTargets
, MetaIndexParser
);
796 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
798 // Delete any existing sigfile, so that this source isn't
799 // mistakenly trusted
800 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
801 unlink(Final
.c_str());
803 // if we get a timeout if fail
804 if(LookupTag(Message
,"FailReason") == "Timeout" ||
805 LookupTag(Message
,"FailReason") == "TmpResolveFailure") {
806 Item::Failed(Message
,Cnf
);
810 // queue a pkgAcqMetaIndex with no sigfile
811 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
812 "", IndexTargets
, MetaIndexParser
);
814 if (Cnf
->LocalOnly
== true ||
815 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
824 Item::Failed(Message
,Cnf
);
827 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
828 string URI
,string URIDesc
,string ShortDesc
,
830 const vector
<struct IndexTarget
*>* IndexTargets
,
831 indexRecords
* MetaIndexParser
) :
832 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
834 this->AuthPass
= false;
835 this->MetaIndexParser
= MetaIndexParser
;
836 this->IndexTargets
= IndexTargets
;
837 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
838 DestFile
+= URItoFileName(URI
);
841 Desc
.Description
= URIDesc
;
843 Desc
.ShortDesc
= ShortDesc
;
850 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
851 // ---------------------------------------------------------------------
852 /* The only header we use is the last-modified header. */
853 string
pkgAcqMetaIndex::Custom600Headers()
855 string Final
= _config
->FindDir("Dir::State::lists");
856 Final
+= URItoFileName(RealURI
);
859 if (stat(Final
.c_str(),&Buf
) != 0)
860 return "\nIndex-File: true";
862 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
865 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
866 pkgAcquire::MethodConfig
*Cfg
)
868 Item::Done(Message
,Size
,MD5
,Cfg
);
870 // MetaIndexes are done in two passes: one to download the
871 // metaindex with an appropriate method, and a second to verify it
872 // with the gpgv method
874 if (AuthPass
== true)
880 RetrievalDone(Message
);
882 // Still more retrieving to do
887 // There was no signature file, so we are finished. Download
888 // the indexes without verification.
893 // There was a signature file, so pass it to gpgv for
896 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
897 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
898 << SigFile
<< "," << DestFile
<< ")\n";
900 Desc
.URI
= "gpgv:" + SigFile
;
907 void pkgAcqMetaIndex::RetrievalDone(string Message
)
909 // We have just finished downloading a Release file (it is not
912 string FileName
= LookupTag(Message
,"Filename");
913 if (FileName
.empty() == true)
916 ErrorText
= "Method gave a blank filename";
920 if (FileName
!= DestFile
)
923 Desc
.URI
= "copy:" + FileName
;
930 string FinalFile
= _config
->FindDir("Dir::State::lists");
931 FinalFile
+= URItoFileName(RealURI
);
933 // The files timestamp matches
934 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
936 // Move it into position
937 Rename(DestFile
,FinalFile
);
939 DestFile
= FinalFile
;
942 void pkgAcqMetaIndex::AuthDone(string Message
)
944 // At this point, the gpgv method has succeeded, so there is a
945 // valid signature from a key in the trusted keyring. We
946 // perform additional verification of its contents, and use them
947 // to verify the indexes we are about to download
949 if (!MetaIndexParser
->Load(DestFile
))
951 Status
= StatAuthError
;
952 ErrorText
= MetaIndexParser
->ErrorText
;
961 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
962 std::cerr
<< "Signature verification succeeded: "
963 << DestFile
<< std::endl
;
965 // Download further indexes with verification
968 // Done, move signature file into position
970 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
971 URItoFileName(RealURI
) + ".gpg";
972 Rename(SigFile
,VerifiedSigFile
);
973 chmod(VerifiedSigFile
.c_str(),0644);
976 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
978 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
979 Target
!= IndexTargets
->end();
982 string ExpectedIndexMD5
;
985 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
988 Status
= StatAuthError
;
989 ErrorText
= "Unable to find expected entry "
990 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
993 ExpectedIndexMD5
= Record
->MD5Hash
;
994 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
996 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
997 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
999 if (ExpectedIndexMD5
.empty())
1001 Status
= StatAuthError
;
1002 ErrorText
= "Unable to find MD5 sum for "
1003 + (*Target
)->MetaKey
+ " in Meta-index file";
1008 // Queue Packages file
1009 new pkgAcqDiffIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1010 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
1014 bool pkgAcqMetaIndex::VerifyVendor()
1016 // // Maybe this should be made available from above so we don't have
1017 // // to read and parse it every time?
1018 // pkgVendorList List;
1019 // List.ReadMainList();
1021 // const Vendor* Vndr = NULL;
1022 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
1024 // string::size_type pos = (*I).find("VALIDSIG ");
1025 // if (_config->FindB("Debug::Vendor", false))
1026 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
1028 // if (pos != std::string::npos)
1030 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
1031 // if (_config->FindB("Debug::Vendor", false))
1032 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
1034 // Vndr = List.FindVendor(Fingerprint) != "";
1035 // if (Vndr != NULL);
1040 string Transformed
= MetaIndexParser
->GetExpectedDist();
1042 if (Transformed
== "../project/experimental")
1044 Transformed
= "experimental";
1047 string::size_type pos
= Transformed
.rfind('/');
1048 if (pos
!= string::npos
)
1050 Transformed
= Transformed
.substr(0, pos
);
1053 if (Transformed
== ".")
1058 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1060 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1061 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1062 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1065 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1067 // This might become fatal one day
1068 // Status = StatAuthError;
1069 // ErrorText = "Conflicting distribution; expected "
1070 // + MetaIndexParser->GetExpectedDist() + " but got "
1071 // + MetaIndexParser->GetDist();
1073 if (!Transformed
.empty())
1075 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
1076 Desc
.Description
.c_str(),
1077 Transformed
.c_str(),
1078 MetaIndexParser
->GetDist().c_str());
1085 // pkgAcqMetaIndex::Failed - no Release file present or no signature
1086 // file present /*{{{*/
1087 // ---------------------------------------------------------------------
1089 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1091 if (AuthPass
== true)
1093 // gpgv method failed
1094 _error
->Warning("GPG error: %s: %s",
1095 Desc
.Description
.c_str(),
1096 LookupTag(Message
,"Message").c_str());
1099 // No Release file was present, or verification failed, so fall
1100 // back to queueing Packages files without verification
1101 QueueIndexes(false);
1106 // AcqArchive::AcqArchive - Constructor /*{{{*/
1107 // ---------------------------------------------------------------------
1108 /* This just sets up the initial fetch environment and queues the first
1110 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
1111 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
1112 string
&StoreFilename
) :
1113 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
1114 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
1117 Retries
= _config
->FindI("Acquire::Retries",0);
1119 if (Version
.Arch() == 0)
1121 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1122 "This might mean you need to manually fix this package. "
1123 "(due to missing arch)"),
1124 Version
.ParentPkg().Name());
1128 /* We need to find a filename to determine the extension. We make the
1129 assumption here that all the available sources for this version share
1130 the same extension.. */
1131 // Skip not source sources, they do not have file fields.
1132 for (; Vf
.end() == false; Vf
++)
1134 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1139 // Does not really matter here.. we are going to fail out below
1140 if (Vf
.end() != true)
1142 // If this fails to get a file name we will bomb out below.
1143 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1144 if (_error
->PendingError() == true)
1147 // Generate the final file name as: package_version_arch.foo
1148 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1149 QuoteString(Version
.VerStr(),"_:") + '_' +
1150 QuoteString(Version
.Arch(),"_:.") +
1151 "." + flExtension(Parse
.FileName());
1154 // check if we have one trusted source for the package. if so, switch
1155 // to "TrustedOnly" mode
1156 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
1158 pkgIndexFile
*Index
;
1159 if (Sources
->FindIndex(i
.File(),Index
) == false)
1161 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1163 std::cerr
<< "Checking index: " << Index
->Describe()
1164 << "(Trusted=" << Index
->IsTrusted() << ")\n";
1166 if (Index
->IsTrusted()) {
1173 if (QueueNext() == false && _error
->PendingError() == false)
1174 _error
->Error(_("I wasn't able to locate file for the %s package. "
1175 "This might mean you need to manually fix this package."),
1176 Version
.ParentPkg().Name());
1179 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1180 // ---------------------------------------------------------------------
1181 /* This queues the next available file version for download. It checks if
1182 the archive is already available in the cache and stashs the MD5 for
1184 bool pkgAcqArchive::QueueNext()
1186 for (; Vf
.end() == false; Vf
++)
1188 // Ignore not source sources
1189 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1192 // Try to cross match against the source list
1193 pkgIndexFile
*Index
;
1194 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1197 // only try to get a trusted package from another source if that source
1199 if(Trusted
&& !Index
->IsTrusted())
1202 // Grab the text package record
1203 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1204 if (_error
->PendingError() == true)
1207 string PkgFile
= Parse
.FileName();
1208 MD5
= Parse
.MD5Hash();
1209 if (PkgFile
.empty() == true)
1210 return _error
->Error(_("The package index files are corrupted. No Filename: "
1211 "field for package %s."),
1212 Version
.ParentPkg().Name());
1214 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1215 Desc
.Description
= Index
->ArchiveInfo(Version
);
1217 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1219 // See if we already have the file. (Legacy filenames)
1220 FileSize
= Version
->Size
;
1221 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1223 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1225 // Make sure the size matches
1226 if ((unsigned)Buf
.st_size
== Version
->Size
)
1231 StoreFilename
= DestFile
= FinalFile
;
1235 /* Hmm, we have a file and its size does not match, this means it is
1236 an old style mismatched arch */
1237 unlink(FinalFile
.c_str());
1240 // Check it again using the new style output filenames
1241 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1242 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1244 // Make sure the size matches
1245 if ((unsigned)Buf
.st_size
== Version
->Size
)
1250 StoreFilename
= DestFile
= FinalFile
;
1254 /* Hmm, we have a file and its size does not match, this shouldnt
1256 unlink(FinalFile
.c_str());
1259 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1261 // Check the destination file
1262 if (stat(DestFile
.c_str(),&Buf
) == 0)
1264 // Hmm, the partial file is too big, erase it
1265 if ((unsigned)Buf
.st_size
> Version
->Size
)
1266 unlink(DestFile
.c_str());
1268 PartialSize
= Buf
.st_size
;
1273 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1274 Desc
.Description
= Index
->ArchiveInfo(Version
);
1276 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1285 // AcqArchive::Done - Finished fetching /*{{{*/
1286 // ---------------------------------------------------------------------
1288 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
1289 pkgAcquire::MethodConfig
*Cfg
)
1291 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
1294 if (Size
!= Version
->Size
)
1297 ErrorText
= _("Size mismatch");
1302 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1307 ErrorText
= _("MD5Sum mismatch");
1308 if(FileExists(DestFile
))
1309 Rename(DestFile
,DestFile
+ ".FAILED");
1314 // Grab the output filename
1315 string FileName
= LookupTag(Message
,"Filename");
1316 if (FileName
.empty() == true)
1319 ErrorText
= "Method gave a blank filename";
1325 // Reference filename
1326 if (FileName
!= DestFile
)
1328 StoreFilename
= DestFile
= FileName
;
1333 // Done, move it into position
1334 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1335 FinalFile
+= flNotDir(StoreFilename
);
1336 Rename(DestFile
,FinalFile
);
1338 StoreFilename
= DestFile
= FinalFile
;
1342 // AcqArchive::Failed - Failure handler /*{{{*/
1343 // ---------------------------------------------------------------------
1344 /* Here we try other sources */
1345 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1347 ErrorText
= LookupTag(Message
,"Message");
1349 /* We don't really want to retry on failed media swaps, this prevents
1350 that. An interesting observation is that permanent failures are not
1352 if (Cnf
->Removable
== true &&
1353 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1355 // Vf = Version.FileList();
1356 while (Vf
.end() == false) Vf
++;
1357 StoreFilename
= string();
1358 Item::Failed(Message
,Cnf
);
1362 if (QueueNext() == false)
1364 // This is the retry counter
1366 Cnf
->LocalOnly
== false &&
1367 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1370 Vf
= Version
.FileList();
1371 if (QueueNext() == true)
1375 StoreFilename
= string();
1376 Item::Failed(Message
,Cnf
);
1380 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1381 // trusted source /*{{{*/
1382 // ---------------------------------------------------------------------
1383 bool pkgAcqArchive::IsTrusted()
1388 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1389 // ---------------------------------------------------------------------
1391 void pkgAcqArchive::Finished()
1393 if (Status
== pkgAcquire::Item::StatDone
&&
1396 StoreFilename
= string();
1400 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1401 // ---------------------------------------------------------------------
1402 /* The file is added to the queue */
1403 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1404 unsigned long Size
,string Dsc
,string ShortDesc
) :
1405 Item(Owner
), Md5Hash(MD5
)
1407 Retries
= _config
->FindI("Acquire::Retries",0);
1409 DestFile
= flNotDir(URI
);
1413 Desc
.Description
= Dsc
;
1416 // Set the short description to the archive component
1417 Desc
.ShortDesc
= ShortDesc
;
1419 // Get the transfer sizes
1422 if (stat(DestFile
.c_str(),&Buf
) == 0)
1424 // Hmm, the partial file is too big, erase it
1425 if ((unsigned)Buf
.st_size
> Size
)
1426 unlink(DestFile
.c_str());
1428 PartialSize
= Buf
.st_size
;
1434 // AcqFile::Done - Item downloaded OK /*{{{*/
1435 // ---------------------------------------------------------------------
1437 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1438 pkgAcquire::MethodConfig
*Cnf
)
1441 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1446 ErrorText
= "MD5Sum mismatch";
1447 Rename(DestFile
,DestFile
+ ".FAILED");
1452 Item::Done(Message
,Size
,MD5
,Cnf
);
1454 string FileName
= LookupTag(Message
,"Filename");
1455 if (FileName
.empty() == true)
1458 ErrorText
= "Method gave a blank filename";
1464 // The files timestamp matches
1465 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1468 // We have to copy it into place
1469 if (FileName
!= DestFile
)
1472 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1473 Cnf
->Removable
== true)
1475 Desc
.URI
= "copy:" + FileName
;
1480 // Erase the file if it is a symlink so we can overwrite it
1482 if (lstat(DestFile
.c_str(),&St
) == 0)
1484 if (S_ISLNK(St
.st_mode
) != 0)
1485 unlink(DestFile
.c_str());
1489 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1491 ErrorText
= "Link to " + DestFile
+ " failure ";
1498 // AcqFile::Failed - Failure handler /*{{{*/
1499 // ---------------------------------------------------------------------
1500 /* Here we try other sources */
1501 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1503 ErrorText
= LookupTag(Message
,"Message");
1505 // This is the retry counter
1507 Cnf
->LocalOnly
== false &&
1508 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1515 Item::Failed(Message
,Cnf
);