index-copy.c revision 100e980900f652c9114997d7153e010e0639effe
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter/* Copyright (C) 2002 Timo Sirainen */
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#include "lib.h"
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#include "ibuffer.h"
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#include "mail-custom-flags.h"
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#include "index-storage.h"
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#include "index-messageset.h"
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter#include <unistd.h>
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Waltertypedef struct {
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter Mailbox *dest;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter const char **custom_flags;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter} CopyContext;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walterstatic int copy_func(MailIndex *index, MailIndexRecord *rec,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter unsigned int client_seq __attr_unused__,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter unsigned int idx_seq __attr_unused__, void *context)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter{
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter CopyContext *ctx = context;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter IBuffer *inbuf;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter time_t internal_date;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter int failed, deleted;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
8dc21698c4ed699801d2b6f9135b3d6cb8512917Lukas Slebodnik inbuf = index->open_mail(index, rec, &internal_date, &deleted);
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if (inbuf == NULL)
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return FALSE;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter /* save it in destination mailbox */
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter failed = !ctx->dest->save(ctx->dest, rec->msg_flags,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter ctx->custom_flags, internal_date, 0,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter inbuf, inbuf->v_size);
1203e462650f035b0df2304075d60b9a99e36715Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter i_buffer_unref(inbuf);
1203e462650f035b0df2304075d60b9a99e36715Stef Walter return !failed;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter}
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnikint index_storage_copy(Mailbox *box, Mailbox *destbox,
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik const char *messageset, int uidset)
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik{
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik IndexMailbox *ibox = (IndexMailbox *) box;
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik CopyContext ctx;
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik MailLockType lock_type;
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik int failed;
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik if (destbox->readonly) {
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik mail_storage_set_error(box->storage,
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik "Destination mailbox is read-only");
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik return FALSE;
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik }
1a94716209e40a9ccaedc7e70f9de961d1cced48Lukas Slebodnik
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter lock_type = destbox->storage == box->storage &&
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter strcmp(destbox->name, box->name) == 0 ?
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter MAIL_LOCK_EXCLUSIVE : MAIL_LOCK_SHARED;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if (!index_storage_sync_and_lock(ibox, TRUE, lock_type))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return FALSE;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter ctx.custom_flags =
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter mail_custom_flags_list_get(ibox->index->custom_flags);
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter ctx.dest = destbox;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter failed = index_messageset_foreach(ibox, messageset, uidset,
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter copy_func, &ctx) <= 0;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter if (!ibox->index->set_lock(ibox->index, MAIL_LOCK_UNLOCK))
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return mail_storage_set_index_error(ibox);
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter return !failed;
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter}
b699c4d7f85a5404be1d1ee9450331aea869b886Stef Walter