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,Desc
.URI
.size()-1) == "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,Desc
.URI
.size()-1);
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
)
320 this->MetaIndexParser
= MetaIndexParser
;
321 this->IndexTargets
= IndexTargets
;
322 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
323 DestFile
+= URItoFileName(URI
);
325 // remove any partial downloaded sig-file. it may confuse proxies
326 // and is too small to warrant a 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
);
344 // unlink the file and do not try to use I-M-S and Last-Modified
345 // if the users proxy is broken
346 if(_config
->FindB("Acquire::BrokenProxy", false) == true) {
347 std::cerr
<< "forcing re-get of the signature file as requested" << std::endl
;
348 unlink(DestFile
.c_str());
355 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
356 // ---------------------------------------------------------------------
357 /* The only header we use is the last-modified header. */
358 string
pkgAcqMetaSig::Custom600Headers()
361 if (stat(DestFile
.c_str(),&Buf
) != 0)
362 return "\nIndex-File: true";
364 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
367 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
368 pkgAcquire::MethodConfig
*Cfg
)
370 Item::Done(Message
,Size
,MD5
,Cfg
);
372 string FileName
= LookupTag(Message
,"Filename");
373 if (FileName
.empty() == true)
376 ErrorText
= "Method gave a blank filename";
380 if (FileName
!= DestFile
)
382 // We have to copy it into place
384 Desc
.URI
= "copy:" + FileName
;
391 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
392 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
393 DestFile
, IndexTargets
, MetaIndexParser
);
397 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
399 // Delete any existing sigfile, so that this source isn't
400 // mistakenly trusted
401 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
402 unlink(Final
.c_str());
404 // if we get a timeout if fail
405 if(LookupTag(Message
,"FailReason") == "Timeout" ||
406 LookupTag(Message
,"FailReason") == "TmpResolveFailure") {
407 Item::Failed(Message
,Cnf
);
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
)
435 this->AuthPass
= false;
436 this->MetaIndexParser
= MetaIndexParser
;
437 this->IndexTargets
= IndexTargets
;
438 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
439 DestFile
+= URItoFileName(URI
);
442 Desc
.Description
= URIDesc
;
444 Desc
.ShortDesc
= ShortDesc
;
451 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
452 // ---------------------------------------------------------------------
453 /* The only header we use is the last-modified header. */
454 string
pkgAcqMetaIndex::Custom600Headers()
456 string Final
= _config
->FindDir("Dir::State::lists");
457 Final
+= URItoFileName(RealURI
);
460 if (stat(Final
.c_str(),&Buf
) != 0)
461 return "\nIndex-File: true";
463 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
466 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
467 pkgAcquire::MethodConfig
*Cfg
)
469 Item::Done(Message
,Size
,MD5
,Cfg
);
471 // MetaIndexes are done in two passes: one to download the
472 // metaindex with an appropriate method, and a second to verify it
473 // with the gpgv method
475 if (AuthPass
== true)
481 RetrievalDone(Message
);
483 // Still more retrieving to do
488 // There was no signature file, so we are finished. Download
489 // the indexes without verification.
494 // There was a signature file, so pass it to gpgv for
497 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
498 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
499 << SigFile
<< "," << DestFile
<< ")\n";
501 Desc
.URI
= "gpgv:" + SigFile
;
508 void pkgAcqMetaIndex::RetrievalDone(string Message
)
510 // We have just finished downloading a Release file (it is not
513 string FileName
= LookupTag(Message
,"Filename");
514 if (FileName
.empty() == true)
517 ErrorText
= "Method gave a blank filename";
521 if (FileName
!= DestFile
)
524 Desc
.URI
= "copy:" + FileName
;
531 string FinalFile
= _config
->FindDir("Dir::State::lists");
532 FinalFile
+= URItoFileName(RealURI
);
534 // The files timestamp matches
535 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
537 // Move it into position
538 Rename(DestFile
,FinalFile
);
540 DestFile
= FinalFile
;
543 void pkgAcqMetaIndex::AuthDone(string Message
)
545 // At this point, the gpgv method has succeeded, so there is a
546 // valid signature from a key in the trusted keyring. We
547 // perform additional verification of its contents, and use them
548 // to verify the indexes we are about to download
550 if (!MetaIndexParser
->Load(DestFile
))
552 Status
= StatAuthError
;
553 ErrorText
= MetaIndexParser
->ErrorText
;
562 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
563 std::cerr
<< "Signature verification succeeded: "
564 << DestFile
<< std::endl
;
566 // Download further indexes with verification
569 // Done, move signature file into position
571 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
572 URItoFileName(RealURI
) + ".gpg";
573 Rename(SigFile
,VerifiedSigFile
);
574 chmod(VerifiedSigFile
.c_str(),0644);
577 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
579 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
580 Target
!= IndexTargets
->end();
583 string ExpectedIndexMD5
;
586 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
589 Status
= StatAuthError
;
590 ErrorText
= "Unable to find expected entry "
591 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
594 ExpectedIndexMD5
= Record
->MD5Hash
;
595 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
597 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
598 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
600 if (ExpectedIndexMD5
.empty())
602 Status
= StatAuthError
;
603 ErrorText
= "Unable to find MD5 sum for "
604 + (*Target
)->MetaKey
+ " in Meta-index file";
609 // Queue Packages file
610 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
611 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
615 bool pkgAcqMetaIndex::VerifyVendor()
617 // // Maybe this should be made available from above so we don't have
618 // // to read and parse it every time?
619 // pkgVendorList List;
620 // List.ReadMainList();
622 // const Vendor* Vndr = NULL;
623 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
625 // string::size_type pos = (*I).find("VALIDSIG ");
626 // if (_config->FindB("Debug::Vendor", false))
627 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
629 // if (pos != std::string::npos)
631 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
632 // if (_config->FindB("Debug::Vendor", false))
633 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
635 // Vndr = List.FindVendor(Fingerprint) != "";
636 // if (Vndr != NULL);
641 string Transformed
= MetaIndexParser
->GetExpectedDist();
643 if (Transformed
== "../project/experimental")
645 Transformed
= "experimental";
648 string::size_type pos
= Transformed
.rfind('/');
649 if (pos
!= string::npos
)
651 Transformed
= Transformed
.substr(0, pos
);
654 if (Transformed
== ".")
659 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
661 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
662 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
663 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
666 if (MetaIndexParser
->CheckDist(Transformed
) == false)
668 // This might become fatal one day
669 // Status = StatAuthError;
670 // ErrorText = "Conflicting distribution; expected "
671 // + MetaIndexParser->GetExpectedDist() + " but got "
672 // + MetaIndexParser->GetDist();
674 if (!Transformed
.empty())
676 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
677 Desc
.Description
.c_str(),
679 MetaIndexParser
->GetDist().c_str());
686 // pkgAcqMetaIndex::Failed - no Release file present or no signature
687 // file present /*{{{*/
688 // ---------------------------------------------------------------------
690 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
692 if (AuthPass
== true)
694 // gpgv method failed
695 _error
->Warning("GPG error: %s: %s",
696 Desc
.Description
.c_str(),
697 LookupTag(Message
,"Message").c_str());
700 // No Release file was present, or verification failed, so fall
701 // back to queueing Packages files without verification
707 // AcqArchive::AcqArchive - Constructor /*{{{*/
708 // ---------------------------------------------------------------------
709 /* This just sets up the initial fetch environment and queues the first
711 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
712 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
713 string
&StoreFilename
) :
714 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
715 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
718 Retries
= _config
->FindI("Acquire::Retries",0);
720 if (Version
.Arch() == 0)
722 _error
->Error(_("I wasn't able to locate a file for the %s package. "
723 "This might mean you need to manually fix this package. "
724 "(due to missing arch)"),
725 Version
.ParentPkg().Name());
729 /* We need to find a filename to determine the extension. We make the
730 assumption here that all the available sources for this version share
731 the same extension.. */
732 // Skip not source sources, they do not have file fields.
733 for (; Vf
.end() == false; Vf
++)
735 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
740 // Does not really matter here.. we are going to fail out below
741 if (Vf
.end() != true)
743 // If this fails to get a file name we will bomb out below.
744 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
745 if (_error
->PendingError() == true)
748 // Generate the final file name as: package_version_arch.foo
749 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
750 QuoteString(Version
.VerStr(),"_:") + '_' +
751 QuoteString(Version
.Arch(),"_:.") +
752 "." + flExtension(Parse
.FileName());
755 // check if we have one trusted source for the package. if so, switch
756 // to "TrustedOnly" mode
757 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
760 if (Sources
->FindIndex(i
.File(),Index
) == false)
762 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
764 std::cerr
<< "Checking index: " << Index
->Describe()
765 << "(Trusted=" << Index
->IsTrusted() << ")\n";
767 if (Index
->IsTrusted()) {
774 if (QueueNext() == false && _error
->PendingError() == false)
775 _error
->Error(_("I wasn't able to locate file for the %s package. "
776 "This might mean you need to manually fix this package."),
777 Version
.ParentPkg().Name());
780 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
781 // ---------------------------------------------------------------------
782 /* This queues the next available file version for download. It checks if
783 the archive is already available in the cache and stashs the MD5 for
785 bool pkgAcqArchive::QueueNext()
787 for (; Vf
.end() == false; Vf
++)
789 // Ignore not source sources
790 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
793 // Try to cross match against the source list
795 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
798 // only try to get a trusted package from another source if that source
800 if(Trusted
&& !Index
->IsTrusted())
803 // Grab the text package record
804 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
805 if (_error
->PendingError() == true)
808 string PkgFile
= Parse
.FileName();
809 MD5
= Parse
.MD5Hash();
810 if (PkgFile
.empty() == true)
811 return _error
->Error(_("The package index files are corrupted. No Filename: "
812 "field for package %s."),
813 Version
.ParentPkg().Name());
815 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
816 Desc
.Description
= Index
->ArchiveInfo(Version
);
818 Desc
.ShortDesc
= Version
.ParentPkg().Name();
820 // See if we already have the file. (Legacy filenames)
821 FileSize
= Version
->Size
;
822 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
824 if (stat(FinalFile
.c_str(),&Buf
) == 0)
826 // Make sure the size matches
827 if ((unsigned)Buf
.st_size
== Version
->Size
)
832 StoreFilename
= DestFile
= FinalFile
;
836 /* Hmm, we have a file and its size does not match, this means it is
837 an old style mismatched arch */
838 unlink(FinalFile
.c_str());
841 // Check it again using the new style output filenames
842 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
843 if (stat(FinalFile
.c_str(),&Buf
) == 0)
845 // Make sure the size matches
846 if ((unsigned)Buf
.st_size
== Version
->Size
)
851 StoreFilename
= DestFile
= FinalFile
;
855 /* Hmm, we have a file and its size does not match, this shouldnt
857 unlink(FinalFile
.c_str());
860 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
862 // Check the destination file
863 if (stat(DestFile
.c_str(),&Buf
) == 0)
865 // Hmm, the partial file is too big, erase it
866 if ((unsigned)Buf
.st_size
> Version
->Size
)
867 unlink(DestFile
.c_str());
869 PartialSize
= Buf
.st_size
;
874 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
875 Desc
.Description
= Index
->ArchiveInfo(Version
);
877 Desc
.ShortDesc
= Version
.ParentPkg().Name();
886 // AcqArchive::Done - Finished fetching /*{{{*/
887 // ---------------------------------------------------------------------
889 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
890 pkgAcquire::MethodConfig
*Cfg
)
892 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
895 if (Size
!= Version
->Size
)
898 ErrorText
= _("Size mismatch");
903 if (Md5Hash
.empty() == false && MD5
.empty() == false)
908 ErrorText
= _("MD5Sum mismatch");
909 if(FileExists(DestFile
))
910 Rename(DestFile
,DestFile
+ ".FAILED");
915 // Grab the output filename
916 string FileName
= LookupTag(Message
,"Filename");
917 if (FileName
.empty() == true)
920 ErrorText
= "Method gave a blank filename";
926 // Reference filename
927 if (FileName
!= DestFile
)
929 StoreFilename
= DestFile
= FileName
;
934 // Done, move it into position
935 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
936 FinalFile
+= flNotDir(StoreFilename
);
937 Rename(DestFile
,FinalFile
);
939 StoreFilename
= DestFile
= FinalFile
;
943 // AcqArchive::Failed - Failure handler /*{{{*/
944 // ---------------------------------------------------------------------
945 /* Here we try other sources */
946 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
948 ErrorText
= LookupTag(Message
,"Message");
950 /* We don't really want to retry on failed media swaps, this prevents
951 that. An interesting observation is that permanent failures are not
953 if (Cnf
->Removable
== true &&
954 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
956 // Vf = Version.FileList();
957 while (Vf
.end() == false) Vf
++;
958 StoreFilename
= string();
959 Item::Failed(Message
,Cnf
);
963 if (QueueNext() == false)
965 // This is the retry counter
967 Cnf
->LocalOnly
== false &&
968 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
971 Vf
= Version
.FileList();
972 if (QueueNext() == true)
976 StoreFilename
= string();
977 Item::Failed(Message
,Cnf
);
981 // AcqArchive::IsTrusted - Determine whether this archive comes from a
982 // trusted source /*{{{*/
983 // ---------------------------------------------------------------------
984 bool pkgAcqArchive::IsTrusted()
989 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
990 // ---------------------------------------------------------------------
992 void pkgAcqArchive::Finished()
994 if (Status
== pkgAcquire::Item::StatDone
&&
997 StoreFilename
= string();
1001 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1002 // ---------------------------------------------------------------------
1003 /* The file is added to the queue */
1004 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1005 unsigned long Size
,string Dsc
,string ShortDesc
) :
1006 Item(Owner
), Md5Hash(MD5
)
1008 Retries
= _config
->FindI("Acquire::Retries",0);
1010 DestFile
= flNotDir(URI
);
1014 Desc
.Description
= Dsc
;
1017 // Set the short description to the archive component
1018 Desc
.ShortDesc
= ShortDesc
;
1020 // Get the transfer sizes
1023 if (stat(DestFile
.c_str(),&Buf
) == 0)
1025 // Hmm, the partial file is too big, erase it
1026 if ((unsigned)Buf
.st_size
> Size
)
1027 unlink(DestFile
.c_str());
1029 PartialSize
= Buf
.st_size
;
1035 // AcqFile::Done - Item downloaded OK /*{{{*/
1036 // ---------------------------------------------------------------------
1038 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1039 pkgAcquire::MethodConfig
*Cnf
)
1042 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1047 ErrorText
= "MD5Sum mismatch";
1048 Rename(DestFile
,DestFile
+ ".FAILED");
1053 Item::Done(Message
,Size
,MD5
,Cnf
);
1055 string FileName
= LookupTag(Message
,"Filename");
1056 if (FileName
.empty() == true)
1059 ErrorText
= "Method gave a blank filename";
1065 // The files timestamp matches
1066 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1069 // We have to copy it into place
1070 if (FileName
!= DestFile
)
1073 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1074 Cnf
->Removable
== true)
1076 Desc
.URI
= "copy:" + FileName
;
1081 // Erase the file if it is a symlink so we can overwrite it
1083 if (lstat(DestFile
.c_str(),&St
) == 0)
1085 if (S_ISLNK(St
.st_mode
) != 0)
1086 unlink(DestFile
.c_str());
1090 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1092 ErrorText
= "Link to " + DestFile
+ " failure ";
1099 // AcqFile::Failed - Failure handler /*{{{*/
1100 // ---------------------------------------------------------------------
1101 /* Here we try other sources */
1102 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1104 ErrorText
= LookupTag(Message
,"Message");
1106 // This is the retry counter
1108 Cnf
->LocalOnly
== false &&
1109 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1116 Item::Failed(Message
,Cnf
);