aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArslaan Pathan <[email protected]>2026-07-04 11:52:59 +1200
committerArslaan Pathan <[email protected]>2026-07-04 11:52:59 +1200
commit92ada1294f0381b29b41aaef4471c0f4585ae5e4 (patch)
treeb485892e625096ac10e0188c437bdad373b595ed
parent9326469e02f826937f70a6d3fd18845b51485331 (diff)
downloadmbpk-92ada1294f0381b29b41aaef4471c0f4585ae5e4.tar.xz
mbpk-92ada1294f0381b29b41aaef4471c0f4585ae5e4.zip
Make it fetch packages and work on making it save them to the pkg directory
-rw-r--r--src/libmbpk.c103
1 files changed, 100 insertions, 3 deletions
diff --git a/src/libmbpk.c b/src/libmbpk.c
index fe13bd5..8b21c60 100644
--- a/src/libmbpk.c
+++ b/src/libmbpk.c
@@ -1,3 +1,5 @@
+#define _POSIX_C_SOURCE 200809L
+#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
@@ -5,6 +7,7 @@
#include "ini.h"
#include <sys/stat.h>
#include <curl/curl.h>
+#include <ftw.h>
#define MBPK_DB_DIR "/var/lib/mbpk"
#define MBPK_REPO_DIR "/var/lib/mbpk-repo"
@@ -26,14 +29,33 @@ struct curl_memory {
mbpk_repository *repos[MAX_REPOS];
int repo_count = 0;
-static char* strdup(const char *s) {
+static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
+ int rv;
+
+ /* silence, compiler */
+ (void)sb;
+ (void)typeflag;
+ (void)ftwbuf;
+
+ rv = remove(fpath);
+ if (rv) {
+ perror(fpath);
+ }
+ return rv;
+}
+
+int rm_rf(const char *path) {
+ return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
+}
+
+/* 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 char **split_string(const char *str) {
char *str_copy;
@@ -145,6 +167,9 @@ int mbpk_init(void) {
}
static int repo_conf_handler(void* user, const char* section, const char* name, const char* value) {
+ /* SHUT UP COMPILER AAAAAAAAAAAAA */
+ (void)user;
+
if (!(strcmp(name, "url") == 0)) {
return 0; /* unknown name, error */
}
@@ -177,6 +202,9 @@ static int repo_conf_handler(void* user, const char* section, const char* name,
static int repo_ini_handler(void *user, const char *section, const char *name, const char *value) {
mbpk_repository *repo;
+
+ /* SILENCE YOU COMPILER */
+ (void)section;
repo = (mbpk_repository*)user;
@@ -210,8 +238,12 @@ int mbpk_update_repositories(void) {
char *repo_ini_url;
char *repo_ini_path;
char *repo_dir;
+ char *pkg_ini_url;
+ char *pkg_dir;
size_t dir_len;
size_t repo_ini_path_len;
+ size_t pkg_ini_url_len;
+ size_t pkg_dir_len;
FILE *fp;
struct curl_memory chunk;
@@ -243,6 +275,8 @@ int mbpk_update_repositories(void) {
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);
+ curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); /* fail on errors like 404 */
+ curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, ""); /* accept encoding like gzip */
result = curl_easy_perform(curl);
curl_easy_cleanup(curl);
@@ -297,7 +331,6 @@ int mbpk_update_repositories(void) {
fwrite(chunk.memory, 1, chunk.size, fp);
fclose(fp);
free(chunk.memory);
- free(repo_dir);
if (ini_parse(repo_ini_path, repo_ini_handler, (void*)repo) < 0) {
free(repo_ini_path);
@@ -306,8 +339,72 @@ int mbpk_update_repositories(void) {
free(repo_ini_path);
printf("[libmbpk debug] repo %s desc: %s\n", repo->name, repo->desc);
printf("[libmbpk debug] repo %s packages:\n", repo->name);
+
+ pkg_dir_len = strlen(repo_dir) + 1 + strlen("pkg") + 1;
+ pkg_dir = malloc(pkg_dir_len);
+ if (!pkg_dir) {
+ return MBPK_OUT_OF_MEMORY;
+ }
+ sprintf(pkg_dir, "%s/pkg", repo_dir);
+
+ if (stat(pkg_dir, &st) == -1) {
+ printf("[libmbpk debug] removing old pkg directory for %s\n", repo->name);
+ if (rm_rf(pkg_dir) != 0) {
+ free(pkg_dir);
+ free(repo_dir);
+ return MBPK_REPO_FAIL;
+ }
+ }
+
for (i = 0; repo->packages[i] != NULL; i++) {
+ chunk.memory = malloc(1);
+ if (!chunk.memory) {
+ free(pkg_dir);
+ free(repo_dir);
+ return MBPK_OUT_OF_MEMORY;
+ }
+ chunk.size = 0; /* oh fuck, thats why this didnt work - i was overwriting broken shit */
printf(" %s\n", repo->packages[i]);
+ curl = curl_easy_init();
+ if (curl) {
+ pkg_ini_url_len = strlen(repo->url) + 1 + strlen("pkg") + 1 + strlen(repo->packages[i]) + strlen(".ini") + 1;
+ pkg_ini_url = malloc(pkg_ini_url_len);
+ if (!pkg_ini_url) {
+ free(repo_dir);
+ return MBPK_OUT_OF_MEMORY;
+ }
+ sprintf(pkg_ini_url, "%s/pkg/%s.ini", repo->url, repo->packages[i]);
+ curl_easy_setopt(curl, CURLOPT_URL, pkg_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);
+ curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); /* fail on errors like 404 */
+ curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, ""); /* accept encoding like gzip */
+
+ result = curl_easy_perform(curl);
+ curl_easy_cleanup(curl);
+ free(pkg_ini_url);
+
+ if (result != CURLE_OK) {
+ printf("[libmbpk debug] perform cURL failed\n");
+ free(chunk.memory);
+ free(pkg_dir);
+ free(repo_dir);
+ 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);
+ }
+ } else {
+ free(chunk.memory);
+ free(repo_dir);
+ free(pkg_dir);
+ return MBPK_CURL_INIT_FAIL;
+ }
+
+ free(chunk.memory);
+ free(pkg_dir);
+ free(repo_dir);
}
}