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
), needed_files(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(needed_files
.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 string cmd
= "(zcat " + PatchFile
+ "; echo \"wq\" ) | red " + FinalFile
+ " >/dev/null 2>/dev/null";
266 std::clog
<< "Runing: " << cmd
<< std::endl
;
267 res
= system(cmd
.c_str());
268 _exit(WEXITSTATUS(res
));
270 if(!ExecWait(Process
, error
, true)) {
271 //_error->Error("Patch failed: %s ", error);
278 bool pkgAcqIndexDiffs::QueueNextDiff()
280 // FIXME: don't use the needed_files[0] but check sha1 and search
281 // for the right patch in needed_files
282 // -> and rename needed_files to "available_patches"
285 Desc
.URI
= string(RealURI
) + string(".diff/") + needed_files
[0].file
+ string(".gz");
286 Desc
.Description
= needed_files
[0].file
+ string(".pdiff");
288 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
289 DestFile
+= URItoFileName(RealURI
+ string(".diff/") + needed_files
[0].file
);
292 std::clog
<< "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc
.URI
<< std::endl
;
299 bool pkgAcqIndexDiffs::ParseIndexDiff(string IndexDiffFile
)
302 std::clog
<< "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile
305 FileFd
Fd(IndexDiffFile
,FileFd::ReadOnly
);
307 if (_error
->PendingError() == true)
310 if(TF
.Step(Tags
) == true)
313 string tmp
= Tags
.FindS("SHA1-Current");
314 std::stringstream
ss(tmp
);
317 FileFd
fd(CurrentPackagesFile
, FileFd::ReadOnly
);
319 SHA1
.AddFD(fd
.Fd(), fd
.Size());
320 local_sha1
= string(SHA1
.Result());
322 if(local_sha1
== ServerSha1
) {
324 std::clog
<< "Package file is up-to-date" << std::endl
;
329 std::clog
<< "SHA1-Current: " << ServerSha1
<< std::endl
;
331 // check the historie and see what patches we need
332 string history
= Tags
.FindS("SHA1-History");
333 std::stringstream
hist(history
);
337 while(hist
>> d
.sha1
>> size
>> d
.file
) {
338 d
.size
= atoi(size
.c_str());
339 if(d
.sha1
== local_sha1
)
343 std::clog
<< "Need to get diff: " << d
.file
<< std::endl
;
344 needed_files
.push_back(d
);
347 // no information how to get the patches, bail out
350 std::clog
<< "Can't find a patch in the index file" << std::endl
;
351 // Failed will queue a big package file
355 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
356 ExpectedMD5
, needed_files
);
366 void pkgAcqIndexDiffs::Done(string Message
,unsigned long Size
,string Md5Hash
,
367 pkgAcquire::MethodConfig
*Cnf
)
370 std::clog
<< "pkgAcqIndexDiffs::Done(): " << Desc
.URI
<< std::endl
;
372 Item::Done(Message
,Size
,Md5Hash
,Cnf
);
374 int len
= Desc
.URI
.size();
375 // sucess in downloading the index
376 if(Desc
.URI
.substr(len
-strlen("Index"),len
-1) == "Index") {
379 string FinalFile
= _config
->FindDir("Dir::State::lists");
380 FinalFile
+= URItoFileName(RealURI
) + string(".IndexDiff");
382 std::clog
<< "Renaming: " << DestFile
<< " -> " << FinalFile
384 Rename(DestFile
,FinalFile
);
385 chmod(FinalFile
.c_str(),0644);
386 DestFile
= FinalFile
;
388 if(!ParseIndexDiff(DestFile
))
389 return Failed("", NULL
);
394 // sucess in downloading a diff
395 if(Desc
.URI
.find(".diff") != string::npos
) {
397 needed_files
.erase(needed_files
.begin());
399 if(needed_files
.size() > 0) {
400 new pkgAcqIndexDiffs(Owner
, RealURI
, Description
, Desc
.ShortDesc
,
401 ExpectedMD5
, needed_files
);
412 // AcqIndex::AcqIndex - Constructor /*{{{*/
413 // ---------------------------------------------------------------------
414 /* The package file is added to the queue and a second class is
415 instantiated to fetch the revision file */
416 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
417 string URI
,string URIDesc
,string ShortDesc
,
418 string ExpectedMD5
, string comprExt
)
419 : Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
)
421 Decompression
= false;
424 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
425 DestFile
+= URItoFileName(URI
);
430 if(FileExists("/usr/bin/bzip2"))
431 Desc
.URI
= URI
+ ".bz2";
433 Desc
.URI
= URI
+ ".gz";
435 Desc
.URI
= URI
+ comprExt
;
438 Desc
.Description
= URIDesc
;
440 Desc
.ShortDesc
= ShortDesc
;
445 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
446 // ---------------------------------------------------------------------
447 /* The only header we use is the last-modified header. */
448 string
pkgAcqIndex::Custom600Headers()
450 string Final
= _config
->FindDir("Dir::State::lists");
451 Final
+= URItoFileName(RealURI
);
454 if (stat(Final
.c_str(),&Buf
) != 0)
455 return "\nIndex-File: true";
457 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
461 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
463 // no .bz2 found, retry with .gz
464 if(Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1) == "bz2") {
465 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
467 // retry with a gzip one
468 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
469 ExpectedMD5
, string(".gz"));
477 Item::Failed(Message
,Cnf
);
481 // AcqIndex::Done - Finished a fetch /*{{{*/
482 // ---------------------------------------------------------------------
483 /* This goes through a number of states.. On the initial fetch the
484 method could possibly return an alternate filename which points
485 to the uncompressed version of the file. If this is so the file
486 is copied into the partial directory. In all other cases the file
487 is decompressed with a gzip uri. */
488 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
489 pkgAcquire::MethodConfig
*Cfg
)
491 Item::Done(Message
,Size
,MD5
,Cfg
);
493 if (Decompression
== true)
495 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
497 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
498 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
504 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
505 sum
.AddFD(Fd
.Fd(), Fd
.Size());
507 MD5
= (string
)sum
.Result();
510 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
512 Status
= StatAuthError
;
513 ErrorText
= _("MD5Sum mismatch");
514 Rename(DestFile
,DestFile
+ ".FAILED");
517 // Done, move it into position
518 string FinalFile
= _config
->FindDir("Dir::State::lists");
519 FinalFile
+= URItoFileName(RealURI
);
520 Rename(DestFile
,FinalFile
);
521 chmod(FinalFile
.c_str(),0644);
523 /* We restore the original name to DestFile so that the clean operation
525 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
526 DestFile
+= URItoFileName(RealURI
);
528 // Remove the compressed version.
530 unlink(DestFile
.c_str());
537 // Handle the unzipd case
538 string FileName
= LookupTag(Message
,"Alt-Filename");
539 if (FileName
.empty() == false)
541 // The files timestamp matches
542 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
545 Decompression
= true;
547 DestFile
+= ".decomp";
548 Desc
.URI
= "copy:" + FileName
;
554 FileName
= LookupTag(Message
,"Filename");
555 if (FileName
.empty() == true)
558 ErrorText
= "Method gave a blank filename";
561 // The files timestamp matches
562 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
565 if (FileName
== DestFile
)
570 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1);
573 decompProg
= "bzip2";
574 else if(compExt
== ".gz")
577 _error
->Error("Unsupported extension: %s", compExt
.c_str());
581 Decompression
= true;
582 DestFile
+= ".decomp";
583 Desc
.URI
= string(decompProg
) + ":" + FileName
;
588 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
589 string URI
,string URIDesc
,string ShortDesc
,
590 string MetaIndexURI
, string MetaIndexURIDesc
,
591 string MetaIndexShortDesc
,
592 const vector
<IndexTarget
*>* IndexTargets
,
593 indexRecords
* MetaIndexParser
) :
594 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
595 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
)
597 this->MetaIndexParser
= MetaIndexParser
;
598 this->IndexTargets
= IndexTargets
;
599 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
600 DestFile
+= URItoFileName(URI
);
602 // remove any partial downloaded sig-file. it may confuse proxies
603 // and is too small to warrant a partial download anyway
604 unlink(DestFile
.c_str());
607 Desc
.Description
= URIDesc
;
609 Desc
.ShortDesc
= ShortDesc
;
613 string Final
= _config
->FindDir("Dir::State::lists");
614 Final
+= URItoFileName(RealURI
);
616 if (stat(Final
.c_str(),&Buf
) == 0)
618 // File was already in place. It needs to be re-verified
619 // because Release might have changed, so Move it into partial
620 Rename(Final
,DestFile
);
626 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
627 // ---------------------------------------------------------------------
628 /* The only header we use is the last-modified header. */
629 string
pkgAcqMetaSig::Custom600Headers()
632 if (stat(DestFile
.c_str(),&Buf
) != 0)
633 return "\nIndex-File: true";
635 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
638 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
639 pkgAcquire::MethodConfig
*Cfg
)
641 Item::Done(Message
,Size
,MD5
,Cfg
);
643 string FileName
= LookupTag(Message
,"Filename");
644 if (FileName
.empty() == true)
647 ErrorText
= "Method gave a blank filename";
651 if (FileName
!= DestFile
)
653 // We have to copy it into place
655 Desc
.URI
= "copy:" + FileName
;
662 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
663 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
664 DestFile
, IndexTargets
, MetaIndexParser
);
668 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
670 // Delete any existing sigfile, so that this source isn't
671 // mistakenly trusted
672 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
673 unlink(Final
.c_str());
675 // queue a pkgAcqMetaIndex with no sigfile
676 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
677 "", IndexTargets
, MetaIndexParser
);
679 if (Cnf
->LocalOnly
== true ||
680 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
689 Item::Failed(Message
,Cnf
);
692 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
693 string URI
,string URIDesc
,string ShortDesc
,
695 const vector
<struct IndexTarget
*>* IndexTargets
,
696 indexRecords
* MetaIndexParser
) :
697 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
699 this->AuthPass
= false;
700 this->MetaIndexParser
= MetaIndexParser
;
701 this->IndexTargets
= IndexTargets
;
702 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
703 DestFile
+= URItoFileName(URI
);
706 Desc
.Description
= URIDesc
;
708 Desc
.ShortDesc
= ShortDesc
;
715 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
716 // ---------------------------------------------------------------------
717 /* The only header we use is the last-modified header. */
718 string
pkgAcqMetaIndex::Custom600Headers()
720 string Final
= _config
->FindDir("Dir::State::lists");
721 Final
+= URItoFileName(RealURI
);
724 if (stat(Final
.c_str(),&Buf
) != 0)
725 return "\nIndex-File: true";
727 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
730 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
731 pkgAcquire::MethodConfig
*Cfg
)
733 Item::Done(Message
,Size
,MD5
,Cfg
);
735 // MetaIndexes are done in two passes: one to download the
736 // metaindex with an appropriate method, and a second to verify it
737 // with the gpgv method
739 if (AuthPass
== true)
745 RetrievalDone(Message
);
747 // Still more retrieving to do
752 // There was no signature file, so we are finished. Download
753 // the indexes without verification.
758 // There was a signature file, so pass it to gpgv for
761 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
762 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
763 << SigFile
<< "," << DestFile
<< ")\n";
765 Desc
.URI
= "gpgv:" + SigFile
;
772 void pkgAcqMetaIndex::RetrievalDone(string Message
)
774 // We have just finished downloading a Release file (it is not
777 string FileName
= LookupTag(Message
,"Filename");
778 if (FileName
.empty() == true)
781 ErrorText
= "Method gave a blank filename";
785 if (FileName
!= DestFile
)
788 Desc
.URI
= "copy:" + FileName
;
795 string FinalFile
= _config
->FindDir("Dir::State::lists");
796 FinalFile
+= URItoFileName(RealURI
);
798 // The files timestamp matches
799 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
801 // Move it into position
802 Rename(DestFile
,FinalFile
);
804 DestFile
= FinalFile
;
807 void pkgAcqMetaIndex::AuthDone(string Message
)
809 // At this point, the gpgv method has succeeded, so there is a
810 // valid signature from a key in the trusted keyring. We
811 // perform additional verification of its contents, and use them
812 // to verify the indexes we are about to download
814 if (!MetaIndexParser
->Load(DestFile
))
816 Status
= StatAuthError
;
817 ErrorText
= MetaIndexParser
->ErrorText
;
826 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
827 std::cerr
<< "Signature verification succeeded: "
828 << DestFile
<< std::endl
;
830 // Download further indexes with verification
833 // Done, move signature file into position
835 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
836 URItoFileName(RealURI
) + ".gpg";
837 Rename(SigFile
,VerifiedSigFile
);
838 chmod(VerifiedSigFile
.c_str(),0644);
841 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
843 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
844 Target
!= IndexTargets
->end();
847 string ExpectedIndexMD5
;
850 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
853 Status
= StatAuthError
;
854 ErrorText
= "Unable to find expected entry "
855 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
858 ExpectedIndexMD5
= Record
->MD5Hash
;
859 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
861 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
862 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
864 if (ExpectedIndexMD5
.empty())
866 Status
= StatAuthError
;
867 ErrorText
= "Unable to find MD5 sum for "
868 + (*Target
)->MetaKey
+ " in Meta-index file";
873 // Queue Packages file
874 new pkgAcqIndexDiffs(Owner
, (*Target
)->URI
, (*Target
)->Description
,
875 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
879 bool pkgAcqMetaIndex::VerifyVendor()
881 // // Maybe this should be made available from above so we don't have
882 // // to read and parse it every time?
883 // pkgVendorList List;
884 // List.ReadMainList();
886 // const Vendor* Vndr = NULL;
887 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
889 // string::size_type pos = (*I).find("VALIDSIG ");
890 // if (_config->FindB("Debug::Vendor", false))
891 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
893 // if (pos != std::string::npos)
895 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
896 // if (_config->FindB("Debug::Vendor", false))
897 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
899 // Vndr = List.FindVendor(Fingerprint) != "";
900 // if (Vndr != NULL);
905 string Transformed
= MetaIndexParser
->GetExpectedDist();
907 if (Transformed
== "../project/experimental")
909 Transformed
= "experimental";
912 string::size_type pos
= Transformed
.rfind('/');
913 if (pos
!= string::npos
)
915 Transformed
= Transformed
.substr(0, pos
);
918 if (Transformed
== ".")
923 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
925 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
926 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
927 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
930 if (MetaIndexParser
->CheckDist(Transformed
) == false)
932 // This might become fatal one day
933 // Status = StatAuthError;
934 // ErrorText = "Conflicting distribution; expected "
935 // + MetaIndexParser->GetExpectedDist() + " but got "
936 // + MetaIndexParser->GetDist();
938 if (!Transformed
.empty())
940 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
941 Desc
.Description
.c_str(),
943 MetaIndexParser
->GetDist().c_str());
950 // pkgAcqMetaIndex::Failed - no Release file present or no signature
951 // file present /*{{{*/
952 // ---------------------------------------------------------------------
954 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
956 if (AuthPass
== true)
958 // gpgv method failed
959 _error
->Warning("GPG error: %s: %s",
960 Desc
.Description
.c_str(),
961 LookupTag(Message
,"Message").c_str());
964 // No Release file was present, or verification failed, so fall
965 // back to queueing Packages files without verification
971 // AcqArchive::AcqArchive - Constructor /*{{{*/
972 // ---------------------------------------------------------------------
973 /* This just sets up the initial fetch environment and queues the first
975 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
976 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
977 string
&StoreFilename
) :
978 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
979 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
982 Retries
= _config
->FindI("Acquire::Retries",0);
984 if (Version
.Arch() == 0)
986 _error
->Error(_("I wasn't able to locate a file for the %s package. "
987 "This might mean you need to manually fix this package. "
988 "(due to missing arch)"),
989 Version
.ParentPkg().Name());
993 /* We need to find a filename to determine the extension. We make the
994 assumption here that all the available sources for this version share
995 the same extension.. */
996 // Skip not source sources, they do not have file fields.
997 for (; Vf
.end() == false; Vf
++)
999 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1004 // Does not really matter here.. we are going to fail out below
1005 if (Vf
.end() != true)
1007 // If this fails to get a file name we will bomb out below.
1008 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1009 if (_error
->PendingError() == true)
1012 // Generate the final file name as: package_version_arch.foo
1013 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
1014 QuoteString(Version
.VerStr(),"_:") + '_' +
1015 QuoteString(Version
.Arch(),"_:.") +
1016 "." + flExtension(Parse
.FileName());
1019 // check if we have one trusted source for the package. if so, switch
1020 // to "TrustedOnly" mode
1021 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
1023 pkgIndexFile
*Index
;
1024 if (Sources
->FindIndex(i
.File(),Index
) == false)
1026 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
1028 std::cerr
<< "Checking index: " << Index
->Describe()
1029 << "(Trusted=" << Index
->IsTrusted() << ")\n";
1031 if (Index
->IsTrusted()) {
1038 if (QueueNext() == false && _error
->PendingError() == false)
1039 _error
->Error(_("I wasn't able to locate file for the %s package. "
1040 "This might mean you need to manually fix this package."),
1041 Version
.ParentPkg().Name());
1044 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
1045 // ---------------------------------------------------------------------
1046 /* This queues the next available file version for download. It checks if
1047 the archive is already available in the cache and stashs the MD5 for
1049 bool pkgAcqArchive::QueueNext()
1051 for (; Vf
.end() == false; Vf
++)
1053 // Ignore not source sources
1054 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
1057 // Try to cross match against the source list
1058 pkgIndexFile
*Index
;
1059 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
1062 // only try to get a trusted package from another source if that source
1064 if(Trusted
&& !Index
->IsTrusted())
1067 // Grab the text package record
1068 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
1069 if (_error
->PendingError() == true)
1072 string PkgFile
= Parse
.FileName();
1073 MD5
= Parse
.MD5Hash();
1074 if (PkgFile
.empty() == true)
1075 return _error
->Error(_("The package index files are corrupted. No Filename: "
1076 "field for package %s."),
1077 Version
.ParentPkg().Name());
1079 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1080 Desc
.Description
= Index
->ArchiveInfo(Version
);
1082 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1084 // See if we already have the file. (Legacy filenames)
1085 FileSize
= Version
->Size
;
1086 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
1088 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1090 // Make sure the size matches
1091 if ((unsigned)Buf
.st_size
== Version
->Size
)
1096 StoreFilename
= DestFile
= FinalFile
;
1100 /* Hmm, we have a file and its size does not match, this means it is
1101 an old style mismatched arch */
1102 unlink(FinalFile
.c_str());
1105 // Check it again using the new style output filenames
1106 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
1107 if (stat(FinalFile
.c_str(),&Buf
) == 0)
1109 // Make sure the size matches
1110 if ((unsigned)Buf
.st_size
== Version
->Size
)
1115 StoreFilename
= DestFile
= FinalFile
;
1119 /* Hmm, we have a file and its size does not match, this shouldnt
1121 unlink(FinalFile
.c_str());
1124 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
1126 // Check the destination file
1127 if (stat(DestFile
.c_str(),&Buf
) == 0)
1129 // Hmm, the partial file is too big, erase it
1130 if ((unsigned)Buf
.st_size
> Version
->Size
)
1131 unlink(DestFile
.c_str());
1133 PartialSize
= Buf
.st_size
;
1138 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
1139 Desc
.Description
= Index
->ArchiveInfo(Version
);
1141 Desc
.ShortDesc
= Version
.ParentPkg().Name();
1150 // AcqArchive::Done - Finished fetching /*{{{*/
1151 // ---------------------------------------------------------------------
1153 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
1154 pkgAcquire::MethodConfig
*Cfg
)
1156 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
1159 if (Size
!= Version
->Size
)
1162 ErrorText
= _("Size mismatch");
1167 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1172 ErrorText
= _("MD5Sum mismatch");
1173 Rename(DestFile
,DestFile
+ ".FAILED");
1178 // Grab the output filename
1179 string FileName
= LookupTag(Message
,"Filename");
1180 if (FileName
.empty() == true)
1183 ErrorText
= "Method gave a blank filename";
1189 // Reference filename
1190 if (FileName
!= DestFile
)
1192 StoreFilename
= DestFile
= FileName
;
1197 // Done, move it into position
1198 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1199 FinalFile
+= flNotDir(StoreFilename
);
1200 Rename(DestFile
,FinalFile
);
1202 StoreFilename
= DestFile
= FinalFile
;
1206 // AcqArchive::Failed - Failure handler /*{{{*/
1207 // ---------------------------------------------------------------------
1208 /* Here we try other sources */
1209 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1211 ErrorText
= LookupTag(Message
,"Message");
1213 /* We don't really want to retry on failed media swaps, this prevents
1214 that. An interesting observation is that permanent failures are not
1216 if (Cnf
->Removable
== true &&
1217 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1219 // Vf = Version.FileList();
1220 while (Vf
.end() == false) Vf
++;
1221 StoreFilename
= string();
1222 Item::Failed(Message
,Cnf
);
1226 if (QueueNext() == false)
1228 // This is the retry counter
1230 Cnf
->LocalOnly
== false &&
1231 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1234 Vf
= Version
.FileList();
1235 if (QueueNext() == true)
1239 StoreFilename
= string();
1240 Item::Failed(Message
,Cnf
);
1244 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1245 // trusted source /*{{{*/
1246 // ---------------------------------------------------------------------
1247 bool pkgAcqArchive::IsTrusted()
1252 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1253 // ---------------------------------------------------------------------
1255 void pkgAcqArchive::Finished()
1257 if (Status
== pkgAcquire::Item::StatDone
&&
1260 StoreFilename
= string();
1264 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1265 // ---------------------------------------------------------------------
1266 /* The file is added to the queue */
1267 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1268 unsigned long Size
,string Dsc
,string ShortDesc
) :
1269 Item(Owner
), Md5Hash(MD5
)
1271 Retries
= _config
->FindI("Acquire::Retries",0);
1273 DestFile
= flNotDir(URI
);
1277 Desc
.Description
= Dsc
;
1280 // Set the short description to the archive component
1281 Desc
.ShortDesc
= ShortDesc
;
1283 // Get the transfer sizes
1286 if (stat(DestFile
.c_str(),&Buf
) == 0)
1288 // Hmm, the partial file is too big, erase it
1289 if ((unsigned)Buf
.st_size
> Size
)
1290 unlink(DestFile
.c_str());
1292 PartialSize
= Buf
.st_size
;
1298 // AcqFile::Done - Item downloaded OK /*{{{*/
1299 // ---------------------------------------------------------------------
1301 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1302 pkgAcquire::MethodConfig
*Cnf
)
1305 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1310 ErrorText
= "MD5Sum mismatch";
1311 Rename(DestFile
,DestFile
+ ".FAILED");
1316 Item::Done(Message
,Size
,MD5
,Cnf
);
1318 string FileName
= LookupTag(Message
,"Filename");
1319 if (FileName
.empty() == true)
1322 ErrorText
= "Method gave a blank filename";
1328 // The files timestamp matches
1329 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1332 // We have to copy it into place
1333 if (FileName
!= DestFile
)
1336 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1337 Cnf
->Removable
== true)
1339 Desc
.URI
= "copy:" + FileName
;
1344 // Erase the file if it is a symlink so we can overwrite it
1346 if (lstat(DestFile
.c_str(),&St
) == 0)
1348 if (S_ISLNK(St
.st_mode
) != 0)
1349 unlink(DestFile
.c_str());
1353 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1355 ErrorText
= "Link to " + DestFile
+ " failure ";
1362 // AcqFile::Failed - Failure handler /*{{{*/
1363 // ---------------------------------------------------------------------
1364 /* Here we try other sources */
1365 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1367 ErrorText
= LookupTag(Message
,"Message");
1369 // This is the retry counter
1371 Cnf
->LocalOnly
== false &&
1372 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1379 Item::Failed(Message
,Cnf
);