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 /*{{{*/
16 #include <apt-pkg/acquire-item.h>
17 #include <apt-pkg/configuration.h>
18 #include <apt-pkg/sourcelist.h>
19 #include <apt-pkg/vendorlist.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/strutl.h>
22 #include <apt-pkg/fileutl.h>
23 #include <apt-pkg/md5.h>
24 #include <apt-pkg/sha1.h>
25 #include <apt-pkg/tagfile.h>
39 // Acquire::Item::Item - Constructor /*{{{*/
40 // ---------------------------------------------------------------------
42 pkgAcquire::Item::Item(pkgAcquire
*Owner
) : Owner(Owner
), FileSize(0),
43 PartialSize(0), Mode(0), ID(0), Complete(false),
44 Local(false), QueueCounter(0)
50 // Acquire::Item::~Item - Destructor /*{{{*/
51 // ---------------------------------------------------------------------
53 pkgAcquire::Item::~Item()
58 // Acquire::Item::Failed - Item failed to download /*{{{*/
59 // ---------------------------------------------------------------------
60 /* We return to an idle state if there are still other queues that could
62 void pkgAcquire::Item::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
65 ErrorText
= LookupTag(Message
,"Message");
66 if (QueueCounter
<= 1)
68 /* This indicates that the file is not available right now but might
69 be sometime later. If we do a retry cycle then this should be
71 if (Cnf
->LocalOnly
== true &&
72 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
84 // Acquire::Item::Start - Item has begun to download /*{{{*/
85 // ---------------------------------------------------------------------
86 /* Stash status and the file size. Note that setting Complete means
87 sub-phases of the acquire process such as decompresion are operating */
88 void pkgAcquire::Item::Start(string
/*Message*/,unsigned long Size
)
90 Status
= StatFetching
;
91 if (FileSize
== 0 && Complete
== false)
95 // Acquire::Item::Done - Item downloaded OK /*{{{*/
96 // ---------------------------------------------------------------------
98 void pkgAcquire::Item::Done(string Message
,unsigned long Size
,string
,
99 pkgAcquire::MethodConfig
*Cnf
)
101 // We just downloaded something..
102 string FileName
= LookupTag(Message
,"Filename");
103 // we only inform the Log class if it was actually not a local thing
104 if (Complete
== false && !Local
&& FileName
== DestFile
)
107 Owner
->Log
->Fetched(Size
,atoi(LookupTag(Message
,"Resume-Point","0").c_str()));
114 ErrorText
= string();
115 Owner
->Dequeue(this);
118 // Acquire::Item::Rename - Rename a file /*{{{*/
119 // ---------------------------------------------------------------------
120 /* This helper function is used by alot of item methods as thier final
122 void pkgAcquire::Item::Rename(string From
,string To
)
124 if (rename(From
.c_str(),To
.c_str()) != 0)
127 snprintf(S
,sizeof(S
),_("rename failed, %s (%s -> %s)."),strerror(errno
),
128 From
.c_str(),To
.c_str());
136 // AcqDiffIndex::AcqDiffIndex - Constructor
137 // ---------------------------------------------------------------------
138 /* Get the DiffIndex file first and see if there are patches availabe
139 * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
140 * patches. If anything goes wrong in that process, it will fall back to
141 * the original packages file
143 pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire
*Owner
,
144 string URI
,string URIDesc
,string ShortDesc
,
146 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
), Description(URIDesc
)
149 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
151 Desc
.Description
= URIDesc
+ "/DiffIndex";
153 Desc
.ShortDesc
= ShortDesc
;
154 Desc
.URI
= URI
+ ".diff/Index";
156 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
157 DestFile
+= URItoFileName(URI
) + string(".DiffIndex");
160 std::clog
<< "pkgAcqDiffIndex: " << Desc
.URI
<< std::endl
;
162 // look for the current package file
163 CurrentPackagesFile
= _config
->FindDir("Dir::State::lists");
164 CurrentPackagesFile
+= URItoFileName(RealURI
);
166 // FIXME: this file:/ check is a hack to prevent fetching
167 // from local sources. this is really silly, and
168 // should be fixed cleanly as soon as possible
169 if(!FileExists(CurrentPackagesFile
) ||
170 Desc
.URI
.substr(0,strlen("file:/")) == "file:/")
172 // we don't have a pkg file or we don't want to queue
174 std::clog
<< "No index file, local or canceld by user" << std::endl
;
180 std::clog
<< "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
181 << CurrentPackagesFile
<< std::endl
;
187 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
188 // ---------------------------------------------------------------------
189 /* The only header we use is the last-modified header. */
190 string
pkgAcqDiffIndex::Custom600Headers()
192 string Final
= _config
->FindDir("Dir::State::lists");
193 Final
+= URItoFileName(RealURI
) + string(".IndexDiff");
196 std::clog
<< "Custom600Header-IMS: " << Final
<< std::endl
;
199 if (stat(Final
.c_str(),&Buf
) != 0)
200 return "\nIndex-File: true";
202 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
206 bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile
)
209 std::clog
<< "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
214 vector
<DiffInfo
> available_patches
;
216 FileFd
Fd(IndexDiffFile
,FileFd::ReadOnly
);
218 if (_error
->PendingError() == true)
221 if(TF
.Step(Tags
) == true)
228 string tmp
= Tags
.FindS("SHA1-Current");
229 std::stringstream
ss(tmp
);
232 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnly
);
234 SHA1
.AddFD(fd
.Fd(), fd
.Size());
235 local_sha1
= string(SHA1
.Result());
237 if(local_sha1
== ServerSha1
)
239 // we have the same sha1 as the server
241 std::clog
<< "Package file is up-to-date" << std::endl
;
242 // set found to true, this will queue a pkgAcqIndexDiffs with
243 // a empty availabe_patches
249 std::clog
<< "SHA1-Current: " << ServerSha1
<< std::endl
;
251 // check the historie and see what patches we need
252 string history
= Tags
.FindS("SHA1-History");
253 std::stringstream
hist(history
);
254 while(hist
>> d
.sha1
>> size
>> d
.file
)
256 d
.size
= atoi(size
.c_str());
257 // read until the first match is found
258 if(d
.sha1
== local_sha1
)
260 // from that point on, we probably need all diffs
264 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
265 available_patches
.push_back(d
);
270 // we have something, queue the next diff
274 int last_space
= Description
.rfind(" ");
275 if(last_space
!= string::npos
)
276 Description
.erase(last_space
, Description
.size()-last_space
);
277 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
278 ExpectedMD5
, available_patches
);
286 // Nothing found, report and return false
287 // Failing here is ok, if we return false later, the full
288 // IndexFile is queued
290 std::clog
<< "Can't find a patch in the index file" << std::endl
;
294 void pkgAcqDiffIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
297 std::clog
<< "pkgAcqDiffIndex failed: " << Desc
.URI
<< std::endl
298 << "Falling back to normal index file aquire" << std::endl
;
300 new pkgAcqIndex(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
308 void pkgAcqDiffIndex::Done(string Message
,unsigned long Size
,string Md5Hash
,
309 pkgAcquire::MethodConfig
*Cnf
)
312 std::clog
<< "pkgAcqDiffIndex::Done(): " << Desc
.URI
<< std::endl
;
314 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
317 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
319 // sucess in downloading the index
321 FinalFile
+= string(".IndexDiff");
323 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
325 Rename(DestFile
,FinalFile
);
326 chmod(FinalFile
.c_str(),0644);
327 DestFile
= FinalFile
;
329 if(!ParseDiffIndex(DestFile
))
330 return Failed("", NULL
);
340 // AcqIndexDiffs::AcqIndexDiffs - Constructor
341 // ---------------------------------------------------------------------
342 /* The package diff is added to the queue. one object is constructed
343 * for each diff and the index
345 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire
*Owner
,
346 string URI
,string URIDesc
,string ShortDesc
,
347 string ExpectedMD5
, vector
<DiffInfo
> diffs
)
348 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
),
349 available_patches(diffs
)
352 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
353 DestFile
+= URItoFileName(URI
);
355 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
357 Description
= URIDesc
;
359 Desc
.ShortDesc
= ShortDesc
;
361 if(available_patches
.size() == 0)
363 // we are done (yeah!)
369 State
= StateFetchDiff
;
375 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
378 std::clog
<< "pkgAcqIndexDiffs failed: " << Desc
.URI
<< std::endl
379 << "Falling back to normal index file aquire" << std::endl
;
380 new pkgAcqIndex(Owner
, RealURI
, Description
,Desc
.ShortDesc
,
386 // helper that cleans the item out of the fetcher queue
387 void pkgAcqIndexDiffs::Finish(bool allDone
)
389 // we restore the original name, this is required, otherwise
390 // the file will be cleaned
393 DestFile
= _config
->FindDir("Dir::State::lists");
394 DestFile
+= URItoFileName(RealURI
);
396 // do the final md5sum checking
398 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
399 sum
.AddFD(Fd
.Fd(), Fd
.Size());
401 string MD5
= (string
)sum
.Result();
403 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
405 Status
= StatAuthError
;
406 ErrorText
= _("MD5Sum mismatch");
407 Rename(DestFile
,DestFile
+ ".FAILED");
412 // this is for the "real" finish
417 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
422 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
431 bool pkgAcqIndexDiffs::QueueNextDiff()
434 // calc sha1 of the just patched file
435 string FinalFile
= _config
->FindDir("Dir::State::lists");
436 FinalFile
+= URItoFileName(RealURI
);
438 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
440 SHA1
.AddFD(fd
.Fd(), fd
.Size());
441 string local_sha1
= string(SHA1
.Result());
443 std::clog
<< "QueueNextDiff: "
444 << FinalFile
<< " (" << local_sha1
<< ")"<<std::endl
;
446 // remove all patches until the next matching patch is found
447 // this requires the Index file to be ordered
448 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
449 available_patches
.size() > 0 &&
450 I
!= available_patches
.end() &&
451 (*I
).sha1
!= local_sha1
;
454 available_patches
.erase(I
);
457 // error checking and falling back if no patch was found
458 if(available_patches
.size() == 0)
464 // queue the right diff
465 Desc
.URI
= string(RealURI
) + ".diff/" + available_patches
[0].file
+ ".gz";
466 Desc
.Description
= Description
+ " " + available_patches
[0].file
+ string(".pdiff");
467 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
468 DestFile
+= URItoFileName(RealURI
+ ".diff/" + available_patches
[0].file
);
471 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
480 void pkgAcqIndexDiffs::Done(string Message
,unsigned long Size
,string Md5Hash
,
481 pkgAcquire::MethodConfig
*Cnf
)
484 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
486 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
489 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
491 // sucess in downloading a diff, enter ApplyDiff state
492 if(State
== StateFetchDiff
)
496 std::clog
<< "Sending to gzip method: " << FinalFile
<< std::endl
;
498 string FileName
= LookupTag(Message
,"Filename");
499 State
= StateUnzipDiff
;
501 Desc
.URI
= "gzip:" + FileName
;
502 DestFile
+= ".decomp";
508 // sucess in downloading a diff, enter ApplyDiff state
509 if(State
== StateUnzipDiff
)
512 // rred excepts the patch as $FinalFile.ed
513 Rename(DestFile
,FinalFile
+".ed");
516 std::clog
<< "Sending to rred method: " << FinalFile
<< std::endl
;
518 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
);
540 chmod(FinalFile
.c_str(),0644);
542 // see if there is more to download
543 if(available_patches
.size() > 0) {
544 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
545 ExpectedMD5
, available_patches
);
553 // AcqIndex::AcqIndex - Constructor /*{{{*/
554 // ---------------------------------------------------------------------
555 /* The package file is added to the queue and a second class is
556 instantiated to fetch the revision file */
557 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
558 string URI
,string URIDesc
,string ShortDesc
,
559 string ExpectedMD5
, string comprExt
)
560 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
)
562 Decompression
= false;
565 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
566 DestFile
+= URItoFileName(URI
);
570 // autoselect the compression method
571 if(FileExists("/bin/bzip2"))
572 CompressionExtension
= ".bz2";
574 CompressionExtension
= ".gz";
576 CompressionExtension
= comprExt
;
578 Desc
.URI
= URI
+ CompressionExtension
;
580 Desc
.Description
= URIDesc
;
582 Desc
.ShortDesc
= ShortDesc
;
587 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
588 // ---------------------------------------------------------------------
589 /* The only header we use is the last-modified header. */
590 string
pkgAcqIndex::Custom600Headers()
592 string Final
= _config
->FindDir("Dir::State::lists");
593 Final
+= URItoFileName(RealURI
);
596 if (stat(Final
.c_str(),&Buf
) != 0)
597 return "\nIndex-File: true";
599 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
603 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
605 // no .bz2 found, retry with .gz
606 if(Desc
.URI
.substr(Desc
.URI
.size()-3) == "bz2") {
607 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
609 // retry with a gzip one
610 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
611 ExpectedMD5
, string(".gz"));
618 // on decompression failure, remove bad versions in partial/
619 if(Decompression
&& Erase
) {
620 string s
= _config
->FindDir("Dir::State::lists") + "partial/";
621 s
+= URItoFileName(RealURI
);
625 Item::Failed(Message
,Cnf
);
629 // AcqIndex::Done - Finished a fetch /*{{{*/
630 // ---------------------------------------------------------------------
631 /* This goes through a number of states.. On the initial fetch the
632 method could possibly return an alternate filename which points
633 to the uncompressed version of the file. If this is so the file
634 is copied into the partial directory. In all other cases the file
635 is decompressed with a gzip uri. */
636 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
637 pkgAcquire::MethodConfig
*Cfg
)
639 Item::Done(Message
,Size
,MD5
,Cfg
);
641 if (Decompression
== true)
643 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
645 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
646 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
652 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
653 sum
.AddFD(Fd
.Fd(), Fd
.Size());
655 MD5
= (string
)sum
.Result();
658 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
660 Status
= StatAuthError
;
661 ErrorText
= _("MD5Sum mismatch");
662 Rename(DestFile
,DestFile
+ ".FAILED");
665 // Done, move it into position
666 string FinalFile
= _config
->FindDir("Dir::State::lists");
667 FinalFile
+= URItoFileName(RealURI
);
668 Rename(DestFile
,FinalFile
);
669 chmod(FinalFile
.c_str(),0644);
671 /* We restore the original name to DestFile so that the clean operation
673 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
674 DestFile
+= URItoFileName(RealURI
);
676 // Remove the compressed version.
678 unlink(DestFile
.c_str());
685 // Handle the unzipd case
686 string FileName
= LookupTag(Message
,"Alt-Filename");
687 if (FileName
.empty() == false)
689 // The files timestamp matches
690 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
693 Decompression
= true;
695 DestFile
+= ".decomp";
696 Desc
.URI
= "copy:" + FileName
;
702 FileName
= LookupTag(Message
,"Filename");
703 if (FileName
.empty() == true)
706 ErrorText
= "Method gave a blank filename";
709 // The files timestamp matches
710 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
713 if (FileName
== DestFile
)
718 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3);
721 decompProg
= "bzip2";
722 else if(compExt
== ".gz")
725 _error
->Error("Unsupported extension: %s", compExt
.c_str());
729 Decompression
= true;
730 DestFile
+= ".decomp";
731 Desc
.URI
= string(decompProg
) + ":" + FileName
;
736 // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
737 // ---------------------------------------------------------------------
738 /* The Translation file is added to the queue */
739 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
,
740 string URI
,string URIDesc
,string ShortDesc
) :
741 pkgAcqIndex(Owner
, URI
, URIDesc
, ShortDesc
, "", "")
746 // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
747 // ---------------------------------------------------------------------
749 void pkgAcqIndexTrans::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
751 if (Cnf
->LocalOnly
== true ||
752 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
761 Item::Failed(Message
,Cnf
);
765 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
766 string URI
,string URIDesc
,string ShortDesc
,
767 string MetaIndexURI
, string MetaIndexURIDesc
,
768 string MetaIndexShortDesc
,
769 const vector
<IndexTarget
*>* IndexTargets
,
770 indexRecords
* MetaIndexParser
) :
771 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
772 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
773 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
)
775 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
776 DestFile
+= URItoFileName(URI
);
778 // remove any partial downloaded sig-file in partial/.
779 // it may confuse proxies and is too small to warrant a
780 // partial download anyway
781 unlink(DestFile
.c_str());
784 Desc
.Description
= URIDesc
;
786 Desc
.ShortDesc
= ShortDesc
;
790 string Final
= _config
->FindDir("Dir::State::lists");
791 Final
+= URItoFileName(RealURI
);
793 if (stat(Final
.c_str(),&Buf
) == 0)
795 // File was already in place. It needs to be re-verified
796 // because Release might have changed, so Move it into partial
797 Rename(Final
,DestFile
);
803 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
804 // ---------------------------------------------------------------------
805 /* The only header we use is the last-modified header. */
806 string
pkgAcqMetaSig::Custom600Headers()
809 if (stat(DestFile
.c_str(),&Buf
) != 0)
810 return "\nIndex-File: true";
812 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
815 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
816 pkgAcquire::MethodConfig
*Cfg
)
818 Item::Done(Message
,Size
,MD5
,Cfg
);
820 string FileName
= LookupTag(Message
,"Filename");
821 if (FileName
.empty() == true)
824 ErrorText
= "Method gave a blank filename";
828 if (FileName
!= DestFile
)
830 // We have to copy it into place
832 Desc
.URI
= "copy:" + FileName
;
839 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
840 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
841 DestFile
, IndexTargets
, MetaIndexParser
);
845 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
847 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
849 // if we get a network error we fail gracefully
850 if(Status
== StatTransientNetworkError
)
852 Item::Failed(Message
,Cnf
);
853 // move the sigfile back on transient network failures
854 if(FileExists(DestFile
))
855 Rename(DestFile
,Final
);
857 // set the status back to , Item::Failed likes to reset it
858 Status
= pkgAcquire::Item::StatTransientNetworkError
;
862 // Delete any existing sigfile when the acquire failed
863 unlink(Final
.c_str());
865 // queue a pkgAcqMetaIndex with no sigfile
866 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
867 "", IndexTargets
, MetaIndexParser
);
869 if (Cnf
->LocalOnly
== true ||
870 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
879 Item::Failed(Message
,Cnf
);
882 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
883 string URI
,string URIDesc
,string ShortDesc
,
885 const vector
<struct IndexTarget
*>* IndexTargets
,
886 indexRecords
* MetaIndexParser
) :
887 Item(Owner
), RealURI(URI
), SigFile(SigFile
), AuthPass(false),
888 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
), IMSHit(false)
890 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
891 DestFile
+= URItoFileName(URI
);
894 Desc
.Description
= URIDesc
;
896 Desc
.ShortDesc
= ShortDesc
;
903 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
904 // ---------------------------------------------------------------------
905 /* The only header we use is the last-modified header. */
906 string
pkgAcqMetaIndex::Custom600Headers()
908 string Final
= _config
->FindDir("Dir::State::lists");
909 Final
+= URItoFileName(RealURI
);
912 if (stat(Final
.c_str(),&Buf
) != 0)
913 return "\nIndex-File: true";
915 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
918 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
919 pkgAcquire::MethodConfig
*Cfg
)
921 Item::Done(Message
,Size
,MD5
,Cfg
);
923 // MetaIndexes are done in two passes: one to download the
924 // metaindex with an appropriate method, and a second to verify it
925 // with the gpgv method
927 if (AuthPass
== true)
933 RetrievalDone(Message
);
935 // Still more retrieving to do
940 // There was no signature file, so we are finished. Download
941 // the indexes without verification.
946 // There was a signature file, so pass it to gpgv for
949 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
950 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
951 << SigFile
<< "," << DestFile
<< ")\n";
953 Desc
.URI
= "gpgv:" + SigFile
;
960 void pkgAcqMetaIndex::RetrievalDone(string Message
)
962 // We have just finished downloading a Release file (it is not
965 string FileName
= LookupTag(Message
,"Filename");
966 if (FileName
.empty() == true)
969 ErrorText
= "Method gave a blank filename";
973 if (FileName
!= DestFile
)
976 Desc
.URI
= "copy:" + FileName
;
981 // see if the download was a IMSHit
982 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
986 string FinalFile
= _config
->FindDir("Dir::State::lists");
987 FinalFile
+= URItoFileName(RealURI
);
989 // The files timestamp matches
990 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
992 // Move it into position
993 Rename(DestFile
,FinalFile
);
995 chmod(FinalFile
.c_str(),0644);
996 DestFile
= FinalFile
;
999 void pkgAcqMetaIndex::AuthDone(string Message
)
1001 // At this point, the gpgv method has succeeded, so there is a
1002 // valid signature from a key in the trusted keyring. We
1003 // perform additional verification of its contents, and use them
1004 // to verify the indexes we are about to download
1006 if (!MetaIndexParser
->Load(DestFile
))
1008 Status
= StatAuthError
;
1009 ErrorText
= MetaIndexParser
->ErrorText
;
1013 if (!VerifyVendor(Message
))
1018 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1019 std::cerr
<< "Signature verification succeeded: "
1020 << DestFile
<< std::endl
;
1022 // Download further indexes with verification
1025 // Done, move signature file into position
1027 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
1028 URItoFileName(RealURI
) + ".gpg";
1029 Rename(SigFile
,VerifiedSigFile
);
1030 chmod(VerifiedSigFile
.c_str(),0644);
1033 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
1035 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
1036 Target
!= IndexTargets
->end();
1039 string ExpectedIndexMD5
;
1042 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
1045 Status
= StatAuthError
;
1046 ErrorText
= "Unable to find expected entry "
1047 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
1050 ExpectedIndexMD5
= Record
->MD5Hash
;
1051 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1053 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
1054 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
1056 if (ExpectedIndexMD5
.empty())
1058 Status
= StatAuthError
;
1059 ErrorText
= "Unable to find MD5 sum for "
1060 + (*Target
)->MetaKey
+ " in Meta-index file";
1065 // Queue Packages file (either diff or full packages files, depending
1066 // on the users option)
1067 if(_config
->FindB("Acquire::PDiffs",true) == true)
1068 new pkgAcqDiffIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1069 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
1071 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1072 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
1076 bool pkgAcqMetaIndex::VerifyVendor(string Message
)
1078 // // Maybe this should be made available from above so we don't have
1079 // // to read and parse it every time?
1080 // pkgVendorList List;
1081 // List.ReadMainList();
1083 // const Vendor* Vndr = NULL;
1084 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
1086 // string::size_type pos = (*I).find("VALIDSIG ");
1087 // if (_config->FindB("Debug::Vendor", false))
1088 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
1090 // if (pos != std::string::npos)
1092 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
1093 // if (_config->FindB("Debug::Vendor", false))
1094 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
1096 // Vndr = List.FindVendor(Fingerprint) != "";
1097 // if (Vndr != NULL);
1101 string::size_type pos
;
1103 // check for missing sigs (that where not fatal because otherwise we had
1106 string msg
= _("There is no public key available for the "
1107 "following key IDs:\n");
1108 pos
= Message
.find("NO_PUBKEY ");
1109 if (pos
!= std::string::npos
)
1111 string::size_type start
= pos
+strlen("NO_PUBKEY ");
1112 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
1113 missingkeys
+= (Fingerprint
);
1115 if(!missingkeys
.empty())
1116 _error
->Warning("%s", string(msg
+missingkeys
).c_str());
1118 string Transformed
= MetaIndexParser
->GetExpectedDist();
1120 if (Transformed
== "../project/experimental")
1122 Transformed
= "experimental";
1125 pos
= Transformed
.rfind('/');
1126 if (pos
!= string::npos
)
1128 Transformed
= Transformed
.substr(0, pos
);
1131 if (Transformed
== ".")
1136 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1138 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1139 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1140 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1143 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1145 // This might become fatal one day
1146 // Status = StatAuthError;
1147 // ErrorText = "Conflicting distribution; expected "
1148 // + MetaIndexParser->GetExpectedDist() + " but got "
1149 // + MetaIndexParser->GetDist();
1151 if (!Transformed
.empty())
1153 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
1154 Desc
.Description
.c_str(),
1155 Transformed
.c_str(),
1156 MetaIndexParser
->GetDist().c_str());
1163 // pkgAcqMetaIndex::Failed - no Release file present or no signature
1164 // file present /*{{{*/
1165 // ---------------------------------------------------------------------
1167 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1169 if (AuthPass
== true)
1171 // if we fail the authentication but got the file via a IMS-Hit
1172 // this means that the file wasn't downloaded and that it might be
1173 // just stale (server problem, proxy etc). we delete what we have
1174 // queue it again without i-m-s
1175 // alternatively we could just unlink the file and let the user try again
1181 unlink(DestFile
.c_str());
1183 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1184 DestFile
+= URItoFileName(RealURI
);
1190 // gpgv method failed
1191 _error
->Warning("GPG error: %s: %s",
1192 Desc
.Description
.c_str(),
1193 LookupTag(Message
,"Message").c_str());
1197 // No Release file was present, or verification failed, so fall
1198 // back to queueing Packages files without verification
1199 QueueIndexes(false);
1204 // AcqArchive::AcqArchive - Constructor /*{{{*/
1205 // ---------------------------------------------------------------------
1206 /* This just sets up the initial fetch environment and queues the first
1208 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
1209 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
1210 string
&StoreFilename
) :
1211 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
1212 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
1215 Retries
= _config
->FindI("Acquire::Retries",0);
1217 if (Version
.Arch() == 0)
1219 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1220 "This might mean you need to manually fix this package. "
1221 "(due to missing arch)"),
1222 Version
.ParentPkg().Name());
1226 /* We need to find a filename to determine the extension. We make the
1227 assumption here that all the available sources for this version share
1228 the same extension.. */
1229 // Skip not source sources, they do not have file fields.
1230 for (; Vf
.end() == false; Vf
++)
1232 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1237 // Does not really matter here.. we are going to fail out below
1238 if (Vf
.end() != true)
1240 // If this fails to get a file name we will bomb out below.
1241 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1242 if (_error
->PendingError() == true)
1245 // Generate the final file name as: package_version_arch.foo
1246 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1247 QuoteString(Version
.VerStr(),"_:") + '_' +
1248 QuoteString(Version
.Arch(),"_:.") +
1249 "." + flExtension(Parse
.FileName());
1252 // check if we have one trusted source for the package. if so, switch
1253 // to "TrustedOnly" mode
1254 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
1256 pkgIndexFile
*Index
;
1257 if (Sources
->FindIndex(i
.File(),Index
) == false)
1259 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1261 std::cerr
<< "Checking index: " << Index
->Describe()
1262 << "(Trusted=" << Index
->IsTrusted() << ")\n";
1264 if (Index
->IsTrusted()) {
1270 // "allow-unauthenticated" restores apts old fetching behaviour
1271 // that means that e.g. unauthenticated file:// uris are higher
1272 // priority than authenticated http:// uris
1273 if (_config
->FindB("APT::Get::AllowUnauthenticated",false) == true)
1277 if (QueueNext() == false && _error
->PendingError() == false)
1278 _error
->Error(_("I wasn't able to locate file for the %s package. "
1279 "This might mean you need to manually fix this package."),
1280 Version
.ParentPkg().Name());
1283 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1284 // ---------------------------------------------------------------------
1285 /* This queues the next available file version for download. It checks if
1286 the archive is already available in the cache and stashs the MD5 for
1288 bool pkgAcqArchive::QueueNext()
1290 for (; Vf
.end() == false; Vf
++)
1292 // Ignore not source sources
1293 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1296 // Try to cross match against the source list
1297 pkgIndexFile
*Index
;
1298 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1301 // only try to get a trusted package from another source if that source
1303 if(Trusted
&& !Index
->IsTrusted())
1306 // Grab the text package record
1307 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1308 if (_error
->PendingError() == true)
1311 string PkgFile
= Parse
.FileName();
1312 MD5
= Parse
.MD5Hash();
1313 if (PkgFile
.empty() == true)
1314 return _error
->Error(_("The package index files are corrupted. No Filename: "
1315 "field for package %s."),
1316 Version
.ParentPkg().Name());
1318 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1319 Desc
.Description
= Index
->ArchiveInfo(Version
);
1321 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1323 // See if we already have the file. (Legacy filenames)
1324 FileSize
= Version
->Size
;
1325 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1327 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1329 // Make sure the size matches
1330 if ((unsigned)Buf
.st_size
== Version
->Size
)
1335 StoreFilename
= DestFile
= FinalFile
;
1339 /* Hmm, we have a file and its size does not match, this means it is
1340 an old style mismatched arch */
1341 unlink(FinalFile
.c_str());
1344 // Check it again using the new style output filenames
1345 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1346 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1348 // Make sure the size matches
1349 if ((unsigned)Buf
.st_size
== Version
->Size
)
1354 StoreFilename
= DestFile
= FinalFile
;
1358 /* Hmm, we have a file and its size does not match, this shouldnt
1360 unlink(FinalFile
.c_str());
1363 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1365 // Check the destination file
1366 if (stat(DestFile
.c_str(),&Buf
) == 0)
1368 // Hmm, the partial file is too big, erase it
1369 if ((unsigned)Buf
.st_size
> Version
->Size
)
1370 unlink(DestFile
.c_str());
1372 PartialSize
= Buf
.st_size
;
1377 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1378 Desc
.Description
= Index
->ArchiveInfo(Version
);
1380 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1389 // AcqArchive::Done - Finished fetching /*{{{*/
1390 // ---------------------------------------------------------------------
1392 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
1393 pkgAcquire::MethodConfig
*Cfg
)
1395 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
1398 if (Size
!= Version
->Size
)
1401 ErrorText
= _("Size mismatch");
1406 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1411 ErrorText
= _("MD5Sum mismatch");
1412 if(FileExists(DestFile
))
1413 Rename(DestFile
,DestFile
+ ".FAILED");
1418 // Grab the output filename
1419 string FileName
= LookupTag(Message
,"Filename");
1420 if (FileName
.empty() == true)
1423 ErrorText
= "Method gave a blank filename";
1429 // Reference filename
1430 if (FileName
!= DestFile
)
1432 StoreFilename
= DestFile
= FileName
;
1437 // Done, move it into position
1438 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1439 FinalFile
+= flNotDir(StoreFilename
);
1440 Rename(DestFile
,FinalFile
);
1442 StoreFilename
= DestFile
= FinalFile
;
1446 // AcqArchive::Failed - Failure handler /*{{{*/
1447 // ---------------------------------------------------------------------
1448 /* Here we try other sources */
1449 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1451 ErrorText
= LookupTag(Message
,"Message");
1453 /* We don't really want to retry on failed media swaps, this prevents
1454 that. An interesting observation is that permanent failures are not
1456 if (Cnf
->Removable
== true &&
1457 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1459 // Vf = Version.FileList();
1460 while (Vf
.end() == false) Vf
++;
1461 StoreFilename
= string();
1462 Item::Failed(Message
,Cnf
);
1466 if (QueueNext() == false)
1468 // This is the retry counter
1470 Cnf
->LocalOnly
== false &&
1471 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1474 Vf
= Version
.FileList();
1475 if (QueueNext() == true)
1479 StoreFilename
= string();
1480 Item::Failed(Message
,Cnf
);
1484 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1485 // trusted source /*{{{*/
1486 // ---------------------------------------------------------------------
1487 bool pkgAcqArchive::IsTrusted()
1492 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1493 // ---------------------------------------------------------------------
1495 void pkgAcqArchive::Finished()
1497 if (Status
== pkgAcquire::Item::StatDone
&&
1500 StoreFilename
= string();
1504 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1505 // ---------------------------------------------------------------------
1506 /* The file is added to the queue */
1507 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1508 unsigned long Size
,string Dsc
,string ShortDesc
,
1509 const string
&DestDir
, const string
&DestFilename
) :
1510 Item(Owner
), Md5Hash(MD5
)
1512 Retries
= _config
->FindI("Acquire::Retries",0);
1514 if(!DestFilename
.empty())
1515 DestFile
= DestFilename
;
1516 else if(!DestDir
.empty())
1517 DestFile
= DestDir
+ "/" + flNotDir(URI
);
1519 DestFile
= flNotDir(URI
);
1523 Desc
.Description
= Dsc
;
1526 // Set the short description to the archive component
1527 Desc
.ShortDesc
= ShortDesc
;
1529 // Get the transfer sizes
1532 if (stat(DestFile
.c_str(),&Buf
) == 0)
1534 // Hmm, the partial file is too big, erase it
1535 if ((unsigned)Buf
.st_size
> Size
)
1536 unlink(DestFile
.c_str());
1538 PartialSize
= Buf
.st_size
;
1544 // AcqFile::Done - Item downloaded OK /*{{{*/
1545 // ---------------------------------------------------------------------
1547 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1548 pkgAcquire::MethodConfig
*Cnf
)
1551 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1556 ErrorText
= "MD5Sum mismatch";
1557 Rename(DestFile
,DestFile
+ ".FAILED");
1562 Item::Done(Message
,Size
,MD5
,Cnf
);
1564 string FileName
= LookupTag(Message
,"Filename");
1565 if (FileName
.empty() == true)
1568 ErrorText
= "Method gave a blank filename";
1574 // The files timestamp matches
1575 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1578 // We have to copy it into place
1579 if (FileName
!= DestFile
)
1582 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1583 Cnf
->Removable
== true)
1585 Desc
.URI
= "copy:" + FileName
;
1590 // Erase the file if it is a symlink so we can overwrite it
1592 if (lstat(DestFile
.c_str(),&St
) == 0)
1594 if (S_ISLNK(St
.st_mode
) != 0)
1595 unlink(DestFile
.c_str());
1599 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1601 ErrorText
= "Link to " + DestFile
+ " failure ";
1608 // AcqFile::Failed - Failure handler /*{{{*/
1609 // ---------------------------------------------------------------------
1610 /* Here we try other sources */
1611 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1613 ErrorText
= LookupTag(Message
,"Message");
1615 // This is the retry counter
1617 Cnf
->LocalOnly
== false &&
1618 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1625 Item::Failed(Message
,Cnf
);