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
);
324 // remove any partial downloaded sig-file. it may confuse proxies
325 // and is too small to warrant a partial download anyway
326 unlink(DestFile
.c_str());
329 Desc
.Description
= URIDesc
;
331 Desc
.ShortDesc
= ShortDesc
;
335 string Final
= _config
->FindDir("Dir::State::lists");
336 Final
+= URItoFileName(RealURI
);
338 if (stat(Final
.c_str(),&Buf
) == 0)
340 // File was already in place. It needs to be re-verified
341 // because Release might have changed, so Move it into partial
342 Rename(Final
,DestFile
);
348 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
349 // ---------------------------------------------------------------------
350 /* The only header we use is the last-modified header. */
351 string
pkgAcqMetaSig::Custom600Headers()
353 // mvo: we don't really need the last-modified header here
354 // 1) it points to "Final" and that was renamed to "DestFile"
355 // so it's never send anyway
356 // 2) because DestFIle is in partial/ we will send a partial request
357 // with if-range in the http method (or the equivalent for ftp).
358 // that should give the same result
360 string Final
= _config
->FindDir("Dir::State::lists");
361 Final
+= URItoFileName(RealURI
);
364 if (stat(Final
.c_str(),&Buf
) != 0)
365 return "\nIndex-File: true";
367 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
370 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
371 pkgAcquire::MethodConfig
*Cfg
)
373 Item::Done(Message
,Size
,MD5
,Cfg
);
375 string FileName
= LookupTag(Message
,"Filename");
376 if (FileName
.empty() == true)
379 ErrorText
= "Method gave a blank filename";
383 if (FileName
!= DestFile
)
385 // We have to copy it into place
387 Desc
.URI
= "copy:" + FileName
;
394 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
395 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
396 DestFile
, IndexTargets
, MetaIndexParser
);
400 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
402 // Delete any existing sigfile, so that this source isn't
403 // mistakenly trusted
404 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
405 unlink(Final
.c_str());
407 // queue a pkgAcqMetaIndex with no sigfile
408 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
409 "", IndexTargets
, MetaIndexParser
);
411 if (Cnf
->LocalOnly
== true ||
412 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
421 Item::Failed(Message
,Cnf
);
424 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
425 string URI
,string URIDesc
,string ShortDesc
,
427 const vector
<struct IndexTarget
*>* IndexTargets
,
428 indexRecords
* MetaIndexParser
) :
429 Item(Owner
), RealURI(URI
), SigFile(SigFile
)
431 this->AuthPass
= false;
432 this->MetaIndexParser
= MetaIndexParser
;
433 this->IndexTargets
= IndexTargets
;
434 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
435 DestFile
+= URItoFileName(URI
);
438 Desc
.Description
= URIDesc
;
440 Desc
.ShortDesc
= ShortDesc
;
447 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
448 // ---------------------------------------------------------------------
449 /* The only header we use is the last-modified header. */
450 string
pkgAcqMetaIndex::Custom600Headers()
452 string Final
= _config
->FindDir("Dir::State::lists");
453 Final
+= URItoFileName(RealURI
);
456 if (stat(Final
.c_str(),&Buf
) != 0)
457 return "\nIndex-File: true";
459 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
462 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
463 pkgAcquire::MethodConfig
*Cfg
)
465 Item::Done(Message
,Size
,MD5
,Cfg
);
467 // MetaIndexes are done in two passes: one to download the
468 // metaindex with an appropriate method, and a second to verify it
469 // with the gpgv method
471 if (AuthPass
== true)
477 RetrievalDone(Message
);
479 // Still more retrieving to do
484 // There was no signature file, so we are finished. Download
485 // the indexes without verification.
490 // There was a signature file, so pass it to gpgv for
493 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
494 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
495 << SigFile
<< "," << DestFile
<< ")\n";
497 Desc
.URI
= "gpgv:" + SigFile
;
504 void pkgAcqMetaIndex::RetrievalDone(string Message
)
506 // We have just finished downloading a Release file (it is not
509 string FileName
= LookupTag(Message
,"Filename");
510 if (FileName
.empty() == true)
513 ErrorText
= "Method gave a blank filename";
517 if (FileName
!= DestFile
)
520 Desc
.URI
= "copy:" + FileName
;
527 string FinalFile
= _config
->FindDir("Dir::State::lists");
528 FinalFile
+= URItoFileName(RealURI
);
530 // The files timestamp matches
531 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
533 // Move it into position
534 Rename(DestFile
,FinalFile
);
536 DestFile
= FinalFile
;
539 void pkgAcqMetaIndex::AuthDone(string Message
)
541 // At this point, the gpgv method has succeeded, so there is a
542 // valid signature from a key in the trusted keyring. We
543 // perform additional verification of its contents, and use them
544 // to verify the indexes we are about to download
546 if (!MetaIndexParser
->Load(DestFile
))
548 Status
= StatAuthError
;
549 ErrorText
= MetaIndexParser
->ErrorText
;
558 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
559 std::cerr
<< "Signature verification succeeded: "
560 << DestFile
<< std::endl
;
562 // Download further indexes with verification
565 // Done, move signature file into position
567 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
568 URItoFileName(RealURI
) + ".gpg";
569 Rename(SigFile
,VerifiedSigFile
);
570 chmod(VerifiedSigFile
.c_str(),0644);
573 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
575 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
576 Target
!= IndexTargets
->end();
579 string ExpectedIndexMD5
;
582 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
585 Status
= StatAuthError
;
586 ErrorText
= "Unable to find expected entry "
587 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
590 ExpectedIndexMD5
= Record
->MD5Hash
;
591 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
593 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
594 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
596 if (ExpectedIndexMD5
.empty())
598 Status
= StatAuthError
;
599 ErrorText
= "Unable to find MD5 sum for "
600 + (*Target
)->MetaKey
+ " in Meta-index file";
605 // Queue Packages file
606 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
607 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
611 bool pkgAcqMetaIndex::VerifyVendor()
613 // // Maybe this should be made available from above so we don't have
614 // // to read and parse it every time?
615 // pkgVendorList List;
616 // List.ReadMainList();
618 // const Vendor* Vndr = NULL;
619 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
621 // string::size_type pos = (*I).find("VALIDSIG ");
622 // if (_config->FindB("Debug::Vendor", false))
623 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
625 // if (pos != std::string::npos)
627 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
628 // if (_config->FindB("Debug::Vendor", false))
629 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
631 // Vndr = List.FindVendor(Fingerprint) != "";
632 // if (Vndr != NULL);
637 string Transformed
= MetaIndexParser
->GetExpectedDist();
639 if (Transformed
== "../project/experimental")
641 Transformed
= "experimental";
644 string::size_type pos
= Transformed
.rfind('/');
645 if (pos
!= string::npos
)
647 Transformed
= Transformed
.substr(0, pos
);
650 if (Transformed
== ".")
655 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
657 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
658 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
659 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
662 if (MetaIndexParser
->CheckDist(Transformed
) == false)
664 // This might become fatal one day
665 // Status = StatAuthError;
666 // ErrorText = "Conflicting distribution; expected "
667 // + MetaIndexParser->GetExpectedDist() + " but got "
668 // + MetaIndexParser->GetDist();
670 if (!Transformed
.empty())
672 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
673 Desc
.Description
.c_str(),
675 MetaIndexParser
->GetDist().c_str());
682 // pkgAcqMetaIndex::Failed - no Release file present or no signature
683 // file present /*{{{*/
684 // ---------------------------------------------------------------------
686 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
688 if (AuthPass
== true)
690 // gpgv method failed
691 _error
->Warning("GPG error: %s: %s",
692 Desc
.Description
.c_str(),
693 LookupTag(Message
,"Message").c_str());
696 // No Release file was present, or verification failed, so fall
697 // back to queueing Packages files without verification
703 // AcqArchive::AcqArchive - Constructor /*{{{*/
704 // ---------------------------------------------------------------------
705 /* This just sets up the initial fetch environment and queues the first
707 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
708 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
709 string
&StoreFilename
) :
710 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
711 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
714 Retries
= _config
->FindI("Acquire::Retries",0);
716 if (Version
.Arch() == 0)
718 _error
->Error(_("I wasn't able to locate a file for the %s package. "
719 "This might mean you need to manually fix this package. "
720 "(due to missing arch)"),
721 Version
.ParentPkg().Name());
725 /* We need to find a filename to determine the extension. We make the
726 assumption here that all the available sources for this version share
727 the same extension.. */
728 // Skip not source sources, they do not have file fields.
729 for (; Vf
.end() == false; Vf
++)
731 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
736 // Does not really matter here.. we are going to fail out below
737 if (Vf
.end() != true)
739 // If this fails to get a file name we will bomb out below.
740 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
741 if (_error
->PendingError() == true)
744 // Generate the final file name as: package_version_arch.foo
745 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
746 QuoteString(Version
.VerStr(),"_:") + '_' +
747 QuoteString(Version
.Arch(),"_:.") +
748 "." + flExtension(Parse
.FileName());
751 // check if we have one trusted source for the package. if so, switch
752 // to "TrustedOnly" mode
753 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
756 if (Sources
->FindIndex(i
.File(),Index
) == false)
758 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
760 std::cerr
<< "Checking index: " << Index
->Describe()
761 << "(Trusted=" << Index
->IsTrusted() << ")\n";
763 if (Index
->IsTrusted()) {
770 if (QueueNext() == false && _error
->PendingError() == false)
771 _error
->Error(_("I wasn't able to locate file for the %s package. "
772 "This might mean you need to manually fix this package."),
773 Version
.ParentPkg().Name());
776 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
777 // ---------------------------------------------------------------------
778 /* This queues the next available file version for download. It checks if
779 the archive is already available in the cache and stashs the MD5 for
781 bool pkgAcqArchive::QueueNext()
783 for (; Vf
.end() == false; Vf
++)
785 // Ignore not source sources
786 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
789 // Try to cross match against the source list
791 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
794 // only try to get a trusted package from another source if that source
796 if(Trusted
&& !Index
->IsTrusted())
799 // Grab the text package record
800 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
801 if (_error
->PendingError() == true)
804 string PkgFile
= Parse
.FileName();
805 MD5
= Parse
.MD5Hash();
806 if (PkgFile
.empty() == true)
807 return _error
->Error(_("The package index files are corrupted. No Filename: "
808 "field for package %s."),
809 Version
.ParentPkg().Name());
811 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
812 Desc
.Description
= Index
->ArchiveInfo(Version
);
814 Desc
.ShortDesc
= Version
.ParentPkg().Name();
816 // See if we already have the file. (Legacy filenames)
817 FileSize
= Version
->Size
;
818 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
820 if (stat(FinalFile
.c_str(),&Buf
) == 0)
822 // Make sure the size matches
823 if ((unsigned)Buf
.st_size
== Version
->Size
)
828 StoreFilename
= DestFile
= FinalFile
;
832 /* Hmm, we have a file and its size does not match, this means it is
833 an old style mismatched arch */
834 unlink(FinalFile
.c_str());
837 // Check it again using the new style output filenames
838 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
839 if (stat(FinalFile
.c_str(),&Buf
) == 0)
841 // Make sure the size matches
842 if ((unsigned)Buf
.st_size
== Version
->Size
)
847 StoreFilename
= DestFile
= FinalFile
;
851 /* Hmm, we have a file and its size does not match, this shouldnt
853 unlink(FinalFile
.c_str());
856 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
858 // Check the destination file
859 if (stat(DestFile
.c_str(),&Buf
) == 0)
861 // Hmm, the partial file is too big, erase it
862 if ((unsigned)Buf
.st_size
> Version
->Size
)
863 unlink(DestFile
.c_str());
865 PartialSize
= Buf
.st_size
;
870 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
871 Desc
.Description
= Index
->ArchiveInfo(Version
);
873 Desc
.ShortDesc
= Version
.ParentPkg().Name();
882 // AcqArchive::Done - Finished fetching /*{{{*/
883 // ---------------------------------------------------------------------
885 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
886 pkgAcquire::MethodConfig
*Cfg
)
888 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
891 if (Size
!= Version
->Size
)
894 ErrorText
= _("Size mismatch");
899 if (Md5Hash
.empty() == false && MD5
.empty() == false)
904 ErrorText
= _("MD5Sum mismatch");
905 Rename(DestFile
,DestFile
+ ".FAILED");
910 // Grab the output filename
911 string FileName
= LookupTag(Message
,"Filename");
912 if (FileName
.empty() == true)
915 ErrorText
= "Method gave a blank filename";
921 // Reference filename
922 if (FileName
!= DestFile
)
924 StoreFilename
= DestFile
= FileName
;
929 // Done, move it into position
930 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
931 FinalFile
+= flNotDir(StoreFilename
);
932 Rename(DestFile
,FinalFile
);
934 StoreFilename
= DestFile
= FinalFile
;
938 // AcqArchive::Failed - Failure handler /*{{{*/
939 // ---------------------------------------------------------------------
940 /* Here we try other sources */
941 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
943 ErrorText
= LookupTag(Message
,"Message");
945 /* We don't really want to retry on failed media swaps, this prevents
946 that. An interesting observation is that permanent failures are not
948 if (Cnf
->Removable
== true &&
949 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
951 // Vf = Version.FileList();
952 while (Vf
.end() == false) Vf
++;
953 StoreFilename
= string();
954 Item::Failed(Message
,Cnf
);
958 if (QueueNext() == false)
960 // This is the retry counter
962 Cnf
->LocalOnly
== false &&
963 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
966 Vf
= Version
.FileList();
967 if (QueueNext() == true)
971 StoreFilename
= string();
972 Item::Failed(Message
,Cnf
);
976 // AcqArchive::IsTrusted - Determine whether this archive comes from a
977 // trusted source /*{{{*/
978 // ---------------------------------------------------------------------
979 bool pkgAcqArchive::IsTrusted()
984 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
985 // ---------------------------------------------------------------------
987 void pkgAcqArchive::Finished()
989 if (Status
== pkgAcquire::Item::StatDone
&&
992 StoreFilename
= string();
996 // AcqFile::pkgAcqFile - Constructor /*{{{*/
997 // ---------------------------------------------------------------------
998 /* The file is added to the queue */
999 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1000 unsigned long Size
,string Dsc
,string ShortDesc
) :
1001 Item(Owner
), Md5Hash(MD5
)
1003 Retries
= _config
->FindI("Acquire::Retries",0);
1005 DestFile
= flNotDir(URI
);
1009 Desc
.Description
= Dsc
;
1012 // Set the short description to the archive component
1013 Desc
.ShortDesc
= ShortDesc
;
1015 // Get the transfer sizes
1018 if (stat(DestFile
.c_str(),&Buf
) == 0)
1020 // Hmm, the partial file is too big, erase it
1021 if ((unsigned)Buf
.st_size
> Size
)
1022 unlink(DestFile
.c_str());
1024 PartialSize
= Buf
.st_size
;
1030 // AcqFile::Done - Item downloaded OK /*{{{*/
1031 // ---------------------------------------------------------------------
1033 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1034 pkgAcquire::MethodConfig
*Cnf
)
1037 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1042 ErrorText
= "MD5Sum mismatch";
1043 Rename(DestFile
,DestFile
+ ".FAILED");
1048 Item::Done(Message
,Size
,MD5
,Cnf
);
1050 string FileName
= LookupTag(Message
,"Filename");
1051 if (FileName
.empty() == true)
1054 ErrorText
= "Method gave a blank filename";
1060 // The files timestamp matches
1061 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1064 // We have to copy it into place
1065 if (FileName
!= DestFile
)
1068 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1069 Cnf
->Removable
== true)
1071 Desc
.URI
= "copy:" + FileName
;
1076 // Erase the file if it is a symlink so we can overwrite it
1078 if (lstat(DestFile
.c_str(),&St
) == 0)
1080 if (S_ISLNK(St
.st_mode
) != 0)
1081 unlink(DestFile
.c_str());
1085 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1087 ErrorText
= "Link to " + DestFile
+ " failure ";
1094 // AcqFile::Failed - Failure handler /*{{{*/
1095 // ---------------------------------------------------------------------
1096 /* Here we try other sources */
1097 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1099 ErrorText
= LookupTag(Message
,"Message");
1101 // This is the retry counter
1103 Cnf
->LocalOnly
== false &&
1104 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1111 Item::Failed(Message
,Cnf
);