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
);
151 // autoselect the compression method
152 if(FileExists("/usr/bin/bzip2"))
153 CompressionExtension
= ".bz2";
155 CompressionExtension
= ".gz";
157 CompressionExtension
= comprExt
;
159 Desc
.URI
= URI
+ CompressionExtension
;
161 Desc
.Description
= URIDesc
;
163 Desc
.ShortDesc
= ShortDesc
;
168 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
169 // ---------------------------------------------------------------------
170 /* The only header we use is the last-modified header. */
171 string
pkgAcqIndex::Custom600Headers()
173 string Final
= _config
->FindDir("Dir::State::lists");
174 Final
+= URItoFileName(RealURI
);
177 if (stat(Final
.c_str(),&Buf
) != 0)
178 return "\nIndex-File: true";
180 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
184 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
186 // no .bz2 found, retry with .gz
187 if(Desc
.URI
.substr(Desc
.URI
.size()-3) == "bz2") {
188 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
190 // retry with a gzip one
191 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
192 ExpectedMD5
, string(".gz"));
200 Item::Failed(Message
,Cnf
);
204 // AcqIndex::Done - Finished a fetch /*{{{*/
205 // ---------------------------------------------------------------------
206 /* This goes through a number of states.. On the initial fetch the
207 method could possibly return an alternate filename which points
208 to the uncompressed version of the file. If this is so the file
209 is copied into the partial directory. In all other cases the file
210 is decompressed with a gzip uri. */
211 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
212 pkgAcquire::MethodConfig
*Cfg
)
214 Item::Done(Message
,Size
,MD5
,Cfg
);
216 if (Decompression
== true)
218 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
220 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
221 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
227 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
228 sum
.AddFD(Fd
.Fd(), Fd
.Size());
230 MD5
= (string
)sum
.Result();
233 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
235 Status
= StatAuthError
;
236 ErrorText
= _("MD5Sum mismatch");
237 Rename(DestFile
,DestFile
+ ".FAILED");
240 // Done, move it into position
241 string FinalFile
= _config
->FindDir("Dir::State::lists");
242 FinalFile
+= URItoFileName(RealURI
);
243 Rename(DestFile
,FinalFile
);
244 chmod(FinalFile
.c_str(),0644);
246 /* We restore the original name to DestFile so that the clean operation
248 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
249 DestFile
+= URItoFileName(RealURI
);
251 // Remove the compressed version.
253 unlink(DestFile
.c_str());
260 // Handle the unzipd case
261 string FileName
= LookupTag(Message
,"Alt-Filename");
262 if (FileName
.empty() == false)
264 // The files timestamp matches
265 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
268 Decompression
= true;
270 DestFile
+= ".decomp";
271 Desc
.URI
= "copy:" + FileName
;
277 FileName
= LookupTag(Message
,"Filename");
278 if (FileName
.empty() == true)
281 ErrorText
= "Method gave a blank filename";
284 // The files timestamp matches
285 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
288 if (FileName
== DestFile
)
293 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3);
296 decompProg
= "bzip2";
297 else if(compExt
== ".gz")
300 _error
->Error("Unsupported extension: %s", compExt
.c_str());
304 Decompression
= true;
305 DestFile
+= ".decomp";
306 Desc
.URI
= string(decompProg
) + ":" + FileName
;
311 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
312 string URI
,string URIDesc
,string ShortDesc
,
313 string MetaIndexURI
, string MetaIndexURIDesc
,
314 string MetaIndexShortDesc
,
315 const vector
<IndexTarget
*>* IndexTargets
,
316 indexRecords
* MetaIndexParser
) :
317 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
318 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
319 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
)
321 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
322 DestFile
+= URItoFileName(URI
);
324 // remove any partial downloaded sig-file in partial/.
325 // it may confuse proxies and is too small to warrant a
326 // partial download anyway
327 unlink(DestFile
.c_str());
330 Desc
.Description
= URIDesc
;
332 Desc
.ShortDesc
= ShortDesc
;
336 string Final
= _config
->FindDir("Dir::State::lists");
337 Final
+= URItoFileName(RealURI
);
339 if (stat(Final
.c_str(),&Buf
) == 0)
341 // File was already in place. It needs to be re-verified
342 // because Release might have changed, so Move it into partial
343 Rename(Final
,DestFile
);
349 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
350 // ---------------------------------------------------------------------
351 /* The only header we use is the last-modified header. */
352 string
pkgAcqMetaSig::Custom600Headers()
355 if (stat(DestFile
.c_str(),&Buf
) != 0)
356 return "\nIndex-File: true";
358 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
361 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
362 pkgAcquire::MethodConfig
*Cfg
)
364 Item::Done(Message
,Size
,MD5
,Cfg
);
366 string FileName
= LookupTag(Message
,"Filename");
367 if (FileName
.empty() == true)
370 ErrorText
= "Method gave a blank filename";
374 if (FileName
!= DestFile
)
376 // We have to copy it into place
378 Desc
.URI
= "copy:" + FileName
;
385 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
386 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
387 DestFile
, IndexTargets
, MetaIndexParser
);
391 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
393 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
395 // if we get a network error we fail gracefully
396 if(Status
== StatTransientNetworkError
)
398 Item::Failed(Message
,Cnf
);
399 // move the sigfile back on network failures (and re-authenticated?)
400 if(FileExists(DestFile
))
401 Rename(DestFile
,Final
);
403 // set the status back to , Item::Failed likes to reset it
404 Status
= pkgAcquire::Item::StatTransientNetworkError
;
408 // Delete any existing sigfile when the acquire failed
409 unlink(Final
.c_str());
411 // queue a pkgAcqMetaIndex with no sigfile
412 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
413 "", IndexTargets
, MetaIndexParser
);
415 if (Cnf
->LocalOnly
== true ||
416 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
425 Item::Failed(Message
,Cnf
);
428 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
429 string URI
,string URIDesc
,string ShortDesc
,
431 const vector
<struct IndexTarget
*>* IndexTargets
,
432 indexRecords
* MetaIndexParser
) :
433 Item(Owner
), RealURI(URI
), SigFile(SigFile
), AuthPass(false),
434 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
), IMSHit(false)
436 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
437 DestFile
+= URItoFileName(URI
);
440 Desc
.Description
= URIDesc
;
442 Desc
.ShortDesc
= ShortDesc
;
449 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
450 // ---------------------------------------------------------------------
451 /* The only header we use is the last-modified header. */
452 string
pkgAcqMetaIndex::Custom600Headers()
454 string Final
= _config
->FindDir("Dir::State::lists");
455 Final
+= URItoFileName(RealURI
);
458 if (stat(Final
.c_str(),&Buf
) != 0)
459 return "\nIndex-File: true";
461 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
464 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
465 pkgAcquire::MethodConfig
*Cfg
)
467 Item::Done(Message
,Size
,MD5
,Cfg
);
469 // MetaIndexes are done in two passes: one to download the
470 // metaindex with an appropriate method, and a second to verify it
471 // with the gpgv method
473 if (AuthPass
== true)
479 RetrievalDone(Message
);
481 // Still more retrieving to do
486 // There was no signature file, so we are finished. Download
487 // the indexes without verification.
492 // There was a signature file, so pass it to gpgv for
495 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
496 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
497 << SigFile
<< "," << DestFile
<< ")\n";
499 Desc
.URI
= "gpgv:" + SigFile
;
506 void pkgAcqMetaIndex::RetrievalDone(string Message
)
508 // We have just finished downloading a Release file (it is not
511 string FileName
= LookupTag(Message
,"Filename");
512 if (FileName
.empty() == true)
515 ErrorText
= "Method gave a blank filename";
519 if (FileName
!= DestFile
)
522 Desc
.URI
= "copy:" + FileName
;
527 // see if the download was a IMSHit
528 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
532 string FinalFile
= _config
->FindDir("Dir::State::lists");
533 FinalFile
+= URItoFileName(RealURI
);
535 // The files timestamp matches
536 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
538 // Move it into position
539 Rename(DestFile
,FinalFile
);
541 DestFile
= FinalFile
;
544 void pkgAcqMetaIndex::AuthDone(string Message
)
546 // At this point, the gpgv method has succeeded, so there is a
547 // valid signature from a key in the trusted keyring. We
548 // perform additional verification of its contents, and use them
549 // to verify the indexes we are about to download
551 if (!MetaIndexParser
->Load(DestFile
))
553 Status
= StatAuthError
;
554 ErrorText
= MetaIndexParser
->ErrorText
;
558 if (!VerifyVendor(Message
))
563 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
564 std::cerr
<< "Signature verification succeeded: "
565 << DestFile
<< std::endl
;
567 // Download further indexes with verification
570 // Done, move signature file into position
572 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
573 URItoFileName(RealURI
) + ".gpg";
574 Rename(SigFile
,VerifiedSigFile
);
575 chmod(VerifiedSigFile
.c_str(),0644);
578 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
580 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
581 Target
!= IndexTargets
->end();
584 string ExpectedIndexMD5
;
587 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
590 Status
= StatAuthError
;
591 ErrorText
= "Unable to find expected entry "
592 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
595 ExpectedIndexMD5
= Record
->MD5Hash
;
596 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
598 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
599 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
601 if (ExpectedIndexMD5
.empty())
603 Status
= StatAuthError
;
604 ErrorText
= "Unable to find MD5 sum for "
605 + (*Target
)->MetaKey
+ " in Meta-index file";
610 // Queue Packages file
611 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
612 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
616 bool pkgAcqMetaIndex::VerifyVendor(string Message
)
618 // // Maybe this should be made available from above so we don't have
619 // // to read and parse it every time?
620 // pkgVendorList List;
621 // List.ReadMainList();
623 // const Vendor* Vndr = NULL;
624 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
626 // string::size_type pos = (*I).find("VALIDSIG ");
627 // if (_config->FindB("Debug::Vendor", false))
628 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
630 // if (pos != std::string::npos)
632 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
633 // if (_config->FindB("Debug::Vendor", false))
634 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
636 // Vndr = List.FindVendor(Fingerprint) != "";
637 // if (Vndr != NULL);
641 string::size_type pos
;
643 // check for missing sigs (that where not fatal because otherwise we had
646 string msg
= _("There are no public key available for the "
647 "following key IDs:\n");
648 pos
= Message
.find("NO_PUBKEY ");
649 if (pos
!= std::string::npos
)
651 string::size_type start
= pos
+strlen("NO_PUBKEY ");
652 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
653 missingkeys
+= (Fingerprint
);
655 if(!missingkeys
.empty())
656 _error
->Warning("%s", string(msg
+missingkeys
).c_str());
658 string Transformed
= MetaIndexParser
->GetExpectedDist();
660 if (Transformed
== "../project/experimental")
662 Transformed
= "experimental";
665 pos
= Transformed
.rfind('/');
666 if (pos
!= string::npos
)
668 Transformed
= Transformed
.substr(0, pos
);
671 if (Transformed
== ".")
676 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
678 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
679 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
680 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
683 if (MetaIndexParser
->CheckDist(Transformed
) == false)
685 // This might become fatal one day
686 // Status = StatAuthError;
687 // ErrorText = "Conflicting distribution; expected "
688 // + MetaIndexParser->GetExpectedDist() + " but got "
689 // + MetaIndexParser->GetDist();
691 if (!Transformed
.empty())
693 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
694 Desc
.Description
.c_str(),
696 MetaIndexParser
->GetDist().c_str());
703 // pkgAcqMetaIndex::Failed - no Release file present or no signature
704 // file present /*{{{*/
705 // ---------------------------------------------------------------------
707 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
709 if (AuthPass
== true)
711 // if we fail the authentication but got the file via a IMS-Hit
712 // this means that the file wasn't downloaded and that it might be
713 // just stale (server problem, proxy etc). we delete what we have
714 // queue it again without i-m-s
715 // alternatively we could just unlink the file and let the user try again
721 unlink(DestFile
.c_str());
723 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
724 DestFile
+= URItoFileName(RealURI
);
730 // gpgv method failed
731 _error
->Warning("GPG error: %s: %s",
732 Desc
.Description
.c_str(),
733 LookupTag(Message
,"Message").c_str());
737 // No Release file was present, or verification failed, so fall
738 // back to queueing Packages files without verification
744 // AcqArchive::AcqArchive - Constructor /*{{{*/
745 // ---------------------------------------------------------------------
746 /* This just sets up the initial fetch environment and queues the first
748 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
749 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
750 string
&StoreFilename
) :
751 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
752 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
755 Retries
= _config
->FindI("Acquire::Retries",0);
757 if (Version
.Arch() == 0)
759 _error
->Error(_("I wasn't able to locate a file for the %s package. "
760 "This might mean you need to manually fix this package. "
761 "(due to missing arch)"),
762 Version
.ParentPkg().Name());
766 /* We need to find a filename to determine the extension. We make the
767 assumption here that all the available sources for this version share
768 the same extension.. */
769 // Skip not source sources, they do not have file fields.
770 for (; Vf
.end() == false; Vf
++)
772 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
777 // Does not really matter here.. we are going to fail out below
778 if (Vf
.end() != true)
780 // If this fails to get a file name we will bomb out below.
781 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
782 if (_error
->PendingError() == true)
785 // Generate the final file name as: package_version_arch.foo
786 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
787 QuoteString(Version
.VerStr(),"_:") + '_' +
788 QuoteString(Version
.Arch(),"_:.") +
789 "." + flExtension(Parse
.FileName());
792 // check if we have one trusted source for the package. if so, switch
793 // to "TrustedOnly" mode
794 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
797 if (Sources
->FindIndex(i
.File(),Index
) == false)
799 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
801 std::cerr
<< "Checking index: " << Index
->Describe()
802 << "(Trusted=" << Index
->IsTrusted() << ")\n";
804 if (Index
->IsTrusted()) {
810 // "allow-unauthenticated" restores apts old fetching behaviour
811 // that means that e.g. unauthenticated file:// uris are higher
812 // priority than authenticated http:// uris
813 if (_config
->FindB("APT::Get::AllowUnauthenticated",false) == true)
817 if (QueueNext() == false && _error
->PendingError() == false)
818 _error
->Error(_("I wasn't able to locate file for the %s package. "
819 "This might mean you need to manually fix this package."),
820 Version
.ParentPkg().Name());
823 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
824 // ---------------------------------------------------------------------
825 /* This queues the next available file version for download. It checks if
826 the archive is already available in the cache and stashs the MD5 for
828 bool pkgAcqArchive::QueueNext()
830 for (; Vf
.end() == false; Vf
++)
832 // Ignore not source sources
833 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
836 // Try to cross match against the source list
838 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
841 // only try to get a trusted package from another source if that source
843 if(Trusted
&& !Index
->IsTrusted())
846 // Grab the text package record
847 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
848 if (_error
->PendingError() == true)
851 string PkgFile
= Parse
.FileName();
852 MD5
= Parse
.MD5Hash();
853 if (PkgFile
.empty() == true)
854 return _error
->Error(_("The package index files are corrupted. No Filename: "
855 "field for package %s."),
856 Version
.ParentPkg().Name());
858 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
859 Desc
.Description
= Index
->ArchiveInfo(Version
);
861 Desc
.ShortDesc
= Version
.ParentPkg().Name();
863 // See if we already have the file. (Legacy filenames)
864 FileSize
= Version
->Size
;
865 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
867 if (stat(FinalFile
.c_str(),&Buf
) == 0)
869 // Make sure the size matches
870 if ((unsigned)Buf
.st_size
== Version
->Size
)
875 StoreFilename
= DestFile
= FinalFile
;
879 /* Hmm, we have a file and its size does not match, this means it is
880 an old style mismatched arch */
881 unlink(FinalFile
.c_str());
884 // Check it again using the new style output filenames
885 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
886 if (stat(FinalFile
.c_str(),&Buf
) == 0)
888 // Make sure the size matches
889 if ((unsigned)Buf
.st_size
== Version
->Size
)
894 StoreFilename
= DestFile
= FinalFile
;
898 /* Hmm, we have a file and its size does not match, this shouldnt
900 unlink(FinalFile
.c_str());
903 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
905 // Check the destination file
906 if (stat(DestFile
.c_str(),&Buf
) == 0)
908 // Hmm, the partial file is too big, erase it
909 if ((unsigned)Buf
.st_size
> Version
->Size
)
910 unlink(DestFile
.c_str());
912 PartialSize
= Buf
.st_size
;
917 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
918 Desc
.Description
= Index
->ArchiveInfo(Version
);
920 Desc
.ShortDesc
= Version
.ParentPkg().Name();
929 // AcqArchive::Done - Finished fetching /*{{{*/
930 // ---------------------------------------------------------------------
932 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
933 pkgAcquire::MethodConfig
*Cfg
)
935 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
938 if (Size
!= Version
->Size
)
941 ErrorText
= _("Size mismatch");
946 if (Md5Hash
.empty() == false && MD5
.empty() == false)
951 ErrorText
= _("MD5Sum mismatch");
952 if(FileExists(DestFile
))
953 Rename(DestFile
,DestFile
+ ".FAILED");
958 // Grab the output filename
959 string FileName
= LookupTag(Message
,"Filename");
960 if (FileName
.empty() == true)
963 ErrorText
= "Method gave a blank filename";
969 // Reference filename
970 if (FileName
!= DestFile
)
972 StoreFilename
= DestFile
= FileName
;
977 // Done, move it into position
978 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
979 FinalFile
+= flNotDir(StoreFilename
);
980 Rename(DestFile
,FinalFile
);
982 StoreFilename
= DestFile
= FinalFile
;
986 // AcqArchive::Failed - Failure handler /*{{{*/
987 // ---------------------------------------------------------------------
988 /* Here we try other sources */
989 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
991 ErrorText
= LookupTag(Message
,"Message");
993 /* We don't really want to retry on failed media swaps, this prevents
994 that. An interesting observation is that permanent failures are not
996 if (Cnf
->Removable
== true &&
997 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
999 // Vf = Version.FileList();
1000 while (Vf
.end() == false) Vf
++;
1001 StoreFilename
= string();
1002 Item::Failed(Message
,Cnf
);
1006 if (QueueNext() == false)
1008 // This is the retry counter
1010 Cnf
->LocalOnly
== false &&
1011 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1014 Vf
= Version
.FileList();
1015 if (QueueNext() == true)
1019 StoreFilename
= string();
1020 Item::Failed(Message
,Cnf
);
1024 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1025 // trusted source /*{{{*/
1026 // ---------------------------------------------------------------------
1027 bool pkgAcqArchive::IsTrusted()
1032 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1033 // ---------------------------------------------------------------------
1035 void pkgAcqArchive::Finished()
1037 if (Status
== pkgAcquire::Item::StatDone
&&
1040 StoreFilename
= string();
1044 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1045 // ---------------------------------------------------------------------
1046 /* The file is added to the queue */
1047 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1048 unsigned long Size
,string Dsc
,string ShortDesc
,
1049 const string
&DestDir
, const string
&DestFilename
) :
1050 Item(Owner
), Md5Hash(MD5
)
1052 Retries
= _config
->FindI("Acquire::Retries",0);
1054 if(!DestFilename
.empty())
1055 DestFile
= DestFilename
;
1056 else if(!DestDir
.empty())
1057 DestFile
= DestDir
+ "/" + flNotDir(URI
);
1059 DestFile
= flNotDir(URI
);
1063 Desc
.Description
= Dsc
;
1066 // Set the short description to the archive component
1067 Desc
.ShortDesc
= ShortDesc
;
1069 // Get the transfer sizes
1072 if (stat(DestFile
.c_str(),&Buf
) == 0)
1074 // Hmm, the partial file is too big, erase it
1075 if ((unsigned)Buf
.st_size
> Size
)
1076 unlink(DestFile
.c_str());
1078 PartialSize
= Buf
.st_size
;
1084 // AcqFile::Done - Item downloaded OK /*{{{*/
1085 // ---------------------------------------------------------------------
1087 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1088 pkgAcquire::MethodConfig
*Cnf
)
1091 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1096 ErrorText
= "MD5Sum mismatch";
1097 Rename(DestFile
,DestFile
+ ".FAILED");
1102 Item::Done(Message
,Size
,MD5
,Cnf
);
1104 string FileName
= LookupTag(Message
,"Filename");
1105 if (FileName
.empty() == true)
1108 ErrorText
= "Method gave a blank filename";
1114 // The files timestamp matches
1115 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1118 // We have to copy it into place
1119 if (FileName
!= DestFile
)
1122 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1123 Cnf
->Removable
== true)
1125 Desc
.URI
= "copy:" + FileName
;
1130 // Erase the file if it is a symlink so we can overwrite it
1132 if (lstat(DestFile
.c_str(),&St
) == 0)
1134 if (S_ISLNK(St
.st_mode
) != 0)
1135 unlink(DestFile
.c_str());
1139 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1141 ErrorText
= "Link to " + DestFile
+ " failure ";
1148 // AcqFile::Failed - Failure handler /*{{{*/
1149 // ---------------------------------------------------------------------
1150 /* Here we try other sources */
1151 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1153 ErrorText
= LookupTag(Message
,"Message");
1155 // This is the retry counter
1157 Cnf
->LocalOnly
== false &&
1158 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1165 Item::Failed(Message
,Cnf
);