Skip to content

Commit

Permalink
lib-storage: Moved common code to index_storage_save_continue()
Browse files Browse the repository at this point in the history
  • Loading branch information
sirainen authored and GitLab committed May 12, 2016
1 parent 7c91b85 commit c68f28e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 60 deletions.
23 changes: 6 additions & 17 deletions src/lib-storage/index/cydir/cydir-save.c
Expand Up @@ -91,6 +91,7 @@ int cydir_save_begin(struct mail_save_context *_ctx, struct istream *input)
if (ctx->fd != -1) {
_ctx->data.output =
o_stream_create_fd_file(ctx->fd, 0, FALSE);
o_stream_set_name(_ctx->data.output, path);
o_stream_cork(_ctx->data.output);
} else {
mail_storage_set_critical(trans->box->storage,
Expand Down Expand Up @@ -131,27 +132,15 @@ int cydir_save_begin(struct mail_save_context *_ctx, struct istream *input)
int cydir_save_continue(struct mail_save_context *_ctx)
{
struct cydir_save_context *ctx = (struct cydir_save_context *)_ctx;
struct mail_storage *storage = &ctx->mbox->storage->storage;

if (ctx->failed)
return -1;

do {
if (o_stream_send_istream(_ctx->data.output, ctx->input) < 0) {
if (!mail_storage_set_error_from_errno(storage)) {
mail_storage_set_critical(storage,
"write(%s) failed: %m",
cydir_get_save_path(ctx, ctx->mail_count));
}
ctx->failed = TRUE;
return -1;
}
index_mail_cache_parse_continue(_ctx->dest_mail);

/* both tee input readers may consume data from our primary
input stream. we'll have to make sure we don't return with
one of the streams still having data in them. */
} while (i_stream_read(ctx->input) > 0);
if (index_storage_save_continue(_ctx, ctx->input,
_ctx->dest_mail) < 0) {
ctx->failed = TRUE;
return -1;
}
return 0;
}

Expand Down
23 changes: 6 additions & 17 deletions src/lib-storage/index/dbox-common/dbox-save.c
Expand Up @@ -7,6 +7,7 @@
#include "str.h"
#include "hex-binary.h"
#include "index-mail.h"
#include "index-storage.h"
#include "dbox-attachment.h"
#include "dbox-file.h"
#include "dbox-save.h"
Expand Down Expand Up @@ -70,30 +71,18 @@ void dbox_save_begin(struct dbox_save_context *ctx, struct istream *input)
int dbox_save_continue(struct mail_save_context *_ctx)
{
struct dbox_save_context *ctx = (struct dbox_save_context *)_ctx;
struct mail_storage *storage = _ctx->transaction->box->storage;

if (ctx->failed)
return -1;

if (_ctx->data.attach != NULL)
return index_attachment_save_continue(_ctx);

do {
if (o_stream_send_istream(_ctx->data.output, ctx->input) < 0) {
if (!mail_storage_set_error_from_errno(storage)) {
mail_storage_set_critical(storage,
"write(%s) failed: %m",
o_stream_get_name(_ctx->data.output));
}
ctx->failed = TRUE;
return -1;
}
index_mail_cache_parse_continue(_ctx->dest_mail);

/* both tee input readers may consume data from our primary
input stream. we'll have to make sure we don't return with
one of the streams still having data in them. */
} while (i_stream_read(ctx->input) > 0);
if (index_storage_save_continue(_ctx, ctx->input,
_ctx->dest_mail) < 0) {
ctx->failed = TRUE;
return -1;
}
return 0;
}

Expand Down
8 changes: 1 addition & 7 deletions src/lib-storage/index/imapc/imapc-save.c
Expand Up @@ -89,17 +89,11 @@ int imapc_save_begin(struct mail_save_context *_ctx, struct istream *input)
int imapc_save_continue(struct mail_save_context *_ctx)
{
struct imapc_save_context *ctx = (struct imapc_save_context *)_ctx;
struct mail_storage *storage = _ctx->transaction->box->storage;

if (ctx->failed)
return -1;

if (o_stream_send_istream(_ctx->data.output, ctx->input) < 0) {
if (!mail_storage_set_error_from_errno(storage)) {
mail_storage_set_critical(storage,
"o_stream_send_istream(%s) failed: %m",
ctx->temp_path);
}
if (index_storage_save_continue(_ctx, ctx->input, NULL) < 0) {
ctx->failed = TRUE;
return -1;
}
Expand Down
26 changes: 26 additions & 0 deletions src/lib-storage/index/index-storage.c
Expand Up @@ -3,6 +3,7 @@
#include "lib.h"
#include "array.h"
#include "istream.h"
#include "ostream.h"
#include "ioloop.h"
#include "str.h"
#include "mkdir-parents.h"
Expand Down Expand Up @@ -1016,3 +1017,28 @@ int index_storage_expunged_sync_begin(struct mailbox *box,
}
return 1;
}

int index_storage_save_continue(struct mail_save_context *ctx,
struct istream *input,
struct mail *cache_dest_mail)
{
struct mail_storage *storage = ctx->transaction->box->storage;

do {
if (o_stream_send_istream(ctx->data.output, input) < 0) {
if (!mail_storage_set_error_from_errno(storage)) {
mail_storage_set_critical(storage,
"write(%s) failed: %m",
o_stream_get_name(ctx->data.output));
}
return -1;
}
if (cache_dest_mail != NULL)
index_mail_cache_parse_continue(cache_dest_mail);

/* both tee input readers may consume data from our primary
input stream. we'll have to make sure we don't return with
one of the streams still having data in them. */
} while (i_stream_read(input) > 0);
return 0;
}
4 changes: 4 additions & 0 deletions src/lib-storage/index/index-storage.h
Expand Up @@ -180,4 +180,8 @@ int index_storage_expunged_sync_begin(struct mailbox *box,
enum mail_index_sync_flags flags);
void index_storage_expunging_deinit(struct mailbox *box);

int index_storage_save_continue(struct mail_save_context *ctx,
struct istream *input,
struct mail *cache_dest_mail);

#endif
26 changes: 7 additions & 19 deletions src/lib-storage/index/maildir/maildir-save.c
Expand Up @@ -440,6 +440,8 @@ int maildir_save_begin(struct mail_save_context *_ctx, struct istream *input)

if (!ctx->failed) {
_ctx->data.output = o_stream_create_fd_file(ctx->fd, 0, FALSE);
o_stream_set_name(_ctx->data.output, t_strdup_printf(
"%s/%s", ctx->tmpdir, ctx->file_last->tmp_name));
o_stream_cork(_ctx->data.output);
ctx->last_save_finished = FALSE;
}
Expand All @@ -449,29 +451,15 @@ int maildir_save_begin(struct mail_save_context *_ctx, struct istream *input)
int maildir_save_continue(struct mail_save_context *_ctx)
{
struct maildir_save_context *ctx = (struct maildir_save_context *)_ctx;
struct mail_storage *storage = &ctx->mbox->storage->storage;

if (ctx->failed)
return -1;

do {
if (o_stream_send_istream(_ctx->data.output, ctx->input) < 0) {
if (!mail_storage_set_error_from_errno(storage)) {
mail_storage_set_critical(storage,
"o_stream_send_istream(%s/%s) "
"failed: %m",
ctx->tmpdir, ctx->file_last->tmp_name);
}
ctx->failed = TRUE;
return -1;
}
if (ctx->cur_dest_mail != NULL)
index_mail_cache_parse_continue(ctx->cur_dest_mail);

/* both tee input readers may consume data from our primary
input stream. we'll have to make sure we don't return with
one of the streams still having data in them. */
} while (i_stream_read(ctx->input) > 0);
if (index_storage_save_continue(_ctx, ctx->input,
ctx->cur_dest_mail) < 0) {
ctx->failed = TRUE;
return -1;
}
return 0;
}

Expand Down

0 comments on commit c68f28e

Please sign in to comment.