Mercurial > dovecot > core-2.2
annotate src/lib/ostream-rawlog.c @ 23007:36e01285b5b8
lib: buffer - Improve header comment for buffer_insert() and buffer_delete().
author | Stephan Bosch <stephan.bosch@dovecot.fi> |
---|---|
date | Mon, 18 Mar 2019 00:52:37 +0100 |
parents | cb108f786fb4 |
children |
rev | line source |
---|---|
22713
cb108f786fb4
Updated copyright notices to include the year 2018.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents:
21390
diff
changeset
|
1 /* Copyright (c) 2011-2018 Dovecot authors, see the included COPYING file */ |
13534 | 2 |
3 #include "lib.h" | |
4 #include "iostream-rawlog-private.h" | |
5 #include "ostream-private.h" | |
6 #include "ostream-rawlog.h" | |
7 | |
8 struct rawlog_ostream { | |
9 struct ostream_private ostream; | |
10 struct rawlog_iostream riostream; | |
11 }; | |
12 | |
16020
6cabb95d32ec
iostreams: Added close_parent flag to close() handler and clarified close/destroy APIs.
Timo Sirainen <tss@iki.fi>
parents:
16006
diff
changeset
|
13 static void o_stream_rawlog_close(struct iostream_private *stream, |
6cabb95d32ec
iostreams: Added close_parent flag to close() handler and clarified close/destroy APIs.
Timo Sirainen <tss@iki.fi>
parents:
16006
diff
changeset
|
14 bool close_parent) |
13534 | 15 { |
16 struct rawlog_ostream *rstream = (struct rawlog_ostream *)stream; | |
17 | |
14681
ca37d1577291
Added o_stream_nsend*() and related functions to make delayed error handling safer.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
18 (void)o_stream_flush(rstream->ostream.parent); |
13534 | 19 iostream_rawlog_close(&rstream->riostream); |
16006
01cfcf2b1c3c
iostream-rawlog: Pass through close() to parent input/output stream.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
20 |
16020
6cabb95d32ec
iostreams: Added close_parent flag to close() handler and clarified close/destroy APIs.
Timo Sirainen <tss@iki.fi>
parents:
16006
diff
changeset
|
21 if (close_parent) |
6cabb95d32ec
iostreams: Added close_parent flag to close() handler and clarified close/destroy APIs.
Timo Sirainen <tss@iki.fi>
parents:
16006
diff
changeset
|
22 o_stream_close(rstream->ostream.parent); |
13534 | 23 } |
24 | |
25 static ssize_t | |
26 o_stream_rawlog_sendv(struct ostream_private *stream, | |
27 const struct const_iovec *iov, unsigned int iov_count) | |
28 { | |
29 struct rawlog_ostream *rstream = (struct rawlog_ostream *)stream; | |
30 unsigned int i; | |
15424
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
31 ssize_t ret, bytes; |
13534 | 32 |
33 if ((ret = o_stream_sendv(stream->parent, iov, iov_count)) < 0) { | |
34 o_stream_copy_error_from_parent(stream); | |
35 return -1; | |
36 } | |
15424
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
37 bytes = ret; |
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
38 for (i = 0; i < iov_count && bytes > 0; i++) { |
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
39 if (iov[i].iov_len < (size_t)bytes) { |
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
40 iostream_rawlog_write(&rstream->riostream, |
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
41 iov[i].iov_base, iov[i].iov_len); |
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
42 bytes -= iov[i].iov_len; |
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
43 } else { |
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
44 iostream_rawlog_write(&rstream->riostream, |
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
45 iov[i].iov_base, bytes); |
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
46 break; |
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
47 } |
b10fbe79f619
ostream-rawlog: Make sure rawlog logs only the data that got actually sent.
Timo Sirainen <tss@iki.fi>
parents:
14967
diff
changeset
|
48 } |
13534 | 49 |
50 stream->ostream.offset += ret; | |
51 return ret; | |
52 } | |
53 | |
54 struct ostream * | |
55 o_stream_create_rawlog(struct ostream *output, const char *rawlog_path, | |
14967
8ce6d41d2d41
iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents:
14851
diff
changeset
|
56 int rawlog_fd, enum iostream_rawlog_flags flags) |
13534 | 57 { |
18166
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
58 struct ostream *rawlog_output; |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
59 bool autoclose_fd = (flags & IOSTREAM_RAWLOG_FLAG_AUTOCLOSE) != 0; |
13534 | 60 |
61 i_assert(rawlog_path != NULL); | |
62 i_assert(rawlog_fd != -1); | |
63 | |
18166
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
64 rawlog_output = o_stream_create_fd(rawlog_fd, 0, autoclose_fd); |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
65 o_stream_set_name(rawlog_output, |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
66 t_strdup_printf("rawlog(%s)", rawlog_path)); |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
67 return o_stream_create_rawlog_from_stream(output, rawlog_output, flags); |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
68 } |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
69 |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
70 struct ostream * |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
71 o_stream_create_rawlog_from_stream(struct ostream *output, |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
72 struct ostream *rawlog_output, |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
73 enum iostream_rawlog_flags flags) |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
74 { |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
75 struct rawlog_ostream *rstream; |
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
76 |
13534 | 77 rstream = i_new(struct rawlog_ostream, 1); |
78 rstream->ostream.sendv = o_stream_rawlog_sendv; | |
79 rstream->ostream.iostream.close = o_stream_rawlog_close; | |
80 | |
18166
c02f29ca104d
lib: iostream-rawlog now writes to ostreams instead of directly to fds.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
81 rstream->riostream.rawlog_output = rawlog_output; |
14967
8ce6d41d2d41
iostream-rawlog: Added possibility to save input/output to the same file.
Timo Sirainen <tss@iki.fi>
parents:
14851
diff
changeset
|
82 iostream_rawlog_init(&rstream->riostream, flags, FALSE); |
18378
ebc2672a30f6
lib: o_stream_create_rawlog_from_stream() should preserve the output stream fd.
Timo Sirainen <tss@iki.fi>
parents:
18166
diff
changeset
|
83 return o_stream_create(&rstream->ostream, output, |
ebc2672a30f6
lib: o_stream_create_rawlog_from_stream() should preserve the output stream fd.
Timo Sirainen <tss@iki.fi>
parents:
18166
diff
changeset
|
84 o_stream_get_fd(output)); |
13534 | 85 } |