dovecot-2.2: Moved doveadm zlib commands from zlib plugin to dov...

dovecot at dovecot.org dovecot at dovecot.org
Sat Oct 13 00:41:16 EEST 2012


details:   http://hg.dovecot.org/dovecot-2.2/rev/cea1d5504a84
changeset: 15216:cea1d5504a84
user:      Timo Sirainen <tss at iki.fi>
date:      Sat Oct 13 00:40:41 2012 +0300
description:
Moved doveadm zlib commands from zlib plugin to doveadm directly.
Previously it was a plugin, because the istream-zlib existed only in zlib
plugin. Now there's a lib-compression that implements it.

diffstat:

 src/doveadm/Makefile.am         |    7 +-
 src/doveadm/doveadm-dump.c      |    3 +-
 src/doveadm/doveadm-dump.h      |    1 +
 src/doveadm/doveadm-zlib.c      |  190 +++++++++++++++++++++++++++++++++++++
 src/doveadm/doveadm.c           |    3 +-
 src/doveadm/doveadm.h           |    1 +
 src/plugins/zlib/Makefile.am    |   12 +--
 src/plugins/zlib/doveadm-zlib.c |  205 ----------------------------------------
 8 files changed, 202 insertions(+), 220 deletions(-)

diffs (truncated from 514 to 300 lines):

diff -r cafdd35ac437 -r cea1d5504a84 src/doveadm/Makefile.am
--- a/src/doveadm/Makefile.am	Sat Oct 13 00:32:34 2012 +0300
+++ b/src/doveadm/Makefile.am	Sat Oct 13 00:40:41 2012 +0300
@@ -10,6 +10,7 @@
 	-I$(top_srcdir)/src/lib \
 	-I$(top_srcdir)/src/lib-settings \
 	-I$(top_srcdir)/src/lib-auth \
+	-I$(top_srcdir)/src/lib-compression \
 	-I$(top_srcdir)/src/lib-dict \
 	-I$(top_srcdir)/src/lib-fs \
 	-I$(top_srcdir)/src/lib-master \
@@ -32,7 +33,8 @@
 	../lib-otp/libotp.a
 
 libs = \
-	dsync/libdsync.la
+	dsync/libdsync.la \
+	../lib-compression/libcompression.la
 
 doveadm_LDADD = \
 	$(libs) \
@@ -102,7 +104,8 @@
 	doveadm-pw.c \
 	doveadm-sis.c \
 	doveadm-stats.c \
-	doveadm-who.c
+	doveadm-who.c \
+	doveadm-zlib.c
 
 doveadm_server_SOURCES = \
 	$(common) \
diff -r cafdd35ac437 -r cea1d5504a84 src/doveadm/doveadm-dump.c
--- a/src/doveadm/doveadm-dump.c	Sat Oct 13 00:32:34 2012 +0300
+++ b/src/doveadm/doveadm-dump.c	Sat Oct 13 00:40:41 2012 +0300
@@ -86,7 +86,8 @@
 	&doveadm_cmd_dump_index,
 	&doveadm_cmd_dump_log,
 	&doveadm_cmd_dump_mailboxlog,
-	&doveadm_cmd_dump_thread
+	&doveadm_cmd_dump_thread,
+	&doveadm_cmd_dump_zlib
 };
 
 void print_dump_types(void)
diff -r cafdd35ac437 -r cea1d5504a84 src/doveadm/doveadm-dump.h
--- a/src/doveadm/doveadm-dump.h	Sat Oct 13 00:32:34 2012 +0300
+++ b/src/doveadm/doveadm-dump.h	Sat Oct 13 00:40:41 2012 +0300
@@ -14,6 +14,7 @@
 extern struct doveadm_cmd_dump doveadm_cmd_dump_log;
 extern struct doveadm_cmd_dump doveadm_cmd_dump_mailboxlog;
 extern struct doveadm_cmd_dump doveadm_cmd_dump_thread;
+extern struct doveadm_cmd_dump doveadm_cmd_dump_zlib;
 
 void doveadm_dump_register(const struct doveadm_cmd_dump *dump);
 
