libmpm

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

commit 1bb9d6349baac90612f961292198286ee7196066
parent 70211ab412da9f77b4b251e46ae2041ee4bcbb45
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri, 31 Mar 2017 18:51:15 +0200

Fix(Packer): New inits functions, fix tests

Diffstat:
Minc/packer.h | 28+++++++++++++++++++++++-----
Msrc/packer.c | 46++++++++++++++++++++++++++++++++++++----------
Mtests/test_packer.c | 29+++++++++++++++++++++++------
3 files changed, 82 insertions(+), 21 deletions(-)

diff --git a/inc/packer.h b/inc/packer.h @@ -23,11 +23,17 @@ # include <json.h> -# define PACKER_DEF_CONF_FN "/package.json" +# define PACKER_DEF_CONF_FN "package.json" + +typedef enum packer_type_e { + PACKER_TYPE_DIRECTORY, + PACKER_TYPE_ARCHIVE +} packer_type_t; typedef struct packer_s { - char *dir; /*!< Directory of the package */ - json_object *json; /*!< JSON object */ + char *str; /*!< Directory, or path of the package */ + json_object *json; /*!< JSON object */ + packer_type_t type; /*!< Type of packer */ } packer_t; /*! @@ -37,7 +43,16 @@ typedef struct packer_s { * * \return A freshly allocated pointer on success, NULL on failure */ -packer_t *packer_init(const char *dir); +packer_t *packer_init_dir(const char *dir); + +/*! + * \brief Allocate, fill an init a packer_t structure + * + * \param[in] path Path of the archive + * + * \return A freshly allocated pointer on success, NULL on failure + */ +packer_t *packer_init_archive(const char *path); /*! * \brief Free a packer_t structure @@ -51,8 +66,11 @@ void packer_free(packer_t *ptr); * * \param[in,out] ctx Already initialized packer_t struct * + * \note This function is only for directory TO pack + * \note Throw an assertion if the passed pointer is NULL + * * \return true on success, false on failure */ -bool packer_read(packer_t *ctx); +bool packer_read_dir(packer_t *ctx); #endif /* PACKER_H */ diff --git a/src/packer.c b/src/packer.c @@ -16,39 +16,67 @@ #include <packer.h> -packer_t *packer_init(const char *dir) { +static packer_t *packer_init(const char *str) { packer_t *ret; ret = malloc(sizeof(*ret)); if (ret == NULL) return NULL; - ret->dir = strdup(dir); - if (ret->dir == NULL) + ret->str = strdup(str); + if (ret->str == NULL) goto cleanup; + return ret; cleanup: free(ret); return NULL; + +} + +packer_t *packer_init_dir(const char *dir) { + packer_t *ret; + + ret = packer_init(dir); + if (ret == NULL) + return NULL; + ret->type = PACKER_TYPE_DIRECTORY; + return ret; +} + +packer_t *packer_init_archive(const char *path) { + packer_t *ret; + + ret = packer_init(path); + if (ret == NULL) + return NULL; + ret->type = PACKER_TYPE_ARCHIVE; + return ret; } void packer_free(packer_t *ptr) { if (ptr != NULL) { - free(ptr->dir); + free(ptr->str); free(ptr); } } -bool packer_read(packer_t *ctx) { +bool packer_read_dir(packer_t *ctx) { struct stat st; int fd; - char *fn = strcat(ctx->dir, PACKER_DEF_CONF_FN); char *file_c = NULL; - assert(fn != NULL); - fd = open(fn, O_RDONLY); + assert(ctx != NULL); + + if (ctx->type != PACKER_TYPE_DIRECTORY) + return false; + + if (chdir(ctx->str) == -1) + return false; + + fd = open(PACKER_DEF_CONF_FN, O_RDONLY); if (fd == -1) goto error; if (fstat(fd, &st) == -1) @@ -64,13 +92,11 @@ bool packer_read(packer_t *ctx) { if (ctx->json == NULL) goto error; - free(fn); free(file_c); close(fd); return true; error: - free(fn); if (file_c != NULL) free(file_c); close(fd); diff --git a/tests/test_packer.c b/tests/test_packer.c @@ -1,21 +1,38 @@ #include "test.h" -TEST(packer_init) { +TEST(packer_init_dir) { packer_t *ptr = NULL; set_strdup_fail(1); set_malloc_fail(1); - ptr = packer_init("Test 123"); + ptr = packer_init_dir("Test 123"); TEST_ASSERT(ptr != NULL, "Function failed"); - TEST_ASSERT(strcmp(ptr->dir, "Test 123") == 0, "Directory name is wrong"); + TEST_ASSERT(strcmp(ptr->str, "Test 123") == 0, "Directory name is wrong"); packer_free(ptr); - ptr = packer_init("Test 123"); + ptr = packer_init_dir("Test 123"); TEST_ASSERT(ptr == NULL, "Error not raised"); - ptr = packer_init("Test 123"); + ptr = packer_init_dir("Test 123"); + TEST_ASSERT(ptr == NULL, "Error not raised"); + return TEST_SUCCESS; +} + +TEST(packer_init_archive) { + packer_t *ptr = NULL; + + set_strdup_fail(1); + set_malloc_fail(1); + ptr = packer_init_dir("Test 123"); + TEST_ASSERT(ptr != NULL, "Function failed"); + TEST_ASSERT(strcmp(ptr->str, "Test 123") == 0, "Directory name is wrong"); + packer_free(ptr); + ptr = packer_init_dir("Test 123"); + TEST_ASSERT(ptr == NULL, "Error not raised"); + ptr = packer_init_dir("Test 123"); TEST_ASSERT(ptr == NULL, "Error not raised"); return TEST_SUCCESS; } void register_test_packer(void) { - reg_test("packer", packer_init); + reg_test("packer", packer_init_dir); + reg_test("packer", packer_init_archive); }