bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen#define IOSTREAM_TEMP_MAX_BUF_SIZE_DEFAULT (1024*128)
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen uoff_t dupstream_offset, dupstream_start_offset;
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainenstatic bool o_stream_temp_dup_cancel(struct temp_ostream *tstream,
e2ce8d4a6ac5d82a906178148453e7613fab9ba0Timo Siraineno_stream_temp_close(struct iostream_private *stream,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen struct temp_ostream *tstream = (struct temp_ostream *)stream;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenstatic int o_stream_temp_move_to_fd(struct temp_ostream *tstream)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen tstream->fd = safe_mkstemp_hostpid(path, 0600, (uid_t)-1, (gid_t)-1);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen i_error("safe_mkstemp(%s) failed: %m", str_c(path));
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen if (write_full(tstream->fd, tstream->buf->data, tstream->buf->used) < 0) {
e245fb1302121d2bc2580f61e040c2c8a558ee9eTimo Sirainen /* make the fd available also to o_stream_get_fd(),
e245fb1302121d2bc2580f61e040c2c8a558ee9eTimo Sirainen e.g. for unit tests */
d97f081a3de44852197ced772e21560c108895a6Timo Sirainenint o_stream_temp_move_to_memory(struct ostream *output)
d97f081a3de44852197ced772e21560c108895a6Timo Sirainen tstream->buf = buffer_create_dynamic(default_pool, 8192);
d97f081a3de44852197ced772e21560c108895a6Timo Sirainen while (offset < tstream->ostream.ostream.offset &&
d97f081a3de44852197ced772e21560c108895a6Timo Sirainen (ret = pread(tstream->fd, buf, sizeof(buf), offset)) > 0) {
d97f081a3de44852197ced772e21560c108895a6Timo Sirainen if ((size_t)ret > tstream->ostream.ostream.offset - offset)
d97f081a3de44852197ced772e21560c108895a6Timo Sirainen ret = tstream->ostream.ostream.offset - offset;
d97f081a3de44852197ced772e21560c108895a6Timo Sirainen /* not really expecting this to happen */
d97f081a3de44852197ced772e21560c108895a6Timo Sirainen i_error("iostream-temp %s: read(%s*) failed: %m",
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Siraineno_stream_temp_fd_sendv(struct temp_ostream *tstream,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen const struct const_iovec *iov, unsigned int iov_count)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen unsigned int i;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen for (i = 0; i < iov_count; i++) {
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen if (write_full(tstream->fd, iov[i].iov_base, iov[i].iov_len) < 0) {
d97f081a3de44852197ced772e21560c108895a6Timo Sirainen i_error("iostream-temp %s: write(%s*) failed: %m - moving to memory",
d97f081a3de44852197ced772e21560c108895a6Timo Sirainen if (o_stream_temp_move_to_memory(&tstream->ostream.ostream) < 0)
d97f081a3de44852197ced772e21560c108895a6Timo Sirainen for (; i < iov_count; i++) {
d97f081a3de44852197ced772e21560c108895a6Timo Sirainen buffer_append(tstream->buf, iov[i].iov_base, iov[i].iov_len);
d97f081a3de44852197ced772e21560c108895a6Timo Sirainen tstream->ostream.ostream.offset += iov[i].iov_len;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen tstream->ostream.ostream.offset += iov[i].iov_len;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Siraineno_stream_temp_sendv(struct ostream_private *stream,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen const struct const_iovec *iov, unsigned int iov_count)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen struct temp_ostream *tstream = (struct temp_ostream *)stream;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen unsigned int i;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen tstream->flags &= ~IOSTREAM_TEMP_FLAG_TRY_FD_DUP;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen return o_stream_temp_fd_sendv(tstream, iov, iov_count);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen for (i = 0; i < iov_count; i++) {
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen if (tstream->buf->used + iov[i].iov_len > tstream->max_mem_size) {
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* failed to move to temp fd, just keep it in memory */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen buffer_append(tstream->buf, iov[i].iov_base, iov[i].iov_len);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainenstatic bool o_stream_temp_dup_cancel(struct temp_ostream *tstream,
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen bool ret = TRUE; /* use res_r to return error */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen i_stream_seek(tstream->dupstream, tstream->dupstream_start_offset);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen input = i_stream_create_limit(tstream->dupstream, size);
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen *res_r = io_stream_copy(&tstream->ostream.ostream, input);
6adf683655750bcb809275cd65dc75fd12214198Timo Sirainen /* everything copied */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen tstream->ostream.ostream.stream_errno = input->stream_errno;
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen io_stream_set_error(&tstream->ostream.iostream,
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen "iostream-temp: read(%s) failed: %s",
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen case OSTREAM_SEND_ISTREAM_RESULT_ERROR_OUTPUT:
378e6cb162b355d6f103526505bc00b9a78962e7Timo Siraineno_stream_temp_dup_istream(struct temp_ostream *outstream,
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen if (!instream->readable_fd || i_stream_get_fd(instream) == -1)
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen if (i_stream_get_size(instream, TRUE, &in_size) <= 0) {
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen return o_stream_temp_dup_cancel(outstream, res_r);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen outstream->dupstream_start_offset = instream->v_offset;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen outstream->dupstream_offset != instream->v_offset ||
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen return o_stream_temp_dup_cancel(outstream, res_r);
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen /* we should be at EOF now. o_stream_send_istream() asserts if
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen eof isn't set. */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen outstream->dupstream_offset = instream->v_offset;
c076ad69e28e7d41af83ada84e12019793ffcfa2Timo Sirainen outstream->dupstream_offset - outstream->dupstream_start_offset;
378e6cb162b355d6f103526505bc00b9a78962e7Timo Sirainen *res_r = OSTREAM_SEND_ISTREAM_RESULT_FINISHED;
378e6cb162b355d6f103526505bc00b9a78962e7Timo Siraineno_stream_temp_send_istream(struct ostream_private *_outstream,
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen struct temp_ostream *outstream = (struct temp_ostream *)_outstream;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen if ((outstream->flags & IOSTREAM_TEMP_FLAG_TRY_FD_DUP) != 0) {
e20f5920759215f328ce12dcca071b4e7dda3d48Timo Sirainen if (o_stream_temp_dup_istream(outstream, instream, &res))
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen outstream->flags &= ~IOSTREAM_TEMP_FLAG_TRY_FD_DUP;
46631c1d903c409444b1b1c4a1d41a033c09ee37Timo Sirainen return io_stream_copy(&outstream->ostream.ostream, instream);
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Siraineno_stream_temp_write_at(struct ostream_private *stream,
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen struct temp_ostream *tstream = (struct temp_ostream *)stream;
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen i_assert(stream->ostream.offset == tstream->buf->used);
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen buffer_write(tstream->buf, offset, data, size);
d5e839aea288aceaddae28a1578cebda3c9e3b58Timo Sirainen if (pwrite_full(tstream->fd, data, size, offset) < 0) {
153ed0fbca1f5f944b70937dfd71911db172ca97Timo Sirainenstatic int o_stream_temp_seek(struct ostream_private *_stream, uoff_t offset)
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenstruct ostream *iostream_temp_create(const char *temp_path_prefix,
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen return iostream_temp_create_named(temp_path_prefix, flags, "");
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainenstruct ostream *iostream_temp_create_named(const char *temp_path_prefix,
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainen return iostream_temp_create_sized(temp_path_prefix, flags, name,
8a8a3b43987b5ade914f22765e51c9e3de8179d3Timo Sirainenstruct ostream *iostream_temp_create_sized(const char *temp_path_prefix,
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen tstream->ostream.send_istream = o_stream_temp_send_istream;
fb176cdc122707cda985ab3c09c02ccf3cec0af1Timo Sirainen tstream->ostream.write_at = o_stream_temp_write_at;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen tstream->ostream.iostream.close = o_stream_temp_close;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen tstream->temp_path_prefix = i_strdup(temp_path_prefix);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen tstream->buf = buffer_create_dynamic(default_pool, 8192);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen output = o_stream_create(&tstream->ostream, NULL, -1);
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen "(temp iostream in %s for %s)", temp_path_prefix, name));
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenstatic void iostream_temp_buf_destroyed(buffer_t *buf)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenstruct istream *iostream_temp_finish(struct ostream **output,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen (struct temp_ostream *)(*output)->real_stream;
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen for_path = t_strdup_printf(" for %s", tstream->name);
e44028b5df7045dd9e7f324175e73e3ff490cb5dTimo Sirainen if (tstream->dupstream != NULL && !tstream->dupstream->closed) {
d28179fd78550a58be44dcb1e3e830ab7d33172dTimo Sirainen abs_offset = i_stream_get_absolute_offset(tstream->dupstream) -
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen fd = dup(i_stream_get_fd(tstream->dupstream));
b3f4c31f1533e25380f49f77d5bb1251bf43db2aTimo Sirainen input = i_stream_create_error_str(errno, "dup() failed: %m");
bace943c67e6cd14ce6c994f533d82a3caad5bf1Timo Sirainen input2 = i_stream_create_fd_autoclose(&fd, max_buffer_size);
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen "(Temp file in %s%s, from %s)", tstream->temp_path_prefix,
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen for_path, i_stream_get_name(tstream->dupstream)));
e44028b5df7045dd9e7f324175e73e3ff490cb5dTimo Sirainen /* return the original failed stream. */
bace943c67e6cd14ce6c994f533d82a3caad5bf1Timo Sirainen input = i_stream_create_fd_autoclose(&tstream->fd, max_buffer_size);
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen "(Temp file fd %d in %s%s, %"PRIuUOFF_T" bytes)",
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen fd, tstream->temp_path_prefix, for_path, tstream->fd_size));
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen input = i_stream_create_from_data(tstream->buf->data,
2f4f603d4cebab2cc956c72164efb02da83515c5Timo Sirainen tstream->temp_path_prefix, for_path, tstream->buf->used));