diff -r cafdd35ac437 -r cea1d5504a84 src/doveadm/doveadm-zlib.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/doveadm/doveadm-zlib.c	Sat Oct 13 00:40:41 2012 +0300
@@ -0,0 +1,190 @@
+/* Copyright (c) 2010-2012 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "net.h"
+#include "istream.h"
+#include "ostream.h"
+#include "istream-zlib.h"
+#include "ostream-zlib.h"
+#include "module-dir.h"
+#include "master-service.h"
+#include "doveadm-dump.h"
+
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+const char *doveadm_zlib_plugin_version = DOVECOT_ABI_VERSION;
+
+static void cmd_dump_imapzlib(int argc ATTR_UNUSED, char *argv[])
+{
+	struct istream *input, *input2;
+	const unsigned char *data;
+	size_t size;
+	const char *line;
+	int fd;
+
+	fd = open(argv[1], O_RDONLY);
+	if (fd < 0)
+		i_fatal("open(%s) failed: %m", argv[1]);
+	input = i_stream_create_fd(fd, 1024*32, TRUE);
+	while ((line = i_stream_read_next_line(input)) != NULL) {
+		/* skip tag */
+		printf("%s\r\n", line);
+		while (*line != ' ' && *line != '\0') line++;
+		if (*line == '\0')
+			continue;
+		line++;
+
+		if (strcmp(line, "OK Begin compression.") == 0 ||
+		    strcasecmp(line, "COMPRESS DEFLATE") == 0)
+			break;
+	}
+
+	input2 = i_stream_create_deflate(input, TRUE);
+	i_stream_unref(&input);
+
+	while (i_stream_read_data(input2, &data, &size, 0) != -1) {
+		fwrite(data, 1, size, stdout);
+		i_stream_skip(input2, size);
+	}
+	i_stream_unref(&input2);
+	fflush(stdout);
+}
+
+static bool test_dump_imapzlib(const char *path)
+{
+	const char *p;
+	char buf[4096];
+	int fd, ret;
+	bool match = FALSE;
+
+	p = strrchr(path, '.');
+	if (p == NULL || (strcmp(p, ".in") != 0 && strcmp(p, ".out") != 0))
+		return FALSE;
+
+	fd = open(path, O_RDONLY);
+	if (fd == -1)
+		return FALSE;
+
+	ret = read(fd, buf, sizeof(buf)-1);
+	if (ret > 0) {
+		buf[ret] = '\0';
+		(void)str_lcase(buf);
+		match = strstr(buf, " ok begin compression.") != NULL ||
+			strstr(buf, " compress deflate") != NULL;
+	}
+	i_close_fd(&fd);
+	return match;
+}
+
+struct client {
+	int fd;
+	struct io *io_client, *io_server;
+	struct istream *input;
+	struct ostream *output;
+	bool compressed;
+};
+
+static void client_input(struct client *client)
+{
+	struct istream *input;
+	struct ostream *output;
+	unsigned char buf[1024];
+	ssize_t ret;
+
+	ret = read(STDIN_FILENO, buf, sizeof(buf));
+	if (ret == 0) {
+		if (client->compressed) {
+			master_service_stop(master_service);
+			return;
+		}
+		/* start compression */
+		i_info("<Compression started>");
+		input = i_stream_create_deflate(client->input, TRUE);
+		output = o_stream_create_deflate(client->output, 6);
+		i_stream_unref(&client->input);
+		o_stream_unref(&client->output);
+		client->input = input;
+		client->output = output;
+		client->compressed = TRUE;
+		return;
+	}
+	if (ret < 0)
+		i_fatal("read(stdin) failed: %m");
+
+	o_stream_nsend(client->output, buf, ret);
+}
+
+static void server_input(struct client *client)
+{
+	const unsigned char *data;
+	size_t size;
+
+	if (i_stream_read(client->input) == -1) {
+		if (client->input->stream_errno != 0) {
+			errno = client->input->stream_errno;
+			i_fatal("read(server) failed: %m");
+		}
+
+		i_info("Server disconnected");
+		master_service_stop(master_service);
+		return;
+	}
+
+	data = i_stream_get_data(client->input, &size);
+	if (write(STDOUT_FILENO, data, size) < 0)
+		i_fatal("write(stdout) failed: %m");
+	i_stream_skip(client->input, size);
+}
+
+static void cmd_zlibconnect(int argc ATTR_UNUSED, char *argv[])
+{
+	struct client client;
+	struct ip_addr *ips;
+	unsigned int ips_count, port = 143;
+	int fd, ret;
+
+	if (argv[1] == NULL ||
+	    (argv[2] != NULL && str_to_uint(argv[2], &port) < 0))
+		help(&doveadm_cmd_zlibconnect);
+
+	ret = net_gethostbyname(argv[1], &ips, &ips_count);
+	if (ret != 0) {
+		i_fatal("Host %s lookup failed: %s", argv[1],
+			net_gethosterror(ret));
+	}
+
+	if ((fd = net_connect_ip(&ips[0], port, NULL)) == -1)
+		i_fatal("connect(%s, %u) failed: %m", argv[1], port);
+
+	i_info("Connected to %s port %u. Ctrl-D starts compression",
+	       net_ip2addr(&ips[0]), port);
+
+	memset(&client, 0, sizeof(client));
+	client.fd = fd;
+	client.input = i_stream_create_fd(fd, (size_t)-1, FALSE);
+	client.output = o_stream_create_fd(fd, 0, FALSE);
+	o_stream_set_no_error_handling(client.output, TRUE);
+	client.io_client = io_add(STDIN_FILENO, IO_READ, client_input, &client);
+	client.io_server = io_add(fd, IO_READ, server_input, &client);
+	master_service_run(master_service, NULL);
+	io_remove(&client.io_client);
+	io_remove(&client.io_server);
+	i_stream_unref(&client.input);
+	o_stream_unref(&client.output);
+	if (close(fd) < 0)
+		i_fatal("close() failed: %m");
+}
+
+struct doveadm_cmd_dump doveadm_cmd_dump_zlib = {
+	"imapzlib",
+	test_dump_imapzlib,
+	cmd_dump_imapzlib
+};
+
+struct doveadm_cmd doveadm_cmd_zlibconnect = {
+	cmd_zlibconnect,
+	"zlibconnect",
+	"<host> [<port>]"
+};
diff -r cafdd35ac437 -r cea1d5504a84 src/doveadm/doveadm.c
--- a/src/doveadm/doveadm.c	Sat Oct 13 00:32:34 2012 +0300
+++ b/src/doveadm/doveadm.c	Sat Oct 13 00:40:41 2012 +0300
@@ -300,7 +300,8 @@
 	&doveadm_cmd_sis_deduplicate,
 	&doveadm_cmd_sis_find,
 	&doveadm_cmd_stats_dump,
