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
);
324 // remove any partial downloaded sig-file. it may confuse proxies
325 // and is too small to warrant a partial download anyway
326 unlink(DestFile
.c_str());
329 Desc
.Description
= URIDesc
;
331 Desc
.ShortDesc
= ShortDesc
;
335 string Final
= _config
->FindDir("Dir::State::lists");
336 Final
+= URItoFileName(RealURI
);
338 if (stat(Final
.c_str(),&Buf
) == 0)
340 // File was already in place. It needs to be re-verified
341 // because Release might have changed, so Move it into partial
342 Rename(Final
,DestFile
);
348 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
349 // ---------------------------------------------------------------------
350 /* The only header we use is the last-modified header. */
351 string
pkgAcqMetaSig::Custom600Headers()
354 if (stat(DestFile
.c_str(),&Buf
) != 0)
355 return "\nIndex-File: true";
357 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
360 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
361 pkgAcquire::MethodConfig
*Cfg
)
363 Item::Done(Message
,Size
,MD5
,Cfg
);
365 string FileName
= LookupTag(Message
,"Filename");
366 if (FileName
.empty() == true)
369 ErrorText
= "Method gave a blank filename";
373 if (FileName
!= DestFile
)
375 // We have to copy it into place
377 Desc
.URI
= "copy:" + FileName
;
384 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
385 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
386 DestFile
, IndexTargets
, MetaIndexParser
);
390 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
392 // Delete any existing sigfile, so that this source isn't
393 // mistakenly trusted
394 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
395 unlink(Final
.c_str());
397 // queue a pkgAcqMetaIndex with no sigfile
398 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
399 "", IndexTargets
, MetaIndexParser
);
401 if (Cnf
->LocalOnly
== true ||
402 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
411 Item::Failed(Message
,Cnf
);
414 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
415 string URI
,string URIDesc
,string ShortDesc
,
417 const vector
<struct IndexTarget
*>* IndexTargets
,
418 indexRecords
* MetaIndexParser
) :
419 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
421 this->AuthPass
= false;
422 this->MetaIndexParser
= MetaIndexParser
;
423 this->IndexTargets
= IndexTargets
;
424 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
425 DestFile
+= URItoFileName(URI
);
428 Desc
.Description
= URIDesc
;
430 Desc
.ShortDesc
= ShortDesc
;
437 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
438 // ---------------------------------------------------------------------
439 /* The only header we use is the last-modified header. */
440 string
pkgAcqMetaIndex::Custom600Headers()
442 string Final
= _config
->FindDir("Dir::State::lists");
443 Final
+= URItoFileName(RealURI
);
446 if (stat(Final
.c_str(),&Buf
) != 0)
447 return "\nIndex-File: true";
449 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
452 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
453 pkgAcquire::MethodConfig
*Cfg
)
455 Item::Done(Message
,Size
,MD5
,Cfg
);
457 // MetaIndexes are done in two passes: one to download the
458 // metaindex with an appropriate method, and a second to verify it
459 // with the gpgv method
461 if (AuthPass
== true)
467 RetrievalDone(Message
);
469 // Still more retrieving to do
474 // There was no signature file, so we are finished. Download
475 // the indexes without verification.
480 // There was a signature file, so pass it to gpgv for
483 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
484 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
485 << SigFile
<< "," << DestFile
<< ")\n";
487 Desc
.URI
= "gpgv:" + SigFile
;
494 void pkgAcqMetaIndex::RetrievalDone(string Message
)
496 // We have just finished downloading a Release file (it is not
499 string FileName
= LookupTag(Message
,"Filename");
500 if (FileName
.empty() == true)
503 ErrorText
= "Method gave a blank filename";
507 if (FileName
!= DestFile
)
510 Desc
.URI
= "copy:" + FileName
;
517 string FinalFile
= _config
->FindDir("Dir::State::lists");
518 FinalFile
+= URItoFileName(RealURI
);
520 // The files timestamp matches
521 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
523 // Move it into position
524 Rename(DestFile
,FinalFile
);
526 DestFile
= FinalFile
;
529 void pkgAcqMetaIndex::AuthDone(string Message
)
531 // At this point, the gpgv method has succeeded, so there is a
532 // valid signature from a key in the trusted keyring. We
533 // perform additional verification of its contents, and use them
534 // to verify the indexes we are about to download
536 if (!MetaIndexParser
->Load(DestFile
))
538 Status
= StatAuthError
;
539 ErrorText
= MetaIndexParser
->ErrorText
;
548 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
549 std::cerr
<< "Signature verification succeeded: "
550 << DestFile
<< std::endl
;
552 // Download further indexes with verification
555 // Done, move signature file into position
557 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
558 URItoFileName(RealURI
) + ".gpg";
559 Rename(SigFile
,VerifiedSigFile
);
560 chmod(VerifiedSigFile
.c_str(),0644);
563 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
565 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
566 Target
!= IndexTargets
->end();
569 string ExpectedIndexMD5
;
572 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
575 Status
= StatAuthError
;
576 ErrorText
= "Unable to find expected entry "
577 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
580 ExpectedIndexMD5
= Record
->MD5Hash
;
581 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
583 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
584 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
586 if (ExpectedIndexMD5
.empty())
588 Status
= StatAuthError
;
589 ErrorText
= "Unable to find MD5 sum for "
590 + (*Target
)->MetaKey
+ " in Meta-index file";
595 // Queue Packages file
596 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
597 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
601 bool pkgAcqMetaIndex::VerifyVendor()
603 // // Maybe this should be made available from above so we don't have
604 // // to read and parse it every time?
605 // pkgVendorList List;
606 // List.ReadMainList();
608 // const Vendor* Vndr = NULL;
609 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
611 // string::size_type pos = (*I).find("VALIDSIG ");
612 // if (_config->FindB("Debug::Vendor", false))
613 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
615 // if (pos != std::string::npos)
617 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
618 // if (_config->FindB("Debug::Vendor", false))
619 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
621 // Vndr = List.FindVendor(Fingerprint) != "";
622 // if (Vndr != NULL);
627 string Transformed
= MetaIndexParser
->GetExpectedDist();
629 if (Transformed
== "../project/experimental")
631 Transformed
= "experimental";
634 string::size_type pos
= Transformed
.rfind('/');
635 if (pos
!= string::npos
)
637 Transformed
= Transformed
.substr(0, pos
);
640 if (Transformed
== ".")
645 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
647 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
648 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
649 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
652 if (MetaIndexParser
->CheckDist(Transformed
) == false)
654 // This might become fatal one day
655 // Status = StatAuthError;
656 // ErrorText = "Conflicting distribution; expected "
657 // + MetaIndexParser->GetExpectedDist() + " but got "
658 // + MetaIndexParser->GetDist();
660 if (!Transformed
.empty())
662 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
663 Desc
.Description
.c_str(),
665 MetaIndexParser
->GetDist().c_str());
672 // pkgAcqMetaIndex::Failed - no Release file present or no signature
673 // file present /*{{{*/
674 // ---------------------------------------------------------------------
676 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
678 if (AuthPass
== true)
680 // gpgv method failed
681 _error
->Warning("GPG error: %s: %s",
682 Desc
.Description
.c_str(),
683 LookupTag(Message
,"Message").c_str());
686 // No Release file was present, or verification failed, so fall
687 // back to queueing Packages files without verification
693 // AcqArchive::AcqArchive - Constructor /*{{{*/
694 // ---------------------------------------------------------------------
695 /* This just sets up the initial fetch environment and queues the first
697 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
698 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
699 string
&StoreFilename
) :
700 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
701 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
704 Retries
= _config
->FindI("Acquire::Retries",0);
706 if (Version
.Arch() == 0)
708 _error
->Error(_("I wasn't able to locate a file for the %s package. "
709 "This might mean you need to manually fix this package. "
710 "(due to missing arch)"),
711 Version
.ParentPkg().Name());
715 /* We need to find a filename to determine the extension. We make the
716 assumption here that all the available sources for this version share
717 the same extension.. */
718 // Skip not source sources, they do not have file fields.
719 for (; Vf
.end() == false; Vf
++)
721 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
726 // Does not really matter here.. we are going to fail out below
727 if (Vf
.end() != true)
729 // If this fails to get a file name we will bomb out below.
730 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
731 if (_error
->PendingError() == true)
734 // Generate the final file name as: package_version_arch.foo
735 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
736 QuoteString(Version
.VerStr(),"_:") + '_' +
737 QuoteString(Version
.Arch(),"_:.") +
738 "." + flExtension(Parse
.FileName());
741 // check if we have one trusted source for the package. if so, switch
742 // to "TrustedOnly" mode
743 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
746 if (Sources
->FindIndex(i
.File(),Index
) == false)
748 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
750 std::cerr
<< "Checking index: " << Index
->Describe()
751 << "(Trusted=" << Index
->IsTrusted() << ")\n";
753 if (Index
->IsTrusted()) {
760 if (QueueNext() == false && _error
->PendingError() == false)
761 _error
->Error(_("I wasn't able to locate file for the %s package. "
762 "This might mean you need to manually fix this package."),
763 Version
.ParentPkg().Name());
766 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
767 // ---------------------------------------------------------------------
768 /* This queues the next available file version for download. It checks if
769 the archive is already available in the cache and stashs the MD5 for
771 bool pkgAcqArchive::QueueNext()
773 for (; Vf
.end() == false; Vf
++)
775 // Ignore not source sources
776 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
779 // Try to cross match against the source list
781 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
784 // only try to get a trusted package from another source if that source
786 if(Trusted
&& !Index
->IsTrusted())
789 // Grab the text package record
790 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
791 if (_error
->PendingError() == true)
794 string PkgFile
= Parse
.FileName();
795 MD5
= Parse
.MD5Hash();
796 if (PkgFile
.empty() == true)
797 return _error
->Error(_("The package index files are corrupted. No Filename: "
798 "field for package %s."),
799 Version
.ParentPkg().Name());
801 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
802 Desc
.Description
= Index
->ArchiveInfo(Version
);
804 Desc
.ShortDesc
= Version
.ParentPkg().Name();
806 // See if we already have the file. (Legacy filenames)
807 FileSize
= Version
->Size
;
808 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
810 if (stat(FinalFile
.c_str(),&Buf
) == 0)
812 // Make sure the size matches
813 if ((unsigned)Buf
.st_size
== Version
->Size
)
818 StoreFilename
= DestFile
= FinalFile
;
822 /* Hmm, we have a file and its size does not match, this means it is
823 an old style mismatched arch */
824 unlink(FinalFile
.c_str());
827 // Check it again using the new style output filenames
828 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
829 if (stat(FinalFile
.c_str(),&Buf
) == 0)
831 // Make sure the size matches
832 if ((unsigned)Buf
.st_size
== Version
->Size
)
837 StoreFilename
= DestFile
= FinalFile
;
841 /* Hmm, we have a file and its size does not match, this shouldnt
843 unlink(FinalFile
.c_str());
846 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
848 // Check the destination file
849 if (stat(DestFile
.c_str(),&Buf
) == 0)
851 // Hmm, the partial file is too big, erase it
852 if ((unsigned)Buf
.st_size
> Version
->Size
)
853 unlink(DestFile
.c_str());
855 PartialSize
= Buf
.st_size
;
860 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
861 Desc
.Description
= Index
->ArchiveInfo(Version
);
863 Desc
.ShortDesc
= Version
.ParentPkg().Name();
872 // AcqArchive::Done - Finished fetching /*{{{*/
873 // ---------------------------------------------------------------------
875 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
876 pkgAcquire::MethodConfig
*Cfg
)
878 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
881 if (Size
!= Version
->Size
)
884 ErrorText
= _("Size mismatch");
889 if (Md5Hash
.empty() == false && MD5
.empty() == false)
894 ErrorText
= _("MD5Sum mismatch");
895 Rename(DestFile
,DestFile
+ ".FAILED");
900 // Grab the output filename
901 string FileName
= LookupTag(Message
,"Filename");
902 if (FileName
.empty() == true)
905 ErrorText
= "Method gave a blank filename";
911 // Reference filename
912 if (FileName
!= DestFile
)
914 StoreFilename
= DestFile
= FileName
;
919 // Done, move it into position
920 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
921 FinalFile
+= flNotDir(StoreFilename
);
922 Rename(DestFile
,FinalFile
);
924 StoreFilename
= DestFile
= FinalFile
;
928 // AcqArchive::Failed - Failure handler /*{{{*/
929 // ---------------------------------------------------------------------
930 /* Here we try other sources */
931 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
933 ErrorText
= LookupTag(Message
,"Message");
935 /* We don't really want to retry on failed media swaps, this prevents
936 that. An interesting observation is that permanent failures are not
938 if (Cnf
->Removable
== true &&
939 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
941 // Vf = Version.FileList();
942 while (Vf
.end() == false) Vf
++;
943 StoreFilename
= string();
944 Item::Failed(Message
,Cnf
);
948 if (QueueNext() == false)
950 // This is the retry counter
952 Cnf
->LocalOnly
== false &&
953 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
956 Vf
= Version
.FileList();
957 if (QueueNext() == true)
961 StoreFilename
= string();
962 Item::Failed(Message
,Cnf
);
966 // AcqArchive::IsTrusted - Determine whether this archive comes from a
967 // trusted source /*{{{*/
968 // ---------------------------------------------------------------------
969 bool pkgAcqArchive::IsTrusted()
974 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
975 // ---------------------------------------------------------------------
977 void pkgAcqArchive::Finished()
979 if (Status
== pkgAcquire::Item::StatDone
&&
982 StoreFilename
= string();
986 // AcqFile::pkgAcqFile - Constructor /*{{{*/
987 // ---------------------------------------------------------------------
988 /* The file is added to the queue */
989 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
990 unsigned long Size
,string Dsc
,string ShortDesc
) :
991 Item(Owner
), Md5Hash(MD5
)
993 Retries
= _config
->FindI("Acquire::Retries",0);
995 DestFile
= flNotDir(URI
);
999 Desc
.Description
= Dsc
;
1002 // Set the short description to the archive component
1003 Desc
.ShortDesc
= ShortDesc
;
1005 // Get the transfer sizes
1008 if (stat(DestFile
.c_str(),&Buf
) == 0)
1010 // Hmm, the partial file is too big, erase it
1011 if ((unsigned)Buf
.st_size
> Size
)
1012 unlink(DestFile
.c_str());
1014 PartialSize
= Buf
.st_size
;
1020 // AcqFile::Done - Item downloaded OK /*{{{*/
1021 // ---------------------------------------------------------------------
1023 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1024 pkgAcquire::MethodConfig
*Cnf
)
1027 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1032 ErrorText
= "MD5Sum mismatch";
1033 Rename(DestFile
,DestFile
+ ".FAILED");
1038 Item::Done(Message
,Size
,MD5
,Cnf
);
1040 string FileName
= LookupTag(Message
,"Filename");
1041 if (FileName
.empty() == true)
1044 ErrorText
= "Method gave a blank filename";
1050 // The files timestamp matches
1051 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1054 // We have to copy it into place
1055 if (FileName
!= DestFile
)
1058 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1059 Cnf
->Removable
== true)
1061 Desc
.URI
= "copy:" + FileName
;
1066 // Erase the file if it is a symlink so we can overwrite it
1068 if (lstat(DestFile
.c_str(),&St
) == 0)
1070 if (S_ISLNK(St
.st_mode
) != 0)
1071 unlink(DestFile
.c_str());
1075 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1077 ErrorText
= "Link to " + DestFile
+ " failure ";
1084 // AcqFile::Failed - Failure handler /*{{{*/
1085 // ---------------------------------------------------------------------
1086 /* Here we try other sources */
1087 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1089 ErrorText
= LookupTag(Message
,"Message");
1091 // This is the retry counter
1093 Cnf
->LocalOnly
== false &&
1094 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1101 Item::Failed(Message
,Cnf
);