From: Stephan Bosch Date: Sun, 25 Feb 2018 08:55:00 +0000 (+0100) Subject: lib-program-client: Use iostream-temp rather than istream-seekable for seekable output. X-Git-Tag: 2.3.9~2088 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8760bc069233fd7dfda797fcd36c27c7f797b146;p=thirdparty%2Fdovecot%2Fcore.git lib-program-client: Use iostream-temp rather than istream-seekable for seekable output. This considerably simplifies the code. --- diff --git a/src/lib-program-client/program-client-local.c b/src/lib-program-client/program-client-local.c index 1570e39145..f82bd46849 100644 --- a/src/lib-program-client/program-client-local.c +++ b/src/lib-program-client/program-client-local.c @@ -159,7 +159,7 @@ program_client_local_connect(struct program_client *pclient) return -1; } } - if (pclient->output != NULL || pclient->output_seekable) { + if (pclient->output != NULL) { if (pipe(fd_out) < 0) { i_error("pipe(out) failed: %m"); return -1; diff --git a/src/lib-program-client/program-client-private.h b/src/lib-program-client/program-client-private.h index 9201a4e027..cf4f4d14c9 100644 --- a/src/lib-program-client/program-client-private.h +++ b/src/lib-program-client/program-client-private.h @@ -41,10 +41,8 @@ struct program_client { struct timeout *to; struct timeval start_time; - struct istream *input, *program_input, *seekable_output; - struct istream *dot_input; + struct istream *input, *program_input, *dot_input; struct ostream *output, *program_output, *dot_output; - char *temp_prefix; ARRAY(struct program_client_extra_fd) extra_fds; diff --git a/src/lib-program-client/program-client-remote.c b/src/lib-program-client/program-client-remote.c index 6e5a1d9ee0..63bf8616e1 100644 --- a/src/lib-program-client/program-client-remote.c +++ b/src/lib-program-client/program-client-remote.c @@ -314,8 +314,8 @@ program_client_unix_connect(struct program_client *pclient) } } - pclient->fd_in = (prclient->noreply && pclient->output == NULL && - !pclient->output_seekable ? -1 : fd); + pclient->fd_in = (prclient->noreply && pclient->output == NULL ? + -1 : fd); pclient->fd_out = fd; pclient->io = io_add(fd, IO_WRITE, program_client_remote_connected, prclient); @@ -397,8 +397,8 @@ program_client_net_connect_real(struct program_client_remote *prclient) return; } - pclient->fd_in = (prclient->noreply && pclient->output == NULL && - !pclient->output_seekable ? -1 : fd); + pclient->fd_in = (prclient->noreply && pclient->output == NULL ? + -1 : fd); pclient->fd_out = fd; pclient->io = io_add(fd, IO_WRITE, program_client_net_connected, prclient); diff --git a/src/lib-program-client/program-client.c b/src/lib-program-client/program-client.c index c3d1b3d079..c29473f4e8 100644 --- a/src/lib-program-client/program-client.c +++ b/src/lib-program-client/program-client.c @@ -6,10 +6,10 @@ #include "str.h" #include "safe-mkstemp.h" #include "istream-private.h" -#include "istream-seekable.h" #include "ostream-dot.h" #include "istream-dot.h" #include "ostream.h" +#include "iostream-temp.h" #include "lib-signals.h" #include "program-client-private.h" @@ -31,32 +31,6 @@ program_client_callback(struct program_client *pclient, int result, callback(result, context); } -static int -program_client_seekable_fd_callback(const char **path_r, void *context) -{ - struct program_client *pclient = (struct program_client *)context; - string_t *path; - int fd; - - path = t_str_new(128); - str_append(path, pclient->temp_prefix); - fd = safe_mkstemp(path, 0600, (uid_t)-1, (gid_t)-1); - if (fd == -1) { - i_error("safe_mkstemp(%s) failed: %m", str_c(path)); - return -1; - } - - /* we just want the fd, unlink it */ - if (i_unlink(str_c(path)) < 0) { - /* shouldn't happen.. */ - i_close_fd(&fd); - return -1; - } - - *path_r = str_c(path); - return fd; -} - static void program_client_timeout(struct program_client *pclient) { @@ -119,8 +93,7 @@ program_client_disconnect_extra_fds(struct program_client *pclient) void program_client_disconnected(struct program_client *pclient) { if (pclient->program_input != NULL) { - if (pclient->output_seekable || - pclient->set.use_dotstream) + if (pclient->set.use_dotstream) i_stream_unref(&pclient->program_input); else i_stream_destroy(&pclient->program_input); @@ -300,20 +273,6 @@ void program_client_program_input(struct program_client *pclient) size_t size; int ret = 0; - /* initialize seekable output if required */ - if (pclient->output_seekable && pclient->seekable_output == NULL) { - struct istream *input_list[2] = { input, NULL }; - - input = i_stream_create_seekable(input_list, - MAX_OUTPUT_MEMORY_BUFFER, - program_client_seekable_fd_callback, pclient); - i_stream_unref(&pclient->program_input); - pclient->program_input = input; - - pclient->seekable_output = input; - i_stream_ref(pclient->seekable_output); - } - if (input != NULL) { /* initialize dot stream if required */ if (!pclient->input_dot_created && @@ -461,26 +420,23 @@ void program_client_set_output(struct program_client *pclient, o_stream_ref(output); pclient->output = output; pclient->output_seekable = FALSE; - i_free(pclient->temp_prefix); } void program_client_set_output_seekable(struct program_client *pclient, const char *temp_prefix) { o_stream_unref(&pclient->output); - pclient->temp_prefix = i_strdup(temp_prefix); + pclient->output = iostream_temp_create_sized(temp_prefix, 0, + "(program client seekable output)", + MAX_OUTPUT_MEMORY_BUFFER); pclient->output_seekable = TRUE; } struct istream * program_client_get_output_seekable(struct program_client *pclient) { - struct istream *input = pclient->seekable_output; - - pclient->seekable_output = NULL; - - i_stream_seek(input, 0); - return input; + i_assert(pclient->output_seekable); + return iostream_temp_finish(&pclient->output, IO_BLOCK_SIZE); } #undef program_client_set_extra_fd @@ -582,10 +538,8 @@ void program_client_destroy(struct program_client **_pclient) i_stream_unref(&pclient->program_input); o_stream_unref(&pclient->program_output); o_stream_unref(&pclient->output); - i_stream_unref(&pclient->seekable_output); io_remove(&pclient->io); - i_free(pclient->temp_prefix); if (pclient->destroy != NULL) pclient->destroy(pclient); @@ -599,8 +553,6 @@ void program_client_switch_ioloop(struct program_client *pclient) i_stream_switch_ioloop(pclient->input); if (pclient->program_input != NULL) i_stream_switch_ioloop(pclient->program_input); - if (pclient->seekable_output != NULL) - i_stream_switch_ioloop(pclient->seekable_output); if (pclient->output != NULL) o_stream_switch_ioloop(pclient->output); if (pclient->program_output != NULL)