X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/77a7df0e00b10252abb2fe52a083d53d5878ab11..6627bdccce46a9acdae09d37e6e1444a5f7384e1:/apt-pkg/acquire-item.cc diff --git a/apt-pkg/acquire-item.cc b/apt-pkg/acquire-item.cc index 79e0b1898..da9becc44 100644 --- a/apt-pkg/acquire-item.cc +++ b/apt-pkg/acquire-item.cc @@ -24,8 +24,6 @@ #include #include #include -#include -#include #include @@ -33,7 +31,6 @@ #include #include #include -#include #include /*}}}*/ @@ -78,7 +75,7 @@ void pkgAcquire::Item::Failed(string Message,pkgAcquire::MethodConfig *Cnf) Dequeue(); return; } - + Status = StatError; Dequeue(); } @@ -134,328 +131,32 @@ void pkgAcquire::Item::Rename(string From,string To) } /*}}}*/ -// AcqIndexDiffs::AcqIndexDiffs - Constructor -// --------------------------------------------------------------------- -/* The package diff is added to the queue. one object is constructed - * for each diff and the index - */ -pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire *Owner, - string URI,string URIDesc,string ShortDesc, - string ExpectedMD5, vector diffs) - : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5), available_patches(diffs) -{ - - DestFile = _config->FindDir("Dir::State::lists") + "partial/"; - DestFile += URItoFileName(URI); - - Debug = _config->FindB("Debug::pkgAcquire::Diffs",false); - - Desc.Description = URIDesc; - Desc.Owner = this; - Desc.ShortDesc = ShortDesc; - - CurrentPackagesFile = _config->FindDir("Dir::State::lists"); - CurrentPackagesFile += URItoFileName(RealURI); - - if(Debug) { - std::clog << "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): " - << CurrentPackagesFile << std::endl; - } - - if(!FileExists(CurrentPackagesFile) || - !_config->FindB("Acquire::Diffs",true)) { - // we don't have a pkg file or we don't want to queue - if(Debug) - std::clog << "No index file or canceld by user" << std::endl; - Failed("", NULL); - return; - } - - if(available_patches.size() == 0) - QueueDiffIndex(URI); - else - QueueNextDiff(); -} - -void pkgAcqIndexDiffs::QueueDiffIndex(string URI) -{ - Desc.URI = URI + ".diff/Index"; - Desc.Description = Description + "IndexDiff"; - DestFile = _config->FindDir("Dir::State::lists") + "partial/"; - DestFile += URItoFileName(URI) + string(".IndexDiff"); - - if(Debug) - std::clog << "QueueDiffIndex: " << Desc.URI << std::endl; - - QueueURI(Desc); -} - -// AcqIndex::Custom600Headers - Insert custom request headers /*{{{*/ -// --------------------------------------------------------------------- -/* The only header we use is the last-modified header. */ -string pkgAcqIndexDiffs::Custom600Headers() -{ - if(DestFile.rfind(".IndexDiff") == string::npos) - return string(""); - - string Final = _config->FindDir("Dir::State::lists"); - Final += URItoFileName(RealURI) + string(".IndexDiff"); - - if(Debug) - std::clog << "Custom600Header-IMS: " << Final << std::endl; - - struct stat Buf; - if (stat(Final.c_str(),&Buf) != 0) - return "\nIndex-File: true"; - - return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime); -} - -void pkgAcqIndexDiffs::Failed(string Message,pkgAcquire::MethodConfig *Cnf) -{ - if(Debug) - std::clog << "Failed(): " << Desc.URI << std::endl - << "Falling back to big package file" << std::endl; - new pkgAcqIndex(Owner, RealURI, Desc.Description,Desc.ShortDesc, - ExpectedMD5); - Finish(); -} - - -// helper that cleans the item out of the fetcher queue -void pkgAcqIndexDiffs::Finish(bool allDone) -{ - // we restore the original name, this is required, otherwise - // the file will be cleaned - if(allDone) { - // this is for the "real" finish - DestFile = _config->FindDir("Dir::State::lists"); - DestFile += URItoFileName(RealURI); - Complete = true; - Dequeue(); - if(Debug) - std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl; - return; - - } - - if(Debug) - std::clog << "Finishing: " << Desc.URI << std::endl; - Complete = false; - Status = StatDone; - Dequeue(); - return; -} - - -// this needs to be rewriten to not depend on the external ed -bool pkgAcqIndexDiffs::ApplyDiff(string PatchFile) -{ - char *error; - int res=0; - - string FinalFile = _config->FindDir("Dir::State::lists"); - FinalFile += URItoFileName(RealURI); - - int Process = ExecFork(); - if (Process == 0) - { - chdir(_config->FindDir("Dir::State::lists").c_str()); - // for some reason "red" fails with the pdiffs from p.d.o/~aba ?!? - string cmd = "(zcat " + PatchFile + "; echo \"wq\" ) | /bin/ed " + FinalFile + " >/dev/null 2>/dev/null"; - if(Debug) - std::clog << "Runing: " << cmd << std::endl; - res = system(cmd.c_str()); - _exit(WEXITSTATUS(res)); - } - if(!ExecWait(Process, error, true)) { - //_error->Error("Patch failed: %s ", error); - return false; - } - - return true; -} - -bool pkgAcqIndexDiffs::QueueNextDiff() -{ - // calc sha1 of the just patched file - string FinalFile = _config->FindDir("Dir::State::lists"); - FinalFile += URItoFileName(RealURI); - - FileFd fd(FinalFile, FileFd::ReadOnly); - SHA1Summation SHA1; - SHA1.AddFD(fd.Fd(), fd.Size()); - string local_sha1 = string(SHA1.Result()); - - // remove all patches until the next matching patch is found - // this requires the Index file to be ordered - for(vector::iterator I=available_patches.begin(); - I != available_patches.end() && (*I).sha1 != local_sha1; - I++) - available_patches.erase(I); - - // error checking and falling back if no patch was found - if(available_patches.size() == 0) { - Failed("", NULL); - return false; - } - - // queue the right diff - Desc.URI = string(RealURI) + string(".diff/") + available_patches[0].file + string(".gz"); - Desc.Description = available_patches[0].file + string(".pdiff"); - - DestFile = _config->FindDir("Dir::State::lists") + "partial/"; - DestFile += URItoFileName(RealURI + string(".diff/") + available_patches[0].file); - - if(Debug) - std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl; - - QueueURI(Desc); - - return true; -} - -bool pkgAcqIndexDiffs::ParseIndexDiff(string IndexDiffFile) -{ - if(Debug) - std::clog << "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile - << std::endl; - - FileFd Fd(IndexDiffFile,FileFd::ReadOnly); - pkgTagFile TF(&Fd); - if (_error->PendingError() == true) - return false; - pkgTagSection Tags; - if(TF.Step(Tags) == true) - { - string local_sha1; - string tmp = Tags.FindS("SHA1-Current"); - std::stringstream ss(tmp); - ss >> ServerSha1; - - FileFd fd(CurrentPackagesFile, FileFd::ReadOnly); - SHA1Summation SHA1; - SHA1.AddFD(fd.Fd(), fd.Size()); - local_sha1 = string(SHA1.Result()); - - if(local_sha1 == ServerSha1) { - if(Debug) - std::clog << "Package file is up-to-date" << std::endl; - Finish(true); - return true; - } - if(Debug) - std::clog << "SHA1-Current: " << ServerSha1 << std::endl; - - // check the historie and see what patches we need - string history = Tags.FindS("SHA1-History"); - std::stringstream hist(history); - DiffInfo d; - string size; - bool found = false; - while(hist >> d.sha1 >> size >> d.file) { - d.size = atoi(size.c_str()); - // read until the first match is found - if(d.sha1 == local_sha1) - found=true; - // from that point on, we probably need all diffs - if(found) { - if(Debug) - std::clog << "Need to get diff: " << d.file << std::endl; - available_patches.push_back(d); - } - } - - // no information how to get the patches, bail out - if(!found) { - if(Debug) - std::clog << "Can't find a patch in the index file" << std::endl; - // Failed will queue a big package file - Failed("", NULL); - } else { - // queue the diffs - new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc, - ExpectedMD5, available_patches); - Finish(); - return true; - } - } - - return false; -} - - -void pkgAcqIndexDiffs::Done(string Message,unsigned long Size,string Md5Hash, - pkgAcquire::MethodConfig *Cnf) -{ - if(Debug) - std::clog << "pkgAcqIndexDiffs::Done(): " << Desc.URI << std::endl; - - Item::Done(Message,Size,Md5Hash,Cnf); - - int len = Desc.URI.size(); - // sucess in downloading the index - if(Desc.URI.substr(len-strlen("Index"),len-1) == "Index") { - - // rename - string FinalFile = _config->FindDir("Dir::State::lists"); - FinalFile += URItoFileName(RealURI) + string(".IndexDiff"); - if(Debug) - std::clog << "Renaming: " << DestFile << " -> " << FinalFile - << std::endl; - Rename(DestFile,FinalFile); - chmod(FinalFile.c_str(),0644); - DestFile = FinalFile; - - if(!ParseIndexDiff(DestFile)) - return Failed("", NULL); - else - return Finish(); - } - - // sucess in downloading a diff - if(Desc.URI.find(".diff") != string::npos) { - ApplyDiff(DestFile); - available_patches.erase(available_patches.begin()); - - if(available_patches.size() > 0) { - new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc, - ExpectedMD5, available_patches); - } else { - Finish(true); - return; - } - } - - Finish(); -} - - // AcqIndex::AcqIndex - Constructor /*{{{*/ // --------------------------------------------------------------------- /* The package file is added to the queue and a second class is instantiated to fetch the revision file */ pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner, string URI,string URIDesc,string ShortDesc, - string ExpectedMD5, string comprExt) - : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5) + string ExpectedMD5, string comprExt) : + Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5) { Decompression = false; Erase = false; - + DestFile = _config->FindDir("Dir::State::lists") + "partial/"; DestFile += URItoFileName(URI); if(comprExt.empty()) { - // autoselect - if(FileExists("/usr/bin/bzip2")) - Desc.URI = URI + ".bz2"; - else - Desc.URI = URI + ".gz"; + // autoselect the compression method + if(FileExists("/usr/bin/bzip2")) + CompressionExtension = ".bz2"; + else + CompressionExtension = ".gz"; } else { - Desc.URI = URI + comprExt; + CompressionExtension = comprExt; } + Desc.URI = URI + CompressionExtension; Desc.Description = URIDesc; Desc.Owner = this; @@ -483,7 +184,7 @@ string pkgAcqIndex::Custom600Headers() void pkgAcqIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) { // no .bz2 found, retry with .gz - if(Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1) == "bz2") { + if(Desc.URI.substr(Desc.URI.size()-3) == "bz2") { Desc.URI = Desc.URI.substr(0,Desc.URI.size()-3) + "gz"; // retry with a gzip one @@ -589,7 +290,7 @@ void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5, else Local = true; - string compExt = Desc.URI.substr(Desc.URI.size()-3,Desc.URI.size()-1); + string compExt = Desc.URI.substr(Desc.URI.size()-3); char *decompProg; if(compExt == "bz2") decompProg = "bzip2"; @@ -614,15 +315,15 @@ pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner, const vector* IndexTargets, indexRecords* MetaIndexParser) : Item(Owner), RealURI(URI), MetaIndexURI(MetaIndexURI), - MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc) + MetaIndexURIDesc(MetaIndexURIDesc), MetaIndexShortDesc(MetaIndexShortDesc), + MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets) { - this->MetaIndexParser = MetaIndexParser; - this->IndexTargets = IndexTargets; DestFile = _config->FindDir("Dir::State::lists") + "partial/"; DestFile += URItoFileName(URI); - // remove any partial downloaded sig-file. it may confuse proxies - // and is too small to warrant a partial download anyway + // remove any partial downloaded sig-file in partial/. + // it may confuse proxies and is too small to warrant a + // partial download anyway unlink(DestFile.c_str()); // Create the item @@ -640,12 +341,6 @@ pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner, // File was already in place. It needs to be re-verified // because Release might have changed, so Move it into partial Rename(Final,DestFile); - // unlink the file and do not try to use I-M-S and Last-Modified - // if the users proxy is broken - if(_config->FindB("Acquire::BrokenProxy", false) == true) { - std::cerr << "forcing re-get of the signature file as requested" << std::endl; - unlink(DestFile.c_str()); - } } QueueURI(Desc); @@ -695,9 +390,22 @@ void pkgAcqMetaSig::Done(string Message,unsigned long Size,string MD5, /*}}}*/ void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf) { - // Delete any existing sigfile, so that this source isn't - // mistakenly trusted string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI); + + // if we get a network error we fail gracefully + if(Status == StatTransientNetworkError) + { + Item::Failed(Message,Cnf); + // move the sigfile back on network failures (and re-authenticated?) + if(FileExists(DestFile)) + Rename(DestFile,Final); + + // set the status back to , Item::Failed likes to reset it + Status = pkgAcquire::Item::StatTransientNetworkError; + return; + } + + // Delete any existing sigfile when the acquire failed unlink(Final.c_str()); // queue a pkgAcqMetaIndex with no sigfile @@ -722,11 +430,9 @@ pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner, string SigFile, const vector* IndexTargets, indexRecords* MetaIndexParser) : - Item(Owner), RealURI(URI), SigFile(SigFile) + Item(Owner), RealURI(URI), SigFile(SigFile), AuthPass(false), + MetaIndexParser(MetaIndexParser), IndexTargets(IndexTargets), IMSHit(false) { - this->AuthPass = false; - this->MetaIndexParser = MetaIndexParser; - this->IndexTargets = IndexTargets; DestFile = _config->FindDir("Dir::State::lists") + "partial/"; DestFile += URItoFileName(URI); @@ -818,6 +524,9 @@ void pkgAcqMetaIndex::RetrievalDone(string Message) return; } + // see if the download was a IMSHit + IMSHit = StringToBool(LookupTag(Message,"IMS-Hit"),false); + Complete = true; string FinalFile = _config->FindDir("Dir::State::lists"); @@ -846,7 +555,7 @@ void pkgAcqMetaIndex::AuthDone(string Message) return; } - if (!VerifyVendor()) + if (!VerifyVendor(Message)) { return; } @@ -899,12 +608,12 @@ void pkgAcqMetaIndex::QueueIndexes(bool verify) } // Queue Packages file - new pkgAcqIndexDiffs(Owner, (*Target)->URI, (*Target)->Description, - (*Target)->ShortDesc, ExpectedIndexMD5); + new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description, + (*Target)->ShortDesc, ExpectedIndexMD5); } } -bool pkgAcqMetaIndex::VerifyVendor() +bool pkgAcqMetaIndex::VerifyVendor(string Message) { // // Maybe this should be made available from above so we don't have // // to read and parse it every time? @@ -929,6 +638,22 @@ bool pkgAcqMetaIndex::VerifyVendor() // break; // } // } + string::size_type pos; + + // check for missing sigs (that where not fatal because otherwise we had + // bombed earlier) + string missingkeys; + string msg = _("There are no public key available for the " + "following key IDs:\n"); + pos = Message.find("NO_PUBKEY "); + if (pos != std::string::npos) + { + string::size_type start = pos+strlen("NO_PUBKEY "); + string Fingerprint = Message.substr(start, Message.find("\n")-start); + missingkeys += (Fingerprint); + } + if(!missingkeys.empty()) + _error->Warning("%s", string(msg+missingkeys).c_str()); string Transformed = MetaIndexParser->GetExpectedDist(); @@ -937,7 +662,7 @@ bool pkgAcqMetaIndex::VerifyVendor() Transformed = "experimental"; } - string::size_type pos = Transformed.rfind('/'); + pos = Transformed.rfind('/'); if (pos != string::npos) { Transformed = Transformed.substr(0, pos); @@ -983,10 +708,30 @@ void pkgAcqMetaIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf) { if (AuthPass == true) { - // gpgv method failed + // if we fail the authentication but got the file via a IMS-Hit + // this means that the file wasn't downloaded and that it might be + // just stale (server problem, proxy etc). we delete what we have + // queue it again without i-m-s + // alternatively we could just unlink the file and let the user try again + if (IMSHit) + { + Complete = false; + Local = false; + AuthPass = false; + unlink(DestFile.c_str()); + + DestFile = _config->FindDir("Dir::State::lists") + "partial/"; + DestFile += URItoFileName(RealURI); + Desc.URI = RealURI; + QueueURI(Desc); + return; + } + + // gpgv method failed _error->Warning("GPG error: %s: %s", Desc.Description.c_str(), LookupTag(Message,"Message").c_str()); + } // No Release file was present, or verification failed, so fall @@ -1062,6 +807,12 @@ pkgAcqArchive::pkgAcqArchive(pkgAcquire *Owner,pkgSourceList *Sources, } } + // "allow-unauthenticated" restores apts old fetching behaviour + // that means that e.g. unauthenticated file:// uris are higher + // priority than authenticated http:// uris + if (_config->FindB("APT::Get::AllowUnauthenticated",false) == true) + Trusted = false; + // Select a source if (QueueNext() == false && _error->PendingError() == false) _error->Error(_("I wasn't able to locate file for the %s package. " @@ -1198,7 +949,8 @@ void pkgAcqArchive::Done(string Message,unsigned long Size,string Md5Hash, { Status = StatError; ErrorText = _("MD5Sum mismatch"); - Rename(DestFile,DestFile + ".FAILED"); + if(FileExists(DestFile)) + Rename(DestFile,DestFile + ".FAILED"); return; } } @@ -1293,13 +1045,19 @@ void pkgAcqArchive::Finished() // --------------------------------------------------------------------- /* The file is added to the queue */ pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5, - unsigned long Size,string Dsc,string ShortDesc) : + unsigned long Size,string Dsc,string ShortDesc, + const string &DestDir, const string &DestFilename) : Item(Owner), Md5Hash(MD5) { Retries = _config->FindI("Acquire::Retries",0); - DestFile = flNotDir(URI); - + if(!DestFilename.empty()) + DestFile = DestFilename; + else if(!DestDir.empty()) + DestFile = DestDir + "/" + flNotDir(URI); + else + DestFile = flNotDir(URI); + // Create the item Desc.URI = URI; Desc.Description = Dsc; @@ -1319,7 +1077,7 @@ pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5, else PartialSize = Buf.st_size; } - + QueueURI(Desc); } /*}}}*/