summaryrefslogtreecommitdiff
path: root/libdaemon/client/config-util.c
blob: e5f4e2205ceae66cc7e16b447d22bb882e27b698 (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
/*
 * Copyright (C) 2011-2012 Red Hat, Inc.
 *
 * This file is part of LVM2.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License v.2.1.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "daemon-io.h"
#include "dm-logging.h"

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

int buffer_append_vf(struct buffer *buf, va_list ap)
{
	char *append;
	char *next;
	int keylen;
	int64_t value;
	char *string;
	char *block;

	while ((next = va_arg(ap, char *))) {
		append = NULL;
		if (!strchr(next, '=')) {
			log_error(INTERNAL_ERROR "Bad format string at '%s'", next);
			goto fail;
		}
		keylen = strchr(next, '=') - next;
		if (strstr(next, "%d") || strstr(next, "%" PRId64)) {
			value = va_arg(ap, int64_t);
			if (dm_asprintf(&append, "%.*s= %" PRId64 "\n", keylen, next, value) < 0)
				goto fail;
		} else if (strstr(next, "%s")) {
			string = va_arg(ap, char *);
			if (dm_asprintf(&append, "%.*s= \"%s\"\n", keylen, next, string) < 0)
				goto fail;
		} else if (strstr(next, "%b")) {
			if (!(block = va_arg(ap, char *)))
				continue;
			if (dm_asprintf(&append, "%.*s%s", keylen, next, block) < 0)
				goto fail;
		} else if (dm_asprintf(&append, "%s", next) < 0)
			goto fail;

		if (!append ||
		    !buffer_append(buf, append))
			goto fail;

		dm_free(append);
	}

	return 1;
fail:
	dm_free(append);
	return 0;
}

int buffer_append_f(struct buffer *buf, ...)
{
	int res;
	va_list ap;

	va_start(ap, buf);
	res = buffer_append_vf(buf, ap);
	va_end(ap);

	return res;
}

int set_flag(struct dm_config_tree *cft, struct dm_config_node *parent,
	     const char *field, const char *flag, int want)
{
	struct dm_config_value *value = NULL, *pred = NULL;
	struct dm_config_node *node = dm_config_find_node(parent->child, field);
	struct dm_config_value *new;

	if (node)
		value = node->v;

	while (value && value->type != DM_CFG_EMPTY_ARRAY && strcmp(value->v.str, flag)) {
		pred = value;
		value = value->next;
	}

	if (value && want)
		return 1;

	if (!value && !want)
		return 1;

	if (value && !want) {
		if (pred) {
			pred->next = value->next;
		} else if (value == node->v && value->next) {
			node->v = value->next;
		} else {
			node->v->type = DM_CFG_EMPTY_ARRAY;
		}
	}

	if (!value && want) {
		if (!node) {
			if (!(node = dm_config_create_node(cft, field)))
				return 0;
			node->sib = parent->child;
			if (!(node->v = dm_config_create_value(cft)))
				return 0;
			node->v->type = DM_CFG_EMPTY_ARRAY;
			node->parent = parent;
			parent->child = node;
		}
		if (!(new = dm_config_create_value(cft))) {
			/* FIXME error reporting */
			return 0;
		}
		new->type = DM_CFG_STRING;
		new->v.str = flag;
		new->next = node->v;
		node->v = new;
	}

	return 1;
}

static void chain_node(struct dm_config_node *cn,
		       struct dm_config_node *parent,
		       struct dm_config_node *pre_sib)
{
	cn->parent = parent;
	cn->sib = NULL;

	if (parent && parent->child && !pre_sib) { /* find the last one */
		pre_sib = parent->child;
		while (pre_sib && pre_sib->sib)
			pre_sib = pre_sib->sib;
	}

	if (parent && !parent->child)
		parent->child = cn;
	if (pre_sib) {
		cn->sib = pre_sib->sib;
		pre_sib->sib = cn;
	}

}

struct dm_config_node *make_config_node(struct dm_config_tree *cft,
					const char *key,
					struct dm_config_node *parent,
					struct dm_config_node *pre_sib)
{
	struct dm_config_node *cn;

	if (!(cn = dm_config_create_node(cft, key)))
		return NULL;

	cn->v = NULL;
	cn->child = NULL;

	chain_node(cn, parent, pre_sib);

	return cn;
}

