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 // FIXME: this file:/ check is a hack to prevent fetching
169 // from local sources. this is really silly, and
170 // should be fixed cleanly as soon as possible
171 if(!FileExists(CurrentPackagesFile
) ||
172 Desc
.URI
.substr(0,strlen("file:/")) == "file:/" ||
173 !_config
->FindB("Acquire::Diffs",true)) {
174 // we don't have a pkg file or we don't want to queue
176 std::clog
<< "No index file, local or canceld by user" << std::endl
;
182 std::clog
<< "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
183 << CurrentPackagesFile
<< std::endl
;
190 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
191 // ---------------------------------------------------------------------
192 /* The only header we use is the last-modified header. */
193 string
pkgAcqDiffIndex::Custom600Headers()
195 string Final
= _config
->FindDir("Dir::State::lists");
196 Final
+= URItoFileName(RealURI
) + string(".IndexDiff");
199 std::clog
<< "Custom600Header-IMS: " << Final
<< std::endl
;
202 if (stat(Final
.c_str(),&Buf
) != 0)
203 return "\nIndex-File: true";
205 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
209 bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile
)
212 std::clog
<< "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
217 vector
<DiffInfo
> available_patches
;
219 FileFd
Fd(IndexDiffFile
,FileFd::ReadOnly
);
221 if (_error
->PendingError() == true)
224 if(TF
.Step(Tags
) == true)
231 string tmp
= Tags
.FindS("SHA1-Current");
232 std::stringstream
ss(tmp
);
235 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnly
);
237 SHA1
.AddFD(fd
.Fd(), fd
.Size());
238 local_sha1
= string(SHA1
.Result());
240 if(local_sha1
== ServerSha1
) {
242 std::clog
<< "Package file is up-to-date" << std::endl
;
243 // set found to true, this will queue a pkgAcqIndexDiffs with
244 // a empty availabe_patches
248 std::clog
<< "SHA1-Current: " << ServerSha1
<< std::endl
;
250 // check the historie and see what patches we need
251 string history
= Tags
.FindS("SHA1-History");
252 std::stringstream
hist(history
);
253 while(hist
>> d
.sha1
>> size
>> d
.file
) {
254 d
.size
= atoi(size
.c_str());
255 // read until the first match is found
256 if(d
.sha1
== local_sha1
)
258 // from that point on, we probably need all diffs
261 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
262 available_patches
.push_back(d
);
267 // no information how to get the patches, bail out
270 std::clog
<< "Can't find a patch in the index file" << std::endl
;
271 // Failed will queue a big package file
275 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
276 ExpectedMD5
, available_patches
);
287 void pkgAcqDiffIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
290 std::clog
<< "pkgAcqDiffIndex failed: " << Desc
.URI
<< std::endl
291 << "Falling back to normal index file aquire" << std::endl
;
293 new pkgAcqIndex(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
301 void pkgAcqDiffIndex::Done(string Message
,unsigned long Size
,string Md5Hash
,
302 pkgAcquire::MethodConfig
*Cnf
)
305 std::clog
<< "pkgAcqDiffIndex::Done(): " << Desc
.URI
<< std::endl
;
307 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
310 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
312 // sucess in downloading the index
314 FinalFile
+= string(".IndexDiff");
316 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
318 Rename(DestFile
,FinalFile
);
319 chmod(FinalFile
.c_str(),0644);
320 DestFile
= FinalFile
;
322 if(!ParseDiffIndex(DestFile
))
323 return Failed("", NULL
);
333 // AcqIndexDiffs::AcqIndexDiffs - Constructor
334 // ---------------------------------------------------------------------
335 /* The package diff is added to the queue. one object is constructed
336 * for each diff and the index
338 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire
*Owner
,
339 string URI
,string URIDesc
,string ShortDesc
,
340 string ExpectedMD5
, vector
<DiffInfo
> diffs
)
341 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
),
342 available_patches(diffs
)
345 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
346 DestFile
+= URItoFileName(URI
);
348 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
350 Desc
.Description
= URIDesc
;
352 Desc
.ShortDesc
= ShortDesc
;
354 if(available_patches
.size() == 0) {
355 // we are done (yeah!)
359 State
= StateFetchDiff
;
365 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
368 std::clog
<< "pkgAcqIndexDiffs failed: " << Desc
.URI
<< std::endl
369 << "Falling back to normal index file aquire" << std::endl
;
370 new pkgAcqIndex(Owner
, RealURI
, Description
,Desc
.ShortDesc
,
376 // helper that cleans the item out of the fetcher queue
377 void pkgAcqIndexDiffs::Finish(bool allDone
)
379 // we restore the original name, this is required, otherwise
380 // the file will be cleaned
382 DestFile
= _config
->FindDir("Dir::State::lists");
383 DestFile
+= URItoFileName(RealURI
);
385 // do the final md5sum checking
387 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
388 sum
.AddFD(Fd
.Fd(), Fd
.Size());
390 string MD5
= (string
)sum
.Result();
392 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
394 Status
= StatAuthError
;
395 ErrorText
= _("MD5Sum mismatch");
396 Rename(DestFile
,DestFile
+ ".FAILED");
401 // this is for the "real" finish
406 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
411 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
420 bool pkgAcqIndexDiffs::QueueNextDiff()
423 // calc sha1 of the just patched file
424 string FinalFile
= _config
->FindDir("Dir::State::lists");
425 FinalFile
+= URItoFileName(RealURI
);
427 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
429 SHA1
.AddFD(fd
.Fd(), fd
.Size());
430 string local_sha1
= string(SHA1
.Result());
432 std::clog
<< "QueueNextDiff: "
433 << FinalFile
<< " (" << local_sha1
<< ")"<<std::endl
;
435 // remove all patches until the next matching patch is found
436 // this requires the Index file to be ordered
437 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
438 available_patches
.size() > 0 && I
!= available_patches
.end()
439 && (*I
).sha1
!= local_sha1
;
441 available_patches
.erase(I
);
444 // error checking and falling back if no patch was found
445 if(available_patches
.size() == 0) {
450 // queue the right diff
451 Desc
.URI
= string(RealURI
) + ".diff/" + available_patches
[0].file
+ ".gz";
452 Desc
.Description
= available_patches
[0].file
+ string(".pdiff");
454 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
455 DestFile
+= URItoFileName(RealURI
+ ".diff/" + available_patches
[0].file
);
458 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
467 void pkgAcqIndexDiffs::Done(string Message
,unsigned long Size
,string Md5Hash
,
468 pkgAcquire::MethodConfig
*Cnf
)
471 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
473 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
476 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
478 // sucess in downloading a diff, enter ApplyDiff state
479 if(State
== StateFetchDiff
)
483 std::clog
<< "Sending to gzip method: " << FinalFile
<< std::endl
;
485 string FileName
= LookupTag(Message
,"Filename");
486 State
= StateUnzipDiff
;
487 Desc
.URI
= "gzip:" + FileName
;
488 DestFile
+= ".decomp";
494 // sucess in downloading a diff, enter ApplyDiff state
495 if(State
== StateUnzipDiff
)
498 // rred excepts the patch as $FinalFile.ed
499 Rename(DestFile
,FinalFile
+".ed");
502 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
504 State
= StateApplyDiff
;
505 Desc
.URI
= "rred:" + FinalFile
;
512 // success in download/apply a diff, queue next (if needed)
513 if(State
== StateApplyDiff
)
515 // remove the just applied patch
516 available_patches
.erase(available_patches
.begin());
521 std::clog
<< "Moving patched file in place: " << std::endl
522 << DestFile
<< " -> " << FinalFile
<< std::endl
;
524 Rename(DestFile
,FinalFile
);
526 // see if there is more to download
527 if(available_patches
.size() > 0) {
528 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
529 ExpectedMD5
, available_patches
);
537 // AcqIndex::AcqIndex - Constructor /*{{{*/
538 // ---------------------------------------------------------------------
539 /* The package file is added to the queue and a second class is
540 instantiated to fetch the revision file */
541 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
542 string URI
,string URIDesc
,string ShortDesc
,
543 string ExpectedMD5
, string comprExt
)
544 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
)
546 Decompression
= false;
549 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
550 DestFile
+= URItoFileName(URI
);
554 // autoselect the compression method
555 if(FileExists("/usr/bin/bzip2"))
556 CompressionExtension
= ".bz2";
558 CompressionExtension
= ".gz";
560 CompressionExtension
= comprExt
;
562 Desc
.URI
= URI
+ CompressionExtension
;
564 Desc
.Description
= URIDesc
;
566 Desc
.ShortDesc
= ShortDesc
;
571 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
572 // ---------------------------------------------------------------------
573 /* The only header we use is the last-modified header. */
574 string
pkgAcqIndex::Custom600Headers()
576 string Final
= _config
->FindDir("Dir::State::lists");
577 Final
+= URItoFileName(RealURI
);
580 if (stat(Final
.c_str(),&Buf
) != 0)
581 return "\nIndex-File: true";
583 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
587 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
589 // no .bz2 found, retry with .gz
590 if(Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1) == "bz2") {
591 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
593 // retry with a gzip one
594 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
595 ExpectedMD5
, string(".gz"));
603 Item::Failed(Message
,Cnf
);
607 // AcqIndex::Done - Finished a fetch /*{{{*/
608 // ---------------------------------------------------------------------
609 /* This goes through a number of states.. On the initial fetch the
610 method could possibly return an alternate filename which points
611 to the uncompressed version of the file. If this is so the file
612 is copied into the partial directory. In all other cases the file
613 is decompressed with a gzip uri. */
614 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
615 pkgAcquire::MethodConfig
*Cfg
)
617 Item::Done(Message
,Size
,MD5
,Cfg
);
619 if (Decompression
== true)
621 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
623 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
624 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
630 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
631 sum
.AddFD(Fd
.Fd(), Fd
.Size());
633 MD5
= (string
)sum
.Result();
636 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
638 Status
= StatAuthError
;
639 ErrorText
= _("MD5Sum mismatch");
640 Rename(DestFile
,DestFile
+ ".FAILED");
643 // Done, move it into position
644 string FinalFile
= _config
->FindDir("Dir::State::lists");
645 FinalFile
+= URItoFileName(RealURI
);
646 Rename(DestFile
,FinalFile
);
647 chmod(FinalFile
.c_str(),0644);
649 /* We restore the original name to DestFile so that the clean operation
651 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
652 DestFile
+= URItoFileName(RealURI
);
654 // Remove the compressed version.
656 unlink(DestFile
.c_str());
663 // Handle the unzipd case
664 string FileName
= LookupTag(Message
,"Alt-Filename");
665 if (FileName
.empty() == false)
667 // The files timestamp matches
668 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
671 Decompression
= true;
673 DestFile
+= ".decomp";
674 Desc
.URI
= "copy:" + FileName
;
680 FileName
= LookupTag(Message
,"Filename");
681 if (FileName
.empty() == true)
684 ErrorText
= "Method gave a blank filename";
687 // The files timestamp matches
688 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
691 if (FileName
== DestFile
)
696 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1);
699 decompProg
= "bzip2";
700 else if(compExt
== ".gz")
703 _error
->Error("Unsupported extension: %s", compExt
.c_str());
707 Decompression
= true;
708 DestFile
+= ".decomp";
709 Desc
.URI
= string(decompProg
) + ":" + FileName
;
714 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
715 string URI
,string URIDesc
,string ShortDesc
,
716 string MetaIndexURI
, string MetaIndexURIDesc
,
717 string MetaIndexShortDesc
,
718 const vector
<IndexTarget
*>* IndexTargets
,
719 indexRecords
* MetaIndexParser
) :
720 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
721 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
)
723 this->MetaIndexParser
= MetaIndexParser
;
724 this->IndexTargets
= IndexTargets
;
725 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
726 DestFile
+= URItoFileName(URI
);
728 // remove any partial downloaded sig-file. it may confuse proxies
729 // and is too small to warrant a partial download anyway
730 unlink(DestFile
.c_str());
733 Desc
.Description
= URIDesc
;
735 Desc
.ShortDesc
= ShortDesc
;
739 string Final
= _config
->FindDir("Dir::State::lists");
740 Final
+= URItoFileName(RealURI
);
742 if (stat(Final
.c_str(),&Buf
) == 0)
744 // File was already in place. It needs to be re-verified
745 // because Release might have changed, so Move it into partial
746 Rename(Final
,DestFile
);
747 // unlink the file and do not try to use I-M-S and Last-Modified
748 // if the users proxy is broken
749 if(_config
->FindB("Acquire::BrokenProxy", false) == true) {
750 std::cerr
<< "forcing re-get of the signature file as requested" << std::endl
;
751 unlink(DestFile
.c_str());
758 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
759 // ---------------------------------------------------------------------
760 /* The only header we use is the last-modified header. */
761 string
pkgAcqMetaSig::Custom600Headers()
764 if (stat(DestFile
.c_str(),&Buf
) != 0)
765 return "\nIndex-File: true";
767 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
770 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
771 pkgAcquire::MethodConfig
*Cfg
)
773 Item::Done(Message
,Size
,MD5
,Cfg
);
775 string FileName
= LookupTag(Message
,"Filename");
776 if (FileName
.empty() == true)
779 ErrorText
= "Method gave a blank filename";
783 if (FileName
!= DestFile
)
785 // We have to copy it into place
787 Desc
.URI
= "copy:" + FileName
;
794 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
795 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
796 DestFile
, IndexTargets
, MetaIndexParser
);
800 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
802 // Delete any existing sigfile, so that this source isn't
803 // mistakenly trusted
804 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
805 unlink(Final
.c_str());
807 // if we get a timeout if fail
808 if(LookupTag(Message
,"FailReason") == "Timeout" ||
809 LookupTag(Message
,"FailReason") == "TmpResolveFailure") {
810 Item::Failed(Message
,Cnf
);
814 // queue a pkgAcqMetaIndex with no sigfile
815 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
816 "", IndexTargets
, MetaIndexParser
);
818 if (Cnf
->LocalOnly
== true ||
819 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
828 Item::Failed(Message
,Cnf
);
831 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
832 string URI
,string URIDesc
,string ShortDesc
,
834 const vector
<struct IndexTarget
*>* IndexTargets
,
835 indexRecords
* MetaIndexParser
) :
836 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
838 this->AuthPass
= false;
839 this->MetaIndexParser
= MetaIndexParser
;
840 this->IndexTargets
= IndexTargets
;
841 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
842 DestFile
+= URItoFileName(URI
);
845 Desc
.Description
= URIDesc
;
847 Desc
.ShortDesc
= ShortDesc
;
854 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
855 // ---------------------------------------------------------------------
856 /* The only header we use is the last-modified header. */
857 string
pkgAcqMetaIndex::Custom600Headers()
859 string Final
= _config
->FindDir("Dir::State::lists");
860 Final
+= URItoFileName(RealURI
);
863 if (stat(Final
.c_str(),&Buf
) != 0)
864 return "\nIndex-File: true";
866 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
869 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
870 pkgAcquire::MethodConfig
*Cfg
)
872 Item::Done(Message
,Size
,MD5
,Cfg
);
874 // MetaIndexes are done in two passes: one to download the
875 // metaindex with an appropriate method, and a second to verify it
876 // with the gpgv method
878 if (AuthPass
== true)
884 RetrievalDone(Message
);
886 // Still more retrieving to do
891 // There was no signature file, so we are finished. Download
892 // the indexes without verification.
897 // There was a signature file, so pass it to gpgv for
900 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
901 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
902 << SigFile
<< "," << DestFile
<< ")\n";
904 Desc
.URI
= "gpgv:" + SigFile
;
911 void pkgAcqMetaIndex::RetrievalDone(string Message
)
913 // We have just finished downloading a Release file (it is not
916 string FileName
= LookupTag(Message
,"Filename");
917 if (FileName
.empty() == true)
920 ErrorText
= "Method gave a blank filename";
924 if (FileName
!= DestFile
)
927 Desc
.URI
= "copy:" + FileName
;
934 string FinalFile
= _config
->FindDir("Dir::State::lists");
935 FinalFile
+= URItoFileName(RealURI
);
937 // The files timestamp matches
938 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
940 // Move it into position
941 Rename(DestFile
,FinalFile
);
943 DestFile
= FinalFile
;
946 void pkgAcqMetaIndex::AuthDone(string Message
)
948 // At this point, the gpgv method has succeeded, so there is a
949 // valid signature from a key in the trusted keyring. We
950 // perform additional verification of its contents, and use them
951 // to verify the indexes we are about to download
953 if (!MetaIndexParser
->Load(DestFile
))
955 Status
= StatAuthError
;
956 ErrorText
= MetaIndexParser
->ErrorText
;
965 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
966 std::cerr
<< "Signature verification succeeded: "
967 << DestFile
<< std::endl
;
969 // Download further indexes with verification
972 // Done, move signature file into position
974 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
975 URItoFileName(RealURI
) + ".gpg";
976 Rename(SigFile
,VerifiedSigFile
);
977 chmod(VerifiedSigFile
.c_str(),0644);
980 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
982 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
983 Target
!= IndexTargets
->end();
986 string ExpectedIndexMD5
;
989 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
992 Status
= StatAuthError
;
993 ErrorText
= "Unable to find expected entry "
994 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
997 ExpectedIndexMD5
= Record
->MD5Hash
;
998 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1000 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
1001 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
1003 if (ExpectedIndexMD5
.empty())
1005 Status
= StatAuthError
;
1006 ErrorText
= "Unable to find MD5 sum for "
1007 + (*Target
)->MetaKey
+ " in Meta-index file";
1012 // Queue Packages file
1013 new pkgAcqDiffIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1014 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
1018 bool pkgAcqMetaIndex::VerifyVendor()
1020 // // Maybe this should be made available from above so we don't have
1021 // // to read and parse it every time?
1022 // pkgVendorList List;
1023 // List.ReadMainList();
1025 // const Vendor* Vndr = NULL;
1026 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
1028 // string::size_type pos = (*I).find("VALIDSIG ");
1029 // if (_config->FindB("Debug::Vendor", false))
1030 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
1032 // if (pos != std::string::npos)
1034 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
1035 // if (_config->FindB("Debug::Vendor", false))
1036 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
1038 // Vndr = List.FindVendor(Fingerprint) != "";
1039 // if (Vndr != NULL);
1044 string Transformed
= MetaIndexParser
->GetExpectedDist();
1046 if (Transformed
== "../project/experimental")
1048 Transformed
= "experimental";
1051 string::size_type pos
= Transformed
.rfind('/');
1052 if (pos
!= string::npos
)
1054 Transformed
= Transformed
.substr(0, pos
);
1057 if (Transformed
== ".")
1062 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1064 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1065 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1066 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1069 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1071 // This might become fatal one day
1072 // Status = StatAuthError;
1073 // ErrorText = "Conflicting distribution; expected "
1074 // + MetaIndexParser->GetExpectedDist() + " but got "
1075 // + MetaIndexParser->GetDist();
1077 if (!Transformed
.empty())
1079 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
1080 Desc
.Description
.c_str(),
1081 Transformed
.c_str(),
1082 MetaIndexParser
->GetDist().c_str());
1089 // pkgAcqMetaIndex::Failed - no Release file present or no signature
1090 // file present /*{{{*/
1091 // ---------------------------------------------------------------------
1093 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1095 if (AuthPass
== true)
1097 // gpgv method failed
1098 _error
->Warning("GPG error: %s: %s",
1099 Desc
.Description
.c_str(),
1100 LookupTag(Message
,"Message").c_str());
1103 // No Release file was present, or verification failed, so fall
1104 // back to queueing Packages files without verification
1105 QueueIndexes(false);
1110 // AcqArchive::AcqArchive - Constructor /*{{{*/
1111 // ---------------------------------------------------------------------
1112 /* This just sets up the initial fetch environment and queues the first
1114 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
1115 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
1116 string
&StoreFilename
) :
1117 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
1118 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
1121 Retries
= _config
->FindI("Acquire::Retries",0);
1123 if (Version
.Arch() == 0)
1125 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1126 "This might mean you need to manually fix this package. "
1127 "(due to missing arch)"),
1128 Version
.ParentPkg().Name());
1132 /* We need to find a filename to determine the extension. We make the
1133 assumption here that all the available sources for this version share
1134 the same extension.. */
1135 // Skip not source sources, they do not have file fields.
1136 for (; Vf
.end() == false; Vf
++)
1138 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1143 // Does not really matter here.. we are going to fail out below
1144 if (Vf
.end() != true)
1146 // If this fails to get a file name we will bomb out below.
1147 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1148 if (_error
->PendingError() == true)
1151 // Generate the final file name as: package_version_arch.foo
1152 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1153 QuoteString(Version
.VerStr(),"_:") + '_' +
1154 QuoteString(Version
.Arch(),"_:.") +
1155 "." + flExtension(Parse
.FileName());
1158 // check if we have one trusted source for the package. if so, switch
1159 // to "TrustedOnly" mode
1160 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
1162 pkgIndexFile
*Index
;
1163 if (Sources
->FindIndex(i
.File(),Index
) == false)
1165 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1167 std::cerr
<< "Checking index: " << Index
->Describe()
1168 << "(Trusted=" << Index
->IsTrusted() << ")\n";
1170 if (Index
->IsTrusted()) {
1177 if (QueueNext() == false && _error
->PendingError() == false)
1178 _error
->Error(_("I wasn't able to locate file for the %s package. "
1179 "This might mean you need to manually fix this package."),
1180 Version
.ParentPkg().Name());
1183 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1184 // ---------------------------------------------------------------------
1185 /* This queues the next available file version for download. It checks if
1186 the archive is already available in the cache and stashs the MD5 for
1188 bool pkgAcqArchive::QueueNext()
1190 for (; Vf
.end() == false; Vf
++)
1192 // Ignore not source sources
1193 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1196 // Try to cross match against the source list
1197 pkgIndexFile
*Index
;
1198 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1201 // only try to get a trusted package from another source if that source
1203 if(Trusted
&& !Index
->IsTrusted())
1206 // Grab the text package record
1207 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1208 if (_error
->PendingError() == true)
1211 string PkgFile
= Parse
.FileName();
1212 MD5
= Parse
.MD5Hash();
1213 if (PkgFile
.empty() == true)
1214 return _error
->Error(_("The package index files are corrupted. No Filename: "
1215 "field for package %s."),
1216 Version
.ParentPkg().Name());
1218 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1219 Desc
.Description
= Index
->ArchiveInfo(Version
);
1221 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1223 // See if we already have the file. (Legacy filenames)
1224 FileSize
= Version
->Size
;
1225 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1227 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1229 // Make sure the size matches
1230 if ((unsigned)Buf
.st_size
== Version
->Size
)
1235 StoreFilename
= DestFile
= FinalFile
;
1239 /* Hmm, we have a file and its size does not match, this means it is
1240 an old style mismatched arch */
1241 unlink(FinalFile
.c_str());
1244 // Check it again using the new style output filenames
1245 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1246 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1248 // Make sure the size matches
1249 if ((unsigned)Buf
.st_size
== Version
->Size
)
1254 StoreFilename
= DestFile
= FinalFile
;
1258 /* Hmm, we have a file and its size does not match, this shouldnt
1260 unlink(FinalFile
.c_str());
1263 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1265 // Check the destination file
1266 if (stat(DestFile
.c_str(),&Buf
) == 0)
1268 // Hmm, the partial file is too big, erase it
1269 if ((unsigned)Buf
.st_size
> Version
->Size
)
1270 unlink(DestFile
.c_str());
1272 PartialSize
= Buf
.st_size
;
1277 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1278 Desc
.Description
= Index
->ArchiveInfo(Version
);
1280 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1289 // AcqArchive::Done - Finished fetching /*{{{*/
1290 // ---------------------------------------------------------------------
1292 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
1293 pkgAcquire::MethodConfig
*Cfg
)
1295 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
1298 if (Size
!= Version
->Size
)
1301 ErrorText
= _("Size mismatch");
1306 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1311 ErrorText
= _("MD5Sum mismatch");
1312 if(FileExists(DestFile
))
1313 Rename(DestFile
,DestFile
+ ".FAILED");
1318 // Grab the output filename
1319 string FileName
= LookupTag(Message
,"Filename");
1320 if (FileName
.empty() == true)
1323 ErrorText
= "Method gave a blank filename";
1329 // Reference filename
1330 if (FileName
!= DestFile
)
1332 StoreFilename
= DestFile
= FileName
;
1337 // Done, move it into position
1338 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1339 FinalFile
+= flNotDir(StoreFilename
);
1340 Rename(DestFile
,FinalFile
);
1342 StoreFilename
= DestFile
= FinalFile
;
1346 // AcqArchive::Failed - Failure handler /*{{{*/
1347 // ---------------------------------------------------------------------
1348 /* Here we try other sources */
1349 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1351 ErrorText
= LookupTag(Message
,"Message");
1353 /* We don't really want to retry on failed media swaps, this prevents
1354 that. An interesting observation is that permanent failures are not
1356 if (Cnf
->Removable
== true &&
1357 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1359 // Vf = Version.FileList();
1360 while (Vf
.end() == false) Vf
++;
1361 StoreFilename
= string();
1362 Item::Failed(Message
,Cnf
);
1366 if (QueueNext() == false)
1368 // This is the retry counter
1370 Cnf
->LocalOnly
== false &&
1371 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1374 Vf
= Version
.FileList();
1375 if (QueueNext() == true)
1379 StoreFilename
= string();
1380 Item::Failed(Message
,Cnf
);
1384 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1385 // trusted source /*{{{*/
1386 // ---------------------------------------------------------------------
1387 bool pkgAcqArchive::IsTrusted()
1392 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1393 // ---------------------------------------------------------------------
1395 void pkgAcqArchive::Finished()
1397 if (Status
== pkgAcquire::Item::StatDone
&&
1400 StoreFilename
= string();
1404 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1405 // ---------------------------------------------------------------------
1406 /* The file is added to the queue */
1407 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1408 unsigned long Size
,string Dsc
,string ShortDesc
) :
1409 Item(Owner
), Md5Hash(MD5
)
1411 Retries
= _config
->FindI("Acquire::Retries",0);
1413 DestFile
= flNotDir(URI
);
1417 Desc
.Description
= Dsc
;
1420 // Set the short description to the archive component
1421 Desc
.ShortDesc
= ShortDesc
;
1423 // Get the transfer sizes
1426 if (stat(DestFile
.c_str(),&Buf
) == 0)
1428 // Hmm, the partial file is too big, erase it
1429 if ((unsigned)Buf
.st_size
> Size
)
1430 unlink(DestFile
.c_str());
1432 PartialSize
= Buf
.st_size
;
1438 // AcqFile::Done - Item downloaded OK /*{{{*/
1439 // ---------------------------------------------------------------------
1441 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1442 pkgAcquire::MethodConfig
*Cnf
)
1445 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1450 ErrorText
= "MD5Sum mismatch";
1451 Rename(DestFile
,DestFile
+ ".FAILED");
1456 Item::Done(Message
,Size
,MD5
,Cnf
);
1458 string FileName
= LookupTag(Message
,"Filename");
1459 if (FileName
.empty() == true)
1462 ErrorText
= "Method gave a blank filename";
1468 // The files timestamp matches
1469 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1472 // We have to copy it into place
1473 if (FileName
!= DestFile
)
1476 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1477 Cnf
->Removable
== true)
1479 Desc
.URI
= "copy:" + FileName
;
1484 // Erase the file if it is a symlink so we can overwrite it
1486 if (lstat(DestFile
.c_str(),&St
) == 0)
1488 if (S_ISLNK(St
.st_mode
) != 0)
1489 unlink(DestFile
.c_str());
1493 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1495 ErrorText
= "Link to " + DestFile
+ " failure ";
1502 // AcqFile::Failed - Failure handler /*{{{*/
1503 // ---------------------------------------------------------------------
1504 /* Here we try other sources */
1505 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1507 ErrorText
= LookupTag(Message
,"Message");
1509 // This is the retry counter
1511 Cnf
->LocalOnly
== false &&
1512 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1519 Item::Failed(Message
,Cnf
);