index-save.c revision 087e7957fb95f619d86db2d3d8c0664937cea419
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (C) 2002 Timo Sirainen */
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic int write_with_crlf(struct ostream *output, const unsigned char *data,
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch for (i = 0; i < size; i++) {
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch if (data[i] == '\n' && (i == 0 || data[i-1] != '\r')) {
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch /* missing CR */
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch if (o_stream_send(output, data + start, i - start) < 0)
feba5e502b2131c9a1c766b7ef9ff041dbf71d1dStephan Bosch /* \n is written next time */
7ebcb054e0d3cc4be54038cbf763ec4189d9725bStephan Bosch /* if last char is \r, leave it to buffer */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (o_stream_send(output, data + start, size - start) < 0)
f9d2a1f21ad65262bc630f0834d7eead06a1bac3Timo Sirainenstatic int write_with_lf(struct ostream *output, const unsigned char *data,
6a90041707f1290c8970a3bacb0f8f928aeaaba6Stephan Bosch for (i = 0; i < size; i++) {
6a90041707f1290c8970a3bacb0f8f928aeaaba6Stephan Bosch if (data[i] == '\n' && i > 0 && data[i-1] == '\r') {
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* \r\n - skip \r */
b72c3363092b73cab1da2de4a9d75592e7d8fd6bTimo Sirainen /* \n is written next time */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch /* if last char is \r, leave it to buffer */
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch if (o_stream_send(output, data + start, size - start) < 0)
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic void set_write_error(struct mail_storage *storage,
1ec26e0b70ac7f8a4e3dfbc59aa77f572651d5aeStephan Bosch mail_storage_set_error(storage, "Not enough disk space");
7384b4e78eaab44693c985192276e31322155e32Stephan Boschstatic void save_header_callback(struct message_part *part __attr_unused__,
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch ret = ctx->header_callback(name, name_len, ctx->write_func,
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen if (ctx->write_func(ctx->output, name, value_len) < 0) {
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen set_write_error(ctx->storage, ctx->output, ctx->path);
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainenint index_storage_save(struct mail_storage *storage, const char *path,
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen struct istream *input, struct ostream *output,
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen header_callback_t *header_callback, void *context)
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen int (*write_func)(struct ostream *, const unsigned char *, size_t);
d08e49550aa890e71a5f10b7de43347ec44473acTimo Sirainen const unsigned char *data;
7384b4e78eaab44693c985192276e31322155e32Stephan Bosch write_func = getenv("MAIL_SAVE_CRLF") ? write_with_crlf : write_with_lf;
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch /* too early */
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch "Unexpected EOF");
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch "Timeout while waiting for input");
208dcaf62332b80b220c8c66e776f7cc0c39253bStephan Bosch "Error reading mail from client: %m");