libmpm

morphux C package management library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/libmpm

commit 187f590a0031accd56b0f679fafa7e8bbde9aaae
parent 71c1be6c588e79eb70aa471336f2503cec80516a
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 30 May 2017 10:39:21 +0200

Merge branch 'unstable' of https://github.com/Morphux/libmpm into unstable

Diffstat:
Minc/config.h | 1+
Ainc/error.h | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Minc/libmpm.h | 1+
Msrc/compile.c | 41+++++++++++++++++++++++++++++++++++++++++
Msrc/config.c | 3+++
Msrc/database.c | 53++++++++++++++++++++++++++++++++++++++++++++++++++++-
Asrc/error.c | 46++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 214 insertions(+), 1 deletion(-)

diff --git a/inc/config.h b/inc/config.h @@ -22,6 +22,7 @@ # include <confuse.h> # include <morphux.h> # include <fcntl.h> +# include <error.h> # define CONFIG_DEF_PATH "/etc/mpm/mpm.conf" diff --git a/inc/error.h b/inc/error.h @@ -0,0 +1,70 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +#ifndef ERROR_H +# define ERROR_H + +# include <morphux.h> + +# define SET_ERR_STR(num, str) [num] = str + +typedef enum u32_t { + /* No Error */ + ERR_NO_ERROR = 0, + /* Insufficient memory */ + ERR_MEMORY, + /* Bad pointer */ + ERR_BAD_PTR, + /* chdir(2) call failed */ + ERR_CHDIR_FAILED, + /* A binary execution failed */ + ERR_EXEC_FAILED, + /* An extract of an archive failed */ + ERR_ARCHIVE_EXTRACT, + /* An installation failed */ + ERR_INSTALLATION_FAILED, + /* Bad configuration */ + ERR_BAD_CONFIG, + /* Always keep this one last */ + ERR_LAST +} mpm_error_t; + +/*! + * \brief Set an error + * + * \param err_num Error number + */ +void set_mpm_error(mpm_error_t err_num); +# define SET_ERR(num) set_mpm_error(num) + +/*! + * \brief Get the error number + * + * \return The error number + */ +mpm_error_t get_mpm_error(void); +# define MPM_ERR() get_mpm_error() + +/*! + * \brief Return a string from an error code + * + * \brief err_num Error code + * + * \return Error, in string format + */ +const char *mpm_strerror(mpm_error_t err_num); + +#endif diff --git a/inc/libmpm.h b/inc/libmpm.h @@ -24,6 +24,7 @@ # include <package.h> # include <compile.h> # include <packer.h> +# include <error.h> /*! * \brief Init the mpm library diff --git a/src/compile.c b/src/compile.c @@ -23,7 +23,10 @@ compile_t *package_install_init(packer_t *ctx) { ret = malloc(sizeof(*ret)); if (ret == NULL) + { + set_mpm_error(ERR_MEMORY); return NULL; + } ret->package = ctx; ret->state = INST_STATE_NONE; @@ -32,6 +35,7 @@ compile_t *package_install_init(packer_t *ctx) { if (chdir(ctx->out_dir) != 0) { free(ret); + set_mpm_error(ERR_MEMORY); return NULL; } if (ctx->header->compilation->env == NULL) @@ -50,7 +54,10 @@ compile_t *package_install_init(packer_t *ctx) { bool package_install_cleanup(compile_t *ctx) { if (chdir(ctx->old_pwd) != 0) + { + set_mpm_error(ERR_CHDIR_FAILED); return false; + } packer_free(ctx->package); free(ctx); @@ -62,7 +69,10 @@ bool before_package(compile_t *ctx) { goto end; if (exec_line("sh " COMP_BEFORE_SCRIPT) != 0) + { + set_mpm_error(ERR_EXEC_FAILED); return false; + } end: ctx->state = INST_STATE_BEFORE; @@ -80,7 +90,10 @@ bool patch_package(compile_t *ctx) { return true; if (chdir(PACKER_SRC_DIR) == -1) + { + set_mpm_error(ERR_CHDIR_FAILED); goto end; + } while ((dinfo = readdir(dir))) { @@ -93,6 +106,7 @@ bool patch_package(compile_t *ctx) { asprintf(&cmd, "%s ../%s%s", PATCH_CMD, PACKER_PATCH_DIR, dinfo->d_name); if (exec_line(cmd) != 0) { + set_mpm_error(ERR_EXEC_FAILED); free(cmd); goto end; } @@ -101,7 +115,10 @@ bool patch_package(compile_t *ctx) { } if (chdir("..") == -1) + { + set_mpm_error(ERR_CHDIR_FAILED); goto end; + } status = true; end: @@ -124,7 +141,10 @@ bool configure_package(compile_t *ctx) { } if (chdir(PACKER_SRC_DIR) == -1) + { + set_mpm_error(ERR_CHDIR_FAILED); goto end; + } list_add(cmd, CONFIGURE_CMD, sizeof(CONFIGURE_CMD) + 1); @@ -139,6 +159,8 @@ bool configure_package(compile_t *ctx) { if (exec_list(cmd) == 0) ret = true; + else + set_mpm_error(ERR_EXEC_FAILED); list_free(cmd, NULL); @@ -157,10 +179,16 @@ bool make_package(compile_t *ctx) { goto end; if (chdir(PACKER_SRC_DIR)) + { + set_mpm_error(ERR_CHDIR_FAILED); return false; + } if (exec_line(ctx->package->header->compilation->make) != 0) + { + set_mpm_error(ERR_EXEC_FAILED); status = false; + } chdir(".."); @@ -177,10 +205,16 @@ bool install_package(compile_t *ctx) { goto end; if (chdir(PACKER_SRC_DIR)) + { + set_mpm_error(ERR_CHDIR_FAILED); return false; + } if (exec_line(ctx->package->header->compilation->install) != 0) + { + set_mpm_error(ERR_EXEC_FAILED); status = false; + } chdir(".."); @@ -194,7 +228,10 @@ bool after_package(compile_t *ctx) { goto end; if (exec_line("sh " COMP_AFTER_SCRIPT) != 0) + { + set_mpm_error(ERR_EXEC_FAILED); return false; + } end: ctx->state = INST_STATE_AFTER; @@ -208,12 +245,14 @@ bool install_archive(packer_t *ctx) { if (packer_extract_archive(ctx, DEFAULT_EXTRACT_DIR) == false) { packer_free(ctx); + set_mpm_error(ERR_ARCHIVE_EXTRACT); return ret; } ptr = package_install_init(ctx); if (ptr == NULL) { + set_mpm_error(ERR_MEMORY); recursive_delete(ctx->out_dir); packer_free(ctx); return ret; @@ -240,6 +279,8 @@ bool install_archive(packer_t *ctx) { ret = true; end: + if (ret != true) + set_mpm_error(ERR_INSTALLATION_FAILED); recursive_delete(ctx->out_dir); package_install_cleanup(ptr); return ret; diff --git a/src/config.c b/src/config.c @@ -105,7 +105,10 @@ config_t *parse_config(const char *path, u8_t *ret) { config->fn = malloc(strlen(path != NULL ? path : CONFIG_DEF_PATH) + 1); strcpy(config->fn, path != NULL ? path : CONFIG_DEF_PATH); if (*ret != 0) + { + set_mpm_error(ERR_BAD_CONFIG); config_get_error_string(config); + } return config; } diff --git a/src/database.c b/src/database.c @@ -72,10 +72,12 @@ database_t *mpm_database_open(u8_t *ret, const char *fn) { *ret = 0; ptr = malloc(sizeof(database_t)); assert(ptr != NULL); + if (fn != NULL) error = sqlite3_open(fn, &ptr->sql); else error = sqlite3_open(DB_FN, &ptr->sql); + if (error != 0) goto error; return ptr; @@ -101,7 +103,10 @@ u8_t mpm_database_close(database_t *ptr) { u8_t mpm_database_exec(database_t *ptr, const char *query, SQL_CALLBACK_PTR(cl), void *ct, char **err) { if (ptr == NULL || query == NULL) + { + set_mpm_error(ERR_BAD_PTR); return 1; + } return sqlite3_exec(ptr->sql, query, cl, ct, err); } @@ -112,7 +117,10 @@ u8_t mpm_get_package_by_id(database_t *ptr, u64_t id, mlist_t **pkg) { u8_t ret; if (ptr == NULL) + { + set_mpm_error(ERR_BAD_PTR); return 1; + } *pkg = NULL; asprintf(&query, QUERY_GET_PACKAGE_BY_ID(id)); @@ -126,7 +134,10 @@ u8_t mpm_get_package_by_name(database_t *ptr, const char *name, mlist_t **pkg) { u8_t ret; if (ptr == NULL) + { + set_mpm_error(ERR_BAD_PTR); return 1; + } *pkg = NULL; asprintf(&query, QUERY_GET_PACKAGE_BY_NAME(name)); @@ -137,7 +148,10 @@ u8_t mpm_get_package_by_name(database_t *ptr, const char *name, mlist_t **pkg) { package_t *sql_to_package(package_t *ptr, char *name, char *val) { if (ptr == NULL) + { + set_mpm_error(ERR_BAD_PTR); return ptr; + } /* TODO: Get all columns */ if (strcmp(name, PKG_COL_ID) == 0) @@ -197,7 +211,10 @@ u8_t mpm_database_init(database_t *ptr) { u8_t ret = 0; if (ptr == NULL) - return 1; + { + set_mpm_error(ERR_BAD_PTR); + return 1; + } for (u8_t i = 0; i < sizeof(query_table) / sizeof(query_table[0]); i++) { @@ -220,7 +237,10 @@ u8_t mpm_database_add_pkg(database_t *ptr, package_t *pkg) { u8_t ret; if (pkg == NULL || ptr == NULL) + { + set_mpm_error(ERR_BAD_PTR); return 1; + } deps = NULL; files = NULL; @@ -248,7 +268,10 @@ u8_t mpm_get_file_by_id(database_t *ptr, u64_t id, mlist_t **files) { u8_t ret; if (ptr == NULL) + { + set_mpm_error(ERR_BAD_PTR); return 1; + } *files = NULL; asprintf(&query, QUERY_GET_FILES_BY_ID(id)); @@ -262,7 +285,10 @@ u8_t mpm_get_file_by_path(database_t *ptr, const char *path, mlist_t **files) { u8_t ret; if (ptr == NULL || path == NULL) + { + set_mpm_error(ERR_BAD_PTR); return 1; + } *files = NULL; asprintf(&query, QUERY_GET_FILES_BY_PATH(path)); @@ -277,7 +303,10 @@ u8_t mpm_get_file_by_parent_id(database_t *ptr, u64_t id, u8_t ret; if (ptr == NULL) + { + set_mpm_error(ERR_BAD_PTR); return 1; + } *files = NULL; asprintf(&query, QUERY_GET_FILES_BY_PARENT_ID(id)); @@ -291,7 +320,10 @@ u8_t mpm_get_file_by_parent_name(database_t *ptr, const char *name, mlist_t **fi u8_t ret; if (ptr == NULL || name == NULL) + { + set_mpm_error(ERR_BAD_PTR); return 1; + } *files = NULL; asprintf(&query, QUERY_GET_FILES_BY_PARENT_NAME(name)); @@ -302,7 +334,10 @@ u8_t mpm_get_file_by_parent_name(database_t *ptr, const char *name, mlist_t **fi file_t *sql_to_file(file_t *ptr, char *name, char *val) { if (ptr == NULL) + { + set_mpm_error(ERR_BAD_PTR); return ptr; + } if (strcmp(name, FILE_COL_ID) == 0) { @@ -340,7 +375,10 @@ u8_t mpm_database_add_file(database_t *ptr, file_t *file) { u8_t ret; if (ptr == NULL || file == NULL) + { + set_mpm_error(ERR_BAD_PTR); return 1; + } asprintf(&query, SQL_INSERT_TABLE FILE_TABLE \ " (%s, %s, %s, %s, %s) " \ @@ -361,7 +399,10 @@ u8_t mpm_database_add_categ(database_t *ptr, category_t *cat) { u8_t ret; if (ptr == NULL || cat == NULL) + { + set_mpm_error(ERR_BAD_PTR); return 1; + } asprintf(&query, SQL_INSERT_TABLE CAT_TABLE \ "(%s, %s, %s) " \ @@ -382,7 +423,10 @@ u8_t mpm_get_categ_by_id(database_t *ptr, u64_t id, mlist_t **cat) { u8_t ret; if (ptr == NULL) + { + set_mpm_error(ERR_BAD_PTR); return 1; + } *cat = NULL; asprintf(&query, QUERY_GET_CATEG_BY_ID(id)); @@ -397,7 +441,11 @@ u8_t mpm_get_categ_by_name(database_t *ptr, const char *name, mlist_t **cat) { u8_t ret; if (ptr == NULL) + { + set_mpm_error(ERR_BAD_PTR); return 1; + } + *cat = NULL; asprintf(&query, QUERY_GET_CATEG_BY_NAME(name)); ret = sqlite3_exec(ptr->sql, query, &callback_categ, cat, NULL); @@ -408,7 +456,10 @@ u8_t mpm_get_categ_by_name(database_t *ptr, const char *name, mlist_t **cat) { category_t *sql_to_category(category_t *ptr, char *name, char *val) { if (ptr == NULL) + { + set_mpm_error(ERR_BAD_PTR); return ptr; + } if (strcmp(name, CAT_COL_ID) == 0) { diff --git a/src/error.c b/src/error.c @@ -0,0 +1,46 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +#include <error.h> + +static u32_t g_error = ERR_NO_ERROR; +static const char *g_str_errors[] = { + SET_ERR_STR(ERR_NO_ERROR, "No error"), + SET_ERR_STR(ERR_MEMORY, "Insufficient memory"), + SET_ERR_STR(ERR_BAD_PTR, "Bad pointer"), + SET_ERR_STR(ERR_CHDIR_FAILED, "Can't change directory"), + SET_ERR_STR(ERR_EXEC_FAILED, "A binary execution failed"), + SET_ERR_STR(ERR_ARCHIVE_EXTRACT, "An archive extraction failed"), + SET_ERR_STR(ERR_INSTALLATION_FAILED, "An installation failed"), + SET_ERR_STR(ERR_BAD_CONFIG, "Bad configuration") +}; + +void set_mpm_error(mpm_error_t err_num) { + g_error = err_num; +} + +u32_t get_mpm_error(void) { + return g_error; +} + +const char *mpm_strerror(mpm_error_t err_num) { + const char *ret = NULL; + + if (err_num > 0 && err_num < ERR_LAST) + ret = g_str_errors[err_num]; + err_num = ERR_NO_ERROR; + return ret; +}