]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/acquire-item.cc
* merged pdiff support
[apt.git] / apt-pkg / acquire-item.cc
index 79e0b18986348e325e21a823dd3387e36546bf38..75a7faa2acc333afbd7362dfd6f9de4642557dc0 100644 (file)
@@ -134,34 +134,37 @@ void pkgAcquire::Item::Rename(string From,string To)
 }
                                                                        /*}}}*/
 
-// AcqIndexDiffs::AcqIndexDiffs - Constructor                  
+
+// AcqDiffIndex::AcqDiffIndex - Constructor                    
 // ---------------------------------------------------------------------
-/* The package diff is added to the queue. one object is constructed
- * for each diff and the index
+/* Get the DiffIndex file first and see if there are patches availabe 
+ * If so, create a pkgAcqIndexDiffs fetcher that will get and apply the
+ * patches. If anything goes wrong in that process, it will fall back to
+ * the original packages file
  */
-pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire *Owner,
-                                  string URI,string URIDesc,string ShortDesc,
-                                  string ExpectedMD5, vector<DiffInfo> diffs) 
-   : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5), available_patches(diffs)
+pkgAcqDiffIndex::pkgAcqDiffIndex(pkgAcquire *Owner,
+                                string URI,string URIDesc,string ShortDesc,
+                                string ExpectedMD5)
+   : Item(Owner), RealURI(URI), ExpectedMD5(ExpectedMD5), Description(URIDesc)
 {
    
-   DestFile = _config->FindDir("Dir::State::lists") + "partial/";
-   DestFile += URItoFileName(URI);
-
    Debug = _config->FindB("Debug::pkgAcquire::Diffs",false);
 
-   Desc.Description = URIDesc;
+   Desc.Description = URIDesc + "/DiffIndex";
    Desc.Owner = this;
    Desc.ShortDesc = ShortDesc;
+   Desc.URI = URI + ".diff/Index";
 
+   DestFile = _config->FindDir("Dir::State::lists") + "partial/";
+   DestFile += URItoFileName(URI) + string(".DiffIndex");
+
+   if(Debug)
+      std::clog << "pkgAcqDiffIndex: " << Desc.URI << std::endl;
+
+   // look for the current package file
    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
@@ -171,33 +174,20 @@ pkgAcqIndexDiffs::pkgAcqIndexDiffs(pkgAcquire *Owner,
       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;
+   if(Debug) {
+      std::clog << "pkgAcqIndexDiffs::pkgAcqIndexDiffs(): " 
+               << CurrentPackagesFile << std::endl;
+   }
 
    QueueURI(Desc);
+
 }
 
 // AcqIndex::Custom600Headers - Insert custom request headers          /*{{{*/
 // ---------------------------------------------------------------------
 /* The only header we use is the last-modified header. */
-string pkgAcqIndexDiffs::Custom600Headers()
+string pkgAcqDiffIndex::Custom600Headers()
 {
-   if(DestFile.rfind(".IndexDiff") == string::npos)
-      return string("");
-
    string Final = _config->FindDir("Dir::State::lists");
    Final += URItoFileName(RealURI) + string(".IndexDiff");
    
@@ -211,12 +201,169 @@ string pkgAcqIndexDiffs::Custom600Headers()
    return "\nIndex-File: true\nLast-Modified: " + TimeRFC1123(Buf.st_mtime);
 }
 
+
+bool pkgAcqDiffIndex::ParseDiffIndex(string IndexDiffFile)
+{
+   if(Debug)
+      std::clog << "pkgAcqIndexDiffs::ParseIndexDiff() " << IndexDiffFile 
+               << std::endl;
+
+   pkgTagSection Tags;
+   string ServerSha1;
+   vector<DiffInfo> available_patches;
+   
+   FileFd Fd(IndexDiffFile,FileFd::ReadOnly);
+   pkgTagFile TF(&Fd);
+   if (_error->PendingError() == true)
+      return false;
+
+   if(TF.Step(Tags) == true)
+   {
+      string local_sha1;
+      bool found = false;
+      DiffInfo d;
+      string size;
+
+      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;
+        // set found to true, this will queue a pkgAcqIndexDiffs with
+        // a empty availabe_patches
+        found = true;
+      } 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) {
+           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);
+        Complete = false;
+        Status = StatDone;
+        Dequeue();
+        return true;
+      }
+   }
+
+   return false;
+}
+
+void pkgAcqDiffIndex::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
+{
+   if(Debug)
+      std::clog << "pkgAcqDiffIndex failed: " << Desc.URI << std::endl
+               << "Falling back to normal index file aquire" << std::endl;
+
+   new pkgAcqIndex(Owner, RealURI, Description, Desc.ShortDesc, 
+                  ExpectedMD5);
+
+   Complete = false;
+   Status = StatDone;
+   Dequeue();
+}
+
+void pkgAcqDiffIndex::Done(string Message,unsigned long Size,string Md5Hash,
+                          pkgAcquire::MethodConfig *Cnf)
+{
+   if(Debug)
+      std::clog << "pkgAcqDiffIndex::Done(): " << Desc.URI << std::endl;
+
+   Item::Done(Message,Size,Md5Hash,Cnf);
+
+   string FinalFile;
+   FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
+
+   // sucess in downloading the index
+   // rename the index
+   FinalFile += string(".IndexDiff");
+   if(Debug)
+      std::clog << "Renaming: " << DestFile << " -> " << FinalFile 
+               << std::endl;
+   Rename(DestFile,FinalFile);
+   chmod(FinalFile.c_str(),0644);
+   DestFile = FinalFile;
+
+   if(!ParseDiffIndex(DestFile))
+      return Failed("", NULL);
+
+   Complete = true;
+   Status = StatDone;
+   Dequeue();
+   return;
+}
+
+
+
+// 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<DiffInfo> 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;
+
+   if(available_patches.size() == 0) {
+      // we are done (yeah!)
+      Finish(true);
+   } else {
+      // get the next diff
+      State = StateFetchDiff;
+      QueueNextDiff();
+   }
+}
+
+
 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, 
