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 UsedMirror
= LookupTag(Message
,"UsedMirror");
67 if (QueueCounter
<= 1)
69 /* This indicates that the file is not available right now but might
70 be sometime later. If we do a retry cycle then this should be
72 if (Cnf
->LocalOnly
== true &&
73 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
84 // report mirror failure back to LP if we actually use a mirror
85 string FailReason
= LookupTag(Message
, "FailReason");
86 if(FailReason
.size() != 0)
87 ReportMirrorFailure(FailReason
);
89 ReportMirrorFailure(ErrorText
);
92 // Acquire::Item::Start - Item has begun to download /*{{{*/
93 // ---------------------------------------------------------------------
94 /* Stash status and the file size. Note that setting Complete means
95 sub-phases of the acquire process such as decompresion are operating */
96 void pkgAcquire::Item::Start(string
/*Message*/,unsigned long Size
)
98 Status
= StatFetching
;
99 if (FileSize
== 0 && Complete
== false)
103 // Acquire::Item::Done - Item downloaded OK /*{{{*/
104 // ---------------------------------------------------------------------
106 void pkgAcquire::Item::Done(string Message
,unsigned long Size
,string
,
107 pkgAcquire::MethodConfig
*Cnf
)
109 // We just downloaded something..
110 string FileName
= LookupTag(Message
,"Filename");
111 UsedMirror
= LookupTag(Message
,"UsedMirror");
112 if (Complete
== false && FileName
== DestFile
)
115 Owner
->Log
->Fetched(Size
,atoi(LookupTag(Message
,"Resume-Point","0").c_str()));
121 ErrorText
= string();
122 Owner
->Dequeue(this);
125 // Acquire::Item::Rename - Rename a file /*{{{*/
126 // ---------------------------------------------------------------------
127 /* This helper function is used by alot of item methods as thier final
129 void pkgAcquire::Item::Rename(string From
,string To
)
131 if (rename(From
.c_str(),To
.c_str()) != 0)
134 snprintf(S
,sizeof(S
),_("rename failed, %s (%s -> %s)."),strerror(errno
),
135 From
.c_str(),To
.c_str());
142 void pkgAcquire::Item::ReportMirrorFailure(string FailCode
)
144 // we only act if a mirror was used at all
145 if(UsedMirror
.empty())
148 std::cerr
<< "\nReportMirrorFailure: "
150 << " Uri: " << DescURI()
152 << FailCode
<< std::endl
;
154 const char *Args
[40];
156 string report
= _config
->Find("Methods::Mirror::ProblemReporting",
157 "/usr/lib/apt/apt-report-mirror-failure");
158 if(!FileExists(report
))
160 Args
[i
++] = report
.c_str();
161 Args
[i
++] = UsedMirror
.c_str();
162 Args
[i
++] = DescURI().c_str();
163 Args
[i
++] = FailCode
.c_str();
165 pid_t pid
= ExecFork();
168 _error
->Error("ReportMirrorFailure Fork failed");
173 execvp(Args
[0], (char**)Args
);
174 std::cerr
<< "Could not exec " << Args
[0] << std::endl
;
177 if(!ExecWait(pid
, "report-mirror-failure"))
179 _error
->Warning("Couldn't report problem to '%s'",
180 _config
->Find("Methods::Mirror::ProblemReporting").c_str());
185 // AcqIndex::AcqIndex - Constructor /*{{{*/
186 // ---------------------------------------------------------------------
187 /* The package file is added to the queue and a second class is
188 instantiated to fetch the revision file */
189 pkgAcqIndex::pkgAcqIndex(pkgAcquire
*Owner
,
190 string URI
,string URIDesc
,string ShortDesc
,
191 string ExpectedMD5
, string comprExt
) :
192 Item(Owner
), RealURI(URI
), ExpectedMD5(ExpectedMD5
)
194 Decompression
= false;
197 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
198 DestFile
+= URItoFileName(URI
);
202 // autoselect the compression method
203 if(FileExists("/bin/bzip2"))
204 CompressionExtension
= ".bz2";
206 CompressionExtension
= ".gz";
208 CompressionExtension
= comprExt
;
210 Desc
.URI
= URI
+ CompressionExtension
;
212 Desc
.Description
= URIDesc
;
214 Desc
.ShortDesc
= ShortDesc
;
219 // AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/
220 // ---------------------------------------------------------------------
221 /* The only header we use is the last-modified header. */
222 string
pkgAcqIndex::Custom600Headers()
224 string Final
= _config
->FindDir("Dir::State::lists");
225 Final
+= URItoFileName(RealURI
);
228 if (stat(Final
.c_str(),&Buf
) != 0)
229 return "\nIndex-File: true";
230 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
234 void pkgAcqIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
236 // no .bz2 found, retry with .gz
237 if(Desc
.URI
.substr(Desc
.URI
.size()-3) == "bz2") {
238 Desc
.URI
= Desc
.URI
.substr(0,Desc
.URI
.size()-3) + "gz";
240 // retry with a gzip one
241 new pkgAcqIndex(Owner
, RealURI
, Desc
.Description
,Desc
.ShortDesc
,
242 ExpectedMD5
, string(".gz"));
250 Item::Failed(Message
,Cnf
);
254 // AcqIndex::Done - Finished a fetch /*{{{*/
255 // ---------------------------------------------------------------------
256 /* This goes through a number of states.. On the initial fetch the
257 method could possibly return an alternate filename which points
258 to the uncompressed version of the file. If this is so the file
259 is copied into the partial directory. In all other cases the file
260 is decompressed with a gzip uri. */
261 void pkgAcqIndex::Done(string Message
,unsigned long Size
,string MD5
,
262 pkgAcquire::MethodConfig
*Cfg
)
264 Item::Done(Message
,Size
,MD5
,Cfg
);
266 if (Decompression
== true)
268 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
270 std::cerr
<< std::endl
<< RealURI
<< ": Computed MD5: " << MD5
;
271 std::cerr
<< " Expected MD5: " << ExpectedMD5
<< std::endl
;
277 FileFd
Fd(DestFile
, FileFd::ReadOnly
);
278 sum
.AddFD(Fd
.Fd(), Fd
.Size());
280 MD5
= (string
)sum
.Result();
283 if (!ExpectedMD5
.empty() && MD5
!= ExpectedMD5
)
285 Status
= StatAuthError
;
286 ErrorText
= _("MD5Sum mismatch");
287 Rename(DestFile
,DestFile
+ ".FAILED");
288 ReportMirrorFailure("HashChecksumFailure");
291 // Done, move it into position
292 string FinalFile
= _config
->FindDir("Dir::State::lists");
293 FinalFile
+= URItoFileName(RealURI
);
294 Rename(DestFile
,FinalFile
);
295 chmod(FinalFile
.c_str(),0644);
297 /* We restore the original name to DestFile so that the clean operation
299 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
300 DestFile
+= URItoFileName(RealURI
);
302 // Remove the compressed version.
304 unlink(DestFile
.c_str());
311 // Handle the unzipd case
312 string FileName
= LookupTag(Message
,"Alt-Filename");
313 if (FileName
.empty() == false)
315 // The files timestamp matches
316 if (StringToBool(LookupTag(Message
,"Alt-IMS-Hit"),false) == true)
319 Decompression
= true;
321 DestFile
+= ".decomp";
322 Desc
.URI
= "copy:" + FileName
;
328 FileName
= LookupTag(Message
,"Filename");
329 if (FileName
.empty() == true)
332 ErrorText
= "Method gave a blank filename";
335 // The files timestamp matches
336 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
339 if (FileName
== DestFile
)
344 string compExt
= Desc
.URI
.substr(Desc
.URI
.size()-3);
347 decompProg
= "bzip2";
348 else if(compExt
== ".gz")
351 _error
->Error("Unsupported extension: %s", compExt
.c_str());
355 Decompression
= true;
356 DestFile
+= ".decomp";
357 Desc
.URI
= string(decompProg
) + ":" + FileName
;
362 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire
*Owner
,
363 string URI
,string URIDesc
,string ShortDesc
,
364 string MetaIndexURI
, string MetaIndexURIDesc
,
365 string MetaIndexShortDesc
,
366 const vector
<IndexTarget
*>* IndexTargets
,
367 indexRecords
* MetaIndexParser
) :
368 Item(Owner
), RealURI(URI
), MetaIndexURI(MetaIndexURI
),
369 MetaIndexURIDesc(MetaIndexURIDesc
), MetaIndexShortDesc(MetaIndexShortDesc
),
370 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
)
372 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
373 DestFile
+= URItoFileName(URI
);
375 // remove any partial downloaded sig-file. it may confuse proxies
376 // and is too small to warrant a partial download anyway
377 unlink(DestFile
.c_str());
380 Desc
.Description
= URIDesc
;
382 Desc
.ShortDesc
= ShortDesc
;
386 string Final
= _config
->FindDir("Dir::State::lists");
387 Final
+= URItoFileName(RealURI
);
389 if (stat(Final
.c_str(),&Buf
) == 0)
391 // File was already in place. It needs to be re-verified
392 // because Release might have changed, so Move it into partial
393 Rename(Final
,DestFile
);
399 // pkgAcqMetaSig::Custom600Headers - Insert custom request headers /*{{{*/
400 // ---------------------------------------------------------------------
401 /* The only header we use is the last-modified header. */
402 string
pkgAcqMetaSig::Custom600Headers()
405 if (stat(DestFile
.c_str(),&Buf
) != 0)
406 return "\nIndex-File: true";
408 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
411 void pkgAcqMetaSig::Done(string Message
,unsigned long Size
,string MD5
,
412 pkgAcquire::MethodConfig
*Cfg
)
414 Item::Done(Message
,Size
,MD5
,Cfg
);
416 string FileName
= LookupTag(Message
,"Filename");
417 if (FileName
.empty() == true)
420 ErrorText
= "Method gave a blank filename";
424 if (FileName
!= DestFile
)
426 // We have to copy it into place
428 Desc
.URI
= "copy:" + FileName
;
435 // queue a pkgAcqMetaIndex to be verified against the sig we just retrieved
436 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
437 DestFile
, IndexTargets
, MetaIndexParser
);
441 void pkgAcqMetaSig::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
444 // if we get a network error we fail gracefully
445 if(LookupTag(Message
,"FailReason") == "Timeout" ||
446 LookupTag(Message
,"FailReason") == "TmpResolveFailure" ||
447 LookupTag(Message
,"FailReason") == "ConnectionRefused") {
448 Item::Failed(Message
,Cnf
);
452 // Delete any existing sigfile when the acquire failed
453 string Final
= _config
->FindDir("Dir::State::lists") + URItoFileName(RealURI
);
454 unlink(Final
.c_str());
456 // queue a pkgAcqMetaIndex with no sigfile
457 new pkgAcqMetaIndex(Owner
, MetaIndexURI
, MetaIndexURIDesc
, MetaIndexShortDesc
,
458 "", IndexTargets
, MetaIndexParser
);
460 if (Cnf
->LocalOnly
== true ||
461 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == false)
470 Item::Failed(Message
,Cnf
);
473 pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire
*Owner
,
474 string URI
,string URIDesc
,string ShortDesc
,
476 const vector
<struct IndexTarget
*>* IndexTargets
,
477 indexRecords
* MetaIndexParser
) :
478 Item(Owner
), RealURI(URI
), SigFile(SigFile
), AuthPass(false),
479 MetaIndexParser(MetaIndexParser
), IndexTargets(IndexTargets
), IMSHit(false)
481 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
482 DestFile
+= URItoFileName(URI
);
485 Desc
.Description
= URIDesc
;
487 Desc
.ShortDesc
= ShortDesc
;
494 // pkgAcqMetaIndex::Custom600Headers - Insert custom request headers /*{{{*/
495 // ---------------------------------------------------------------------
496 /* The only header we use is the last-modified header. */
497 string
pkgAcqMetaIndex::Custom600Headers()
499 string Final
= _config
->FindDir("Dir::State::lists");
500 Final
+= URItoFileName(RealURI
);
503 if (stat(Final
.c_str(),&Buf
) != 0)
504 return "\nIndex-File: true";
506 return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf
.st_mtime
);
509 void pkgAcqMetaIndex::Done(string Message
,unsigned long Size
,string MD5
,
510 pkgAcquire::MethodConfig
*Cfg
)
512 Item::Done(Message
,Size
,MD5
,Cfg
);
514 // MetaIndexes are done in two passes: one to download the
515 // metaindex with an appropriate method, and a second to verify it
516 // with the gpgv method
518 if (AuthPass
== true)
524 RetrievalDone(Message
);
526 // Still more retrieving to do
531 // There was no signature file, so we are finished. Download
532 // the indexes without verification.
537 // There was a signature file, so pass it to gpgv for
540 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
541 std::cerr
<< "Metaindex acquired, queueing gpg verification ("
542 << SigFile
<< "," << DestFile
<< ")\n";
544 Desc
.URI
= "gpgv:" + SigFile
;
551 void pkgAcqMetaIndex::RetrievalDone(string Message
)
553 // We have just finished downloading a Release file (it is not
556 string FileName
= LookupTag(Message
,"Filename");
557 if (FileName
.empty() == true)
560 ErrorText
= "Method gave a blank filename";
564 if (FileName
!= DestFile
)
567 Desc
.URI
= "copy:" + FileName
;
572 // see if the download was a IMSHit
573 IMSHit
= StringToBool(LookupTag(Message
,"IMS-Hit"),false);
577 string FinalFile
= _config
->FindDir("Dir::State::lists");
578 FinalFile
+= URItoFileName(RealURI
);
580 // The files timestamp matches
581 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == false)
583 // Move it into position
584 Rename(DestFile
,FinalFile
);
586 DestFile
= FinalFile
;
589 void pkgAcqMetaIndex::AuthDone(string Message
)
591 // At this point, the gpgv method has succeeded, so there is a
592 // valid signature from a key in the trusted keyring. We
593 // perform additional verification of its contents, and use them
594 // to verify the indexes we are about to download
596 if (!MetaIndexParser
->Load(DestFile
))
598 Status
= StatAuthError
;
599 ErrorText
= MetaIndexParser
->ErrorText
;
603 if (!VerifyVendor(Message
))
608 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
609 std::cerr
<< "Signature verification succeeded: "
610 << DestFile
<< std::endl
;
612 // Download further indexes with verification
615 // Done, move signature file into position
617 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
618 URItoFileName(RealURI
) + ".gpg";
619 Rename(SigFile
,VerifiedSigFile
);
620 chmod(VerifiedSigFile
.c_str(),0644);
623 void pkgAcqMetaIndex::QueueIndexes(bool verify
)
625 for (vector
<struct IndexTarget
*>::const_iterator Target
= IndexTargets
->begin();
626 Target
!= IndexTargets
->end();
629 string ExpectedIndexMD5
;
632 const indexRecords::checkSum
*Record
= MetaIndexParser
->Lookup((*Target
)->MetaKey
);
635 Status
= StatAuthError
;
636 ErrorText
= "Unable to find expected entry "
637 + (*Target
)->MetaKey
+ " in Meta-index file (malformed Release file?)";
640 ExpectedIndexMD5
= Record
->MD5Hash
;
641 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
643 std::cerr
<< "Queueing: " << (*Target
)->URI
<< std::endl
;
644 std::cerr
<< "Expected MD5: " << ExpectedIndexMD5
<< std::endl
;
646 if (ExpectedIndexMD5
.empty())
648 Status
= StatAuthError
;
649 ErrorText
= "Unable to find MD5 sum for "
650 + (*Target
)->MetaKey
+ " in Meta-index file";
655 // Queue Packages file
656 new pkgAcqIndex(Owner
, (*Target
)->URI
, (*Target
)->Description
,
657 (*Target
)->ShortDesc
, ExpectedIndexMD5
);
661 bool pkgAcqMetaIndex::VerifyVendor(string Message
)
663 // // Maybe this should be made available from above so we don't have
664 // // to read and parse it every time?
665 // pkgVendorList List;
666 // List.ReadMainList();
668 // const Vendor* Vndr = NULL;
669 // for (std::vector<string>::const_iterator I = GPGVOutput.begin(); I != GPGVOutput.end(); I++)
671 // string::size_type pos = (*I).find("VALIDSIG ");
672 // if (_config->FindB("Debug::Vendor", false))
673 // std::cerr << "Looking for VALIDSIG in \"" << (*I) << "\": pos " << pos
675 // if (pos != std::string::npos)
677 // string Fingerprint = (*I).substr(pos+sizeof("VALIDSIG"));
678 // if (_config->FindB("Debug::Vendor", false))
679 // std::cerr << "Looking for \"" << Fingerprint << "\" in vendor..." <<
681 // Vndr = List.FindVendor(Fingerprint) != "";
682 // if (Vndr != NULL);
686 string::size_type pos
;
688 // check for missing sigs (that where not fatal because otherwise we had
691 string msg
= _("There is no public key available for the "
692 "following key IDs:\n");
693 pos
= Message
.find("NO_PUBKEY ");
694 if (pos
!= std::string::npos
)
696 string::size_type start
= pos
+strlen("NO_PUBKEY ");
697 string Fingerprint
= Message
.substr(start
, Message
.find("\n")-start
);
698 missingkeys
+= (Fingerprint
);
700 if(!missingkeys
.empty())
701 _error
->Warning("%s", string(msg
+missingkeys
).c_str());
703 string Transformed
= MetaIndexParser
->GetExpectedDist();
705 if (Transformed
== "../project/experimental")
707 Transformed
= "experimental";
710 pos
= Transformed
.rfind('/');
711 if (pos
!= string::npos
)
713 Transformed
= Transformed
.substr(0, pos
);
716 if (Transformed
== ".")
721 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
723 std::cerr
<< "Got Codename: " << MetaIndexParser
->GetDist() << std::endl
;
724 std::cerr
<< "Expecting Dist: " << MetaIndexParser
->GetExpectedDist() << std::endl
;
725 std::cerr
<< "Transformed Dist: " << Transformed
<< std::endl
;
728 if (MetaIndexParser
->CheckDist(Transformed
) == false)
730 // This might become fatal one day
731 // Status = StatAuthError;
732 // ErrorText = "Conflicting distribution; expected "
733 // + MetaIndexParser->GetExpectedDist() + " but got "
734 // + MetaIndexParser->GetDist();
736 if (!Transformed
.empty())
738 _error
->Warning("Conflicting distribution: %s (expected %s but got %s)",
739 Desc
.Description
.c_str(),
741 MetaIndexParser
->GetDist().c_str());
748 // pkgAcqMetaIndex::Failed - no Release file present or no signature
749 // file present /*{{{*/
750 // ---------------------------------------------------------------------
752 void pkgAcqMetaIndex::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
754 if (AuthPass
== true)
756 // if we fail the authentication but got the file via a IMS-Hit
757 // this means that the file wasn't downloaded and that it might be
758 // just stale (server problem, proxy etc). we delete what we have
759 // queue it again without i-m-s
760 // alternatively we could just unlink the file and let the user try again
766 unlink(DestFile
.c_str());
768 DestFile
= _config
->FindDir("Dir::State::lists") + "partial/";
769 DestFile
+= URItoFileName(RealURI
);
775 // gpgv method failed
776 ReportMirrorFailure("GPGFailure");
777 _error
->Warning("GPG error: %s: %s",
778 Desc
.Description
.c_str(),
779 LookupTag(Message
,"Message").c_str());
783 // No Release file was present, or verification failed, so fall
784 // back to queueing Packages files without verification
790 // AcqArchive::AcqArchive - Constructor /*{{{*/
791 // ---------------------------------------------------------------------
792 /* This just sets up the initial fetch environment and queues the first
794 pkgAcqArchive::pkgAcqArchive(pkgAcquire
*Owner
,pkgSourceList
*Sources
,
795 pkgRecords
*Recs
,pkgCache::VerIterator
const &Version
,
796 string
&StoreFilename
) :
797 Item(Owner
), Version(Version
), Sources(Sources
), Recs(Recs
),
798 StoreFilename(StoreFilename
), Vf(Version
.FileList()),
801 Retries
= _config
->FindI("Acquire::Retries",0);
803 if (Version
.Arch() == 0)
805 _error
->Error(_("I wasn't able to locate a file for the %s package. "
806 "This might mean you need to manually fix this package. "
807 "(due to missing arch)"),
808 Version
.ParentPkg().Name());
812 /* We need to find a filename to determine the extension. We make the
813 assumption here that all the available sources for this version share
814 the same extension.. */
815 // Skip not source sources, they do not have file fields.
816 for (; Vf
.end() == false; Vf
++)
818 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
823 // Does not really matter here.. we are going to fail out below
824 if (Vf
.end() != true)
826 // If this fails to get a file name we will bomb out below.
827 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
828 if (_error
->PendingError() == true)
831 // Generate the final file name as: package_version_arch.foo
832 StoreFilename
= QuoteString(Version
.ParentPkg().Name(),"_:") + '_' +
833 QuoteString(Version
.VerStr(),"_:") + '_' +
834 QuoteString(Version
.Arch(),"_:.") +
835 "." + flExtension(Parse
.FileName());
838 // check if we have one trusted source for the package. if so, switch
839 // to "TrustedOnly" mode
840 for (pkgCache::VerFileIterator i
= Version
.FileList(); i
.end() == false; i
++)
843 if (Sources
->FindIndex(i
.File(),Index
) == false)
845 if (_config
->FindB("Debug::pkgAcquire::Auth", false))
847 std::cerr
<< "Checking index: " << Index
->Describe()
848 << "(Trusted=" << Index
->IsTrusted() << ")\n";
850 if (Index
->IsTrusted()) {
856 // "allow-unauthenticated" restores apts old fetching behaviour
857 // that means that e.g. unauthenticated file:// uris are higher
858 // priority than authenticated http:// uris
859 if (_config
->FindB("APT::Get::AllowUnauthenticated",false) == true)
863 if (QueueNext() == false && _error
->PendingError() == false)
864 _error
->Error(_("I wasn't able to locate file for the %s package. "
865 "This might mean you need to manually fix this package."),
866 Version
.ParentPkg().Name());
869 // AcqArchive::QueueNext - Queue the next file source /*{{{*/
870 // ---------------------------------------------------------------------
871 /* This queues the next available file version for download. It checks if
872 the archive is already available in the cache and stashs the MD5 for
874 bool pkgAcqArchive::QueueNext()
876 for (; Vf
.end() == false; Vf
++)
878 // Ignore not source sources
879 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) != 0)
882 // Try to cross match against the source list
884 if (Sources
->FindIndex(Vf
.File(),Index
) == false)
887 // only try to get a trusted package from another source if that source
889 if(Trusted
&& !Index
->IsTrusted())
892 // Grab the text package record
893 pkgRecords::Parser
&Parse
= Recs
->Lookup(Vf
);
894 if (_error
->PendingError() == true)
897 string PkgFile
= Parse
.FileName();
898 MD5
= Parse
.MD5Hash();
899 if (PkgFile
.empty() == true)
900 return _error
->Error(_("The package index files are corrupted. No Filename: "
901 "field for package %s."),
902 Version
.ParentPkg().Name());
904 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
905 Desc
.Description
= Index
->ArchiveInfo(Version
);
907 Desc
.ShortDesc
= Version
.ParentPkg().Name();
909 // See if we already have the file. (Legacy filenames)
910 FileSize
= Version
->Size
;
911 string FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(PkgFile
);
913 if (stat(FinalFile
.c_str(),&Buf
) == 0)
915 // Make sure the size matches
916 if ((unsigned)Buf
.st_size
== Version
->Size
)
921 StoreFilename
= DestFile
= FinalFile
;
925 /* Hmm, we have a file and its size does not match, this means it is
926 an old style mismatched arch */
927 unlink(FinalFile
.c_str());
930 // Check it again using the new style output filenames
931 FinalFile
= _config
->FindDir("Dir::Cache::Archives") + flNotDir(StoreFilename
);
932 if (stat(FinalFile
.c_str(),&Buf
) == 0)
934 // Make sure the size matches
935 if ((unsigned)Buf
.st_size
== Version
->Size
)
940 StoreFilename
= DestFile
= FinalFile
;
944 /* Hmm, we have a file and its size does not match, this shouldnt
946 unlink(FinalFile
.c_str());
949 DestFile
= _config
->FindDir("Dir::Cache::Archives") + "partial/" + flNotDir(StoreFilename
);
951 // Check the destination file
952 if (stat(DestFile
.c_str(),&Buf
) == 0)
954 // Hmm, the partial file is too big, erase it
955 if ((unsigned)Buf
.st_size
> Version
->Size
)
956 unlink(DestFile
.c_str());
958 PartialSize
= Buf
.st_size
;
963 Desc
.URI
= Index
->ArchiveURI(PkgFile
);
964 Desc
.Description
= Index
->ArchiveInfo(Version
);
966 Desc
.ShortDesc
= Version
.ParentPkg().Name();
975 // AcqArchive::Done - Finished fetching /*{{{*/
976 // ---------------------------------------------------------------------
978 void pkgAcqArchive::Done(string Message
,unsigned long Size
,string Md5Hash
,
979 pkgAcquire::MethodConfig
*Cfg
)
981 Item::Done(Message
,Size
,Md5Hash
,Cfg
);
984 if (Size
!= Version
->Size
)
987 ErrorText
= _("Size mismatch");
992 if (Md5Hash
.empty() == false && MD5
.empty() == false)
997 ErrorText
= _("MD5Sum mismatch");
998 if(FileExists(DestFile
))
999 Rename(DestFile
,DestFile
+ ".FAILED");
1004 // Grab the output filename
1005 string FileName
= LookupTag(Message
,"Filename");
1006 if (FileName
.empty() == true)
1009 ErrorText
= "Method gave a blank filename";
1015 // Reference filename
1016 if (FileName
!= DestFile
)
1018 StoreFilename
= DestFile
= FileName
;
1023 // Done, move it into position
1024 string FinalFile
= _config
->FindDir("Dir::Cache::Archives");
1025 FinalFile
+= flNotDir(StoreFilename
);
1026 Rename(DestFile
,FinalFile
);
1028 StoreFilename
= DestFile
= FinalFile
;
1032 // AcqArchive::Failed - Failure handler /*{{{*/
1033 // ---------------------------------------------------------------------
1034 /* Here we try other sources */
1035 void pkgAcqArchive::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1037 ErrorText
= LookupTag(Message
,"Message");
1039 /* We don't really want to retry on failed media swaps, this prevents
1040 that. An interesting observation is that permanent failures are not
1042 if (Cnf
->Removable
== true &&
1043 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1045 // Vf = Version.FileList();
1046 while (Vf
.end() == false) Vf
++;
1047 StoreFilename
= string();
1048 Item::Failed(Message
,Cnf
);
1052 if (QueueNext() == false)
1054 // This is the retry counter
1056 Cnf
->LocalOnly
== false &&
1057 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1060 Vf
= Version
.FileList();
1061 if (QueueNext() == true)
1065 StoreFilename
= string();
1066 Item::Failed(Message
,Cnf
);
1070 // AcqArchive::IsTrusted - Determine whether this archive comes from a
1071 // trusted source /*{{{*/
1072 // ---------------------------------------------------------------------
1073 bool pkgAcqArchive::IsTrusted()
1078 // AcqArchive::Finished - Fetching has finished, tidy up /*{{{*/
1079 // ---------------------------------------------------------------------
1081 void pkgAcqArchive::Finished()
1083 if (Status
== pkgAcquire::Item::StatDone
&&
1086 StoreFilename
= string();
1090 // AcqFile::pkgAcqFile - Constructor /*{{{*/
1091 // ---------------------------------------------------------------------
1092 /* The file is added to the queue */
1093 pkgAcqFile::pkgAcqFile(pkgAcquire
*Owner
,string URI
,string MD5
,
1094 unsigned long Size
,string Dsc
,string ShortDesc
,
1095 const string
&DestDir
, const string
&DestFilename
) :
1096 Item(Owner
), Md5Hash(MD5
)
1098 Retries
= _config
->FindI("Acquire::Retries",0);
1100 if(!DestFilename
.empty())
1101 DestFile
= DestFilename
;
1102 else if(!DestDir
.empty())
1103 DestFile
= DestDir
+ "/" + flNotDir(URI
);
1105 DestFile
= flNotDir(URI
);
1109 Desc
.Description
= Dsc
;
1112 // Set the short description to the archive component
1113 Desc
.ShortDesc
= ShortDesc
;
1115 // Get the transfer sizes
1118 if (stat(DestFile
.c_str(),&Buf
) == 0)
1120 // Hmm, the partial file is too big, erase it
1121 if ((unsigned)Buf
.st_size
> Size
)
1122 unlink(DestFile
.c_str());
1124 PartialSize
= Buf
.st_size
;
1130 // AcqFile::Done - Item downloaded OK /*{{{*/
1131 // ---------------------------------------------------------------------
1133 void pkgAcqFile::Done(string Message
,unsigned long Size
,string MD5
,
1134 pkgAcquire::MethodConfig
*Cnf
)
1137 if (Md5Hash
.empty() == false && MD5
.empty() == false)
1142 ErrorText
= "MD5Sum mismatch";
1143 Rename(DestFile
,DestFile
+ ".FAILED");
1148 Item::Done(Message
,Size
,MD5
,Cnf
);
1150 string FileName
= LookupTag(Message
,"Filename");
1151 if (FileName
.empty() == true)
1154 ErrorText
= "Method gave a blank filename";
1160 // The files timestamp matches
1161 if (StringToBool(LookupTag(Message
,"IMS-Hit"),false) == true)
1164 // We have to copy it into place
1165 if (FileName
!= DestFile
)
1168 if (_config
->FindB("Acquire::Source-Symlinks",true) == false ||
1169 Cnf
->Removable
== true)
1171 Desc
.URI
= "copy:" + FileName
;
1176 // Erase the file if it is a symlink so we can overwrite it
1178 if (lstat(DestFile
.c_str(),&St
) == 0)
1180 if (S_ISLNK(St
.st_mode
) != 0)
1181 unlink(DestFile
.c_str());
1185 if (symlink(FileName
.c_str(),DestFile
.c_str()) != 0)
1187 ErrorText
= "Link to " + DestFile
+ " failure ";
1194 // AcqFile::Failed - Failure handler /*{{{*/
1195 // ---------------------------------------------------------------------
1196 /* Here we try other sources */
1197 void pkgAcqFile::Failed(string Message
,pkgAcquire::MethodConfig
*Cnf
)
1199 ErrorText
= LookupTag(Message
,"Message");
1201 // This is the retry counter
1203 Cnf
->LocalOnly
== false &&
1204 StringToBool(LookupTag(Message
,"Transient-Failure"),false) == true)
1211 Item::Failed(Message
,Cnf
);