]> git.saurik.com Git - apt.git/blobdiff - methods/rred.cc
correct IndexDiff vs DiffIndex in Debug output
[apt.git] / methods / rred.cc
index bf9294d96c328f7669df43e5a0fbb2bd716a481d..bea8ed263c100931eda64494f38d125448b3bb35 100644 (file)
 
 #include <sys/stat.h>
 #include <sys/uio.h>
+#include <sys/types.h>
+#include <fcntl.h>
 #include <unistd.h>
 #include <utime.h>
 #include <stdio.h>
 #include <errno.h>
-#include <zlib.h>
 #include <apti18n.h>
                                                                                /*}}}*/
 /** \brief RredMethod - ed-style incremential patch method                     {{{
@@ -227,6 +228,21 @@ struct EdCommand {
   char type;
 };
 #define IOV_COUNT 1024 /* Don't really want IOV_MAX since it can be arbitrarily large */
+static ssize_t retry_writev(int fd, const struct iovec *iov, int iovcnt) {
+       ssize_t Res;
+       errno = 0;
+       ssize_t i = 0;
+       do {
+               Res = writev(fd, iov + i, iovcnt);
+               if (Res < 0 && errno == EINTR)
+                       continue;
+               if (Res < 0)
+                       return _error->Errno("writev",_("Write error"));
+               iovcnt -= Res;
+               i += Res;
+       } while (Res > 0 && iovcnt > 0);
+       return i;
+}
 #endif
                                                                                /*}}}*/
 RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From,           /*{{{*/
@@ -235,7 +251,9 @@ RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From,                /*{{{*/
        MMap ed_cmds(Patch, MMap::ReadOnly);
        MMap in_file(From, MMap::ReadOnly);
 
-       if (ed_cmds.Size() == 0 || in_file.Size() == 0)
+       unsigned long long const ed_size = ed_cmds.Size();
+       unsigned long long const in_size = in_file.Size();
+       if (ed_size == 0 || in_size == 0)
                return MMAP_FAILED;
 
        EdCommand* commands = 0;
@@ -244,10 +262,10 @@ RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From,              /*{{{*/
 
        const char* begin = (char*) ed_cmds.Data();
        const char* end = begin;
-       const char* ed_end = (char*) ed_cmds.Data() + ed_cmds.Size();
+       const char* ed_end = (char*) ed_cmds.Data() + ed_size;
 
        const char* input = (char*) in_file.Data();
-       const char* input_end = (char*) in_file.Data() + in_file.Size();
+       const char* input_end = (char*) in_file.Data() + in_size;
 
        size_t i;
 
@@ -331,7 +349,12 @@ RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From,               /*{{{*/
                }
                if(command_count == command_alloc) {
                        command_alloc = (command_alloc + 64) * 3 / 2;
-                       commands = (EdCommand*) realloc(commands, command_alloc * sizeof(EdCommand));
+                       EdCommand* newCommands = (EdCommand*) realloc(commands, command_alloc * sizeof(EdCommand));
+                       if (newCommands == NULL) {
+                               free(commands);
+                               return MMAP_FAILED;
+                       }
+                       commands = newCommands;
                }
                commands[command_count++] = cmd;
        }
@@ -370,7 +393,7 @@ RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From,                /*{{{*/
                        hash->Add((const unsigned char*) begin, input - begin);
 
                        if(++iov_size == IOV_COUNT) {
-                               writev(out_file.Fd(), iov, IOV_COUNT);
+                               retry_writev(out_file.Fd(), iov, IOV_COUNT);
                                iov_size = 0;
                        }
                }
@@ -395,7 +418,7 @@ RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From,                /*{{{*/
                                iov[iov_size].iov_len);
 
                                if(++iov_size == IOV_COUNT) {
-                                       writev(out_file.Fd(), iov, IOV_COUNT);
+                                       retry_writev(out_file.Fd(), iov, IOV_COUNT);
                                        iov_size = 0;
                                }
                        }
@@ -410,15 +433,15 @@ RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From,              /*{{{*/
        }
 
        if(iov_size) {
-               writev(out_file.Fd(), iov, iov_size);
+               retry_writev(out_file.Fd(), iov, iov_size);
                iov_size = 0;
        }
 
        for(i = 0; i < iov_size; i += IOV_COUNT) {
                if(iov_size - i < IOV_COUNT)
-                       writev(out_file.Fd(), iov + i, iov_size - i);
+                       retry_writev(out_file.Fd(), iov + i, iov_size - i);
                else
-                       writev(out_file.Fd(), iov + i, IOV_COUNT);
+                       retry_writev(out_file.Fd(), iov + i, IOV_COUNT);
        }
 
        delete [] iov;
@@ -444,50 +467,112 @@ bool RredMethod::Fetch(FetchItem *Itm)                                           /*{{{*/
    } else
       URIStart(Res);
 
-   if (Debug == true) 
-      std::clog << "Patching " << Path << " with " << Path 
-         << ".ed and putting result into " << Itm->DestFile << std::endl;
-   // Open the source and destination files (the d'tor of FileFd will do 
-   // the cleanup/closing of the fds)
-   FileFd From(Path,FileFd::ReadOnly);
-   FileFd Patch(Path+".ed",FileFd::ReadOnly, FileFd::Gzip);
-   FileFd To(Itm->DestFile,FileFd::WriteAtomic);   
-   To.EraseOnFailure();
-   if (_error->PendingError() == true)
-      return false;
-   
+   std::string lastPatchName;
    Hashes Hash;
