libmpm

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

commit 8f2cb17b0bf9b95b96a47b2892733efb487059d5
parent f842b4e0fb284e3f84dc53bbfa2bfa5484df97a5
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 11 Apr 2017 18:34:21 +0200

Add(Packer): Beggining of archive creation

Diffstat:
Minc/packer.h | 17++++++++++++++++-
Msrc/packer.c | 42++++++++++++++++++++++++++++++++++++++++++
Mtests/test_packer.c | 11+++++++++++
3 files changed, 69 insertions(+), 1 deletion(-)

diff --git a/inc/packer.h b/inc/packer.h @@ -23,6 +23,7 @@ # include <json.h> # define PACKER_DEF_CONF_FN "package.json" +# define PACKER_DEF_EXT ".mpx" typedef enum packer_type_e { PACKER_TYPE_DIRECTORY, @@ -40,7 +41,6 @@ typedef enum packer_type_e { # define PACKER_CONF_COMP_INST_TOKEN "install" # define PACKER_CONF_DEPS_TOKEN "dependencies" - typedef struct packer_header_package_s { char *name; /*!< Name of the package */ char *version; /*!< Version of the package */ @@ -113,4 +113,19 @@ void packer_free(packer_t *ptr); */ bool packer_read_dir(packer_t *ctx); +/*! + * \brief Create a .mpx archive with a directory + * + * \param[in] ctx Already initialized packer_t struct + * \param[in] archive_path Filename of the output + * + * Create a .mpx archive with archive with an already parsed directory. + * + * \note This function is not NULL safe against ctx and archive_path parameters, + * it will raise an assertion + * + * \return True on success, false on failure + */ +bool packer_create_archive(packer_t *ctx, const char *archive_path); + #endif /* PACKER_H */ diff --git a/src/packer.c b/src/packer.c @@ -413,3 +413,45 @@ error: return false; } +MPX_STATIC void write_package_header(FILE *fd, packer_t *ctx) { + packer_header_t *h = ctx->header; + mlist_t *tmp = NULL; + packer_conf_opt_t *opt = NULL; + const char *tmp_str = NULL; + + fprintf(fd, "%s%c", h->package->name, 0); + fprintf(fd, "%s%c", h->package->version, 0); + fprintf(fd, "%s%c", h->package->description, 0); + + fprintf(fd, "%d", list_size(h->compilation->configure)); + list_for_each(h->compilation->configure, tmp, opt) { + if (opt->name != NULL) + fprintf(fd, "%s:%s%c", opt->name, opt->value, 0); + else + fprintf(fd, "%s%c", opt->value, 0); + } + fprintf(fd, "%s%c", h->compilation->make, 0); + fprintf(fd, "%s%c", h->compilation->test, 0); + fprintf(fd, "%s%c", h->compilation->install, 0); + + list_for_each(h->dependencies->list, tmp, tmp_str) { + fprintf(fd, "%s%c", tmp_str, 0); + } +} + +bool packer_create_archive(packer_t *ctx, const char *archive_path) { + FILE *fd; + + assert(archive_path == NULL || ctx != NULL); + if (ctx->type != PACKER_TYPE_DIRECTORY) + return false; + + fd = fopen(archive_path, "w+"); + if (fd == NULL) + return false; + + write_package_header(fd, ctx); + fclose(fd); + return true; +} + diff --git a/tests/test_packer.c b/tests/test_packer.c @@ -261,6 +261,16 @@ TEST(packer_header_deps_init) { return TEST_SUCCESS; } +TEST(packer_create_archive_1) { + packer_t *ptr; + + ptr = packer_init_dir("packer/right/"); + TEST_ASSERT(packer_read_dir(ptr) == true, "An error happened"); + TEST_ASSERT(packer_create_archive(ptr, "package" PACKER_DEF_EXT) == true, + "An error happened"); + return TEST_SUCCESS; +} + void register_test_packer(void) { reg_test("packer", packer_init_dir); reg_test("packer", packer_init_archive); @@ -291,5 +301,6 @@ void register_test_packer(void) { reg_test("packer", packer_conf_opt_init); reg_test("packer", packer_header_comp_init); reg_test("packer", packer_header_deps_init); + reg_test("packer", packer_create_archive_1); }