libmpm

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

commit 4ad00f71de480efb5015d6495fb95f02bdd53b51
parent e6ae59b6a395dfc86034aad0ac3544d2aef72944
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 16 May 2017 12:00:55 +0200

Work(Compilation): Beginning of the work on source compilation:

Fixed a null handling in package.json

Diffstat:
Ainc/compile.h | 32++++++++++++++++++++++++++++++++
Minc/libmpm.h | 3++-
Minc/packer.h | 18+++++++++---------
Minc/packer_file.h | 4+++-
Asrc/compile.c | 39+++++++++++++++++++++++++++++++++++++++
Msrc/packer.c | 49++++++++++++++++++++++---------------------------
Mtests/main.c | 1+
Atests/packer/compilation/package.json | 17+++++++++++++++++
Atests/packer/compilation/patches/awesome_patch.patch | 10++++++++++
Atests/packer/compilation/scripts/after.sh | 2++
Atests/packer/compilation/scripts/before.sh | 2++
Atests/packer/compilation/srcs/Makefile | 2++
Atests/packer/compilation/srcs/main.c | 6++++++
Mtests/test.h | 1+
Atests/test_compile.c | 30++++++++++++++++++++++++++++++
Mtests/test_packer.c | 48++++++++----------------------------------------
16 files changed, 186 insertions(+), 78 deletions(-)

