aboutsummaryrefslogtreecommitdiffstats
path: root/config.c
blob: c02854ed9b853b441747a2977f7de24ad0e5c047 (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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (C) 2019 Daniel Borkmann <daniel@iogearbox.net> */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <wordexp.h>
#include <stdbool.h>

#include <sys/resource.h>

#include "l2md.h"

enum {
	STATE_NONE = 0,
	STATE_GENERAL,
	STATE_REPO,
	STATE_MAX,
};

static void config_dump(struct config *cfg)
{
	struct config_repo *repo;
	struct config_url *url;
	uint32_t i, j;

	if (!verbose_enabled)
		return;

	verbose("general.base = %s\n", cfg->general.base);
	verbose("general.mode = %s\n", cfg->ops->name);
	verbose("general.%s = %s\n", cfg->ops->name, cfg->general.out);
	verbose("general.period = %u\n", cfg->general.period);

	repo_for_each(cfg, repo, i) {
		verbose("repos.%s.%s = %s\n", repo->name, cfg->ops->name, repo->out);
		verbose("repos.%s.initial_import = %u\n", repo->name, repo->initial_import);
		url_for_each(repo, url, j) {
			verbose("repos.%s.url = %s\n", repo->name, url->path);
			verbose("repos.%s.oid = %s\n", repo->name,
				url->oid_known ? url->oid : "[unknown]");
		}
	}

	verbose("oneshot = %d\n", cfg->oneshot);
}

static void config_probe_oids(struct config *cfg)
{
	struct config_repo *repo;
	struct config_url *url;
	char path[PATH_MAX];
	uint32_t i, j;
	int ret;

	repo_for_each(cfg, repo, i) {
		url_for_each(repo, url, j) {
			repo_local_oid(cfg, repo, url, path, sizeof(path));
			ret = xread_file(path, url->oid, sizeof(url->oid) - 1,
					 false);
			if (!ret)
				url->oid_known = true;
		}
	}
}

static void config_set_basedir(struct config *cfg, const char *dir)
{
	wordexp_t p;

	wordexp(dir, &p, 0);
	dir = p.we_wordv[0];
	__strlcpy(cfg->general.base, dir, sizeof(cfg->general.base));
	wordfree(&p);
}

static void config_set_ops(struct config *cfg, const struct mail_ops* ops)
{
	cfg->ops = ops;
}

static void config_check_ops(struct config *cfg, const struct mail_ops* ops)
{
	if (cfg->ops != ops)
		panic("mode %s in [general] must match [repo *] mode %s\n",
		      cfg->ops->name, ops->name);
}

static void config_set_mode(struct config *cfg, const char *mode)
{
	if (!strncmp(mode, "maildir", sizeof("maildir")))
		config_set_ops(cfg, &ops_maildir);
	else if (!strncmp(mode, "pipe", sizeof("pipe")))
		config_set_ops(cfg, &ops_pipe);
	else
		panic("Unknown mode: %s!\n", mode);
}

static void config_set_out(struct config *cfg, const char *ctx, bool root)
{
	struct config_repo *repo = repo_last(cfg);
	char *out = root ? cfg->general.out : repo->out;
	wordexp_t p;

	wordexp(ctx, &p, 0);
	ctx = p.we_wordv[0];
	__strlcpy(out, ctx, sizeof(repo->out));
	wordfree(&p);
}

static void config_set_initial_import(struct config *cfg, uint32_t limit)
{
	struct config_repo *repo = repo_last(cfg);

	repo->initial_import = limit;
	if (repo->initial_import > 0)
		repo->limit = true;
}

static void config_set_repo_status(struct config *cfg, bool status)
{
	struct config_repo *repo = repo_last(cfg);

	repo->sync_enabled = status;
}

static void config_new_url(struct config *cfg, const char *git_url)
{
	struct config_repo *repo = repo_last(cfg);
	struct config_url *url;

	repo->urls_num++;
	repo->urls = xrealloc(repo->urls, sizeof(*repo->urls) *
			      repo->urls_num);
	url = url_last(repo);
	memset(url, 0, sizeof(*url));
	__strlcpy(url->path, git_url, sizeof(url->path));
}

static void config_new_repo(struct config *cfg, const char *name)
{
	struct config_repo *repo;

	cfg->repos_num++;
	cfg->repos = xrealloc(cfg->repos, sizeof(*cfg->repos) *
			      cfg->repos_num);
	repo = repo_last(cfg);
	memset(repo, 0, sizeof(*repo));
	config_set_out(cfg, cfg->general.out, false);
	config_set_repo_status(cfg, true);
	__strlcpy(repo->name, name, sizeof(repo->name));
}

static void config_set_defaults(struct config *cfg, const char *homedir)
{
	char default_data_dir[PATH_MAX];
	char *data_dir;

	cfg->general.period = 60;

	/* Default base is ~/.l2md/ if it exists, otherwise
	 * ~/${XDG_DATA_HOME}/l2md/. Fallback for empty XDG_DATA_DIR is
	 * ~/.local/share/.
	 */
	slprintf(default_data_dir, sizeof(default_data_dir), "%s/.l2md",
		 homedir);
	if (access(default_data_dir, F_OK) >= 0)
		data_dir = default_data_dir;
	else if (!(data_dir = getenv("XDG_DATA_HOME"))) {
		slprintf(default_data_dir, sizeof(default_data_dir),
			 "%s/.local/share/l2md", homedir);
		data_dir = default_data_dir;
	}

	verbose("Using base dir %s\n", default_data_dir);
	__strlcpy(cfg->general.base, data_dir, sizeof(cfg->general.base));

	config_set_ops(cfg, &ops_maildir);
	cfg->ops->set_defaults(cfg);
}

static void config_ulimits(void)
{
	struct rlimit limit;
	int ret;

	ret = getrlimit(RLIMIT_NOFILE, &limit);
	if (ret < 0)
		panic("Cannot retrieve rlimit!\n");

	/* The git repos we're dealing with can have a lot of
	 * pack files potentially, hence increase open file limit
	 * to help libgit2's revwalk.
	 *
	 * If there is a better API supported by native libgit2
	 * for doing repack, we should use that instead:
	 *
	 * https://github.com/libgit2/libgit2/issues/3247
	 */
	limit.rlim_cur = limit.rlim_max;

	ret = setrlimit(RLIMIT_NOFILE, &limit);
	if (ret < 0)
		panic("Cannot set rlimit!\n");
}

void config_uninit(struct config *cfg)
{
	struct config_repo *repo;
	uint32_t i;

	repo_for_each(cfg, repo, i)
		xfree(repo->urls);
	xfree(cfg->repos);
	xfree(cfg);
}

static FILE *config_open(const char *homedir)
{
	struct dirinfo {
		const char *env;
		const char *suffix;
		const char *fallback;
		bool prefix_home_to_fallback;
	};
	struct dirinfo dirinfo[] = {
		{ "XDG_CONFIG_HOME", "l2md/config", ".config", true, },
		{ "HOME", ".l2mdconfig", },
		{ },
	};
	struct dirinfo *cur;
	char path[PATH_MAX];
	FILE *fp;

	for (cur = dirinfo; cur->suffix; cur++) {
		char *env = getenv(cur->env);

		if (env)
			slprintf(path, sizeof(path), "%s/%s", env,
				 cur->suffix);
		else if (cur->fallback) {
			if (cur->prefix_home_to_fallback)
				slprintf(path, sizeof(path), "%s/%s/%s",
					 homedir, cur->fallback, cur->suffix);
			else
				slprintf(path, sizeof(path), "%s/%s",
					 cur->fallback, cur->suffix);
		}

		verbose("Attempting to open config file %s\n", path);
		fp = fopen(path, "r");
		if (fp)
			return fp;
		if (errno != ENOENT)
			panic("Cannot open config %s: %s\n", path, strerror(errno));
	}

	return panic("Unable to find config. Please check l2md README.\n"),
	       NULL;
}

struct config *config_init(int argc, char **argv)
{
	const char *homedir = getenv("HOME");
	char buff[1024], tmp[1024] = {};
	bool seen[STATE_MAX] = {};
	int state = STATE_NONE;
	struct config *cfg;
	FILE *fp;

	if (argc > 1) {
		if (argc == 2 && !strncmp(argv[1], "--verbose",
					  sizeof("--verbose")))
			verbose_enabled = true;
		else
			panic("Usage: %s [--verbose]\n", argv[0]);
	}

	if (!homedir)
		panic("Cannot retrieve $HOME from env!\n");

	fp = config_open(homedir);

	config_ulimits();

	cfg = xzmalloc(sizeof(*cfg));
	config_set_defaults(cfg, homedir);

	while (fgets(buff, sizeof(buff), fp)) {
		uint32_t val;

		if (buff[0] == '#' || buff[0] == '\n')
			continue;

		seen[state] = true;
		switch (state) {
		case STATE_NONE:
		state_next:
			if (!strcmp(buff, "[general]\n")) {
				state = STATE_GENERAL;
			} else if (sscanf(buff, "[repo %1023[a-z0-9-]]\n",
					  tmp) == 1) {
				state = STATE_REPO;
				config_new_repo(cfg, tmp);
			} else {
				panic("Cannot parse: '%s'\n", buff);
			}
			break;

		case STATE_GENERAL:
			if (seen[STATE_REPO]) {
				panic("[general] config must be before [repo *] config\n");
			} else if (sscanf(buff, "\tperiod = %u", &val) == 1) {
				cfg->general.period = val;
			} else if (sscanf(buff, "\tmode = %1023s", tmp) == 1) {
				config_set_mode(cfg, tmp);
			} else if (sscanf(buff, "\tmaildir = %1023s", tmp) == 1) {
				config_set_ops(cfg, &ops_maildir);
				config_set_out(cfg, tmp, true);
			} else if (sscanf(buff, "\tpipe = %1023s", tmp) == 1) {
				config_set_ops(cfg, &ops_pipe);
				config_set_out(cfg, tmp, true);
			} else if (sscanf(buff, "\tbase = %1023s", tmp) == 1) {
				config_set_basedir(cfg, tmp);
			} else if (sscanf(buff, "\toneshot = %u", &val) == 1) {
				cfg->oneshot = val;
			} else {
				goto state_next;
			}
			break;

		case STATE_REPO:
			if (sscanf(buff, "\turl = %1023s", tmp) == 1) {
				config_new_url(cfg, tmp);
			} else if (sscanf(buff, "\tmaildir = %1023s", tmp) == 1) {
				config_check_ops(cfg, &ops_maildir);
				config_set_out(cfg, tmp, false);
			} else if (sscanf(buff, "\tpipe = %1023s", tmp) == 1) {
				config_check_ops(cfg, &ops_pipe);
				config_set_out(cfg, tmp, false);
			} else if (sscanf(buff, "\tinitial_import = %u", &val) == 1) {
				config_set_initial_import(cfg, val);
			} else if (sscanf(buff, "\tsync_enabled = %u", &val) == 1) {
				config_set_repo_status(cfg, val);
			} else {
				goto state_next;
			}
			break;

		default:
			panic("Invalid parser state: %d\n", state);
		};
	}

	fclose(fp);

	config_probe_oids(cfg);
	config_dump(cfg);

	return cfg;
}