[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[PATCH v2 12/13] libdpkg: compression: move each format's code into its own function



Add compress_gzip, decompress_gzip, etc functions with code from
compress_cat and decompress_cat.  Instead of

	switch (type) {
	case compress_type_gzip:
	#ifdef WITH_ZLIB
		/* decompress using zlib */
	#else
		fd_fd_filter(fd_in, fd_out, GZIP, "gzip", "-dc", v.buf);
	#endif

we write

	switch(type) {
	case compress_type_gzip:
		decompress_gzip(...);

with decompress_gzip defined appropriately depending on the value
of WITH_ZLIB.  This should make each function easier to read on
its own.

Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 lib/dpkg/Makefile.am           |    1 +
 lib/dpkg/compression-backend.c |  207 ++++++++++++++++++++++++++++++++++++++++
 lib/dpkg/compression-backend.h |   35 +++++++
 lib/dpkg/compression.c         |  149 ++--------------------------
 4 files changed, 254 insertions(+), 138 deletions(-)
 create mode 100644 lib/dpkg/compression-backend.c
 create mode 100644 lib/dpkg/compression-backend.h

diff --git a/lib/dpkg/Makefile.am b/lib/dpkg/Makefile.am
index 4b31209..7428f7c 100644
--- a/lib/dpkg/Makefile.am
+++ b/lib/dpkg/Makefile.am
@@ -22,6 +22,7 @@ libdpkg_a_SOURCES = \
 	buffer.c buffer.h \
 	cleanup.c \
 	compression.c \
+	compression-backend.c compression-backend.h \
 	database.c \
 	dbmodify.c \
 	dump.c \
diff --git a/lib/dpkg/compression-backend.c b/lib/dpkg/compression-backend.c
new file mode 100644
index 0000000..9bfbcee
--- /dev/null
+++ b/lib/dpkg/compression-backend.c
@@ -0,0 +1,207 @@
+#include <config.h>
+#include <compat.h>
+
+#include <dpkg/i18n.h>
+
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#ifdef WITH_ZLIB
+#include <zlib.h>
+#endif
+#ifdef WITH_BZ2
+#include <bzlib.h>
+#endif
+
+#include <dpkg/dpkg.h>
+#include <dpkg/varbuf.h>
+#include <dpkg/buffer.h>
+#include <dpkg/macros.h>
+
+#include "compression-backend.h"
+
+static void fd_fd_filter(int fd_in, int fd_out, const char *desc,
+	const char *file, const char *cmd, const char *argfmt, ...)
+	DPKG_ATTR_NORET DPKG_ATTR_PRINTF(6);
+
+static void compress_cmd(int fd_in, int fd_out, const char *path,
+	const char *cmd, char compression, const char *desc)
+	DPKG_ATTR_NORET;
+
+static void
+fd_fd_filter(int fd_in, int fd_out, const char *desc,
+	const char *file, const char *cmd,
+	const char *argfmt, ...)
+{
+	struct varbuf argbuf = VARBUF_INIT;
+	va_list ap;
+
+	va_start(ap, argfmt);
+	varbufvprintf(&argbuf, argfmt, ap);
+	va_end(ap);
+
+	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, argbuf.buf, NULL);
+	ohshite(_("%s: failed to exec '%s %s'"), desc, cmd, argbuf.buf);
+
+	varbuffree(&argbuf);
+}
+
+#define DECOMPRESS(format, zFile, zdopen, zread, zerror, ERR_ERRNO, \
+		fd_in, fd_out, desc) do \
+{ \
+	char buffer[4096]; \
+	int actualread; \
+	zFile zfile = zdopen(fd_in, "r"); \
+	\
+	while ((actualread = zread(zfile, buffer, sizeof(buffer)))) { \
+		int actualwrite; \
+		\
+		if (actualread < 0) { \
+			int err = 0; \
+			const char *errmsg = zerror(zfile, &err); \
+			\
+			if (err == ERR_ERRNO) \
+				errmsg = strerror(errno); \
+			ohshit(_("%s: internal " format " error: %s: %s"), \
+			       desc, "read", errmsg); \
+		} \
+		\
+		actualwrite = write(fd_out, buffer, actualread); \
+		if (actualwrite != actualread) \
+			ohshite(_("%s: internal " format " error: %s"), \
+				desc, "write"); \
+	} \
+	exit(0); \
+} while(0)
+
+#define COMPRESS(format, zFile, zdopen, zwrite, zclose, zerror, ERR_ERRNO, \
+		fd_in, fd_out, compression, desc) do \
+{ \
+	char combuf[] = {'w', compression, '\0'}; \
+	int actualread, actualwrite; \
+	char buffer[4096]; \
+	zFile zfile; \
+	\
+	zfile = zdopen(fd_out, combuf); \
+	while ((actualread = read(fd_in, buffer, sizeof(buffer)))) { \
+		if (actualread < 0) \
+			ohshite(_("%s: internal " format " error: %s"), \
+			        desc, "read"); \
+		\
+		actualwrite = zwrite(zfile, buffer, actualread); \
+		if (actualwrite != actualread) { \
+			int err = 0; \
+			const char *errmsg = zerror(zfile, &err); \
+			\
+			if (err == ERR_ERRNO) \
+				errmsg = strerror(errno); \
+			ohshit(_("%s: internal " format " error: %s: %s"), \
+			        desc, "write", errmsg); \
+		} \
+	} \
+	zclose(zfile); \
+	exit(0); \
+} while(0)
+
+static void
+compress_cmd(int fd_in, int fd_out, const char *path,
+	const char *cmd, char compression, const char *desc)
+{
+	fd_fd_filter(fd_in, fd_out, desc, path, cmd, "-c%c", compression);
+}
+
+#ifdef WITH_ZLIB
+void
+decompress_gzip(int fd_in, int fd_out, const char *desc)
+{
+	DECOMPRESS("gzip", gzFile, gzdopen, gzread, gzerror, Z_ERRNO,
+		fd_in, fd_out, desc);
+}
+
+void
+compress_gzip(int fd_in, int fd_out, char compression, const char *desc)
+{
+	COMPRESS("gzip", gzFile, gzdopen, gzwrite, gzclose, gzerror, Z_ERRNO,
+		fd_in, fd_out, compression, desc);
+}
+#else /* !WITH_ZLIB */
+void
+decompress_gzip(int fd_in, int fd_out, const char *desc)
+{
+	fd_fd_filter(fd_in, fd_out, desc, GZIP, "gzip", "-dc");
+}
+
+void
+compress_gzip(int fd_in, int fd_out, char compression, const char *desc)
+{
+	compress_cmd(fd_in, fd_out, GZIP, "gzip", compression, desc);
+}
+#endif
+
+#ifdef WITH_BZ2
+void
+decompress_bzip2(int fd_in, int fd_out, const char *desc)
+{
+	DECOMPRESS("bzip2", BZFILE *, BZ2_bzdopen, BZ2_bzread,
+		BZ2_bzerror, BZ_IO_ERROR,
+		fd_in, fd_out, desc);
+}
+
+void
+compress_bzip2(int fd_in, int fd_out, char compression, const char *desc)
+{
+	COMPRESS("bzip2", BZFILE *, BZ2_bzdopen, BZ2_bzwrite, BZ2_bzclose,
+		BZ2_bzerror, BZ_IO_ERROR,
+		fd_in, fd_out, compression, desc);
+}
+#else /* !WITH_BZ2 */
+void
+decompress_bzip2(int fd_in, int fd_out, const char *desc)
+{
+	fd_fd_filter(fd_in, fd_out, desc, BZIP2, "bzip2", "-dc");
+}
+
+void
+compress_bzip2(int fd_in, int fd_out, char compression, const char *desc)
+{
+	compress_cmd(fd_in, fd_out, BZIP2, "bzip2", compression, desc);
+}
+#endif
+
+void
+decompress_lzma(int fd_in, int fd_out, const char *desc)
+{
+	fd_fd_filter(fd_in, fd_out, desc, LZMA, "lzma", "-dc");
+}
+
+void
+compress_lzma(int fd_in, int fd_out, char compression, const char *desc)
+{
+	compress_cmd(fd_in, fd_out, LZMA, "lzma", compression, desc);
+}
+
+void
+decompress_noop(int fd_in, int fd_out, const char *desc)
+{
+	fd_fd_copy(fd_in, fd_out, -1, _("%s: decompression"), desc);
+	exit(0);
+}
+
+void
+compress_noop(int fd_in, int fd_out, const char *desc)
+{
+	fd_fd_copy(fd_in, fd_out, -1, _("%s: compression"), desc);
+	exit(0);
+}
diff --git a/lib/dpkg/compression-backend.h b/lib/dpkg/compression-backend.h
new file mode 100644
index 0000000..7f3c5d6
--- /dev/null
+++ b/lib/dpkg/compression-backend.h
@@ -0,0 +1,35 @@
+/*
+ * libdpkg - Debian packaging suite library functions
+ * compression_backend.h - internal functions to compress and
+ *                         decompress archives
+ *
+ * See dpkg.h for the public interface (compress_cat/decompress_cat).
+ */
+
+#ifndef COMPRESSION_BACKEND_H
+#define COMPRESSION_BACKEND_H
+
+#include <config.h>
+#include <compat.h>
+
+#include <dpkg/macros.h>
+
+void decompress_gzip(int fd_in, int fd_out, const char *desc)
+	DPKG_ATTR_NORET;
+void decompress_bzip2(int fd_in, int fd_out, const char *desc)
+	DPKG_ATTR_NORET;
+void decompress_lzma(int fd_in, int fd_out, const char *desc)
+	DPKG_ATTR_NORET;
+void decompress_noop(int fd_in, int fd_out, const char *desc)
+	DPKG_ATTR_NORET;
+
+void compress_gzip(int fd_in, int fd_out,
+	char compression, const char *desc) DPKG_ATTR_NORET;
+void compress_bzip2(int fd_in, int fd_out,
+	char compression, const char *desc) DPKG_ATTR_NORET;
+void compress_lzma(int fd_in, int fd_out,
+	char compression, const char *desc) DPKG_ATTR_NORET;
+void compress_noop(int fd_in, int fd_out,
+	const char *desc) DPKG_ATTR_NORET;
+
+#endif /* COMPRESSION_BACKEND_H */
diff --git a/lib/dpkg/compression.c b/lib/dpkg/compression.c
index 33b0922..813526f 100644
--- a/lib/dpkg/compression.c
+++ b/lib/dpkg/compression.c
@@ -1,115 +1,12 @@
 #include <config.h>
 #include <compat.h>
 
