/src/dovecot/src/lib/istream.c
Line | Count | Source |
1 | | /* Copyright (c) Dovecot authors, see top-level COPYING file */ |
2 | | |
3 | | #include "lib.h" |
4 | | #include "ioloop.h" |
5 | | #include "array.h" |
6 | | #include "str.h" |
7 | | #include "memarea.h" |
8 | | #include "istream-private.h" |
9 | | |
10 | | static bool i_stream_is_buffer_invalid(const struct istream_private *stream); |
11 | | |
12 | | void i_stream_set_name(struct istream *stream, const char *name) |
13 | 0 | { |
14 | 0 | i_free(stream->real_stream->iostream.name); |
15 | 0 | stream->real_stream->iostream.name = i_strdup(name); |
16 | 0 | } |
17 | | |
18 | | const char *i_stream_get_name(struct istream *stream) |
19 | 0 | { |
20 | 0 | i_assert(stream != NULL); |
21 | 0 | while (stream->real_stream->iostream.name == NULL) { |
22 | 0 | stream = stream->real_stream->parent; |
23 | 0 | if (stream == NULL) |
24 | 0 | return ""; |
25 | 0 | } |
26 | 0 | return stream->real_stream->iostream.name; |
27 | 0 | } |
28 | | |
29 | | static void i_stream_close_set_io_pending(struct istream *stream, |
30 | | bool close_parents) |
31 | 0 | { |
32 | 0 | struct istream *root = i_stream_get_root_io(stream); |
33 | |
|
34 | 0 | if (root != stream && !close_parents) { |
35 | | /* Only this istream was closed, while the root istream that |
36 | | owns the IO is still open (e.g. a literal limit-istream |
37 | | destroyed while the connection istream continues to be |
38 | | read). Don't wake up the IO. */ |
39 | 0 | return; |
40 | 0 | } |
41 | | /* Make sure the IO callback gets called, so it notices that the |
42 | | istream is closed. Otherwise it could wait forever for more input. |
43 | | If the IO isn't added yet, call it once it gets added. The IO is |
44 | | owned by the root istream. Closing the parents usually closes the |
45 | | root istream as well, but not always (e.g. istream-multiplex keeps |
46 | | its parent open until all channels are closed), so wake up the |
47 | | root istream's IO explicitly. */ |
48 | 0 | if (root->real_stream->io != NULL) |
49 | 0 | io_set_pending(root->real_stream->io); |
50 | 0 | else |
51 | 0 | root->real_stream->io_pending = TRUE; |
52 | 0 | } |
53 | | |
54 | | static void i_stream_close_full(struct istream *stream, bool close_parents) |
55 | 0 | { |
56 | 0 | bool was_closed = stream->closed; |
57 | |
|
58 | 0 | io_stream_close(&stream->real_stream->iostream, close_parents); |
59 | 0 | stream->closed = TRUE; |
60 | |
|
61 | 0 | if (stream->stream_errno == 0) |
62 | 0 | stream->stream_errno = EPIPE; |
63 | | |
64 | | /* Wake up the IO only when the istream becomes closed. Closing an |
65 | | already closed istream again shouldn't call the IO callback |
66 | | again. */ |
67 | 0 | if (!was_closed) |
68 | 0 | i_stream_close_set_io_pending(stream, close_parents); |
69 | 0 | } |
70 | | |
71 | | void i_stream_destroy(struct istream **stream) |
72 | 0 | { |
73 | 0 | if (*stream == NULL) |
74 | 0 | return; |
75 | | |
76 | 0 | i_stream_close_full(*stream, FALSE); |
77 | 0 | i_stream_unref(stream); |
78 | 0 | } |
79 | | |
80 | | void i_stream_ref(struct istream *stream) |
81 | 0 | { |
82 | 0 | io_stream_ref(&stream->real_stream->iostream); |
83 | 0 | } |
84 | | |
85 | | void i_stream_unref(struct istream **_stream) |
86 | 0 | { |
87 | 0 | struct istream *stream = *_stream; |
88 | 0 | struct istream_private *rstream; |
89 | |
|
90 | 0 | if (stream == NULL) |
91 | 0 | return; |
92 | | |
93 | 0 | *_stream = NULL; |
94 | 0 | rstream = stream->real_stream; |
95 | |
|
96 | 0 | if (rstream->iostream.refcount > 1) { |
97 | 0 | if (!io_stream_unref(&rstream->iostream)) |
98 | 0 | i_unreached(); |
99 | 0 | } else { |
100 | | /* The snapshot may contain pointers to the parent istreams. |
101 | | Free it before io_stream_unref() frees the parents. */ |
102 | 0 | i_stream_snapshot_free(&rstream->prev_snapshot); |
103 | |
|
104 | 0 | if (io_stream_unref(&rstream->iostream)) |
105 | 0 | i_unreached(); |
106 | 0 | str_free(&rstream->line_str); |
107 | 0 | i_stream_unref(&rstream->parent); |
108 | 0 | io_stream_free(&rstream->iostream); |
109 | 0 | } |
110 | 0 | } |
111 | | |
112 | | #undef i_stream_add_destroy_callback |
113 | | void i_stream_add_destroy_callback(struct istream *stream, |
114 | | istream_callback_t *callback, void *context) |
115 | 0 | { |
116 | 0 | io_stream_add_destroy_callback(&stream->real_stream->iostream, |
117 | 0 | callback, context); |
118 | 0 | } |
119 | | |
120 | | void i_stream_remove_destroy_callback(struct istream *stream, |
121 | | void (*callback)()) |
122 | 0 | { |
123 | 0 | io_stream_remove_destroy_callback(&stream->real_stream->iostream, |
124 | 0 | callback); |
125 | 0 | } |
126 | | |
127 | | int i_stream_get_fd(struct istream *stream) |
128 | 0 | { |
129 | 0 | struct istream_private *_stream = stream->real_stream; |
130 | |
|
131 | 0 | return _stream->fd; |
132 | 0 | } |
133 | | |
134 | | void i_stream_copy_fd(struct istream *dest, struct istream *source) |
135 | 0 | { |
136 | 0 | int fd = i_stream_get_fd(source); |
137 | |
|
138 | 0 | i_assert(fd != -1); |
139 | 0 | i_assert(dest->real_stream->fd == -1); |
140 | 0 | dest->real_stream->fd = fd; |
141 | 0 | dest->readable_fd = source->readable_fd; |
142 | 0 | } |
143 | | |
144 | | void i_stream_set_error(struct istream *stream, int stream_errno, |
145 | | const char *fmt, ...) |
146 | 0 | { |
147 | 0 | va_list args; |
148 | |
|
149 | 0 | va_start(args, fmt); |
150 | 0 | stream->stream_errno = stream_errno; |
151 | 0 | io_stream_set_verror(&stream->real_stream->iostream, fmt, args); |
152 | 0 | va_end(args); |
153 | 0 | } |
154 | | |
155 | | const char *i_stream_get_error(struct istream *stream) |
156 | 0 | { |
157 | 0 | struct istream *s; |
158 | | |
159 | | /* we'll only return errors for streams that have stream_errno set or |
160 | | that have reached EOF. we might be returning unintended error |
161 | | otherwise. */ |
162 | 0 | if (stream->stream_errno == 0) |
163 | 0 | return stream->eof ? "EOF" : "<no error>"; |
164 | | |
165 | 0 | for (s = stream; s != NULL; s = s->real_stream->parent) { |
166 | 0 | if (s->stream_errno == 0) |
167 | 0 | break; |
168 | 0 | if (s->real_stream->iostream.error != NULL) |
169 | 0 | return s->real_stream->iostream.error; |
170 | 0 | } |
171 | 0 | return strerror(stream->stream_errno); |
172 | 0 | } |
173 | | |
174 | | const char *i_stream_get_disconnect_reason(struct istream *stream) |
175 | 0 | { |
176 | 0 | return io_stream_get_disconnect_reason(stream, NULL); |
177 | 0 | } |
178 | | |
179 | | void i_stream_close(struct istream *stream) |
180 | 0 | { |
181 | 0 | if (stream != NULL) |
182 | 0 | i_stream_close_full(stream, TRUE); |
183 | 0 | } |
184 | | |
185 | | void i_stream_set_init_buffer_size(struct istream *stream, size_t size) |
186 | 0 | { |
187 | 0 | stream->real_stream->init_buffer_size = size; |
188 | 0 | } |
189 | | |
190 | | void i_stream_set_max_buffer_size(struct istream *stream, size_t max_size) |
191 | 0 | { |
192 | 0 | io_stream_set_max_buffer_size(&stream->real_stream->iostream, max_size); |
193 | 0 | } |
194 | | |
195 | | size_t i_stream_get_max_buffer_size(struct istream *stream) |
196 | 0 | { |
197 | 0 | size_t max_size = 0; |
198 | |
|
199 | 0 | do { |
200 | 0 | if (max_size < stream->real_stream->max_buffer_size) |
201 | 0 | max_size = stream->real_stream->max_buffer_size; |
202 | 0 | stream = stream->real_stream->parent; |
203 | 0 | } while (stream != NULL); |
204 | 0 | return max_size; |
205 | 0 | } |
206 | | |
207 | | void i_stream_set_return_partial_line(struct istream *stream, bool set) |
208 | 0 | { |
209 | 0 | stream->real_stream->return_nolf_line = set; |
210 | 0 | } |
211 | | |
212 | | void i_stream_set_persistent_buffers(struct istream *stream, bool set) |
213 | 0 | { |
214 | 0 | do { |
215 | 0 | stream->real_stream->nonpersistent_buffers = !set; |
216 | 0 | stream = stream->real_stream->parent; |
217 | 0 | } while (stream != NULL); |
218 | 0 | } |
219 | | |
220 | | void i_stream_set_blocking(struct istream *stream, bool blocking) |
221 | 0 | { |
222 | 0 | int prev_fd = -1; |
223 | |
|
224 | 0 | do { |
225 | 0 | stream->blocking = blocking; |
226 | 0 | if (stream->real_stream->fd != -1 && |
227 | 0 | stream->real_stream->fd != prev_fd) { |
228 | 0 | fd_set_nonblock(stream->real_stream->fd, !blocking); |
229 | 0 | prev_fd = stream->real_stream->fd; |
230 | 0 | } |
231 | 0 | stream = stream->real_stream->parent; |
232 | 0 | } while (stream != NULL); |
233 | 0 | } |
234 | | |
235 | | static void i_stream_update(struct istream_private *stream) |
236 | 0 | { |
237 | 0 | if (stream->parent == NULL) |
238 | 0 | stream->access_counter++; |
239 | 0 | else { |
240 | 0 | stream->access_counter = |
241 | 0 | stream->parent->real_stream->access_counter; |
242 | 0 | stream->parent_expected_offset = stream->parent->v_offset; |
243 | 0 | } |
244 | 0 | } |
245 | | |
246 | | static bool snapshot_has_memarea(struct istream_snapshot *snapshot, |
247 | | struct memarea *memarea) |
248 | 0 | { |
249 | 0 | if (snapshot->old_memarea == memarea) |
250 | 0 | return TRUE; |
251 | 0 | if (snapshot->prev_snapshot != NULL) |
252 | 0 | return snapshot_has_memarea(snapshot->prev_snapshot, memarea); |
253 | 0 | return FALSE; |
254 | 0 | } |
255 | | |
256 | | struct istream_snapshot * |
257 | | i_stream_default_snapshot(struct istream_private *stream, |
258 | | struct istream_snapshot *prev_snapshot) |
259 | 0 | { |
260 | 0 | struct istream_snapshot *snapshot; |
261 | |
|
262 | 0 | if (stream->memarea != NULL) { |
263 | 0 | if (prev_snapshot != NULL) { |
264 | 0 | if (snapshot_has_memarea(prev_snapshot, stream->memarea)) |
265 | 0 | return prev_snapshot; |
266 | 0 | } |
267 | | /* This stream has a memarea. Reference it, so we can later on |
268 | | rollback if needed. */ |
269 | 0 | snapshot = i_new(struct istream_snapshot, 1); |
270 | 0 | snapshot->old_memarea = stream->memarea; |
271 | 0 | snapshot->prev_snapshot = prev_snapshot; |
272 | 0 | memarea_ref(snapshot->old_memarea); |
273 | 0 | return snapshot; |
274 | 0 | } |
275 | 0 | if (stream->parent == NULL) { |
276 | 0 | if (stream->nonpersistent_buffers) { |
277 | | /* Assume that memarea would be used normally, but |
278 | | now it's NULL because the buffer is empty and |
279 | | empty buffers are freed. */ |
280 | 0 | i_assert(stream->skip == stream->pos); |
281 | 0 | return prev_snapshot; |
282 | 0 | } |
283 | 0 | i_panic("%s is missing istream.snapshot() implementation", |
284 | 0 | i_stream_get_name(&stream->istream)); |
285 | 0 | } |
286 | 0 | struct istream_private *_parent_stream = |
287 | 0 | stream->parent->real_stream; |
288 | 0 | return _parent_stream->snapshot(_parent_stream, prev_snapshot); |
289 | 0 | } |
290 | | |
291 | | void i_stream_snapshot_free(struct istream_snapshot **_snapshot) |
292 | 0 | { |
293 | 0 | struct istream_snapshot *snapshot = *_snapshot; |
294 | |
|
295 | 0 | if (*_snapshot == NULL) |
296 | 0 | return; |
297 | 0 | *_snapshot = NULL; |
298 | |
|
299 | 0 | i_stream_snapshot_free(&snapshot->prev_snapshot); |
300 | 0 | if (snapshot->free != NULL) |
301 | 0 | snapshot->free(snapshot); |
302 | 0 | else { |
303 | 0 | if (snapshot->old_memarea != NULL) |
304 | 0 | memarea_unref(&snapshot->old_memarea); |
305 | 0 | i_stream_unref(&snapshot->istream); |
306 | 0 | i_free(snapshot); |
307 | 0 | } |
308 | 0 | } |
309 | | |
310 | | static struct istream_snapshot * |
311 | | i_stream_noop_snapshot(struct istream_private *stream ATTR_UNUSED, |
312 | | struct istream_snapshot *prev_snapshot) |
313 | 0 | { |
314 | 0 | return prev_snapshot; |
315 | 0 | } |
316 | | |
317 | | static struct istream_private * |
318 | | i_stream_get_io_parent(struct istream_private *_stream) |
319 | 0 | { |
320 | 0 | if (_stream->parent != NULL) |
321 | 0 | return _stream->parent->real_stream; |
322 | 0 | if (_stream->io_parent != NULL) |
323 | 0 | return _stream->io_parent->real_stream; |
324 | 0 | return NULL; |
325 | 0 | } |
326 | | |
327 | | static bool i_stream_is_io_pending_until_read(struct istream_private *_stream) |
328 | 0 | { |
329 | 0 | struct istream_private *parent; |
330 | |
|
331 | 0 | while (!_stream->io_pending_until_read && |
332 | 0 | (parent = i_stream_get_io_parent(_stream)) != NULL) |
333 | 0 | _stream = parent; |
334 | 0 | return _stream->io_pending_until_read; |
335 | 0 | } |
336 | | |
337 | | ssize_t i_stream_read(struct istream *stream) |
338 | 0 | { |
339 | 0 | struct istream_private *_stream = stream->real_stream; |
340 | 0 | ssize_t ret; |
341 | | #ifdef DEBUG |
342 | | unsigned char prev_buf[4]; |
343 | | const unsigned char *prev_data = _stream->buffer; |
344 | | size_t prev_skip = _stream->skip, prev_pos = _stream->pos; |
345 | | bool invalid = i_stream_is_buffer_invalid(_stream); |
346 | | |
347 | | i_assert(prev_skip <= prev_pos); |
348 | | size_t prev_size = prev_pos - prev_skip; |
349 | | if (invalid) |
350 | | ; |
351 | | else if (prev_size > 4) { |
352 | | memcpy(prev_buf, prev_data + prev_skip, 2); |
353 | | memcpy(prev_buf+2, prev_data + prev_pos - 2, 2); |
354 | | } else if (prev_size > 0) { |
355 | | memcpy(prev_buf, prev_data + prev_skip, prev_size); |
356 | | } |
357 | | #endif |
358 | |
|
359 | 0 | if (_stream->skip != _stream->pos || _stream->prev_snapshot != NULL) { |
360 | 0 | _stream->prev_snapshot = |
361 | 0 | _stream->snapshot(_stream, _stream->prev_snapshot); |
362 | 0 | } |
363 | 0 | ret = i_stream_read_memarea(stream); |
364 | 0 | if (ret > 0) |
365 | 0 | i_stream_snapshot_free(&_stream->prev_snapshot); |
366 | | #ifdef DEBUG |
367 | | else if (!invalid) { |
368 | | i_assert((_stream->pos - _stream->skip) == (prev_pos - prev_skip) || |
369 | | prev_pos == prev_skip); |
370 | | if (prev_data == NULL) |
371 | | i_assert(prev_pos == prev_skip); |
372 | | else if (prev_pos - prev_skip <= 4) |
373 | | i_assert(memcmp(prev_buf, prev_data + prev_skip, prev_pos - prev_skip) == 0); |
374 | | else { |
375 | | i_assert(memcmp(prev_buf, prev_data + prev_skip, 2) == 0); |
376 | | i_assert(memcmp(prev_buf+2, prev_data + prev_pos - 2, 2) == 0); |
377 | | } |
378 | | } |
379 | | #endif |
380 | 0 | if (!_stream->istream.eof && |
381 | 0 | i_stream_is_io_pending_until_read(_stream)) { |
382 | | /* One of the parent istreams still has IO pending, because its |
383 | | read() wasn't called. Set IO back to pending to prevent |
384 | | hangs. */ |
385 | 0 | i_stream_set_input_pending(stream, TRUE); |
386 | 0 | } |
387 | 0 | return ret; |
388 | 0 | } |
389 | | |
390 | | /* The istreams whose read() is currently running, innermost first. Used for |
391 | | verifying that istreams declare the istreams they read. */ |
392 | | struct istream_reader { |
393 | | const struct istream_reader *prev; |
394 | | struct istream *stream; |
395 | | /* The ioloop that was running when read() was called. A nested |
396 | | io_loop_run() (e.g. dict_wait()) runs unrelated istreams, which |
397 | | this istream isn't reading. */ |
398 | | struct ioloop *ioloop; |
399 | | }; |
400 | | static const struct istream_reader *istream_cur_reader = NULL; |
401 | | |
402 | | static void i_stream_verify_reader(struct istream *stream) |
403 | 0 | { |
404 | 0 | struct istream_private *reader; |
405 | |
|
406 | 0 | if (istream_cur_reader == NULL || istream_cur_reader->stream == stream) |
407 | 0 | return; |
408 | 0 | if (istream_cur_reader->ioloop != current_ioloop) { |
409 | | /* Running in a nested ioloop, so this istream is unrelated to |
410 | | the istream whose read() is running. */ |
411 | 0 | return; |
412 | 0 | } |
413 | 0 | if (stream->blocking) { |
414 | | /* Blocking istreams are read until they're finished, so their |
415 | | ioloop IO doesn't matter. read() implementations may create |
416 | | and drain such istreams internally (e.g. a temp file). */ |
417 | 0 | return; |
418 | 0 | } |
419 | 0 | if (istream_cur_reader->stream->blocking) { |
420 | | /* A blocking istream is also read until it's finished, so it |
421 | | never depends on an ioloop IO to wake it up, and it doesn't |
422 | | matter which istreams it reads. */ |
423 | 0 | return; |
424 | 0 | } |
425 | | |
426 | 0 | reader = istream_cur_reader->stream->real_stream; |
427 | 0 | if (stream == reader->parent) |
428 | 0 | return; |
429 | 0 | if (reader->io_parent != NULL && |
430 | 0 | i_stream_get_root_io(stream) == |
431 | 0 | i_stream_get_root_io(reader->io_parent)) { |
432 | | /* The declared istream, or another istream reading from the |
433 | | same one (e.g. istream-decompress reads both its input and |
434 | | the decompressing istream created on top of it). */ |
435 | 0 | return; |
436 | 0 | } |
437 | 0 | if (reader->hidden_inputs == ISTREAM_HIDDEN_INPUTS_UNCHECKED || |
438 | 0 | reader->hidden_inputs == ISTREAM_HIDDEN_INPUTS_PANIC) { |
439 | | /* Reads istreams it doesn't declare, so there's nothing to |
440 | | compare against. */ |
441 | 0 | return; |
442 | 0 | } |
443 | | |
444 | 0 | i_panic("istream %s reads undeclared istream %s " |
445 | 0 | "(missing istream_private.io_parent?)", |
446 | 0 | i_stream_get_name(&reader->istream), |
447 | 0 | i_stream_get_name(stream)); |
448 | 0 | } |
449 | | |
450 | | ssize_t i_stream_read_memarea(struct istream *stream) |
451 | 0 | { |
452 | 0 | struct istream_private *_stream = stream->real_stream; |
453 | 0 | size_t old_size; |
454 | 0 | ssize_t ret; |
455 | |
|
456 | 0 | if (unlikely(stream->closed || stream->stream_errno != 0)) { |
457 | 0 | stream->eof = TRUE; |
458 | 0 | errno = stream->stream_errno; |
459 | 0 | return -1; |
460 | 0 | } |
461 | | |
462 | 0 | i_stream_verify_reader(stream); |
463 | 0 | stream->eof = FALSE; |
464 | |
|
465 | 0 | if (_stream->parent != NULL) |
466 | 0 | i_stream_seek(_stream->parent, _stream->parent_expected_offset); |
467 | |
|
468 | 0 | old_size = _stream->pos - _stream->skip; |
469 | 0 | if (_stream->pos < _stream->high_pos) { |
470 | | /* we're here because we seeked back within the read buffer. */ |
471 | 0 | ret = _stream->high_pos - _stream->pos; |
472 | 0 | _stream->pos = _stream->high_pos; |
473 | 0 | _stream->high_pos = 0; |
474 | 0 | } else { |
475 | 0 | struct istream_reader reader = { |
476 | 0 | .prev = istream_cur_reader, |
477 | 0 | .stream = stream, |
478 | 0 | .ioloop = current_ioloop, |
479 | 0 | }; |
480 | |
|
481 | 0 | _stream->high_pos = 0; |
482 | 0 | _stream->io_pending_until_read = FALSE; |
483 | 0 | istream_cur_reader = &reader; |
484 | 0 | ret = _stream->read(_stream); |
485 | 0 | istream_cur_reader = reader.prev; |
486 | 0 | } |
487 | 0 | i_assert(_stream->skip <= _stream->pos); |
488 | 0 | i_assert(old_size <= _stream->pos - _stream->skip); |
489 | 0 | switch (ret) { |
490 | 0 | case -2: |
491 | 0 | i_assert(_stream->skip != _stream->pos); |
492 | 0 | break; |
493 | 0 | case -1: |
494 | 0 | if (stream->stream_errno != 0) { |
495 | | /* error handling should be easier if we now just |
496 | | assume the stream is now at EOF */ |
497 | 0 | stream->eof = TRUE; |
498 | 0 | errno = stream->stream_errno; |
499 | 0 | } else { |
500 | 0 | i_assert(stream->eof); |
501 | 0 | i_assert(old_size == _stream->pos - _stream->skip); |
502 | 0 | } |
503 | 0 | break; |
504 | 0 | case 0: |
505 | 0 | i_assert(!stream->blocking); |
506 | 0 | break; |
507 | 0 | default: |
508 | 0 | i_assert(ret > 0); |
509 | 0 | i_assert(_stream->skip < _stream->pos); |
510 | 0 | i_assert((size_t)ret+old_size == _stream->pos - _stream->skip); |
511 | 0 | _stream->last_read_timeval = ioloop_timeval; |
512 | 0 | break; |
513 | 0 | } |
514 | | |
515 | 0 | if (stream->stream_errno != 0) { |
516 | | /* error handling should be easier if we now just |
517 | | assume the stream is now at EOF. Note that we could get here |
518 | | even if read() didn't return -1, although that's a little |
519 | | bit sloppy istream implementation. */ |
520 | 0 | stream->eof = TRUE; |
521 | 0 | } |
522 | |
|
523 | 0 | i_stream_update(_stream); |
524 | | /* verify that parents' access_counters are valid. the parent's |
525 | | i_stream_read() should guarantee this. */ |
526 | 0 | i_assert(!i_stream_is_buffer_invalid(_stream)); |
527 | 0 | return ret; |
528 | 0 | } |
529 | | |
530 | | int i_stream_read_more_memarea(struct istream *stream, |
531 | | const unsigned char **data_r, size_t *size_r) |
532 | 0 | { |
533 | 0 | *data_r = i_stream_get_data(stream, size_r); |
534 | 0 | if (*size_r > 0) |
535 | 0 | return 1; |
536 | | |
537 | 0 | int ret = i_stream_read_memarea(stream); |
538 | 0 | *data_r = i_stream_get_data(stream, size_r); |
539 | 0 | return ret; |
540 | 0 | } |
541 | | |
542 | | void i_stream_get_last_read_time(struct istream *stream, struct timeval *tv_r) |
543 | 0 | { |
544 | 0 | *tv_r = stream->real_stream->last_read_timeval; |
545 | 0 | } |
546 | | |
547 | | ssize_t i_stream_read_copy_from_parent(struct istream *istream) |
548 | 0 | { |
549 | 0 | struct istream_private *stream = istream->real_stream; |
550 | 0 | size_t pos; |
551 | 0 | ssize_t ret; |
552 | |
|
553 | 0 | stream->pos -= stream->skip; |
554 | 0 | stream->skip = 0; |
555 | |
|
556 | 0 | stream->buffer = i_stream_get_data(stream->parent, &pos); |
557 | 0 | if (pos > stream->pos) |
558 | 0 | ret = 0; |
559 | 0 | else do { |
560 | 0 | ret = i_stream_read_memarea(stream->parent); |
561 | 0 | stream->istream.stream_errno = stream->parent->stream_errno; |
562 | 0 | stream->istream.eof = stream->parent->eof; |
563 | 0 | stream->buffer = i_stream_get_data(stream->parent, &pos); |
564 | | /* check again, in case the parent stream had been seeked |
565 | | backwards and the previous read() didn't get us far |
566 | | enough. */ |
567 | 0 | } while (pos <= stream->pos && ret > 0); |
568 | 0 | if (ret == -2) { |
569 | 0 | i_stream_update(stream); |
570 | 0 | return -2; |
571 | 0 | } |
572 | | |
573 | 0 | ret = pos > stream->pos ? (ssize_t)(pos - stream->pos) : |
574 | 0 | (ret == 0 ? 0 : -1); |
575 | 0 | stream->pos = pos; |
576 | 0 | i_assert(ret != -1 || stream->istream.eof || |
577 | 0 | stream->istream.stream_errno != 0); |
578 | 0 | i_stream_update(stream); |
579 | 0 | return ret; |
580 | 0 | } |
581 | | |
582 | | void i_stream_free_buffer(struct istream_private *stream) |
583 | 0 | { |
584 | 0 | if (stream->memarea != NULL) { |
585 | 0 | memarea_unref(&stream->memarea); |
586 | 0 | stream->w_buffer = NULL; |
587 | 0 | } else if (stream->w_buffer != NULL) { |
588 | 0 | i_free_and_null(stream->w_buffer); |
589 | 0 | } else { |
590 | | /* don't know how to free it */ |
591 | 0 | return; |
592 | 0 | } |
593 | 0 | stream->buffer_size = 0; |
594 | 0 | } |
595 | | |
596 | | void i_stream_skip(struct istream *stream, uoff_t count) |
597 | 0 | { |
598 | 0 | struct istream_private *_stream = stream->real_stream; |
599 | 0 | size_t data_size; |
600 | |
|
601 | 0 | data_size = _stream->pos - _stream->skip; |
602 | 0 | if (count <= data_size) { |
603 | | /* within buffer */ |
604 | 0 | stream->v_offset += count; |
605 | 0 | _stream->skip += count; |
606 | 0 | if (_stream->nonpersistent_buffers && |
607 | 0 | _stream->skip == _stream->pos) { |
608 | 0 | _stream->skip = _stream->pos = 0; |
609 | 0 | i_stream_free_buffer(_stream); |
610 | 0 | } |
611 | 0 | return; |
612 | 0 | } |
613 | | |
614 | | /* have to seek forward */ |
615 | 0 | count -= data_size; |
616 | 0 | _stream->skip = _stream->pos; |
617 | 0 | stream->v_offset += data_size; |
618 | |
|
619 | 0 | if (unlikely(stream->closed || stream->stream_errno != 0)) |
620 | 0 | return; |
621 | | |
622 | 0 | _stream->seek(_stream, stream->v_offset + count, FALSE); |
623 | 0 | } |
624 | | |
625 | | static bool i_stream_can_optimize_seek(struct istream_private *stream) |
626 | 0 | { |
627 | 0 | if (stream->parent == NULL) |
628 | 0 | return TRUE; |
629 | | |
630 | | /* use the fast route only if the parent stream hasn't been changed */ |
631 | 0 | if (stream->access_counter != |
632 | 0 | stream->parent->real_stream->access_counter) |
633 | 0 | return FALSE; |
634 | | |
635 | 0 | return i_stream_can_optimize_seek(stream->parent->real_stream); |
636 | 0 | } |
637 | | |
638 | | void i_stream_seek(struct istream *stream, uoff_t v_offset) |
639 | 0 | { |
640 | 0 | struct istream_private *_stream = stream->real_stream; |
641 | |
|
642 | 0 | if (v_offset >= stream->v_offset && |
643 | 0 | i_stream_can_optimize_seek(_stream)) |
644 | 0 | i_stream_skip(stream, v_offset - stream->v_offset); |
645 | 0 | else { |
646 | 0 | if (unlikely(stream->closed || stream->stream_errno != 0)) { |
647 | 0 | stream->eof = TRUE; |
648 | 0 | return; |
649 | 0 | } |
650 | 0 | stream->eof = FALSE; |
651 | 0 | _stream->seek(_stream, v_offset, FALSE); |
652 | 0 | } |
653 | 0 | i_stream_update(_stream); |
654 | 0 | } |
655 | | |
656 | | void i_stream_seek_mark(struct istream *stream, uoff_t v_offset) |
657 | 0 | { |
658 | 0 | struct istream_private *_stream = stream->real_stream; |
659 | |
|
660 | 0 | if (unlikely(stream->closed || stream->stream_errno != 0)) |
661 | 0 | return; |
662 | | |
663 | 0 | stream->eof = FALSE; |
664 | 0 | _stream->seek(_stream, v_offset, TRUE); |
665 | 0 | i_stream_update(_stream); |
666 | 0 | } |
667 | | |
668 | | void i_stream_sync(struct istream *stream) |
669 | 0 | { |
670 | 0 | struct istream_private *_stream = stream->real_stream; |
671 | |
|
672 | 0 | if (unlikely(stream->closed || stream->stream_errno != 0)) |
673 | 0 | return; |
674 | | |
675 | 0 | if (_stream->sync != NULL) { |
676 | 0 | _stream->sync(_stream); |
677 | 0 | i_stream_update(_stream); |
678 | 0 | } |
679 | 0 | } |
680 | | |
681 | | int i_stream_stat(struct istream *stream, bool exact, const struct stat **st_r) |
682 | 0 | { |
683 | 0 | struct istream_private *_stream = stream->real_stream; |
684 | |
|
685 | 0 | if (unlikely(stream->closed || stream->stream_errno != 0)) |
686 | 0 | return -1; |
687 | | |
688 | 0 | if (_stream->stat(_stream, exact) < 0) { |
689 | 0 | stream->eof = TRUE; |
690 | 0 | return -1; |
691 | 0 | } |
692 | 0 | *st_r = &_stream->statbuf; |
693 | 0 | return 0; |
694 | 0 | } |
695 | | |
696 | | int i_stream_get_size(struct istream *stream, bool exact, uoff_t *size_r) |
697 | 0 | { |
698 | 0 | struct istream_private *_stream = stream->real_stream; |
699 | |
|
700 | 0 | if (unlikely(stream->closed || stream->stream_errno != 0)) |
701 | 0 | return -1; |
702 | | |
703 | 0 | int ret; |
704 | 0 | if ((ret = _stream->get_size(_stream, exact, size_r)) < 0) |
705 | 0 | stream->eof = TRUE; |
706 | 0 | return ret; |
707 | 0 | } |
708 | | |
709 | | bool i_stream_have_bytes_left(struct istream *stream) |
710 | 0 | { |
711 | 0 | return i_stream_get_data_size(stream) > 0 || !stream->eof; |
712 | 0 | } |
713 | | |
714 | | bool i_stream_read_eof(struct istream *stream) |
715 | 0 | { |
716 | 0 | if (i_stream_get_data_size(stream) == 0) |
717 | 0 | (void)i_stream_read(stream); |
718 | 0 | return !i_stream_have_bytes_left(stream); |
719 | 0 | } |
720 | | |
721 | | uoff_t i_stream_get_absolute_offset(struct istream *stream) |
722 | 0 | { |
723 | 0 | uoff_t abs_offset = stream->v_offset; |
724 | 0 | while (stream != NULL) { |
725 | 0 | abs_offset += stream->real_stream->start_offset; |
726 | 0 | stream = stream->real_stream->parent; |
727 | 0 | } |
728 | 0 | return abs_offset; |
729 | 0 | } |
730 | | |
731 | | static char *i_stream_next_line_finish(struct istream_private *stream, size_t i) |
732 | 0 | { |
733 | 0 | char *ret; |
734 | 0 | size_t end; |
735 | |
|
736 | 0 | if (i > stream->skip && stream->buffer[i-1] == '\r') { |
737 | 0 | end = i - 1; |
738 | 0 | stream->line_crlf = TRUE; |
739 | 0 | } else { |
740 | 0 | end = i; |
741 | 0 | stream->line_crlf = FALSE; |
742 | 0 | } |
743 | |
|
744 | 0 | if (stream->buffer == stream->w_buffer && |
745 | 0 | end < stream->buffer_size) { |
746 | | /* modify the buffer directly */ |
747 | 0 | stream->w_buffer[end] = '\0'; |
748 | 0 | ret = (char *)stream->w_buffer + stream->skip; |
749 | 0 | } else { |
750 | | /* use a temporary string to return it */ |
751 | 0 | if (stream->line_str == NULL) |
752 | 0 | stream->line_str = str_new(default_pool, 256); |
753 | 0 | str_truncate(stream->line_str, 0); |
754 | 0 | if (stream->skip < end) |
755 | 0 | str_append_data(stream->line_str, stream->buffer + stream->skip, |
756 | 0 | end - stream->skip); |
757 | 0 | ret = str_c_modifiable(stream->line_str); |
758 | 0 | } |
759 | |
|
760 | 0 | if (i < stream->pos) |
761 | 0 | i++; |
762 | 0 | stream->istream.v_offset += i - stream->skip; |
763 | 0 | stream->skip = i; |
764 | 0 | return ret; |
765 | 0 | } |
766 | | |
767 | | static char *i_stream_last_line(struct istream_private *_stream) |
768 | 0 | { |
769 | 0 | if (_stream->istream.eof && _stream->skip != _stream->pos && |
770 | 0 | _stream->return_nolf_line) { |
771 | | /* the last line is missing LF and we want to return it. */ |
772 | 0 | return i_stream_next_line_finish(_stream, _stream->pos); |
773 | 0 | } |
774 | 0 | return NULL; |
775 | 0 | } |
776 | | |
777 | | char *i_stream_next_line(struct istream *stream) |
778 | 0 | { |
779 | 0 | struct istream_private *_stream = stream->real_stream; |
780 | 0 | const unsigned char *pos; |
781 | |
|
782 | 0 | if (_stream->skip >= _stream->pos) |
783 | 0 | return NULL; |
784 | | |
785 | 0 | pos = memchr(_stream->buffer + _stream->skip, '\n', |
786 | 0 | _stream->pos - _stream->skip); |
787 | 0 | if (pos != NULL) { |
788 | 0 | return i_stream_next_line_finish(_stream, |
789 | 0 | pos - _stream->buffer); |
790 | 0 | } else { |
791 | 0 | return i_stream_last_line(_stream); |
792 | 0 | } |
793 | 0 | } |
794 | | |
795 | | char *i_stream_read_next_line(struct istream *stream) |
796 | 0 | { |
797 | 0 | char *line; |
798 | |
|
799 | 0 | for (;;) { |
800 | 0 | line = i_stream_next_line(stream); |
801 | 0 | if (line != NULL) |
802 | 0 | break; |
803 | | |
804 | 0 | switch (i_stream_read(stream)) { |
805 | 0 | case -2: |
806 | 0 | io_stream_set_error(&stream->real_stream->iostream, |
807 | 0 | "Line is too long (over %zu" |
808 | 0 | " bytes at offset %"PRIuUOFF_T")", |
809 | 0 | i_stream_get_data_size(stream), stream->v_offset); |
810 | 0 | stream->stream_errno = errno = ENOBUFS; |
811 | 0 | stream->eof = TRUE; |
812 | 0 | return NULL; |
813 | 0 | case -1: |
814 | 0 | return i_stream_last_line(stream->real_stream); |
815 | 0 | case 0: |
816 | 0 | return NULL; |
817 | 0 | } |
818 | 0 | } |
819 | 0 | return line; |
820 | 0 | } |
821 | | |
822 | | bool i_stream_last_line_crlf(struct istream *stream) |
823 | 0 | { |
824 | 0 | return stream->real_stream->line_crlf; |
825 | 0 | } |
826 | | |
827 | | static bool i_stream_is_buffer_invalid(const struct istream_private *stream) |
828 | 0 | { |
829 | 0 | if (stream->parent == NULL) { |
830 | | /* the buffer can't point to parent, because it doesn't exist */ |
831 | 0 | return FALSE; |
832 | 0 | } |
833 | 0 | if (stream->w_buffer != NULL) { |
834 | | /* we can pretty safely assume that the stream is using its |
835 | | own private buffer, so it can never become invalid. */ |
836 | 0 | return FALSE; |
837 | 0 | } |
838 | 0 | if (stream->access_counter != |
839 | 0 | stream->parent->real_stream->access_counter) { |
840 | | /* parent has been modified behind this stream, we can't trust |
841 | | that our buffer is valid */ |
842 | 0 | return TRUE; |
843 | 0 | } |
844 | 0 | return i_stream_is_buffer_invalid(stream->parent->real_stream); |
845 | 0 | } |
846 | | |
847 | | const unsigned char * |
848 | | i_stream_get_data(struct istream *stream, size_t *size_r) |
849 | 0 | { |
850 | 0 | struct istream_private *_stream = stream->real_stream; |
851 | |
|
852 | 0 | if (_stream->skip >= _stream->pos) { |
853 | 0 | *size_r = 0; |
854 | 0 | return uchar_empty_ptr; |
855 | 0 | } |
856 | | |
857 | 0 | if (unlikely(i_stream_is_buffer_invalid(_stream))) { |
858 | | /* This stream may be using parent's buffer directly as |
859 | | _stream->buffer, but the parent stream has already been |
860 | | modified indirectly. This means that the buffer might no |
861 | | longer point to where we assume it points to. So we'll |
862 | | just return the stream as empty until it's read again. |
863 | | |
864 | | It's a bit ugly to suddenly drop data from the stream that |
865 | | was already read, but since this happens only with shared |
866 | | parent istreams the caller is hopefully aware enough that |
867 | | something like this might happen. The other solutions would |
868 | | be to a) try to automatically read the data back (but we |
869 | | can't handle errors..) or b) always copy data to stream's |
870 | | own buffer instead of pointing to parent's buffer (but this |
871 | | causes data copying that is nearly always unnecessary). */ |
872 | 0 | *size_r = 0; |
873 | | /* if we had already read until EOF, mark the stream again as |
874 | | not being at the end of file. */ |
875 | 0 | if (stream->stream_errno == 0) { |
876 | 0 | _stream->skip = _stream->pos = 0; |
877 | 0 | stream->eof = FALSE; |
878 | 0 | } |
879 | 0 | return uchar_empty_ptr; |
880 | 0 | } |
881 | | |
882 | 0 | *size_r = _stream->pos - _stream->skip; |
883 | 0 | return _stream->buffer + _stream->skip; |
884 | 0 | } |
885 | | |
886 | | size_t i_stream_get_data_size(struct istream *stream) |
887 | 0 | { |
888 | 0 | size_t size; |
889 | |
|
890 | 0 | (void)i_stream_get_data(stream, &size); |
891 | 0 | return size; |
892 | 0 | } |
893 | | |
894 | | unsigned char *i_stream_get_modifiable_data(struct istream *stream, |
895 | | size_t *size_r) |
896 | 0 | { |
897 | 0 | struct istream_private *_stream = stream->real_stream; |
898 | |
|
899 | 0 | if (_stream->skip >= _stream->pos || _stream->w_buffer == NULL) { |
900 | 0 | *size_r = 0; |
901 | 0 | return NULL; |
902 | 0 | } |
903 | | |
904 | 0 | *size_r = _stream->pos - _stream->skip; |
905 | 0 | return _stream->w_buffer + _stream->skip; |
906 | 0 | } |
907 | | |
908 | | int i_stream_read_data(struct istream *stream, const unsigned char **data_r, |
909 | | size_t *size_r, size_t threshold) |
910 | 0 | { |
911 | 0 | ssize_t ret = 0; |
912 | 0 | bool read_more = FALSE; |
913 | |
|
914 | 0 | do { |
915 | 0 | *data_r = i_stream_get_data(stream, size_r); |
916 | 0 | if (*size_r > threshold) |
917 | 0 | return 1; |
918 | | |
919 | | /* we need more data */ |
920 | 0 | ret = i_stream_read(stream); |
921 | 0 | if (ret > 0) |
922 | 0 | read_more = TRUE; |
923 | 0 | } while (ret > 0); |
924 | | |
925 | 0 | *data_r = i_stream_get_data(stream, size_r); |
926 | 0 | if (ret == -2) |
927 | 0 | return -2; |
928 | | |
929 | 0 | if (ret == 0) { |
930 | | /* need to read more */ |
931 | 0 | i_assert(!stream->blocking); |
932 | 0 | return 0; |
933 | 0 | } |
934 | 0 | if (stream->stream_errno == 0 && read_more) { |
935 | | /* we read at least some new data */ |
936 | 0 | return 0; |
937 | 0 | } |
938 | 0 | return -1; |
939 | 0 | } |
940 | | |
941 | | int i_stream_read_limited(struct istream *stream, const unsigned char **data_r, |
942 | | size_t *size_r, size_t limit) |
943 | 0 | { |
944 | 0 | struct istream_private *_stream = stream->real_stream; |
945 | 0 | int ret; |
946 | |
|
947 | 0 | *data_r = i_stream_get_data(stream, size_r); |
948 | 0 | if (*size_r >= limit) { |
949 | 0 | *size_r = limit; |
950 | 0 | return 1; |
951 | 0 | } |
952 | | |
953 | 0 | _stream->data_limit = limit; |
954 | 0 | ret = i_stream_read_more(stream, data_r, size_r); |
955 | 0 | _stream->data_limit = 0; |
956 | |
|
957 | 0 | if (*size_r >= limit) |
958 | 0 | *size_r = limit; |
959 | 0 | return ret; |
960 | 0 | } |
961 | | |
962 | | void i_stream_compress(struct istream_private *stream) |
963 | 0 | { |
964 | 0 | i_assert(stream->memarea == NULL || |
965 | 0 | memarea_get_refcount(stream->memarea) == 1); |
966 | | |
967 | 0 | if (stream->skip != stream->pos) { |
968 | 0 | memmove(stream->w_buffer, stream->w_buffer + stream->skip, |
969 | 0 | stream->pos - stream->skip); |
970 | 0 | } |
971 | 0 | stream->pos -= stream->skip; |
972 | |
|
973 | 0 | stream->skip = 0; |
974 | 0 | } |
975 | | |
976 | | static void i_stream_w_buffer_free(void *buf) |
977 | 0 | { |
978 | 0 | i_free(buf); |
979 | 0 | } |
980 | | |
981 | | static void |
982 | | i_stream_w_buffer_realloc(struct istream_private *stream, size_t old_size) |
983 | 0 | { |
984 | 0 | void *new_buffer; |
985 | |
|
986 | 0 | if (stream->memarea != NULL && |
987 | 0 | memarea_get_refcount(stream->memarea) == 1) { |
988 | | /* Nobody else is referencing the memarea. |
989 | | We can just reallocate it. */ |
990 | 0 | memarea_free_without_callback(&stream->memarea); |
991 | 0 | new_buffer = i_realloc(stream->w_buffer, old_size, |
992 | 0 | stream->buffer_size); |
993 | 0 | } else { |
994 | 0 | new_buffer = i_malloc(stream->buffer_size); |
995 | 0 | if (old_size > 0) { |
996 | 0 | i_assert(stream->w_buffer != NULL); |
997 | 0 | memcpy(new_buffer, stream->w_buffer, old_size); |
998 | 0 | } |
999 | 0 | if (stream->memarea != NULL) |
1000 | 0 | memarea_unref(&stream->memarea); |
1001 | 0 | } |
1002 | | |
1003 | 0 | stream->w_buffer = new_buffer; |
1004 | 0 | stream->buffer = new_buffer; |
1005 | |
|
1006 | 0 | stream->memarea = memarea_init(stream->w_buffer, stream->buffer_size, |
1007 | 0 | i_stream_w_buffer_free, new_buffer); |
1008 | 0 | } |
1009 | | |
1010 | | void i_stream_grow_buffer(struct istream_private *stream, size_t bytes) |
1011 | 0 | { |
1012 | 0 | size_t old_size, max_size; |
1013 | |
|
1014 | 0 | old_size = stream->buffer_size; |
1015 | |
|
1016 | 0 | stream->buffer_size = stream->pos + bytes; |
1017 | 0 | if (stream->buffer_size <= stream->init_buffer_size) |
1018 | 0 | stream->buffer_size = stream->init_buffer_size; |
1019 | 0 | else |
1020 | 0 | stream->buffer_size = nearest_power(stream->buffer_size); |
1021 | |
|
1022 | 0 | max_size = i_stream_get_max_buffer_size(&stream->istream); |
1023 | 0 | i_assert(max_size > 0); |
1024 | 0 | if (stream->buffer_size > max_size) |
1025 | 0 | stream->buffer_size = max_size; |
1026 | |
|
1027 | 0 | if (stream->buffer_size <= old_size) |
1028 | 0 | stream->buffer_size = old_size; |
1029 | 0 | else |
1030 | 0 | i_stream_w_buffer_realloc(stream, old_size); |
1031 | 0 | } |
1032 | | |
1033 | | bool i_stream_try_alloc(struct istream_private *stream, |
1034 | | size_t wanted_size, size_t *size_r) |
1035 | 0 | { |
1036 | 0 | i_assert(wanted_size > 0); |
1037 | 0 | i_assert(stream->buffer_size >= stream->pos); |
1038 | | |
1039 | 0 | if (wanted_size > stream->buffer_size - stream->pos) { |
1040 | 0 | if (stream->skip > 0) { |
1041 | | /* remove the unused bytes from beginning of buffer */ |
1042 | 0 | if (stream->memarea != NULL && |
1043 | 0 | memarea_get_refcount(stream->memarea) > 1) { |
1044 | | /* The memarea is still referenced. We can't |
1045 | | overwrite data until extra references are |
1046 | | gone. */ |
1047 | 0 | i_stream_w_buffer_realloc(stream, stream->buffer_size); |
1048 | 0 | } |
1049 | 0 | i_stream_compress(stream); |
1050 | 0 | } else if (stream->buffer_size < i_stream_get_max_buffer_size(&stream->istream)) { |
1051 | | /* buffer is full - grow it */ |
1052 | 0 | i_stream_grow_buffer(stream, I_STREAM_MIN_SIZE); |
1053 | 0 | } |
1054 | 0 | } |
1055 | |
|
1056 | 0 | if (stream->data_limit == 0 || |
1057 | 0 | (stream->buffer_size - stream->skip) < stream->data_limit) |
1058 | 0 | *size_r = stream->buffer_size - stream->pos; |
1059 | 0 | else { |
1060 | 0 | size_t buffered = (stream->pos - stream->skip); |
1061 | |
|
1062 | 0 | if (buffered >= stream->data_limit) |
1063 | 0 | *size_r = 0; |
1064 | 0 | else |
1065 | 0 | *size_r = stream->data_limit - buffered; |
1066 | 0 | } |
1067 | 0 | i_assert(stream->w_buffer != NULL || *size_r == 0); |
1068 | 0 | return *size_r > 0; |
1069 | 0 | } |
1070 | | |
1071 | | bool ATTR_NOWARN_UNUSED_RESULT |
1072 | | i_stream_try_alloc_avoid_compress(struct istream_private *stream, |
1073 | | size_t wanted_size, size_t *size_r) |
1074 | 0 | { |
1075 | 0 | size_t old_skip = stream->skip; |
1076 | | |
1077 | | /* try first with skip=0, so no compression is done */ |
1078 | 0 | stream->skip = 0; |
1079 | 0 | bool ret = i_stream_try_alloc(stream, wanted_size, size_r); |
1080 | 0 | stream->skip = old_skip; |
1081 | 0 | if (ret || old_skip == 0) |
1082 | 0 | return ret; |
1083 | | /* it's full. try with compression. */ |
1084 | 0 | return i_stream_try_alloc(stream, wanted_size, size_r); |
1085 | 0 | } |
1086 | | |
1087 | | void *i_stream_alloc(struct istream_private *stream, size_t size) |
1088 | 0 | { |
1089 | 0 | size_t old_size, avail_size; |
1090 | |
|
1091 | 0 | (void)i_stream_try_alloc(stream, size, &avail_size); |
1092 | 0 | if (avail_size < size) { |
1093 | 0 | old_size = stream->buffer_size; |
1094 | 0 | stream->buffer_size = nearest_power(stream->pos + size); |
1095 | 0 | i_stream_w_buffer_realloc(stream, old_size); |
1096 | |
|
1097 | 0 | (void)i_stream_try_alloc(stream, size, &avail_size); |
1098 | 0 | i_assert(avail_size >= size); |
1099 | 0 | } |
1100 | 0 | return stream->w_buffer + stream->pos; |
1101 | 0 | } |
1102 | | |
1103 | | void i_stream_memarea_detach(struct istream_private *stream) |
1104 | 0 | { |
1105 | 0 | if (stream->memarea != NULL) { |
1106 | | /* Don't overwrite data in a snapshot. Allocate a new |
1107 | | buffer instead. */ |
1108 | 0 | memarea_unref(&stream->memarea); |
1109 | 0 | stream->buffer_size = 0; |
1110 | 0 | stream->buffer = NULL; |
1111 | 0 | stream->w_buffer = NULL; |
1112 | 0 | } |
1113 | 0 | } |
1114 | | |
1115 | | bool i_stream_add_data(struct istream *_stream, const unsigned char *data, |
1116 | | size_t size) |
1117 | 0 | { |
1118 | 0 | struct istream_private *stream = _stream->real_stream; |
1119 | 0 | size_t size2; |
1120 | |
|
1121 | 0 | if (size == 0) |
1122 | 0 | return TRUE; |
1123 | 0 | (void)i_stream_try_alloc(stream, size, &size2); |
1124 | 0 | if (size > size2) |
1125 | 0 | return FALSE; |
1126 | | |
1127 | 0 | memcpy(stream->w_buffer + stream->pos, data, size); |
1128 | 0 | stream->pos += size; |
1129 | 0 | return TRUE; |
1130 | 0 | } |
1131 | | |
1132 | | struct istream *i_stream_get_root_io(struct istream *stream) |
1133 | 0 | { |
1134 | 0 | struct istream_private *parent; |
1135 | |
|
1136 | 0 | while ((parent = i_stream_get_io_parent(stream->real_stream)) != NULL) { |
1137 | 0 | i_assert(stream->real_stream->io == NULL); |
1138 | 0 | stream = &parent->istream; |
1139 | 0 | } |
1140 | 0 | return stream; |
1141 | 0 | } |
1142 | | |
1143 | | static void i_stream_verify_pending_reachable(struct istream *owner) |
1144 | 0 | { |
1145 | 0 | const struct istream_reader *reader; |
1146 | | |
1147 | | /* The IO may still be added to owner later on. But if an istream that |
1148 | | is currently reading it already owns one, and it can't share it with |
1149 | | owner, this pending is lost. */ |
1150 | 0 | for (reader = istream_cur_reader; reader != NULL; reader = reader->prev) { |
1151 | 0 | struct istream *root; |
1152 | |
|
1153 | 0 | if (reader->ioloop != current_ioloop) { |
1154 | | /* Nested ioloop - the outer istreams aren't reading |
1155 | | this one. */ |
1156 | 0 | break; |
1157 | 0 | } |
1158 | 0 | if (reader->stream->real_stream->hidden_inputs != |
1159 | 0 | ISTREAM_HIDDEN_INPUTS_PANIC) |
1160 | 0 | continue; |
1161 | 0 | root = i_stream_get_root_io(reader->stream); |
1162 | 0 | if (root == owner || root->real_stream->io == NULL) |
1163 | 0 | continue; |
1164 | 0 | i_panic("i_stream_set_input_pending(%s) is lost: " |
1165 | 0 | "istream %s reads it and owns the ioloop IO", |
1166 | 0 | i_stream_get_name(owner), i_stream_get_name(root)); |
1167 | 0 | } |
1168 | 0 | } |
1169 | | |
1170 | | void i_stream_set_input_pending(struct istream *stream, bool pending) |
1171 | 0 | { |
1172 | 0 | if (!pending) |
1173 | 0 | return; |
1174 | | |
1175 | 0 | stream->real_stream->io_pending_until_read = TRUE; |
1176 | |
|
1177 | 0 | stream = i_stream_get_root_io(stream); |
1178 | 0 | if (stream->real_stream->io != NULL) |
1179 | 0 | io_set_pending(stream->real_stream->io); |
1180 | 0 | else { |
1181 | 0 | i_stream_verify_pending_reachable(stream); |
1182 | 0 | stream->real_stream->io_pending = TRUE; |
1183 | 0 | } |
1184 | 0 | } |
1185 | | |
1186 | | void i_stream_switch_ioloop_to(struct istream *stream, struct ioloop *ioloop) |
1187 | 0 | { |
1188 | 0 | io_stream_switch_ioloop_to(&stream->real_stream->iostream, ioloop); |
1189 | |
|
1190 | 0 | do { |
1191 | 0 | if (stream->real_stream->switch_ioloop_to != NULL) { |
1192 | 0 | stream->real_stream->switch_ioloop_to( |
1193 | 0 | stream->real_stream, ioloop); |
1194 | 0 | } |
1195 | 0 | if (stream->real_stream->io_parent != NULL) { |
1196 | | /* Switch also the hidden input istream, which isn't |
1197 | | reached by walking the istream-parents. */ |
1198 | 0 | i_stream_switch_ioloop_to( |
1199 | 0 | stream->real_stream->io_parent, ioloop); |
1200 | 0 | } |
1201 | 0 | stream = stream->real_stream->parent; |
1202 | 0 | } while (stream != NULL); |
1203 | 0 | } |
1204 | | |
1205 | | void i_stream_switch_ioloop(struct istream *stream) |
1206 | 0 | { |
1207 | 0 | i_stream_switch_ioloop_to(stream, current_ioloop); |
1208 | 0 | } |
1209 | | |
1210 | | bool i_stream_io_ever_added(struct istream *stream) |
1211 | 0 | { |
1212 | 0 | return i_stream_get_root_io(stream)->real_stream->io_ever_added; |
1213 | 0 | } |
1214 | | |
1215 | | void i_stream_set_io(struct istream *stream, struct io *io) |
1216 | 0 | { |
1217 | 0 | stream = i_stream_get_root_io(stream); |
1218 | |
|
1219 | 0 | i_assert(stream->real_stream->io == NULL); |
1220 | 0 | stream->real_stream->io = io; |
1221 | 0 | stream->real_stream->io_ever_added = TRUE; |
1222 | 0 | if (stream->real_stream->io_pending) { |
1223 | 0 | io_set_pending(io); |
1224 | 0 | stream->real_stream->io_pending = FALSE; |
1225 | 0 | } |
1226 | 0 | } |
1227 | | |
1228 | | void i_stream_unset_io(struct istream *stream, struct io *io) |
1229 | 0 | { |
1230 | 0 | stream = i_stream_get_root_io(stream); |
1231 | |
|
1232 | 0 | i_assert(stream->real_stream->io == io); |
1233 | 0 | if (io_is_pending(io)) |
1234 | 0 | stream->real_stream->io_pending = TRUE; |
1235 | 0 | stream->real_stream->io = NULL; |
1236 | 0 | } |
1237 | | |
1238 | | static void |
1239 | | i_stream_default_set_max_buffer_size(struct iostream_private *stream, |
1240 | | size_t max_size) |
1241 | 0 | { |
1242 | 0 | struct istream_private *_stream = |
1243 | 0 | container_of(stream, struct istream_private, iostream); |
1244 | |
|
1245 | 0 | _stream->max_buffer_size = max_size; |
1246 | 0 | if (_stream->parent != NULL) |
1247 | 0 | i_stream_set_max_buffer_size(_stream->parent, max_size); |
1248 | 0 | } |
1249 | | |
1250 | | static void i_stream_default_close(struct iostream_private *stream, |
1251 | | bool close_parent) |
1252 | 0 | { |
1253 | 0 | struct istream_private *_stream = |
1254 | 0 | container_of(stream, struct istream_private, iostream); |
1255 | |
|
1256 | 0 | if (close_parent) |
1257 | 0 | i_stream_close(_stream->parent); |
1258 | 0 | } |
1259 | | |
1260 | | static void i_stream_default_destroy(struct iostream_private *stream) |
1261 | 0 | { |
1262 | 0 | struct istream_private *_stream = |
1263 | 0 | container_of(stream, struct istream_private, iostream); |
1264 | |
|
1265 | 0 | i_stream_free_buffer(_stream); |
1266 | 0 | i_stream_unref(&_stream->parent); |
1267 | 0 | } |
1268 | | |
1269 | | static void |
1270 | | i_stream_default_seek_seekable(struct istream_private *stream, |
1271 | | uoff_t v_offset, bool mark ATTR_UNUSED) |
1272 | 0 | { |
1273 | 0 | stream->istream.v_offset = v_offset; |
1274 | 0 | stream->skip = stream->pos = 0; |
1275 | 0 | } |
1276 | | |
1277 | | void i_stream_default_seek_nonseekable(struct istream_private *stream, |
1278 | | uoff_t v_offset, bool mark ATTR_UNUSED) |
1279 | 0 | { |
1280 | 0 | size_t available; |
1281 | |
|
1282 | 0 | if (stream->istream.v_offset > v_offset) |
1283 | 0 | i_panic("stream %s doesn't support seeking backwards", |
1284 | 0 | i_stream_get_name(&stream->istream)); |
1285 | | |
1286 | 0 | while (stream->istream.v_offset < v_offset) { |
1287 | 0 | (void)i_stream_read(&stream->istream); |
1288 | |
|
1289 | 0 | available = stream->pos - stream->skip; |
1290 | 0 | if (available == 0) { |
1291 | 0 | if (stream->istream.stream_errno != 0) { |
1292 | | /* read failed */ |
1293 | 0 | return; |
1294 | 0 | } |
1295 | 0 | io_stream_set_error(&stream->iostream, |
1296 | 0 | "Can't seek to offset %"PRIuUOFF_T |
1297 | 0 | ", because we have data only up to offset %" |
1298 | 0 | PRIuUOFF_T" (eof=%d)", v_offset, |
1299 | 0 | stream->istream.v_offset, stream->istream.eof ? 1 : 0); |
1300 | 0 | stream->istream.stream_errno = ESPIPE; |
1301 | 0 | return; |
1302 | 0 | } |
1303 | 0 | if (available <= v_offset - stream->istream.v_offset) |
1304 | 0 | i_stream_skip(&stream->istream, available); |
1305 | 0 | else { |
1306 | 0 | i_stream_skip(&stream->istream, |
1307 | 0 | v_offset - stream->istream.v_offset); |
1308 | 0 | } |
1309 | 0 | } |
1310 | 0 | } |
1311 | | |
1312 | | bool i_stream_nonseekable_try_seek(struct istream_private *stream, |
1313 | | uoff_t v_offset) |
1314 | 0 | { |
1315 | 0 | uoff_t start_offset = stream->istream.v_offset - stream->skip; |
1316 | |
|
1317 | 0 | if (v_offset < start_offset) { |
1318 | | /* have to seek backwards */ |
1319 | 0 | i_stream_seek(stream->parent, stream->parent_start_offset); |
1320 | 0 | stream->parent_expected_offset = stream->parent_start_offset; |
1321 | 0 | stream->skip = stream->pos = 0; |
1322 | 0 | stream->istream.v_offset = 0; |
1323 | 0 | stream->high_pos = 0; |
1324 | 0 | return FALSE; |
1325 | 0 | } |
1326 | | |
1327 | 0 | if (v_offset <= start_offset + stream->pos) { |
1328 | | /* seeking backwards within what's already cached */ |
1329 | 0 | stream->skip = v_offset - start_offset; |
1330 | 0 | stream->istream.v_offset = v_offset; |
1331 | 0 | if (stream->high_pos == 0) |
1332 | 0 | stream->high_pos = stream->pos; |
1333 | 0 | stream->pos = stream->skip; |
1334 | 0 | } else { |
1335 | | /* read forward */ |
1336 | 0 | i_stream_default_seek_nonseekable(stream, v_offset, FALSE); |
1337 | 0 | } |
1338 | 0 | return TRUE; |
1339 | 0 | } |
1340 | | |
1341 | | static int |
1342 | | seekable_i_stream_get_size(struct istream_private *stream) |
1343 | 0 | { |
1344 | 0 | if (stream->cached_stream_size == UOFF_T_MAX) { |
1345 | 0 | uoff_t old_offset = stream->istream.v_offset; |
1346 | 0 | ssize_t ret; |
1347 | |
|
1348 | 0 | do { |
1349 | 0 | i_stream_skip(&stream->istream, |
1350 | 0 | i_stream_get_data_size(&stream->istream)); |
1351 | 0 | } while ((ret = i_stream_read(&stream->istream)) > 0); |
1352 | 0 | i_assert(ret == -1); |
1353 | 0 | if (stream->istream.stream_errno != 0) |
1354 | 0 | return -1; |
1355 | | |
1356 | 0 | stream->cached_stream_size = stream->istream.v_offset; |
1357 | 0 | i_stream_seek(&stream->istream, old_offset); |
1358 | 0 | } |
1359 | 0 | stream->statbuf.st_size = stream->cached_stream_size; |
1360 | 0 | return 0; |
1361 | 0 | } |
1362 | | |
1363 | | static int |
1364 | | i_stream_default_stat(struct istream_private *stream, bool exact) |
1365 | 0 | { |
1366 | 0 | const struct stat *st; |
1367 | |
|
1368 | 0 | if (stream->parent == NULL) |
1369 | 0 | return stream->istream.stream_errno == 0 ? 0 : -1; |
1370 | | |
1371 | 0 | if (i_stream_stat(stream->parent, exact, &st) < 0) { |
1372 | 0 | stream->istream.stream_errno = stream->parent->stream_errno; |
1373 | 0 | return -1; |
1374 | 0 | } |
1375 | 0 | stream->statbuf = *st; |
1376 | 0 | if (exact && !stream->stream_size_passthrough) { |
1377 | | /* exact size is not known, even if parent returned something */ |
1378 | 0 | stream->statbuf.st_size = -1; |
1379 | 0 | if (stream->istream.seekable) { |
1380 | 0 | if (seekable_i_stream_get_size(stream) < 0) |
1381 | 0 | return -1; |
1382 | 0 | } |
1383 | 0 | } else { |
1384 | | /* When exact=FALSE always return the parent stat's size, even |
1385 | | if we know the exact value. This is necessary because |
1386 | | otherwise e.g. mbox code can see two different values and |
1387 | | think that the mbox file keeps changing. */ |
1388 | 0 | } |
1389 | 0 | return 0; |
1390 | 0 | } |
1391 | | |
1392 | | static int |
1393 | | i_stream_default_get_size(struct istream_private *stream, |
1394 | | bool exact, uoff_t *size_r) |
1395 | 0 | { |
1396 | 0 | if (stream->stat(stream, exact) < 0) |
1397 | 0 | return -1; |
1398 | 0 | if (stream->statbuf.st_size == -1) |
1399 | 0 | return 0; |
1400 | | |
1401 | 0 | *size_r = stream->statbuf.st_size; |
1402 | 0 | return 1; |
1403 | 0 | } |
1404 | | |
1405 | | void i_stream_init_parent(struct istream_private *_stream, |
1406 | | struct istream *parent) |
1407 | 0 | { |
1408 | 0 | _stream->access_counter = parent->real_stream->access_counter; |
1409 | 0 | _stream->parent = parent; |
1410 | 0 | _stream->parent_start_offset = parent->v_offset; |
1411 | 0 | _stream->parent_expected_offset = parent->v_offset; |
1412 | 0 | _stream->start_offset = parent->v_offset; |
1413 | | /* if parent stream is an istream-error, copy the error */ |
1414 | 0 | _stream->istream.stream_errno = parent->stream_errno; |
1415 | 0 | _stream->istream.eof = parent->eof; |
1416 | 0 | i_stream_ref(parent); |
1417 | 0 | } |
1418 | | |
1419 | | struct istream * |
1420 | | i_stream_create(struct istream_private *_stream, struct istream *parent, int fd, |
1421 | | enum istream_hidden_inputs hidden_inputs, |
1422 | | enum istream_create_flag flags) |
1423 | 0 | { |
1424 | 0 | bool noop_snapshot = (flags & ISTREAM_CREATE_FLAG_NOOP_SNAPSHOT) != 0; |
1425 | | |
1426 | | /* io_parent is what ISTREAM_HIDDEN_INPUTS_DECLARED declares, so one |
1427 | | without the other is a mistake in either direction. */ |
1428 | 0 | i_assert((_stream->io_parent != NULL) == |
1429 | 0 | (hidden_inputs == ISTREAM_HIDDEN_INPUTS_DECLARED)); |
1430 | 0 | _stream->hidden_inputs = hidden_inputs; |
1431 | |
|
1432 | 0 | _stream->fd = fd; |
1433 | 0 | if (parent != NULL) |
1434 | 0 | i_stream_init_parent(_stream, parent); |
1435 | 0 | else if (_stream->memarea == NULL && !noop_snapshot) { |
1436 | | /* The stream has no parent and no memarea yet. We'll assume |
1437 | | that it wants to be using memareas for the reads. */ |
1438 | 0 | _stream->memarea = memarea_init_empty(); |
1439 | 0 | } |
1440 | 0 | _stream->istream.real_stream = _stream; |
1441 | |
|
1442 | 0 | if (_stream->iostream.close == NULL) |
1443 | 0 | _stream->iostream.close = i_stream_default_close; |
1444 | 0 | if (_stream->iostream.destroy == NULL) |
1445 | 0 | _stream->iostream.destroy = i_stream_default_destroy; |
1446 | 0 | if (_stream->seek == NULL) { |
1447 | 0 | _stream->seek = _stream->istream.seekable ? |
1448 | 0 | i_stream_default_seek_seekable : |
1449 | 0 | i_stream_default_seek_nonseekable; |
1450 | 0 | } |
1451 | 0 | if (_stream->stat == NULL) |
1452 | 0 | _stream->stat = i_stream_default_stat; |
1453 | 0 | if (_stream->get_size == NULL) |
1454 | 0 | _stream->get_size = i_stream_default_get_size; |
1455 | 0 | if (_stream->snapshot == NULL) { |
1456 | 0 | _stream->snapshot = noop_snapshot ? |
1457 | 0 | i_stream_noop_snapshot : |
1458 | 0 | i_stream_default_snapshot; |
1459 | 0 | } |
1460 | 0 | if (_stream->iostream.set_max_buffer_size == NULL) { |
1461 | 0 | _stream->iostream.set_max_buffer_size = |
1462 | 0 | i_stream_default_set_max_buffer_size; |
1463 | 0 | } |
1464 | 0 | if (_stream->init_buffer_size == 0) |
1465 | 0 | _stream->init_buffer_size = I_STREAM_MIN_SIZE; |
1466 | |
|
1467 | 0 | i_zero(&_stream->statbuf); |
1468 | 0 | _stream->statbuf.st_size = -1; |
1469 | 0 | _stream->statbuf.st_atime = |
1470 | 0 | _stream->statbuf.st_mtime = |
1471 | 0 | _stream->statbuf.st_ctime = ioloop_time; |
1472 | 0 | _stream->cached_stream_size = UOFF_T_MAX; |
1473 | |
|
1474 | 0 | io_stream_init(&_stream->iostream); |
1475 | |
|
1476 | 0 | if (_stream->istream.stream_errno != 0) |
1477 | 0 | _stream->istream.eof = TRUE; |
1478 | |
|
1479 | 0 | return &_stream->istream; |
1480 | 0 | } |
1481 | | |
1482 | | struct istream *i_stream_create_error(int stream_errno) |
1483 | 0 | { |
1484 | 0 | struct istream_private *stream; |
1485 | |
|
1486 | 0 | stream = i_new(struct istream_private, 1); |
1487 | 0 | stream->istream.closed = TRUE; |
1488 | 0 | stream->istream.readable_fd = FALSE; |
1489 | 0 | stream->istream.blocking = TRUE; |
1490 | 0 | stream->istream.seekable = TRUE; |
1491 | 0 | stream->istream.eof = TRUE; |
1492 | 0 | stream->istream.stream_errno = stream_errno; |
1493 | | /* Nothing can ever actually be read from this stream, but set a |
1494 | | reasonable max_buffer_size anyway since some filter istreams don't |
1495 | | behave properly otherwise. */ |
1496 | 0 | stream->max_buffer_size = IO_BLOCK_SIZE; |
1497 | 0 | i_stream_create(stream, NULL, -1, |
1498 | 0 | ISTREAM_HIDDEN_INPUTS_NONE, 0); |
1499 | 0 | i_stream_set_name(&stream->istream, "(error)"); |
1500 | 0 | return &stream->istream; |
1501 | 0 | } |
1502 | | |
1503 | | struct istream * |
1504 | | i_stream_create_error_str(int stream_errno, const char *fmt, ...) |
1505 | 0 | { |
1506 | 0 | struct istream *input; |
1507 | 0 | va_list args; |
1508 | |
|
1509 | 0 | va_start(args, fmt); |
1510 | 0 | input = i_stream_create_error(stream_errno); |
1511 | 0 | io_stream_set_verror(&input->real_stream->iostream, fmt, args); |
1512 | | va_end(args); |
1513 | 0 | return input; |
1514 | 0 | } |