+      std::clog << "pkgAcqIndexDiffs failed: " << Desc.URI << std::endl
+               << "Falling back to normal index file aquire" << std::endl;
+   new pkgAcqIndex(Owner, RealURI, Description,Desc.ShortDesc, 
                   ExpectedMD5);
    Finish();
 }
@@ -228,15 +375,32 @@ 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);
+
+      // do the final md5sum checking
+      MD5Summation sum;
+      FileFd Fd(DestFile, FileFd::ReadOnly);
+      sum.AddFD(Fd.Fd(), Fd.Size());
+      Fd.Close();
+      string MD5 = (string)sum.Result();
+
+      if (!ExpectedMD5.empty() && MD5 != ExpectedMD5)
+      {
+        Status = StatAuthError;
+        ErrorText = _("MD5Sum mismatch");
+        Rename(DestFile,DestFile + ".FAILED");
+        Dequeue();
+        return;
+      }
+
+      // this is for the "real" finish
       Complete = true;
+      Status = StatDone;
       Dequeue();
       if(Debug)
         std::clog << "\n\nallDone: " << DestFile << "\n" << std::endl;
       return;
-
    }
 
    if(Debug)
@@ -248,36 +412,10 @@ void pkgAcqIndexDiffs::Finish(bool allDone)
 }
 
 
-// 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);
@@ -286,13 +424,18 @@ bool pkgAcqIndexDiffs::QueueNextDiff()
    SHA1Summation SHA1;
    SHA1.AddFD(fd.Fd(), fd.Size());
    string local_sha1 = string(SHA1.Result());
+   if(Debug)
+      std::clog << "QueueNextDiff: " 
+               << FinalFile << " (" << local_sha1 << ")"<<std::endl;
 
    // 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();
-       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) { 
@@ -301,11 +444,11 @@ bool pkgAcqIndexDiffs::QueueNextDiff()
    }
 
    // queue the right diff
-   Desc.URI = string(RealURI) + string(".diff/") + available_patches[0].file + string(".gz");
+   Desc.URI = string(RealURI) + ".diff/" + available_patches[0].file + ".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);
+   DestFile += URItoFileName(RealURI + ".diff/" + available_patches[0].file);
 
    if(Debug)
       std::clog << "pkgAcqIndexDiffs::QueueNextDiff(): " << Desc.URI << std::endl;
@@ -315,74 +458,6 @@ bool pkgAcqIndexDiffs::QueueNextDiff()
    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,
