From 1bf9126f08b19fd8e3a8ee57cf55731185791f43 Mon Sep 17 00:00:00 2001 From: Arslaan Pathan Date: Tue, 23 Jun 2026 18:36:15 +1200 Subject: Get the parsing of repo config file working --- README.md | 1 + include/libmbpk.h | 2 ++ meson.build | 14 ++++++++++-- src/libmbpk.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++------ src/mbpk-install.c | 15 +++++++++++-- 5 files changed, 84 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b3a76ab..64cbf87 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,7 @@ core/ # Example repository name name = core desc = Core repo for Yerba Linux url = https://repo.yerba.arslaancodes.com/core +packages = pkgname pkgname2 (...) ``` #### pkg/pkgname.ini diff --git a/include/libmbpk.h b/include/libmbpk.h index 319e464..73a5806 100644 --- a/include/libmbpk.h +++ b/include/libmbpk.h @@ -4,6 +4,8 @@ #define MBPK_OK 0 #define MBPK_DB_FAIL 1 #define MBPK_REPO_FAIL 2 +#define MBPK_DL_FAIL 3 +#define MBPK_EXTRACT_FAIL 4 typedef struct mbpk_pkg mbpk_pkg; diff --git a/meson.build b/meson.build index f9b6369..8f89072 100644 --- a/meson.build +++ b/meson.build @@ -1,4 +1,14 @@ -project('mbpk', 'c') +project('mbpk', 'c', default_options: ['c_std=c89', 'warning_level=3']) + +add_project_arguments( + '-pedantic-errors', + '-Werror', + '-Wshadow', + '-Wconversion', + '-Wsign-conversion', + '-Wno-cast-qual', + language : 'c' +) inc = include_directories('include') @@ -26,7 +36,7 @@ executable('mbpk-install', 'src/mbpk-install.c', dependencies: deps, install: tr pkg = import('pkgconfig') pkg.generate(libmbpk, subdirs: 'libmbpk', - name: 'libmbpk', + name: 'mbpk', filebase: 'libmbpk', description: 'Minimal Binary Package Kit') 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 +#include #include #include "libmbpk.h" +#include "ini.h" #include #include #include @@ -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; } diff --git a/src/mbpk-install.c b/src/mbpk-install.c index da7a39e..5ff555a 100644 --- a/src/mbpk-install.c +++ b/src/mbpk-install.c @@ -2,7 +2,18 @@ #include "libmbpk.h" int main(void) { + int res; + printf("[mbpk_install] please note that mbpk_install is currently used as a test, and does not do what is said in the README. the proper wrapper binaries will be created once libmbpk is finished.\n"); printf("[mbpk_install] calling mbpk_init()\n"); - int result = mbpk_init(); - printf("result: %d", result); + res = mbpk_init(); + if (res != MBPK_OK) { + fprintf(stderr, "mbpk init failed: %d\n", res); + return 1; + } + printf("[mbpk_install] calling mbpk_update_repositories()\n"); + res = mbpk_update_repositories(); + if (res != MBPK_OK) { + fprintf(stderr, "mbpk repo failed: %d\n", res); + } + return 0; } -- cgit v1.2.3