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 /*{{{*/
16 #include <apt-pkg/acquire-item.h>
17 #include <apt-pkg/configuration.h>
18 #include <apt-pkg/sourcelist.h>
19 #include <apt-pkg/vendorlist.h>
20 #include <apt-pkg/error.h>
21 #include <apt-pkg/strutl.h>
22 #include <apt-pkg/fileutl.h>
23 #include <apt-pkg/md5.h>
36 // Acquire::Item::Item - Constructor /*{{{*/
37 // ---------------------------------------------------------------------
39 pkgAcquire::Item::Item(pkgAcquire
*Owner
) : Owner(Owner
), FileSize(0),
40 PartialSize(0), Mode(0), ID(0), Complete(false),
41 Local(false), QueueCounter(0)
47 // Acquire::Item::~Item - Destructor /*{{{*/
48 // ---------------------------------------------------------------------
50 pkgAcquire::Item::~Item()
55 // Acquire::Item::Failed - Item failed to download /*{{{*/
56 // ---------------------------------------------------------------------
57 /* We return to an idle state if there are still other queues that could
59 void pkgAcquire::Item::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
62 ErrorText
= LookupTag(Message
,"Message");
63 if (QueueCounter
<= 1)
65 /* This indicates that the file is not available right now but might
66 be sometime later. If we do a retry cycle then this should be
68 if (Cnf
->LocalOnly
== true &&
69 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
81 // Acquire::Item::Start - Item has begun to download /*{{{*/
82 // ---------------------------------------------------------------------
83 /* Stash status and the file size. Note that setting Complete means
84 sub-phases of the acquire process such as decompresion are operating */
85 void pkgAcquire::Item::Start(string
/*Message*/,unsigned long Size
)
87 Status
= StatFetching
;
88 if (FileSize
== 0 && Complete
== false)
92 // Acquire::Item::Done - Item downloaded OK /*{{{*/
93 // ---------------------------------------------------------------------
95 void pkgAcquire::Item::Done(string Message
,unsigned long Size
,string
,
96 pkgAcquire::MethodConfig
*Cnf
)
98 // We just downloaded something..
99 string FileName
= LookupTag(Message
,"Filename");
100 if (Complete
== false && FileName
== DestFile
)
103 Owner
->Log
->Fetched(Size
,atoi(LookupTag(Message
,"Resume-Point","0").c_str()));
110 ErrorText
= string();
111 Owner
->Dequeue(this);
114 // Acquire::Item::Rename - Rename a file /*{{{*/
115 // ---------------------------------------------------------------------
116 /* This helper function is used by alot of item methods as thier final
118 void pkgAcquire::Item::Rename(string From
,string To
)
120 if (rename(From
.c_str(),To
.c_str()) != 0)
123 snprintf(S
,sizeof(S
),_("rename failed, %s (%s -> %s)."),strerror(errno
),
124 From
.c_str(),To
.c_str());
131 // AcqIndex::AcqIndex - Constructor /*{{{*/
132 // ---------------------------------------------------------------------
133 /* The package file is added to the queue and a second class is
134 instantiated to fetch the revision file */
135 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
136 string URI
,string URIDesc
,string ShortDesc
,
137 string ExpectedMD5
, string comprExt
) :
138 Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
)
140 Decompression
= false;
143 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
144 DestFile
+= URItoFileName(URI
);
148 // autoselect the compression method
149 if(FileExists("/bin/bzip2"))
150 CompressionExtension
= ".bz2";
152 CompressionExtension
= ".gz";
154 CompressionExtension
= comprExt
;
156 Desc
.URI
= URI
+ CompressionExtension
;
158 Desc
.Description
= URIDesc
;
160 Desc
.ShortDesc
= ShortDesc
;
165 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
166 // ---------------------------------------------------------------------
167 /* The only header we use is the last-modified header. */
168 string
pkgAcqIndex::Custom600Headers()
170 string Final
= _config
->FindDir("Dir::State::lists");
171 Final
+= URItoFileName(RealURI
);
174 if (stat(Final
.c_str(),&Buf
) != 0)
175 return "\nIndex-File: true";
177 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
181 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
183 // no .bz2 found, retry with .gz
184 if(Desc
.URI
.substr(Desc
.URI
.size()-3) == "bz2") {
185 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
187 // retry with a gzip one
188 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
189 ExpectedMD5
, string(".gz"));
197 Item::Failed(Message
,Cnf
);
201 // AcqIndex::Done - Finished a fetch /*{{{*/
202 // ---------------------------------------------------------------------
203 /* This goes through a number of states.. On the initial fetch the
204 method could possibly return an alternate filename which points
205 to the uncompressed version of the file. If this is so the file
206 is copied into the partial directory. In all other cases the file
207 is decompressed with a gzip uri. */
208 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
209 pkgAcquire::MethodConfig
*Cfg
)
211 Item::Done(Message
,Size
,MD5
,Cfg
);
213 if (Decompression
== true)
215 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
217 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
218 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
224 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
225 sum
.AddFD(Fd
.Fd(), Fd
.Size());
227 MD5
= (string
)sum
.Result();
230 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
232 Status
= StatAuthError
;
233 ErrorText
= _("MD5Sum mismatch");
234 Rename(DestFile
,DestFile
+ ".FAILED");
237 // Done, move it into position
238 string FinalFile
= _config
->FindDir("Dir::State::lists");
239 FinalFile
+= URItoFileName(RealURI
);
240 Rename(DestFile
,FinalFile
);
241 chmod(FinalFile
.c_str(),0644);
243 /* We restore the original name to DestFile so that the clean operation
245 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
246 DestFile
+= URItoFileName(RealURI
);
248 // Remove the compressed version.
250 unlink(DestFile
.c_str());
257 // Handle the unzipd case
258 string FileName
= LookupTag(Message
,"Alt-Filename");
259 if (FileName
.empty() == false)
261 // The files timestamp matches
262 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
265 Decompression
= true;
267 DestFile
+= ".decomp";
268 Desc
.URI
= "copy:" + FileName
;
274 FileName
= LookupTag(Message
,"Filename");
275 if (FileName
.empty() == true)
278 ErrorText
= "Method gave a blank filename";
281 // The files timestamp matches
282 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
285 if (FileName
== DestFile
)
290 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3);
293 decompProg
= "bzip2";
294 else if(compExt
== ".gz")
297 _error
->Error("Unsupported extension: %s", compExt
.c_str());
301 Decompression
= true;
302 DestFile
+= ".decomp";
303 Desc
.URI
= string(decompProg
) + ":" + FileName
;
308 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
309 string URI
,string URIDesc
,string ShortDesc
,
310 string MetaIndexURI
, string MetaIndexURIDesc
,
311 string MetaIndexShortDesc
,
312 const vector
<IndexTarget
*>* IndexTargets
,
313 indexRecords
* MetaIndexParser
) :
314 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
315 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
316 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
)
318 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
319 DestFile
+= URItoFileName(URI
);
321 // remove any partial downloaded sig-file. it may confuse proxies
322 // and is too small to warrant a partial download anyway
323 unlink(DestFile
.c_str());
326 Desc
.Description
= URIDesc
;
328 Desc
.ShortDesc
= ShortDesc
;
332 string Final
= _config
->FindDir("Dir::State::lists");
333 Final
+= URItoFileName(RealURI
);
335 if (stat(Final
.c_str(),&Buf
) == 0)
337 // File was already in place. It needs to be re-verified
338 // because Release might have changed, so Move it into partial
339 Rename(Final
,DestFile
);
345 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
346 // ---------------------------------------------------------------------
347 /* The only header we use is the last-modified header. */
348 string
pkgAcqMetaSig::Custom600Headers()
351 if (stat(DestFile
.c_str(),&Buf
) != 0)
352 return "\nIndex-File: true";
354 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
357 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
358 pkgAcquire::MethodConfig
*Cfg
)
360 Item::Done(Message
,Size
,MD5
,Cfg
);
362 string FileName
= LookupTag(Message
,"Filename");
363 if (FileName
.empty() == true)
366 ErrorText
= "Method gave a blank filename";
370 if (FileName
!= DestFile
)
372 // We have to copy it into place
374 Desc
.URI
= "copy:" + FileName
;
381 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
382 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
383 DestFile
, IndexTargets
, MetaIndexParser
);
387 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
390 // if we get a network error we fail gracefully
391 if(LookupTag(Message
,"FailReason") == "Timeout" ||
392 LookupTag(Message
,"FailReason") == "TmpResolveFailure" ||
393 LookupTag(Message
,"FailReason") == "ConnectionRefused") {
394 Item::Failed(Message
,Cnf
);
398 // Delete any existing sigfile when the acquire failed
399 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
400 unlink(Final
.c_str());
402 // queue a pkgAcqMetaIndex with no sigfile
403 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
404 "", IndexTargets
, MetaIndexParser
);
406 if (Cnf
->LocalOnly
== true ||
407 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
416 Item::Failed(Message
,Cnf
);
419 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
420 string URI
,string URIDesc
,string ShortDesc
,
422 const vector
<struct IndexTarget
*>* IndexTargets
,
423 indexRecords
* MetaIndexParser
) :
424 Item(Owner
), RealURI(URI
), SigFile(SigFile
), AuthPass(false),
425 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
), IMSHit(false)
427 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
428 DestFile
+= URItoFileName(URI
);
431 Desc
.Description
= URIDesc
;
433 Desc
.ShortDesc
= ShortDesc
;
440 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
441 // ---------------------------------------------------------------------
442 /* The only header we use is the last-modified header. */
443 string
pkgAcqMetaIndex::Custom600Headers()
445 string Final
= _config
->FindDir("Dir::State::lists");
446 Final
+= URItoFileName(RealURI
);
449 if (stat(Final
.c_str(),&Buf
) != 0)
450 return "\nIndex-File: true";
452 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
455 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
456 pkgAcquire::MethodConfig
*Cfg
)
458 Item::Done(Message
,Size
,MD5
,Cfg
);
460 // MetaIndexes are done in two passes: one to download the
461 // metaindex with an appropriate method, and a second to verify it
462 // with the gpgv method
464 if (AuthPass
== true)
470 RetrievalDone(Message
);
472 // Still more retrieving to do
477 // There was no signature file, so we are finished. Download
478 // the indexes without verification.
483 // There was a signature file, so pass it to gpgv for
486 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
487 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
488 << SigFile
<< "," << DestFile
<< ")\n";
490 Desc
.URI
= "gpgv:" + SigFile
;
497 void pkgAcqMetaIndex::RetrievalDone(string Message
)
499 // We have just finished downloading a Release file (it is not
502 string FileName
= LookupTag(Message
,"Filename");
503 if (FileName
.empty() == true)
506 ErrorText
= "Method gave a blank filename";
510 if (FileName
!= DestFile
)
513 Desc
.URI
= "copy:" + FileName
;
518 // see if the download was a IMSHit
519 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
523 string FinalFile
= _config
->FindDir("Dir::State::lists");
524 FinalFile
+= URItoFileName(RealURI
);
526 // The files timestamp matches
527 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
529 // Move it into position
530 Rename(DestFile
,FinalFile
);
532 DestFile
= FinalFile
;
535 void pkgAcqMetaIndex::AuthDone(string Message
)
537 // At this point, the gpgv method has succeeded, so there is a
538 // valid signature from a key in the trusted keyring. We
539 // perform additional verification of its contents, and use them
540 // to verify the indexes we are about to download
542 if (!MetaIndexParser
->Load(DestFile
))
544 Status
= StatAuthError
;
545 ErrorText
= MetaIndexParser
->ErrorText
;
549 if (!VerifyVendor(Message
))
554 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
555 std::cerr
<< "Signature verification succeeded: "
556 << DestFile
<< std::endl
;
558 // Download further indexes with verification
561 // Done, move signature file into position
563 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
564 URItoFileName(RealURI
) + ".gpg";
565 Rename(SigFile
,VerifiedSigFile
);
566 chmod(VerifiedSigFile
.c_str(),0644);
569 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
571 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
572 Target
!= IndexTargets
->end();
575 string ExpectedIndexMD5
;
578 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
581 Status
= StatAuthError
;
582 ErrorText
= "Unable to find expected entry "
583 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
586 ExpectedIndexMD5
= Record
->MD5Hash
;
587 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
589 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
590 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
592 if (ExpectedIndexMD5
.empty())
594 Status
= StatAuthError
;
595 ErrorText
= "Unable to find MD5 sum for "
596 + (*Target
)->MetaKey
+ " in Meta-index file";
601 // Queue Packages file
602 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
603 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
607 bool pkgAcqMetaIndex::VerifyVendor(string Message
)
609 // // Maybe this should be made available from above so we don't have
610 // // to read and parse it every time?
611 // pkgVendorList List;
612 // List.ReadMainList();
614 // const Vendor* Vndr = NULL;
615 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
617 // string::size_type pos = (*I).find("VALIDSIG ");
618 // if (_config->FindB("Debug::Vendor", false))
619 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
621 // if (pos != std::string::npos)
623 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
624 // if (_config->FindB("Debug::Vendor", false))
625 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
627 // Vndr = List.FindVendor(Fingerprint) != "";
628 // if (Vndr != NULL);
632 string::size_type pos
;
634 // check for missing sigs (that where not fatal because otherwise we had
637 string msg
= _("There are no public key available for the "
638 "following key IDs:\n");
639 pos
= Message
.find("NO_PUBKEY ");
640 if (pos
!= std::string::npos
)
642 string::size_type start
= pos
+strlen("NO_PUBKEY ");
643 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
644 missingkeys
+= (Fingerprint
);
646 if(!missingkeys
.empty())
647 _error
->Warning("%s", string(msg
+missingkeys
).c_str());
649 string Transformed
= MetaIndexParser
->GetExpectedDist();
651 if (Transformed
== "../project/experimental")
653 Transformed
= "experimental";
656 pos
= Transformed
.rfind('/');
657 if (pos
!= string::npos
)
659 Transformed
= Transformed
.substr(0, pos
);
662 if (Transformed
== ".")
667 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
669 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
670 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
671 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
674 if (MetaIndexParser
->CheckDist(Transformed
) == false)
676 // This might become fatal one day
677 // Status = StatAuthError;
678 // ErrorText = "Conflicting distribution; expected "
679 // + MetaIndexParser->GetExpectedDist() + " but got "
680 // + MetaIndexParser->GetDist();
682 if (!Transformed
.empty())
684 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
685 Desc
.Description
.c_str(),
687 MetaIndexParser
->GetDist().c_str());
694 // pkgAcqMetaIndex::Failed - no Release file present or no signature
695 // file present /*{{{*/
696 // ---------------------------------------------------------------------
698 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
700 if (AuthPass
== true)
702 // if we fail the authentication but got the file via a IMS-Hit
703 // this means that the file wasn't downloaded and that it might be
704 // just stale (server problem, proxy etc). we delete what we have
705 // queue it again without i-m-s
706 // alternatively we could just unlink the file and let the user try again
712 unlink(DestFile
.c_str());
714 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
715 DestFile
+= URItoFileName(RealURI
);
721 // gpgv method failed
722 _error
->Warning("GPG error: %s: %s",
723 Desc
.Description
.c_str(),
724 LookupTag(Message
,"Message").c_str());
728 // No Release file was present, or verification failed, so fall
729 // back to queueing Packages files without verification
735 // AcqArchive::AcqArchive - Constructor /*{{{*/
736 // ---------------------------------------------------------------------
737 /* This just sets up the initial fetch environment and queues the first
739 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
740 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
741 string
&StoreFilename
) :
742 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
743 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
746 Retries
= _config
->FindI("Acquire::Retries",0);
748 if (Version
.Arch() == 0)
750 _error
->Error(_("I wasn't able to locate a file for the %s package. "
751 "This might mean you need to manually fix this package. "
752 "(due to missing arch)"),
753 Version
.ParentPkg().Name());
757 /* We need to find a filename to determine the extension. We make the
758 assumption here that all the available sources for this version share
759 the same extension.. */
760 // Skip not source sources, they do not have file fields.
761 for (; Vf
.end() == false; Vf
++)
763 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
768 // Does not really matter here.. we are going to fail out below
769 if (Vf
.end() != true)
771 // If this fails to get a file name we will bomb out below.
772 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
773 if (_error
->PendingError() == true)
776 // Generate the final file name as: package_version_arch.foo
777 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
778 QuoteString(Version
.VerStr(),"_:") + '_' +
779 QuoteString(Version
.Arch(),"_:.") +
780 "." + flExtension(Parse
.FileName());
783 // check if we have one trusted source for the package. if so, switch
784 // to "TrustedOnly" mode
785 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
788 if (Sources
->FindIndex(i
.File(),Index
) == false)
790 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
792 std::cerr
<< "Checking index: " << Index
->Describe()
793 << "(Trusted=" << Index
->IsTrusted() << ")\n";
795 if (Index
->IsTrusted()) {
801 // "allow-unauthenticated" restores apts old fetching behaviour
802 // that means that e.g. unauthenticated file:// uris are higher
803 // priority than authenticated http:// uris
804 if (_config
->FindB("APT::Get::AllowUnauthenticated",false) == true)
808 if (QueueNext() == false && _error
->PendingError() == false)
809 _error
->Error(_("I wasn't able to locate file for the %s package. "
810 "This might mean you need to manually fix this package."),
811 Version
.ParentPkg().Name());
814 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
815 // ---------------------------------------------------------------------
816 /* This queues the next available file version for download. It checks if
817 the archive is already available in the cache and stashs the MD5 for
819 bool pkgAcqArchive::QueueNext()
821 for (; Vf
.end() == false; Vf
++)
823 // Ignore not source sources
824 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
827 // Try to cross match against the source list
829 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
832 // only try to get a trusted package from another source if that source
834 if(Trusted
&& !Index
->IsTrusted())
837 // Grab the text package record
838 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
839 if (_error
->PendingError() == true)
842 string PkgFile
= Parse
.FileName();
843 MD5
= Parse
.MD5Hash();
844 if (PkgFile
.empty() == true)
845 return _error
->Error(_("The package index files are corrupted. No Filename: "
846 "field for package %s."),
847 Version
.ParentPkg().Name());
849 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
850 Desc
.Description
= Index
->ArchiveInfo(Version
);
852 Desc
.ShortDesc
= Version
.ParentPkg().Name();
854 // See if we already have the file. (Legacy filenames)
855 FileSize
= Version
->Size
;
856 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
858 if (stat(FinalFile
.c_str(),&Buf
) == 0)
860 // Make sure the size matches
861 if ((unsigned)Buf
.st_size
== Version
->Size
)
866 StoreFilename
= DestFile
= FinalFile
;
870 /* Hmm, we have a file and its size does not match, this means it is
871 an old style mismatched arch */
872 unlink(FinalFile
.c_str());
875 // Check it again using the new style output filenames
876 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
877 if (stat(FinalFile
.c_str(),&Buf
) == 0)
879 // Make sure the size matches
880 if ((unsigned)Buf
.st_size
== Version
->Size
)
885 StoreFilename
= DestFile
= FinalFile
;
889 /* Hmm, we have a file and its size does not match, this shouldnt
891 unlink(FinalFile
.c_str());
894 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
896 // Check the destination file
897 if (stat(DestFile
.c_str(),&Buf
) == 0)
899 // Hmm, the partial file is too big, erase it
900 if ((unsigned)Buf
.st_size
> Version
->Size
)
901 unlink(DestFile
.c_str());
903 PartialSize
= Buf
.st_size
;
908 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
909 Desc
.Description
= Index
->ArchiveInfo(Version
);
911 Desc
.ShortDesc
= Version
.ParentPkg().Name();
920 // AcqArchive::Done - Finished fetching /*{{{*/
921 // ---------------------------------------------------------------------
923 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
924 pkgAcquire::MethodConfig
*Cfg
)
926 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
929 if (Size
!= Version
->Size
)
932 ErrorText
= _("Size mismatch");
937 if (Md5Hash
.empty() == false && MD5
.empty() == false)
942 ErrorText
= _("MD5Sum mismatch");
943 if(FileExists(DestFile
))
944 Rename(DestFile
,DestFile
+ ".FAILED");
949 // Grab the output filename
950 string FileName
= LookupTag(Message
,"Filename");
951 if (FileName
.empty() == true)
954 ErrorText
= "Method gave a blank filename";
960 // Reference filename
961 if (FileName
!= DestFile
)
963 StoreFilename
= DestFile
= FileName
;
968 // Done, move it into position
969 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
970 FinalFile
+= flNotDir(StoreFilename
);
971 Rename(DestFile
,FinalFile
);
973 StoreFilename
= DestFile
= FinalFile
;
977 // AcqArchive::Failed - Failure handler /*{{{*/
978 // ---------------------------------------------------------------------
979 /* Here we try other sources */
980 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
982 ErrorText
= LookupTag(Message
,"Message");
984 /* We don't really want to retry on failed media swaps, this prevents
985 that. An interesting observation is that permanent failures are not
987 if (Cnf
->Removable
== true &&
988 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
990 // Vf = Version.FileList();
991 while (Vf
.end() == false) Vf
++;
992 StoreFilename
= string();
993 Item::Failed(Message
,Cnf
);
997 if (QueueNext() == false)
999 // This is the retry counter
1001 Cnf
->LocalOnly
== false &&
1002 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1005 Vf
= Version
.FileList();
1006 if (QueueNext() == true)
1010 StoreFilename
= string();
1011 Item::Failed(Message
,Cnf
);
1015 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1016 // trusted source /*{{{*/
1017 // ---------------------------------------------------------------------
1018 bool pkgAcqArchive::IsTrusted()
1023 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1024 // ---------------------------------------------------------------------
1026 void pkgAcqArchive::Finished()
1028 if (Status
== pkgAcquire::Item::StatDone
&&
1031 StoreFilename
= string();
1035 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1036 // ---------------------------------------------------------------------
1037 /* The file is added to the queue */
1038 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1039 unsigned long Size
,string Dsc
,string ShortDesc
,
1040 const string
&DestDir
, const string
&DestFilename
) :
1041 Item(Owner
), Md5Hash(MD5
)
1043 Retries
= _config
->FindI("Acquire::Retries",0);
1045 if(!DestFilename
.empty())
1046 DestFile
= DestFilename
;
1047 else if(!DestDir
.empty())
1048 DestFile
= DestDir
+ "/" + flNotDir(URI
);
1050 DestFile
= flNotDir(URI
);
1054 Desc
.Description
= Dsc
;
1057 // Set the short description to the archive component
1058 Desc
.ShortDesc
= ShortDesc
;
1060 // Get the transfer sizes
1063 if (stat(DestFile
.c_str(),&Buf
) == 0)
1065 // Hmm, the partial file is too big, erase it
1066 if ((unsigned)Buf
.st_size
> Size
)
1067 unlink(DestFile
.c_str());
1069 PartialSize
= Buf
.st_size
;
1075 // AcqFile::Done - Item downloaded OK /*{{{*/
1076 // ---------------------------------------------------------------------
1078 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1079 pkgAcquire::MethodConfig
*Cnf
)
1082 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1087 ErrorText
= "MD5Sum mismatch";
1088 Rename(DestFile
,DestFile
+ ".FAILED");
1093 Item::Done(Message
,Size
,MD5
,Cnf
);
1095 string FileName
= LookupTag(Message
,"Filename");
1096 if (FileName
.empty() == true)
1099 ErrorText
= "Method gave a blank filename";
1105 // The files timestamp matches
1106 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1109 // We have to copy it into place
1110 if (FileName
!= DestFile
)
1113 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1114 Cnf
->Removable
== true)
1116 Desc
.URI
= "copy:" + FileName
;
1121 // Erase the file if it is a symlink so we can overwrite it
1123 if (lstat(DestFile
.c_str(),&St
) == 0)
1125 if (S_ISLNK(St
.st_mode
) != 0)
1126 unlink(DestFile
.c_str());
1130 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1132 ErrorText
= "Link to " + DestFile
+ " failure ";
1139 // AcqFile::Failed - Failure handler /*{{{*/
1140 // ---------------------------------------------------------------------
1141 /* Here we try other sources */
1142 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1144 ErrorText
= LookupTag(Message
,"Message");
1146 // This is the retry counter
1148 Cnf
->LocalOnly
== false &&
1149 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1156 Item::Failed(Message
,Cnf
);