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 // AcqIndexTrans::pkgAcqIndexTrans - Constructor /*{{{*/
311 // ---------------------------------------------------------------------
312 /* The Translation file is added to the queue */
313 pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire
*Owner
,
314 string URI
,string URIDesc
,string ShortDesc
) :
315 pkgAcqIndex(Owner
, URI
, URIDesc
, ShortDesc
, "", "")
320 // AcqIndexTrans::Failed - Silence failure messages for missing files /*{{{*/
321 // ---------------------------------------------------------------------
323 void pkgAcqIndexTrans::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
325 if (Cnf
->LocalOnly
== true ||
326 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
335 Item::Failed(Message
,Cnf
);
339 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
340 string URI
,string URIDesc
,string ShortDesc
,
341 string MetaIndexURI
, string MetaIndexURIDesc
,
342 string MetaIndexShortDesc
,
343 const vector
<IndexTarget
*>* IndexTargets
,
344 indexRecords
* MetaIndexParser
) :
345 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
346 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
)
348 this->MetaIndexParser
= MetaIndexParser
;
349 this->IndexTargets
= IndexTargets
;
350 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
351 DestFile
+= URItoFileName(URI
);
353 // remove any partial downloaded sig-file. it may confuse proxies
354 // and is too small to warrant a partial download anyway
355 unlink(DestFile
.c_str());
358 Desc
.Description
= URIDesc
;
360 Desc
.ShortDesc
= ShortDesc
;
364 string Final
= _config
->FindDir("Dir::State::lists");
365 Final
+= URItoFileName(RealURI
);
367 if (stat(Final
.c_str(),&Buf
) == 0)
369 // File was already in place. It needs to be re-verified
370 // because Release might have changed, so Move it into partial
371 Rename(Final
,DestFile
);
372 // unlink the file and do not try to use I-M-S and Last-Modified
373 // if the users proxy is broken
374 if(_config
->FindB("Acquire::BrokenProxy", false) == true) {
375 std::cerr
<< "forcing re-get of the signature file as requested" << std::endl
;
376 unlink(DestFile
.c_str());
383 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
384 // ---------------------------------------------------------------------
385 /* The only header we use is the last-modified header. */
386 string
pkgAcqMetaSig::Custom600Headers()
389 if (stat(DestFile
.c_str(),&Buf
) != 0)
390 return "\nIndex-File: true";
392 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
395 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
396 pkgAcquire::MethodConfig
*Cfg
)
398 Item::Done(Message
,Size
,MD5
,Cfg
);
400 string FileName
= LookupTag(Message
,"Filename");
401 if (FileName
.empty() == true)
404 ErrorText
= "Method gave a blank filename";
408 if (FileName
!= DestFile
)
410 // We have to copy it into place
412 Desc
.URI
= "copy:" + FileName
;
419 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
420 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
421 DestFile
, IndexTargets
, MetaIndexParser
);
425 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
427 // Delete any existing sigfile, so that this source isn't
428 // mistakenly trusted
429 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
430 unlink(Final
.c_str());
432 // queue a pkgAcqMetaIndex with no sigfile
433 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
434 "", IndexTargets
, MetaIndexParser
);
436 if (Cnf
->LocalOnly
== true ||
437 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
446 Item::Failed(Message
,Cnf
);
449 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
450 string URI
,string URIDesc
,string ShortDesc
,
452 const vector
<struct IndexTarget
*>* IndexTargets
,
453 indexRecords
* MetaIndexParser
) :
454 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
456 this->AuthPass
= false;
457 this->MetaIndexParser
= MetaIndexParser
;
458 this->IndexTargets
= IndexTargets
;
459 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
460 DestFile
+= URItoFileName(URI
);
463 Desc
.Description
= URIDesc
;
465 Desc
.ShortDesc
= ShortDesc
;
472 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
473 // ---------------------------------------------------------------------
474 /* The only header we use is the last-modified header. */
475 string
pkgAcqMetaIndex::Custom600Headers()
477 string Final
= _config
->FindDir("Dir::State::lists");
478 Final
+= URItoFileName(RealURI
);
481 if (stat(Final
.c_str(),&Buf
) != 0)
482 return "\nIndex-File: true";
484 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
487 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
488 pkgAcquire::MethodConfig
*Cfg
)
490 Item::Done(Message
,Size
,MD5
,Cfg
);
492 // MetaIndexes are done in two passes: one to download the
493 // metaindex with an appropriate method, and a second to verify it
494 // with the gpgv method
496 if (AuthPass
== true)
502 RetrievalDone(Message
);
504 // Still more retrieving to do
509 // There was no signature file, so we are finished. Download
510 // the indexes without verification.
515 // There was a signature file, so pass it to gpgv for
518 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
519 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
520 << SigFile
<< "," << DestFile
<< ")\n";
522 Desc
.URI
= "gpgv:" + SigFile
;
529 void pkgAcqMetaIndex::RetrievalDone(string Message
)
531 // We have just finished downloading a Release file (it is not
534 string FileName
= LookupTag(Message
,"Filename");
535 if (FileName
.empty() == true)
538 ErrorText
= "Method gave a blank filename";
542 if (FileName
!= DestFile
)
545 Desc
.URI
= "copy:" + FileName
;
552 string FinalFile
= _config
->FindDir("Dir::State::lists");
553 FinalFile
+= URItoFileName(RealURI
);
555 // The files timestamp matches
556 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
558 // Move it into position
559 Rename(DestFile
,FinalFile
);
561 DestFile
= FinalFile
;
564 void pkgAcqMetaIndex::AuthDone(string Message
)
566 // At this point, the gpgv method has succeeded, so there is a
567 // valid signature from a key in the trusted keyring. We
568 // perform additional verification of its contents, and use them
569 // to verify the indexes we are about to download
571 if (!MetaIndexParser
->Load(DestFile
))
573 Status
= StatAuthError
;
574 ErrorText
= MetaIndexParser
->ErrorText
;
583 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
584 std::cerr
<< "Signature verification succeeded: "
585 << DestFile
<< std::endl
;
587 // Download further indexes with verification
590 // Done, move signature file into position
592 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
593 URItoFileName(RealURI
) + ".gpg";
594 Rename(SigFile
,VerifiedSigFile
);
595 chmod(VerifiedSigFile
.c_str(),0644);
598 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
600 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
601 Target
!= IndexTargets
->end();
604 string ExpectedIndexMD5
;
607 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
610 Status
= StatAuthError
;
611 ErrorText
= "Unable to find expected entry "
612 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
615 ExpectedIndexMD5
= Record
->MD5Hash
;
616 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
618 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
619 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
621 if (ExpectedIndexMD5
.empty())
623 Status
= StatAuthError
;
624 ErrorText
= "Unable to find MD5 sum for "
625 + (*Target
)->MetaKey
+ " in Meta-index file";
630 // Queue Packages file
631 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
632 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
636 bool pkgAcqMetaIndex::VerifyVendor()
638 // // Maybe this should be made available from above so we don't have
639 // // to read and parse it every time?
640 // pkgVendorList List;
641 // List.ReadMainList();
643 // const Vendor* Vndr = NULL;
644 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
646 // string::size_type pos = (*I).find("VALIDSIG ");
647 // if (_config->FindB("Debug::Vendor", false))
648 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
650 // if (pos != std::string::npos)
652 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
653 // if (_config->FindB("Debug::Vendor", false))
654 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
656 // Vndr = List.FindVendor(Fingerprint) != "";
657 // if (Vndr != NULL);
662 string Transformed
= MetaIndexParser
->GetExpectedDist();
664 if (Transformed
== "../project/experimental")
666 Transformed
= "experimental";
669 string::size_type pos
= Transformed
.rfind('/');
670 if (pos
!= string::npos
)
672 Transformed
= Transformed
.substr(0, pos
);
675 if (Transformed
== ".")
680 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
682 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
683 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
684 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
687 if (MetaIndexParser
->CheckDist(Transformed
) == false)
689 // This might become fatal one day
690 // Status = StatAuthError;
691 // ErrorText = "Conflicting distribution; expected "
692 // + MetaIndexParser->GetExpectedDist() + " but got "
693 // + MetaIndexParser->GetDist();
695 if (!Transformed
.empty())
697 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
698 Desc
.Description
.c_str(),
700 MetaIndexParser
->GetDist().c_str());
707 // pkgAcqMetaIndex::Failed - no Release file present or no signature
708 // file present /*{{{*/
709 // ---------------------------------------------------------------------
711 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
713 if (AuthPass
== true)
715 // gpgv method failed
716 _error
->Warning("GPG error: %s: %s",
717 Desc
.Description
.c_str(),
718 LookupTag(Message
,"Message").c_str());
721 // No Release file was present, or verification failed, so fall
722 // back to queueing Packages files without verification
728 // AcqArchive::AcqArchive - Constructor /*{{{*/
729 // ---------------------------------------------------------------------
730 /* This just sets up the initial fetch environment and queues the first
732 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
733 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
734 string
&StoreFilename
) :
735 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
736 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
739 Retries
= _config
->FindI("Acquire::Retries",0);
741 if (Version
.Arch() == 0)
743 _error
->Error(_("I wasn't able to locate a file for the %s package. "
744 "This might mean you need to manually fix this package. "
745 "(due to missing arch)"),
746 Version
.ParentPkg().Name());
750 /* We need to find a filename to determine the extension. We make the
751 assumption here that all the available sources for this version share
752 the same extension.. */
753 // Skip not source sources, they do not have file fields.
754 for (; Vf
.end() == false; Vf
++)
756 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
761 // Does not really matter here.. we are going to fail out below
762 if (Vf
.end() != true)
764 // If this fails to get a file name we will bomb out below.
765 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
766 if (_error
->PendingError() == true)
769 // Generate the final file name as: package_version_arch.foo
770 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
771 QuoteString(Version
.VerStr(),"_:") + '_' +
772 QuoteString(Version
.Arch(),"_:.") +
773 "." + flExtension(Parse
.FileName());
776 // check if we have one trusted source for the package. if so, switch
777 // to "TrustedOnly" mode
778 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
781 if (Sources
->FindIndex(i
.File(),Index
) == false)
783 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
785 std::cerr
<< "Checking index: " << Index
->Describe()
786 << "(Trusted=" << Index
->IsTrusted() << ")\n";
788 if (Index
->IsTrusted()) {
795 if (QueueNext() == false && _error
->PendingError() == false)
796 _error
->Error(_("I wasn't able to locate file for the %s package. "
797 "This might mean you need to manually fix this package."),
798 Version
.ParentPkg().Name());
801 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
802 // ---------------------------------------------------------------------
803 /* This queues the next available file version for download. It checks if
804 the archive is already available in the cache and stashs the MD5 for
806 bool pkgAcqArchive::QueueNext()
808 for (; Vf
.end() == false; Vf
++)
810 // Ignore not source sources
811 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
814 // Try to cross match against the source list
816 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
819 // only try to get a trusted package from another source if that source
821 if(Trusted
&& !Index
->IsTrusted())
824 // Grab the text package record
825 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
826 if (_error
->PendingError() == true)
829 string PkgFile
= Parse
.FileName();
830 MD5
= Parse
.MD5Hash();
831 if (PkgFile
.empty() == true)
832 return _error
->Error(_("The package index files are corrupted. No Filename: "
833 "field for package %s."),
834 Version
.ParentPkg().Name());
836 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
837 Desc
.Description
= Index
->ArchiveInfo(Version
);
839 Desc
.ShortDesc
= Version
.ParentPkg().Name();
841 // See if we already have the file. (Legacy filenames)
842 FileSize
= Version
->Size
;
843 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
845 if (stat(FinalFile
.c_str(),&Buf
) == 0)
847 // Make sure the size matches
848 if ((unsigned)Buf
.st_size
== Version
->Size
)
853 StoreFilename
= DestFile
= FinalFile
;
857 /* Hmm, we have a file and its size does not match, this means it is
858 an old style mismatched arch */
859 unlink(FinalFile
.c_str());
862 // Check it again using the new style output filenames
863 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
864 if (stat(FinalFile
.c_str(),&Buf
) == 0)
866 // Make sure the size matches
867 if ((unsigned)Buf
.st_size
== Version
->Size
)
872 StoreFilename
= DestFile
= FinalFile
;
876 /* Hmm, we have a file and its size does not match, this shouldnt
878 unlink(FinalFile
.c_str());
881 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
883 // Check the destination file
884 if (stat(DestFile
.c_str(),&Buf
) == 0)
886 // Hmm, the partial file is too big, erase it
887 if ((unsigned)Buf
.st_size
> Version
->Size
)
888 unlink(DestFile
.c_str());
890 PartialSize
= Buf
.st_size
;
895 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
896 Desc
.Description
= Index
->ArchiveInfo(Version
);
898 Desc
.ShortDesc
= Version
.ParentPkg().Name();
907 // AcqArchive::Done - Finished fetching /*{{{*/
908 // ---------------------------------------------------------------------
910 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
911 pkgAcquire::MethodConfig
*Cfg
)
913 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
916 if (Size
!= Version
->Size
)
919 ErrorText
= _("Size mismatch");
924 if (Md5Hash
.empty() == false && MD5
.empty() == false)
929 ErrorText
= _("MD5Sum mismatch");
930 Rename(DestFile
,DestFile
+ ".FAILED");
935 // Grab the output filename
936 string FileName
= LookupTag(Message
,"Filename");
937 if (FileName
.empty() == true)
940 ErrorText
= "Method gave a blank filename";
946 // Reference filename
947 if (FileName
!= DestFile
)
949 StoreFilename
= DestFile
= FileName
;
954 // Done, move it into position
955 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
956 FinalFile
+= flNotDir(StoreFilename
);
957 Rename(DestFile
,FinalFile
);
959 StoreFilename
= DestFile
= FinalFile
;
963 // AcqArchive::Failed - Failure handler /*{{{*/
964 // ---------------------------------------------------------------------
965 /* Here we try other sources */
966 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
968 ErrorText
= LookupTag(Message
,"Message");
970 /* We don't really want to retry on failed media swaps, this prevents
971 that. An interesting observation is that permanent failures are not
973 if (Cnf
->Removable
== true &&
974 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
976 // Vf = Version.FileList();
977 while (Vf
.end() == false) Vf
++;
978 StoreFilename
= string();
979 Item::Failed(Message
,Cnf
);
983 if (QueueNext() == false)
985 // This is the retry counter
987 Cnf
->LocalOnly
== false &&
988 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
991 Vf
= Version
.FileList();
992 if (QueueNext() == true)
996 StoreFilename
= string();
997 Item::Failed(Message
,Cnf
);
1001 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1002 // trusted source /*{{{*/
1003 // ---------------------------------------------------------------------
1004 bool pkgAcqArchive::IsTrusted()
1009 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1010 // ---------------------------------------------------------------------
1012 void pkgAcqArchive::Finished()
1014 if (Status
== pkgAcquire::Item::StatDone
&&
1017 StoreFilename
= string();
1021 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1022 // ---------------------------------------------------------------------
1023 /* The file is added to the queue */
1024 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1025 unsigned long Size
,string Dsc
,string ShortDesc
) :
1026 Item(Owner
), Md5Hash(MD5
)
1028 Retries
= _config
->FindI("Acquire::Retries",0);
1030 DestFile
= flNotDir(URI
);
1034 Desc
.Description
= Dsc
;
1037 // Set the short description to the archive component
1038 Desc
.ShortDesc
= ShortDesc
;
1040 // Get the transfer sizes
1043 if (stat(DestFile
.c_str(),&Buf
) == 0)
1045 // Hmm, the partial file is too big, erase it
1046 if ((unsigned)Buf
.st_size
> Size
)
1047 unlink(DestFile
.c_str());
1049 PartialSize
= Buf
.st_size
;
1055 // AcqFile::Done - Item downloaded OK /*{{{*/
1056 // ---------------------------------------------------------------------
1058 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1059 pkgAcquire::MethodConfig
*Cnf
)
1062 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1067 ErrorText
= "MD5Sum mismatch";
1068 Rename(DestFile
,DestFile
+ ".FAILED");
1073 Item::Done(Message
,Size
,MD5
,Cnf
);
1075 string FileName
= LookupTag(Message
,"Filename");
1076 if (FileName
.empty() == true)
1079 ErrorText
= "Method gave a blank filename";
1085 // The files timestamp matches
1086 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1089 // We have to copy it into place
1090 if (FileName
!= DestFile
)
1093 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1094 Cnf
->Removable
== true)
1096 Desc
.URI
= "copy:" + FileName
;
1101 // Erase the file if it is a symlink so we can overwrite it
1103 if (lstat(DestFile
.c_str(),&St
) == 0)
1105 if (S_ISLNK(St
.st_mode
) != 0)
1106 unlink(DestFile
.c_str());
1110 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1112 ErrorText
= "Link to " + DestFile
+ " failure ";
1119 // AcqFile::Failed - Failure handler /*{{{*/
1120 // ---------------------------------------------------------------------
1121 /* Here we try other sources */
1122 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1124 ErrorText
= LookupTag(Message
,"Message");
1126 // This is the retry counter
1128 Cnf
->LocalOnly
== false &&
1129 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1136 Item::Failed(Message
,Cnf
);