-   // now do the actual patching
-   State const result = patchMMap(Patch, From, To, &Hash);
-   if (result == MMAP_FAILED) {
-      // retry with patchFile
-      Patch.Seek(0);
-      From.Seek(0);
-      To.Open(Itm->DestFile,FileFd::WriteAtomic);
+
+   // check for a single ed file
+   if (FileExists(Path+".ed") == true)
+   {
+      if (Debug == true)
+        std::clog << "Patching " << Path << " with " << Path
+           << ".ed and putting result into " << Itm->DestFile << std::endl;
+
+      // Open the source and destination files
+      lastPatchName = Path + ".ed";
+      FileFd From(Path,FileFd::ReadOnly);
+      FileFd To(Itm->DestFile,FileFd::WriteAtomic);
+      To.EraseOnFailure();
+      FileFd Patch(lastPatchName, FileFd::ReadOnly, FileFd::Gzip);
       if (_error->PendingError() == true)
-         return false;
-      if (patchFile(Patch, From, To, &Hash) != ED_OK) {
-        return _error->WarningE("rred", _("Could not patch %s with mmap and with file operation usage - the patch seems to be corrupt."), Path.c_str());
+        return false;
+
+      // now do the actual patching
+      State const result = patchMMap(Patch, From, To, &Hash);
+      if (result == MMAP_FAILED) {
+        // retry with patchFile
+        Patch.Seek(0);
+        From.Seek(0);
+        To.Open(Itm->DestFile,FileFd::WriteAtomic);
+        if (_error->PendingError() == true)
+           return false;
+        if (patchFile(Patch, From, To, &Hash) != ED_OK) {
+           return _error->WarningE("rred", _("Could not patch %s with mmap and with file operation usage - the patch seems to be corrupt."), Path.c_str());
+        } else if (Debug == true) {
+           std::clog << "rred: finished file patching of " << Path  << " after mmap failed." << std::endl;
+        }
+      } else if (result != ED_OK) {
+        return _error->Errno("rred", _("Could not patch %s with mmap (but no mmap specific fail) - the patch seems to be corrupt."), Path.c_str());
       } else if (Debug == true) {
-        std::clog << "rred: finished file patching of " << Path  << " after mmap failed." << std::endl;
+        std::clog << "rred: finished mmap patching of " << Path << std::endl;
       }
-   } else if (result != ED_OK) {
-      return _error->Errno("rred", _("Could not patch %s with mmap (but no mmap specific fail) - the patch seems to be corrupt."), Path.c_str());
-   } else if (Debug == true) {
-      std::clog << "rred: finished mmap patching of " << Path << std::endl;
+
+      // write out the result
+      From.Close();
+      Patch.Close();
+      To.Close();
    }
+   else
+   {
+      if (Debug == true)
+        std::clog << "Patching " << Path << " with all " << Path << ".ed.*.gz files and "
+           << "putting result into " << Itm->DestFile << std::endl;
+
+      int From = open(Path.c_str(), O_RDONLY);
+      unlink(Itm->DestFile.c_str());
+      int To = open(Itm->DestFile.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0644);
+      SetCloseExec(From, false);
+      SetCloseExec(To, false);
+
+      _error->PushToStack();
+      std::vector<std::string> patches = GetListOfFilesInDir(flNotFile(Path), "gz", true, false);
+      _error->RevertToStack();
+
+      std::string externalrred = _config->Find("Dir::Bin::rred", "/usr/bin/diffindex-rred");
+      std::vector<const char *> Args;
+      Args.reserve(22);
+      Args.push_back(externalrred.c_str());
+
+      std::string const baseName = Path + ".ed.";
+      for (std::vector<std::string>::const_iterator p = patches.begin();
+           p != patches.end(); ++p)
+        if (p->compare(0, baseName.length(), baseName) == 0)
+           Args.push_back(p->c_str());
+
+      Args.push_back(NULL);
+
+      pid_t Patcher = ExecFork();
+      if (Patcher == 0) {
+        dup2(From, STDIN_FILENO);
+        dup2(To, STDOUT_FILENO);
+
+        execvp(Args[0], (char **) &Args[0]);
+        std::cerr << "Failed to execute patcher " << Args[0] << "!" << std::endl;
+        _exit(100);
+      }
+      // last is NULL, so the one before is the last patch
+      lastPatchName = Args[Args.size() - 2];
+
+      if (ExecWait(Patcher, "rred") == false)
+        return _error->Errno("rred", "Patching via external rred failed");
+
+      close(From);
+      close(To);
 
-   // write out the result
-   From.Close();
-   Patch.Close();
-   To.Close();
+      struct stat Buf;
+      if (stat(Itm->DestFile.c_str(), &Buf) != 0)
+        return _error->Errno("stat",_("Failed to stat"));
+
+      To = open(Path.c_str(), O_RDONLY);
+      Hash.AddFD(To, Buf.st_size);
+      close(To);
+   }
 
    /* Transfer the modification times from the patch file
       to be able to see in which state the file should be
       and use the access time from the "old" file */
    struct stat BufBase, BufPatch;
    if (stat(Path.c_str(),&BufBase) != 0 ||
-       stat(std::string(Path+".ed").c_str(),&BufPatch) != 0)
+        stat(lastPatchName.c_str(), &BufPatch) != 0)
       return _error->Errno("stat",_("Failed to stat"));
 
    struct utimbuf TimeBuf;