@@ -393,41 +468,65 @@ void pkgAcqIndexDiffs::Done(string Message,unsigned long Size,string Md5Hash,
 
    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") {
+   string FinalFile;
+   FinalFile = _config->FindDir("Dir::State::lists")+URItoFileName(RealURI);
+
+   // sucess in downloading a diff, enter ApplyDiff state
+   if(State == StateFetchDiff) 
+   {
 
-      // 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;
+        std::clog << "Sending to gzip method: " << FinalFile << std::endl;
+
+      string FileName = LookupTag(Message,"Filename");
+      State = StateUnzipDiff;
+      Desc.URI = "gzip:" + FileName;
+      DestFile += ".decomp";
+      QueueURI(Desc);
+      Mode = "gzip";
+      return;
+   } 
+
+   // sucess in downloading a diff, enter ApplyDiff state
+   if(State == StateUnzipDiff) 
+   {
+
+      // rred excepts the patch as $FinalFile.ed
+      Rename(DestFile,FinalFile+".ed");
+
+      if(Debug)
+        std::clog << "Sending to rred method: " << FinalFile << std::endl;
+
+      State = StateApplyDiff;
+      Desc.URI = "rred:" + FinalFile;
+      QueueURI(Desc);
+      Mode = "rred";
+      return;
+   } 
 
-      if(!ParseIndexDiff(DestFile))
-        return Failed("", NULL);
-      else 
-        return Finish();
-   }
 
-   // sucess in downloading a diff
-   if(Desc.URI.find(".diff") != string::npos) {
-      ApplyDiff(DestFile);
+   // success in download/apply a diff, queue next (if needed)
+   if(State == StateApplyDiff)
+   {
+      // remove the just applied patch
       available_patches.erase(available_patches.begin());
 
+      // move into place
+      if(Debug) 
+      {
+        std::clog << "Moving patched file in place: " << std::endl
+                  << DestFile << " -> " << FinalFile << std::endl;
+      }
+      Rename(DestFile,FinalFile);
+
+      // see if there is more to download
       if(available_patches.size() > 0) {
         new pkgAcqIndexDiffs(Owner, RealURI, Description, Desc.ShortDesc,
                              ExpectedMD5, available_patches);
-      } else {
-        Finish(true);
-        return;
-      }
+        return Finish();
+      } else 
+        return Finish(true);
    }
-
-   Finish();
 }
 
 
@@ -442,20 +541,21 @@ pkgAcqIndex::pkgAcqIndex(pkgAcquire *Owner,
 {
    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;
@@ -607,6 +707,35 @@ void pkgAcqIndex::Done(string Message,unsigned long Size,string MD5,
    Mode = decompProg;
 }
 
+// AcqIndexTrans::pkgAcqIndexTrans - Constructor                       /*{{{*/
+// ---------------------------------------------------------------------
+/* The Translation file is added to the queue */
+pkgAcqIndexTrans::pkgAcqIndexTrans(pkgAcquire *Owner,
+                           string URI,string URIDesc,string ShortDesc) :
+                      pkgAcqIndex(Owner, URI, URIDesc, ShortDesc, "", "")
+{
+}
+
+                                                                       /*}}}*/
+// AcqIndexTrans::Failed - Silence failure messages for missing files  /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void pkgAcqIndexTrans::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
+{
+   if (Cnf->LocalOnly == true || 
+       StringToBool(LookupTag(Message,"Transient-Failure"),false) == false)
+   {      
+      // Ignore this
+      Status = StatDone;
+      Complete = false;
+      Dequeue();
+      return;
+   }
+   
+   Item::Failed(Message,Cnf);
+}
+                                                                       /*}}}*/
+
 pkgAcqMetaSig::pkgAcqMetaSig(pkgAcquire *Owner,
                             string URI,string URIDesc,string ShortDesc,
                             string MetaIndexURI, string MetaIndexURIDesc,
@@ -700,6 +829,13 @@ void pkgAcqMetaSig::Failed(string Message,pkgAcquire::MethodConfig *Cnf)
    string Final = _config->FindDir("Dir::State::lists") + URItoFileName(RealURI);
    unlink(Final.c_str());
 
+   // if we get a timeout if fail
+   if(LookupTag(Message,"FailReason") == "Timeout" || 
+      LookupTag(Message,"FailReason") == "TmpResolveFailure") {
+      Item::Failed(Message,Cnf);
+      return;
+   }
+
    // queue a pkgAcqMetaIndex with no sigfile
    new pkgAcqMetaIndex(Owner, MetaIndexURI, MetaIndexURIDesc, MetaIndexShortDesc,
                       "", IndexTargets, MetaIndexParser);
@@ -899,8 +1035,8 @@ void pkgAcqMetaIndex::QueueIndexes(bool verify)
       }
       
       // Queue Packages file
-      new pkgAcqIndexDiffs(Owner, (*Target)->URI, (*Target)->Description,
-                          (*Target)->ShortDesc, ExpectedIndexMD5);
+      new pkgAcqDiffIndex(Owner, (*Target)->URI, (*Target)->Description,
+                         (*Target)->ShortDesc, ExpectedIndexMD5);
    }
 }
 
@@ -1198,7 +1334,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;
       }
    }