Преглед изворни кода

Refactor compression filtering code.

Guillem Jover пре 19 година
родитељ
комит
0bfd57916d
3 измењених фајлова са 31 додато и 46 уклоњено
  1. 8 0
      ChangeLog
  2. 1 0
      debian/changelog
  3. 22 46
      lib/compression.c

+ 8 - 0
ChangeLog

@@ -1,3 +1,11 @@
+2007-03-06  Guillem Jover  <guillem@debian.org>
+
+	* lib/compression.c (fd_fd_filter): New function, refactored. As
+	a side effect the 'failed to exec' string gets unified, and all
+	commands use oshite now.
+	(decompress_cat): Use fd_fd_filter instead of the duped code.
+	(compress_cat): Likewise.
+
 2007-03-06  Guillem Jover  <guillem@debian.org>
 2007-03-06  Guillem Jover  <guillem@debian.org>
 
 
 	* scripts/dpkg-scanpackages.pl (usage): Documemt that the override
 	* scripts/dpkg-scanpackages.pl (usage): Documemt that the override

+ 1 - 0
debian/changelog

@@ -43,6 +43,7 @@ dpkg (1.14.0) UNRELEASED; urgency=low
     and dpkg-shlibdeps. Closes: #162348
     and dpkg-shlibdeps. Closes: #162348
   * Cleaning and format unification of manual pages.
   * Cleaning and format unification of manual pages.
   * Make the override-file argument to dpkg-scanpackages optional.
   * Make the override-file argument to dpkg-scanpackages optional.
+  * Refactor compression filtering code.
 
 
   [ Updated dpkg translations ]
   [ Updated dpkg translations ]
   * Romanian (Eddy Petrișor).
   * Romanian (Eddy Petrișor).

+ 22 - 46
lib/compression.c

@@ -17,6 +17,23 @@
 #include "dpkg.h"
 #include "dpkg.h"
 #include "dpkg-db.h"
 #include "dpkg-db.h"
 
 
+static void
+fd_fd_filter(int fd_in, int fd_out,
+	     const char *file, const char *cmd, const char *args,
+	     const char *desc)
+{
+  if (fd_in != 0) {
+    m_dup2(fd_in, 0);
+    close(fd_in);
+  }
+  if (fd_out != 1) {
+    m_dup2(fd_out, 1);
+    close(fd_out);
+  }
+  execlp(file, cmd, args, NULL);
+  ohshite(_("%s: failed to exec '%s %s'"), desc, cmd, args);
+}
+
 void decompress_cat(enum compression_type type, int fd_in, int fd_out, char *desc, ...) {
 void decompress_cat(enum compression_type type, int fd_in, int fd_out, char *desc, ...) {
   va_list al;
   va_list al;
   struct varbuf v;
   struct varbuf v;
@@ -49,15 +66,7 @@ void decompress_cat(enum compression_type type, int fd_in, int fd_out, char *des
       }
       }
       exit(0);
       exit(0);
 #else
 #else
-      if (fd_in != 0) {
-        m_dup2(fd_in, 0);
-        close(fd_in);
-      }
-      if (fd_out != 1) {
-        m_dup2(fd_out, 1);
-        close(fd_out);
-      }
-      execlp(GZIP,"gzip","-dc",(char*)0); ohshite(_("%s: failed to exec gzip -dc"), v.buf);
+      fd_fd_filter(fd_in, fd_out, GZIP, "gzip", "-dc", v.buf);
 #endif
 #endif
     case BZ2:
     case BZ2:
 #ifdef WITH_BZ2
 #ifdef WITH_BZ2
@@ -80,27 +89,10 @@ void decompress_cat(enum compression_type type, int fd_in, int fd_out, char *des
       }
       }
       exit(0);
       exit(0);
 #else
 #else
-      if (fd_in != 0) {
-        m_dup2(fd_in, 0);
-        close(fd_in);
-      }
-      if (fd_out != 1) {
-        m_dup2(fd_out, 1);
-        close(fd_out);
-      }
-      execlp(BZIP2,"bzip2","-dc",(char*)0); ohshite(_("%s: failed to exec bzip2 -dc"), v.buf);
+      fd_fd_filter(fd_in, fd_out, BZIP2, "bzip2", "-dc", v.buf);
 #endif
 #endif
     case compress_type_lzma:
     case compress_type_lzma:
-      if (fd_in != 0) {
-        m_dup2(fd_in, 0);
-        close(fd_in);
-      }
-      if (fd_out != 1) {
-        m_dup2(fd_out, 1);
-        close(fd_out);
-      }
-      execlp(LZMA, "lzma", "-dc", (char *)0);
-      ohshite(_("%s: failed to exec %s"), v.buf, "lzma -dc");
+      fd_fd_filter(fd_in, fd_out, LZMA, "lzma", "-dc", v.buf);
     case CAT:
     case CAT:
       fd_fd_copy(fd_in, fd_out, -1, _("%s: decompression"), v.buf);
       fd_fd_copy(fd_in, fd_out, -1, _("%s: decompression"), v.buf);
       exit(0);
       exit(0);
@@ -155,17 +147,9 @@ void compress_cat(enum compression_type type, int fd_in, int fd_out, const char
         exit(0);
         exit(0);
       }
       }
 #else
 #else
-      if (fd_in != 0) {
-        m_dup2(fd_in, 0);
-        close(fd_in);
-      }
-      if (fd_out != 1) {
-        m_dup2(fd_out, 1);
-        close(fd_out);
-      }
       strncpy(combuf, "-9c", sizeof(combuf));
       strncpy(combuf, "-9c", sizeof(combuf));
       combuf[1]= *compression;
       combuf[1]= *compression;
-      execlp(GZIP,"gzip",combuf,(char*)0); ohshit(_("%s: failed to exec gzip %s"), v.buf, combuf);
+      fd_fd_filter(fd_in, fd_out, GZIP, "gzip", combuf, v.buf);
 #endif
 #endif
     case BZ2:
     case BZ2:
 #ifdef WITH_BZ2
 #ifdef WITH_BZ2
@@ -198,17 +182,9 @@ void compress_cat(enum compression_type type, int fd_in, int fd_out, const char
         exit(0);
         exit(0);
       }
       }
 #else
 #else
-      if (fd_in != 0) {
-        m_dup2(fd_in, 0);
-        close(fd_in);
-      }
-      if (fd_out != 1) {
-        m_dup2(fd_out, 1);
-        close(fd_out);
-      }
       strncpy(combuf, "-9c", sizeof(combuf));
       strncpy(combuf, "-9c", sizeof(combuf));
       combuf[1]= *compression;
       combuf[1]= *compression;
-      execlp(BZIP2,"bzip2",combuf,(char*)0); ohshit(_("%s: failed to exec bzip2 %s"), v.buf, combuf);
+      fd_fd_filter(fd_in, fd_out, BZIP2, "bzip2", combuf, v.buf);
 #endif
 #endif
     case CAT:
     case CAT:
       fd_fd_copy(fd_in, fd_out, -1, _("%s: compression"), v.buf);
       fd_fd_copy(fd_in, fd_out, -1, _("%s: compression"), v.buf);