libmpm

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

commit 70211ab412da9f77b4b251e46ae2041ee4bcbb45
parent 759d3215774064e3efb9c495334646a128a0b05f
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Fri, 31 Mar 2017 00:17:53 +0200

Add(packer): Add function packer_read:

Not tested at all at the moment !

Diffstat:
Minc/packer.h | 18+++++++++++++++++-
Msrc/packer.c | 36++++++++++++++++++++++++++++++++++++
2 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/inc/packer.h b/inc/packer.h @@ -18,9 +18,16 @@ # define PACKER_H # include <libmpm.h> +# include <sys/types.h> +# include <sys/stat.h> +# include <json.h> + + +# define PACKER_DEF_CONF_FN "/package.json" typedef struct packer_s { - char *dir; /*!< Directory of the package */ + char *dir; /*!< Directory of the package */ + json_object *json; /*!< JSON object */ } packer_t; /*! @@ -39,4 +46,13 @@ packer_t *packer_init(const char *dir); */ void packer_free(packer_t *ptr); +/*! + * \brief Read a directory and prepare the packer + * + * \param[in,out] ctx Already initialized packer_t struct + * + * \return true on success, false on failure + */ +bool packer_read(packer_t *ctx); + #endif /* PACKER_H */ diff --git a/src/packer.c b/src/packer.c @@ -40,3 +40,39 @@ void packer_free(packer_t *ptr) { free(ptr); } } + +bool packer_read(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); + if (fd == -1) + goto error; + if (fstat(fd, &st) == -1) + goto error; + + file_c = malloc(st.st_size); + if (file_c == NULL) + goto error; + if (read(fd, file_c, st.st_size) == -1) + goto error; + ctx->json = json_tokener_parse(file_c); + + 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); + return false; +}