libmpm

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

commit 2cc8d569b48fdb4db2eb024b7a5d1f6151448541
parent 4ad00f71de480efb5015d6495fb95f02bdd53b51
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 16 May 2017 15:52:51 +0200

Add(Compile): Proper struct, init and delete functions

Diffstat:
Minc/compile.h | 55++++++++++++++++++++++++++++++++++++++++++++++++++++---
Msrc/compile.c | 36+++++++++++++++++++++++++++++-------
2 files changed, 81 insertions(+), 10 deletions(-)

diff --git a/inc/compile.h b/inc/compile.h @@ -19,14 +19,63 @@ # include <packer.h> +typedef enum { + INST_STATE_NONE = 0, + INST_STATE_CONFIGURATION, + INST_STATE_COMPILATION, + INST_STATE_INSTALLATION, + INST_STATE_DONE +} install_state_t; + +typedef struct compile_s { + packer_t *package; /*!< Pointer to the package */ + char old_pwd[PATH_MAX]; /*!< Old processs path */ + install_state_t state; /*!< State of the installation */ +} compile_t; + + +/*! + * \brief Init a compile_t structure, in order to prepare installation + * + * \param[in] ctx Package to install + * + * This function will create a new compile_t structure, save the current pwd, + * and chdir in the decompressed directory of the package. + * + * \note This function take the ownership of the packer_t pointer + * \return A freshly allocated structure on success, NULL on failure + */ +compile_t *package_install_init(packer_t *ctx); + +/*! + * \brief Cleanup after an installation + * + * \param[in] ctx Installation to cleanup + * + * This function will chdir into the old directory, and free all the structure + * + * \note If the function fails to chdir in the old directory, data will not be + * freed + * \return true on success, false on failure + */ +bool package_install_cleanup(compile_t *ctx); + /*! * \brief Configure a package * * \param[in] ctx Package to configure - * \param[in] dir Sources directory + * + * \return true on success, false on failure */ -bool configure_package(packer_t *ctx); +bool configure_package(compile_t *ctx); -bool make_package(packer_t *ctx); +/*! + * \brief Compile a package + * + * \param[in] ctx Package to compile + * + * \return true on success, false on failure + */ +bool make_package(compile_t *ctx); #endif /* COMPILE_H */ diff --git a/src/compile.c b/src/compile.c @@ -16,23 +16,45 @@ #include <compile.h> -static char old_pwd[PATH_MAX] = ""; +compile_t *package_install_init(packer_t *ctx) { + compile_t *ret = NULL; -bool package_install_init(packer_t *ctx) { - // Chdir, getcwd, cleanup + ret = malloc(sizeof(*ret)); + if (ret == NULL) + return NULL; + + ret->package = ctx; + ret->state = INST_STATE_NONE; + + getcwd(ret->old_pwd, sizeof(ret->old_pwd)); + if (chdir(ctx->out_dir) != 0) + { + free(ret); + return NULL; + } + return ret; +} + +bool package_install_cleanup(compile_t *ctx) { + if (chdir(ctx->old_pwd) != 0) + return false; + + packer_free(ctx->package); + free(ctx); + return true; } -bool configure_package(packer_t *ctx) { +bool configure_package(compile_t *ctx) { /* Nothing to configure, we're good */ - if (ctx->header->compilation->configure == NULL) + if (ctx->package->header->compilation->configure == NULL) return true; return true; } -bool make_package(packer_t *ctx) { +bool make_package(compile_t *ctx) { /* Nothing to compile, we're good */ - if (ctx->header->compilation->make == NULL) + if (ctx->package->header->compilation->make == NULL) return true; return true;