* merge a fix from the pdiff branch
[ntk/apt.git] / apt-pkg / acquire-item.cc
index ad977dd..cbccfbf 100644 (file)
@@ -103,7 +103,8 @@ void pkgAcquire::Item::Done(string Message,unsigned long Size,string,
 {
    // We just downloaded something..
    string FileName = LookupTag(Message,"Filename");
-   if (Complete == false && FileName == DestFile)
+   // we only inform the Log class if it was actually not a local thing
+   if (Complete == false && !Local && FileName == DestFile)
    {
       if (Owner->Log != 0)
         Owner->Log->Fetched(Size,atoi(LookupTag(Message,"Resume-Point","0").c_str()));
@@ -169,8 +170,8 @@ pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire *Owner,
    //        from local sources. this is really silly, and
    //        should be fixed cleanly as soon as possible
    if(!FileExists(CurrentPackagesFile) || 
-      Desc.URI.substr(0,strlen("file:/")) == "file:/" ||
-      !_config->FindB("Acquire::Diffs",true)) {
+      Desc.URI.substr(0,strlen("file:/")) == "file:/")
+   {
       // we don't have a pkg file or we don't want to queue
       if(Debug)
         std::clog << "No index file, local or canceld by user" << std::endl;
@@ -178,11 +179,10 @@ pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire *Owner,
       return;
    }
 
-   if(Debug) {
+   if(Debug) 
       std::clog << "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): " 
                << CurrentPackagesFile << std::endl;
-   }
-
+   
    QueueURI(Desc);
 
 }
@@ -237,26 +237,32 @@ bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile)
       SHA1.AddFD(fd.Fd(), fd.Size());
       local_sha1 = string(SHA1.Result());
 
-      if(local_sha1 == ServerSha1) {
+      if(local_sha1 == ServerSha1) 
+      {
+        // we have the same sha1 as the server
         if(Debug)
            std::clog << "Package file is up-to-date" << std::endl;
         // set found to true, this will queue a pkgAcqIndexDiffs with
         // a empty availabe_patches
         found = true;
-      } else {
+      } 
+      else 
+      {
         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);
-        while(hist >> d.sha1 >> size >> d.file) {
+        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(found) 
+           {
               if(Debug)
                  std::clog << "Need to get diff: " << d.file << std::endl;
               available_patches.push_back(d);
@@ -265,12 +271,15 @@ bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile)
       }
 
       // no information how to get the patches, bail out
-      if(!found) {
+      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 {
+      } 
+      else 
+      {
         // queue the diffs
         new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
                              ExpectedMD5, available_patches);
@@ -351,10 +360,13 @@ pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire *Owner,
    Desc.Owner = this;
    Desc.ShortDesc = ShortDesc;
 
-   if(available_patches.size() == 0) {
+   if(available_patches.size() == 0) 
+   {
       // we are done (yeah!)
       Finish(true);
-   } else {
+   }
+   else
+   {
       // get the next diff
       State = StateFetchDiff;
       QueueNextDiff();
@@ -378,7 +390,8 @@ void pkgAcqIndexDiffs::Finish(bool allDone)
 {
    // we restore the original name, this is required, otherwise
    // the file will be cleaned
-   if(allDone) {
+   if(allDone) 
+   {
       DestFile = _config->FindDir("Dir::State::lists");
       DestFile += URItoFileName(RealURI);
 
@@ -435,14 +448,17 @@ bool pkgAcqIndexDiffs::QueueNextDiff()
    // remove all patches until the next matching patch is found
    // this requires the Index file to be ordered
    for(vector<DiffInfo>::iterator I=available_patches.begin();
-       available_patches.size() > 0 && I != available_patches.end() 
-         && (*I).sha1 != local_sha1; 
-       I++) {
+       available_patches.size() > 0 && 
+         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) { 
+   if(available_patches.size() == 0) 
+   { 
       Failed("", NULL);
       return false;
    }
@@ -484,6 +500,7 @@ void pkgAcqIndexDiffs::Done(string Message,unsigned long Size,string Md5Hash,
 
       string FileName = LookupTag(Message,"Filename");
       State = StateUnzipDiff;
+      Local = true;
       Desc.URI = "gzip:" + FileName;
       DestFile += ".decomp";
       QueueURI(Desc);
@@ -502,6 +519,7 @@ void pkgAcqIndexDiffs::Done(string Message,unsigned long Size,string Md5Hash,
         std::clog << "Sending to rred method: " << FinalFile << std::endl;
 
       State = StateApplyDiff;
+      Local = true;
       Desc.URI = "rred:" + FinalFile;
       QueueURI(Desc);
       Mode = "rred";
@@ -587,7 +605,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 
@@ -693,7 +711,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";
@@ -718,10 +736,9 @@ pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
                             const vector<IndexTarget*>* 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);
 
@@ -744,12 +761,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);
@@ -799,18 +810,19 @@ 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);
-   unlink(Final.c_str());
 
-   // if we get a timeout if fail
+   // if we get a network error we fail gracefully
    if(LookupTag(Message,"FailReason") == "Timeout" || 
-      LookupTag(Message,"FailReason") == "TmpResolveFailure") {
+      LookupTag(Message,"FailReason") == "TmpResolveFailure" ||
+      LookupTag(Message,"FailReason") == "ConnectionRefused") {
       Item::Failed(Message,Cnf);
       return;
    }
 
+   // Delete any existing sigfile when the acquire failed
+   string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
+   unlink(Final.c_str());
+
    // queue a pkgAcqMetaIndex with no sigfile
    new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
                       "", IndexTargets, MetaIndexParser);
@@ -833,11 +845,9 @@ pkgAcqMetaIndex::pkgAcqMetaIndex(pkgAcquire *Owner,
                                 string SigFile,
                                 const vector<struct IndexTarget*>* 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);
 
@@ -929,6 +939,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");
@@ -957,7 +970,7 @@ void pkgAcqMetaIndex::AuthDone(string Message)
       return;
    }
 
-   if (!VerifyVendor())
+   if (!VerifyVendor(Message))
    {
       return;
    }
@@ -1009,13 +1022,18 @@ void pkgAcqMetaIndex::QueueIndexes(bool verify)
          }
       }
       
-      // Queue Packages file
-      new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
-                         (*Target)->ShortDesc, ExpectedIndexMD5);
+      // Queue Packages file (either diff or full packages files, depending
+      // on the users option)
+      if(_config->FindB("Acquire::PDiffs",false) == false) 
+        new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
+                            (*Target)->ShortDesc, ExpectedIndexMD5);
+      else 
+        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?
@@ -1040,6 +1058,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();
 
@@ -1048,7 +1082,7 @@ bool pkgAcqMetaIndex::VerifyVendor()
       Transformed = "experimental";
    }
 
-   string::size_type pos = Transformed.rfind('/');
+   pos = Transformed.rfind('/');
    if (pos != string::npos)
    {
       Transformed = Transformed.substr(0, pos);
@@ -1094,10 +1128,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
@@ -1173,6 +1227,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. "
@@ -1405,13 +1465,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;
@@ -1431,7 +1497,7 @@ pkgAcqFile::pkgAcqFile(pkgAcquire *Owner,string URI,string MD5,
       else
         PartialSize = Buf.st_size;
    }
-   
+
    QueueURI(Desc);
 }
                                                                        /*}}}*/