From 13b607bf8468cefd24cb4d8d3593c38b43e0dd31 Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Thu, 2 Jul 2026 13:25:50 +1200 Subject: Update repo URLs and make it save cURL to a buffer --- README.md | 8 ++++---- include/libmbpk.h | 1 + src/libmbpk.c | 41 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 13d31c5..bca474d 100644 --- a/README.md +++ b/README.md @@ -52,10 +52,10 @@ Configuration format is as follows: # Format: name = url [core] -url = https://repo.yerba.arslaancodes.com/core +url = https://yerba.arslaancodes.com/repo/core [extra] -url = https://repo.yerba.arslaancodes.com/extra +url = https://yerba.arslaancodes.com/repo/extra ``` Repos will be synchronized by copying them into `/var/lib/mbpk-repo/`. @@ -82,7 +82,7 @@ core/ # Example repository name ```ini name = core desc = Core repo for Yerba Linux -url = https://repo.yerba.arslaancodes.com/core +url = https://yerba.arslaancodes.com/repo/core packages = pkgname pkgname2 (...) ``` @@ -92,7 +92,7 @@ packages = pkgname pkgname2 (...) name = pkgname version = 1.2.3 desc = testing testing 123, i like chicken nuggets, not 3 -url = https://repo.yerba.arslaancodes.com/core/files/pkgname-1.2.3.mpk +url = https://yerba.arslaancodes.com/repo/core/files/pkgname-1.2.3.mpk deps = dep1 dep2 dep3 maintainer = French Hacker license = WTFPL diff --git a/include/libmbpk.h b/include/libmbpk.h index 3e849bd..f425967 100644 --- a/include/libmbpk.h +++ b/include/libmbpk.h @@ -8,6 +8,7 @@ #define MBPK_EXTRACT_FAIL 4 #define MBPK_CURL_INIT_FAIL 5 #define MBPK_CURL_PERFORM_FAIL 6 +#define MBPK_OUT_OF_MEMORY 7 typedef struct mbpk_pkg mbpk_pkg; 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(); } -- cgit v1.2.3