annotate src/lib/ostream-rawlog.c @ 22664:fea53c2725c0

director: Fix director_max_parallel_moves/kicks type Should be uint, not time.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 09 Nov 2017 12:24:16 +0200
parents 2e2563132d5f
children cb108f786fb4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21390
2e2563132d5f Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents: 19552
diff changeset
1 /* Copyright (c) 2011-2017 Dovecot authors, see the included COPYING file */
13534
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
2
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
3 #include "lib.h"
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
4 #include "iostream-rawlog-private.h"
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
5 #include "ostream-private.h"
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
6 #include "ostream-rawlog.h"
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
7
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
8 struct rawlog_ostream {
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
9 struct ostream_private ostream;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
10 struct rawlog_iostream riostream;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
11 };
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
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
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
15 {
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
16 struct rawlog_ostream *rstream = (struct rawlog_ostream *)stream;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
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
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
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
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
23 }
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
24
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
25 static ssize_t
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
26 o_stream_rawlog_sendv(struct ostream_private *stream,
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
27 const struct const_iovec *iov, unsigned int iov_count)
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
28 {
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
29 struct rawlog_ostream *rstream = (struct rawlog_ostream *)stream;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
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
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
32
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
33 if ((ret = o_stream_sendv(stream->parent, iov, iov_count)) < 0) {
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
34 o_stream_copy_error_from_parent(stream);
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
35 return -1;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
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
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
49
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
50 stream->ostream.offset += ret;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
51 return ret;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
52 }
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
53
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
54 struct ostream *
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
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
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
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
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
60
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
61 i_assert(rawlog_path != NULL);
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
62 i_assert(rawlog_fd != -1);
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
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
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
77 rstream = i_new(struct rawlog_ostream, 1);
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
78 rstream->ostream.sendv = o_stream_rawlog_sendv;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
79 rstream->ostream.iostream.close = o_stream_rawlog_close;
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
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
0860ac364dec Added rawlog i/ostreams.
Timo Sirainen <tss@iki.fi>
parents:
diff changeset
85 }