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:/")
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
;
189 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
190 // ---------------------------------------------------------------------
191 /* The only header we use is the last-modified header. */
192 string
pkgAcqDiffIndex::Custom600Headers()
194 string Final
= _config
->FindDir("Dir::State::lists");
195 Final
+= URItoFileName(RealURI
) + string(".IndexDiff");
198 std::clog
<< "Custom600Header-IMS: " << Final
<< std::endl
;
201 if (stat(Final
.c_str(),&Buf
) != 0)
202 return "\nIndex-File: true";
204 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
208 bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile
)
211 std::clog
<< "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
216 vector
<DiffInfo
> available_patches
;
218 FileFd
Fd(IndexDiffFile
,FileFd::ReadOnly
);
220 if (_error
->PendingError() == true)
223 if(TF
.Step(Tags
) == true)
230 string tmp
= Tags
.FindS("SHA1-Current");
231 std::stringstream
ss(tmp
);
234 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnly
);
236 SHA1
.AddFD(fd
.Fd(), fd
.Size());
237 local_sha1
= string(SHA1
.Result());
239 if(local_sha1
== ServerSha1
)
241 // we have the same sha1 as the server
243 std::clog
<< "Package file is up-to-date" << std::endl
;
244 // set found to true, this will queue a pkgAcqIndexDiffs with
245 // a empty availabe_patches
251 std::clog
<< "SHA1-Current: " << ServerSha1
<< std::endl
;
253 // check the historie and see what patches we need
254 string history
= Tags
.FindS("SHA1-History");
255 std::stringstream
hist(history
);
256 while(hist
>> d
.sha1
>> size
>> d
.file
)
258 d
.size
= atoi(size
.c_str());
259 // read until the first match is found
260 if(d
.sha1
== local_sha1
)
262 // from that point on, we probably need all diffs
266 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
267 available_patches
.push_back(d
);
272 // no information how to get the patches, bail out
276 std::clog
<< "Can't find a patch in the index file" << std::endl
;
277 // Failed will queue a big package file
283 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
284 ExpectedMD5
, available_patches
);
295 void pkgAcqDiffIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
298 std::clog
<< "pkgAcqDiffIndex failed: " << Desc
.URI
<< std::endl
299 << "Falling back to normal index file aquire" << std::endl
;
301 new pkgAcqIndex(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
309 void pkgAcqDiffIndex::Done(string Message
,unsigned long Size
,string Md5Hash
,
310 pkgAcquire::MethodConfig
*Cnf
)
313 std::clog
<< "pkgAcqDiffIndex::Done(): " << Desc
.URI
<< std::endl
;
315 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
318 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
320 // sucess in downloading the index
322 FinalFile
+= string(".IndexDiff");
324 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
326 Rename(DestFile
,FinalFile
);
327 chmod(FinalFile
.c_str(),0644);
328 DestFile
= FinalFile
;
330 if(!ParseDiffIndex(DestFile
))
331 return Failed("", NULL
);
341 // AcqIndexDiffs::AcqIndexDiffs - Constructor
342 // ---------------------------------------------------------------------
343 /* The package diff is added to the queue. one object is constructed
344 * for each diff and the index
346 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire
*Owner
,
347 string URI
,string URIDesc
,string ShortDesc
,
348 string ExpectedMD5
, vector
<DiffInfo
> diffs
)
349 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
),
350 available_patches(diffs
)
353 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
354 DestFile
+= URItoFileName(URI
);
356 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
358 Desc
.Description
= URIDesc
;
360 Desc
.ShortDesc
= ShortDesc
;
362 if(available_patches
.size() == 0)
364 // we are done (yeah!)
370 State
= StateFetchDiff
;
376 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
379 std::clog
<< "pkgAcqIndexDiffs failed: " << Desc
.URI
<< std::endl
380 << "Falling back to normal index file aquire" << std::endl
;
381 new pkgAcqIndex(Owner
, RealURI
, Description
,Desc
.ShortDesc
,
387 // helper that cleans the item out of the fetcher queue
388 void pkgAcqIndexDiffs::Finish(bool allDone
)
390 // we restore the original name, this is required, otherwise
391 // the file will be cleaned
394 DestFile
= _config
->FindDir("Dir::State::lists");
395 DestFile
+= URItoFileName(RealURI
);
397 // do the final md5sum checking
399 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
400 sum
.AddFD(Fd
.Fd(), Fd
.Size());
402 string MD5
= (string
)sum
.Result();
404 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
406 Status
= StatAuthError
;
407 ErrorText
= _("MD5Sum mismatch");
408 Rename(DestFile
,DestFile
+ ".FAILED");
413 // this is for the "real" finish
418 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
423 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
432 bool pkgAcqIndexDiffs::QueueNextDiff()
435 // calc sha1 of the just patched file
436 string FinalFile
= _config
->FindDir("Dir::State::lists");
437 FinalFile
+= URItoFileName(RealURI
);
439 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
441 SHA1
.AddFD(fd
.Fd(), fd
.Size());
442 string local_sha1
= string(SHA1
.Result());
444 std::clog
<< "QueueNextDiff: "
445 << FinalFile
<< " (" << local_sha1
<< ")"<<std::endl
;
447 // remove all patches until the next matching patch is found
448 // this requires the Index file to be ordered
449 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
450 available_patches
.size() > 0 &&
451 I
!= available_patches
.end() &&
452 (*I
).sha1
!= local_sha1
;
455 available_patches
.erase(I
);
458 // error checking and falling back if no patch was found
459 if(available_patches
.size() == 0)
465 // queue the right diff
466 Desc
.URI
= string(RealURI
) + ".diff/" + available_patches
[0].file
+ ".gz";
467 Desc
.Description
= available_patches
[0].file
+ string(".pdiff");
469 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
470 DestFile
+= URItoFileName(RealURI
+ ".diff/" + available_patches
[0].file
);
473 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
482 void pkgAcqIndexDiffs::Done(string Message
,unsigned long Size
,string Md5Hash
,
483 pkgAcquire::MethodConfig
*Cnf
)
486 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
488 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
491 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
493 // sucess in downloading a diff, enter ApplyDiff state
494 if(State
== StateFetchDiff
)
498 std::clog
<< "Sending to gzip method: " << FinalFile
<< std::endl
;
500 string FileName
= LookupTag(Message
,"Filename");
501 State
= StateUnzipDiff
;
502 Desc
.URI
= "gzip:" + FileName
;
503 DestFile
+= ".decomp";
509 // sucess in downloading a diff, enter ApplyDiff state
510 if(State
== StateUnzipDiff
)
513 // rred excepts the patch as $FinalFile.ed
514 Rename(DestFile
,FinalFile
+".ed");
517 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
519 State
= StateApplyDiff
;
520 Desc
.URI
= "rred:" + FinalFile
;
527 // success in download/apply a diff, queue next (if needed)
528 if(State
== StateApplyDiff
)
530 // remove the just applied patch
531 available_patches
.erase(available_patches
.begin());
536 std::clog
<< "Moving patched file in place: " << std::endl
537 << DestFile
<< " -> " << FinalFile
<< std::endl
;
539 Rename(DestFile
,FinalFile
);
541 // see if there is more to download
542 if(available_patches
.size() > 0) {
543 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
544 ExpectedMD5
, available_patches
);
552 // AcqIndex::AcqIndex - Constructor /*{{{*/
553 // ---------------------------------------------------------------------
554 /* The package file is added to the queue and a second class is
555 instantiated to fetch the revision file */
556 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
557 string URI
,string URIDesc
,string ShortDesc
,
558 string ExpectedMD5
, string comprExt
)
559 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
)
561 Decompression
= false;
564 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
565 DestFile
+= URItoFileName(URI
);
569 // autoselect the compression method
570 if(FileExists("/usr/bin/bzip2"))
571 CompressionExtension
= ".bz2";
573 CompressionExtension
= ".gz";
575 CompressionExtension
= comprExt
;
577 Desc
.URI
= URI
+ CompressionExtension
;
579 Desc
.Description
= URIDesc
;
581 Desc
.ShortDesc
= ShortDesc
;
586 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
587 // ---------------------------------------------------------------------
588 /* The only header we use is the last-modified header. */
589 string
pkgAcqIndex::Custom600Headers()
591 string Final
= _config
->FindDir("Dir::State::lists");
592 Final
+= URItoFileName(RealURI
);
595 if (stat(Final
.c_str(),&Buf
) != 0)
596 return "\nIndex-File: true";
598 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
602 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
604 // no .bz2 found, retry with .gz
605 if(Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1) == "bz2") {
606 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
608 // retry with a gzip one
609 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
610 ExpectedMD5
, string(".gz"));
618 Item::Failed(Message
,Cnf
);
622 // AcqIndex::Done - Finished a fetch /*{{{*/
623 // ---------------------------------------------------------------------
624 /* This goes through a number of states.. On the initial fetch the
625 method could possibly return an alternate filename which points
626 to the uncompressed version of the file. If this is so the file
627 is copied into the partial directory. In all other cases the file
628 is decompressed with a gzip uri. */
629 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
630 pkgAcquire::MethodConfig
*Cfg
)
632 Item::Done(Message
,Size
,MD5
,Cfg
);
634 if (Decompression
== true)
636 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
638 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
639 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
645 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
646 sum
.AddFD(Fd
.Fd(), Fd
.Size());
648 MD5
= (string
)sum
.Result();
651 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
653 Status
= StatAuthError
;
654 ErrorText
= _("MD5Sum mismatch");
655 Rename(DestFile
,DestFile
+ ".FAILED");
658 // Done, move it into position
659 string FinalFile
= _config
->FindDir("Dir::State::lists");
660 FinalFile
+= URItoFileName(RealURI
);
661 Rename(DestFile
,FinalFile
);
662 chmod(FinalFile
.c_str(),0644);
664 /* We restore the original name to DestFile so that the clean operation
666 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
667 DestFile
+= URItoFileName(RealURI
);
669 // Remove the compressed version.
671 unlink(DestFile
.c_str());
678 // Handle the unzipd case
679 string FileName
= LookupTag(Message
,"Alt-Filename");
680 if (FileName
.empty() == false)
682 // The files timestamp matches
683 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
686 Decompression
= true;
688 DestFile
+= ".decomp";
689 Desc
.URI
= "copy:" + FileName
;
695 FileName
= LookupTag(Message
,"Filename");
696 if (FileName
.empty() == true)
699 ErrorText
= "Method gave a blank filename";
702 // The files timestamp matches
703 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
706 if (FileName
== DestFile
)
711 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1);
714 decompProg
= "bzip2";
715 else if(compExt
== ".gz")
718 _error
->Error("Unsupported extension: %s", compExt
.c_str());
722 Decompression
= true;
723 DestFile
+= ".decomp";
724 Desc
.URI
= string(decompProg
) + ":" + FileName
;
729 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
730 string URI
,string URIDesc
,string ShortDesc
,
731 string MetaIndexURI
, string MetaIndexURIDesc
,
732 string MetaIndexShortDesc
,
733 const vector
<IndexTarget
*>* IndexTargets
,
734 indexRecords
* MetaIndexParser
) :
735 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
736 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
)
738 this->MetaIndexParser
= MetaIndexParser
;
739 this->IndexTargets
= IndexTargets
;
740 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
741 DestFile
+= URItoFileName(URI
);
743 // remove any partial downloaded sig-file. it may confuse proxies
744 // and is too small to warrant a partial download anyway
745 unlink(DestFile
.c_str());
748 Desc
.Description
= URIDesc
;
750 Desc
.ShortDesc
= ShortDesc
;
754 string Final
= _config
->FindDir("Dir::State::lists");
755 Final
+= URItoFileName(RealURI
);
757 if (stat(Final
.c_str(),&Buf
) == 0)
759 // File was already in place. It needs to be re-verified
760 // because Release might have changed, so Move it into partial
761 Rename(Final
,DestFile
);
762 // unlink the file and do not try to use I-M-S and Last-Modified
763 // if the users proxy is broken
764 if(_config
->FindB("Acquire::BrokenProxy", false) == true) {
765 std::cerr
<< "forcing re-get of the signature file as requested" << std::endl
;
766 unlink(DestFile
.c_str());
773 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
774 // ---------------------------------------------------------------------
775 /* The only header we use is the last-modified header. */
776 string
pkgAcqMetaSig::Custom600Headers()
779 if (stat(DestFile
.c_str(),&Buf
) != 0)
780 return "\nIndex-File: true";
782 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
785 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
786 pkgAcquire::MethodConfig
*Cfg
)
788 Item::Done(Message
,Size
,MD5
,Cfg
);
790 string FileName
= LookupTag(Message
,"Filename");
791 if (FileName
.empty() == true)
794 ErrorText
= "Method gave a blank filename";
798 if (FileName
!= DestFile
)
800 // We have to copy it into place
802 Desc
.URI
= "copy:" + FileName
;
809 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
810 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
811 DestFile
, IndexTargets
, MetaIndexParser
);
815 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
817 // Delete any existing sigfile, so that this source isn't
818 // mistakenly trusted
819 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
820 unlink(Final
.c_str());
822 // if we get a timeout if fail
823 if(LookupTag(Message
,"FailReason") == "Timeout" ||
824 LookupTag(Message
,"FailReason") == "TmpResolveFailure") {
825 Item::Failed(Message
,Cnf
);
829 // queue a pkgAcqMetaIndex with no sigfile
830 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
831 "", IndexTargets
, MetaIndexParser
);
833 if (Cnf
->LocalOnly
== true ||
834 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
843 Item::Failed(Message
,Cnf
);
846 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
847 string URI
,string URIDesc
,string ShortDesc
,
849 const vector
<struct IndexTarget
*>* IndexTargets
,
850 indexRecords
* MetaIndexParser
) :
851 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
853 this->AuthPass
= false;
854 this->MetaIndexParser
= MetaIndexParser
;
855 this->IndexTargets
= IndexTargets
;
856 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
857 DestFile
+= URItoFileName(URI
);
860 Desc
.Description
= URIDesc
;
862 Desc
.ShortDesc
= ShortDesc
;
869 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
870 // ---------------------------------------------------------------------
871 /* The only header we use is the last-modified header. */
872 string
pkgAcqMetaIndex::Custom600Headers()
874 string Final
= _config
->FindDir("Dir::State::lists");
875 Final
+= URItoFileName(RealURI
);
878 if (stat(Final
.c_str(),&Buf
) != 0)
879 return "\nIndex-File: true";
881 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
884 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
885 pkgAcquire::MethodConfig
*Cfg
)
887 Item::Done(Message
,Size
,MD5
,Cfg
);
889 // MetaIndexes are done in two passes: one to download the
890 // metaindex with an appropriate method, and a second to verify it
891 // with the gpgv method
893 if (AuthPass
== true)
899 RetrievalDone(Message
);
901 // Still more retrieving to do
906 // There was no signature file, so we are finished. Download
907 // the indexes without verification.
912 // There was a signature file, so pass it to gpgv for
915 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
916 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
917 << SigFile
<< "," << DestFile
<< ")\n";
919 Desc
.URI
= "gpgv:" + SigFile
;
926 void pkgAcqMetaIndex::RetrievalDone(string Message
)
928 // We have just finished downloading a Release file (it is not
931 string FileName
= LookupTag(Message
,"Filename");
932 if (FileName
.empty() == true)
935 ErrorText
= "Method gave a blank filename";
939 if (FileName
!= DestFile
)
942 Desc
.URI
= "copy:" + FileName
;
949 string FinalFile
= _config
->FindDir("Dir::State::lists");
950 FinalFile
+= URItoFileName(RealURI
);
952 // The files timestamp matches
953 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
955 // Move it into position
956 Rename(DestFile
,FinalFile
);
958 DestFile
= FinalFile
;
961 void pkgAcqMetaIndex::AuthDone(string Message
)
963 // At this point, the gpgv method has succeeded, so there is a
964 // valid signature from a key in the trusted keyring. We
965 // perform additional verification of its contents, and use them
966 // to verify the indexes we are about to download
968 if (!MetaIndexParser
->Load(DestFile
))
970 Status
= StatAuthError
;
971 ErrorText
= MetaIndexParser
->ErrorText
;
980 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
981 std::cerr
<< "Signature verification succeeded: "
982 << DestFile
<< std::endl
;
984 // Download further indexes with verification
987 // Done, move signature file into position
989 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
990 URItoFileName(RealURI
) + ".gpg";
991 Rename(SigFile
,VerifiedSigFile
);
992 chmod(VerifiedSigFile
.c_str(),0644);
995 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
997 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
998 Target
!= IndexTargets
->end();
1001 string ExpectedIndexMD5
;
1004 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
1007 Status
= StatAuthError
;
1008 ErrorText
= "Unable to find expected entry "
1009 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
1012 ExpectedIndexMD5
= Record
->MD5Hash
;
1013 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1015 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
1016 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
1018 if (ExpectedIndexMD5
.empty())
1020 Status
= StatAuthError
;
1021 ErrorText
= "Unable to find MD5 sum for "
1022 + (*Target
)->MetaKey
+ " in Meta-index file";
1027 // Queue Packages file (either diff or full packages files, depending
1028 // on the users option)
1029 if(_config
->FindB("Acquire::PDiffs",false) == false)
1030 new pkgAcqDiffIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1031 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
1033 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1034 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
1038 bool pkgAcqMetaIndex::VerifyVendor()
1040 // // Maybe this should be made available from above so we don't have
1041 // // to read and parse it every time?
1042 // pkgVendorList List;
1043 // List.ReadMainList();
1045 // const Vendor* Vndr = NULL;
1046 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
1048 // string::size_type pos = (*I).find("VALIDSIG ");
1049 // if (_config->FindB("Debug::Vendor", false))
1050 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
1052 // if (pos != std::string::npos)
1054 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
1055 // if (_config->FindB("Debug::Vendor", false))
1056 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
1058 // Vndr = List.FindVendor(Fingerprint) != "";
1059 // if (Vndr != NULL);
1064 string Transformed
= MetaIndexParser
->GetExpectedDist();
1066 if (Transformed
== "../project/experimental")
1068 Transformed
= "experimental";
1071 string::size_type pos
= Transformed
.rfind('/');
1072 if (pos
!= string::npos
)
1074 Transformed
= Transformed
.substr(0, pos
);
1077 if (Transformed
== ".")
1082 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1084 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1085 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1086 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1089 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1091 // This might become fatal one day
1092 // Status = StatAuthError;
1093 // ErrorText = "Conflicting distribution; expected "
1094 // + MetaIndexParser->GetExpectedDist() + " but got "
1095 // + MetaIndexParser->GetDist();
1097 if (!Transformed
.empty())
1099 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
1100 Desc
.Description
.c_str(),
1101 Transformed
.c_str(),
1102 MetaIndexParser
->GetDist().c_str());
1109 // pkgAcqMetaIndex::Failed - no Release file present or no signature
1110 // file present /*{{{*/
1111 // ---------------------------------------------------------------------
1113 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1115 if (AuthPass
== true)
1117 // gpgv method failed
1118 _error
->Warning("GPG error: %s: %s",
1119 Desc
.Description
.c_str(),
1120 LookupTag(Message
,"Message").c_str());
1123 // No Release file was present, or verification failed, so fall
1124 // back to queueing Packages files without verification
1125 QueueIndexes(false);
1130 // AcqArchive::AcqArchive - Constructor /*{{{*/
1131 // ---------------------------------------------------------------------
1132 /* This just sets up the initial fetch environment and queues the first
1134 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
1135 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
1136 string
&StoreFilename
) :
1137 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
1138 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
1141 Retries
= _config
->FindI("Acquire::Retries",0);
1143 if (Version
.Arch() == 0)
1145 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1146 "This might mean you need to manually fix this package. "
1147 "(due to missing arch)"),
1148 Version
.ParentPkg().Name());
1152 /* We need to find a filename to determine the extension. We make the
1153 assumption here that all the available sources for this version share
1154 the same extension.. */
1155 // Skip not source sources, they do not have file fields.
1156 for (; Vf
.end() == false; Vf
++)
1158 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1163 // Does not really matter here.. we are going to fail out below
1164 if (Vf
.end() != true)
1166 // If this fails to get a file name we will bomb out below.
1167 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1168 if (_error
->PendingError() == true)
1171 // Generate the final file name as: package_version_arch.foo
1172 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1173 QuoteString(Version
.VerStr(),"_:") + '_' +
1174 QuoteString(Version
.Arch(),"_:.") +
1175 "." + flExtension(Parse
.FileName());
1178 // check if we have one trusted source for the package. if so, switch
1179 // to "TrustedOnly" mode
1180 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
1182 pkgIndexFile
*Index
;
1183 if (Sources
->FindIndex(i
.File(),Index
) == false)
1185 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1187 std::cerr
<< "Checking index: " << Index
->Describe()
1188 << "(Trusted=" << Index
->IsTrusted() << ")\n";
1190 if (Index
->IsTrusted()) {
1197 if (QueueNext() == false && _error
->PendingError() == false)
1198 _error
->Error(_("I wasn't able to locate file for the %s package. "
1199 "This might mean you need to manually fix this package."),
1200 Version
.ParentPkg().Name());
1203 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1204 // ---------------------------------------------------------------------
1205 /* This queues the next available file version for download. It checks if
1206 the archive is already available in the cache and stashs the MD5 for
1208 bool pkgAcqArchive::QueueNext()
1210 for (; Vf
.end() == false; Vf
++)
1212 // Ignore not source sources
1213 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1216 // Try to cross match against the source list
1217 pkgIndexFile
*Index
;
1218 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1221 // only try to get a trusted package from another source if that source
1223 if(Trusted
&& !Index
->IsTrusted())
1226 // Grab the text package record
1227 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1228 if (_error
->PendingError() == true)
1231 string PkgFile
= Parse
.FileName();
1232 MD5
= Parse
.MD5Hash();
1233 if (PkgFile
.empty() == true)
1234 return _error
->Error(_("The package index files are corrupted. No Filename: "
1235 "field for package %s."),
1236 Version
.ParentPkg().Name());
1238 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1239 Desc
.Description
= Index
->ArchiveInfo(Version
);
1241 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1243 // See if we already have the file. (Legacy filenames)
1244 FileSize
= Version
->Size
;
1245 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1247 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1249 // Make sure the size matches
1250 if ((unsigned)Buf
.st_size
== Version
->Size
)
1255 StoreFilename
= DestFile
= FinalFile
;
1259 /* Hmm, we have a file and its size does not match, this means it is
1260 an old style mismatched arch */
1261 unlink(FinalFile
.c_str());
1264 // Check it again using the new style output filenames
1265 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1266 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1268 // Make sure the size matches
1269 if ((unsigned)Buf
.st_size
== Version
->Size
)
1274 StoreFilename
= DestFile
= FinalFile
;
1278 /* Hmm, we have a file and its size does not match, this shouldnt
1280 unlink(FinalFile
.c_str());
1283 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1285 // Check the destination file
1286 if (stat(DestFile
.c_str(),&Buf
) == 0)
1288 // Hmm, the partial file is too big, erase it
1289 if ((unsigned)Buf
.st_size
> Version
->Size
)
1290 unlink(DestFile
.c_str());
1292 PartialSize
= Buf
.st_size
;
1297 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1298 Desc
.Description
= Index
->ArchiveInfo(Version
);
1300 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1309 // AcqArchive::Done - Finished fetching /*{{{*/
1310 // ---------------------------------------------------------------------
1312 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
1313 pkgAcquire::MethodConfig
*Cfg
)
1315 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
1318 if (Size
!= Version
->Size
)
1321 ErrorText
= _("Size mismatch");
1326 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1331 ErrorText
= _("MD5Sum mismatch");
1332 if(FileExists(DestFile
))
1333 Rename(DestFile
,DestFile
+ ".FAILED");
1338 // Grab the output filename
1339 string FileName
= LookupTag(Message
,"Filename");
1340 if (FileName
.empty() == true)
1343 ErrorText
= "Method gave a blank filename";
1349 // Reference filename
1350 if (FileName
!= DestFile
)
1352 StoreFilename
= DestFile
= FileName
;
1357 // Done, move it into position
1358 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1359 FinalFile
+= flNotDir(StoreFilename
);
1360 Rename(DestFile
,FinalFile
);
1362 StoreFilename
= DestFile
= FinalFile
;
1366 // AcqArchive::Failed - Failure handler /*{{{*/
1367 // ---------------------------------------------------------------------
1368 /* Here we try other sources */
1369 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1371 ErrorText
= LookupTag(Message
,"Message");
1373 /* We don't really want to retry on failed media swaps, this prevents
1374 that. An interesting observation is that permanent failures are not
1376 if (Cnf
->Removable
== true &&
1377 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1379 // Vf = Version.FileList();
1380 while (Vf
.end() == false) Vf
++;
1381 StoreFilename
= string();
1382 Item::Failed(Message
,Cnf
);
1386 if (QueueNext() == false)
1388 // This is the retry counter
1390 Cnf
->LocalOnly
== false &&
1391 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1394 Vf
= Version
.FileList();
1395 if (QueueNext() == true)
1399 StoreFilename
= string();
1400 Item::Failed(Message
,Cnf
);
1404 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1405 // trusted source /*{{{*/
1406 // ---------------------------------------------------------------------
1407 bool pkgAcqArchive::IsTrusted()
1412 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1413 // ---------------------------------------------------------------------
1415 void pkgAcqArchive::Finished()
1417 if (Status
== pkgAcquire::Item::StatDone
&&
1420 StoreFilename
= string();
1424 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1425 // ---------------------------------------------------------------------
1426 /* The file is added to the queue */
1427 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1428 unsigned long Size
,string Dsc
,string ShortDesc
) :
1429 Item(Owner
), Md5Hash(MD5
)
1431 Retries
= _config
->FindI("Acquire::Retries",0);
1433 DestFile
= flNotDir(URI
);
1437 Desc
.Description
= Dsc
;
1440 // Set the short description to the archive component
1441 Desc
.ShortDesc
= ShortDesc
;
1443 // Get the transfer sizes
1446 if (stat(DestFile
.c_str(),&Buf
) == 0)
1448 // Hmm, the partial file is too big, erase it
1449 if ((unsigned)Buf
.st_size
> Size
)
1450 unlink(DestFile
.c_str());
1452 PartialSize
= Buf
.st_size
;
1458 // AcqFile::Done - Item downloaded OK /*{{{*/
1459 // ---------------------------------------------------------------------
1461 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1462 pkgAcquire::MethodConfig
*Cnf
)
1465 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1470 ErrorText
= "MD5Sum mismatch";
1471 Rename(DestFile
,DestFile
+ ".FAILED");
1476 Item::Done(Message
,Size
,MD5
,Cnf
);
1478 string FileName
= LookupTag(Message
,"Filename");
1479 if (FileName
.empty() == true)
1482 ErrorText
= "Method gave a blank filename";
1488 // The files timestamp matches
1489 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1492 // We have to copy it into place
1493 if (FileName
!= DestFile
)
1496 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1497 Cnf
->Removable
== true)
1499 Desc
.URI
= "copy:" + FileName
;
1504 // Erase the file if it is a symlink so we can overwrite it
1506 if (lstat(DestFile
.c_str(),&St
) == 0)
1508 if (S_ISLNK(St
.st_mode
) != 0)
1509 unlink(DestFile
.c_str());
1513 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1515 ErrorText
= "Link to " + DestFile
+ " failure ";
1522 // AcqFile::Failed - Failure handler /*{{{*/
1523 // ---------------------------------------------------------------------
1524 /* Here we try other sources */
1525 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1527 ErrorText
= LookupTag(Message
,"Message");
1529 // This is the retry counter
1531 Cnf
->LocalOnly
== false &&
1532 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1539 Item::Failed(Message
,Cnf
);