-	&doveadm_cmd_stats_top
+	&doveadm_cmd_stats_top,
+	&doveadm_cmd_zlibconnect
 };
 
 int main(int argc, char *argv[])
diff -r cafdd35ac437 -r cea1d5504a84 src/doveadm/doveadm.h
--- a/src/doveadm/doveadm.h	Sat Oct 13 00:32:34 2012 +0300
+++ b/src/doveadm/doveadm.h	Sat Oct 13 00:40:41 2012 +0300
@@ -32,6 +32,7 @@
 extern struct doveadm_cmd doveadm_cmd_sis_find;
 extern struct doveadm_cmd doveadm_cmd_stats_dump;
 extern struct doveadm_cmd doveadm_cmd_stats_top;
+extern struct doveadm_cmd doveadm_cmd_zlibconnect;
 
 void doveadm_register_cmd(const struct doveadm_cmd *cmd);
 
diff -r cafdd35ac437 -r cea1d5504a84 src/plugins/zlib/Makefile.am
--- a/src/plugins/zlib/Makefile.am	Sat Oct 13 00:32:34 2012 +0300
+++ b/src/plugins/zlib/Makefile.am	Sat Oct 13 00:40:41 2012 +0300
@@ -1,5 +1,3 @@
-doveadm_moduledir = $(moduledir)/doveadm
-
 AM_CPPFLAGS = \
 	-I$(top_srcdir)/src/lib \
 	-I$(top_srcdir)/src/lib-master \
@@ -8,11 +6,9 @@
 	-I$(top_srcdir)/src/lib-index \
 	-I$(top_srcdir)/src/lib-storage \
 	-I$(top_srcdir)/src/lib-storage/index \
-	-I$(top_srcdir)/src/lib-storage/index/dbox-common \
-	-I$(top_srcdir)/src/doveadm
+	-I$(top_srcdir)/src/lib-storage/index/dbox-common
 
 NOPLUGIN_LDFLAGS =
-lib10_doveadm_zlib_plugin_la_LDFLAGS = -module -avoid-version
 lib20_zlib_plugin_la_LDFLAGS = -module -avoid-version
 
 module_LTLIBRARIES = \
@@ -26,9 +22,3 @@
 
 noinst_HEADERS = \
 	zlib-plugin.h
-


More information about the dovecot-cvs mailing list