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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
/* Copyright (c) 2015-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "buffer.h"
#include "ostream-private.h"
#include "ostream-failure-at.h"
struct failure_at_ostream {
struct ostream_private ostream;
char *error_string;
uoff_t failure_offset;
bool failed;
};
static void o_stream_failure_at_destroy(struct iostream_private *stream)
{
struct failure_at_ostream *fstream =
container_of(stream, struct failure_at_ostream,
ostream.iostream);
i_free(fstream->error_string);
o_stream_unref(&fstream->ostream.parent);
}
static ssize_t
o_stream_failure_at_sendv(struct ostream_private *stream,
const struct const_iovec *iov, unsigned int iov_count)
{
struct failure_at_ostream *fstream =
container_of(stream, struct failure_at_ostream, ostream);
unsigned int i;
struct const_iovec *iov_dup;
unsigned int iov_dup_count;
uoff_t bytes_until_failure, blocking_bytes_count = 0;
ssize_t ret;
if (stream->ostream.blocking) {
/* blocking ostream must return either a full success or a
failure. if the current write would go past failure_offset,
return a failure now before writing anything. */
for (i = 0; i < iov_count; i++)
blocking_bytes_count += iov[i].iov_len;
if (blocking_bytes_count > 0) {
/* if we're exactly at the failure offset after this
write, fail it only on the next write. */
blocking_bytes_count--;
}
}
if (fstream->failure_offset <= stream->ostream.offset + blocking_bytes_count) {
io_stream_set_error(&stream->iostream, "%s",
fstream->error_string);
stream->ostream.stream_errno = errno = EIO;
fstream->failed = TRUE;
return -1;
}
bytes_until_failure = fstream->failure_offset - stream->ostream.offset;
iov_dup = i_new(struct const_iovec, iov_count);
iov_dup_count = iov_count;
for (i = 0; i < iov_count; i++) {
iov_dup[i] = iov[i];
if (iov_dup[i].iov_len >= bytes_until_failure) {
iov_dup[i].iov_len = bytes_until_failure;
iov_dup_count = i+1;
break;
}
}
ret = o_stream_sendv(stream->parent, iov_dup, iov_dup_count);
i_free(iov_dup);
if (ret < 0) {
o_stream_copy_error_from_parent(stream);
return -1;
}
stream->ostream.offset += ret;
return ret;
}
static int
o_stream_failure_at_flush(struct ostream_private *stream)
{
struct failure_at_ostream *fstream =
container_of(stream, struct failure_at_ostream, ostream);
if (fstream->failed) {
io_stream_set_error(&stream->iostream, "%s",
fstream->error_string);
stream->ostream.stream_errno = errno = EIO;
return -1;
}
return o_stream_flush_parent(stream);
}
struct ostream *
o_stream_create_failure_at(struct ostream *output, uoff_t failure_offset,
const char *error_string)
{
struct failure_at_ostream *fstream;
fstream = i_new(struct failure_at_ostream, 1);
fstream->ostream.sendv = o_stream_failure_at_sendv;
fstream->ostream.flush = o_stream_failure_at_flush;
fstream->ostream.iostream.destroy = o_stream_failure_at_destroy;
fstream->failure_offset = failure_offset;
fstream->error_string = i_strdup(error_string);
return o_stream_create(&fstream->ostream, output,
o_stream_get_fd(output));
}
struct ostream *
o_stream_create_failure_at_flush(struct ostream *output, const char *error_string)
{
struct failure_at_ostream *fstream;
fstream = i_new(struct failure_at_ostream, 1);
fstream->ostream.flush = o_stream_failure_at_flush;
fstream->ostream.iostream.destroy = o_stream_failure_at_destroy;
fstream->error_string = i_strdup(error_string);
fstream->failed = TRUE;
return o_stream_create(&fstream->ostream, output,
o_stream_get_fd(output));
}
|