]> git.saurik.com Git - apt.git/blobdiff - methods/server.cc
cleanup
[apt.git] / methods / server.cc
index 6c05700a57ecc20ccc9840b541c778c5cb8732bd..f61a6fedb7221617154e189a6bdd06b299e55258 100644 (file)
@@ -245,10 +245,21 @@ ServerState::ServerState(URI Srv, ServerMethod *Owner) : ServerName(Srv), TimeOu
    Reset();
 }
                                                                        /*}}}*/
+bool ServerState::AddPartialFileToHashes(FileFd &File)                 /*{{{*/
+{
+   File.Truncate(StartPos);
+   return GetHashes()->AddFD(File, StartPos);
+}
+                                                                       /*}}}*/
 
 bool ServerMethod::Configuration(string Message)                       /*{{{*/
 {
-   return pkgAcqMethod::Configuration(Message);
+   if (pkgAcqMethod::Configuration(Message) == false)
+      return false;
+
+   DropPrivsOrDie();
+
+   return true;
 }
                                                                        /*}}}*/
 
@@ -268,7 +279,7 @@ ServerMethod::DealWithHeaders(FetchResult &Res)
       Res.LastModified = Queue->LastModified;
       return IMS_HIT;
    }
-   
+
    /* Redirect
     *
     * Note that it is only OK for us to treat all redirection the same
@@ -313,7 +324,20 @@ ServerMethod::DealWithHeaders(FetchResult &Res)
       struct stat SBuf;
       if (stat(Queue->DestFile.c_str(),&SBuf) >= 0 && SBuf.st_size > 0)
       {
-        if ((unsigned long long)SBuf.st_size == Server->TotalFileSize)
+        bool partialHit = false;
+        if (Queue->ExpectedHashes.usable() == true)
+        {
+           Hashes resultHashes(Queue->ExpectedHashes);
+           FileFd file(Queue->DestFile, FileFd::ReadOnly);
+           Server->TotalFileSize = file.FileSize();
+           Server->Date = file.ModificationTime();
+           resultHashes.AddFD(file);
+           HashStringList const hashList = resultHashes.GetHashStringList();
+           partialHit = (Queue->ExpectedHashes == hashList);
+        }
+        else if ((unsigned long long)SBuf.st_size == Server->TotalFileSize)
+           partialHit = true;
+        if (partialHit == true)
         {
            // the file is completely downloaded, but was not moved
            if (Server->HaveContent == true)
@@ -338,10 +362,10 @@ ServerMethod::DealWithHeaders(FetchResult &Res)
       failure */
    if (Server->Result < 200 || Server->Result >= 300)
    {
-      char err[255];
-      snprintf(err,sizeof(err)-1,"HttpError%i",Server->Result);
+      std::string err;
+      strprintf(err, "HttpError%u", Server->Result);
       SetFailReason(err);
-      _error->Error("%u %s",Server->Result,Server->Code);
+      _error->Error("%u %s", Server->Result, Server->Code);
       if (Server->HaveContent == true)
         return ERROR_WITH_CONTENT_PAGE;
       return ERROR_UNRECOVERABLE;
@@ -362,7 +386,7 @@ ServerMethod::DealWithHeaders(FetchResult &Res)
    FailFd = File->Fd();
    FailTime = Server->Date;
 
-   if (Server->InitHashes(*File) == false)
+   if (Server->InitHashes(Queue->ExpectedHashes) == false || Server->AddPartialFileToHashes(*File) == false)
    {
       _error->Errno("read",_("Problem hashing file"));
       return ERROR_NOT_FROM_SERVER;
@@ -407,9 +431,16 @@ bool ServerMethod::Fetch(FetchItem *)
    for (FetchItem *I = Queue; I != 0 && Depth < (signed)PipelineDepth; 
        I = I->Next, Depth++)
    {
-      // If pipelining is disabled, we only queue 1 request
-      if (Server->Pipeline == false && Depth >= 0)
-        break;
+      if (Depth >= 0)
+      {
+        // If pipelining is disabled, we only queue 1 request
+        if (Server->Pipeline == false)
+           break;
+        // if we have no hashes, do at most one such request
+        // as we can't fixup pipeling misbehaviors otherwise
+        else if (I->ExpectedHashes.usable() == false)
+           break;
+      }
       
       // Make sure we stick with the same server
       if (Server->Comp(I->Uri) == false)
@@ -539,6 +570,13 @@ int ServerMethod::Loop()
 
            // Run the data
            bool Result = true;
+
+            // ensure we don't fetch too much
+            // we could do "Server->MaximumSize = Queue->MaximumSize" here
+            // but that would break the clever pipeline messup detection
+            // so instead we use the size of the biggest item in the queue
+            Server->MaximumSize = FindMaximumObjectSizeInQueue();
+
             if (Server->HaveContent)
               Result = Server->RunData(File);
 
@@ -561,7 +599,38 @@ int ServerMethod::Loop()
            // Send status to APT
            if (Result == true)
            {
-              Res.TakeHashes(*Server->GetHashes());
+              Hashes * const resultHashes = Server->GetHashes();
+              HashStringList const hashList = resultHashes->GetHashStringList();
+              if (PipelineDepth != 0 && Queue->ExpectedHashes.usable() == true && Queue->ExpectedHashes != hashList)
+              {
+                 // we did not get the expected hash… mhhh:
+                 // could it be that server/proxy messed up pipelining?
+                 FetchItem * BeforeI = Queue;
+                 for (FetchItem *I = Queue->Next; I != 0 && I != QueueBack; I = I->Next)
+                 {
+                    if (I->ExpectedHashes.usable() == true && I->ExpectedHashes == hashList)
+                    {
+                       // yes, he did! Disable pipelining and rewrite queue
+                       if (Server->Pipeline == true)
+                       {
+                          // FIXME: fake a warning message as we have no proper way of communicating here
+                          std::string out;
+                          strprintf(out, _("Automatically disabled %s due to incorrect response from server/proxy. (man 5 apt.conf)"), "Acquire::http::PipelineDepth");
+                          std::cerr << "W: " << out << std::endl;
+                          Server->Pipeline = false;
+                          // we keep the PipelineDepth value so that the rest of the queue can be fixed up as well
+                       }
+                       Rename(Res.Filename, I->DestFile);
+                       Res.Filename = I->DestFile;
+                       BeforeI->Next = I->Next;
+                       I->Next = Queue;
+                       Queue = I;
+                       break;
+                    }
+                    BeforeI = I;
+                 }
+              }
+              Res.TakeHashes(*resultHashes);
               URIDone(Res);
            }
            else
@@ -581,7 +650,10 @@ int ServerMethod::Loop()
                  QueueBack = Queue;
               }
               else
+               {
+                  Server->Close();
                  Fail(true);
+               }
            }
            break;
         }
@@ -674,5 +746,15 @@ int ServerMethod::Loop()
    }
    
    return 0;
+}
+                                                                       /*}}}*/
+                                                                       /*{{{*/
+unsigned long long
+ServerMethod::FindMaximumObjectSizeInQueue() const 
+{
+   unsigned long long MaxSizeInQueue = 0;
+   for (FetchItem *I = Queue; I != 0 && I != QueueBack; I = I->Next)
+      MaxSizeInQueue = std::max(MaxSizeInQueue, I->MaximumSize);
+   return MaxSizeInQueue;
 }
                                                                        /*}}}*/