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

struct curl_memory {
	char* memory;
	size_t size;
};

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;
}

static size_t curl_memory_write_calback(void *contents, size_t size, size_t nmemb, void *userp) {
	size_t realsize = size * nmemb;
	struct curl_memory *mem = (struct curl_memory*)userp;

	char *ptr = realloc(mem->memory, mem->size + realsize + 1);
	if (!ptr) {
		return 0;
	}

	mem->memory = ptr;
	memcpy(&(mem->memory[mem->size]), contents, realsize);
	mem->size += realsize;
	mem->memory[mem->size] = 0;

	return realsize;
}

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 */
	struct stat st;
	int i;
	mbpk_repository *repo;
	CURL* curl;
	CURLcode result;
	char* repo_ini_url;
	struct curl_memory chunk;

	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++) {
		repo = repos[i];
		printf("[libmbpk debug] processing repo %s\n", repo->name);
		chunk.memory = malloc(1);
		chunk.size = 0;
		result = curl_global_init(CURL_GLOBAL_ALL);
		if (result != CURLE_OK) {
			return MBPK_CURL_INIT_FAIL;
		}

		curl = curl_easy_init();
		if (curl) {
			repo_ini_url = malloc(strlen(repo->url) + strlen("/repo.ini") + 1);
			if (!repo_ini_url) {
				return MBPK_OUT_OF_MEMORY;
			}
			strcpy(repo_ini_url, repo->url);
			strcat(repo_ini_url, "/repo.ini");
			printf("[libmbpk debug] URL: %s\n", repo_ini_url);
			printf("[libmbpk debug] repo url field: %s\n", repo->url);
			curl_easy_setopt(curl, CURLOPT_URL, repo_ini_url);
			curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); /* follow redirects */
			curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_memory_write_calback);
			curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&chunk);

			result = curl_easy_perform(curl);

			if (result != CURLE_OK) {
				printf("[libmbpk debug] perform cURL failed\n");
				return MBPK_CURL_PERFORM_FAIL;
			} else {
				printf("[libmbpk debug] %lu bytes received\n", (unsigned long)chunk.size);
				printf("[libmbpk debug] Response:\n%s\n", chunk.memory);
			}

			curl_easy_cleanup(curl);
		} else {
			free(chunk.memory);
			curl_global_cleanup();
			return MBPK_CURL_INIT_FAIL;
		}
		free(chunk.memory);
		curl_global_cleanup();
	}

	return MBPK_OK;
}

void mbpk_cleanup(void) {
	
}