summaryrefslogtreecommitdiffstats
path: root/src/lib-storage/mailbox-tree.c
blob: 9333fb61399a669e602a7f878d8f2d31c89ccef4 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/* Copyright (c) 2003-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "array.h"
#include "str.h"
#include "mailbox-tree.h"

struct mailbox_tree_context {
	pool_t pool;
	char separator;
	bool parents_nonexistent;
	bool sorted;
	unsigned int node_size;

	struct mailbox_node *nodes;
};

struct mailbox_tree_iterate_context {
	struct mailbox_node *root, *next_node;
	unsigned int flags_mask;

	char separator;

	ARRAY(struct mailbox_node *) node_path;
	string_t *path_str;
	size_t parent_pos;

	bool first_child:1;
};

struct mailbox_tree_context *mailbox_tree_init(char separator)
{
	return mailbox_tree_init_size(separator, sizeof(struct mailbox_node));
}

struct mailbox_tree_context *
mailbox_tree_init_size(char separator, unsigned int mailbox_node_size)
{
	struct mailbox_tree_context *tree;

	i_assert(mailbox_node_size >= sizeof(struct mailbox_node));

	tree = i_new(struct mailbox_tree_context, 1);
	tree->pool = pool_alloconly_create(MEMPOOL_GROWING"mailbox_tree", 10240);
	tree->separator = separator;
	tree->node_size = mailbox_node_size;
	return tree;
}

void mailbox_tree_deinit(struct mailbox_tree_context **_tree)
{
	struct mailbox_tree_context *tree = *_tree;

	*_tree = NULL;
	pool_unref(&tree->pool);
	i_free(tree);
}

void mailbox_tree_set_separator(struct mailbox_tree_context *tree,
				char separator)
{
	tree->separator = separator;
}

void mailbox_tree_set_parents_nonexistent(struct mailbox_tree_context *tree)
{
	tree->parents_nonexistent = TRUE;
}

void mailbox_tree_clear(struct mailbox_tree_context *tree)
{
	p_clear(tree->pool);
	tree->nodes = NULL;
}

pool_t mailbox_tree_get_pool(struct mailbox_tree_context *tree)
{
	return tree->pool;
}

static struct mailbox_node * ATTR_NULL(2)
mailbox_tree_traverse(struct mailbox_tree_context *tree, const char *path,
		      bool create, bool *created_r)
{
	struct mailbox_node **node, *parent;
	const char *name;
	string_t *str;

	*created_r = FALSE;

	if (path == NULL)
		return tree->nodes;

	if (strncasecmp(path, "INBOX", 5) == 0 &&
	    (path[5] == '\0' || path[5] == tree->separator))
		path = t_strdup_printf("INBOX%s", path+5);

	parent = NULL;
	node = &tree->nodes;

	str = t_str_new(strlen(path)+1);
	for (name = path;; path++) {
		if (*path != tree->separator && *path != '\0')
			continue;

		str_truncate(str, 0);
		str_append_data(str, name, (size_t) (path - name));
		name = str_c(str);

		/* find the node */
		while (*node != NULL) {
			if (strcmp((*node)->name, name) == 0)
				break;

			node = &(*node)->next;
		}

		if (*node == NULL) {
			/* not found, create it */
			if (!create)
				break;

			*node = p_malloc(tree->pool, tree->node_size);
			(*node)->parent = parent;
			(*node)->name = p_strdup(tree->pool, name);
			if (tree->parents_nonexistent)
				(*node)->flags = MAILBOX_NONEXISTENT;
			tree->sorted = FALSE;
			*created_r = TRUE;
		}

		if (*path == '\0')
			break;

		name = path+1;

		parent = *node;
		node = &(*node)->children;
	}

	return *node;
}

struct mailbox_node *
mailbox_tree_get(struct mailbox_tree_context *tree, const char *path,
		 bool *created_r)
{
	struct mailbox_node *node;
	bool created;

	T_BEGIN {
		node = mailbox_tree_traverse(tree, path, TRUE, &created);
	} T_END;
	if (created && tree->parents_nonexistent)
		node->flags = 0;

	*created_r = created;
	return node;
}

struct mailbox_node *
mailbox_tree_lookup(struct mailbox_tree_context *tree, const char *path)
{
	struct mailbox_node *node;
	bool created;

	i_assert(tree != NULL);

	T_BEGIN {
		node = mailbox_tree_traverse(tree, path, FALSE, &created);
	} T_END;
	return node;
}

struct mailbox_tree_iterate_context *
mailbox_tree_iterate_init(struct mailbox_tree_context *tree,
			  struct mailbox_node *root, unsigned int flags_mask)
{
	struct mailbox_tree_iterate_context *ctx;

	ctx = i_new(struct mailbox_tree_iterate_context, 1);
	ctx->separator = tree->separator;
	ctx->root = root != NULL ? root : tree->nodes;
	ctx->flags_mask = flags_mask;
	ctx->path_str = str_new(default_pool, 256);
	i_array_init(&ctx->node_path, 16);

