index-copy.c revision 8bd7404367404f38cc36c1edb0872915c4d06bf1
1516N/A/* Copyright (C) 2002 Timo Sirainen */
39N/A
39N/A#include "lib.h"
39N/A#include "ibuffer.h"
39N/A#include "mail-custom-flags.h"
39N/A#include "index-storage.h"
39N/A#include "index-messageset.h"
39N/A
39N/A#include <unistd.h>
39N/A
39N/Atypedef struct {
39N/A Mailbox *dest;
39N/A const char **custom_flags;
39N/A int copy_inside_mailbox;
39N/A} CopyContext;
39N/A
39N/Astatic int copy_func(MailIndex *index, MailIndexRecord *rec,
39N/A unsigned int client_seq __attr_unused__,
39N/A unsigned int idx_seq __attr_unused__, void *context)
39N/A{
39N/A CopyContext *ctx = context;
926N/A IndexMailbox *dest_ibox = NULL;
926N/A IBuffer *inbuf;
3312N/A time_t internal_date;
926N/A int failed, deleted;
39N/A
3339N/A inbuf = index->open_mail(index, rec, &internal_date, &deleted);
2453N/A if (inbuf == NULL)
3194N/A return FALSE;
3194N/A
342N/A if (ctx->copy_inside_mailbox) {
3041N/A /* kludgy.. */
1516N/A dest_ibox = (IndexMailbox *) ctx->dest;
1636N/A dest_ibox->delay_save_unlocking = TRUE;
3041N/A }
3234N/A
1386N/A /* save it in destination mailbox */
3234N/A failed = !ctx->dest->save(ctx->dest, rec->msg_flags,
2639N/A ctx->custom_flags, internal_date, 0,
3234N/A inbuf, inbuf->v_limit);
39N/A
51N/A if (ctx->copy_inside_mailbox)
2073N/A dest_ibox->delay_save_unlocking = FALSE;
2910N/A
3110N/A i_buffer_unref(inbuf);
2144N/A return !failed;
1066N/A}
1231N/A
2453N/Aint index_storage_copy(Mailbox *box, Mailbox *destbox,
1352N/A const char *messageset, int uidset)
1890N/A{
296N/A IndexMailbox *ibox = (IndexMailbox *) box;
2876N/A CopyContext ctx;
39N/A MailLockType lock_type;
3041N/A int failed;
3041N/A
3041N/A if (destbox->readonly) {
3041N/A mail_storage_set_error(box->storage,
3041N/A "Destination mailbox is read-only");
3041N/A return FALSE;
3041N/A }
3041N/A
3041N/A ctx.copy_inside_mailbox =
3234N/A destbox->storage == box->storage &&
3041N/A strcmp(destbox->name, box->name) == 0;
3041N/A
3041N/A if (ctx.copy_inside_mailbox) {
3041N/A /* copying inside same mailbox */
3041N/A if (!ibox->index->set_lock(ibox->index, MAIL_LOCK_EXCLUSIVE))
3041N/A return mail_storage_set_index_error(ibox);
3041N/A
3041N/A lock_type = MAIL_LOCK_EXCLUSIVE;
3041N/A } else {
3041N/A lock_type = MAIL_LOCK_SHARED;
3041N/A }
3041N/A
3234N/A if (!index_storage_sync_and_lock(ibox, TRUE, lock_type))
3041N/A return FALSE;
3041N/A
3041N/A ctx.custom_flags =
3041N/A mail_custom_flags_list_get(ibox->index->custom_flags);
3041N/A ctx.dest = destbox;
3041N/A
3041N/A failed = index_messageset_foreach(ibox, messageset, uidset,
3041N/A copy_func, &ctx) <= 0;
2690N/A
2690N/A if (!ibox->index->set_lock(ibox->index, MAIL_LOCK_UNLOCK))
2690N/A return mail_storage_set_index_error(ibox);
2690N/A
2690N/A return !failed;
2690N/A}
2690N/A