new: first database functions

This commit is contained in:
ngn 2024-07-02 04:44:07 +03:00
parent f50e5c42b4
commit 16992760ad
25 changed files with 693 additions and 197 deletions

View File

@ -8,8 +8,8 @@ PO_DIRS = $(wildcard locale/*/*)
SRCS = $(wildcard src/*.c) $(wildcard src/*/*.c) SRCS = $(wildcard src/*.c) $(wildcard src/*/*.c)
OBJS = $(patsubst src/%.c,dist/%.o,$(SRCS)) OBJS = $(patsubst src/%.c,dist/%.o,$(SRCS))
HDRS = $(wildcard include/*.h) HDRS = $(wildcard include/*.h)
CFLAGS = -O3 -march=native -fstack-protector-strong -fcf-protection=full -fstack-clash-protection CFLAGS = -O3 -fstack-protector-strong -fcf-protection=full -fstack-clash-protection
LIBS = -larchive -linih -lgpgme LIBS = -lpthread -larchive -linih -lgpgme -lsqlite3
DEBUG = 0 DEBUG = 0
VERSION = 24.00 VERSION = 24.00
@ -17,7 +17,6 @@ VERSION = 24.00
all: dist/libmp.so $(PO_OUTS) all: dist/libmp.so $(PO_OUTS)
dist/libmp.so: $(OBJS) dist/libmp.so: $(OBJS)
mkdir -p dist
$(CC) -shared -o $@ $^ $(LIBS) $(CFLAGS) $(CC) -shared -o $@ $^ $(LIBS) $(CFLAGS)
dist/%.o: src/%.c dist/%.o: src/%.c
@ -25,6 +24,7 @@ dist/%.o: src/%.c
mkdir -p dist/mptp mkdir -p dist/mptp
mkdir -p dist/pool mkdir -p dist/pool
mkdir -p dist/package mkdir -p dist/package
mkdir -p dist/database
$(CC) -c -Wall -fPIC -o $@ $^ $(LIBS) $(CFLAGS) -DLM_VERSION=\"${VERSION}\" -DLM_DEBUG=${DEBUG} $(CC) -c -Wall -fPIC -o $@ $^ $(LIBS) $(CFLAGS) -DLM_VERSION=\"${VERSION}\" -DLM_DEBUG=${DEBUG}
locale/%.mo: locale/%.po locale/%.mo: locale/%.po

View File

@ -35,6 +35,11 @@ int main(int argc, char *argv[]) {
goto end; goto end;
} }
if (!lm_ctx_package_verify(&ctx, "which", NULL)) {
printf("failed to verify package: %s (%d)\n", lm_strerror(), lm_error());
goto end;
}
ret = EXIT_SUCCESS; ret = EXIT_SUCCESS;
end: end:

View File

@ -1,8 +1,9 @@
#pragma once #pragma once
#include "database.h"
#include "types.h" #include "types.h"
#include <stdbool.h> #include <stdbool.h>
typedef void (*lm_ctx_pool_callback_t)(lm_ctx_t *ctx, lm_pool_t *pool, bool status, void *data); typedef bool (*lm_ctx_pool_callback_t)(lm_ctx_t *ctx, lm_pool_t *pool, bool status, void *data);
void lm_ctx_init(lm_ctx_t *ctx); void lm_ctx_init(lm_ctx_t *ctx);
bool lm_ctx_set_data(lm_ctx_t *ctx, char *dir); bool lm_ctx_set_data(lm_ctx_t *ctx, char *dir);
@ -22,3 +23,6 @@ void lm_ctx_pool_get_list(
lm_pkg_t *lm_ctx_package_install(lm_ctx_t *ctx, char *name, char *version); lm_pkg_t *lm_ctx_package_install(lm_ctx_t *ctx, char *name, char *version);
lm_pkg_t *lm_ctx_package_get(lm_ctx_t *ctx, char *name, char *version); lm_pkg_t *lm_ctx_package_get(lm_ctx_t *ctx, char *name, char *version);
bool lm_ctx_package_verify(lm_ctx_t *ctx, char *name, char *version);
lm_database_t *lm_ctx_database_new(lm_ctx_t *ctx);

36
include/database.h Normal file
View File

@ -0,0 +1,36 @@
#pragma once
#include "package.h"
#include <sqlite3.h>
typedef enum lm_query_index {
QUERY_CREATE_TABLE = 0,
QUERY_INSERT_PACKAGE = 1,
} lm_query_index_t;
extern char *queries[];
typedef struct lm_database {
lm_pkg_t *pkg;
char *path;
sqlite3 *sql;
} lm_database_t;
typedef bool (*lm_database_files_eachfunc_t)(lm_pkg_t *pkg, char *path, char *hash, void *data);
typedef bool (*lm_database_keeps_eachfunc_t)(lm_pkg_t *pkg, char *path, void *data);
lm_database_t *lm_database_new(char *path);
void lm_database_free(lm_database_t *db);
bool lm_database_add(lm_database_t *db, lm_pkg_t *pkg);
bool lm_database_del(lm_database_t *db, lm_pkg_t *pkg);
bool lm_database_find(lm_database_t *db, lm_pkg_t *pkg);
bool lm_database_files_del(lm_database_t *db, lm_pkg_t *pkg);
bool lm_database_files_add(lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash);
bool lm_database_files_get(lm_database_t *db, lm_pkg_t *pkg, char *path, char *hash);
bool lm_database_files_foreach(lm_database_t *db, lm_pkg_t *pkg, lm_database_files_eachfunc_t func);
bool lm_database_keeps_del(lm_database_t *db, lm_pkg_t *pkg);
bool lm_database_keeps_add(lm_database_t *db, lm_pkg_t *pkg, char *path);
bool lm_database_keeps_contains(lm_database_t *db, lm_pkg_t *pkg, char *path);
bool lm_database_keeps_foreach(lm_database_t *db, lm_pkg_t *pkg, lm_database_keeps_eachfunc_t func);

View File

@ -59,13 +59,28 @@ typedef enum lm_error {
LM_ERR_CtxDataFailMkdir = 54, LM_ERR_CtxDataFailMkdir = 54,
LM_ERR_ArcRealpathFail = 55, LM_ERR_ArcRealpathFail = 55,
LM_ERR_PoolTestNotPong = 56, LM_ERR_PoolTestNotPong = 56,
LM_ERR_PackagePathsEmpty = 57, LM_ERR_PkgPathsEmpty = 57,
LM_ERR_SendOpenFail = 58, LM_ERR_SendOpenFail = 58,
LM_ERR_RecvDelFail = 59, LM_ERR_RecvDelFail = 59,
LM_ERR_RecvOpenFail = 60, LM_ERR_RecvOpenFail = 60,
LM_ERR_RecvBadCode = 61, LM_ERR_RecvBadCode = 61,
LM_ERR_RecvWriteFail = 62, LM_ERR_RecvWriteFail = 62,
LM_ERR_PkgNotFound = 63 LM_ERR_PkgNotFound = 63,
LM_ERR_DbCantAccess = 64,
LM_ERR_DbSqlOpenFail = 65,
LM_ERR_DbSqlCreateFail = 66,
LM_ERR_DbSqlPrepareFail = 67,
LM_ERR_DbSqlInsertFail = 68,
LM_ERR_PkgGPGFail = 69,
LM_ERR_PkgGPGSigFail = 70,
LM_ERR_PkgGPGArchiveFail = 71,
LM_ERR_PkgSigNoMatch = 72,
LM_ERR_PkgSigNoResult = 73,
LM_ERR_PoolPathsEmpty = 74,
LM_ERR_PoolNotAvailable = 75,
LM_ERR_PoolUrlEmpty = 76,
LM_ERR_PoolBadDir = 77,
LM_ERR_PoolBadPaths = 78,
} lm_error_t; } lm_error_t;
typedef struct lm_error_desc { typedef struct lm_error_desc {

View File

@ -9,12 +9,19 @@
lm_pkg_t *lm_package_new(); lm_pkg_t *lm_package_new();
void lm_package_free(lm_pkg_t *pkg); void lm_package_free(lm_pkg_t *pkg);
bool lm_package_verify(lm_pkg_t *pkg);
bool lm_package_data_load(lm_pkg_t *pkg, char *file); bool lm_package_data_load(lm_pkg_t *pkg, char *file);
void lm_package_data_free(lm_pkg_t *pkg); void lm_package_data_free(lm_pkg_t *pkg);
bool lm_package_depend_add(lm_pkg_t *pkg, char *depend); bool lm_package_depend_add(lm_pkg_t *pkg, char *depend);
size_t lm_package_depend_count(lm_pkg_t *pkg); size_t lm_package_depend_count(lm_pkg_t *pkg);
size_t lm_package_depend_strlen(lm_pkg_t *pkg);
bool lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer);
size_t lm_package_keep_count(lm_pkg_t *pkg);
bool lm_package_keep_add(lm_pkg_t *pkg, char *path);
bool lm_package_keep_contains(lm_pkg_t *pkg, char *path);
bool lm_package_path_set_signature(lm_pkg_t *pkg, char *signature_path); bool lm_package_path_set_signature(lm_pkg_t *pkg, char *signature_path);
bool lm_package_path_set_archive(lm_pkg_t *pkg, char *archive_path); bool lm_package_path_set_archive(lm_pkg_t *pkg, char *archive_path);

View File

@ -24,9 +24,7 @@ lm_pkg_t *lm_pool_package_find(lm_pool_t *pool, char *name, char *version);
bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg); bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg);
bool lm_pool_package_get(lm_pool_t *pool, lm_pkg_t *pkg); bool lm_pool_package_get(lm_pool_t *pool, lm_pkg_t *pkg);
bool lm_pool_path_set_info(lm_pool_t *pool, char *info_path); bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir);
bool lm_pool_path_set_list(lm_pool_t *pool, char *list_path);
bool lm_pool_path_set_packages(lm_pool_t *pool, char *packaes_path);
bool lm_pool_path_is_empty(lm_pool_t *pool); bool lm_pool_path_is_empty(lm_pool_t *pool);
void lm_pool_path_free(lm_pool_t *pool); void lm_pool_path_free(lm_pool_t *pool);
@ -36,6 +34,7 @@ void lm_pool_info_free(lm_pool_t *pool);
bool lm_pool_list_load(lm_pool_t *pool); bool lm_pool_list_load(lm_pool_t *pool);
bool lm_pool_list_get(lm_pool_t *pool); bool lm_pool_list_get(lm_pool_t *pool);
void lm_pool_list_free(lm_pool_t *pool);
void lm_pool_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr *addr); void lm_pool_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr *addr);
void lm_pool_serve_thread(void *arg); void lm_pool_serve_thread(void *arg);

View File

@ -5,6 +5,11 @@
#include <stddef.h> #include <stddef.h>
#include <sys/types.h> #include <sys/types.h>
typedef struct lm_pkg_file {
char *path;
char *hash;
} lm_pkg_file_t;
typedef struct lm_pkg_path { typedef struct lm_pkg_path {
char *archive; char *archive;
char *signature; char *signature;
@ -16,6 +21,7 @@ typedef struct lm_pkg {
char *name; char *name;
char *desc; char *desc;
char **depends; char **depends;
char **keeps;
char *version; char *version;
size_t size; size_t size;
} lm_pkg_t; } lm_pkg_t;
@ -27,9 +33,10 @@ typedef struct lm_pool_info {
} lm_pool_info_t; } lm_pool_info_t;
typedef struct lm_pool_path { typedef struct lm_pool_path {
char *packages; char *info_file;
char *list; char *list_file;
char *info; char *list_dir;
char *dir;
} lm_pool_path_t; } lm_pool_path_t;
typedef struct lm_pool { typedef struct lm_pool {

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-01 06:41+0300\n" "POT-Creation-Date: 2024-07-02 03:53+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"
@ -284,3 +284,63 @@ msgstr ""
#, fuzzy #, fuzzy
msgid "package not found" msgid "package not found"
msgstr "URL hostname is too large" msgstr "URL hostname is too large"
#: src/error.c:78
msgid "failed to access to the database file/directory"
msgstr ""
#: src/error.c:79
msgid "failed to open SQLite database"
msgstr ""
#: src/error.c:80
msgid "failed to create table in SQLite database"
msgstr ""
#: src/error.c:81
msgid "failed to prepare statement for SQLite database"
msgstr ""
#: src/error.c:82
msgid "failed to insert to the table in SQLite database"
msgstr ""
#: src/error.c:83
msgid "failed to init GPG for package verification"
msgstr ""
#: src/error.c:84
msgid "failed to import signature to GPG for package verification"
msgstr ""
#: src/error.c:85
msgid "failed to import archive to GPG for package verification"
msgstr ""
#: src/error.c:86
msgid "package signature verification failed with zero matches"
msgstr ""
#: src/error.c:87
msgid "package signature verification failed with zero results"
msgstr ""
#: src/error.c:88
msgid "pool file and directory paths are empty"
msgstr ""
#: src/error.c:89
msgid "pool is not avaliable for connection"
msgstr ""
#: src/error.c:90
msgid "pool URL is empty or invalid"
msgstr ""
#: src/error.c:91
msgid "pool directory path is not accessible"
msgstr ""
#: src/error.c:92
msgid "pool directory sub-paths are not accessible"
msgstr ""

View File

@ -72,14 +72,6 @@ bool lm_ctx_set_data(lm_ctx_t *ctx, char *dir){
return false; return false;
} }
char poolsdir[strlen(dir)+10];
join(poolsdir, dir, "pools");
if(!mkdir_ifnot(poolsdir)){
lm_error_set(LM_ERR_CtxDataFailMkdir);
return false;
}
ctx->data = strdup(dir); ctx->data = strdup(dir);
return true; return true;
} }

11
src/ctx/database.c Normal file
View File

@ -0,0 +1,11 @@
#include "../../include/database.h"
#include "../../include/util.h"
#include "../../include/ctx.h"
#include <string.h>
lm_database_t *lm_ctx_database_new(lm_ctx_t *ctx){
char dbpath[strlen(ctx->data)+10];
join(dbpath, ctx->data, "packages.db");
return lm_database_new(dbpath);
}

View File

@ -1,5 +1,7 @@
#include "../../include/ctx.h" #include "../../include/ctx.h"
#include "../../include/pool.h" #include "../../include/pool.h"
#include "../../include/package.h"
#include "../../include/database.h"
#include "../../include/util.h" #include "../../include/util.h"
#include "../../include/error.h" #include "../../include/error.h"
@ -24,6 +26,37 @@ lm_pkg_t *lm_ctx_package_get(lm_ctx_t *ctx, char *name, char *version) {
return pkg; return pkg;
} }
bool lm_ctx_package_verify(lm_ctx_t *ctx, char *name, char *version) {
lm_pool_t *pool = ctx->pools;
lm_pkg_t *pkg = NULL;
while (NULL != pool) {
if((pkg = lm_pool_package_find(pool, name, version)) != NULL)
break;
pool = pool->next;
}
if(NULL == pool && NULL == pkg){
lm_error_set(LM_ERR_PkgNotFound);
return NULL;
}
return lm_package_verify(pkg);
}
bool lm_ctx_package_is_installed(lm_ctx_t *ctx, char *name, char *version) {
lm_database_t *db = lm_ctx_database_new(ctx);
if(NULL == db)
return false;
bool ret = false;
ret = lm_database_package_find(db, name, version);
lm_database_free(db);
return ret;
}
lm_pkg_t *lm_ctx_package_install(lm_ctx_t *ctx, char *name, char *version){ lm_pkg_t *lm_ctx_package_install(lm_ctx_t *ctx, char *name, char *version){
return NULL; return NULL;
} }

View File

@ -4,6 +4,7 @@
#include "../../include/error.h" #include "../../include/error.h"
#include "../../include/thpool.h" #include "../../include/thpool.h"
#include <assert.h>
#include <errno.h> #include <errno.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
@ -11,34 +12,35 @@
#include <string.h> #include <string.h>
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);
return NULL; return NULL;
}
lm_pool_t *pool = lm_pool_new(name, url); lm_pool_t *pool = lm_pool_new(name, url);
if (NULL == pool) if (NULL == pool)
return false; return NULL;
pdebug(__func__, "pool name is %s", pool->name); pdebug(__func__, "adding new pool: %s (%s://%s:%d%s)", pool->name, pool->url.protocol, pool->url.host, pool->url.port, pool->url.path);
pdebug(__func__, "pool URL is %s://%s:%d%s", pool->url.protocol, pool->url.host, pool->url.port, pool->url.path);
if(NULL != ctx->data){ if(NULL != ctx->data){
char poolp[strlen(ctx->data) + strlen(pool->name) + 20]; char pools_dir[strlen(ctx->data)+strlen(pool->name)+10];
join_multiple(poolp, ctx->data, "pools", pool->name); join(pools_dir, ctx->data, "pools");
if(!mkdir_ifnot(poolp)){ if(!mkdir_ifnot(pools_dir)){
lm_error_set(LM_ERR_CtxDataFailMkdir); lm_error_set(LM_ERR_CtxDataFailMkdir);
return false; lm_pool_free(pool);
return NULL;
} }
size_t poolp_sz = strlen(poolp); bzero(pools_dir, sizeof(pools_dir));
char infop[poolp_sz + 10], listp[poolp_sz + 10]; join_multiple(pools_dir, ctx->data, "pools", pool->name);
join(infop, poolp, "INFO"); if(!lm_pool_path_set_dir(pool, pools_dir)){
join(listp, poolp, "LIST"); // error is set by set_dir
lm_pool_free(pool);
lm_pool_path_set_packages(pool, poolp); return NULL;
lm_pool_path_set_info(pool, infop); }
lm_pool_path_set_list(pool, listp);
} }
if (NULL == ctx->pools) { if (NULL == ctx->pools) {
@ -55,6 +57,9 @@ lm_pool_t *lm_ctx_pool_add(lm_ctx_t *ctx, char *name, char *url) {
cur = cur->next; cur = cur->next;
} }
// if we somehow get here, something very wrong has happend
assert(true);
lm_pool_free(pool); lm_pool_free(pool);
return NULL; return NULL;
} }
@ -76,10 +81,11 @@ void lm_ctx_pool_test(lm_ctx_t *ctx, lm_ctx_pool_callback_t callback, void *data
lm_pool_test(cur); lm_pool_test(cur);
if (!cur->available) if (!cur->available)
pdebug(__func__, "%s is not avaliable: %s", cur->name, lm_strerror()); pdebug(__func__, "(%s) not avaliable: %s", cur->name, lm_strerror());
if(NULL != callback) if(NULL != callback)
callback(ctx, cur, cur->available, data); if(!callback(ctx, cur, cur->available, data))
break;
cur = cur->next; cur = cur->next;
} }

60
src/database/database.c Normal file
View File

@ -0,0 +1,60 @@
#include "../../include/database.h"
#include "../../include/package.h"
#include "../../include/error.h"
#include "../../include/util.h"
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
char *queries[] = {
"CREATE TABLE IF NOT EXISTS packages (" \
" name TEXT PRIMARY KEY NOT NULL," \
" desc TEXT NOT NULL," \
" version TEXT NOT NULL," \
" size INT NOT NULL," \
" depends TEXT NOT NULL);",
"INSERT INTO packages VALUES (?, ?, ?, ?, ?)",
};
lm_database_t *lm_database_new(char *path){
lm_database_t *db = malloc(sizeof(lm_database_t));
char *err = NULL;
bzero(db, sizeof(lm_database_t));
if(exists(path) && !can_read(path)){
lm_error_set(LM_ERR_DbCantAccess);
return NULL;
}
if(sqlite3_open(path, &db->sql)){
pdebug(__func__, "(%s) failed to open databse: %s", path, sqlite3_errmsg(db->sql));
lm_error_set(LM_ERR_DbSqlOpenFail);
return NULL;
}
if(sqlite3_exec(db->sql, queries[QUERY_CREATE_TABLE], NULL, 0, &err) != SQLITE_OK){
pdebug(__func__, "(%s) failed to create packages table: %s", path, err);
lm_error_set(LM_ERR_DbSqlCreateFail);
sqlite3_free(err);
}
db->path = strdup(path);
return db;
}
void lm_database_free(lm_database_t *db){
sqlite3_close(db->sql);
free(db->path);
lm_pkg_t *cur = db->pkg, *prev = NULL;
while(NULL != cur){
prev = cur;
cur = cur->next;
lm_package_free(prev);
}
free(db);
}

53
src/database/package.c Normal file
View File

@ -0,0 +1,53 @@
#include "../../include/database.h"
#include "../../include/package.h"
#include "../../include/error.h"
#include "../../include/util.h"
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
bool lm_database_package_add(lm_database_t *db, lm_pkg_t *pkg){
if(NULL == db || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
char depends[lm_package_depend_strlen(pkg)];
sqlite3_stmt *insert = NULL;
bool ret = false;
if(sqlite3_prepare(db->sql, queries[QUERY_INSERT_PACKAGE], strlen(queries[QUERY_INSERT_PACKAGE]), &insert, NULL) != SQLITE_OK){
pdebug(__func__, "(%s) failed to prepare statement: %s", db->path, sqlite3_errmsg(db->sql));
lm_error_set(LM_ERR_DbSqlPrepareFail);
goto end;
}
sqlite3_bind_text(insert, 1, pkg->name, strlen(pkg->name), SQLITE_STATIC);
sqlite3_bind_text(insert, 2, pkg->desc, strlen(pkg->desc), SQLITE_STATIC);
sqlite3_bind_text(insert, 3, pkg->version, strlen(pkg->version), SQLITE_STATIC);
sqlite3_bind_int64(insert, 4, pkg->size);
if(!lm_package_depend_tostr(pkg, depends)){
pdebug(__func__, "(%s) failed to convert depends to string: %s", db->path, lm_strerror());
goto end;
}
sqlite3_bind_text(insert, 5, depends, strlen(depends), SQLITE_STATIC);
if(sqlite3_step(insert) != SQLITE_OK){
pdebug(__func__, "(%s) failed to execute insert statement: %s", db->path, sqlite3_errmsg(db->sql));
lm_error_set(LM_ERR_DbSqlInsertFail);
goto end;
}
ret = true;
end:
if(NULL != insert)
sqlite3_finalize(insert);
return ret;
}
bool lm_database_package_find(lm_database_t *db, lm_pkg_t *pkg){
return true;
}

View File

@ -10,71 +10,86 @@ void lm_error_set(lm_error_t code) {
char *lm_strerror() { char *lm_strerror() {
lm_error_desc_t errors[] = { lm_error_desc_t errors[] = {
{.code = LM_ERR_NoError, .desc = _("no error") }, {.code = LM_ERR_NoError, .desc = _("no error") },
{.code = LM_ERR_URLBadChar, .desc = _("URL contains an invalid character") }, {.code = LM_ERR_URLBadChar, .desc = _("URL contains an invalid character") },
{.code = LM_ERR_URLBadProtocol, .desc = _("URL does not have a valid protocol field") }, {.code = LM_ERR_URLBadProtocol, .desc = _("URL does not have a valid protocol field") },
{.code = LM_ERR_URLTooLarge, .desc = _("URL is too large") }, {.code = LM_ERR_URLTooLarge, .desc = _("URL is too large") },
{.code = LM_ERR_URLHostLarge, .desc = _("URL hostname is too large") }, {.code = LM_ERR_URLHostLarge, .desc = _("URL hostname is too large") },
{.code = LM_ERR_URLPathLarge, .desc = _("URL path is too large") }, {.code = LM_ERR_URLPathLarge, .desc = _("URL path is too large") },
{.code = LM_ERR_URLBadHost, .desc = _("URL does not have a valid hostname") }, {.code = LM_ERR_URLBadHost, .desc = _("URL does not have a valid hostname") },
{.code = LM_ERR_URLBadPort, .desc = _("URL does not have a valid port number") }, {.code = LM_ERR_URLBadPort, .desc = _("URL does not have a valid port number") },
{.code = LM_ERR_URLBadPath, .desc = _("URL does not have a valid path") }, {.code = LM_ERR_URLBadPath, .desc = _("URL does not have a valid path") },
{.code = LM_ERR_BadPort, .desc = _("hostname does not contain a valid port number") }, {.code = LM_ERR_BadPort, .desc = _("hostname does not contain a valid port number") },
{.code = LM_ERR_BadHost, .desc = _("hostname is not valid") }, {.code = LM_ERR_BadHost, .desc = _("hostname is not valid") },
{.code = LM_ERR_URLPortUnknown, .desc = _("URL protocol port number is unknown") }, {.code = LM_ERR_URLPortUnknown, .desc = _("URL protocol port number is unknown") },
{.code = LM_ERR_URLEnd, .desc = _("URL is incomplete") }, {.code = LM_ERR_URLEnd, .desc = _("URL is incomplete") },
{.code = LM_ERR_PoolNoSupport, .desc = _("pool does not support the specified protocol") }, {.code = LM_ERR_PoolNoSupport, .desc = _("pool does not support the specified protocol") },
{.code = LM_ERR_MPTPBadVersion, .desc = _("unsupported MPTP version") }, {.code = LM_ERR_MPTPBadVersion, .desc = _("unsupported MPTP version") },
{.code = LM_ERR_MPTPBadCode, .desc = _("invalid MPTP request/response code") }, {.code = LM_ERR_MPTPBadCode, .desc = _("invalid MPTP request/response code") },
{.code = LM_ERR_MPTPBadUrl, .desc = _("invalid MPTP URL") }, {.code = LM_ERR_MPTPBadUrl, .desc = _("invalid MPTP URL") },
{.code = LM_ERR_MPTPHostFail, .desc = _("failed to resolve hostname for MPTP connection") }, {.code = LM_ERR_MPTPHostFail, .desc = _("failed to resolve hostname for MPTP connection") },
{.code = LM_ERR_MPTPSocketFail, .desc = _("failed to create a MPTP socket") }, {.code = LM_ERR_MPTPSocketFail, .desc = _("failed to create a MPTP socket") },
{.code = LM_ERR_MPTPConnectFail, .desc = _("failed to connect to the MPTP host") }, {.code = LM_ERR_MPTPConnectFail, .desc = _("failed to connect to the MPTP host") },
{.code = LM_ERR_MPTPRecvFail, .desc = _("failed receive MPTP data from host") }, {.code = LM_ERR_MPTPRecvFail, .desc = _("failed receive MPTP data from host") },
{.code = LM_ERR_MPTPSendFail, .desc = _("failed send MPTP data to host") }, {.code = LM_ERR_MPTPSendFail, .desc = _("failed send MPTP data to host") },
{.code = LM_ERR_MPTPBadData, .desc = _("MPTP data size is invalid") }, {.code = LM_ERR_MPTPBadData, .desc = _("MPTP data size is invalid") },
{.code = LM_ERR_MPTPBadHost, .desc = _("MPTP host size is invalid") }, {.code = LM_ERR_MPTPBadHost, .desc = _("MPTP host size is invalid") },
{.code = LM_ERR_MPTPSetsockopt, .desc = _("failed to set MPTP socket options") }, {.code = LM_ERR_MPTPSetsockopt, .desc = _("failed to set MPTP socket options") },
{.code = LM_ERR_MPTPTimeout, .desc = _("MPTP connection timed out") }, {.code = LM_ERR_MPTPTimeout, .desc = _("MPTP connection timed out") },
{.code = LM_ERR_MPTPBindFail, .desc = _("failed to bind MPTP socket") }, {.code = LM_ERR_MPTPBindFail, .desc = _("failed to bind MPTP socket") },
{.code = LM_ERR_ArgNULL, .desc = _("required argument is a NULL pointer or 0") }, {.code = LM_ERR_ArgNULL, .desc = _("required argument is a NULL pointer or 0") },
{.code = LM_ERR_MPTPNotRequest, .desc = _("not a MPTP request") }, {.code = LM_ERR_MPTPNotRequest, .desc = _("not a MPTP request") },
{.code = LM_ERR_MPTPNotResponse, .desc = _("not a MPTP response") }, {.code = LM_ERR_MPTPNotResponse, .desc = _("not a MPTP response") },
{.code = LM_ERR_MPTPNotLast, .desc = _("MPTP request last flag is not set") }, {.code = LM_ERR_MPTPNotLast, .desc = _("MPTP request last flag is not set") },
{.code = LM_ERR_NoPort, .desc = _("host port not specified") }, {.code = LM_ERR_NoPort, .desc = _("host port not specified") },
{.code = LM_ERR_PoolInfoBad, .desc = _("pool info is badly formatted or is not complete") }, {.code = LM_ERR_PoolInfoBad, .desc = _("pool info is badly formatted or is not complete") },
{.code = LM_ERR_ArcWBlockFail, .desc = _("failed to write block from archive") }, {.code = LM_ERR_ArcWBlockFail, .desc = _("failed to write block from archive") },
{.code = LM_ERR_ArcRBlockFail, .desc = _("failed to read block from archive") }, {.code = LM_ERR_ArcRBlockFail, .desc = _("failed to read block from archive") },
{.code = LM_ERR_ArcOpenFail, .desc = _("failed to open archive") }, {.code = LM_ERR_ArcOpenFail, .desc = _("failed to open archive") },
{.code = LM_ERR_ArcWHeaderFail, .desc = _("failed to write archive header") }, {.code = LM_ERR_ArcWHeaderFail, .desc = _("failed to write archive header") },
{.code = LM_ERR_ArcWEntryFail, .desc = _("failed to finish writing the archive entry") }, {.code = LM_ERR_ArcWEntryFail, .desc = _("failed to finish writing the archive entry") },
{.code = LM_ERR_ArcNewFail, .desc = _("failed to create new archive reader/writer") }, {.code = LM_ERR_ArcNewFail, .desc = _("failed to create new archive reader/writer") },
{.code = LM_ERR_ArcRealpathFail, .desc = _("failed to resolve full path for archive file") }, {.code = LM_ERR_ArcRealpathFail, .desc = _("failed to resolve full path for archive file") },
{.code = LM_ERR_ArcNextHeaderFail, .desc = _("failed to read the next header of the archive") }, {.code = LM_ERR_ArcNextHeaderFail, .desc = _("failed to read the next header of the archive") },
{.code = LM_ERR_GetCwdFail, .desc = _("failed to obtain current working directory") }, {.code = LM_ERR_GetCwdFail, .desc = _("failed to obtain current working directory") },
{.code = LM_ERR_PoolListDirFail, .desc = _("failed to open extracted pool list directory") }, {.code = LM_ERR_PoolListDirFail, .desc = _("failed to open extracted pool list directory") },
{.code = LM_ERR_PoolListCantRead, .desc = _("failed to read access the pool list file") }, {.code = LM_ERR_PoolListCantRead, .desc = _("failed to read access the pool list file") },
{.code = LM_ERR_PoolInfoCantRead, .desc = _("failed to read access the pool info file") }, {.code = LM_ERR_PoolInfoCantRead, .desc = _("failed to read access the pool info file") },
{.code = LM_ERR_PkgDataBad, .desc = _("failed to parse package data") }, {.code = LM_ERR_PkgDataBad, .desc = _("failed to parse package data") },
{.code = LM_ERR_PkgBadName, .desc = _("package name is invalid") }, {.code = LM_ERR_PkgBadName, .desc = _("package name is invalid") },
{.code = LM_ERR_CtxDataNULL, .desc = _("data path is not set with in the ctx") }, {.code = LM_ERR_CtxDataNULL, .desc = _("data path is not set with in the ctx") },
{.code = LM_ERR_CtxTempFail, .desc = _("specified temp path does not exist") }, {.code = LM_ERR_CtxTempFail, .desc = _("specified temp path does not exist") },
{.code = LM_ERR_CtxTempNotDir, .desc = _("specified temp path is not a directory") }, {.code = LM_ERR_CtxTempNotDir, .desc = _("specified temp path is not a directory") },
{.code = LM_ERR_CtxTempNoWrite, .desc = _("specified temp directory does not have write access") }, {.code = LM_ERR_CtxTempNoWrite, .desc = _("specified temp directory does not have write access") },
{.code = LM_ERR_CtxRootFail, .desc = _("specified root path does not exist") }, {.code = LM_ERR_CtxRootFail, .desc = _("specified root path does not exist") },
{.code = LM_ERR_CtxRootNotDir, .desc = _("specified root path is not a directory") }, {.code = LM_ERR_CtxRootNotDir, .desc = _("specified root path is not a directory") },
{.code = LM_ERR_CtxRootNoWrite, .desc = _("specified root directory does not have write access") }, {.code = LM_ERR_CtxRootNoWrite, .desc = _("specified root directory does not have write access") },
{.code = LM_ERR_CtxDataNotDir, .desc = _("specified data path does not exist") }, {.code = LM_ERR_CtxDataNotDir, .desc = _("specified data path does not exist") },
{.code = LM_ERR_CtxDataNoWrite, .desc = _("specified data path is not a directory") }, {.code = LM_ERR_CtxDataNoWrite, .desc = _("specified data path is not a directory") },
{.code = LM_ERR_CtxDataFailMkdir, .desc = _("failed to create specified data directory") }, {.code = LM_ERR_CtxDataFailMkdir, .desc = _("failed to create specified data directory") },
{.code = LM_ERR_PoolTestNotPong, .desc = _("pool did not respond ping with pong") }, {.code = LM_ERR_PoolTestNotPong, .desc = _("pool did not respond ping with pong") },
{.code = LM_ERR_PackagePathsEmpty, .desc = _("package file and directory paths are empty") }, {.code = LM_ERR_PkgPathsEmpty, .desc = _("package file and directory paths are empty") },
{.code = LM_ERR_SendOpenFail, .desc = _("failed to to open target file for sending") }, {.code = LM_ERR_SendOpenFail, .desc = _("failed to to open target file for sending") },
{.code = LM_ERR_RecvDelFail, .desc = _("failed to to delete target file for receiving") }, {.code = LM_ERR_RecvDelFail, .desc = _("failed to to delete target file for receiving") },
{.code = LM_ERR_RecvOpenFail, .desc = _("failed to to open target file for receiving") }, {.code = LM_ERR_RecvOpenFail, .desc = _("failed to to open target file for receiving") },
{.code = LM_ERR_RecvBadCode, .desc = _("got a bad response code for receiving the target file")}, {.code = LM_ERR_RecvBadCode, .desc = _("got a bad response code for receiving the target file") },
{.code = LM_ERR_RecvWriteFail, .desc = _("failed to write to the target file for receiving") }, {.code = LM_ERR_RecvWriteFail, .desc = _("failed to write to the target file for receiving") },
{.code = LM_ERR_PkgNotFound, .desc = _("package not found") }, {.code = LM_ERR_PkgNotFound, .desc = _("package not found") },
{.code = LM_ERR_DbCantAccess, .desc = _("failed to access to the database file/directory") },
{.code = LM_ERR_DbSqlOpenFail, .desc = _("failed to open SQLite database") },
{.code = LM_ERR_DbSqlCreateFail, .desc = _("failed to create table in SQLite database") },
{.code = LM_ERR_DbSqlPrepareFail, .desc = _("failed to prepare statement for SQLite database") },
{.code = LM_ERR_DbSqlInsertFail, .desc = _("failed to insert to the table in SQLite database") },
{.code = LM_ERR_PkgGPGFail, .desc = _("failed to init GPG for package verification") },
{.code = LM_ERR_PkgGPGSigFail, .desc = _("failed to import signature to GPG for package verification")},
{.code = LM_ERR_PkgGPGArchiveFail, .desc = _("failed to import archive to GPG for package verification") },
{.code = LM_ERR_PkgSigNoMatch, .desc = _("package signature verification failed with zero matches") },
{.code = LM_ERR_PkgSigNoResult, .desc = _("package signature verification failed with zero results") },
{.code = LM_ERR_PoolPathsEmpty, .desc = _("pool file and directory paths are empty") },
{.code = LM_ERR_PoolNotAvailable, .desc = _("pool is not avaliable for connection") },
{.code = LM_ERR_PoolUrlEmpty, .desc = _("pool URL is empty or invalid") },
{.code = LM_ERR_PoolBadDir, .desc = _("pool directory path is not accessible") },
{.code = LM_ERR_PoolBadPaths, .desc = _("pool directory sub-paths are not accessible") },
}; };
for (int i = 0; i < sizeof(errors) / sizeof(lm_error_desc_t); i++) { for (int i = 0; i < sizeof(errors) / sizeof(lm_error_desc_t); i++) {

View File

@ -10,7 +10,7 @@ size_t lm_package_depend_count(lm_pkg_t *pkg){
size_t index = 0; size_t index = 0;
if(NULL == pkg->depends) if(NULL == pkg->depends)
return 0; return index;
while(pkg->depends[index] != NULL) while(pkg->depends[index] != NULL)
index++; index++;
@ -42,3 +42,37 @@ bool lm_package_depend_add(lm_pkg_t *pkg, char *depend){
pkg->depends[count] = NULL; pkg->depends[count] = NULL;
return true; return true;
} }
size_t lm_package_depend_strlen(lm_pkg_t *pkg){
size_t len = 1;
if(NULL == pkg->depends)
return len;
for(int i = 0; NULL != pkg->depends[i]; i++)
len += strlen(pkg->depends[i])+1;
return len;
}
bool lm_package_depend_tostr(lm_pkg_t *pkg, char *buffer){
if(NULL == buffer || NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == pkg->depends){
buffer[0] = 0;
return true;
}
for(int i = 0; NULL != pkg->depends[i]; i++){
if(i == 0){
sprintf(buffer, "%s", pkg->depends[i]);
continue;
}
sprintf(buffer, "%s,%s", buffer, pkg->depends[i]);
}
return true;
}

55
src/package/keep.c Normal file
View File

@ -0,0 +1,55 @@
#include "../../include/package.h"
#include "../../include/types.h"
#include "../../include/error.h"
#include "../../include/util.h"
#include <stdlib.h>
#include <string.h>
size_t lm_package_keep_count(lm_pkg_t *pkg){
size_t index = 0;
if(NULL == pkg->keeps)
return index;
while(pkg->keeps[index] != NULL)
index++;
return index;
}
bool lm_package_keep_add(lm_pkg_t *pkg, char *path){
if(NULL == path){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == pkg->keeps){
pkg->keeps = malloc(sizeof(char*)*2);
pkg->keeps[0] = strdup(path);
pkg->keeps[1] = NULL;
return true;
}
size_t count = lm_package_depend_count(pkg);
pkg->keeps = realloc(pkg->depends, sizeof(char*)*(count+2));
pkg->keeps[count++] = strdup(path);
pkg->keeps[count] = NULL;
return true;
}
bool lm_package_keep_contains(lm_pkg_t *pkg, char *path){
if(NULL == pkg || NULL == path){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(NULL == pkg->keeps)
return false;
for(int i = 0; NULL != pkg->keeps[i]; i++)
if(eq(pkg->keeps[i], path))
return true;
return false;
}

83
src/package/verify.c Normal file
View File

@ -0,0 +1,83 @@
#include "../../include/package.h"
#include "../../include/error.h"
#include "../../include/util.h"
#include <stdlib.h>
#include <gpgme.h>
bool lm_package_verify(lm_pkg_t *pkg){
if(NULL == pkg){
lm_error_set(LM_ERR_ArgNULL);
return false;
}
if(lm_package_path_is_empty(pkg)){
lm_error_set(LM_ERR_PkgPathsEmpty);
return false;
}
gpgme_data_t sig = NULL, text = NULL;
gpgme_ctx_t ctx = NULL;
gpgme_signature_t s;
gpgme_verify_result_t result;
gpgme_error_t err;
bool ret = false;
gpgme_check_version(NULL);
err = gpgme_new(&ctx);
if (err != GPG_ERR_NO_ERROR) {
pdebug(__func__, "(%s) failed to create gpgme: %s", pkg->name, gpgme_strerror(err));
lm_error_set(LM_ERR_PkgGPGFail);
return false;
}
gpgme_op_import(ctx, NULL);
if((err = gpgme_data_new_from_file(&sig, pkg->paths.signature, 1)) != GPG_ERR_NO_ERROR){
pdebug(__func__, "(%s) failed to import package signature %s", pkg->name, gpgme_strerror(err));
lm_error_set(LM_ERR_PkgGPGSigFail);
goto end;
}
if ((err = gpgme_data_new_from_file(&text, pkg->paths.archive, 1)) != GPG_ERR_NO_ERROR) {
pdebug(__func__, "(%s) failed to import package archive %s", pkg->name, gpgme_strerror(err));
lm_error_set(LM_ERR_PkgGPGArchiveFail);
goto end;
}
if ((err = gpgme_op_verify(ctx, sig, text, NULL)) != GPG_ERR_NO_ERROR) {
pdebug(__func__, "(%s) failed to verify package archive %s", pkg->name, gpgme_strerror(err));
lm_error_set(LM_ERR_PkgSigNoResult);
goto end;
}
result = gpgme_op_verify_result(ctx);
lm_error_set(LM_ERR_PkgSigNoMatch);
if (result == NULL)
goto end;
s = result->signatures;
while (s) {
int status = gpg_err_code(s->status);
if (status == GPG_ERR_NO_ERROR) {
lm_error_set(LM_ERR_NoError);
ret = true;
break;
}
s = s->next;
}
end:
if(NULL != text)
gpgme_data_release(text);
if(NULL != sig)
gpgme_data_release(sig);
if(NULL != ctx)
gpgme_release(ctx);
return ret;
}

View File

@ -35,15 +35,17 @@ bool lm_pool_info_load(lm_pool_t *pool) {
lm_pool_info_free(pool); lm_pool_info_free(pool);
if(NULL == pool->paths.info) if(lm_pool_path_is_empty(pool)){
lm_error_set(LM_ERR_PoolPathsEmpty);
return false; return false;
}
if(!can_read(pool->paths.info)){ if(!exists(pool->paths.info_file) || !can_read(pool->paths.info_file)){
lm_error_set(LM_ERR_PoolInfoCantRead); lm_error_set(LM_ERR_PoolInfoCantRead);
return false; return false;
} }
if (ini_parse(pool->paths.info, lm_pool_info_handler, pool) < 0) { if (ini_parse(pool->paths.info_file, lm_pool_info_handler, pool) < 0) {
lm_error_set(LM_ERR_PoolInfoBad); lm_error_set(LM_ERR_PoolInfoBad);
return false; return false;
} }
@ -51,7 +53,6 @@ bool lm_pool_info_load(lm_pool_t *pool) {
if(pool->info.size <= 0 || if(pool->info.size <= 0 ||
pool->info.pubkey == NULL || pool->info.pubkey == NULL ||
pool->info.maintainer == NULL){ pool->info.maintainer == NULL){
printf("%lu %s %s\n", pool->info.size, pool->info.pubkey, pool->info.maintainer);
lm_error_set(LM_ERR_PoolInfoBad); lm_error_set(LM_ERR_PoolInfoBad);
return false; return false;
} }
@ -65,17 +66,25 @@ bool lm_pool_info_get(lm_pool_t *pool) {
return false; return false;
} }
if(!pool->available) if(!pool->available){
lm_error_set(LM_ERR_PoolNotAvailable);
return false; return false;
}
if(NULL == pool->paths.info) if(lm_pool_path_is_empty(pool)){
lm_error_set(LM_ERR_PoolPathsEmpty);
return false; return false;
}
if(NULL == pool->url.host) if(!mkdir_ifnot(pool->paths.dir)){
lm_error_set(LM_ERR_PoolBadDir);
return false; return false;
}
if(NULL == pool->url.path) if(NULL == pool->url.path || NULL == pool->url.host){
lm_error_set(LM_ERR_PoolUrlEmpty);
return false; return false;
}
int sock = -1; int sock = -1;
lm_mptp_t packet; lm_mptp_t packet;
@ -91,7 +100,7 @@ bool lm_pool_info_get(lm_pool_t *pool) {
if(!lm_mptp_client_send(sock, &packet)) if(!lm_mptp_client_send(sock, &packet))
goto end; goto end;
if(!lm_mptp_recvfile(sock, pool->paths.info)) if(!lm_mptp_recvfile(sock, pool->paths.info_file))
goto end; goto end;
ret = true; ret = true;

View File

@ -14,46 +14,40 @@ bool lm_pool_list_load(lm_pool_t *pool){
lm_error_set(LM_ERR_ArgNULL); lm_error_set(LM_ERR_ArgNULL);
return false; return false;
} }
lm_pool_list_free(pool);
if(NULL == pool->paths.list) if(lm_pool_path_is_empty(pool)){
lm_error_set(LM_ERR_PoolPathsEmpty);
return false; return false;
}
if(!can_read(pool->paths.list)){ if(!exists(pool->paths.list_file) || !can_read(pool->paths.list_file)){
lm_error_set(LM_ERR_PoolListCantRead); lm_error_set(LM_ERR_PoolListCantRead);
return false; return false;
} }
size_t file_len = strlen(pool->paths.list); pdebug(__func__, "(%s) extracting pool to %s", pool->name, pool->paths.list_dir);
char *list_dir = NULL, *list_name = NULL;
char file_copy[file_len + 1], file_copy2[file_len + 1];
char extract_dir[file_len+15];
memcpy(file_copy, pool->paths.list, file_len+1); if(!mkdir_ifnot(pool->paths.dir)){
memcpy(file_copy2, pool->paths.list, file_len+1); lm_error_set(LM_ERR_PoolBadDir);
list_dir = dirname(file_copy);
list_name = basename(file_copy2);
char extract_name[strlen(list_name)+20];
snprintf(extract_name, sizeof(extract_name), "%s_extracted", list_name);
join(extract_dir, list_dir, extract_name);
pdebug(__func__, "extracting pool to %s", extract_dir);
if(!mkdir_ifnot(extract_dir)){
lm_error_set(LM_ERR_PoolListDirFail);
return false; return false;
} }
if(!mkdir_ifnot(pool->paths.list_dir)){
lm_error_set(LM_ERR_PoolBadPaths);
return false;
}
size_t ent_len = 0, list_dir_len = strlen(pool->paths.list_dir);
struct dirent *ent = NULL; struct dirent *ent = NULL;
size_t ent_len = 0;
DIR *dirfd = NULL; DIR *dirfd = NULL;
bool ret = false; bool ret = false;
if(!extract_archive(extract_dir, pool->paths.list)) if(!extract_archive(pool->paths.list_dir, pool->paths.list_file))
goto end; goto end;
if((dirfd = opendir(extract_dir)) == NULL){ if((dirfd = opendir(pool->paths.list_dir)) == NULL){
lm_error_set(LM_ERR_PoolListDirFail); lm_error_set(LM_ERR_PoolListDirFail);
goto end; goto end;
} }
@ -63,8 +57,8 @@ bool lm_pool_list_load(lm_pool_t *pool){
continue; continue;
ent_len = strlen(ent->d_name); ent_len = strlen(ent->d_name);
char datap[ent_len+sizeof(extract_dir)+10]; char datap[ent_len+list_dir_len+10];
join_multiple(datap, extract_dir, ent->d_name, "DATA"); join_multiple(datap, pool->paths.list_dir, ent->d_name, "DATA");
lm_pkg_t *pkg = lm_package_new(); lm_pkg_t *pkg = lm_package_new();
@ -97,17 +91,30 @@ bool lm_pool_list_get(lm_pool_t *pool) {
return false; return false;
} }
if(NULL == pool->paths.list) if(!pool->available){
lm_error_set(LM_ERR_PoolNotAvailable);
return false; return false;
}
if(!pool->available) if(lm_pool_path_is_empty(pool)){
lm_error_set(LM_ERR_PoolPathsEmpty);
return false; return false;
}
if(NULL == pool->url.host) if(!mkdir_ifnot(pool->paths.dir)){
lm_error_set(LM_ERR_PoolBadDir);
return false; return false;
}
if(NULL == pool->url.path) if(!mkdir_ifnot(pool->paths.list_dir)){
lm_error_set(LM_ERR_PoolBadPaths);
return false; return false;
}
if(NULL == pool->url.path || NULL == pool->url.host){
lm_error_set(LM_ERR_PoolUrlEmpty);
return false;
}
int sock = -1; int sock = -1;
lm_mptp_t packet; lm_mptp_t packet;
@ -123,7 +130,7 @@ bool lm_pool_list_get(lm_pool_t *pool) {
if(!lm_mptp_client_send(sock, &packet)) if(!lm_mptp_client_send(sock, &packet))
goto end; goto end;
if(!lm_mptp_recvfile(sock, pool->paths.list)) if(!lm_mptp_recvfile(sock, pool->paths.list_file))
goto end; goto end;
ret = true; ret = true;
@ -133,3 +140,15 @@ end:
ret = lm_pool_list_load(pool); ret = lm_pool_list_load(pool);
return ret; return ret;
} }
void lm_pool_list_free(lm_pool_t *pool){
if(NULL == pool)
return;
lm_pkg_t *cur = pool->pkg, *prev = NULL;
while(NULL != cur){
prev = cur;
cur = cur->next;
lm_package_free(prev);
}
}

View File

@ -36,8 +36,8 @@ bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg){
} }
if(!lm_pool_path_is_empty(pool)){ if(!lm_pool_path_is_empty(pool)){
size_t path_size = strlen(pool->paths.packages)+strlen(pkg->name)+strlen(pkg->version); size_t path_size = strlen(pool->paths.dir)+strlen(pkg->name)+strlen(pkg->version);
size_t name_size = path_size - strlen(pool->paths.packages); size_t name_size = path_size - strlen(pool->paths.dir);
char archive_path[path_size+10], sig_path[path_size+20]; char archive_path[path_size+10], sig_path[path_size+20];
char archive_name[name_size+10], sig_name[name_size+20]; char archive_name[name_size+10], sig_name[name_size+20];
@ -45,8 +45,8 @@ bool lm_pool_package_add(lm_pool_t *pool, lm_pkg_t *pkg){
sprintf(archive_name, "%s_%s.mpf", pkg->name, pkg->version); sprintf(archive_name, "%s_%s.mpf", pkg->name, pkg->version);
sprintf(sig_name, "%s_%s.mpf.sig", pkg->name, pkg->version); sprintf(sig_name, "%s_%s.mpf.sig", pkg->name, pkg->version);
join(archive_path, pool->paths.packages, archive_name); join(archive_path, pool->paths.dir, archive_name);
join(sig_path, pool->paths.packages, sig_name); join(sig_path, pool->paths.dir, sig_name);
lm_package_path_set_archive(pkg, archive_path); lm_package_path_set_archive(pkg, archive_path);
lm_package_path_set_signature(pkg, sig_path); lm_package_path_set_signature(pkg, sig_path);
@ -76,7 +76,7 @@ bool lm_pool_package_get(lm_pool_t *pool, lm_pkg_t *pkg){
} }
if(lm_package_path_is_empty(pkg)){ if(lm_package_path_is_empty(pkg)){
lm_error_set(LM_ERR_PackagePathsEmpty); lm_error_set(LM_ERR_PkgPathsEmpty);
return false; return false;
} }

View File

@ -1,60 +1,60 @@
#include "../../include/types.h" #include "../../include/types.h"
#include "../../include/error.h"
#include "../../include/pool.h" #include "../../include/pool.h"
#include "../../include/util.h" #include "../../include/util.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
bool lm_pool_path_set_info(lm_pool_t *pool, char *info_path){ bool lm_pool_path_set_dir(lm_pool_t *pool, char *dir){
free(pool->paths.info); free(pool->paths.dir);
pool->paths.info = NULL; pool->paths.dir = NULL;
if(NULL == info_path) if(NULL == dir)
return true; return true;
if(is_dir(info_path)) if(exists(dir) && (is_file(dir) || !can_read(dir) || !can_write(dir))){
lm_error_set(LM_ERR_PoolBadDir);
return false; return false;
}
pool->paths.info = strdup(info_path); pool->paths.dir = strdup(dir);
return true; pool->paths.info_file = join_alloc(dir, "INFO");
} pool->paths.list_file = join_alloc(dir, "LIST");
pool->paths.list_dir = join_alloc(dir, "LIST_extracted");
bool lm_pool_path_set_list(lm_pool_t *pool, char *list_path){
free(pool->paths.list); if(exists(pool->paths.info_file) && (!is_file(pool->paths.info_file) || !can_read(pool->paths.info_file) || !can_write(pool->paths.info_file))){
pool->paths.list = NULL; lm_pool_path_free(pool);
lm_error_set(LM_ERR_PoolBadPaths);
if(NULL == list_path)
return true;
if(is_dir(list_path))
return false; return false;
}
pool->paths.list = strdup(list_path);
return true; if(exists(pool->paths.list_file) && (!is_file(pool->paths.list_file) || !can_read(pool->paths.list_file) || !can_write(pool->paths.list_file))){
} lm_pool_path_free(pool);
lm_error_set(LM_ERR_PoolBadPaths);
bool lm_pool_path_set_packages(lm_pool_t *pool, char *packages_path){
free(pool->paths.packages);
pool->paths.packages = NULL;
if(NULL == packages_path)
return true;
if(is_file(packages_path))
return false; return false;
}
pool->paths.packages = strdup(packages_path); if(exists(pool->paths.list_dir) && (is_file(pool->paths.list_dir) || !can_read(pool->paths.list_dir) || !can_write(pool->paths.list_dir))){
lm_pool_path_free(pool);
lm_error_set(LM_ERR_PoolBadPaths);
return false;
}
return true; return true;
} }
bool lm_pool_path_is_empty(lm_pool_t *pool){ bool lm_pool_path_is_empty(lm_pool_t *pool){
return NULL == pool->paths.info || return NULL == pool->paths.info_file ||
NULL == pool->paths.list || NULL == pool->paths.list_file ||
NULL == pool->paths.packages; NULL == pool->paths.list_dir ||
NULL == pool->paths.dir;
} }
void lm_pool_path_free(lm_pool_t *pool){ void lm_pool_path_free(lm_pool_t *pool){
free(pool->paths.packages); free(pool->paths.dir);
free(pool->paths.info); free(pool->paths.list_dir);
free(pool->paths.list); free(pool->paths.info_file);
free(pool->paths.list_file);
bzero(&pool->paths, sizeof(pool->paths));
} }

View File

@ -13,7 +13,7 @@ lm_pool_t *lm_pool_new(char *name, char *url) {
pool->available = true; pool->available = true;
pool->name = name; pool->name = name;
if (!lm_url_init(&pool->url, url)) { if (NULL != url && !lm_url_init(&pool->url, url)) {
free(pool); free(pool);
return NULL; return NULL;
} }
@ -66,13 +66,6 @@ end:
void lm_pool_free(lm_pool_t *pool) { void lm_pool_free(lm_pool_t *pool) {
lm_url_free(&pool->url); lm_url_free(&pool->url);
lm_pool_info_free(pool); lm_pool_info_free(pool);
lm_pool_list_free(pool);
lm_pkg_t *cur = pool->pkg, *prev = NULL;
while(NULL != cur){
prev = cur;
cur = cur->next;
lm_package_free(prev);
}
free(pool); free(pool);
} }

View File

@ -25,13 +25,13 @@ void lm_pool_serve(lm_pool_t *pool, lm_mptp_t *packet, int sock, struct sockaddr
// when INFO file is requested, send the file // when INFO file is requested, send the file
case MPTP_C2S_INFO: case MPTP_C2S_INFO:
pdebug(__func__, "(%s) INFO from %s attempting to send info", pool->name, ipaddr); pdebug(__func__, "(%s) INFO from %s attempting to send info", pool->name, ipaddr);
lm_mptp_sendfile(sock, addr, pool->paths.info); lm_mptp_sendfile(sock, addr, pool->paths.info_file);
break; break;
// when LIST file is requested, send the file // when LIST file is requested, send the file
case MPTP_C2S_LIST: case MPTP_C2S_LIST:
pdebug(__func__, "(%s) LIST from %s attempting to send list", pool->name, ipaddr); pdebug(__func__, "(%s) LIST from %s attempting to send list", pool->name, ipaddr);
lm_mptp_sendfile(sock, addr, pool->paths.list); lm_mptp_sendfile(sock, addr, pool->paths.list_file);
break; break;
// when the request code is PULL, send the requested package archive and // when the request code is PULL, send the requested package archive and