	ctx->next_node = ctx->root;
	return ctx;
}

static void
mailbox_tree_iterate_set_next_node(struct mailbox_tree_iterate_context *ctx)
{
	struct mailbox_node *node = ctx->next_node;
	struct mailbox_node *const *nodes;
	unsigned int i, count;

	if (node->children != NULL) {
		array_push_back(&ctx->node_path, &node);
		ctx->parent_pos = str_len(ctx->path_str);
		node = node->children;
		ctx->first_child = TRUE;
	} else if (node->next != NULL) {
		node = node->next;
	} else {
		nodes = array_get(&ctx->node_path, &count);
		node = NULL;
		for (i = count; i != 0; i--) {
			size_t len = strlen(nodes[i-1]->name) + 1;

			i_assert(len <= ctx->parent_pos);
			ctx->parent_pos -= len;
			if (nodes[i-1]->next != NULL) {
				node = nodes[i-1]->next;
				ctx->first_child = TRUE;
				i--;

				if (ctx->parent_pos != 0)
					ctx->parent_pos--;
				break;
			}
		}
		array_delete(&ctx->node_path, i, count - i);
	}

	ctx->next_node = node;
}

struct mailbox_node *
mailbox_tree_iterate_next(struct mailbox_tree_iterate_context *ctx,
			  const char **path_r)
{
	struct mailbox_node *node;

	do {
		node = ctx->next_node;
		if (node == NULL)
			return NULL;

		str_truncate(ctx->path_str, ctx->parent_pos);
		if (ctx->first_child) {
			ctx->first_child = FALSE;
			if (node->parent != NULL) {
				str_append_c(ctx->path_str, ctx->separator);
				ctx->parent_pos++;
			}
		}
		str_append(ctx->path_str, node->name);

		mailbox_tree_iterate_set_next_node(ctx);
	} while ((node->flags & ctx->flags_mask) != ctx->flags_mask);

	*path_r = str_c(ctx->path_str);
	return node;
}

void mailbox_tree_iterate_deinit(struct mailbox_tree_iterate_context **_ctx)
{
	struct mailbox_tree_iterate_context *ctx = *_ctx;

	*_ctx = NULL;
	str_free(&ctx->path_str);
	array_free(&ctx->node_path);
	i_free(ctx);
}

static struct mailbox_node * ATTR_NULL(1, 2)
mailbox_tree_dup_branch(struct mailbox_tree_context *dest_tree,
			struct mailbox_node *dest_parent,
			const struct mailbox_node *src)
{
	struct mailbox_node *node, *dest_nodes = NULL, **dest = &dest_nodes;

	for (; src != NULL; src = src->next) {
		*dest = node = p_malloc(dest_tree->pool, dest_tree->node_size);
		node->name = p_strdup(dest_tree->pool, src->name);
		node->flags = src->flags;

		node->parent = dest_parent;
		node->children = mailbox_tree_dup_branch(dest_tree, node,
							 src->children);
		dest = &node->next;
	}
	return dest_nodes;
}

struct mailbox_tree_context *mailbox_tree_dup(struct mailbox_tree_context *src)
{
	struct mailbox_tree_context *dest;

	/* for now we don't need to support extra data */
	i_assert(src->node_size == sizeof(struct mailbox_node));

	dest = mailbox_tree_init_size(src->separator, src->node_size);
	dest->nodes = mailbox_tree_dup_branch(dest, NULL, src->nodes);
	return dest;
}

static int mailbox_node_name_cmp(struct mailbox_node *const *node1,
				 struct mailbox_node *const *node2)
{
	return strcmp((*node1)->name, (*node2)->name);
}

static void mailbox_tree_sort_branch(struct mailbox_node **nodes,
				     ARRAY_TYPE(mailbox_node) *tmparr)
{
	struct mailbox_node *node, **dest;

	if (*nodes == NULL)
		return;

	/* first put the nodes into an array and sort it */
	array_clear(tmparr);
	for (node = *nodes; node != NULL; node = node->next)
		array_push_back(tmparr, &node);
	array_sort(tmparr, mailbox_node_name_cmp);

	/* update the node pointers */
	dest = nodes;
	array_foreach_elem(tmparr, node) {
		*dest = node;
		dest = &(*dest)->next;
	}
	*dest = NULL;

	/* sort the children */
	for (node = *nodes; node != NULL; node = node->next)
		mailbox_tree_sort_branch(&node->children, tmparr);
}

void mailbox_tree_sort(struct mailbox_tree_context *tree)
{
	if (tree->sorted)
		return;
	tree->sorted = TRUE;

	T_BEGIN {
		ARRAY_TYPE(mailbox_node) tmparr;

		t_array_init(&tmparr, 32);
		mailbox_tree_sort_branch(&tree->nodes, &tmparr);
	} T_END;
}