aboutsummaryrefslogtreecommitdiffstats
path: root/env.c
blob: f82156241fe8b3f9d39e1e95541576e54fb1f0d2 (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
// SPDX-License-Identifier: GPL-2.0-only
/* Copyright (C) 2019 Daniel Borkmann <daniel@iogearbox.net> */

#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <unistd.h>
#include <fcntl.h>

#include <sys/stat.h>
#include <sys/types.h>

#include "l2md.h"

static void bootstrap_base(struct config *cfg)
{
	xmkdir1(cfg->general.base);
	xmkdir2(cfg->general.base, REPOS);
	xmkdir2(cfg->general.base, OIDS);
}

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

	verbose("Initial repository check.\n");

	repo_for_each(cfg, repo, i) {
		slprintf(path, sizeof(path), "%s/%s", REPOS, repo->name);
		xmkdir2(cfg->general.base, path);

		slprintf(path, sizeof(path), "%s/%s", OIDS, repo->name);
		xmkdir2(cfg->general.base, path);

		url_for_each(repo, url, j) {
			repo_local_path(cfg, repo, url, path, sizeof(path));
			ret = stat(path, &sb);
			if (ret)
				repo_clone(repo, url, path);
			else
				repo_pull(repo, url, path);
		}
	}

	verbose("Initial repository check completed.\n");
}

static void bootstrap_mail(struct config *cfg)
{
	cfg->ops->bootstrap(cfg);
}

void bootstrap_env(struct config *cfg)
{
	bootstrap_base(cfg);
	bootstrap_mail(cfg);
	bootstrap_repos(cfg);

	verbose("Bootstrap done.\n");
}

void sync_done(struct config *cfg)
{
	verbose("Sync done. ");

	if (!cfg->oneshot) {
		verbose("Sleeping %us.\n", cfg->general.period);
		sleep(cfg->general.period);
	}
}

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

	verbose("Resyncing repositories.\n");

	repo_for_each(cfg, repo, i) {
		url_for_each(repo, url, j) {
			repo_local_path(cfg, repo, url, path, sizeof(path));
			repo_pull(repo, url, path);
		}
	}
}