dsync-mailbox-tree-sync.c revision d12ea923bd1d532c6f7fee44304dde8d0f321dd0
/* Copyright (c) 2013-2015 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "buffer.h"
#include "str.h"
#include "md5.h"
#include "hex-binary.h"
#include "aqueue.h"
#include "hash.h"
#include "dsync-brain-private.h"
#include "dsync-mailbox-tree-private.h"
#define TEMP_MAX_NAME_LEN 100
#define TEMP_SUFFIX_FORMAT "temp-%x"
struct dsync_mailbox_tree_bfs_iter {
struct dsync_mailbox_tree *tree;
struct dsync_mailbox_node *cur;
};
struct dsync_mailbox_tree_sync_ctx {
struct dsync_brain *brain;
unsigned int change_idx;
};
static struct dsync_mailbox_tree_bfs_iter *
{
struct dsync_mailbox_tree_bfs_iter *iter;
return iter;
}
static bool
struct dsync_mailbox_node **node_r)
{
struct dsync_mailbox_node *const *nodep;
return FALSE;
}
return TRUE;
}
static void
{
}
static void
const struct dsync_mailbox_node *node,
{
struct dsync_mailbox_tree_sync_change *change;
const char *name;
}
static void
{
struct dsync_mailbox_tree_sync_change *change;
sizeof(change->mailbox_guid));
}
{
struct dsync_mailbox_node *const *nodes;
unsigned int i, count;
if (count == 0)
return;
for (i = 1; i < count; i++)
}
static void
struct dsync_mailbox_node *node)
{
/* for the rest of this sync assume that the mailbox has
already been deleted */
node->uid_validity = 0;
}
static void
struct dsync_mailbox_tree *tree,
{
struct dsync_mailbox_tree_sync_change *change;
const char *name;
i_debug("brain %c: Deleting mailbox '%s' (GUID %s): %s",
}
/* delete this mailbox locally */
sizeof(change->mailbox_guid));
}
}
static void
struct dsync_mailbox_tree *tree,
{
struct dsync_mailbox_tree *other_tree;
struct dsync_mailbox_node *other_node;
if (other_node == NULL) {
/* doesn't exist / already deleted */
} else {
}
}
static void
struct dsync_mailbox_tree *tree,
bool twoway_sync)
{
struct dsync_mailbox_tree_bfs_iter *iter;
}
if (twoway_sync) {
/* this mailbox was deleted. delete it from the
other side as well */
"Mailbox has been deleted");
} else {
/* treat the node as if it didn't exist. it'll
get either recreated or deleted later. in
any case this function must handle all
existence=DELETED mailbox nodes by changing
them into directories (setting GUID=0) or
we'll assert-crash later */
}
}
}
}
const struct dsync_mailbox_node *n2)
{
return FALSE;
}
}
static void
struct dsync_mailbox_node *parent)
{
struct dsync_mailbox_node **p;
if (dsync_mailbox_node_name_cmp(p, &node) > 0)
break;
}
*p = node;
}
static void
struct dsync_mailbox_node *parent)
{
/* detach from old parent */
/* attach to new parent */
}
static struct dsync_mailbox_node *
{
}
return ret;
}
static struct dsync_mailbox_node *
struct dsync_mailbox_node **pos,
struct dsync_mailbox_node *parent,
const struct dsync_mailbox_node *src)
{
struct dsync_mailbox_node *node;
return node;
}
static struct dsync_mailbox_node *
struct dsync_mailbox_tree *other_tree,
struct dsync_mailbox_node *other_node)
{
const char *parent_name;
}
{
struct dsync_mailbox_node *node;
return TRUE;
}
return FALSE;
}
static bool
{
return TRUE;
return TRUE;
}
return FALSE;
}
{
return TRUE;
}
struct dsync_mailbox_node *node)
{
const char *full_name;
return TRUE;
if (prefix_len == 0)
return FALSE;
prefix_len--;
}
static void
struct dsync_mailbox_tree *tree,
struct dsync_mailbox_node *node,
struct dsync_mailbox_node *new_parent,
const char **reason_r)
{
struct dsync_mailbox_tree_sync_change *change;
if (node->sync_temporary_name) {
/* the source name was also a temporary name. drop the
-<suffix> from it */
}
do {
/* the generated name is quite unlikely to exist,
but check anyway.. */
node->last_renamed_or_created = 0;
/* we're modifying a local tree. remember this change. */
}
}
struct dsync_mailbox_node *parent)
{
return TRUE;
}
return FALSE;
}
static void
struct dsync_mailbox_tree *tree,
struct dsync_mailbox_node *temp_node,
struct dsync_mailbox_node *node,
const struct dsync_mailbox_node *other_node,
const char **reason_r)
{
struct dsync_mailbox_tree_sync_change *change;
struct dsync_mailbox_tree *other_tree;
struct dsync_mailbox_node *parent;
const char *name, *other_name;
to other_node (in other_tree). temp_node's name is changed to
temporary name (i.e. it assumes that node's name becomes temp_node's
original name) */
other_node->parent);
/* don't introduce a loop. temporarily rename node
under root. */
return;
}
/* get the old name before it's modified */
/* set the new name */
/* change node's parent if necessary. in any case detach+reattach it
sorted, because the nodes must be sorted by name, and the node's
name (or its parent) changed. */
/* we're modifying a local tree. remember this change. */
}
}
struct dsync_mailbox_node *node2)
{
int ret;
return -1;
return 1;
sizeof(node1->mailbox_guid));
if (ret != 0)
return ret;
node2->first_child);
if (ret != 0)
return ret;
}
return 0;
}
struct dsync_mailbox_node *node2)
{
int ret;
if (ret != 0)
return ret;
node2->first_child);
if (ret != 0)
return ret;
}
return 0;
}
struct dsync_mailbox_node *node2)
{
int ret;
if (ret == 0) {
/* only a directory name changed and all the timestamps
are equal. just pick the alphabetically smaller. */
}
return ret;
}
struct dsync_mailbox_node *node2)
{
/* avoid using temporary names in case all the timestamps are 0 */
return ts;
}
struct dsync_mailbox_node *node)
{
return FALSE;
return TRUE;
}
struct dsync_mailbox_node *local_node1,
struct dsync_mailbox_node *remote_node1,
struct dsync_mailbox_node *local_node2,
struct dsync_mailbox_node *remote_node2,
const char **reason_r)
{
/* We're scanning the tree at the position of local_node1
and remote_node2. They have identical names. We also know that
local_node1&remote_node1 and local_node2&remote_node2 are "the same"
either because their GUIDs or (in case of one being a directory)
their childrens' GUIDs match. We don't know where local_node2 or
remote_node1 are located in the mailbox tree, or if they exist
at all. Note that node1 and node2 may be the same node pointers. */
*reason_r = "Can't rename namespace prefixes - will be merged later";
return FALSE;
}
/* The algorithm must be deterministic regardless of the sync direction,
so in case the timestamps are equal we need to resort to looking at
the other data. We'll start by looking at the nodes' mailbox GUIDs,
but if both of them don't exist continue looking into their
children. */
/* local nodes have a higher timestamp. we only want to do
renames where the destination parent is the current node's
(local_node1/remote_node2) parent. */
/* Numbers are GUIDs, letters are mailbox names:
local 1A <-name conflict-> remote 2A
local 2B <- potentially -> remote 1[BC]
Here we want to preserve the local 1A & 2B names: */
if (local_node2 == NULL) {
/* local : 1A
remote: 1B, 2A -> 2A-temp, 1A */
return TRUE;
} else if (remote_node1 == remote_node2) {
/* FIXME: this fixes an infinite loop when in
rename2 test, think it through why :) */
*reason_r = "local: remote_node1=remote_node2";
} else if (remote_node1 != NULL) {
/* a) local_node1->parent == local_node2->parent
local : 1A, 2B
remote: 1B, 2A -> 2A-temp, 1A(, 2B)
remote: 1C, 2A -> 2B, 1A
remote: 1C, 2A, 3B -> 2A-temp, 1A(, 3B-temp, 2B)
b) local_node1->parent != local_node2->parent
local : 1X/A, 2Y/B
We can handle all of these more easily by simply
always renaming 2 to a temporary name and handling
it when we reach B handling. */
return TRUE;
/* node2 is a parent of node1, but it should be
vice versa */
return TRUE;
/* node1 is a parent of node2, but it should be
vice versa */
return TRUE;
return TRUE;
} else {
/* local : 1A, 2B
remote: 2A -> (2B)
remote: 2A, 3B -> (3B-temp, 2B) */
*reason_r = "local: unchanged";
}
} else {
/* remote nodes have a higher timestamp */
if (remote_node1 == NULL) {
return TRUE;
} else if (local_node2 == local_node1) {
*reason_r = "remote: remote_node2=remote_node1";
} else if (local_node2 != NULL) {
return TRUE;
return TRUE;
return TRUE;
return TRUE;
} else {
*reason_r = "remote: unchanged";
}
}
return FALSE;
}
struct dsync_mailbox_node *local_node1,
struct dsync_mailbox_node *remote_node2,
const char **reason_r)
{
bool ret;
/* conflicting name, rename the one with lower timestamp */
return ret;
} else if (dsync_mailbox_node_is_dir(local_node1) ||
/* one of the nodes is a directory, and the other is a mailbox
that doesn't exist on the other side. there is no conflict,
we'll just need to create the mailbox later. */
*reason_r = "mailbox not selectable yet";
return FALSE;
} else {
/* both nodes are mailboxes that don't exist on the other side.
we'll merge these mailboxes together later and change their
GUIDs and UIDVALIDITYs to be the same */
*reason_r = "GUIDs conflict - will be merged later";
return FALSE;
}
}
static struct dsync_mailbox_node *
struct dsync_mailbox_tree *other_tree,
struct dsync_mailbox_node *dir_node)
{
if (dsync_mailbox_node_is_dir(node)) {
if (other_node != NULL)
return other_node;
} else {
guid_p);
if (other_node != NULL)
return other_node->parent;
}
}
return NULL;
}
struct dsync_mailbox_node *local_node1,
struct dsync_mailbox_node *remote_node2,
const char **reason_r)
{
/* see if we can find matching mailbox branches based on the nodes'
child mailboxes (with GUIDs). we can then rename the entire branch.
don't try to do this for namespace prefixes though. */
*reason_r = "Directory rename branch not found";
return FALSE;
}
*reason_r = "Directory name paths are equal";
return FALSE;
}
}
struct dsync_mailbox_node *local_parent,
struct dsync_mailbox_node *remote_parent)
{
const char *reason;
bool changed;
/* the nodes are sorted by their names. */
if (local_node == NULL ||
(remote_node != NULL &&
/* add a missing local node */
}
if (remote_node == NULL ||
/* add a missing remote node */
}
str_truncate(debug, 0);
(long)local_node->last_renamed_or_created,
(long)remote_node->last_renamed_or_created,
}
if (dsync_mailbox_node_is_dir(local_node) &&
/* both nodes are directories (or other side is
nonexistent). see if we can match them by their
child mailboxes */
} else if (dsync_mailbox_node_guids_equal(local_node,
remote_node)) {
/* mailboxes are equal, no need to rename */
reason = "Mailboxes are equal";
} else {
/* mailbox naming conflict */
remote_node, &reason);
}
/* handle children, if there are any */
i_debug("brain %c: %s: %s",
}
} T_END;
if (changed)
return TRUE;
}
return FALSE;
}
struct md5_context *md5)
{
sizeof(node->mailbox_guid));
return TRUE;
}
return TRUE;
}
}
return FALSE;
}
static const char *
{
struct md5_context md5;
unsigned char digest[MD5_RESULTLEN];
if (!dsync_mailbox_node_is_dir(node))
i_unreached(); /* we would have deleted it */
}
{
char *data;
unsigned int i;
*data += 1;
return;
} else if (*data == '9') {
*data = 'a';
return;
} else if (*data != 'f') {
i_unreached();
}
}
i_unreached();
}
static void
struct dsync_mailbox_node *node,
const char **reason_r)
{
const char *p, *new_suffix;
unsigned int max_prefix_len;
/* The name is currently <oldname>-<temp>. Both sides need to
use equivalent names, so we'll replace the <temp> if possible
with a) mailbox GUID, b) sha1 of childrens' (GUID|name)s. In the
very unlikely case of such name already existing, just increase
the last letters until it's not found. */
p++;
}
static void
struct dsync_mailbox_tree *tree,
struct dsync_mailbox_node *node)
{
struct dsync_mailbox_node *child;
}
}
static bool
struct dsync_mailbox_tree *tree,
struct dsync_mailbox_node *node)
{
const char *reason;
if (!node->sync_temporary_name) {
} else if (dsync_mailbox_node_is_dir(node) &&
/* we can just delete this directory and
any child directories it may have */
i_debug("brain %c: %s mailbox %s: Delete directory-only tree",
}
} else {
T_BEGIN {
i_debug("brain %c: %s mailbox %s: %s",
}
} T_END;
return TRUE;
}
}
return FALSE;
}
const struct dsync_mailbox_node *wanted_node,
const char **reason_r)
{
"Mailbox has been deleted" : "Mailbox doesn't exist";
return TRUE;
}
node->uid_validity);
return TRUE;
}
/* we can't lower the UIDNEXT */
return TRUE;
}
sizeof(node->mailbox_guid)) != 0) {
*reason_r = "GUID changed";
return TRUE;
}
return FALSE;
}
static void
struct dsync_mailbox_tree *tree,
const struct dsync_mailbox_tree *wanted_tree,
struct dsync_mailbox_node *node,
const struct dsync_mailbox_node *wanted_node)
{
const char *reason;
int ret;
}
if (ret < 0) {
/* node shouldn't exist */
"Mailbox doesn't exist");
}
} else if (ret > 0) {
/* wanted_node doesn't exist. it's created later. */
} else T_BEGIN {
} T_END;
}
/* node and its children shouldn't exist */
}
}
}
static void
struct dsync_mailbox_tree *tree,
const struct dsync_mailbox_tree *wanted_tree)
{
}
struct dsync_mailbox_tree *tree)
{
struct dsync_mailbox_tree *other_tree;
struct dsync_mailbox_tree_iter *iter;
const char *name;
/* make sure the renaming handled everything */
continue;
if (other_node == NULL)
if (dsync_mailbox_node_is_dir(other_node)) {
/* create a missing mailbox */
sizeof(other_node->mailbox_guid));
other_node->mailbox_guid)) {
/* mailbox with same name exists both locally and
remotely, but they have different GUIDs and neither
side has the other's GUID. typically this means that
both sides had autocreated some mailboxes (e.g.
INBOX). we'll just change the GUID for one of
them. */
} else {
/* existing mailbox. mismatching UIDVALIDITY is handled
later while syncing the mailbox. */
}
}
}
static void
struct dsync_mailbox_node *local_node,
struct dsync_mailbox_node *remote_node)
{
/* use local subscription state */
} else {
/* use remote subscription state */
}
}
struct dsync_mailbox_node *local_parent,
struct dsync_mailbox_node *remote_parent)
{
int ret;
/* NOTE: the nodes are always sorted. renaming created all of the
interesting nodes, but it may have left some extra nonexistent nodes
lying around, which we will delete. */
if (ret < 0) {
continue;
}
if (ret > 0) {
continue;
}
/* create to remote */
}
/* create to local */
}
/* delete from remote */
}
/* delete from local */
}
}
while (*local_nodep != NULL) {
}
while (*remote_nodep != NULL) {
}
}
{
}
static void
{
}
struct dsync_mailbox_tree_sync_ctx *
struct dsync_mailbox_tree *remote_tree,
{
struct dsync_mailbox_tree_sync_ctx *ctx;
bool changed;
1024*64);
do {
T_BEGIN {
&remote_tree->root);
} T_END;
changed) {
i_debug("brain %c: -- Mailbox renamed, restart sync --",
}
} while (changed);
/* if we're not doing a two-way sync, delete now any mailboxes, which
a) shouldn't exist, b) doesn't have a matching GUID/UIDVALIDITY,
c) has a too high UIDNEXT */
else if (sync_type == DSYNC_MAILBOX_TREES_SYNC_TYPE_PRESERVE_REMOTE)
return ctx;
}
const struct dsync_mailbox_tree_sync_change *
{
return NULL;
}
{
}