aboutsummaryrefslogtreecommitdiff
path: root/src/libmbpk.c
blob: 477dd97c85a60d03edc2fa466ba66a4f7cb31712 (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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "libmbpk.h"
#include "ini.h"
#include <string.h>
#include <sys/stat.h>

#define MBPK_DB_DIR "/var/lib/mbpk"
#define MBPK_REPO_DIR "/var/lib/mbpk-repo"
#define MBPK_REPO_CONFIG "/etc/mbpk-repos.ini"
#define MAX_REPOS 32

typedef struct {
	const char *name;
	const char *desc;
	const char *url;
	char **packages;
} mbpk_repository;

mbpk_repository *repos[MAX_REPOS];
int repo_count = 0;

static char* strdup(const char *s) {
	size_t size = strlen(s) + 1;
	char* p = (char*)malloc(size);
	if (p) {
		memcpy(p, s, size);
	}
	return p;
}

int mbpk_init(void) {
	struct stat st;
	if (stat(MBPK_DB_DIR, &st) == -1) {
		if (mkdir(MBPK_DB_DIR, 0755) == -1) {
			return MBPK_DB_FAIL;
		}
	}
	return MBPK_OK;
}

static int repo_conf_handler(void* user, const char* section, const char* name, const char* value) {
	if (!(strcmp(name, "url") == 0)) {
		return 0; /* unknown name, error */
	}

	if (repo_count >= MAX_REPOS) {
		return 0; /* too many repos */
	}

	repos[repo_count] = malloc(sizeof(mbpk_repository));
	if (repos[repo_count] == NULL) {
		return 0; /* out of memory */
	}

	repos[repo_count]->name = strdup(section);
	repos[repo_count]->desc = NULL; /* mbpk_update_repositories will fill this in later */
	repos[repo_count]->url = strdup(value);
	repos[repo_count]->packages = NULL; /* mbpk_update_repositories will fill this in later */

	if (repos[repo_count]->name == NULL || repos[repo_count]->url == NULL) {
		free((void*)repos[repo_count]->name);
		free((void*)repos[repo_count]->url);
		free(repos[repo_count]);
		repos[repo_count] = NULL;
		return 0; /* out of memory */
	}

	repo_count++;
	return 1;
}

int mbpk_update_repositories(void) {
	/* Declarations */
	int i;
	struct stat st;

	/* Parse repo config */
	repo_count = 0;
	if (ini_parse(MBPK_REPO_CONFIG, repo_conf_handler, NULL) < 0) {
		return MBPK_REPO_FAIL;
	}

	if (stat(MBPK_REPO_DIR, &st) == -1) {
		if (mkdir(MBPK_REPO_DIR, 0755) == -1) {
			return MBPK_REPO_FAIL;
		}
	}

	for (i = 0; i < repo_count; i++) {
		printf("Debug\n");
	}

	return MBPK_OK;
}