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 // no information how to get the patches, bail out
274 std::clog
<< "Can't find a patch in the index file" << std::endl
;
275 // Failed will queue a big package file
281 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
282 ExpectedMD5
, available_patches
);
293 void pkgAcqDiffIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
296 std::clog
<< "pkgAcqDiffIndex failed: " << Desc
.URI
<< std::endl
297 << "Falling back to normal index file aquire" << std::endl
;
299 new pkgAcqIndex(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
307 void pkgAcqDiffIndex::Done(string Message
,unsigned long Size
,string Md5Hash
,
308 pkgAcquire::MethodConfig
*Cnf
)
311 std::clog
<< "pkgAcqDiffIndex::Done(): " << Desc
.URI
<< std::endl
;
313 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
316 FinalFile
= _config
->FindDir("Dir::State::lists")+URItoFileName(RealURI
);
318 // sucess in downloading the index
320 FinalFile
+= string(".IndexDiff");
322 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
324 Rename(DestFile
,FinalFile
);
325 chmod(FinalFile
.c_str(),0644);
326 DestFile
= FinalFile
;
328 if(!ParseDiffIndex(DestFile
))
329 return Failed("", NULL
);
339 // AcqIndexDiffs::AcqIndexDiffs - Constructor
340 // ---------------------------------------------------------------------
341 /* The package diff is added to the queue. one object is constructed
342 * for each diff and the index
344 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire
*Owner
,
345 string URI
,string URIDesc
,string ShortDesc
,
346 string ExpectedMD5
, vector
<DiffInfo
> diffs
)
347 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
),
348 available_patches(diffs
)
351 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
352 DestFile
+= URItoFileName(URI
);
354 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
356 Desc
.Description
= URIDesc
;
358 Desc
.ShortDesc
= ShortDesc
;
360 if(available_patches
.size() == 0)
362 // we are done (yeah!)
368 State
= StateFetchDiff
;
374 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
377 std::clog
<< "pkgAcqIndexDiffs failed: " << Desc
.URI
<< std::endl
378 << "Falling back to normal index file aquire" << std::endl
;
379 new pkgAcqIndex(Owner
, RealURI
, Description
,Desc
.ShortDesc
,
385 // helper that cleans the item out of the fetcher queue
386 void pkgAcqIndexDiffs::Finish(bool allDone
)
388 // we restore the original name, this is required, otherwise
389 // the file will be cleaned
392 DestFile
= _config
->FindDir("Dir::State::lists");
393 DestFile
+= URItoFileName(RealURI
);
395 // do the final md5sum checking
397 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
398 sum
.AddFD(Fd
.Fd(), Fd
.Size());
400 string MD5
= (string
)sum
.Result();
402 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
404 Status
= StatAuthError
;
405 ErrorText
= _("MD5Sum mismatch");
406 Rename(DestFile
,DestFile
+ ".FAILED");
411 // this is for the "real" finish
416 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
421 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
430 bool pkgAcqIndexDiffs::QueueNextDiff()
433 // calc sha1 of the just patched file
434 string FinalFile
= _config
->FindDir("Dir::State::lists");
435 FinalFile
+= URItoFileName(RealURI
);
437 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
439 SHA1
.AddFD(fd
.Fd(), fd
.Size());
440 string local_sha1
= string(SHA1
.Result());
442 std::clog
<< "QueueNextDiff: "
443 << FinalFile
<< " (" << local_sha1
<< ")"<<std::endl
;
445 // remove all patches until the next matching patch is found
446 // this requires the Index file to be ordered
447 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
448 available_patches
.size() > 0 &&
449 I
!= available_patches
.end() &&
450 (*I
).sha1
!= local_sha1
;
453 available_patches
.erase(I
);
456 // error checking and falling back if no patch was found
457 if(available_patches
.size() == 0)
463 // queue the right diff
464 Desc
.URI
= string(RealURI
) + ".diff/" + available_patches
[0].file
+ ".gz";
465 Desc
.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
)
606 // no .bz2 found, retry with .gz
607 if(Desc
.URI
.substr(Desc
.URI
.size()-3) == "bz2") {
608 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
610 // retry with a gzip one
611 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
612 ExpectedMD5
, string(".gz"));
619 // on decompression failure, remove bad versions in partial/
620 if(Decompression
&& Erase
) {
621 string s
= _config
->FindDir("Dir::State::lists") + "partial/";
622 s
+= URItoFileName(RealURI
);
626 Item::Failed(Message
,Cnf
);
630 // AcqIndex::Done - Finished a fetch /*{{{*/
631 // ---------------------------------------------------------------------
632 /* This goes through a number of states.. On the initial fetch the
633 method could possibly return an alternate filename which points
634 to the uncompressed version of the file. If this is so the file
635 is copied into the partial directory. In all other cases the file
636 is decompressed with a gzip uri. */
637 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
638 pkgAcquire::MethodConfig
*Cfg
)
640 Item::Done(Message
,Size
,MD5
,Cfg
);
642 if (Decompression
== true)
644 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
646 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
647 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
653 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
654 sum
.AddFD(Fd
.Fd(), Fd
.Size());
656 MD5
= (string
)sum
.Result();
659 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
661 Status
= StatAuthError
;
662 ErrorText
= _("MD5Sum mismatch");
663 Rename(DestFile
,DestFile
+ ".FAILED");
666 // Done, move it into position
667 string FinalFile
= _config
->FindDir("Dir::State::lists");
668 FinalFile
+= URItoFileName(RealURI
);
669 Rename(DestFile
,FinalFile
);
670 chmod(FinalFile
.c_str(),0644);
672 /* We restore the original name to DestFile so that the clean operation
674 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
675 DestFile
+= URItoFileName(RealURI
);
677 // Remove the compressed version.
679 unlink(DestFile
.c_str());
686 // Handle the unzipd case
687 string FileName
= LookupTag(Message
,"Alt-Filename");
688 if (FileName
.empty() == false)
690 // The files timestamp matches
691 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
694 Decompression
= true;
696 DestFile
+= ".decomp";
697 Desc
.URI
= "copy:" + FileName
;
703 FileName
= LookupTag(Message
,"Filename");
704 if (FileName
.empty() == true)
707 ErrorText
= "Method gave a blank filename";
710 // The files timestamp matches
711 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
714 if (FileName
== DestFile
)
719 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3);
722 decompProg
= "bzip2";
723 else if(compExt
== ".gz")
726 _error
->Error("Unsupported extension: %s", compExt
.c_str());
730 Decompression
= true;
731 DestFile
+= ".decomp";
732 Desc
.URI
= string(decompProg
) + ":" + FileName
;
737 // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
738 // ---------------------------------------------------------------------
739 /* The Translation file is added to the queue */
740 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
,
741 string URI
,string URIDesc
,string ShortDesc
) :
742 pkgAcqIndex(Owner
, URI
, URIDesc
, ShortDesc
, "", "")
747 // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
748 // ---------------------------------------------------------------------
750 void pkgAcqIndexTrans::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
752 if (Cnf
->LocalOnly
== true ||
753 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
762 Item::Failed(Message
,Cnf
);
766 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
767 string URI
,string URIDesc
,string ShortDesc
,
768 string MetaIndexURI
, string MetaIndexURIDesc
,
769 string MetaIndexShortDesc
,
770 const vector
<IndexTarget
*>* IndexTargets
,
771 indexRecords
* MetaIndexParser
) :
772 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
773 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
774 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
)
776 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
777 DestFile
+= URItoFileName(URI
);
779 // remove any partial downloaded sig-file in partial/.
780 // it may confuse proxies and is too small to warrant a
781 // partial download anyway
782 unlink(DestFile
.c_str());
785 Desc
.Description
= URIDesc
;
787 Desc
.ShortDesc
= ShortDesc
;
791 string Final
= _config
->FindDir("Dir::State::lists");
792 Final
+= URItoFileName(RealURI
);
794 if (stat(Final
.c_str(),&Buf
) == 0)
796 // File was already in place. It needs to be re-verified
797 // because Release might have changed, so Move it into partial
798 Rename(Final
,DestFile
);
804 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
805 // ---------------------------------------------------------------------
806 /* The only header we use is the last-modified header. */
807 string
pkgAcqMetaSig::Custom600Headers()
810 if (stat(DestFile
.c_str(),&Buf
) != 0)
811 return "\nIndex-File: true";
813 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
816 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
817 pkgAcquire::MethodConfig
*Cfg
)
819 Item::Done(Message
,Size
,MD5
,Cfg
);
821 string FileName
= LookupTag(Message
,"Filename");
822 if (FileName
.empty() == true)
825 ErrorText
= "Method gave a blank filename";
829 if (FileName
!= DestFile
)
831 // We have to copy it into place
833 Desc
.URI
= "copy:" + FileName
;
840 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
841 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
842 DestFile
, IndexTargets
, MetaIndexParser
);
846 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
848 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
850 // if we get a network error we fail gracefully
851 if(Status
== StatTransientNetworkError
)
853 Item::Failed(Message
,Cnf
);
854 // move the sigfile back on transient network failures
855 if(FileExists(DestFile
))
856 Rename(DestFile
,Final
);
858 // set the status back to , Item::Failed likes to reset it
859 Status
= pkgAcquire::Item::StatTransientNetworkError
;
863 // Delete any existing sigfile when the acquire failed
864 unlink(Final
.c_str());
866 // queue a pkgAcqMetaIndex with no sigfile
867 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
868 "", IndexTargets
, MetaIndexParser
);
870 if (Cnf
->LocalOnly
== true ||
871 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
880 Item::Failed(Message
,Cnf
);
883 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
884 string URI
,string URIDesc
,string ShortDesc
,
886 const vector
<struct IndexTarget
*>* IndexTargets
,
887 indexRecords
* MetaIndexParser
) :
888 Item(Owner
), RealURI(URI
), SigFile(SigFile
), AuthPass(false),
889 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
), IMSHit(false)
891 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
892 DestFile
+= URItoFileName(URI
);
895 Desc
.Description
= URIDesc
;
897 Desc
.ShortDesc
= ShortDesc
;
904 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
905 // ---------------------------------------------------------------------
906 /* The only header we use is the last-modified header. */
907 string
pkgAcqMetaIndex::Custom600Headers()
909 string Final
= _config
->FindDir("Dir::State::lists");
910 Final
+= URItoFileName(RealURI
);
913 if (stat(Final
.c_str(),&Buf
) != 0)
914 return "\nIndex-File: true";
916 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
919 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
920 pkgAcquire::MethodConfig
*Cfg
)
922 Item::Done(Message
,Size
,MD5
,Cfg
);
924 // MetaIndexes are done in two passes: one to download the
925 // metaindex with an appropriate method, and a second to verify it
926 // with the gpgv method
928 if (AuthPass
== true)
934 RetrievalDone(Message
);
936 // Still more retrieving to do
941 // There was no signature file, so we are finished. Download
942 // the indexes without verification.
947 // There was a signature file, so pass it to gpgv for
950 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
951 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
952 << SigFile
<< "," << DestFile
<< ")\n";
954 Desc
.URI
= "gpgv:" + SigFile
;
961 void pkgAcqMetaIndex::RetrievalDone(string Message
)
963 // We have just finished downloading a Release file (it is not
966 string FileName
= LookupTag(Message
,"Filename");
967 if (FileName
.empty() == true)
970 ErrorText
= "Method gave a blank filename";
974 if (FileName
!= DestFile
)
977 Desc
.URI
= "copy:" + FileName
;
982 // see if the download was a IMSHit
983 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
987 string FinalFile
= _config
->FindDir("Dir::State::lists");
988 FinalFile
+= URItoFileName(RealURI
);
990 // The files timestamp matches
991 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
993 // Move it into position
994 Rename(DestFile
,FinalFile
);
996 chmod(FinalFile
.c_str(),0644);
997 DestFile
= FinalFile
;
1000 void pkgAcqMetaIndex::AuthDone(string Message
)
1002 // At this point, the gpgv method has succeeded, so there is a
1003 // valid signature from a key in the trusted keyring. We
1004 // perform additional verification of its contents, and use them
1005 // to verify the indexes we are about to download
1007 if (!MetaIndexParser
->Load(DestFile
))
1009 Status
= StatAuthError
;
1010 ErrorText
= MetaIndexParser
->ErrorText
;
1014 if (!VerifyVendor(Message
))
1019 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1020 std::cerr
<< "Signature verification succeeded: "
1021 << DestFile
<< std::endl
;
1023 // Download further indexes with verification
1026 // Done, move signature file into position
1028 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
1029 URItoFileName(RealURI
) + ".gpg";
1030 Rename(SigFile
,VerifiedSigFile
);
1031 chmod(VerifiedSigFile
.c_str(),0644);
1034 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
1036 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
1037 Target
!= IndexTargets
->end();
1040 string ExpectedIndexMD5
;
1043 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
1046 Status
= StatAuthError
;
1047 ErrorText
= "Unable to find expected entry "
1048 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
1051 ExpectedIndexMD5
= Record
->MD5Hash
;
1052 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1054 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
1055 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
1057 if (ExpectedIndexMD5
.empty())
1059 Status
= StatAuthError
;
1060 ErrorText
= "Unable to find MD5 sum for "
1061 + (*Target
)->MetaKey
+ " in Meta-index file";
1066 // Queue Packages file (either diff or full packages files, depending
1067 // on the users option)
1068 if(_config
->FindB("Acquire::PDiffs",true) == true)
1069 new pkgAcqDiffIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1070 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
1072 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
1073 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
1077 bool pkgAcqMetaIndex::VerifyVendor(string Message
)
1079 // // Maybe this should be made available from above so we don't have
1080 // // to read and parse it every time?
1081 // pkgVendorList List;
1082 // List.ReadMainList();
1084 // const Vendor* Vndr = NULL;
1085 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
1087 // string::size_type pos = (*I).find("VALIDSIG ");
1088 // if (_config->FindB("Debug::Vendor", false))
1089 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
1091 // if (pos != std::string::npos)
1093 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
1094 // if (_config->FindB("Debug::Vendor", false))
1095 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
1097 // Vndr = List.FindVendor(Fingerprint) != "";
1098 // if (Vndr != NULL);
1102 string::size_type pos
;
1104 // check for missing sigs (that where not fatal because otherwise we had
1107 string msg
= _("There is no public key available for the "
1108 "following key IDs:\n");
1109 pos
= Message
.find("NO_PUBKEY ");
1110 if (pos
!= std::string::npos
)
1112 string::size_type start
= pos
+strlen("NO_PUBKEY ");
1113 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
1114 missingkeys
+= (Fingerprint
);
1116 if(!missingkeys
.empty())
1117 _error
->Warning("%s", string(msg
+missingkeys
).c_str());
1119 string Transformed
= MetaIndexParser
->GetExpectedDist();
1121 if (Transformed
== "../project/experimental")
1123 Transformed
= "experimental";
1126 pos
= Transformed
.rfind('/');
1127 if (pos
!= string::npos
)
1129 Transformed
= Transformed
.substr(0, pos
);
1132 if (Transformed
== ".")
1137 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1139 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
1140 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
1141 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
1144 if (MetaIndexParser
->CheckDist(Transformed
) == false)
1146 // This might become fatal one day
1147 // Status = StatAuthError;
1148 // ErrorText = "Conflicting distribution; expected "
1149 // + MetaIndexParser->GetExpectedDist() + " but got "
1150 // + MetaIndexParser->GetDist();
1152 if (!Transformed
.empty())
1154 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
1155 Desc
.Description
.c_str(),
1156 Transformed
.c_str(),
1157 MetaIndexParser
->GetDist().c_str());
1164 // pkgAcqMetaIndex::Failed - no Release file present or no signature
1165 // file present /*{{{*/
1166 // ---------------------------------------------------------------------
1168 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1170 if (AuthPass
== true)
1172 // if we fail the authentication but got the file via a IMS-Hit
1173 // this means that the file wasn't downloaded and that it might be
1174 // just stale (server problem, proxy etc). we delete what we have
1175 // queue it again without i-m-s
1176 // alternatively we could just unlink the file and let the user try again
1182 unlink(DestFile
.c_str());
1184 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
1185 DestFile
+= URItoFileName(RealURI
);
1191 // gpgv method failed
1192 _error
->Warning("GPG error: %s: %s",
1193 Desc
.Description
.c_str(),
1194 LookupTag(Message
,"Message").c_str());
1198 // No Release file was present, or verification failed, so fall
1199 // back to queueing Packages files without verification
1200 QueueIndexes(false);
1205 // AcqArchive::AcqArchive - Constructor /*{{{*/
1206 // ---------------------------------------------------------------------
1207 /* This just sets up the initial fetch environment and queues the first
1209 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
1210 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
1211 string
&StoreFilename
) :
1212 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
1213 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
1216 Retries
= _config
->FindI("Acquire::Retries",0);
1218 if (Version
.Arch() == 0)
1220 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1221 "This might mean you need to manually fix this package. "
1222 "(due to missing arch)"),
1223 Version
.ParentPkg().Name());
1227 /* We need to find a filename to determine the extension. We make the
1228 assumption here that all the available sources for this version share
1229 the same extension.. */
1230 // Skip not source sources, they do not have file fields.
1231 for (; Vf
.end() == false; Vf
++)
1233 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1238 // Does not really matter here.. we are going to fail out below
1239 if (Vf
.end() != true)
1241 // If this fails to get a file name we will bomb out below.
1242 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1243 if (_error
->PendingError() == true)
1246 // Generate the final file name as: package_version_arch.foo
1247 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1248 QuoteString(Version
.VerStr(),"_:") + '_' +
1249 QuoteString(Version
.Arch(),"_:.") +
1250 "." + flExtension(Parse
.FileName());
1253 // check if we have one trusted source for the package. if so, switch
1254 // to "TrustedOnly" mode
1255 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
1257 pkgIndexFile
*Index
;
1258 if (Sources
->FindIndex(i
.File(),Index
) == false)
1260 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1262 std::cerr
<< "Checking index: " << Index
->Describe()
1263 << "(Trusted=" << Index
->IsTrusted() << ")\n";
1265 if (Index
->IsTrusted()) {
1271 // "allow-unauthenticated" restores apts old fetching behaviour
1272 // that means that e.g. unauthenticated file:// uris are higher
1273 // priority than authenticated http:// uris
1274 if (_config
->FindB("APT::Get::AllowUnauthenticated",false) == true)
1278 if (QueueNext() == false && _error
->PendingError() == false)
1279 _error
->Error(_("I wasn't able to locate file for the %s package. "
1280 "This might mean you need to manually fix this package."),
1281 Version
.ParentPkg().Name());
1284 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1285 // ---------------------------------------------------------------------
1286 /* This queues the next available file version for download. It checks if
1287 the archive is already available in the cache and stashs the MD5 for
1289 bool pkgAcqArchive::QueueNext()
1291 for (; Vf
.end() == false; Vf
++)
1293 // Ignore not source sources
1294 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1297 // Try to cross match against the source list
1298 pkgIndexFile
*Index
;
1299 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1302 // only try to get a trusted package from another source if that source
1304 if(Trusted
&& !Index
->IsTrusted())
1307 // Grab the text package record
1308 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1309 if (_error
->PendingError() == true)
1312 string PkgFile
= Parse
.FileName();
1313 MD5
= Parse
.MD5Hash();
1314 if (PkgFile
.empty() == true)
1315 return _error
->Error(_("The package index files are corrupted. No Filename: "
1316 "field for package %s."),
1317 Version
.ParentPkg().Name());
1319 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1320 Desc
.Description
= Index
->ArchiveInfo(Version
);
1322 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1324 // See if we already have the file. (Legacy filenames)
1325 FileSize
= Version
->Size
;
1326 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1328 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1330 // Make sure the size matches
1331 if ((unsigned)Buf
.st_size
== Version
->Size
)
1336 StoreFilename
= DestFile
= FinalFile
;
1340 /* Hmm, we have a file and its size does not match, this means it is
1341 an old style mismatched arch */
1342 unlink(FinalFile
.c_str());
1345 // Check it again using the new style output filenames
1346 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1347 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1349 // Make sure the size matches
1350 if ((unsigned)Buf
.st_size
== Version
->Size
)
1355 StoreFilename
= DestFile
= FinalFile
;
1359 /* Hmm, we have a file and its size does not match, this shouldnt
1361 unlink(FinalFile
.c_str());
1364 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1366 // Check the destination file
1367 if (stat(DestFile
.c_str(),&Buf
) == 0)
1369 // Hmm, the partial file is too big, erase it
1370 if ((unsigned)Buf
.st_size
> Version
->Size
)
1371 unlink(DestFile
.c_str());
1373 PartialSize
= Buf
.st_size
;
1378 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1379 Desc
.Description
= Index
->ArchiveInfo(Version
);
1381 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1390 // AcqArchive::Done - Finished fetching /*{{{*/
1391 // ---------------------------------------------------------------------
1393 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
1394 pkgAcquire::MethodConfig
*Cfg
)
1396 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
1399 if (Size
!= Version
->Size
)
1402 ErrorText
= _("Size mismatch");
1407 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1412 ErrorText
= _("MD5Sum mismatch");
1413 if(FileExists(DestFile
))
1414 Rename(DestFile
,DestFile
+ ".FAILED");
1419 // Grab the output filename
1420 string FileName
= LookupTag(Message
,"Filename");
1421 if (FileName
.empty() == true)
1424 ErrorText
= "Method gave a blank filename";
1430 // Reference filename
1431 if (FileName
!= DestFile
)
1433 StoreFilename
= DestFile
= FileName
;
1438 // Done, move it into position
1439 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1440 FinalFile
+= flNotDir(StoreFilename
);
1441 Rename(DestFile
,FinalFile
);
1443 StoreFilename
= DestFile
= FinalFile
;
1447 // AcqArchive::Failed - Failure handler /*{{{*/
1448 // ---------------------------------------------------------------------
1449 /* Here we try other sources */
1450 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1452 ErrorText
= LookupTag(Message
,"Message");
1454 /* We don't really want to retry on failed media swaps, this prevents
1455 that. An interesting observation is that permanent failures are not
1457 if (Cnf
->Removable
== true &&
1458 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1460 // Vf = Version.FileList();
1461 while (Vf
.end() == false) Vf
++;
1462 StoreFilename
= string();
1463 Item::Failed(Message
,Cnf
);
1467 if (QueueNext() == false)
1469 // This is the retry counter
1471 Cnf
->LocalOnly
== false &&
1472 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1475 Vf
= Version
.FileList();
1476 if (QueueNext() == true)
1480 StoreFilename
= string();
1481 Item::Failed(Message
,Cnf
);
1485 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1486 // trusted source /*{{{*/
1487 // ---------------------------------------------------------------------
1488 bool pkgAcqArchive::IsTrusted()
1493 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1494 // ---------------------------------------------------------------------
1496 void pkgAcqArchive::Finished()
1498 if (Status
== pkgAcquire::Item::StatDone
&&
1501 StoreFilename
= string();
1505 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1506 // ---------------------------------------------------------------------
1507 /* The file is added to the queue */
1508 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1509 unsigned long Size
,string Dsc
,string ShortDesc
,
1510 const string
&DestDir
, const string
&DestFilename
) :
1511 Item(Owner
), Md5Hash(MD5
)
1513 Retries
= _config
->FindI("Acquire::Retries",0);
1515 if(!DestFilename
.empty())
1516 DestFile
= DestFilename
;
1517 else if(!DestDir
.empty())
1518 DestFile
= DestDir
+ "/" + flNotDir(URI
);
1520 DestFile
= flNotDir(URI
);
1524 Desc
.Description
= Dsc
;
1527 // Set the short description to the archive component
1528 Desc
.ShortDesc
= ShortDesc
;
1530 // Get the transfer sizes
1533 if (stat(DestFile
.c_str(),&Buf
) == 0)
1535 // Hmm, the partial file is too big, erase it
1536 if ((unsigned)Buf
.st_size
> Size
)
1537 unlink(DestFile
.c_str());
1539 PartialSize
= Buf
.st_size
;
1545 // AcqFile::Done - Item downloaded OK /*{{{*/
1546 // ---------------------------------------------------------------------
1548 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1549 pkgAcquire::MethodConfig
*Cnf
)
1552 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1557 ErrorText
= "MD5Sum mismatch";
1558 Rename(DestFile
,DestFile
+ ".FAILED");
1563 Item::Done(Message
,Size
,MD5
,Cnf
);
1565 string FileName
= LookupTag(Message
,"Filename");
1566 if (FileName
.empty() == true)
1569 ErrorText
= "Method gave a blank filename";
1575 // The files timestamp matches
1576 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1579 // We have to copy it into place
1580 if (FileName
!= DestFile
)
1583 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1584 Cnf
->Removable
== true)
1586 Desc
.URI
= "copy:" + FileName
;
1591 // Erase the file if it is a symlink so we can overwrite it
1593 if (lstat(DestFile
.c_str(),&St
) == 0)
1595 if (S_ISLNK(St
.st_mode
) != 0)
1596 unlink(DestFile
.c_str());
1600 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1602 ErrorText
= "Link to " + DestFile
+ " failure ";
1609 // AcqFile::Failed - Failure handler /*{{{*/
1610 // ---------------------------------------------------------------------
1611 /* Here we try other sources */
1612 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1614 ErrorText
= LookupTag(Message
,"Message");
1616 // This is the retry counter
1618 Cnf
->LocalOnly
== false &&
1619 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1626 Item::Failed(Message
,Cnf
);