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 // queue a pkgAcqMetaIndex with no sigfile
397 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
398 "", IndexTargets
, MetaIndexParser
);
400 if (Cnf
->LocalOnly
== true ||
401 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
410 Item::Failed(Message
,Cnf
);
413 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
414 string URI
,string URIDesc
,string ShortDesc
,
416 const vector
<struct IndexTarget
*>* IndexTargets
,
417 indexRecords
* MetaIndexParser
) :
418 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
420 this->AuthPass
= false;
421 this->MetaIndexParser
= MetaIndexParser
;
422 this->IndexTargets
= IndexTargets
;
423 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
424 DestFile
+= URItoFileName(URI
);
427 Desc
.Description
= URIDesc
;
429 Desc
.ShortDesc
= ShortDesc
;
436 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
437 // ---------------------------------------------------------------------
438 /* The only header we use is the last-modified header. */
439 string
pkgAcqMetaIndex::Custom600Headers()
441 string Final
= _config
->FindDir("Dir::State::lists");
442 Final
+= URItoFileName(RealURI
);
445 if (stat(Final
.c_str(),&Buf
) != 0)
446 return "\nIndex-File: true";
448 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
451 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
452 pkgAcquire::MethodConfig
*Cfg
)
454 Item::Done(Message
,Size
,MD5
,Cfg
);
456 // MetaIndexes are done in two passes: one to download the
457 // metaindex with an appropriate method, and a second to verify it
458 // with the gpgv method
460 if (AuthPass
== true)
466 RetrievalDone(Message
);
468 // Still more retrieving to do
473 // There was no signature file, so we are finished. Download
474 // the indexes without verification.
479 // There was a signature file, so pass it to gpgv for
482 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
483 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
484 << SigFile
<< "," << DestFile
<< ")\n";
486 Desc
.URI
= "gpgv:" + SigFile
;
493 void pkgAcqMetaIndex::RetrievalDone(string Message
)
495 // We have just finished downloading a Release file (it is not
498 string FileName
= LookupTag(Message
,"Filename");
499 if (FileName
.empty() == true)
502 ErrorText
= "Method gave a blank filename";
506 if (FileName
!= DestFile
)
509 Desc
.URI
= "copy:" + FileName
;
516 string FinalFile
= _config
->FindDir("Dir::State::lists");
517 FinalFile
+= URItoFileName(RealURI
);
519 // The files timestamp matches
520 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
522 // Move it into position
523 Rename(DestFile
,FinalFile
);
525 DestFile
= FinalFile
;
528 void pkgAcqMetaIndex::AuthDone(string Message
)
530 // At this point, the gpgv method has succeeded, so there is a
531 // valid signature from a key in the trusted keyring. We
532 // perform additional verification of its contents, and use them
533 // to verify the indexes we are about to download
535 if (!MetaIndexParser
->Load(DestFile
))
537 Status
= StatAuthError
;
538 ErrorText
= MetaIndexParser
->ErrorText
;
547 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
548 std::cerr
<< "Signature verification succeeded: "
549 << DestFile
<< std::endl
;
551 // Download further indexes with verification
554 // Done, move signature file into position
556 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
557 URItoFileName(RealURI
) + ".gpg";
558 Rename(SigFile
,VerifiedSigFile
);
559 chmod(VerifiedSigFile
.c_str(),0644);
562 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
564 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
565 Target
!= IndexTargets
->end();
568 string ExpectedIndexMD5
;
571 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
574 Status
= StatAuthError
;
575 ErrorText
= "Unable to find expected entry "
576 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
579 ExpectedIndexMD5
= Record
->MD5Hash
;
580 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
582 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
583 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
585 if (ExpectedIndexMD5
.empty())
587 Status
= StatAuthError
;
588 ErrorText
= "Unable to find MD5 sum for "
589 + (*Target
)->MetaKey
+ " in Meta-index file";
594 // Queue Packages file
595 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
596 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
600 bool pkgAcqMetaIndex::VerifyVendor()
602 // // Maybe this should be made available from above so we don't have
603 // // to read and parse it every time?
604 // pkgVendorList List;
605 // List.ReadMainList();
607 // const Vendor* Vndr = NULL;
608 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
610 // string::size_type pos = (*I).find("VALIDSIG ");
611 // if (_config->FindB("Debug::Vendor", false))
612 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
614 // if (pos != std::string::npos)
616 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
617 // if (_config->FindB("Debug::Vendor", false))
618 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
620 // Vndr = List.FindVendor(Fingerprint) != "";
621 // if (Vndr != NULL);
626 string Transformed
= MetaIndexParser
->GetExpectedDist();
628 if (Transformed
== "../project/experimental")
630 Transformed
= "experimental";
633 string::size_type pos
= Transformed
.rfind('/');
634 if (pos
!= string::npos
)
636 Transformed
= Transformed
.substr(0, pos
);
639 if (Transformed
== ".")
644 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
646 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
647 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
648 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
651 if (MetaIndexParser
->CheckDist(Transformed
) == false)
653 // This might become fatal one day
654 // Status = StatAuthError;
655 // ErrorText = "Conflicting distribution; expected "
656 // + MetaIndexParser->GetExpectedDist() + " but got "
657 // + MetaIndexParser->GetDist();
659 if (!Transformed
.empty())
661 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
662 Desc
.Description
.c_str(),
664 MetaIndexParser
->GetDist().c_str());
671 // pkgAcqMetaIndex::Failed - no Release file present or no signature
672 // file present /*{{{*/
673 // ---------------------------------------------------------------------
675 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
677 if (AuthPass
== true)
679 // gpgv method failed
680 _error
->Warning("GPG error: %s: %s",
681 Desc
.Description
.c_str(),
682 LookupTag(Message
,"Message").c_str());
685 // No Release file was present, or verification failed, so fall
686 // back to queueing Packages files without verification
692 // AcqArchive::AcqArchive - Constructor /*{{{*/
693 // ---------------------------------------------------------------------
694 /* This just sets up the initial fetch environment and queues the first
696 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
697 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
698 string
&StoreFilename
) :
699 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
700 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
703 Retries
= _config
->FindI("Acquire::Retries",0);
705 if (Version
.Arch() == 0)
707 _error
->Error(_("I wasn't able to locate a file for the %s package. "
708 "This might mean you need to manually fix this package. "
709 "(due to missing arch)"),
710 Version
.ParentPkg().Name());
714 /* We need to find a filename to determine the extension. We make the
715 assumption here that all the available sources for this version share
716 the same extension.. */
717 // Skip not source sources, they do not have file fields.
718 for (; Vf
.end() == false; Vf
++)
720 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
725 // Does not really matter here.. we are going to fail out below
726 if (Vf
.end() != true)
728 // If this fails to get a file name we will bomb out below.
729 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
730 if (_error
->PendingError() == true)
733 // Generate the final file name as: package_version_arch.foo
734 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
735 QuoteString(Version
.VerStr(),"_:") + '_' +
736 QuoteString(Version
.Arch(),"_:.") +
737 "." + flExtension(Parse
.FileName());
740 // check if we have one trusted source for the package. if so, switch
741 // to "TrustedOnly" mode
742 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
745 if (Sources
->FindIndex(i
.File(),Index
) == false)
747 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
749 std::cerr
<< "Checking index: " << Index
->Describe()
750 << "(Trusted=" << Index
->IsTrusted() << ")\n";
752 if (Index
->IsTrusted()) {
759 if (QueueNext() == false && _error
->PendingError() == false)
760 _error
->Error(_("I wasn't able to locate file for the %s package. "
761 "This might mean you need to manually fix this package."),
762 Version
.ParentPkg().Name());
765 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
766 // ---------------------------------------------------------------------
767 /* This queues the next available file version for download. It checks if
768 the archive is already available in the cache and stashs the MD5 for
770 bool pkgAcqArchive::QueueNext()
772 for (; Vf
.end() == false; Vf
++)
774 // Ignore not source sources
775 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
778 // Try to cross match against the source list
780 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
783 // only try to get a trusted package from another source if that source
785 if(Trusted
&& !Index
->IsTrusted())
788 // Grab the text package record
789 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
790 if (_error
->PendingError() == true)
793 string PkgFile
= Parse
.FileName();
794 MD5
= Parse
.MD5Hash();
795 if (PkgFile
.empty() == true)
796 return _error
->Error(_("The package index files are corrupted. No Filename: "
797 "field for package %s."),
798 Version
.ParentPkg().Name());
800 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
801 Desc
.Description
= Index
->ArchiveInfo(Version
);
803 Desc
.ShortDesc
= Version
.ParentPkg().Name();
805 // See if we already have the file. (Legacy filenames)
806 FileSize
= Version
->Size
;
807 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
809 if (stat(FinalFile
.c_str(),&Buf
) == 0)
811 // Make sure the size matches
812 if ((unsigned)Buf
.st_size
== Version
->Size
)
817 StoreFilename
= DestFile
= FinalFile
;
821 /* Hmm, we have a file and its size does not match, this means it is
822 an old style mismatched arch */
823 unlink(FinalFile
.c_str());
826 // Check it again using the new style output filenames
827 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
828 if (stat(FinalFile
.c_str(),&Buf
) == 0)
830 // Make sure the size matches
831 if ((unsigned)Buf
.st_size
== Version
->Size
)
836 StoreFilename
= DestFile
= FinalFile
;
840 /* Hmm, we have a file and its size does not match, this shouldnt
842 unlink(FinalFile
.c_str());
845 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
847 // Check the destination file
848 if (stat(DestFile
.c_str(),&Buf
) == 0)
850 // Hmm, the partial file is too big, erase it
851 if ((unsigned)Buf
.st_size
> Version
->Size
)
852 unlink(DestFile
.c_str());
854 PartialSize
= Buf
.st_size
;
859 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
860 Desc
.Description
= Index
->ArchiveInfo(Version
);
862 Desc
.ShortDesc
= Version
.ParentPkg().Name();
871 // AcqArchive::Done - Finished fetching /*{{{*/
872 // ---------------------------------------------------------------------
874 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
875 pkgAcquire::MethodConfig
*Cfg
)
877 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
880 if (Size
!= Version
->Size
)
883 ErrorText
= _("Size mismatch");
888 if (Md5Hash
.empty() == false && MD5
.empty() == false)
893 ErrorText
= _("MD5Sum mismatch");
894 Rename(DestFile
,DestFile
+ ".FAILED");
899 // Grab the output filename
900 string FileName
= LookupTag(Message
,"Filename");
901 if (FileName
.empty() == true)
904 ErrorText
= "Method gave a blank filename";
910 // Reference filename
911 if (FileName
!= DestFile
)
913 StoreFilename
= DestFile
= FileName
;
918 // Done, move it into position
919 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
920 FinalFile
+= flNotDir(StoreFilename
);
921 Rename(DestFile
,FinalFile
);
923 StoreFilename
= DestFile
= FinalFile
;
927 // AcqArchive::Failed - Failure handler /*{{{*/
928 // ---------------------------------------------------------------------
929 /* Here we try other sources */
930 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
932 ErrorText
= LookupTag(Message
,"Message");
934 /* We don't really want to retry on failed media swaps, this prevents
935 that. An interesting observation is that permanent failures are not
937 if (Cnf
->Removable
== true &&
938 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
940 // Vf = Version.FileList();
941 while (Vf
.end() == false) Vf
++;
942 StoreFilename
= string();
943 Item::Failed(Message
,Cnf
);
947 if (QueueNext() == false)
949 // This is the retry counter
951 Cnf
->LocalOnly
== false &&
952 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
955 Vf
= Version
.FileList();
956 if (QueueNext() == true)
960 StoreFilename
= string();
961 Item::Failed(Message
,Cnf
);
965 // AcqArchive::IsTrusted - Determine whether this archive comes from a
966 // trusted source /*{{{*/
967 // ---------------------------------------------------------------------
968 bool pkgAcqArchive::IsTrusted()
973 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
974 // ---------------------------------------------------------------------
976 void pkgAcqArchive::Finished()
978 if (Status
== pkgAcquire::Item::StatDone
&&
981 StoreFilename
= string();
985 // AcqFile::pkgAcqFile - Constructor /*{{{*/
986 // ---------------------------------------------------------------------
987 /* The file is added to the queue */
988 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
989 unsigned long Size
,string Dsc
,string ShortDesc
) :
990 Item(Owner
), Md5Hash(MD5
)
992 Retries
= _config
->FindI("Acquire::Retries",0);
994 DestFile
= flNotDir(URI
);
998 Desc
.Description
= Dsc
;
1001 // Set the short description to the archive component
1002 Desc
.ShortDesc
= ShortDesc
;
1004 // Get the transfer sizes
1007 if (stat(DestFile
.c_str(),&Buf
) == 0)
1009 // Hmm, the partial file is too big, erase it
1010 if ((unsigned)Buf
.st_size
> Size
)
1011 unlink(DestFile
.c_str());
1013 PartialSize
= Buf
.st_size
;
1019 // AcqFile::Done - Item downloaded OK /*{{{*/
1020 // ---------------------------------------------------------------------
1022 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1023 pkgAcquire::MethodConfig
*Cnf
)
1026 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1031 ErrorText
= "MD5Sum mismatch";
1032 Rename(DestFile
,DestFile
+ ".FAILED");
1037 Item::Done(Message
,Size
,MD5
,Cnf
);
1039 string FileName
= LookupTag(Message
,"Filename");
1040 if (FileName
.empty() == true)
1043 ErrorText
= "Method gave a blank filename";
1049 // The files timestamp matches
1050 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1053 // We have to copy it into place
1054 if (FileName
!= DestFile
)
1057 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1058 Cnf
->Removable
== true)
1060 Desc
.URI
= "copy:" + FileName
;
1065 // Erase the file if it is a symlink so we can overwrite it
1067 if (lstat(DestFile
.c_str(),&St
) == 0)
1069 if (S_ISLNK(St
.st_mode
) != 0)
1070 unlink(DestFile
.c_str());
1074 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1076 ErrorText
= "Link to " + DestFile
+ " failure ";
1083 // AcqFile::Failed - Failure handler /*{{{*/
1084 // ---------------------------------------------------------------------
1085 /* Here we try other sources */
1086 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1088 ErrorText
= LookupTag(Message
,"Message");
1090 // This is the retry counter
1092 Cnf
->LocalOnly
== false &&
1093 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1100 Item::Failed(Message
,Cnf
);