summaryrefslogtreecommitdiffstats
path: root/src/doveadm/dsync/dsync-mailbox.c
blob: d6d06fde39cbe33abff40ab8dd491d26ee10c668 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "istream.h"
#include "mail-storage-private.h"
#include "dsync-brain-private.h"
#include "dsync-mailbox.h"

void dsync_mailbox_attribute_dup(pool_t pool,
				 const struct dsync_mailbox_attribute *src,
				 struct dsync_mailbox_attribute *dest_r)
{
	dest_r->type = src->type;
	dest_r->key = p_strdup(pool, src->key);
	dest_r->value = p_strdup(pool, src->value);
	if (src->value_stream != NULL) {
		dest_r->value_stream = src->value_stream;
		i_stream_ref(dest_r->value_stream);
	}

	dest_r->deleted = src->deleted;
	dest_r->last_change = src->last_change;
	dest_r->modseq = src->modseq;
}

int dsync_mailbox_lock(struct dsync_brain *brain, struct mailbox *box,
		       struct file_lock **lock_r)
{
	const char *path, *error;
	int ret;

	/* Make sure the mailbox is open - locking requires it */
	if (mailbox_open(box) < 0) {
		i_error("Can't open mailbox %s: %s", mailbox_get_vname(box),
			mailbox_get_last_internal_error(box, &brain->mail_error));
		return -1;
	}

	ret = mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_INDEX, &path);
	if (ret < 0) {
		i_error("Can't get mailbox %s path: %s", mailbox_get_vname(box),
			mailbox_get_last_internal_error(box, &brain->mail_error));
		return -1;
	}
	if (ret == 0) {
		/* No index files - don't do any locking. In theory we still
		   could, but this lock is mainly meant to prevent replication
		   problems, and replication wouldn't work without indexes. */
		*lock_r = NULL;
		return 0;
	}

	if (mailbox_lock_file_create(box, DSYNC_MAILBOX_LOCK_FILENAME,
				     brain->mailbox_lock_timeout_secs,
				     lock_r, &error) <= 0) {
		i_error("Failed to lock mailbox %s for dsyncing: %s",
			box->vname, error);
		return -1;
	}
	return 0;
}