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
);
152 if(FileExists("/usr/bin/bzip2"))
153 Desc
.URI
= URI
+ ".bz2";
155 Desc
.URI
= URI
+ ".gz";
157 Desc
.URI
= URI
+ comprExt
;
160 Desc
.Description
= URIDesc
;
162 Desc
.ShortDesc
= ShortDesc
;
167 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
168 // ---------------------------------------------------------------------
169 /* The only header we use is the last-modified header. */
170 string
pkgAcqIndex::Custom600Headers()
172 string Final
= _config
->FindDir("Dir::State::lists");
173 Final
+= URItoFileName(RealURI
);
176 if (stat(Final
.c_str(),&Buf
) != 0)
177 return "\nIndex-File: true";
179 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
183 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
185 // no .bz2 found, retry with .gz
186 if(Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1) == "bz2") {
187 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
189 // retry with a gzip one
190 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
191 ExpectedMD5
, string(".gz"));
199 Item::Failed(Message
,Cnf
);
203 // AcqIndex::Done - Finished a fetch /*{{{*/
204 // ---------------------------------------------------------------------
205 /* This goes through a number of states.. On the initial fetch the
206 method could possibly return an alternate filename which points
207 to the uncompressed version of the file. If this is so the file
208 is copied into the partial directory. In all other cases the file
209 is decompressed with a gzip uri. */
210 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
211 pkgAcquire::MethodConfig
*Cfg
)
213 Item::Done(Message
,Size
,MD5
,Cfg
);
215 if (Decompression
== true)
217 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
219 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
220 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
226 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
227 sum
.AddFD(Fd
.Fd(), Fd
.Size());
229 MD5
= (string
)sum
.Result();
232 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
234 Status
= StatAuthError
;
235 ErrorText
= _("MD5Sum mismatch");
236 Rename(DestFile
,DestFile
+ ".FAILED");
239 // Done, move it into position
240 string FinalFile
= _config
->FindDir("Dir::State::lists");
241 FinalFile
+= URItoFileName(RealURI
);
242 Rename(DestFile
,FinalFile
);
243 chmod(FinalFile
.c_str(),0644);
245 /* We restore the original name to DestFile so that the clean operation
247 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
248 DestFile
+= URItoFileName(RealURI
);
250 // Remove the compressed version.
252 unlink(DestFile
.c_str());
259 // Handle the unzipd case
260 string FileName
= LookupTag(Message
,"Alt-Filename");
261 if (FileName
.empty() == false)
263 // The files timestamp matches
264 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
267 Decompression
= true;
269 DestFile
+= ".decomp";
270 Desc
.URI
= "copy:" + FileName
;
276 FileName
= LookupTag(Message
,"Filename");
277 if (FileName
.empty() == true)
280 ErrorText
= "Method gave a blank filename";
283 // The files timestamp matches
284 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
287 if (FileName
== DestFile
)
292 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3,Desc
.URI
.size()-1);
295 decompProg
= "bzip2";
296 else if(compExt
== ".gz")
299 _error
->Error("Unsupported extension: %s", compExt
.c_str());
303 Decompression
= true;
304 DestFile
+= ".decomp";
305 Desc
.URI
= string(decompProg
) + ":" + FileName
;
310 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
311 string URI
,string URIDesc
,string ShortDesc
,
312 string MetaIndexURI
, string MetaIndexURIDesc
,
313 string MetaIndexShortDesc
,
314 const vector
<IndexTarget
*>* IndexTargets
,
315 indexRecords
* MetaIndexParser
) :
316 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
317 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
)
319 this->MetaIndexParser
= MetaIndexParser
;
320 this->IndexTargets
= IndexTargets
;
321 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
322 DestFile
+= URItoFileName(URI
);
325 Desc
.Description
= URIDesc
;
327 Desc
.ShortDesc
= ShortDesc
;
331 string Final
= _config
->FindDir("Dir::State::lists");
332 Final
+= URItoFileName(RealURI
);
334 if (stat(Final
.c_str(),&Buf
) == 0)
336 // File was already in place. It needs to be re-verified
337 // because Release might have changed, so Move it into partial
338 Rename(Final
,DestFile
);
344 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
345 // ---------------------------------------------------------------------
346 /* The only header we use is the last-modified header. */
347 string
pkgAcqMetaSig::Custom600Headers()
349 string Final
= _config
->FindDir("Dir::State::lists");
350 Final
+= URItoFileName(RealURI
);
353 if (stat(Final
.c_str(),&Buf
) != 0)
354 return "\nIndex-File: true";
356 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
359 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
360 pkgAcquire::MethodConfig
*Cfg
)
362 Item::Done(Message
,Size
,MD5
,Cfg
);
364 string FileName
= LookupTag(Message
,"Filename");
365 if (FileName
.empty() == true)
368 ErrorText
= "Method gave a blank filename";
372 if (FileName
!= DestFile
)
374 // We have to copy it into place
376 Desc
.URI
= "copy:" + FileName
;
383 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
384 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
385 DestFile
, IndexTargets
, MetaIndexParser
);
389 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
391 // Delete any existing sigfile, so that this source isn't
392 // mistakenly trusted
393 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
394 unlink(Final
.c_str());
395 Final
= _config
->FindDir("Dir::State::lists") + "partial/"+ URItoFileName(RealURI
);
396 unlink(Final
.c_str());
399 // queue a pkgAcqMetaIndex with no sigfile
400 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
401 "", IndexTargets
, MetaIndexParser
);
403 if (Cnf
->LocalOnly
== true ||
404 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
413 Item::Failed(Message
,Cnf
);
416 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
417 string URI
,string URIDesc
,string ShortDesc
,
419 const vector
<struct IndexTarget
*>* IndexTargets
,
420 indexRecords
* MetaIndexParser
) :
421 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
423 this->AuthPass
= false;
424 this->MetaIndexParser
= MetaIndexParser
;
425 this->IndexTargets
= IndexTargets
;
426 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
427 DestFile
+= URItoFileName(URI
);
430 Desc
.Description
= URIDesc
;
432 Desc
.ShortDesc
= ShortDesc
;
439 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
440 // ---------------------------------------------------------------------
441 /* The only header we use is the last-modified header. */
442 string
pkgAcqMetaIndex::Custom600Headers()
444 string Final
= _config
->FindDir("Dir::State::lists");
445 Final
+= URItoFileName(RealURI
);
448 if (stat(Final
.c_str(),&Buf
) != 0)
449 return "\nIndex-File: true";
451 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
454 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
455 pkgAcquire::MethodConfig
*Cfg
)
457 Item::Done(Message
,Size
,MD5
,Cfg
);
459 // MetaIndexes are done in two passes: one to download the
460 // metaindex with an appropriate method, and a second to verify it
461 // with the gpgv method
463 if (AuthPass
== true)
469 RetrievalDone(Message
);
471 // Still more retrieving to do
476 // There was no signature file, so we are finished. Download
477 // the indexes without verification.
482 // There was a signature file, so pass it to gpgv for
485 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
486 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
487 << SigFile
<< "," << DestFile
<< ")\n";
489 Desc
.URI
= "gpgv:" + SigFile
;
496 void pkgAcqMetaIndex::RetrievalDone(string Message
)
498 // We have just finished downloading a Release file (it is not
501 string FileName
= LookupTag(Message
,"Filename");
502 if (FileName
.empty() == true)
505 ErrorText
= "Method gave a blank filename";
509 if (FileName
!= DestFile
)
512 Desc
.URI
= "copy:" + FileName
;
519 string FinalFile
= _config
->FindDir("Dir::State::lists");
520 FinalFile
+= URItoFileName(RealURI
);
522 // The files timestamp matches
523 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
525 // Move it into position
526 Rename(DestFile
,FinalFile
);
528 DestFile
= FinalFile
;
531 void pkgAcqMetaIndex::AuthDone(string Message
)
533 // At this point, the gpgv method has succeeded, so there is a
534 // valid signature from a key in the trusted keyring. We
535 // perform additional verification of its contents, and use them
536 // to verify the indexes we are about to download
538 if (!MetaIndexParser
->Load(DestFile
))
540 Status
= StatAuthError
;
541 ErrorText
= MetaIndexParser
->ErrorText
;
550 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
551 std::cerr
<< "Signature verification succeeded: "
552 << DestFile
<< std::endl
;
554 // Download further indexes with verification
557 // Done, move signature file into position
559 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
560 URItoFileName(RealURI
) + ".gpg";
561 Rename(SigFile
,VerifiedSigFile
);
562 chmod(VerifiedSigFile
.c_str(),0644);
565 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
567 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
568 Target
!= IndexTargets
->end();
571 string ExpectedIndexMD5
;
574 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
577 Status
= StatAuthError
;
578 ErrorText
= "Unable to find expected entry "
579 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
582 ExpectedIndexMD5
= Record
->MD5Hash
;
583 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
585 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
586 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
588 if (ExpectedIndexMD5
.empty())
590 Status
= StatAuthError
;
591 ErrorText
= "Unable to find MD5 sum for "
592 + (*Target
)->MetaKey
+ " in Meta-index file";
597 // Queue Packages file
598 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
599 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
603 bool pkgAcqMetaIndex::VerifyVendor()
605 // // Maybe this should be made available from above so we don't have
606 // // to read and parse it every time?
607 // pkgVendorList List;
608 // List.ReadMainList();
610 // const Vendor* Vndr = NULL;
611 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
613 // string::size_type pos = (*I).find("VALIDSIG ");
614 // if (_config->FindB("Debug::Vendor", false))
615 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
617 // if (pos != std::string::npos)
619 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
620 // if (_config->FindB("Debug::Vendor", false))
621 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
623 // Vndr = List.FindVendor(Fingerprint) != "";
624 // if (Vndr != NULL);
629 string Transformed
= MetaIndexParser
->GetExpectedDist();
631 if (Transformed
== "../project/experimental")
633 Transformed
= "experimental";
636 string::size_type pos
= Transformed
.rfind('/');
637 if (pos
!= string::npos
)
639 Transformed
= Transformed
.substr(0, pos
);
642 if (Transformed
== ".")
647 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
649 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
650 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
651 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
654 if (MetaIndexParser
->CheckDist(Transformed
) == false)
656 // This might become fatal one day
657 // Status = StatAuthError;
658 // ErrorText = "Conflicting distribution; expected "
659 // + MetaIndexParser->GetExpectedDist() + " but got "
660 // + MetaIndexParser->GetDist();
662 if (!Transformed
.empty())
664 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
665 Desc
.Description
.c_str(),
667 MetaIndexParser
->GetDist().c_str());
674 // pkgAcqMetaIndex::Failed - no Release file present or no signature
675 // file present /*{{{*/
676 // ---------------------------------------------------------------------
678 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
680 if (AuthPass
== true)
682 // gpgv method failed
683 _error
->Warning("GPG error: %s: %s",
684 Desc
.Description
.c_str(),
685 LookupTag(Message
,"Message").c_str());
688 // No Release file was present, or verification failed, so fall
689 // back to queueing Packages files without verification
695 // AcqArchive::AcqArchive - Constructor /*{{{*/
696 // ---------------------------------------------------------------------
697 /* This just sets up the initial fetch environment and queues the first
699 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
700 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
701 string
&StoreFilename
) :
702 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
703 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
706 Retries
= _config
->FindI("Acquire::Retries",0);
708 if (Version
.Arch() == 0)
710 _error
->Error(_("I wasn't able to locate a file for the %s package. "
711 "This might mean you need to manually fix this package. "
712 "(due to missing arch)"),
713 Version
.ParentPkg().Name());
717 /* We need to find a filename to determine the extension. We make the
718 assumption here that all the available sources for this version share
719 the same extension.. */
720 // Skip not source sources, they do not have file fields.
721 for (; Vf
.end() == false; Vf
++)
723 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
728 // Does not really matter here.. we are going to fail out below
729 if (Vf
.end() != true)
731 // If this fails to get a file name we will bomb out below.
732 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
733 if (_error
->PendingError() == true)
736 // Generate the final file name as: package_version_arch.foo
737 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
738 QuoteString(Version
.VerStr(),"_:") + '_' +
739 QuoteString(Version
.Arch(),"_:.") +
740 "." + flExtension(Parse
.FileName());
743 // check if we have one trusted source for the package. if so, switch
744 // to "TrustedOnly" mode
745 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
748 if (Sources
->FindIndex(i
.File(),Index
) == false)
750 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
752 std::cerr
<< "Checking index: " << Index
->Describe()
753 << "(Trusted=" << Index
->IsTrusted() << ")\n";
755 if (Index
->IsTrusted()) {
762 if (QueueNext() == false && _error
->PendingError() == false)
763 _error
->Error(_("I wasn't able to locate file for the %s package. "
764 "This might mean you need to manually fix this package."),
765 Version
.ParentPkg().Name());
768 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
769 // ---------------------------------------------------------------------
770 /* This queues the next available file version for download. It checks if
771 the archive is already available in the cache and stashs the MD5 for
773 bool pkgAcqArchive::QueueNext()
775 for (; Vf
.end() == false; Vf
++)
777 // Ignore not source sources
778 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
781 // Try to cross match against the source list
783 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
786 // only try to get a trusted package from another source if that source
788 if(Trusted
&& !Index
->IsTrusted())
791 // Grab the text package record
792 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
793 if (_error
->PendingError() == true)
796 string PkgFile
= Parse
.FileName();
797 MD5
= Parse
.MD5Hash();
798 if (PkgFile
.empty() == true)
799 return _error
->Error(_("The package index files are corrupted. No Filename: "
800 "field for package %s."),
801 Version
.ParentPkg().Name());
803 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
804 Desc
.Description
= Index
->ArchiveInfo(Version
);
806 Desc
.ShortDesc
= Version
.ParentPkg().Name();
808 // See if we already have the file. (Legacy filenames)
809 FileSize
= Version
->Size
;
810 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
812 if (stat(FinalFile
.c_str(),&Buf
) == 0)
814 // Make sure the size matches
815 if ((unsigned)Buf
.st_size
== Version
->Size
)
820 StoreFilename
= DestFile
= FinalFile
;
824 /* Hmm, we have a file and its size does not match, this means it is
825 an old style mismatched arch */
826 unlink(FinalFile
.c_str());
829 // Check it again using the new style output filenames
830 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
831 if (stat(FinalFile
.c_str(),&Buf
) == 0)
833 // Make sure the size matches
834 if ((unsigned)Buf
.st_size
== Version
->Size
)
839 StoreFilename
= DestFile
= FinalFile
;
843 /* Hmm, we have a file and its size does not match, this shouldnt
845 unlink(FinalFile
.c_str());
848 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
850 // Check the destination file
851 if (stat(DestFile
.c_str(),&Buf
) == 0)
853 // Hmm, the partial file is too big, erase it
854 if ((unsigned)Buf
.st_size
> Version
->Size
)
855 unlink(DestFile
.c_str());
857 PartialSize
= Buf
.st_size
;
862 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
863 Desc
.Description
= Index
->ArchiveInfo(Version
);
865 Desc
.ShortDesc
= Version
.ParentPkg().Name();
874 // AcqArchive::Done - Finished fetching /*{{{*/
875 // ---------------------------------------------------------------------
877 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
878 pkgAcquire::MethodConfig
*Cfg
)
880 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
883 if (Size
!= Version
->Size
)
886 ErrorText
= _("Size mismatch");
891 if (Md5Hash
.empty() == false && MD5
.empty() == false)
896 ErrorText
= _("MD5Sum mismatch");
897 Rename(DestFile
,DestFile
+ ".FAILED");
902 // Grab the output filename
903 string FileName
= LookupTag(Message
,"Filename");
904 if (FileName
.empty() == true)
907 ErrorText
= "Method gave a blank filename";
913 // Reference filename
914 if (FileName
!= DestFile
)
916 StoreFilename
= DestFile
= FileName
;
921 // Done, move it into position
922 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
923 FinalFile
+= flNotDir(StoreFilename
);
924 Rename(DestFile
,FinalFile
);
926 StoreFilename
= DestFile
= FinalFile
;
930 // AcqArchive::Failed - Failure handler /*{{{*/
931 // ---------------------------------------------------------------------
932 /* Here we try other sources */
933 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
935 ErrorText
= LookupTag(Message
,"Message");
937 /* We don't really want to retry on failed media swaps, this prevents
938 that. An interesting observation is that permanent failures are not
940 if (Cnf
->Removable
== true &&
941 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
943 // Vf = Version.FileList();
944 while (Vf
.end() == false) Vf
++;
945 StoreFilename
= string();
946 Item::Failed(Message
,Cnf
);
950 if (QueueNext() == false)
952 // This is the retry counter
954 Cnf
->LocalOnly
== false &&
955 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
958 Vf
= Version
.FileList();
959 if (QueueNext() == true)
963 StoreFilename
= string();
964 Item::Failed(Message
,Cnf
);
968 // AcqArchive::IsTrusted - Determine whether this archive comes from a
969 // trusted source /*{{{*/
970 // ---------------------------------------------------------------------
971 bool pkgAcqArchive::IsTrusted()
976 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
977 // ---------------------------------------------------------------------
979 void pkgAcqArchive::Finished()
981 if (Status
== pkgAcquire::Item::StatDone
&&
984 StoreFilename
= string();
988 // AcqFile::pkgAcqFile - Constructor /*{{{*/
989 // ---------------------------------------------------------------------
990 /* The file is added to the queue */
991 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
992 unsigned long Size
,string Dsc
,string ShortDesc
) :
993 Item(Owner
), Md5Hash(MD5
)
995 Retries
= _config
->FindI("Acquire::Retries",0);
997 DestFile
= flNotDir(URI
);
1001 Desc
.Description
= Dsc
;
1004 // Set the short description to the archive component
1005 Desc
.ShortDesc
= ShortDesc
;
1007 // Get the transfer sizes
1010 if (stat(DestFile
.c_str(),&Buf
) == 0)
1012 // Hmm, the partial file is too big, erase it
1013 if ((unsigned)Buf
.st_size
> Size
)
1014 unlink(DestFile
.c_str());
1016 PartialSize
= Buf
.st_size
;
1022 // AcqFile::Done - Item downloaded OK /*{{{*/
1023 // ---------------------------------------------------------------------
1025 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1026 pkgAcquire::MethodConfig
*Cnf
)
1029 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1034 ErrorText
= "MD5Sum mismatch";
1035 Rename(DestFile
,DestFile
+ ".FAILED");
1040 Item::Done(Message
,Size
,MD5
,Cnf
);
1042 string FileName
= LookupTag(Message
,"Filename");
1043 if (FileName
.empty() == true)
1046 ErrorText
= "Method gave a blank filename";
1052 // The files timestamp matches
1053 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1056 // We have to copy it into place
1057 if (FileName
!= DestFile
)
1060 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1061 Cnf
->Removable
== true)
1063 Desc
.URI
= "copy:" + FileName
;
1068 // Erase the file if it is a symlink so we can overwrite it
1070 if (lstat(DestFile
.c_str(),&St
) == 0)
1072 if (S_ISLNK(St
.st_mode
) != 0)
1073 unlink(DestFile
.c_str());
1077 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1079 ErrorText
= "Link to " + DestFile
+ " failure ";
1086 // AcqFile::Failed - Failure handler /*{{{*/
1087 // ---------------------------------------------------------------------
1088 /* Here we try other sources */
1089 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1091 ErrorText
= LookupTag(Message
,"Message");
1093 // This is the retry counter
1095 Cnf
->LocalOnly
== false &&
1096 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1103 Item::Failed(Message
,Cnf
);