-#include <dpkg/i18n.h>
-
 #include <stdlib.h>
-#include <unistd.h>
-#include <string.h>
-#include <errno.h>
-
-#ifdef WITH_ZLIB
-#include <zlib.h>
-#endif
-#ifdef WITH_BZ2
-#include <bzlib.h>
-#endif
+#include <stdarg.h>
 
 #include <dpkg/dpkg.h>
-#include <dpkg/dpkg-db.h>
-#include <dpkg/buffer.h>
-
-static void
-fd_fd_filter(int fd_in, int fd_out, const char *desc,
-             const char *file, const char *cmd,
-             const char *argfmt, ...)
-{
-  struct varbuf argbuf = VARBUF_INIT;
-  va_list ap;
-
-  va_start(ap, argfmt);
-  varbufvprintf(&argbuf, argfmt, ap);
-  va_end(ap);
-
-  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, argbuf.buf, NULL);
-  ohshite(_("%s: failed to exec '%s %s'"), desc, cmd, argbuf.buf);
-
-  varbuffree(&argbuf);
-}
-
-#define DECOMPRESS(format, zFile, zdopen, zread, zerror, ERR_ERRNO, \
-                   fd_in, fd_out, desc) do \
-{ \
-  char buffer[4096]; \
-  int actualread; \
-  zFile zfile = zdopen(fd_in, "r"); \
-  \
-  while ((actualread = zread(zfile, buffer, sizeof(buffer)))) { \
-    int actualwrite; \
-    \
-    if (actualread < 0) { \
-      int err = 0; \
-      const char *errmsg = zerror(zfile, &err); \
-      \
-      if (err == ERR_ERRNO) \
-        errmsg = strerror(errno); \
-      ohshit(_("%s: internal " format " error: %s: %s"), \
-             desc, "read", errmsg); \
-    } \
-    \
-    actualwrite = write(fd_out, buffer, actualread); \
-    if (actualwrite != actualread) \
-      ohshite(_("%s: internal " format " error: %s"), \
-              desc, "write"); \
-  } \
-  exit(0); \
-} while(0)
-
-#define COMPRESS(format, zFile, zdopen, zwrite, zclose, zerror, ERR_ERRNO, \
-                 fd_in, fd_out, compression, desc) do \
-{ \
-  char combuf[] = {'w', compression, '\0'}; \
-  int actualread, actualwrite; \
-  char buffer[4096]; \
-  zFile zfile; \
-  \
-  zfile = zdopen(fd_out, combuf); \
-  while ((actualread = read(fd_in, buffer, sizeof(buffer)))) { \
-    if (actualread < 0) \
-      ohshite(_("%s: internal " format " error: %s"), \
-              desc, "read"); \
-    \
-    actualwrite = zwrite(zfile, buffer, actualread); \
-    if (actualwrite != actualread) { \
-      int err = 0; \
-      const char *errmsg = zerror(zfile, &err); \
-      \
-      if (err == ERR_ERRNO) \
-        errmsg = strerror(errno); \
-      ohshit(_("%s: internal " format " error: %s: %s"), \
-              desc, "write", errmsg); \
-    } \
-  } \
-  zclose(zfile); \
-  exit(0); \
-} while(0)
-
-static void
-compress_cmd(int fd_in, int fd_out, const char *path,
-             const char *cmd, char compression,
-             const char *desc)
-{
-  fd_fd_filter(fd_in, fd_out, desc, path, cmd, "-c%c", compression);
-}
+#include <dpkg/varbuf.h>
+#include <dpkg/compression-backend.h>
 
 void decompress_cat(enum compress_type type, int fd_in, int fd_out, char *desc, ...) {
   va_list al;
@@ -121,25 +18,13 @@ void decompress_cat(enum compress_type type, int fd_in, int fd_out, char *desc,
 
   switch(type) {
     case compress_type_gzip:
-#ifdef WITH_ZLIB
-      DECOMPRESS("gzip", gzFile, gzdopen, gzread, gzerror, Z_ERRNO,
-                 fd_in, fd_out, v.buf);
-#else
-      fd_fd_filter(fd_in, fd_out, v.buf, GZIP, "gzip", "-dc");
-#endif
+      decompress_gzip(fd_in, fd_out, v.buf);
     case compress_type_bzip2:
-#ifdef WITH_BZ2
-      DECOMPRESS("bzip2", BZFILE *, BZ2_bzdopen, BZ2_bzread,
-                 BZ2_bzerror, BZ_IO_ERROR,
-                 fd_in, fd_out, v.buf);
-#else
-      fd_fd_filter(fd_in, fd_out, v.buf, BZIP2, "bzip2", "-dc");
-#endif
+      decompress_bzip2(fd_in, fd_out, v.buf);
     case compress_type_lzma:
-      fd_fd_filter(fd_in, fd_out, v.buf, LZMA, "lzma", "-dc");
+      decompress_lzma(fd_in, fd_out, v.buf);
     case compress_type_cat:
-      fd_fd_copy(fd_in, fd_out, -1, _("%s: decompression"), v.buf);
-      exit(0);
+      decompress_noop(fd_in, fd_out, v.buf);
     default:
       exit(1);
   }
@@ -159,25 +44,13 @@ void compress_cat(enum compress_type type, int fd_in, int fd_out, const char *co
 
   switch(type) {
     case compress_type_gzip:
-#ifdef WITH_ZLIB
-      COMPRESS("gzip", gzFile, gzdopen, gzwrite, gzclose, gzerror, Z_ERRNO,
-               fd_in, fd_out, *compression, v.buf);
-#else
-      compress_cmd(fd_in, fd_out, GZIP, "gzip", *compression, v.buf);
-#endif
+      compress_gzip(fd_in, fd_out, *compression, v.buf);
     case compress_type_bzip2:
-#ifdef WITH_BZ2
-      COMPRESS("bzip2", BZFILE *, BZ2_bzdopen, BZ2_bzwrite, BZ2_bzclose,
-               BZ2_bzerror, BZ_IO_ERROR,
-               fd_in, fd_out, *compression, v.buf);
-#else
-      compress_cmd(fd_in, fd_out, BZIP2, "bzip2", *compression, v.buf);
-#endif
+      compress_bzip2(fd_in, fd_out, *compression, v.buf);
     case compress_type_lzma:
-      compress_cmd(fd_in, fd_out, LZMA, "lzma", *compression, v.buf);
+      compress_lzma(fd_in, fd_out, *compression, v.buf);
     case compress_type_cat:
-      fd_fd_copy(fd_in, fd_out, -1, _("%s: compression"), v.buf);
-      exit(0);
+      compress_noop(fd_in, fd_out, v.buf);
     default:
       exit(1);
   }
-- 
1.6.5.rc1.199.g596ec


Reply to: