iostream-temp.c revision baf346e71ebd7b44fcba4b48f4d39845453b778b
70ee483d320a270993b56c713e350b736edd753fAki Tuomi/* Copyright (c) 2013 Dovecot authors, see the included COPYING file */
70ee483d320a270993b56c713e350b736edd753fAki Tuomistatic void o_stream_temp_close(struct iostream_private *stream)
70ee483d320a270993b56c713e350b736edd753fAki Tuomi struct temp_ostream *tstream = (struct temp_ostream *)stream;
70ee483d320a270993b56c713e350b736edd753fAki Tuomistatic int o_stream_temp_move_to_fd(struct temp_ostream *tstream)
70ee483d320a270993b56c713e350b736edd753fAki Tuomi tstream->fd = safe_mkstemp_hostpid(path, 0600, (uid_t)-1, (gid_t)-1);
70ee483d320a270993b56c713e350b736edd753fAki Tuomi i_error("safe_mkstemp(%s) failed: %m", str_c(path));
70ee483d320a270993b56c713e350b736edd753fAki Tuomi if (write_full(tstream->fd, tstream->buf->data, tstream->buf->used) < 0) {
70ee483d320a270993b56c713e350b736edd753fAki Tuomio_stream_temp_fd_sendv(struct temp_ostream *tstream,
70ee483d320a270993b56c713e350b736edd753fAki Tuomi const struct const_iovec *iov, unsigned int iov_count)
70ee483d320a270993b56c713e350b736edd753fAki Tuomi unsigned int i;
70ee483d320a270993b56c713e350b736edd753fAki Tuomi for (i = 0; i < iov_count; i++) {
70ee483d320a270993b56c713e350b736edd753fAki Tuomi if (write_full(tstream->fd, iov[i].iov_base, iov[i].iov_len) < 0) {
70ee483d320a270993b56c713e350b736edd753fAki Tuomi tstream->ostream.ostream.offset += iov[i].iov_len;
70ee483d320a270993b56c713e350b736edd753fAki Tuomio_stream_temp_sendv(struct ostream_private *stream,
70ee483d320a270993b56c713e350b736edd753fAki Tuomi const struct const_iovec *iov, unsigned int iov_count)
70ee483d320a270993b56c713e350b736edd753fAki Tuomi struct temp_ostream *tstream = (struct temp_ostream *)stream;
70ee483d320a270993b56c713e350b736edd753fAki Tuomi unsigned int i;
70ee483d320a270993b56c713e350b736edd753fAki Tuomi return o_stream_temp_fd_sendv(tstream, iov, iov_count);
70ee483d320a270993b56c713e350b736edd753fAki Tuomi for (i = 0; i < iov_count; i++) {
70ee483d320a270993b56c713e350b736edd753fAki Tuomi if (tstream->buf->used + iov[i].iov_len > IOSTREAM_TEMP_MAX_BUF_SIZE) {
70ee483d320a270993b56c713e350b736edd753fAki Tuomi /* failed to move to temp fd, just keep it in memory */
70ee483d320a270993b56c713e350b736edd753fAki Tuomi buffer_append(tstream->buf, iov[i].iov_base, iov[i].iov_len);
70ee483d320a270993b56c713e350b736edd753fAki Tuomistatic int o_stream_temp_dup_cancel(struct temp_ostream *tstream)
70ee483d320a270993b56c713e350b736edd753fAki Tuomi i_stream_seek(tstream->dupstream, tstream->dupstream_start_offset);
70ee483d320a270993b56c713e350b736edd753fAki Tuomi input = i_stream_create_limit(tstream->dupstream, size);
70ee483d320a270993b56c713e350b736edd753fAki Tuomi } while (input->v_offset < tstream->dupstream_offset && ret > 0);
70ee483d320a270993b56c713e350b736edd753fAki Tuomi if (ret < 0 && tstream->ostream.ostream.stream_errno == 0) {
70ee483d320a270993b56c713e350b736edd753fAki Tuomi tstream->ostream.ostream.stream_errno = input->stream_errno;
70ee483d320a270993b56c713e350b736edd753fAki Tuomistatic int o_stream_temp_dup_istream(struct temp_ostream *outstream,
70ee483d320a270993b56c713e350b736edd753fAki Tuomi if (!instream->readable_fd || i_stream_get_fd(instream) == -1)
a05819736f348d0c5ac8b4966ac6b04c21e1a391Timo Sirainen if (i_stream_get_size(instream, TRUE, &in_size) <= 0) {
34742a4b92c16c300e3d75731685b678712340b2Timo Sirainen outstream->dupstream_start_offset = instream->v_offset;
a05819736f348d0c5ac8b4966ac6b04c21e1a391Timo Sirainen outstream->dupstream_offset != instream->v_offset ||
70ee483d320a270993b56c713e350b736edd753fAki Tuomistatic off_t o_stream_temp_send_istream(struct ostream_private *_outstream,
70ee483d320a270993b56c713e350b736edd753fAki Tuomi struct temp_ostream *outstream = (struct temp_ostream *)_outstream;
70ee483d320a270993b56c713e350b736edd753fAki Tuomi if ((outstream->flags & IOSTREAM_TEMP_FLAG_TRY_FD_DUP) != 0) {
70ee483d320a270993b56c713e350b736edd753fAki Tuomi if ((ret = o_stream_temp_dup_istream(outstream, instream)) > 0)
70ee483d320a270993b56c713e350b736edd753fAki Tuomi outstream->flags &= ~IOSTREAM_TEMP_FLAG_TRY_FD_DUP;
70ee483d320a270993b56c713e350b736edd753fAki Tuomi return io_stream_copy(&outstream->ostream.ostream,
70ee483d320a270993b56c713e350b736edd753fAki Tuomistruct ostream *iostream_temp_create(const char *temp_path_prefix,
70ee483d320a270993b56c713e350b736edd753fAki Tuomi tstream->ostream.send_istream = o_stream_temp_send_istream;
70ee483d320a270993b56c713e350b736edd753fAki Tuomi tstream->ostream.iostream.close = o_stream_temp_close;
70ee483d320a270993b56c713e350b736edd753fAki Tuomi tstream->temp_path_prefix = i_strdup(temp_path_prefix);
70ee483d320a270993b56c713e350b736edd753fAki Tuomi tstream->buf = buffer_create_dynamic(default_pool, 8192);
70ee483d320a270993b56c713e350b736edd753fAki Tuomi output = o_stream_create(&tstream->ostream, NULL, -1);
70ee483d320a270993b56c713e350b736edd753fAki Tuomistatic void iostream_temp_buf_destroyed(buffer_t *buf)
70ee483d320a270993b56c713e350b736edd753fAki Tuomistruct istream *iostream_temp_finish(struct ostream **output,
70ee483d320a270993b56c713e350b736edd753fAki Tuomi abs_offset = tstream->dupstream->real_stream->abs_start_offset +
70ee483d320a270993b56c713e350b736edd753fAki Tuomi input2 = i_stream_create_fd(fd, max_buffer_size, TRUE);
70ee483d320a270993b56c713e350b736edd753fAki Tuomi input = i_stream_create_fd(tstream->fd, max_buffer_size, TRUE);
70ee483d320a270993b56c713e350b736edd753fAki Tuomi input = i_stream_create_from_data(tstream->buf->data,