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());
137 // AcqIndexDiffs::AcqIndexDiffs - Constructor
138 // ---------------------------------------------------------------------
139 /* The package diff is added to the queue. one object is constructed
140 * for each diff and the index
142 pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire
*Owner
,
143 string URI
,string URIDesc
,string ShortDesc
,
144 string ExpectedMD5
, vector
<DiffInfo
> diffs
)
145 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
), available_patches(diffs
)
148 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
149 DestFile
+= URItoFileName(URI
);
151 Debug
= _config
->FindB("Debug::pkgAcquire::Diffs",false);
153 Desc
.Description
= URIDesc
;
155 Desc
.ShortDesc
= ShortDesc
;
157 CurrentPackagesFile
= _config
->FindDir("Dir::State::lists");
158 CurrentPackagesFile
+= URItoFileName(RealURI
);
161 std::clog
<< "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): "
162 << CurrentPackagesFile
<< std::endl
;
165 if(!FileExists(CurrentPackagesFile
) ||
166 !_config
->FindB("Acquire::Diffs",true)) {
167 // we don't have a pkg file or we don't want to queue
169 std::clog
<< "No index file or canceld by user" << std::endl
;
174 if(available_patches
.size() == 0)
180 void pkgAcqIndexDiffs::QueueDiffIndex(string URI
)
182 Desc
.URI
= URI
+ ".diff/Index";
183 Desc
.Description
= Description
+ "IndexDiff";
184 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
185 DestFile
+= URItoFileName(URI
) + string(".IndexDiff");
188 std::clog
<< "QueueDiffIndex: " << Desc
.URI
<< std::endl
;
193 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
194 // ---------------------------------------------------------------------
195 /* The only header we use is the last-modified header. */
196 string
pkgAcqIndexDiffs::Custom600Headers()
198 if(DestFile
.rfind(".IndexDiff") == string::npos
)
201 string Final
= _config
->FindDir("Dir::State::lists");
202 Final
+= URItoFileName(RealURI
) + string(".IndexDiff");
205 std::clog
<< "Custom600Header-IMS: " << Final
<< std::endl
;
208 if (stat(Final
.c_str(),&Buf
) != 0)
209 return "\nIndex-File: true";
211 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
214 void pkgAcqIndexDiffs::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
217 std::clog
<< "Failed(): " << Desc
.URI
<< std::endl
218 << "Falling back to big package file" << std::endl
;
219 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
225 // helper that cleans the item out of the fetcher queue
226 void pkgAcqIndexDiffs::Finish(bool allDone
)
228 // we restore the original name, this is required, otherwise
229 // the file will be cleaned
231 // this is for the "real" finish
232 DestFile
= _config
->FindDir("Dir::State::lists");
233 DestFile
+= URItoFileName(RealURI
);
237 std::clog
<< "\n\nallDone: " << DestFile
<< "\n" << std::endl
;
243 std::clog
<< "Finishing: " << Desc
.URI
<< std::endl
;
251 // this needs to be rewriten to not depend on the external ed
252 bool pkgAcqIndexDiffs::ApplyDiff(string PatchFile
)
257 string FinalFile
= _config
->FindDir("Dir::State::lists");
258 FinalFile
+= URItoFileName(RealURI
);
260 int Process
= ExecFork();
263 chdir(_config
->FindDir("Dir::State::lists").c_str());
264 // for some reason "red" fails with the pdiffs from p.d.o/~aba ?!?
265 string cmd
= "(zcat " + PatchFile
+ "; echo \"wq\" ) | /bin/ed " + FinalFile
+ " >/dev/null 2>/dev/null";
267 std::clog
<< "Runing: " << cmd
<< std::endl
;
268 res
= system(cmd
.c_str());
269 _exit(WEXITSTATUS(res
));
271 if(!ExecWait(Process
, error
, true)) {
272 //_error->Error("Patch failed: %s ", error);
279 bool pkgAcqIndexDiffs::QueueNextDiff()
281 // calc sha1 of the just patched file
282 string FinalFile
= _config
->FindDir("Dir::State::lists");
283 FinalFile
+= URItoFileName(RealURI
);
285 FileFd
fd(FinalFile
, FileFd::ReadOnly
);
287 SHA1
.AddFD(fd
.Fd(), fd
.Size());
288 string local_sha1
= string(SHA1
.Result());
290 // remove all patches until the next matching patch is found
291 // this requires the Index file to be ordered
292 for(vector
<DiffInfo
>::iterator I
=available_patches
.begin();
293 (*I
).sha1
== local_sha1
|| I
!= available_patches
.end();
295 available_patches
.erase(I
);
298 // error checking and falling back if no patch was found
299 if(available_patches
.size() == 0) {
304 // queue the right diff
305 Desc
.URI
= string(RealURI
) + string(".diff/") + available_patches
[0].file
+ string(".gz");
306 Desc
.Description
= available_patches
[0].file
+ string(".pdiff");
308 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
309 DestFile
+= URItoFileName(RealURI
+ string(".diff/") + available_patches
[0].file
);
312 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
319 bool pkgAcqIndexDiffs::ParseIndexDiff(string IndexDiffFile
)
322 std::clog
<< "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
325 FileFd
Fd(IndexDiffFile
,FileFd::ReadOnly
);
327 if (_error
->PendingError() == true)
330 if(TF
.Step(Tags
) == true)
333 string tmp
= Tags
.FindS("SHA1-Current");
334 std::stringstream
ss(tmp
);
337 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnly
);
339 SHA1
.AddFD(fd
.Fd(), fd
.Size());
340 local_sha1
= string(SHA1
.Result());
342 if(local_sha1
== ServerSha1
) {
344 std::clog
<< "Package file is up-to-date" << std::endl
;
349 std::clog
<< "SHA1-Current: " << ServerSha1
<< std::endl
;
351 // check the historie and see what patches we need
352 string history
= Tags
.FindS("SHA1-History");
353 std::stringstream
hist(history
);
357 while(hist
>> d
.sha1
>> size
>> d
.file
) {
358 d
.size
= atoi(size
.c_str());
359 if(d
.sha1
== local_sha1
)
363 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
364 available_patches
.push_back(d
);
367 // no information how to get the patches, bail out
370 std::clog
<< "Can't find a patch in the index file" << std::endl
;
371 // Failed will queue a big package file
375 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
376 ExpectedMD5
, available_patches
);
386 void pkgAcqIndexDiffs::Done(string Message
,unsigned long Size
,string Md5Hash
,
387 pkgAcquire::MethodConfig
*Cnf
)
390 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
392 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
394 int len
= Desc
.URI
.size();
395 // sucess in downloading the index
396 if(Desc
.URI
.substr(len
-strlen("Index"),len
-1) == "Index") {
399 string FinalFile
= _config
->FindDir("Dir::State::lists");
400 FinalFile
+= URItoFileName(RealURI
) + string(".IndexDiff");
402 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
404 Rename(DestFile
,FinalFile
);
405 chmod(FinalFile
.c_str(),0644);
406 DestFile
= FinalFile
;
408 if(!ParseIndexDiff(DestFile
))
409 return Failed("", NULL
);
414 // sucess in downloading a diff
415 if(Desc
.URI
.find(".diff") != string::npos
) {
417 available_patches
.erase(available_patches
.begin());
419 if(available_patches
.size() > 0) {
420 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
421 ExpectedMD5
, available_patches
);
432 // AcqIndex::AcqIndex - Constructor /*{{{*/
433 // ---------------------------------------------------------------------
434 /* The package file is added to the queue and a second class is
435 instantiated to fetch the revision file */
436 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
437 string URI
,string URIDesc
,string ShortDesc
,
438 string ExpectedMD5
, string comprExt
)
439 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
)
441 Decompression
= false;
444 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
445 DestFile
+= URItoFileName(URI
);
450 if(FileExists("/usr/bin/bzip2"))
451 Desc
.URI
= URI
+ ".bz2";
453 Desc
.URI
= URI
+ ".gz";
455 Desc
.URI
= URI
+ comprExt
;
458 Desc
.Description
= URIDesc
;
460 Desc
.ShortDesc
= ShortDesc
;
465 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
466 // ---------------------------------------------------------------------
467 /* The only header we use is the last-modified header. */
468 string
pkgAcqIndex::Custom600Headers()
470 string Final
= _config
->FindDir("Dir::State::lists");
471 Final
+= URItoFileName(RealURI
);
474 if (stat(Final
.c_str(),&Buf
) != 0)
475 return "\nIndex-File: true";
477 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
481 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
483 // no .bz2 found, retry with .gz
484 if(Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1) == "bz2") {
485 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
487 // retry with a gzip one
488 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
489 ExpectedMD5
, string(".gz"));
497 Item::Failed(Message
,Cnf
);
501 // AcqIndex::Done - Finished a fetch /*{{{*/
502 // ---------------------------------------------------------------------
503 /* This goes through a number of states.. On the initial fetch the
504 method could possibly return an alternate filename which points
505 to the uncompressed version of the file. If this is so the file
506 is copied into the partial directory. In all other cases the file
507 is decompressed with a gzip uri. */
508 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
509 pkgAcquire::MethodConfig
*Cfg
)
511 Item::Done(Message
,Size
,MD5
,Cfg
);
513 if (Decompression
== true)
515 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
517 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
518 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
524 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
525 sum
.AddFD(Fd
.Fd(), Fd
.Size());
527 MD5
= (string
)sum
.Result();
530 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
532 Status
= StatAuthError
;
533 ErrorText
= _("MD5Sum mismatch");
534 Rename(DestFile
,DestFile
+ ".FAILED");
537 // Done, move it into position
538 string FinalFile
= _config
->FindDir("Dir::State::lists");
539 FinalFile
+= URItoFileName(RealURI
);
540 Rename(DestFile
,FinalFile
);
541 chmod(FinalFile
.c_str(),0644);
543 /* We restore the original name to DestFile so that the clean operation
545 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
546 DestFile
+= URItoFileName(RealURI
);
548 // Remove the compressed version.
550 unlink(DestFile
.c_str());
557 // Handle the unzipd case
558 string FileName
= LookupTag(Message
,"Alt-Filename");
559 if (FileName
.empty() == false)
561 // The files timestamp matches
562 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
565 Decompression
= true;
567 DestFile
+= ".decomp";
568 Desc
.URI
= "copy:" + FileName
;
574 FileName
= LookupTag(Message
,"Filename");
575 if (FileName
.empty() == true)
578 ErrorText
= "Method gave a blank filename";
581 // The files timestamp matches
582 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
585 if (FileName
== DestFile
)
590 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1);
593 decompProg
= "bzip2";
594 else if(compExt
== ".gz")
597 _error
->Error("Unsupported extension: %s", compExt
.c_str());
601 Decompression
= true;
602 DestFile
+= ".decomp";
603 Desc
.URI
= string(decompProg
) + ":" + FileName
;
608 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
609 string URI
,string URIDesc
,string ShortDesc
,
610 string MetaIndexURI
, string MetaIndexURIDesc
,
611 string MetaIndexShortDesc
,
612 const vector
<IndexTarget
*>* IndexTargets
,
613 indexRecords
* MetaIndexParser
) :
614 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
615 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
)
617 this->MetaIndexParser
= MetaIndexParser
;
618 this->IndexTargets
= IndexTargets
;
619 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
620 DestFile
+= URItoFileName(URI
);
622 // remove any partial downloaded sig-file. it may confuse proxies
623 // and is too small to warrant a partial download anyway
624 unlink(DestFile
.c_str());
627 Desc
.Description
= URIDesc
;
629 Desc
.ShortDesc
= ShortDesc
;
633 string Final
= _config
->FindDir("Dir::State::lists");
634 Final
+= URItoFileName(RealURI
);
636 if (stat(Final
.c_str(),&Buf
) == 0)
638 // File was already in place. It needs to be re-verified
639 // because Release might have changed, so Move it into partial
640 Rename(Final
,DestFile
);
641 // unlink the file and do not try to use I-M-S and Last-Modified
642 // if the users proxy is broken
643 if(_config
->FindB("Acquire::BrokenProxy", false) == true) {
644 std::cerr
<< "forcing re-get of the signature file as requested" << std::endl
;
645 unlink(DestFile
.c_str());
652 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
653 // ---------------------------------------------------------------------
654 /* The only header we use is the last-modified header. */
655 string
pkgAcqMetaSig::Custom600Headers()
658 if (stat(DestFile
.c_str(),&Buf
) != 0)
659 return "\nIndex-File: true";
661 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
664 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
665 pkgAcquire::MethodConfig
*Cfg
)
667 Item::Done(Message
,Size
,MD5
,Cfg
);
669 string FileName
= LookupTag(Message
,"Filename");
670 if (FileName
.empty() == true)
673 ErrorText
= "Method gave a blank filename";
677 if (FileName
!= DestFile
)
679 // We have to copy it into place
681 Desc
.URI
= "copy:" + FileName
;
688 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
689 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
690 DestFile
, IndexTargets
, MetaIndexParser
);
694 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
696 // Delete any existing sigfile, so that this source isn't
697 // mistakenly trusted
698 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
699 unlink(Final
.c_str());
701 // queue a pkgAcqMetaIndex with no sigfile
702 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
703 "", IndexTargets
, MetaIndexParser
);
705 if (Cnf
->LocalOnly
== true ||
706 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
715 Item::Failed(Message
,Cnf
);
718 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
719 string URI
,string URIDesc
,string ShortDesc
,
721 const vector
<struct IndexTarget
*>* IndexTargets
,
722 indexRecords
* MetaIndexParser
) :
723 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
725 this->AuthPass
= false;
726 this->MetaIndexParser
= MetaIndexParser
;
727 this->IndexTargets
= IndexTargets
;
728 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
729 DestFile
+= URItoFileName(URI
);
732 Desc
.Description
= URIDesc
;
734 Desc
.ShortDesc
= ShortDesc
;
741 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
742 // ---------------------------------------------------------------------
743 /* The only header we use is the last-modified header. */
744 string
pkgAcqMetaIndex::Custom600Headers()
746 string Final
= _config
->FindDir("Dir::State::lists");
747 Final
+= URItoFileName(RealURI
);
750 if (stat(Final
.c_str(),&Buf
) != 0)
751 return "\nIndex-File: true";
753 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
756 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
757 pkgAcquire::MethodConfig
*Cfg
)
759 Item::Done(Message
,Size
,MD5
,Cfg
);
761 // MetaIndexes are done in two passes: one to download the
762 // metaindex with an appropriate method, and a second to verify it
763 // with the gpgv method
765 if (AuthPass
== true)
771 RetrievalDone(Message
);
773 // Still more retrieving to do
778 // There was no signature file, so we are finished. Download
779 // the indexes without verification.
784 // There was a signature file, so pass it to gpgv for
787 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
788 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
789 << SigFile
<< "," << DestFile
<< ")\n";
791 Desc
.URI
= "gpgv:" + SigFile
;
798 void pkgAcqMetaIndex::RetrievalDone(string Message
)
800 // We have just finished downloading a Release file (it is not
803 string FileName
= LookupTag(Message
,"Filename");
804 if (FileName
.empty() == true)
807 ErrorText
= "Method gave a blank filename";
811 if (FileName
!= DestFile
)
814 Desc
.URI
= "copy:" + FileName
;
821 string FinalFile
= _config
->FindDir("Dir::State::lists");
822 FinalFile
+= URItoFileName(RealURI
);
824 // The files timestamp matches
825 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
827 // Move it into position
828 Rename(DestFile
,FinalFile
);
830 DestFile
= FinalFile
;
833 void pkgAcqMetaIndex::AuthDone(string Message
)
835 // At this point, the gpgv method has succeeded, so there is a
836 // valid signature from a key in the trusted keyring. We
837 // perform additional verification of its contents, and use them
838 // to verify the indexes we are about to download
840 if (!MetaIndexParser
->Load(DestFile
))
842 Status
= StatAuthError
;
843 ErrorText
= MetaIndexParser
->ErrorText
;
852 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
853 std::cerr
<< "Signature verification succeeded: "
854 << DestFile
<< std::endl
;
856 // Download further indexes with verification
859 // Done, move signature file into position
861 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
862 URItoFileName(RealURI
) + ".gpg";
863 Rename(SigFile
,VerifiedSigFile
);
864 chmod(VerifiedSigFile
.c_str(),0644);
867 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
869 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
870 Target
!= IndexTargets
->end();
873 string ExpectedIndexMD5
;
876 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
879 Status
= StatAuthError
;
880 ErrorText
= "Unable to find expected entry "
881 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
884 ExpectedIndexMD5
= Record
->MD5Hash
;
885 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
887 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
888 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
890 if (ExpectedIndexMD5
.empty())
892 Status
= StatAuthError
;
893 ErrorText
= "Unable to find MD5 sum for "
894 + (*Target
)->MetaKey
+ " in Meta-index file";
899 // Queue Packages file
900 new pkgAcqIndexDiffs(Owner
, (*Target
)->URI
, (*Target
)->Description
,
901 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
905 bool pkgAcqMetaIndex::VerifyVendor()
907 // // Maybe this should be made available from above so we don't have
908 // // to read and parse it every time?
909 // pkgVendorList List;
910 // List.ReadMainList();
912 // const Vendor* Vndr = NULL;
913 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
915 // string::size_type pos = (*I).find("VALIDSIG ");
916 // if (_config->FindB("Debug::Vendor", false))
917 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
919 // if (pos != std::string::npos)
921 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
922 // if (_config->FindB("Debug::Vendor", false))
923 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
925 // Vndr = List.FindVendor(Fingerprint) != "";
926 // if (Vndr != NULL);
931 string Transformed
= MetaIndexParser
->GetExpectedDist();
933 if (Transformed
== "../project/experimental")
935 Transformed
= "experimental";
938 string::size_type pos
= Transformed
.rfind('/');
939 if (pos
!= string::npos
)
941 Transformed
= Transformed
.substr(0, pos
);
944 if (Transformed
== ".")
949 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
951 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
952 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
953 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
956 if (MetaIndexParser
->CheckDist(Transformed
) == false)
958 // This might become fatal one day
959 // Status = StatAuthError;
960 // ErrorText = "Conflicting distribution; expected "
961 // + MetaIndexParser->GetExpectedDist() + " but got "
962 // + MetaIndexParser->GetDist();
964 if (!Transformed
.empty())
966 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
967 Desc
.Description
.c_str(),
969 MetaIndexParser
->GetDist().c_str());
976 // pkgAcqMetaIndex::Failed - no Release file present or no signature
977 // file present /*{{{*/
978 // ---------------------------------------------------------------------
980 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
982 if (AuthPass
== true)
984 // gpgv method failed
985 _error
->Warning("GPG error: %s: %s",
986 Desc
.Description
.c_str(),
987 LookupTag(Message
,"Message").c_str());
990 // No Release file was present, or verification failed, so fall
991 // back to queueing Packages files without verification
997 // AcqArchive::AcqArchive - Constructor /*{{{*/
998 // ---------------------------------------------------------------------
999 /* This just sets up the initial fetch environment and queues the first
1001 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
1002 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
1003 string
&StoreFilename
) :
1004 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
1005 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
1008 Retries
= _config
->FindI("Acquire::Retries",0);
1010 if (Version
.Arch() == 0)
1012 _error
->Error(_("I wasn't able to locate a file for the %s package. "
1013 "This might mean you need to manually fix this package. "
1014 "(due to missing arch)"),
1015 Version
.ParentPkg().Name());
1019 /* We need to find a filename to determine the extension. We make the
1020 assumption here that all the available sources for this version share
1021 the same extension.. */
1022 // Skip not source sources, they do not have file fields.
1023 for (; Vf
.end() == false; Vf
++)
1025 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1030 // Does not really matter here.. we are going to fail out below
1031 if (Vf
.end() != true)
1033 // If this fails to get a file name we will bomb out below.
1034 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1035 if (_error
->PendingError() == true)
1038 // Generate the final file name as: package_version_arch.foo
1039 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1040 QuoteString(Version
.VerStr(),"_:") + '_' +
1041 QuoteString(Version
.Arch(),"_:.") +
1042 "." + flExtension(Parse
.FileName());
1045 // check if we have one trusted source for the package. if so, switch
1046 // to "TrustedOnly" mode
1047 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
1049 pkgIndexFile
*Index
;
1050 if (Sources
->FindIndex(i
.File(),Index
) == false)
1052 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1054 std::cerr
<< "Checking index: " << Index
->Describe()
1055 << "(Trusted=" << Index
->IsTrusted() << ")\n";
1057 if (Index
->IsTrusted()) {
1064 if (QueueNext() == false && _error
->PendingError() == false)
1065 _error
->Error(_("I wasn't able to locate file for the %s package. "
1066 "This might mean you need to manually fix this package."),
1067 Version
.ParentPkg().Name());
1070 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1071 // ---------------------------------------------------------------------
1072 /* This queues the next available file version for download. It checks if
1073 the archive is already available in the cache and stashs the MD5 for
1075 bool pkgAcqArchive::QueueNext()
1077 for (; Vf
.end() == false; Vf
++)
1079 // Ignore not source sources
1080 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1083 // Try to cross match against the source list
1084 pkgIndexFile
*Index
;
1085 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1088 // only try to get a trusted package from another source if that source
1090 if(Trusted
&& !Index
->IsTrusted())
1093 // Grab the text package record
1094 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1095 if (_error
->PendingError() == true)
1098 string PkgFile
= Parse
.FileName();
1099 MD5
= Parse
.MD5Hash();
1100 if (PkgFile
.empty() == true)
1101 return _error
->Error(_("The package index files are corrupted. No Filename: "
1102 "field for package %s."),
1103 Version
.ParentPkg().Name());
1105 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1106 Desc
.Description
= Index
->ArchiveInfo(Version
);
1108 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1110 // See if we already have the file. (Legacy filenames)
1111 FileSize
= Version
->Size
;
1112 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1114 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1116 // Make sure the size matches
1117 if ((unsigned)Buf
.st_size
== Version
->Size
)
1122 StoreFilename
= DestFile
= FinalFile
;
1126 /* Hmm, we have a file and its size does not match, this means it is
1127 an old style mismatched arch */
1128 unlink(FinalFile
.c_str());
1131 // Check it again using the new style output filenames
1132 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1133 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1135 // Make sure the size matches
1136 if ((unsigned)Buf
.st_size
== Version
->Size
)
1141 StoreFilename
= DestFile
= FinalFile
;
1145 /* Hmm, we have a file and its size does not match, this shouldnt
1147 unlink(FinalFile
.c_str());
1150 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1152 // Check the destination file
1153 if (stat(DestFile
.c_str(),&Buf
) == 0)
1155 // Hmm, the partial file is too big, erase it
1156 if ((unsigned)Buf
.st_size
> Version
->Size
)
1157 unlink(DestFile
.c_str());
1159 PartialSize
= Buf
.st_size
;
1164 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1165 Desc
.Description
= Index
->ArchiveInfo(Version
);
1167 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1176 // AcqArchive::Done - Finished fetching /*{{{*/
1177 // ---------------------------------------------------------------------
1179 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
1180 pkgAcquire::MethodConfig
*Cfg
)
1182 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
1185 if (Size
!= Version
->Size
)
1188 ErrorText
= _("Size mismatch");
1193 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1198 ErrorText
= _("MD5Sum mismatch");
1199 Rename(DestFile
,DestFile
+ ".FAILED");
1204 // Grab the output filename
1205 string FileName
= LookupTag(Message
,"Filename");
1206 if (FileName
.empty() == true)
1209 ErrorText
= "Method gave a blank filename";
1215 // Reference filename
1216 if (FileName
!= DestFile
)
1218 StoreFilename
= DestFile
= FileName
;
1223 // Done, move it into position
1224 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1225 FinalFile
+= flNotDir(StoreFilename
);
1226 Rename(DestFile
,FinalFile
);
1228 StoreFilename
= DestFile
= FinalFile
;
1232 // AcqArchive::Failed - Failure handler /*{{{*/
1233 // ---------------------------------------------------------------------
1234 /* Here we try other sources */
1235 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1237 ErrorText
= LookupTag(Message
,"Message");
1239 /* We don't really want to retry on failed media swaps, this prevents
1240 that. An interesting observation is that permanent failures are not
1242 if (Cnf
->Removable
== true &&
1243 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1245 // Vf = Version.FileList();
1246 while (Vf
.end() == false) Vf
++;
1247 StoreFilename
= string();
1248 Item::Failed(Message
,Cnf
);
1252 if (QueueNext() == false)
1254 // This is the retry counter
1256 Cnf
->LocalOnly
== false &&
1257 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1260 Vf
= Version
.FileList();
1261 if (QueueNext() == true)
1265 StoreFilename
= string();
1266 Item::Failed(Message
,Cnf
);
1270 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1271 // trusted source /*{{{*/
1272 // ---------------------------------------------------------------------
1273 bool pkgAcqArchive::IsTrusted()
1278 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1279 // ---------------------------------------------------------------------
1281 void pkgAcqArchive::Finished()
1283 if (Status
== pkgAcquire::Item::StatDone
&&
1286 StoreFilename
= string();
1290 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1291 // ---------------------------------------------------------------------
1292 /* The file is added to the queue */
1293 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1294 unsigned long Size
,string Dsc
,string ShortDesc
) :
1295 Item(Owner
), Md5Hash(MD5
)
1297 Retries
= _config
->FindI("Acquire::Retries",0);
1299 DestFile
= flNotDir(URI
);
1303 Desc
.Description
= Dsc
;
1306 // Set the short description to the archive component
1307 Desc
.ShortDesc
= ShortDesc
;
1309 // Get the transfer sizes
1312 if (stat(DestFile
.c_str(),&Buf
) == 0)
1314 // Hmm, the partial file is too big, erase it
1315 if ((unsigned)Buf
.st_size
> Size
)
1316 unlink(DestFile
.c_str());
1318 PartialSize
= Buf
.st_size
;
1324 // AcqFile::Done - Item downloaded OK /*{{{*/
1325 // ---------------------------------------------------------------------
1327 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1328 pkgAcquire::MethodConfig
*Cnf
)
1331 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1336 ErrorText
= "MD5Sum mismatch";
1337 Rename(DestFile
,DestFile
+ ".FAILED");
1342 Item::Done(Message
,Size
,MD5
,Cnf
);
1344 string FileName
= LookupTag(Message
,"Filename");
1345 if (FileName
.empty() == true)
1348 ErrorText
= "Method gave a blank filename";
1354 // The files timestamp matches
1355 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1358 // We have to copy it into place
1359 if (FileName
!= DestFile
)
1362 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1363 Cnf
->Removable
== true)
1365 Desc
.URI
= "copy:" + FileName
;
1370 // Erase the file if it is a symlink so we can overwrite it
1372 if (lstat(DestFile
.c_str(),&St
) == 0)
1374 if (S_ISLNK(St
.st_mode
) != 0)
1375 unlink(DestFile
.c_str());
1379 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1381 ErrorText
= "Link to " + DestFile
+ " failure ";
1388 // AcqFile::Failed - Failure handler /*{{{*/
1389 // ---------------------------------------------------------------------
1390 /* Here we try other sources */
1391 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1393 ErrorText
= LookupTag(Message
,"Message");
1395 // This is the retry counter
1397 Cnf
->LocalOnly
== false &&
1398 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1405 Item::Failed(Message
,Cnf
);