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 acquire" << 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) == "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);
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 // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
715 // ---------------------------------------------------------------------
716 /* The Translation file is added to the queue */
717 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
,
718 string URI
,string URIDesc
,string ShortDesc
) :
719 pkgAcqIndex(Owner
, URI
, URIDesc
, ShortDesc
, "", "")
724 // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
725 // ---------------------------------------------------------------------
727 void pkgAcqIndexTrans::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
729 if (Cnf
->LocalOnly
== true ||
730 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
739 Item::Failed(Message
,Cnf
);
743 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
744 string URI
,string URIDesc
,string ShortDesc
,
745 string MetaIndexURI
, string MetaIndexURIDesc
,
746 string MetaIndexShortDesc
,
747 const vector
<IndexTarget
*>* IndexTargets
,
748 indexRecords
* MetaIndexParser
) :
749 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
750 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
751 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
)
753 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
754 DestFile
+= URItoFileName(URI
);
756 // remove any partial downloaded sig-file. it may confuse proxies
757 // and is too small to warrant a partial download anyway
758 unlink(DestFile
.c_str());
761 Desc
.Description
= URIDesc
;
763 Desc
.ShortDesc
= ShortDesc
;
767 string Final
= _config
->FindDir("Dir::State::lists");
768 Final
+= URItoFileName(RealURI
);
770 if (stat(Final
.c_str(),&Buf
) == 0)
772 // File was already in place. It needs to be re-verified
773 // because Release might have changed, so Move it into partial
774 Rename(Final
,DestFile
);
775 // unlink the file and do not try to use I-M-S and Last-Modified
776 // if the users proxy is broken
777 if(_config
->FindB("Acquire::BrokenProxy", false) == true) {
778 std::cerr
<< "forcing re-get of the signature file as requested" << std::endl
;
779 unlink(DestFile
.c_str());
786 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
787 // ---------------------------------------------------------------------
788 /* The only header we use is the last-modified header. */
789 string
pkgAcqMetaSig::Custom600Headers()
792 if (stat(DestFile
.c_str(),&Buf
) != 0)
793 return "\nIndex-File: true";
795 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
798 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
799 pkgAcquire::MethodConfig
*Cfg
)
801 Item::Done(Message
,Size
,MD5
,Cfg
);
803 string FileName
= LookupTag(Message
,"Filename");
804 if (FileName
.empty() == true)
807 ErrorText
= "Method gave a blank filename";
811 if (FileName
!= DestFile
)
813 // We have to copy it into place
815 Desc
.URI
= "copy:" + FileName
;
822 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
823 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
824 DestFile
, IndexTargets
, MetaIndexParser
);
828 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
830 // Delete any existing sigfile, so that this source isn't
831 // mistakenly trusted
832 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
833 unlink(Final
.c_str());
835 // if we get a timeout if fail
836 if(LookupTag(Message
,"FailReason") == "Timeout" ||
837 LookupTag(Message
,"FailReason") == "TmpResolveFailure") {
838 Item::Failed(Message
,Cnf
);
842 // queue a pkgAcqMetaIndex with no sigfile
843 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
844 "", IndexTargets
, MetaIndexParser
);
846 if (Cnf
->LocalOnly
== true ||
847 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
856 Item::Failed(Message
,Cnf
);
859 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
860 string URI
,string URIDesc
,string ShortDesc
,
862 const vector
<struct IndexTarget
*>* IndexTargets
,
863 indexRecords
* MetaIndexParser
) :
864 Item(Owner
), RealURI(URI
), SigFile(SigFile
), AuthPass(false),
865 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
), IMSHit(false)
867 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
868 DestFile
+= URItoFileName(URI
);
871 Desc
.Description
= URIDesc
;
873 Desc
.ShortDesc
= ShortDesc
;
880 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
881 // ---------------------------------------------------------------------
882 /* The only header we use is the last-modified header. */
883 string
pkgAcqMetaIndex::Custom600Headers()
885 string Final
= _config
->FindDir("Dir::State::lists");
886 Final
+= URItoFileName(RealURI
);
889 if (stat(Final
.c_str(),&Buf
) != 0)
890 return "\nIndex-File: true";
892 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
895 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
896 pkgAcquire::MethodConfig
*Cfg
)
898 Item::Done(Message
,Size
,MD5
,Cfg
);
900 // MetaIndexes are done in two passes: one to download the
901 // metaindex with an appropriate method, and a second to verify it
902 // with the gpgv method
904 if (AuthPass
== true)
910 RetrievalDone(Message
);
912 // Still more retrieving to do
917 // There was no signature file, so we are finished. Download
918 // the indexes without verification.
923 // There was a signature file, so pass it to gpgv for
926 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
927 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
928 << SigFile
<< "," << DestFile
<< ")\n";
930 Desc
.URI
= "gpgv:" + SigFile
;
937 void pkgAcqMetaIndex::RetrievalDone(string Message
)
939 // We have just finished downloading a Release file (it is not
942 string FileName
= LookupTag(Message
,"Filename");
943 if (FileName
.empty() == true)
946 ErrorText
= "Method gave a blank filename";
950 if (FileName
!= DestFile
)
953 Desc
.URI
= "copy:" + FileName
;
958 // see if the download was a IMSHit
959 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
963 string FinalFile
= _config
->FindDir("Dir::State::lists");
964 FinalFile
+= URItoFileName(RealURI
);
966 // The files timestamp matches
967 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
969 // Move it into position
970 Rename(DestFile
,FinalFile
);
972 DestFile
= FinalFile
;
975 void pkgAcqMetaIndex::AuthDone(string Message
)
977 // At this point, the gpgv method has succeeded, so there is a
978 // valid signature from a key in the trusted keyring. We
979 // perform additional verification of its contents, and use them
980 // to verify the indexes we are about to download
982 if (!MetaIndexParser
->Load(DestFile
))
984 Status
= StatAuthError
;
985 ErrorText
= MetaIndexParser
->ErrorText
;
989 if (!VerifyVendor(Message
))
994 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
995 std::cerr
<< "Signature verification succeeded: "
996 << DestFile
<< std::endl
;
998 // Download further indexes with verification
1001 // Done, move signature file into position
1003 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
1004 URItoFileName(RealURI
) + ".gpg";
1005 Rename(SigFile
,VerifiedSigFile
);
1006 chmod(VerifiedSigFile
.c_str(),0644);
1009 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
1011 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
1012 Target
!= IndexTargets
->end();
1015 string ExpectedIndexMD5
;
1018 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
1021 Status
= StatAuthError
;
1022 ErrorText
= "Unable to find expected entry "
1023 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
1026 ExpectedIndexMD5
= Record
->MD5Hash
;
1027 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1029 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
1030 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
1032 if (ExpectedIndexMD5
.empty())
1034 Status
= StatAuthError
;
1035 ErrorText
= "Unable to find MD5 sum for "
1036 + (*Target
)->MetaKey
+ " in Meta-index file";
1041 // Queue Packages file
1042 new pkgAcqDiffIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1043 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
1047 bool pkgAcqMetaIndex::VerifyVendor(string Message
)
1049 // // Maybe this should be made available from above so we don't have
1050 // // to read and parse it every time?
1051 // pkgVendorList List;
1052 // List.ReadMainList();
1054 // const Vendor* Vndr = NULL;
1055 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
1057 // string::size_type pos = (*I).find("VALIDSIG ");
1058 // if (_config->FindB("Debug::Vendor", false))
1059 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
1061 // if (pos != std::string::npos)
1063 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
1064 // if (_config->FindB("Debug::Vendor", false))
1065 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
1067 // Vndr = List.FindVendor(Fingerprint) != "";
1068 // if (Vndr != NULL);
1072 string::size_type pos
;
1074 // check for missing sigs (that where not fatal because otherwise we had
1077 string msg
= _("There are no public key available for the "
1078 "following key IDs:\n");
1079 pos
= Message
.find("NO_PUBKEY ");
1080 if (pos
!= std::string::npos
)
1082 string::size_type start
= pos
+strlen("NO_PUBKEY ");
1083 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
1084 missingkeys
+= (Fingerprint
);
1086 if(!missingkeys
.empty())
1087 _error
->Warning("%s", string(msg
+missingkeys
).c_str());
1089 string Transformed
= MetaIndexParser
->GetExpectedDist();
1091 if (Transformed
== "../project/experimental")
1093 Transformed
= "experimental";
1096 pos
= Transformed
.rfind('/');
1097 if (pos
!= string::npos
)
1099 Transformed
= Transformed
.substr(0, pos
);
1102 if (Transformed
== ".")
1107 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1109 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1110 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1111 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1114 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1116 // This might become fatal one day
1117 // Status = StatAuthError;
1118 // ErrorText = "Conflicting distribution; expected "
1119 // + MetaIndexParser->GetExpectedDist() + " but got "
1120 // + MetaIndexParser->GetDist();
1122 if (!Transformed
.empty())
1124 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
1125 Desc
.Description
.c_str(),
1126 Transformed
.c_str(),
1127 MetaIndexParser
->GetDist().c_str());
1134 // pkgAcqMetaIndex::Failed - no Release file present or no signature
1135 // file present /*{{{*/
1136 // ---------------------------------------------------------------------
1138 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1140 if (AuthPass
== true)
1142 // if we fail the authentication but got the file via a IMS-Hit
1143 // this means that the file wasn't downloaded and that it might be
1144 // just stale (server problem, proxy etc). we delete what we have
1145 // queue it again without i-m-s
1146 // alternatively we could just unlink the file and let the user try again
1152 unlink(DestFile
.c_str());
1154 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1155 DestFile
+= URItoFileName(RealURI
);
1161 // gpgv method failed
1162 _error
->Warning("GPG error: %s: %s",
1163 Desc
.Description
.c_str(),
1164 LookupTag(Message
,"Message").c_str());
1168 // No Release file was present, or verification failed, so fall
1169 // back to queueing Packages files without verification
1170 QueueIndexes(false);
1175 // AcqArchive::AcqArchive - Constructor /*{{{*/
1176 // ---------------------------------------------------------------------
1177 /* This just sets up the initial fetch environment and queues the first
1179 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
1180 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
1181 string
&StoreFilename
) :
1182 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
1183 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
1186 Retries
= _config
->FindI("Acquire::Retries",0);
1188 if (Version
.Arch() == 0)
1190 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1191 "This might mean you need to manually fix this package. "
1192 "(due to missing arch)"),
1193 Version
.ParentPkg().Name());
1197 /* We need to find a filename to determine the extension. We make the
1198 assumption here that all the available sources for this version share
1199 the same extension.. */
1200 // Skip not source sources, they do not have file fields.
1201 for (; Vf
.end() == false; Vf
++)
1203 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1208 // Does not really matter here.. we are going to fail out below
1209 if (Vf
.end() != true)
1211 // If this fails to get a file name we will bomb out below.
1212 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1213 if (_error
->PendingError() == true)
1216 // Generate the final file name as: package_version_arch.foo
1217 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1218 QuoteString(Version
.VerStr(),"_:") + '_' +
1219 QuoteString(Version
.Arch(),"_:.") +
1220 "." + flExtension(Parse
.FileName());
1223 // check if we have one trusted source for the package. if so, switch
1224 // to "TrustedOnly" mode
1225 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
1227 pkgIndexFile
*Index
;
1228 if (Sources
->FindIndex(i
.File(),Index
) == false)
1230 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1232 std::cerr
<< "Checking index: " << Index
->Describe()
1233 << "(Trusted=" << Index
->IsTrusted() << ")\n";
1235 if (Index
->IsTrusted()) {
1241 // "allow-unauthenticated" restores apts old fetching behaviour
1242 // that means that e.g. unauthenticated file:// uris are higher
1243 // priority than authenticated http:// uris
1244 if (_config
->FindB("APT::Get::AllowUnauthenticated",false) == true)
1248 if (QueueNext() == false && _error
->PendingError() == false)
1249 _error
->Error(_("I wasn't able to locate file for the %s package. "
1250 "This might mean you need to manually fix this package."),
1251 Version
.ParentPkg().Name());
1254 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1255 // ---------------------------------------------------------------------
1256 /* This queues the next available file version for download. It checks if
1257 the archive is already available in the cache and stashs the MD5 for
1259 bool pkgAcqArchive::QueueNext()
1261 for (; Vf
.end() == false; Vf
++)
1263 // Ignore not source sources
1264 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1267 // Try to cross match against the source list
1268 pkgIndexFile
*Index
;
1269 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1272 // only try to get a trusted package from another source if that source
1274 if(Trusted
&& !Index
->IsTrusted())
1277 // Grab the text package record
1278 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1279 if (_error
->PendingError() == true)
1282 string PkgFile
= Parse
.FileName();
1283 MD5
= Parse
.MD5Hash();
1284 if (PkgFile
.empty() == true)
1285 return _error
->Error(_("The package index files are corrupted. No Filename: "
1286 "field for package %s."),
1287 Version
.ParentPkg().Name());
1289 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1290 Desc
.Description
= Index
->ArchiveInfo(Version
);
1292 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1294 // See if we already have the file. (Legacy filenames)
1295 FileSize
= Version
->Size
;
1296 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1298 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1300 // Make sure the size matches
1301 if ((unsigned)Buf
.st_size
== Version
->Size
)
1306 StoreFilename
= DestFile
= FinalFile
;
1310 /* Hmm, we have a file and its size does not match, this means it is
1311 an old style mismatched arch */
1312 unlink(FinalFile
.c_str());
1315 // Check it again using the new style output filenames
1316 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1317 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1319 // Make sure the size matches
1320 if ((unsigned)Buf
.st_size
== Version
->Size
)
1325 StoreFilename
= DestFile
= FinalFile
;
1329 /* Hmm, we have a file and its size does not match, this shouldnt
1331 unlink(FinalFile
.c_str());
1334 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1336 // Check the destination file
1337 if (stat(DestFile
.c_str(),&Buf
) == 0)
1339 // Hmm, the partial file is too big, erase it
1340 if ((unsigned)Buf
.st_size
> Version
->Size
)
1341 unlink(DestFile
.c_str());
1343 PartialSize
= Buf
.st_size
;
1348 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1349 Desc
.Description
= Index
->ArchiveInfo(Version
);
1351 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1360 // AcqArchive::Done - Finished fetching /*{{{*/
1361 // ---------------------------------------------------------------------
1363 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
1364 pkgAcquire::MethodConfig
*Cfg
)
1366 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
1369 if (Size
!= Version
->Size
)
1372 ErrorText
= _("Size mismatch");
1377 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1382 ErrorText
= _("MD5Sum mismatch");
1383 if(FileExists(DestFile
))
1384 Rename(DestFile
,DestFile
+ ".FAILED");
1389 // Grab the output filename
1390 string FileName
= LookupTag(Message
,"Filename");
1391 if (FileName
.empty() == true)
1394 ErrorText
= "Method gave a blank filename";
1400 // Reference filename
1401 if (FileName
!= DestFile
)
1403 StoreFilename
= DestFile
= FileName
;
1408 // Done, move it into position
1409 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1410 FinalFile
+= flNotDir(StoreFilename
);
1411 Rename(DestFile
,FinalFile
);
1413 StoreFilename
= DestFile
= FinalFile
;
1417 // AcqArchive::Failed - Failure handler /*{{{*/
1418 // ---------------------------------------------------------------------
1419 /* Here we try other sources */
1420 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1422 ErrorText
= LookupTag(Message
,"Message");
1424 /* We don't really want to retry on failed media swaps, this prevents
1425 that. An interesting observation is that permanent failures are not
1427 if (Cnf
->Removable
== true &&
1428 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1430 // Vf = Version.FileList();
1431 while (Vf
.end() == false) Vf
++;
1432 StoreFilename
= string();
1433 Item::Failed(Message
,Cnf
);
1437 if (QueueNext() == false)
1439 // This is the retry counter
1441 Cnf
->LocalOnly
== false &&
1442 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1445 Vf
= Version
.FileList();
1446 if (QueueNext() == true)
1450 StoreFilename
= string();
1451 Item::Failed(Message
,Cnf
);
1455 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1456 // trusted source /*{{{*/
1457 // ---------------------------------------------------------------------
1458 bool pkgAcqArchive::IsTrusted()
1463 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1464 // ---------------------------------------------------------------------
1466 void pkgAcqArchive::Finished()
1468 if (Status
== pkgAcquire::Item::StatDone
&&
1471 StoreFilename
= string();
1475 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1476 // ---------------------------------------------------------------------
1477 /* The file is added to the queue */
1478 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1479 unsigned long Size
,string Dsc
,string ShortDesc
,
1480 const string
&DestDir
, const string
&DestFilename
) :
1481 Item(Owner
), Md5Hash(MD5
)
1483 Retries
= _config
->FindI("Acquire::Retries",0);
1485 if(!DestFilename
.empty())
1486 DestFile
= DestFilename
;
1487 else if(!DestDir
.empty())
1488 DestFile
= DestDir
+ "/" + flNotDir(URI
);
1490 DestFile
= flNotDir(URI
);
1494 Desc
.Description
= Dsc
;
1497 // Set the short description to the archive component
1498 Desc
.ShortDesc
= ShortDesc
;
1500 // Get the transfer sizes
1503 if (stat(DestFile
.c_str(),&Buf
) == 0)
1505 // Hmm, the partial file is too big, erase it
1506 if ((unsigned)Buf
.st_size
> Size
)
1507 unlink(DestFile
.c_str());
1509 PartialSize
= Buf
.st_size
;
1515 // AcqFile::Done - Item downloaded OK /*{{{*/
1516 // ---------------------------------------------------------------------
1518 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1519 pkgAcquire::MethodConfig
*Cnf
)
1522 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1527 ErrorText
= "MD5Sum mismatch";
1528 Rename(DestFile
,DestFile
+ ".FAILED");
1533 Item::Done(Message
,Size
,MD5
,Cnf
);
1535 string FileName
= LookupTag(Message
,"Filename");
1536 if (FileName
.empty() == true)
1539 ErrorText
= "Method gave a blank filename";
1545 // The files timestamp matches
1546 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1549 // We have to copy it into place
1550 if (FileName
!= DestFile
)
1553 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1554 Cnf
->Removable
== true)
1556 Desc
.URI
= "copy:" + FileName
;
1561 // Erase the file if it is a symlink so we can overwrite it
1563 if (lstat(DestFile
.c_str(),&St
) == 0)
1565 if (S_ISLNK(St
.st_mode
) != 0)
1566 unlink(DestFile
.c_str());
1570 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1572 ErrorText
= "Link to " + DestFile
+ " failure ";
1579 // AcqFile::Failed - Failure handler /*{{{*/
1580 // ---------------------------------------------------------------------
1581 /* Here we try other sources */
1582 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1584 ErrorText
= LookupTag(Message
,"Message");
1586 // This is the retry counter
1588 Cnf
->LocalOnly
== false &&
1589 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1596 Item::Failed(Message
,Cnf
);