struct dm_config_node *make_text_node(struct dm_config_tree *cft,
				      const char *key,
				      const char *value,
				      struct dm_config_node *parent,
				      struct dm_config_node *pre_sib)
{
	struct dm_config_node *cn;

	if (!(cn = make_config_node(cft, key, parent, pre_sib)) ||
	    !(cn->v = dm_config_create_value(cft)))
		return NULL;

	cn->v->type = DM_CFG_STRING;
	cn->v->v.str = value;
	return cn;
}

struct dm_config_node *make_int_node(struct dm_config_tree *cft,
				     const char *key,
				     int64_t value,
				     struct dm_config_node *parent,
				     struct dm_config_node *pre_sib)
{
	struct dm_config_node *cn;

	if (!(cn = make_config_node(cft, key, parent, pre_sib)) ||
	    !(cn->v = dm_config_create_value(cft)))
		return NULL;

	cn->v->type = DM_CFG_INT;
	cn->v->v.i = value;
	return cn;
}

struct dm_config_node *config_make_nodes_v(struct dm_config_tree *cft,
					   struct dm_config_node *parent,
					   struct dm_config_node *pre_sib,
					   va_list ap)
{
	const char *next;
	struct dm_config_node *first = NULL;
	struct dm_config_node *cn;
	const char *fmt;
	char *key;

	while ((next = va_arg(ap, char *))) {
		cn = NULL;
		fmt = strchr(next, '=');

		if (!fmt) {
			log_error(INTERNAL_ERROR "Bad format string '%s'", fmt);
			return NULL;
		}

		if (!(key = dm_pool_strdup(cft->mem, next))) {
			log_error("Failed to duplicate node key.");
			return NULL;
		}

		key[fmt - next] = '\0';
		fmt += 2;

		if (!strcmp(fmt, "%d") || !strcmp(fmt, "%" PRId64)) {
			int64_t value = va_arg(ap, int64_t);
			if (!(cn = make_int_node(cft, key, value, parent, pre_sib)))
				return 0;
		} else if (!strcmp(fmt, "%s")) {
			char *value = va_arg(ap, char *);
			if (!(cn = make_text_node(cft, key, value, parent, pre_sib)))
				return 0;
		} else if (!strcmp(fmt, "%t")) {
			struct dm_config_tree *tree = va_arg(ap, struct dm_config_tree *);
			cn = dm_config_clone_node(cft, tree->root, 1);
			if (!cn)
				return 0;
			cn->key = key;
			chain_node(cn, parent, pre_sib);
		} else {
			log_error(INTERNAL_ERROR "Bad format string '%s'", fmt);
			return NULL;
		}
		if (!first)
			first = cn;
		if (cn)
			pre_sib = cn;
	}

	return first;
}

struct dm_config_node *config_make_nodes(struct dm_config_tree *cft,
					 struct dm_config_node *parent,
					 struct dm_config_node *pre_sib,
					 ...)
{
	struct dm_config_node *res;
	va_list ap;

	va_start(ap, pre_sib);
	res = config_make_nodes_v(cft, parent, pre_sib, ap);
	va_end(ap);

	return res;
}

int buffer_realloc(struct buffer *buf, int needed)
{
	char *new;
	int alloc = buf->allocated;
	if (alloc < needed)
		alloc = needed;

	buf->allocated += alloc;
	new = dm_realloc(buf->mem, buf->allocated);
	if (new)
		buf->mem = new;
	else { /* utter failure */
		dm_free(buf->mem);
		buf->mem = 0;
		buf->allocated = buf->used = 0;
		return 0;
	}
	return 1;
}

int buffer_append(struct buffer *buf, const char *string)
{
	int len = strlen(string);

	if ((buf->allocated - buf->used <= len) &&
	    !buffer_realloc(buf, len + 1))
                return 0;

	strcpy(buf->mem + buf->used, string);
	buf->used += len;
	return 1;
}

int buffer_line(const char *line, void *baton)
{
	struct buffer *buf = baton;
	if (!buffer_append(buf, line))
		return 0;
	if (!buffer_append(buf, "\n"))
		return 0;
	return 1;
}

void buffer_destroy(struct buffer *buf)
{
	dm_free(buf->mem);
	buffer_init(buf);
}

void buffer_init(struct buffer *buf)
{
	buf->allocated = buf->used = 0;
	buf->mem = 0;
}