aboutsummaryrefslogtreecommitdiff
path: root/src/libmbpk.c
diff options
context:
space:
mode:
authorArslaan Pathan <[email protected]>2026-06-23 18:36:15 +1200
committerArslaan Pathan <[email protected]>2026-06-23 18:36:15 +1200
commit1bf9126f08b19fd8e3a8ee57cf55731185791f43 (patch)
tree83638c200e8418646dd3a175cd8880ed439e2fd1 /src/libmbpk.c
parentc7842a6c3a89b5ddbd7ac8a003339d8a15d3cf12 (diff)
downloadmbpk-1bf9126f08b19fd8e3a8ee57cf55731185791f43.tar.xz
mbpk-1bf9126f08b19fd8e3a8ee57cf55731185791f43.zip
Get the parsing of repo config file working
Diffstat (limited to 'src/libmbpk.c')
-rw-r--r--src/libmbpk.c63
1 files changed, 56 insertions, 7 deletions
diff --git a/src/libmbpk.c b/src/libmbpk.c
index dd93565..f0fb6f9 100644
--- a/src/libmbpk.c
+++ b/src/libmbpk.c
@@ -1,6 +1,8 @@
#include <stdio.h>
+#include <string.h>
#include <stdlib.h>
#include "libmbpk.h"
+#include "ini.h"
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
@@ -8,25 +10,72 @@
#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;
+ const char *name;
+ const char *desc;
+ const char *url;
+ char **packages;
} mbpk_repository;
-mbpk_repository **repos;
+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) {
- fprintf(stderr, "mbpk [err]: failed to create %s: %s\n", MBPK_DB_DIR, strerror(errno));
return MBPK_DB_FAIL;
}
}
- if (stat(MBPK_REPO_DIR, &st) == -1) {
- fprintf(stderr, "mbpk [warn]: no repositories found! you might want to sync all repositories using mbpk-update.\n");
+ 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) {
+ repo_count = 0;
+ if (ini_parse(MBPK_REPO_CONFIG, repo_conf_handler, NULL) < 0) {
+ return MBPK_REPO_FAIL;
}
return MBPK_OK;
}