summaryrefslogtreecommitdiffstats
path: root/src/lib/ostream-rawlog.c
blob: 8066392e3329579d2a37c4021072e6ddbb414a61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "iostream-rawlog-private.h"
#include "ostream-private.h"
#include "ostream-rawlog.h"

struct rawlog_ostream {
	struct ostream_private ostream;
	struct rawlog_iostream riostream;
};

static void o_stream_rawlog_close(struct iostream_private *stream,
				  bool close_parent)
{
	struct rawlog_ostream *rstream =
		container_of(stream, struct rawlog_ostream, ostream.iostream);

	iostream_rawlog_close(&rstream->riostream);
	if (close_parent)
		o_stream_close(rstream->ostream.parent);
}

static ssize_t
o_stream_rawlog_sendv(struct ostream_private *stream,
		      const struct const_iovec *iov, unsigned int iov_count)
{
	struct rawlog_ostream *rstream =
		container_of(stream, struct rawlog_ostream, ostream);
	unsigned int i;
	ssize_t ret, bytes;

	if ((ret = o_stream_sendv(stream->parent, iov, iov_count)) < 0) {
		o_stream_copy_error_from_parent(stream);
		return -1;
	}
	bytes = ret;
	for (i = 0; i < iov_count && bytes > 0; i++) {
		if (iov[i].iov_len < (size_t)bytes) {
			iostream_rawlog_write(&rstream->riostream,
					      iov[i].iov_base, iov[i].iov_len);
			bytes -= iov[i].iov_len;
		} else {
			iostream_rawlog_write(&rstream->riostream,
					      iov[i].iov_base, bytes);
			break;
		}
	}

	stream->ostream.offset += ret;
	return ret;
}

struct ostream *
o_stream_create_rawlog(struct ostream *output, const char *rawlog_path,
		       int rawlog_fd, enum iostream_rawlog_flags flags)
{
	struct ostream *rawlog_output;
	bool autoclose_fd = (flags & IOSTREAM_RAWLOG_FLAG_AUTOCLOSE) != 0;

	i_assert(rawlog_path != NULL);
	i_assert(rawlog_fd != -1);

	rawlog_output = autoclose_fd ?
		o_stream_create_fd_autoclose(&rawlog_fd, 0):
		o_stream_create_fd(rawlog_fd, 0);

	o_stream_set_name(rawlog_output,
			  t_strdup_printf("rawlog(%s)", rawlog_path));
	return o_stream_create_rawlog_from_stream(output, rawlog_output, flags);
}

struct ostream *
o_stream_create_rawlog_from_stream(struct ostream *output,
				   struct ostream *rawlog_output,
				   enum iostream_rawlog_flags flags)
{
	struct rawlog_ostream *rstream;

	rstream = i_new(struct rawlog_ostream, 1);
	rstream->ostream.sendv = o_stream_rawlog_sendv;
	rstream->ostream.iostream.close = o_stream_rawlog_close;

	rstream->riostream.rawlog_output = rawlog_output;
	iostream_rawlog_init(&rstream->riostream, flags, FALSE);
	return o_stream_create(&rstream->ostream, output,
			       o_stream_get_fd(output));
}