Explorar o código

dpkg-deb: Pass the real file descriptors to compression functions

Instead of duping them to the stdin and stdout file descriptors, pass
them directly. If the compression functions need the file descriptors
on stdin and stdout, then they will take care of setting them up. This
is only the case when using the external compression binaries.
Guillem Jover %!s(int64=15) %!d(string=hai) anos
pai
achega
ec5615cdd1
Modificáronse 2 ficheiros con 22 adicións e 15 borrados
  1. 12 10
      dpkg-deb/build.c
  2. 10 5
      dpkg-deb/extract.c

+ 12 - 10
dpkg-deb/build.c

@@ -474,8 +474,7 @@ do_build(const char *const *argv)
   /* And run gzip to compress our control archive. */
   c2 = subproc_fork();
   if (!c2) {
-    m_dup2(p1[0],0); m_dup2(gzfd,1); close(p1[0]); close(gzfd);
-    compress_filter(&compressor_gzip, 0, 1, 9, _("control member"));
+    compress_filter(&compressor_gzip, p1[0], gzfd, 9, _("control member"));
   }
   close(p1[0]);
   subproc_wait_check(c2, "gzip -9c", 0);
@@ -504,12 +503,17 @@ do_build(const char *const *argv)
     dpkg_ar_member_put_mem(debar, arfd, DEBMAGIC, deb_magic, strlen(deb_magic));
     dpkg_ar_member_put_file(debar, arfd, ADMINMEMBER, gzfd, -1);
   }
+  close(gzfd);
 
-  /* Control is done, now we need to archive the data. Start by creating
-   * a new temporary file. Immediately unlink the temporary file so others
-   * can't mess with it. */
-  if (!oldformatflag) {
-    close(gzfd);
+  /* Control is done, now we need to archive the data. */
+  if (oldformatflag) {
+    /* In old format, the data member is just concatenated after the
+     * control member, so we do not need a temporary file and can use
+     * the compression file descriptor. */
+    gzfd = arfd;
+  } else {
+    /* Start by creating a new temporary file. Immediately unlink the
+     * temporary file so others can't mess with it. */
     tfbuf = path_make_temp_template("dpkg-deb");
     gzfd = mkstemp(tfbuf);
     if (gzfd == -1)
@@ -538,9 +542,7 @@ do_build(const char *const *argv)
   c2 = subproc_fork();
   if (!c2) {
     close(p1[1]);
-    m_dup2(p2[0],0); close(p2[0]);
-    m_dup2(oldformatflag ? arfd : gzfd, 1);
-    compress_filter(compressor, 0, 1, compress_level, _("data member"));
+    compress_filter(compressor, p2[0], gzfd, compress_level, _("data member"));
   }
   close(p2[0]);
 

+ 10 - 5
dpkg-deb/extract.c

@@ -112,6 +112,7 @@ extracthalf(const char *debar, const char *dir, const char *taroption,
   int dummy;
   pid_t c1=0,c2,c3;
   int p1[2], p2[2];
+  int p2_out;
   int arfd;
   struct stat stab;
   char nlc;
@@ -269,14 +270,18 @@ extracthalf(const char *debar, const char *dir, const char *taroption,
   }
   close(p1[1]);
 
-  if (taroption) m_pipe(p2);
+  if (taroption) {
+    m_pipe(p2);
+    p2_out = p2[1];
+  } else {
+    p2_out = 1;
+  }
 
   c2 = subproc_fork();
   if (!c2) {
-    m_dup2(p1[0], 0);
-    if (admininfo) close(p1[0]);
-    if (taroption) { m_dup2(p2[1],1); close(p2[0]); close(p2[1]); }
-    decompress_filter(decompressor, 0, 1, _("data"));
+    if (taroption)
+      close(p2[0]);
+    decompress_filter(decompressor, p1[0], p2_out, _("data"));
   }
   close(p1[0]);
   close(arfd);