aboutsummaryrefslogtreecommitdiff
path: root/src/libmbpk.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libmbpk.c')
-rw-r--r--src/libmbpk.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/src/libmbpk.c b/src/libmbpk.c
index 4045fe2..982e3c8 100644
--- a/src/libmbpk.c
+++ b/src/libmbpk.c
@@ -19,6 +19,11 @@ typedef struct {
char **packages;
} mbpk_repository;
+struct curl_memory {
+ char* memory;
+ size_t size;
+};
+
mbpk_repository *repos[MAX_REPOS];
int repo_count = 0;
@@ -31,6 +36,23 @@ static char* strdup(const char *s) {
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) {
@@ -80,6 +102,7 @@ int mbpk_update_repositories(void) {
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) {
@@ -95,6 +118,8 @@ int mbpk_update_repositories(void) {
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;
@@ -102,21 +127,35 @@ int mbpk_update_repositories(void) {
curl = curl_easy_init();
if (curl) {
- curl_easy_setopt(curl, CURLOPT_URL, repo->url);
+ repo_ini_url = strdup(repo->url);
+ if (!repo_ini_url) {
+ return MBPK_OUT_OF_MEMORY;
+ }
+ 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();
}