diff --git a/inc/compile.h b/inc/compile.h @@ -0,0 +1,32 @@ +/*********************************** 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 COMPILE_H +# define COMPILE_H + +# include <packer.h> + +/*! + * \brief Configure a package + * + * \param[in] ctx Package to configure + * \param[in] dir Sources directory + */ +bool configure_package(packer_t *ctx); + +bool make_package(packer_t *ctx); + +#endif /* COMPILE_H */ diff --git a/inc/libmpm.h b/inc/libmpm.h @@ -21,8 +21,9 @@ # include <flags.h> # include <database.h> # include <config.h> -# include <packer.h> # include <package.h> +# include <compile.h> +# include <packer.h> /*! * \brief Init the mpm library diff --git a/inc/packer.h b/inc/packer.h @@ -17,7 +17,7 @@ #ifndef PACKER_H # define PACKER_H -# include <libmpm.h> +# define _GNU_SOURCE # include <sys/types.h> # include <sys/stat.h> # include <json.h> @@ -26,6 +26,7 @@ # include <dirent.h> # include <limits.h> # include <packer_file.h> +# include <stdio.h> # define PACKER_DEF_CONF_FN "package.json" # define PACKER_DEF_EXT ".mpx" @@ -88,9 +89,12 @@ typedef struct packer_s { json_object *json; /*!< JSON object */ packer_type_t type; /*!< Type of packer */ packer_header_t *header; /*!< Header of the MPX format */ - mlist_t *files; /*!< Files list, if it's an archive */ + mlist_t *files; /*!< Files list, if it's an archive */ + char *out_dir;/*!< Output directory, in case of an archive */ } packer_t; +# include <libmpm.h> + /*! * \brief Allocate, fill and init a packer_t structure * @@ -168,19 +172,15 @@ bool packer_read_archive_header(packer_t *ctx); * * \param[in] ctx Archive to extract * \param[in] dir Parent directory to extract to - * \param[out] output_dir Name of the main extracted directory * * This function will extract an MPX archive in a designated dir * It will create a main directory too, with the format NAME-VERSION * For example, if the call is: - * packer_extract_archive(ctx, "/tmp", &ptr); - * The output directory will be /tmp/NAME-VERSION (Ex: /tmp/test-2.0) - * - * The output_dir parameter is allocated by this function, caller should take - * care of the free + * packer_extract_archive(ctx, "/tmp"); + * The output directory can be found in the member out_dir in packer_t * * \return true on success, false on failure */ -bool packer_extract_archive(packer_t *ctx, const char *dir, char **output_dir); +bool packer_extract_archive(packer_t *ctx, const char *dir); #endif /* PACKER_H */ diff --git a/inc/packer_file.h b/inc/packer_file.h @@ -17,9 +17,11 @@ #ifndef PACKER_FILE_H # define PACKER_FILE_H -# include <libmpm.h> +# include <morphux.h> # include <sodium.h> # include <zlib.h> +# include <sys/types.h> +# include <dirent.h> typedef struct packer_file_s { char *fn; /*!< Filename, with relative path (Ex: srcs/main.c ) */ diff --git a/src/compile.c b/src/compile.c @@ -0,0 +1,39 @@ +/*********************************** 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 <compile.h> + +static char old_pwd[PATH_MAX] = ""; + +bool package_install_init(packer_t *ctx) { + // Chdir, getcwd, cleanup +} + +bool configure_package(packer_t *ctx) { + /* Nothing to configure, we're good */ + if (ctx->header->compilation->configure == NULL) + return true; + + return true; +} + +bool make_package(packer_t *ctx) { + /* Nothing to compile, we're good */ + if (ctx->header->compilation->make == NULL) + return true; + + return true; +} diff --git a/src/packer.c b/src/packer.c @@ -28,6 +28,7 @@ MPX_STATIC packer_t *packer_init(const char *str) { ret->str = strdup(str); ret->header = NULL; ret->files = NULL; + ret->out_dir = NULL; if (ret->str == NULL) goto cleanup; @@ -182,6 +183,7 @@ void packer_free(packer_t *ptr) { json_object_put(ptr->json); free(ptr->str); packer_header_free(ptr->header); + free(ptr->out_dir); if (ptr->files != NULL) { list_free(ptr->files, packer_file_free); @@ -210,8 +212,15 @@ MPX_STATIC bool packer_read_config_comp(packer_t *ctx, struct json_object *obj) tmp = json_object_iter_peek_value(&it); if (strcmp(name, PACKER_CONF_COMP_CONF_TOKEN) == 0) { + if (json_object_get_type(tmp) == json_type_null) + { + json_object_iter_next(&it); + continue ; + } + if (json_object_get_type(tmp) != json_type_array) goto cleanup; + for (size_t i = 0; i < json_object_array_length(tmp); i++) { struct json_object *array_ent, *array_tmp; @@ -449,11 +458,14 @@ MPX_STATIC void write_package_header(FILE *fd, packer_t *ctx) { conf_len = htonl(list_size(h->compilation->configure)); fwrite(&conf_len, sizeof(u32_t), 1, fd); - 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); + if (list_size(h->compilation->configure) != 0) + { + 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); @@ -806,25 +818,7 @@ bool packer_read_archive_header(packer_t *ctx) { return ret; } -MPX_STATIC char *packer_create_directory_name(packer_t *ctx, char sep) { - char *out = NULL; - - out = malloc(strlen(ctx->header->package->name) - + strlen(ctx->header->package->version) + 2); - if (out == NULL) - return NULL; - - strncpy(out, ctx->header->package->name, strlen(ctx->header->package->name)); - out[strlen(ctx->header->package->name)] = sep; - strncpy(out + strlen(ctx->header->package->name) + 1, - ctx->header->package->version, strlen(ctx->header->package->version)); - out[strlen(ctx->header->package->name) + - strlen(ctx->header->package->version) + 1] = '\0'; - - return out; -} - -bool packer_extract_archive(packer_t *ctx, const char *dir, char **output_dir) { +bool packer_extract_archive(packer_t *ctx, const char *dir) { packer_file_t *file = NULL; int fd, t_ctr; bool ret = false; @@ -852,12 +846,13 @@ bool packer_extract_archive(packer_t *ctx, const char *dir, char **output_dir) { if (chdir(dir) == -1) goto cleanup; - *output_dir = packer_create_directory_name(ctx, '-'); + asprintf(&ctx->out_dir, "%s/%s-%s/", dir, + ctx->header->package->name, ctx->header->package->version); - if (mkdir(*output_dir, S_IRWXU | S_IRWXG | S_IRWXO) == -1 && errno != EEXIST) + if (mkdir(ctx->out_dir, S_IRWXU | S_IRWXG | S_IRWXO) == -1 && errno != EEXIST) goto cleanup; - chdir(*output_dir); + chdir(ctx->out_dir); while (size > ctr) { diff --git a/tests/main.c b/tests/main.c @@ -11,6 +11,7 @@ int main(void) { register_test_category(); register_test_config(); register_test_packer(); + register_test_compile(); m_info("Beginning tests...\n"); ret = test_all(); diff --git a/tests/packer/compilation/package.json b/tests/packer/compilation/package.json @@ -0,0 +1,17 @@ +{ + "package": { + "name": "test", + "version": "2.0", + "description": "This is a description. Yeah I know, mind blowing." + }, + "compilation": { + "configure": null, + "make": "", + "test": "", + "install": "" + }, + "dependencies": [ + "something-5.5", + "yrdy" + ] +} diff --git a/tests/packer/compilation/patches/awesome_patch.patch b/tests/packer/compilation/patches/awesome_patch.patch @@ -0,0 +1,10 @@ +--- main.c.orig 2017-04-21 19:08:47.207443526 +0200 ++++ main.c 2017-04-21 19:08:52.499450888 +0200 +@@ -1,6 +1,6 @@ + #include <stdio.h> + + int main(void) { +- printf("Hello World !\n"); ++ printf("Hello World!\n"); + return 0; + } diff --git a/tests/packer/compilation/scripts/after.sh b/tests/packer/compilation/scripts/after.sh @@ -0,0 +1,2 @@ +#!/bin/sh +echo "After !"; diff --git a/tests/packer/compilation/scripts/before.sh b/tests/packer/compilation/scripts/before.sh @@ -0,0 +1,2 @@ +#!/bin/sh +echo "Before !"; diff --git a/tests/packer/compilation/srcs/Makefile b/tests/packer/compilation/srcs/Makefile @@ -0,0 +1,2 @@ +all: + gcc main.c -o main diff --git a/tests/packer/compilation/srcs/main.c b/tests/packer/compilation/srcs/main.c @@ -0,0 +1,6 @@ +#include <stdio.h> + +int main(void) { + printf("Hello World !\n"); + return 0; +} diff --git a/tests/test.h b/tests/test.h @@ -16,6 +16,7 @@ void register_test_files(void); void register_test_category(void); void register_test_config(void); void register_test_packer(void); +void register_test_compile(void); void clean_db(const char *name); int free_sql_results(void *ptr); diff --git a/tests/test_compile.c b/tests/test_compile.c @@ -0,0 +1,30 @@ +#include "test.h" + +#define PACKAGE_OUTPUT_FN "package_compile" PACKER_DEF_EXT + +TEST(init_compile) { + packer_t *ptr; + + ptr = packer_init_dir("packer/compilation//"); + TEST_ASSERT(packer_read_dir(ptr) == true, "An error happened"); + TEST_ASSERT(packer_create_archive(ptr, PACKAGE_OUTPUT_FN) == true, + "An error happened"); + + packer_free(ptr); + return TEST_SUCCESS; +} + +TEST(configure_package) { + packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); + + packer_extract_archive(ctx, "/tmp"); + TEST_ASSERT(configure_package(ctx) == true, "An error happened"); + TEST_ASSERT(make_package(ctx) == true, "An error happened"); + + return TEST_SUCCESS; +} + +void register_test_compile(void) { + reg_test("compile", init_compile) + reg_test("compile", configure_package); +} diff --git a/tests/test_packer.c b/tests/test_packer.c @@ -514,11 +514,9 @@ TEST(packer_file_init) { TEST(packer_extract_archive_1) { packer_t *ctx = packer_init_archive(PACKAGE_OUTPUT_FN); - char *output_dir = NULL; - TEST_ASSERT(packer_extract_archive(ctx, "/tmp", &output_dir), "Wrong return"); + TEST_ASSERT(packer_extract_archive(ctx, "/tmp"), "Wrong return"); packer_free(ctx); - free(output_dir); return TEST_SUCCESS; } @@ -552,23 +550,6 @@ TEST(packer_file_to_disk) { return TEST_SUCCESS; } -MPX_STATIC char *packer_create_directory_name(packer_t *ctx, char sep); -TEST(packer_create_directory_name) { - packer_t *ctx = NULL; - - ctx = packer_init("0"); - ctx->header = packer_header_init(); - ctx->header->package = packer_header_package_init(); - ctx->header->package->name = strdup("1"); - ctx->header->package->version = strdup("2"); - - set_malloc_fail(0); - TEST_ASSERT(packer_create_directory_name(ctx, '_') == NULL, "Error did not raise"); - - packer_free(ctx); - return TEST_SUCCESS; -} - TEST(packer_read_archive_header) { packer_t *ctx = NULL; @@ -605,48 +586,36 @@ TEST(read_package_files) { TEST(packer_extract_archive_2) { packer_t *ctx = NULL; - char *output_dir = NULL; ctx = packer_init_dir("Test"); - TEST_ASSERT(packer_extract_archive(ctx, NULL, NULL) == false, "Error did not raise"); + TEST_ASSERT(packer_extract_archive(ctx, NULL) == false, "Error did not raise"); packer_free(ctx); ctx = packer_init_archive("Not a valid file"); - TEST_ASSERT(packer_extract_archive(ctx, NULL, NULL) == false, "Error did not raise"); + TEST_ASSERT(packer_extract_archive(ctx, NULL) == false, "Error did not raise"); packer_free(ctx); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); set_malloc_fail(0); - TEST_ASSERT(packer_extract_archive(ctx, NULL, NULL) == false, "Error did not raise"); + TEST_ASSERT(packer_extract_archive(ctx, NULL) == false, "Error did not raise"); packer_free(ctx); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); - TEST_ASSERT(packer_extract_archive(ctx, "/totally/not/valid/dir", NULL) == false, "Error did not raise"); + TEST_ASSERT(packer_extract_archive(ctx, "/totally/not/valid/dir") == false, "Error did not raise"); packer_free(ctx); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); - TEST_ASSERT(packer_extract_archive(ctx, "/", &output_dir) == false, "Error did not raise"); - free(output_dir); + TEST_ASSERT(packer_extract_archive(ctx, "/") == false, "Error did not raise"); packer_free(ctx); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); set_malloc_fail(12); - TEST_ASSERT(packer_extract_archive(ctx, "/tmp", &output_dir) == false, "Error did not raise"); - free(output_dir); + TEST_ASSERT(packer_extract_archive(ctx, "/tmp") == false, "Error did not raise"); packer_free(ctx); ctx = packer_init_archive(PACKAGE_OUTPUT_FN); set_mkdir_fail(1); - TEST_ASSERT(packer_extract_archive(ctx, "/tmp", &output_dir) == false, "Error did not raise"); - - char *to_del = NULL; - - asprintf(&to_del, "/tmp/%s", output_dir); - TEST_ASSERT(recursive_delete(to_del) == true, "Cannot delete directory") - - free(output_dir); - free(to_del); - + TEST_ASSERT(packer_extract_archive(ctx, "/tmp") == false, "Error did not raise"); packer_free(ctx); return TEST_SUCCESS; @@ -707,7 +676,6 @@ void register_test_packer(void) { reg_test("packer", packer_extract_archive_2); reg_test("packer", read_packer_file_from_binary); reg_test("packer", packer_file_to_disk); - reg_test("packer", packer_create_directory_name); reg_test("packer", packer_read_archive_header); reg_test("packer", read_package_files); reg_test("packer", packer_create_archive_cleanup);