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());
396 // if we debug leave the sig-file in partial/ to see what went wrong
398 if (!_config
->FindB("Debug::pkgAcquire::Auth", false)) {
400 Final
= _config
->FindDir("Dir::State::lists") + "partial/"+ URItoFileName(RealURI
);
401 unlink(Final
.c_str());
405 // queue a pkgAcqMetaIndex with no sigfile
406 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
407 "", IndexTargets
, MetaIndexParser
);
409 if (Cnf
->LocalOnly
== true ||
410 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
419 Item::Failed(Message
,Cnf
);
422 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
423 string URI
,string URIDesc
,string ShortDesc
,
425 const vector
<struct IndexTarget
*>* IndexTargets
,
426 indexRecords
* MetaIndexParser
) :
427 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
429 this->AuthPass
= false;
430 this->MetaIndexParser
= MetaIndexParser
;
431 this->IndexTargets
= IndexTargets
;
432 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
433 DestFile
+= URItoFileName(URI
);
436 Desc
.Description
= URIDesc
;
438 Desc
.ShortDesc
= ShortDesc
;
445 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
446 // ---------------------------------------------------------------------
447 /* The only header we use is the last-modified header. */
448 string
pkgAcqMetaIndex::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
);
460 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
461 pkgAcquire::MethodConfig
*Cfg
)
463 Item::Done(Message
,Size
,MD5
,Cfg
);
465 // MetaIndexes are done in two passes: one to download the
466 // metaindex with an appropriate method, and a second to verify it
467 // with the gpgv method
469 if (AuthPass
== true)
475 RetrievalDone(Message
);
477 // Still more retrieving to do
482 // There was no signature file, so we are finished. Download
483 // the indexes without verification.
488 // There was a signature file, so pass it to gpgv for
491 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
492 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
493 << SigFile
<< "," << DestFile
<< ")\n";
495 Desc
.URI
= "gpgv:" + SigFile
;
502 void pkgAcqMetaIndex::RetrievalDone(string Message
)
504 // We have just finished downloading a Release file (it is not
507 string FileName
= LookupTag(Message
,"Filename");
508 if (FileName
.empty() == true)
511 ErrorText
= "Method gave a blank filename";
515 if (FileName
!= DestFile
)
518 Desc
.URI
= "copy:" + FileName
;
525 string FinalFile
= _config
->FindDir("Dir::State::lists");
526 FinalFile
+= URItoFileName(RealURI
);
528 // The files timestamp matches
529 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
531 // Move it into position
532 Rename(DestFile
,FinalFile
);
534 DestFile
= FinalFile
;
537 void pkgAcqMetaIndex::AuthDone(string Message
)
539 // At this point, the gpgv method has succeeded, so there is a
540 // valid signature from a key in the trusted keyring. We
541 // perform additional verification of its contents, and use them
542 // to verify the indexes we are about to download
544 if (!MetaIndexParser
->Load(DestFile
))
546 Status
= StatAuthError
;
547 ErrorText
= MetaIndexParser
->ErrorText
;
556 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
557 std::cerr
<< "Signature verification succeeded: "
558 << DestFile
<< std::endl
;
560 // Download further indexes with verification
563 // Done, move signature file into position
565 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
566 URItoFileName(RealURI
) + ".gpg";
567 Rename(SigFile
,VerifiedSigFile
);
568 chmod(VerifiedSigFile
.c_str(),0644);
571 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
573 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
574 Target
!= IndexTargets
->end();
577 string ExpectedIndexMD5
;
580 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
583 Status
= StatAuthError
;
584 ErrorText
= "Unable to find expected entry "
585 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
588 ExpectedIndexMD5
= Record
->MD5Hash
;
589 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
591 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
592 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
594 if (ExpectedIndexMD5
.empty())
596 Status
= StatAuthError
;
597 ErrorText
= "Unable to find MD5 sum for "
598 + (*Target
)->MetaKey
+ " in Meta-index file";
603 // Queue Packages file
604 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
605 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
609 bool pkgAcqMetaIndex::VerifyVendor()
611 // // Maybe this should be made available from above so we don't have
612 // // to read and parse it every time?
613 // pkgVendorList List;
614 // List.ReadMainList();
616 // const Vendor* Vndr = NULL;
617 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
619 // string::size_type pos = (*I).find("VALIDSIG ");
620 // if (_config->FindB("Debug::Vendor", false))
621 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
623 // if (pos != std::string::npos)
625 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
626 // if (_config->FindB("Debug::Vendor", false))
627 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
629 // Vndr = List.FindVendor(Fingerprint) != "";
630 // if (Vndr != NULL);
635 string Transformed
= MetaIndexParser
->GetExpectedDist();
637 if (Transformed
== "../project/experimental")
639 Transformed
= "experimental";
642 string::size_type pos
= Transformed
.rfind('/');
643 if (pos
!= string::npos
)
645 Transformed
= Transformed
.substr(0, pos
);
648 if (Transformed
== ".")
653 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
655 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
656 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
657 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
660 if (MetaIndexParser
->CheckDist(Transformed
) == false)
662 // This might become fatal one day
663 // Status = StatAuthError;
664 // ErrorText = "Conflicting distribution; expected "
665 // + MetaIndexParser->GetExpectedDist() + " but got "
666 // + MetaIndexParser->GetDist();
668 if (!Transformed
.empty())
670 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
671 Desc
.Description
.c_str(),
673 MetaIndexParser
->GetDist().c_str());
680 // pkgAcqMetaIndex::Failed - no Release file present or no signature
681 // file present /*{{{*/
682 // ---------------------------------------------------------------------
684 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
686 if (AuthPass
== true)
688 // gpgv method failed
689 _error
->Warning("GPG error: %s: %s",
690 Desc
.Description
.c_str(),
691 LookupTag(Message
,"Message").c_str());
694 // No Release file was present, or verification failed, so fall
695 // back to queueing Packages files without verification
701 // AcqArchive::AcqArchive - Constructor /*{{{*/
702 // ---------------------------------------------------------------------
703 /* This just sets up the initial fetch environment and queues the first
705 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
706 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
707 string
&StoreFilename
) :
708 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
709 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
712 Retries
= _config
->FindI("Acquire::Retries",0);
714 if (Version
.Arch() == 0)
716 _error
->Error(_("I wasn't able to locate a file for the %s package. "
717 "This might mean you need to manually fix this package. "
718 "(due to missing arch)"),
719 Version
.ParentPkg().Name());
723 /* We need to find a filename to determine the extension. We make the
724 assumption here that all the available sources for this version share
725 the same extension.. */
726 // Skip not source sources, they do not have file fields.
727 for (; Vf
.end() == false; Vf
++)
729 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
734 // Does not really matter here.. we are going to fail out below
735 if (Vf
.end() != true)
737 // If this fails to get a file name we will bomb out below.
738 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
739 if (_error
->PendingError() == true)
742 // Generate the final file name as: package_version_arch.foo
743 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
744 QuoteString(Version
.VerStr(),"_:") + '_' +
745 QuoteString(Version
.Arch(),"_:.") +
746 "." + flExtension(Parse
.FileName());
749 // check if we have one trusted source for the package. if so, switch
750 // to "TrustedOnly" mode
751 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
754 if (Sources
->FindIndex(i
.File(),Index
) == false)
756 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
758 std::cerr
<< "Checking index: " << Index
->Describe()
759 << "(Trusted=" << Index
->IsTrusted() << ")\n";
761 if (Index
->IsTrusted()) {
768 if (QueueNext() == false && _error
->PendingError() == false)
769 _error
->Error(_("I wasn't able to locate file for the %s package. "
770 "This might mean you need to manually fix this package."),
771 Version
.ParentPkg().Name());
774 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
775 // ---------------------------------------------------------------------
776 /* This queues the next available file version for download. It checks if
777 the archive is already available in the cache and stashs the MD5 for
779 bool pkgAcqArchive::QueueNext()
781 for (; Vf
.end() == false; Vf
++)
783 // Ignore not source sources
784 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
787 // Try to cross match against the source list
789 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
792 // only try to get a trusted package from another source if that source
794 if(Trusted
&& !Index
->IsTrusted())
797 // Grab the text package record
798 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
799 if (_error
->PendingError() == true)
802 string PkgFile
= Parse
.FileName();
803 MD5
= Parse
.MD5Hash();
804 if (PkgFile
.empty() == true)
805 return _error
->Error(_("The package index files are corrupted. No Filename: "
806 "field for package %s."),
807 Version
.ParentPkg().Name());
809 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
810 Desc
.Description
= Index
->ArchiveInfo(Version
);
812 Desc
.ShortDesc
= Version
.ParentPkg().Name();
814 // See if we already have the file. (Legacy filenames)
815 FileSize
= Version
->Size
;
816 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
818 if (stat(FinalFile
.c_str(),&Buf
) == 0)
820 // Make sure the size matches
821 if ((unsigned)Buf
.st_size
== Version
->Size
)
826 StoreFilename
= DestFile
= FinalFile
;
830 /* Hmm, we have a file and its size does not match, this means it is
831 an old style mismatched arch */
832 unlink(FinalFile
.c_str());
835 // Check it again using the new style output filenames
836 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
837 if (stat(FinalFile
.c_str(),&Buf
) == 0)
839 // Make sure the size matches
840 if ((unsigned)Buf
.st_size
== Version
->Size
)
845 StoreFilename
= DestFile
= FinalFile
;
849 /* Hmm, we have a file and its size does not match, this shouldnt
851 unlink(FinalFile
.c_str());
854 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
856 // Check the destination file
857 if (stat(DestFile
.c_str(),&Buf
) == 0)
859 // Hmm, the partial file is too big, erase it
860 if ((unsigned)Buf
.st_size
> Version
->Size
)
861 unlink(DestFile
.c_str());
863 PartialSize
= Buf
.st_size
;
868 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
869 Desc
.Description
= Index
->ArchiveInfo(Version
);
871 Desc
.ShortDesc
= Version
.ParentPkg().Name();
880 // AcqArchive::Done - Finished fetching /*{{{*/
881 // ---------------------------------------------------------------------
883 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
884 pkgAcquire::MethodConfig
*Cfg
)
886 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
889 if (Size
!= Version
->Size
)
892 ErrorText
= _("Size mismatch");
897 if (Md5Hash
.empty() == false && MD5
.empty() == false)
902 ErrorText
= _("MD5Sum mismatch");
903 Rename(DestFile
,DestFile
+ ".FAILED");
908 // Grab the output filename
909 string FileName
= LookupTag(Message
,"Filename");
910 if (FileName
.empty() == true)
913 ErrorText
= "Method gave a blank filename";
919 // Reference filename
920 if (FileName
!= DestFile
)
922 StoreFilename
= DestFile
= FileName
;
927 // Done, move it into position
928 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
929 FinalFile
+= flNotDir(StoreFilename
);
930 Rename(DestFile
,FinalFile
);
932 StoreFilename
= DestFile
= FinalFile
;
936 // AcqArchive::Failed - Failure handler /*{{{*/
937 // ---------------------------------------------------------------------
938 /* Here we try other sources */
939 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
941 ErrorText
= LookupTag(Message
,"Message");
943 /* We don't really want to retry on failed media swaps, this prevents
944 that. An interesting observation is that permanent failures are not
946 if (Cnf
->Removable
== true &&
947 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
949 // Vf = Version.FileList();
950 while (Vf
.end() == false) Vf
++;
951 StoreFilename
= string();
952 Item::Failed(Message
,Cnf
);
956 if (QueueNext() == false)
958 // This is the retry counter
960 Cnf
->LocalOnly
== false &&
961 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
964 Vf
= Version
.FileList();
965 if (QueueNext() == true)
969 StoreFilename
= string();
970 Item::Failed(Message
,Cnf
);
974 // AcqArchive::IsTrusted - Determine whether this archive comes from a
975 // trusted source /*{{{*/
976 // ---------------------------------------------------------------------
977 bool pkgAcqArchive::IsTrusted()
982 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
983 // ---------------------------------------------------------------------
985 void pkgAcqArchive::Finished()
987 if (Status
== pkgAcquire::Item::StatDone
&&
990 StoreFilename
= string();
994 // AcqFile::pkgAcqFile - Constructor /*{{{*/
995 // ---------------------------------------------------------------------
996 /* The file is added to the queue */
997 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
998 unsigned long Size
,string Dsc
,string ShortDesc
) :
999 Item(Owner
), Md5Hash(MD5
)
1001 Retries
= _config
->FindI("Acquire::Retries",0);
1003 DestFile
= flNotDir(URI
);
1007 Desc
.Description
= Dsc
;
1010 // Set the short description to the archive component
1011 Desc
.ShortDesc
= ShortDesc
;
1013 // Get the transfer sizes
1016 if (stat(DestFile
.c_str(),&Buf
) == 0)
1018 // Hmm, the partial file is too big, erase it
1019 if ((unsigned)Buf
.st_size
> Size
)
1020 unlink(DestFile
.c_str());
1022 PartialSize
= Buf
.st_size
;
1028 // AcqFile::Done - Item downloaded OK /*{{{*/
1029 // ---------------------------------------------------------------------
1031 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1032 pkgAcquire::MethodConfig
*Cnf
)
1035 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1040 ErrorText
= "MD5Sum mismatch";
1041 Rename(DestFile
,DestFile
+ ".FAILED");
1046 Item::Done(Message
,Size
,MD5
,Cnf
);
1048 string FileName
= LookupTag(Message
,"Filename");
1049 if (FileName
.empty() == true)
1052 ErrorText
= "Method gave a blank filename";
1058 // The files timestamp matches
1059 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1062 // We have to copy it into place
1063 if (FileName
!= DestFile
)
1066 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1067 Cnf
->Removable
== true)
1069 Desc
.URI
= "copy:" + FileName
;
1074 // Erase the file if it is a symlink so we can overwrite it
1076 if (lstat(DestFile
.c_str(),&St
) == 0)
1078 if (S_ISLNK(St
.st_mode
) != 0)
1079 unlink(DestFile
.c_str());
1083 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1085 ErrorText
= "Link to " + DestFile
+ " failure ";
1092 // AcqFile::Failed - Failure handler /*{{{*/
1093 // ---------------------------------------------------------------------
1094 /* Here we try other sources */
1095 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1097 ErrorText
= LookupTag(Message
,"Message");
1099 // This is the retry counter
1101 Cnf
->LocalOnly
== false &&
1102 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1109 Item::Failed(Message
,Cnf
);