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>
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 if (Complete
== false && FileName
== DestFile
)
106 Owner
->Log
->Fetched(Size
,atoi(LookupTag(Message
,"Resume-Point","0").c_str()));
113 ErrorText
= string();
114 Owner
->Dequeue(this);
117 // Acquire::Item::Rename - Rename a file /*{{{*/
118 // ---------------------------------------------------------------------
119 /* This helper function is used by alot of item methods as thier final
121 void pkgAcquire::Item::Rename(string From
,string To
)
123 if (rename(From
.c_str(),To
.c_str()) != 0)
126 snprintf(S
,sizeof(S
),_("rename failed, %s (%s -> %s)."),strerror(errno
),
127 From
.c_str(),To
.c_str());
134 // AcqIndex::AcqIndex - Constructor /*{{{*/
135 // ---------------------------------------------------------------------
136 /* The package file is added to the queue and a second class is
137 instantiated to fetch the revision file */
138 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
139 string URI
,string URIDesc
,string ShortDesc
,
140 string ExpectedMD5
, string comprExt
) :
141 Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
)
143 Decompression
= false;
146 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
147 DestFile
+= URItoFileName(URI
);
151 // autoselect the compression method
152 if(FileExists("/bin/bzip2"))
153 CompressionExtension
= ".bz2";
155 CompressionExtension
= ".gz";
157 CompressionExtension
= comprExt
;
159 Desc
.URI
= URI
+ CompressionExtension
;
161 Desc
.Description
= URIDesc
;
163 Desc
.ShortDesc
= ShortDesc
;
168 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
169 // ---------------------------------------------------------------------
170 /* The only header we use is the last-modified header. */
171 string
pkgAcqIndex::Custom600Headers()
173 string Final
= _config
->FindDir("Dir::State::lists");
174 Final
+= URItoFileName(RealURI
);
177 if (stat(Final
.c_str(),&Buf
) != 0)
178 return "\nIndex-File: true";
180 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
184 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
187 // no .bz2 found, retry with .gz
188 if(Desc
.URI
.substr(Desc
.URI
.size()-3) == "bz2") {
189 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
191 // retry with a gzip one
192 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
193 ExpectedMD5
, string(".gz"));
200 // on decompression failure, remove bad versions in partial/
201 if(Decompression
&& Erase
) {
202 string s
= _config
->FindDir("Dir::State::lists") + "partial/";
203 s
+= URItoFileName(RealURI
);
207 Item::Failed(Message
,Cnf
);
211 // AcqIndex::Done - Finished a fetch /*{{{*/
212 // ---------------------------------------------------------------------
213 /* This goes through a number of states.. On the initial fetch the
214 method could possibly return an alternate filename which points
215 to the uncompressed version of the file. If this is so the file
216 is copied into the partial directory. In all other cases the file
217 is decompressed with a gzip uri. */
218 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
219 pkgAcquire::MethodConfig
*Cfg
)
221 Item::Done(Message
,Size
,MD5
,Cfg
);
223 if (Decompression
== true)
225 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
227 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
228 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
234 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
235 sum
.AddFD(Fd
.Fd(), Fd
.Size());
237 MD5
= (string
)sum
.Result();
240 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
242 Status
= StatAuthError
;
243 ErrorText
= _("MD5Sum mismatch");
244 Rename(DestFile
,DestFile
+ ".FAILED");
247 // Done, move it into position
248 string FinalFile
= _config
->FindDir("Dir::State::lists");
249 FinalFile
+= URItoFileName(RealURI
);
250 Rename(DestFile
,FinalFile
);
251 chmod(FinalFile
.c_str(),0644);
253 /* We restore the original name to DestFile so that the clean operation
255 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
256 DestFile
+= URItoFileName(RealURI
);
258 // Remove the compressed version.
260 unlink(DestFile
.c_str());
267 // Handle the unzipd case
268 string FileName
= LookupTag(Message
,"Alt-Filename");
269 if (FileName
.empty() == false)
271 // The files timestamp matches
272 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
275 Decompression
= true;
277 DestFile
+= ".decomp";
278 Desc
.URI
= "copy:" + FileName
;
284 FileName
= LookupTag(Message
,"Filename");
285 if (FileName
.empty() == true)
288 ErrorText
= "Method gave a blank filename";
291 // The files timestamp matches
292 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
295 if (FileName
== DestFile
)
300 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3);
303 decompProg
= "bzip2";
304 else if(compExt
== ".gz")
307 _error
->Error("Unsupported extension: %s", compExt
.c_str());
311 Decompression
= true;
312 DestFile
+= ".decomp";
313 Desc
.URI
= string(decompProg
) + ":" + FileName
;
318 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
319 string URI
,string URIDesc
,string ShortDesc
,
320 string MetaIndexURI
, string MetaIndexURIDesc
,
321 string MetaIndexShortDesc
,
322 const vector
<IndexTarget
*>* IndexTargets
,
323 indexRecords
* MetaIndexParser
) :
324 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
325 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
326 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
)
328 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
329 DestFile
+= URItoFileName(URI
);
331 // remove any partial downloaded sig-file. it may confuse proxies
332 // and is too small to warrant a partial download anyway
333 unlink(DestFile
.c_str());
336 Desc
.Description
= URIDesc
;
338 Desc
.ShortDesc
= ShortDesc
;
342 string Final
= _config
->FindDir("Dir::State::lists");
343 Final
+= URItoFileName(RealURI
);
345 if (stat(Final
.c_str(),&Buf
) == 0)
347 // File was already in place. It needs to be re-verified
348 // because Release might have changed, so Move it into partial
349 Rename(Final
,DestFile
);
355 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
356 // ---------------------------------------------------------------------
357 /* The only header we use is the last-modified header. */
358 string
pkgAcqMetaSig::Custom600Headers()
361 if (stat(DestFile
.c_str(),&Buf
) != 0)
362 return "\nIndex-File: true";
364 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
367 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
368 pkgAcquire::MethodConfig
*Cfg
)
370 Item::Done(Message
,Size
,MD5
,Cfg
);
372 string FileName
= LookupTag(Message
,"Filename");
373 if (FileName
.empty() == true)
376 ErrorText
= "Method gave a blank filename";
380 if (FileName
!= DestFile
)
382 // We have to copy it into place
384 Desc
.URI
= "copy:" + FileName
;
391 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
392 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
393 DestFile
, IndexTargets
, MetaIndexParser
);
397 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
400 // if we get a network error we fail gracefully
401 if(LookupTag(Message
,"FailReason") == "Timeout" ||
402 LookupTag(Message
,"FailReason") == "TmpResolveFailure" ||
403 LookupTag(Message
,"FailReason") == "ConnectionRefused") {
404 Item::Failed(Message
,Cnf
);
408 // Delete any existing sigfile when the acquire failed
409 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
410 unlink(Final
.c_str());
412 // queue a pkgAcqMetaIndex with no sigfile
413 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
414 "", IndexTargets
, MetaIndexParser
);
416 if (Cnf
->LocalOnly
== true ||
417 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
426 Item::Failed(Message
,Cnf
);
429 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
430 string URI
,string URIDesc
,string ShortDesc
,
432 const vector
<struct IndexTarget
*>* IndexTargets
,
433 indexRecords
* MetaIndexParser
) :
434 Item(Owner
), RealURI(URI
), SigFile(SigFile
), AuthPass(false),
435 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
), IMSHit(false)
437 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
438 DestFile
+= URItoFileName(URI
);
441 Desc
.Description
= URIDesc
;
443 Desc
.ShortDesc
= ShortDesc
;
450 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
451 // ---------------------------------------------------------------------
452 /* The only header we use is the last-modified header. */
453 string
pkgAcqMetaIndex::Custom600Headers()
455 string Final
= _config
->FindDir("Dir::State::lists");
456 Final
+= URItoFileName(RealURI
);
459 if (stat(Final
.c_str(),&Buf
) != 0)
460 return "\nIndex-File: true";
462 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
465 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
466 pkgAcquire::MethodConfig
*Cfg
)
468 Item::Done(Message
,Size
,MD5
,Cfg
);
470 // MetaIndexes are done in two passes: one to download the
471 // metaindex with an appropriate method, and a second to verify it
472 // with the gpgv method
474 if (AuthPass
== true)
480 RetrievalDone(Message
);
482 // Still more retrieving to do
487 // There was no signature file, so we are finished. Download
488 // the indexes without verification.
493 // There was a signature file, so pass it to gpgv for
496 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
497 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
498 << SigFile
<< "," << DestFile
<< ")\n";
500 Desc
.URI
= "gpgv:" + SigFile
;
507 void pkgAcqMetaIndex::RetrievalDone(string Message
)
509 // We have just finished downloading a Release file (it is not
512 string FileName
= LookupTag(Message
,"Filename");
513 if (FileName
.empty() == true)
516 ErrorText
= "Method gave a blank filename";
520 if (FileName
!= DestFile
)
523 Desc
.URI
= "copy:" + FileName
;
528 // see if the download was a IMSHit
529 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
533 string FinalFile
= _config
->FindDir("Dir::State::lists");
534 FinalFile
+= URItoFileName(RealURI
);
536 // The files timestamp matches
537 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
539 // Move it into position
540 Rename(DestFile
,FinalFile
);
542 DestFile
= FinalFile
;
545 void pkgAcqMetaIndex::AuthDone(string Message
)
547 // At this point, the gpgv method has succeeded, so there is a
548 // valid signature from a key in the trusted keyring. We
549 // perform additional verification of its contents, and use them
550 // to verify the indexes we are about to download
552 if (!MetaIndexParser
->Load(DestFile
))
554 Status
= StatAuthError
;
555 ErrorText
= MetaIndexParser
->ErrorText
;
559 if (!VerifyVendor(Message
))
564 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
565 std::cerr
<< "Signature verification succeeded: "
566 << DestFile
<< std::endl
;
568 // Download further indexes with verification
571 // Done, move signature file into position
573 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
574 URItoFileName(RealURI
) + ".gpg";
575 Rename(SigFile
,VerifiedSigFile
);
576 chmod(VerifiedSigFile
.c_str(),0644);
579 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
581 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
582 Target
!= IndexTargets
->end();
585 string ExpectedIndexMD5
;
588 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
591 Status
= StatAuthError
;
592 ErrorText
= "Unable to find expected entry "
593 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
596 ExpectedIndexMD5
= Record
->MD5Hash
;
597 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
599 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
600 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
602 if (ExpectedIndexMD5
.empty())
604 Status
= StatAuthError
;
605 ErrorText
= "Unable to find MD5 sum for "
606 + (*Target
)->MetaKey
+ " in Meta-index file";
611 // Queue Packages file
612 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
613 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
617 bool pkgAcqMetaIndex::VerifyVendor(string Message
)
619 // // Maybe this should be made available from above so we don't have
620 // // to read and parse it every time?
621 // pkgVendorList List;
622 // List.ReadMainList();
624 // const Vendor* Vndr = NULL;
625 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
627 // string::size_type pos = (*I).find("VALIDSIG ");
628 // if (_config->FindB("Debug::Vendor", false))
629 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
631 // if (pos != std::string::npos)
633 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
634 // if (_config->FindB("Debug::Vendor", false))
635 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
637 // Vndr = List.FindVendor(Fingerprint) != "";
638 // if (Vndr != NULL);
642 string::size_type pos
;
644 // check for missing sigs (that where not fatal because otherwise we had
647 string msg
= _("There is no public key available for the "
648 "following key IDs:\n");
649 pos
= Message
.find("NO_PUBKEY ");
650 if (pos
!= std::string::npos
)
652 string::size_type start
= pos
+strlen("NO_PUBKEY ");
653 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
654 missingkeys
+= (Fingerprint
);
656 if(!missingkeys
.empty())
657 _error
->Warning("%s", string(msg
+missingkeys
).c_str());
659 string Transformed
= MetaIndexParser
->GetExpectedDist();
661 if (Transformed
== "../project/experimental")
663 Transformed
= "experimental";
666 pos
= Transformed
.rfind('/');
667 if (pos
!= string::npos
)
669 Transformed
= Transformed
.substr(0, pos
);
672 if (Transformed
== ".")
677 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
679 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
680 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
681 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
684 if (MetaIndexParser
->CheckDist(Transformed
) == false)
686 // This might become fatal one day
687 // Status = StatAuthError;
688 // ErrorText = "Conflicting distribution; expected "
689 // + MetaIndexParser->GetExpectedDist() + " but got "
690 // + MetaIndexParser->GetDist();
692 if (!Transformed
.empty())
694 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
695 Desc
.Description
.c_str(),
697 MetaIndexParser
->GetDist().c_str());
704 // pkgAcqMetaIndex::Failed - no Release file present or no signature
705 // file present /*{{{*/
706 // ---------------------------------------------------------------------
708 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
710 if (AuthPass
== true)
712 // if we fail the authentication but got the file via a IMS-Hit
713 // this means that the file wasn't downloaded and that it might be
714 // just stale (server problem, proxy etc). we delete what we have
715 // queue it again without i-m-s
716 // alternatively we could just unlink the file and let the user try again
722 unlink(DestFile
.c_str());
724 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
725 DestFile
+= URItoFileName(RealURI
);
731 // gpgv method failed
732 _error
->Warning("GPG error: %s: %s",
733 Desc
.Description
.c_str(),
734 LookupTag(Message
,"Message").c_str());
738 // No Release file was present, or verification failed, so fall
739 // back to queueing Packages files without verification
745 // AcqArchive::AcqArchive - Constructor /*{{{*/
746 // ---------------------------------------------------------------------
747 /* This just sets up the initial fetch environment and queues the first
749 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
750 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
751 string
&StoreFilename
) :
752 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
753 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
756 Retries
= _config
->FindI("Acquire::Retries",0);
758 if (Version
.Arch() == 0)
760 _error
->Error(_("I wasn't able to locate a file for the %s package. "
761 "This might mean you need to manually fix this package. "
762 "(due to missing arch)"),
763 Version
.ParentPkg().Name());
767 /* We need to find a filename to determine the extension. We make the
768 assumption here that all the available sources for this version share
769 the same extension.. */
770 // Skip not source sources, they do not have file fields.
771 for (; Vf
.end() == false; Vf
++)
773 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
778 // Does not really matter here.. we are going to fail out below
779 if (Vf
.end() != true)
781 // If this fails to get a file name we will bomb out below.
782 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
783 if (_error
->PendingError() == true)
786 // Generate the final file name as: package_version_arch.foo
787 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
788 QuoteString(Version
.VerStr(),"_:") + '_' +
789 QuoteString(Version
.Arch(),"_:.") +
790 "." + flExtension(Parse
.FileName());
793 // check if we have one trusted source for the package. if so, switch
794 // to "TrustedOnly" mode
795 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
798 if (Sources
->FindIndex(i
.File(),Index
) == false)
800 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
802 std::cerr
<< "Checking index: " << Index
->Describe()
803 << "(Trusted=" << Index
->IsTrusted() << ")\n";
805 if (Index
->IsTrusted()) {
811 // "allow-unauthenticated" restores apts old fetching behaviour
812 // that means that e.g. unauthenticated file:// uris are higher
813 // priority than authenticated http:// uris
814 if (_config
->FindB("APT::Get::AllowUnauthenticated",false) == true)
818 if (QueueNext() == false && _error
->PendingError() == false)
819 _error
->Error(_("I wasn't able to locate file for the %s package. "
820 "This might mean you need to manually fix this package."),
821 Version
.ParentPkg().Name());
824 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
825 // ---------------------------------------------------------------------
826 /* This queues the next available file version for download. It checks if
827 the archive is already available in the cache and stashs the MD5 for
829 bool pkgAcqArchive::QueueNext()
831 for (; Vf
.end() == false; Vf
++)
833 // Ignore not source sources
834 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
837 // Try to cross match against the source list
839 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
842 // only try to get a trusted package from another source if that source
844 if(Trusted
&& !Index
->IsTrusted())
847 // Grab the text package record
848 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
849 if (_error
->PendingError() == true)
852 string PkgFile
= Parse
.FileName();
853 MD5
= Parse
.MD5Hash();
854 if (PkgFile
.empty() == true)
855 return _error
->Error(_("The package index files are corrupted. No Filename: "
856 "field for package %s."),
857 Version
.ParentPkg().Name());
859 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
860 Desc
.Description
= Index
->ArchiveInfo(Version
);
862 Desc
.ShortDesc
= Version
.ParentPkg().Name();
864 // See if we already have the file. (Legacy filenames)
865 FileSize
= Version
->Size
;
866 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
868 if (stat(FinalFile
.c_str(),&Buf
) == 0)
870 // Make sure the size matches
871 if ((unsigned)Buf
.st_size
== Version
->Size
)
876 StoreFilename
= DestFile
= FinalFile
;
880 /* Hmm, we have a file and its size does not match, this means it is
881 an old style mismatched arch */
882 unlink(FinalFile
.c_str());
885 // Check it again using the new style output filenames
886 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
887 if (stat(FinalFile
.c_str(),&Buf
) == 0)
889 // Make sure the size matches
890 if ((unsigned)Buf
.st_size
== Version
->Size
)
895 StoreFilename
= DestFile
= FinalFile
;
899 /* Hmm, we have a file and its size does not match, this shouldnt
901 unlink(FinalFile
.c_str());
904 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
906 // Check the destination file
907 if (stat(DestFile
.c_str(),&Buf
) == 0)
909 // Hmm, the partial file is too big, erase it
910 if ((unsigned)Buf
.st_size
> Version
->Size
)
911 unlink(DestFile
.c_str());
913 PartialSize
= Buf
.st_size
;
918 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
919 Desc
.Description
= Index
->ArchiveInfo(Version
);
921 Desc
.ShortDesc
= Version
.ParentPkg().Name();
930 // AcqArchive::Done - Finished fetching /*{{{*/
931 // ---------------------------------------------------------------------
933 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
934 pkgAcquire::MethodConfig
*Cfg
)
936 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
939 if (Size
!= Version
->Size
)
942 ErrorText
= _("Size mismatch");
947 if (Md5Hash
.empty() == false && MD5
.empty() == false)
952 ErrorText
= _("MD5Sum mismatch");
953 if(FileExists(DestFile
))
954 Rename(DestFile
,DestFile
+ ".FAILED");
959 // Grab the output filename
960 string FileName
= LookupTag(Message
,"Filename");
961 if (FileName
.empty() == true)
964 ErrorText
= "Method gave a blank filename";
970 // Reference filename
971 if (FileName
!= DestFile
)
973 StoreFilename
= DestFile
= FileName
;
978 // Done, move it into position
979 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
980 FinalFile
+= flNotDir(StoreFilename
);
981 Rename(DestFile
,FinalFile
);
983 StoreFilename
= DestFile
= FinalFile
;
987 // AcqArchive::Failed - Failure handler /*{{{*/
988 // ---------------------------------------------------------------------
989 /* Here we try other sources */
990 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
992 ErrorText
= LookupTag(Message
,"Message");
994 /* We don't really want to retry on failed media swaps, this prevents
995 that. An interesting observation is that permanent failures are not
997 if (Cnf
->Removable
== true &&
998 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1000 // Vf = Version.FileList();
1001 while (Vf
.end() == false) Vf
++;
1002 StoreFilename
= string();
1003 Item::Failed(Message
,Cnf
);
1007 if (QueueNext() == false)
1009 // This is the retry counter
1011 Cnf
->LocalOnly
== false &&
1012 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1015 Vf
= Version
.FileList();
1016 if (QueueNext() == true)
1020 StoreFilename
= string();
1021 Item::Failed(Message
,Cnf
);
1025 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1026 // trusted source /*{{{*/
1027 // ---------------------------------------------------------------------
1028 bool pkgAcqArchive::IsTrusted()
1033 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1034 // ---------------------------------------------------------------------
1036 void pkgAcqArchive::Finished()
1038 if (Status
== pkgAcquire::Item::StatDone
&&
1041 StoreFilename
= string();
1045 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1046 // ---------------------------------------------------------------------
1047 /* The file is added to the queue */
1048 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1049 unsigned long Size
,string Dsc
,string ShortDesc
,
1050 const string
&DestDir
, const string
&DestFilename
) :
1051 Item(Owner
), Md5Hash(MD5
)
1053 Retries
= _config
->FindI("Acquire::Retries",0);
1055 if(!DestFilename
.empty())
1056 DestFile
= DestFilename
;
1057 else if(!DestDir
.empty())
1058 DestFile
= DestDir
+ "/" + flNotDir(URI
);
1060 DestFile
= flNotDir(URI
);
1064 Desc
.Description
= Dsc
;
1067 // Set the short description to the archive component
1068 Desc
.ShortDesc
= ShortDesc
;
1070 // Get the transfer sizes
1073 if (stat(DestFile
.c_str(),&Buf
) == 0)
1075 // Hmm, the partial file is too big, erase it
1076 if ((unsigned)Buf
.st_size
> Size
)
1077 unlink(DestFile
.c_str());
1079 PartialSize
= Buf
.st_size
;
1085 // AcqFile::Done - Item downloaded OK /*{{{*/
1086 // ---------------------------------------------------------------------
1088 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1089 pkgAcquire::MethodConfig
*Cnf
)
1092 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1097 ErrorText
= "MD5Sum mismatch";
1098 Rename(DestFile
,DestFile
+ ".FAILED");
1103 Item::Done(Message
,Size
,MD5
,Cnf
);
1105 string FileName
= LookupTag(Message
,"Filename");
1106 if (FileName
.empty() == true)
1109 ErrorText
= "Method gave a blank filename";
1115 // The files timestamp matches
1116 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1119 // We have to copy it into place
1120 if (FileName
!= DestFile
)
1123 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1124 Cnf
->Removable
== true)
1126 Desc
.URI
= "copy:" + FileName
;
1131 // Erase the file if it is a symlink so we can overwrite it
1133 if (lstat(DestFile
.c_str(),&St
) == 0)
1135 if (S_ISLNK(St
.st_mode
) != 0)
1136 unlink(DestFile
.c_str());
1140 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1142 ErrorText
= "Link to " + DestFile
+ " failure ";
1149 // AcqFile::Failed - Failure handler /*{{{*/
1150 // ---------------------------------------------------------------------
1151 /* Here we try other sources */
1152 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1154 ErrorText
= LookupTag(Message
,"Message");
1156 // This is the retry counter
1158 Cnf
->LocalOnly
== false &&
1159 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1166 Item::Failed(Message
,Cnf
);