update: new examples

This commit is contained in:
ngn 2024-07-08 07:24:39 +03:00
parent 1d5880cfa6
commit cb193d0f18
13 changed files with 130 additions and 52 deletions

View File

@ -1,11 +1,15 @@
CC = gcc CC = gcc
all: ../dist/example_client ../dist/example_server all: ../dist/client_install ../dist/client_remove ../dist/server
../dist/example_client: client/*.c ../dist/libmp.so ../dist/client_install: client/install.c client/*.h ../dist/libmp.so
mkdir -pv ../dist mkdir -pv ../dist
$(CC) -L../dist $< -lmp -o $@ $(CC) -L../dist $< -lmp -o $@
../dist/example_server: server/*.c ../dist/libmp.so ../dist/client_remove: client/remove.c client/*.h ../dist/libmp.so
mkdir -pv ../dist
$(CC) -L../dist $< -lmp -o $@
../dist/server: server/*.c ../dist/libmp.so
mkdir -pv ../dist mkdir -pv ../dist
$(CC) -L../dist $< -lmp -o $@ $(CC) -L../dist $< -lmp -o $@

4
examples/client/common.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#define DATA_DIR "/tmp/data"
#define TEMP_DIR "/tmp/temp"
#define ROOT_DIR "/tmp/root"

View File

@ -1,25 +1,22 @@
#include "../../include/all.h" #include "../../include/all.h"
#include "./common.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define DATA_DIR "/tmp/data"
#define TEMP_DIR "/tmp/temp"
#define ROOT_DIR "/tmp/root"
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int ret = EXIT_FAILURE; int ret = EXIT_FAILURE;
lm_pkg_t *pkg = NULL; lm_pkg_t *pkg = NULL;
lm_ctx_t ctx; lm_ctx_t ctx;
if (argc != 2) { if (argc != 4) {
printf("usage: %s <pool url>\n", argv[0]); printf("usage: %s <package name> <pool name> <pool url>\n", argv[0]);
return ret; return ret;
} }
if (!mkdir_ifnot(ROOT_DIR)) { if (!mkdir_ifnot(ROOT_DIR)) {
printf("failed to create root dir: %s", ROOT_DIR); printf("failed to create root dir: %s\n", ROOT_DIR);
return ret; return ret;
} }
@ -40,7 +37,7 @@ int main(int argc, char *argv[]) {
goto end; goto end;
} }
if (lm_ctx_pool_add(&ctx, "base", argv[1]) == NULL) { if (lm_ctx_pool_add(&ctx, argv[2], argv[3]) == NULL) {
printf("failed to add pool: %s (%d)\n", lm_strerror(), lm_error()); printf("failed to add pool: %s (%d)\n", lm_strerror(), lm_error());
goto end; goto end;
} }
@ -48,16 +45,20 @@ int main(int argc, char *argv[]) {
lm_ctx_ping(&ctx, NULL, NULL); lm_ctx_ping(&ctx, NULL, NULL);
lm_ctx_sync(&ctx, true, NULL, NULL); lm_ctx_sync(&ctx, true, NULL, NULL);
if ((pkg = lm_ctx_find(&ctx, "which", NULL)) == NULL) { if ((pkg = lm_ctx_pool_find(&ctx, argv[1], NULL)) == NULL) {
printf("failed to find package: %s (%d)\n", lm_strerror(), lm_error()); printf("failed to find package: %s (%d)\n", lm_strerror(), lm_error());
goto end; goto end;
} }
printf("downloading package: %s (%s)...\n", pkg->name, pkg->version);
if (!lm_ctx_download(&ctx, pkg, NULL, NULL)) { if (!lm_ctx_download(&ctx, pkg, NULL, NULL)) {
printf("failed to download package: %s (%d)\n", lm_strerror(), lm_error()); printf("failed to download package: %s (%d)\n", lm_strerror(), lm_error());
goto end; goto end;
} }
printf("installing package: %s (%s)...\n", pkg->name, pkg->version);
if (!lm_ctx_install(&ctx, pkg, NULL, NULL)) { if (!lm_ctx_install(&ctx, pkg, NULL, NULL)) {
printf("failed to install the package: %s (%d)\n", lm_strerror(), lm_error()); printf("failed to install the package: %s (%d)\n", lm_strerror(), lm_error());
goto end; goto end;

55
examples/client/remove.c Normal file
View File

@ -0,0 +1,55 @@
#include "../../include/all.h"
#include "./common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
int ret = EXIT_FAILURE;
lm_pkg_t pkg;
lm_ctx_t ctx;
if (argc != 2) {
printf("usage: %s <package name>\n", argv[0]);
return ret;
}
if (!mkdir_ifnot(ROOT_DIR)) {
printf("failed to create root dir: %s\n", ROOT_DIR);
return ret;
}
lm_ctx_init(&ctx);
if (!lm_ctx_set_data(&ctx, DATA_DIR)) {
printf("failed to set data dir: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
if (!lm_ctx_set_temp(&ctx, TEMP_DIR)) {
printf("failed to set temp dir: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
if (!lm_ctx_set_root(&ctx, ROOT_DIR)) {
printf("failed to set root dir: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
if (!lm_ctx_database_find(&ctx, &pkg, argv[1])) {
printf("failed to find package: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
if (!lm_ctx_remove(&ctx, &pkg, NULL, NULL)) {
printf("failed to remove package: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
ret = EXIT_SUCCESS;
end:
lm_ctx_free(&ctx);
return ret;
}

View File

@ -61,8 +61,6 @@ void lm_ctx_free(lm_ctx_t *ctx);
/* #################### /* ####################
## main fucntions ## ## main fucntions ##
#################### */ #################### */
lm_pkg_t *lm_ctx_find(lm_ctx_t *ctx, char *name, char *version); // find package by name (and version)
lm_ctx_resolve_list_t *lm_ctx_resolve( lm_ctx_resolve_list_t *lm_ctx_resolve(
lm_ctx_t *ctx, lm_pkg_t *pkg); // resolves a package and returns a list of packages to install lm_ctx_t *ctx, lm_pkg_t *pkg); // resolves a package and returns a list of packages to install
lm_pkg_t *lm_ctx_resolve_next(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list); // returns the next package in the list lm_pkg_t *lm_ctx_resolve_next(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list); // returns the next package in the list
@ -88,9 +86,11 @@ bool lm_ctx_serve(lm_ctx_t *ctx, char *addr, uint8_t threads);
/* #################### /* ####################
## pool fucntions ## ## pool fucntions ##
#################### */ #################### */
lm_pkg_t *lm_ctx_pool_find(lm_ctx_t *ctx, char *name, char *version); // find a package in ctx pool list
lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url); // add a pool to the ctx pool list lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url); // add a pool to the ctx pool list
bool lm_ctx_pool_del(lm_ctx_t *ctx, char *name); // remove a pool from the ctx pool list bool lm_ctx_pool_del(lm_ctx_t *ctx, char *name); // remove a pool from the ctx pool list
void lm_ctx_pool_clear(lm_ctx_t *ctx); // clear all the pools in the ctx pool list void lm_ctx_pool_clear(lm_ctx_t *ctx); // clear all the pools in the ctx pool list
lm_pool_t *lm_ctx_pool_by_name(lm_ctx_t *ctx, char *name); // find pool by name
lm_pool_t *lm_ctx_pool_by_url(lm_ctx_t *ctx, char *host, char *path); // find pool by URL lm_pool_t *lm_ctx_pool_by_url(lm_ctx_t *ctx, char *host, char *path); // find pool by URL
/* ######################## /* ########################

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-07-08 05:46+0300\n" "POT-Creation-Date: 2024-07-08 07:22+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"

View File

@ -1,22 +0,0 @@
#include "../../include/error.h"
#include "../../include/pool.h"
#include "../../include/ctx.h"
lm_pkg_t *lm_ctx_find(lm_ctx_t *ctx, char *name, char *version){
if(NULL == ctx || (NULL == name && NULL == version)){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
lm_pool_t *cur = ctx->pools;
lm_pkg_t *found = NULL;
while(cur != NULL){
if((found = lm_pool_package_find(cur, name, version)) != NULL)
break;
cur = cur->next;
}
if(NULL == found)
lm_error_set(LM_ERR_PkgNotFound);
return found;
}

View File

@ -6,6 +6,7 @@
#include <archive.h> #include <archive.h>
#include <archive_entry.h> #include <archive_entry.h>
#include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -242,6 +243,8 @@ bool lm_ctx_install(lm_ctx_t *ctx, lm_pkg_t *pkg, lm_ctx_install_callback_t call
goto end; goto end;
} }
// TODO: run install script
ret = true; ret = true;
end: end:
free(line); free(line);
@ -255,5 +258,7 @@ end:
lm_database_files_del(ctx->db, pkg); lm_database_files_del(ctx->db, pkg);
} }
unlink(pkg->paths.archive);
unlink(pkg->paths.signature);
return ret; return ret;
} }

View File

@ -7,6 +7,27 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
lm_pkg_t *lm_ctx_pool_find(lm_ctx_t *ctx, char *name, char *version){
if(NULL == ctx || (NULL == name && NULL == version)){
lm_error_set(LM_ERR_ArgNULL);
return NULL;
}
lm_pool_t *cur = ctx->pools;
lm_pkg_t *found = NULL;
while(cur != NULL){
if((found = lm_pool_package_find(cur, name, version)) != NULL)
break;
cur = cur->next;
}
if(NULL == found)
lm_error_set(LM_ERR_PkgNotFound);
return found;
}
lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url) { lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url) {
if(NULL == name){ if(NULL == name){
lm_error_set(LM_ERR_ArgNULL); lm_error_set(LM_ERR_ArgNULL);
@ -57,7 +78,7 @@ void lm_ctx_pool_clear(lm_ctx_t *ctx) {
ctx->pools = NULL; ctx->pools = NULL;
} }
lm_pool_t *lm_ctx_pool_find(lm_ctx_t *ctx, char *name) { lm_pool_t *lm_ctx_pool_by_name(lm_ctx_t *ctx, char *name) {
lm_pool_t *cur = ctx->pools; lm_pool_t *cur = ctx->pools;
while (NULL != cur) { while (NULL != cur) {
if (eq(cur->name, name)) if (eq(cur->name, name))

View File

@ -17,7 +17,9 @@ bool __lm_ctx_resolve(lm_ctx_t *ctx, lm_ctx_resolve_list_t *list, lm_pkg_t *pkg)
list->resolving = pkglist_add(list->resolving, pkg); list->resolving = pkglist_add(list->resolving, pkg);
for(int i = 0; pkg->depends[i] != NULL; i++){ for(int i = 0; pkg->depends[i] != NULL; i++){
lm_pkg_t *depend = lm_ctx_find(ctx, pkg->depends[i], NULL); // TODO: check if exists in the db before checking pools
lm_pkg_t *depend = lm_ctx_pool_find(ctx, pkg->depends[i], NULL);
if(NULL == depend){ if(NULL == depend){
lm_error_set(LM_ERR_DependNotFound, pkg->depends[i], pkg->name); lm_error_set(LM_ERR_DependNotFound, pkg->depends[i], pkg->name);
return false; return false;

View File

@ -21,10 +21,10 @@ char *queries[] = {
"INSERT INTO packages VALUES (?, ?, ?, ?, ?)", "INSERT INTO packages VALUES (?, ?, ?, ?, ?)",
// QUERY_SELECT_PACKAGE_SINGLE // QUERY_SELECT_PACKAGE_SINGLE
"SELECT * FROM packages WHERE name = '?'", "SELECT * FROM packages WHERE name = ?",
// QUERY_DELETE_PACKAGE_SINGLE // QUERY_DELETE_PACKAGE_SINGLE
"DELETE FROM packages WHERE name = '?'", "DELETE FROM packages WHERE name = ?",
// QUERY_SELECT_PACKAGE_ALL // QUERY_SELECT_PACKAGE_ALL
"SELECT * FROM packages", "SELECT * FROM packages",
@ -40,19 +40,19 @@ char *queries[] = {
"INSERT INTO files VALUES (?, ?, ?, ?)", "INSERT INTO files VALUES (?, ?, ?, ?)",
// QUERY_DELETE_FILE_ALL // QUERY_DELETE_FILE_ALL
"DELETE FROM files WHERE package = '?'", "DELETE FROM files WHERE package = ?",
// QUERY_SELECT_FILE_SINGLE // QUERY_SELECT_FILE_SINGLE
"SELECT * FROM files WHERE path = ?", "SELECT * FROM files WHERE path = ?",
// QUERY_SELECT_FILE_ALL // QUERY_SELECT_FILE_ALL
"SELECT * FROM files WHERE package = '?'", "SELECT * FROM files WHERE package = ?",
// QUERY_UPDATE_FILE_1 // QUERY_UPDATE_FILE_1
"UPDATE files SET keep = 1 WHERE path = '?'", "UPDATE files SET keep = 1 WHERE path = ?",
// QUERY_UPDATE_FILE_2 // QUERY_UPDATE_FILE_2
"UPDATE files SET keep = 0 WHERE path = '?'", "UPDATE files SET keep = 0 WHERE path = ?",
}; };
lm_database_t *lm_database_new(char *path){ lm_database_t *lm_database_new(char *path){

View File

@ -32,9 +32,10 @@ bool lm_database_package_add(lm_database_t *db, lm_pkg_t *pkg){
pdebug(__func__, "failed to convert depends to string for inserting %s: %s", pkg->name, lm_strerror()); pdebug(__func__, "failed to convert depends to string for inserting %s: %s", pkg->name, lm_strerror());
goto end; goto end;
} }
pdebug(__func__, "depend list for %s: %s", pkg->name, depends);
sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_DEPENDS, depends, strlen(depends), SQLITE_STATIC); sqlite3_bind_text(db->packages_st, PACKAGES_COLUMN_DEPENDS, depends, strlen(depends), SQLITE_STATIC);
if(sqlite3_step(db->packages_st) != SQLITE_DONE){ if(!lm_database_step_all(db->packages_st)){
pdebug(__func__, "failed to execute insert statement for inserting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db)); pdebug(__func__, "failed to execute insert statement for inserting %s: %s", pkg->name, sqlite3_errmsg(db->packages_db));
lm_error_set(LM_ERR_DbSqlInsertFail); lm_error_set(LM_ERR_DbSqlInsertFail);
goto end; goto end;

View File

@ -87,11 +87,12 @@ bool lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer){
if(i == 0){ if(i == 0){
memcpy(buffer, pkg->depends[i], depsz); memcpy(buffer, pkg->depends[i], depsz);
bufsz += depsz;
continue; continue;
} }
buffer[bufsz++] = ','; buffer[bufsz++] = ',';
memcpy(buffer+bufsz, pkg->depends, depsz); memcpy(buffer+bufsz, pkg->depends[i], depsz);
bufsz += depsz; bufsz += depsz;
} }
@ -111,9 +112,15 @@ bool lm_package_depend_fromstr(lm_pkg_t *pkg, char *buffer){
return true; return true;
char *save = NULL, *dep = NULL; char *save = NULL, *dep = NULL;
while((dep = strtok_r(buffer, ",", &save)) != NULL) dep = strtok_r(buffer, ",", &save);
if(NULL == dep)
return true;
do {
if(!lm_package_depend_add(pkg, dep)) if(!lm_package_depend_add(pkg, dep))
return false; return false;
}while((dep = strtok_r(NULL, ",", &save)) != NULL);
return true; return true;
} }