#include #include #include #include "libmbpk.h" #include "ini.h" #include #include #include #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 */ struct stat st; int i; mbpk_repository *repo; CURL* curl; CURLcode result; char* repo_ini_url; 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); result = curl_global_init(CURL_GLOBAL_ALL); if (result != CURLE_OK) { return MBPK_CURL_INIT_FAIL; } curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, repo->url); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); /* follow redirects */ result = curl_easy_perform(curl); if (result != CURLE_OK) { printf("[libmbpk debug] perform cURL failed\n"); return MBPK_CURL_PERFORM_FAIL; } curl_easy_cleanup(curl); } else { curl_global_cleanup(); return MBPK_CURL_INIT_FAIL; } curl_global_cleanup(); } return MBPK_OK; }