libmpm

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

commit 4c91a7db987501e3129f2bb08e76eb3d34a9b327
parent 49dea4cd435234f3da010f3d4679190a28f1f160
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed, 19 Apr 2017 23:05:21 +0200

Add(Packer): Now compressing files and adding them to the archive

Diffstat:
MMakefile | 3++-
Minc/libmpm.h | 5+++++
Minc/packer.h | 1+
Msrc/lib_init.c | 2+-
Msrc/packer.c | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Mtests/Makefile | 2+-
Mtests/main.c | 2++
Mtests/test_database.c | 4+++-
8 files changed, 72 insertions(+), 10 deletions(-)

diff --git a/Makefile b/Makefile @@ -39,7 +39,8 @@ LFLAGS = -L lib/libmorphux -lmorphux \ -L ./lib/curl/lib/.libs/ -lcurl \ -L ./lib/json-c/.libs -ljson-c \ -L ./lib/libconfuse/.libs -lconfuse \ - -L ./lib/libsodium/src/libsodium/.libs/ -lsodium + -L ./lib/libsodium/src/libsodium/.libs/ -lsodium \ + -lz CFLAGS = $(SFLAGS) $(IFLAGS) $(OFLAGS) $(LFLAGS) ARFLAGS = -cq diff --git a/inc/libmpm.h b/inc/libmpm.h @@ -24,4 +24,9 @@ # include <packer.h> # include <package.h> +/*! + * \brief Init the mpm library + */ +void mpm_init(void); + #endif /* LIBMPM_H */ diff --git a/inc/packer.h b/inc/packer.h @@ -26,6 +26,7 @@ # include <dirent.h> # include <limits.h> # include <sodium.h> +# include <zlib.h> # define PACKER_DEF_CONF_FN "package.json" # define PACKER_DEF_EXT ".mpx" diff --git a/src/lib_init.c b/src/lib_init.c @@ -1,5 +1,5 @@ #include <libmpm.h> -static void __attribute__((constructor)) mpm_init(void) { +void mpm_init(void) { sodium_init(); } diff --git a/src/packer.c b/src/packer.c @@ -496,6 +496,51 @@ error: return NULL; } +static bool get_file_information(packer_file_t *file) { + z_stream stream; + char *file_content = NULL; + char *chunk = NULL; + off_t file_size; + + file_size = mpm_get_file_size_from_fn(file->fn); + file_content = mpm_read_file_from_fn(file->fn); + if (file_content == NULL) + { + file->file_size = 0; + file->content = NULL; + return true; + } + + crypto_hash_sha256((unsigned char *)file->sum, + (const unsigned char *)file_content, + file_size); + + chunk = malloc(file_size); + if (chunk == NULL) + return false; + + /* Compress file */ + stream.zalloc = NULL; + stream.zfree = NULL; + stream.opaque = NULL; + stream.avail_in = file_size; + stream.next_in = (Bytef *)file_content; + stream.avail_out = file_size; + stream.next_out = (Bytef *)chunk; + + deflateInit(&stream, Z_DEFAULT_COMPRESSION); + deflate(&stream, Z_FINISH); + deflateEnd(&stream); + + file->file_size = stream.total_out; + file->content = malloc(file->file_size); + memcpy(file->content, chunk, file->file_size); + + free(file_content); + free(chunk); + return true; +} + static bool read_files_from_dir(const char *dir_name, mlist_t **files, mlist_t **dirs) { DIR *dir = opendir(dir_name); struct dirent *dinfo = NULL; @@ -528,11 +573,11 @@ static bool read_files_from_dir(const char *dir_name, mlist_t **files, mlist_t * } else { - file->file_size = mpm_get_file_size_from_fn(file->fn); - file->content = mpm_read_file_from_fn(file->fn); - crypto_hash_sha256((unsigned char *)file->sum, - (const unsigned char *)file->content, - file->file_size); + if (get_file_information(file) == false) + { + packer_file_free(file); + goto error; + } list_add((*files), file, sizeof(*file)); } free(file); @@ -572,7 +617,13 @@ static bool write_package_sources(FILE *fd, packer_t *ctx) { packer_file_t *file; list_for_each(files_list, tmp, file) { - printf("File: %s %s:\n'%s'\n", file->fn, file->sum, file->content); + fprintf(fd, "%s%c", file->fn, 0); + fwrite(&file->file_size, sizeof(file->file_size), 1, fd); + if (file->file_size != 0) + { + fwrite(&file->sum, crypto_hash_sha256_BYTES, 1, fd); + fwrite(file->content, file->file_size, 1, fd); + } } list_free(files_list, packer_file_free); diff --git a/tests/Makefile b/tests/Makefile @@ -21,7 +21,7 @@ SFLAGS = -Wall -Wextra -Wno-unused-result -DCOMPILE_WITH_TEST #-L ../lib/curl/.libs/ -lcurl \ #-L ../lib/json-c/.libs -ljson-c \ -LFLAGS= -L ../ -lmpm -pthread -ldl +LFLAGS= -L ../ -lmpm -pthread -ldl -lz CFLAGS = $(SFLAGS) $(IFLAGS) $(OFLAGS) $(LFLAGS) diff --git a/tests/main.c b/tests/main.c @@ -3,6 +3,8 @@ int main(void) { int ret; + mpm_init(); + register_flags_test(); register_test_database(); register_test_files(); diff --git a/tests/test_database.c b/tests/test_database.c @@ -205,6 +205,7 @@ TEST(database_init_3) { DUP_ALL_OUTPUTS(fd); ret = mpm_database_init(ptr); TEST_ASSERT((ret != 0), "Error did not trigger"); + exit(0); } else { WAIT_AND_CLOSE(pid, st, fd); } @@ -697,6 +698,7 @@ TEST(database_sql_to_file) { if ((pid = fork()) == 0) { DUP_ALL_OUTPUTS(fd); ptr = sql_to_file(ptr, "Unknown", "Nothing"); + exit(0); } else { WAIT_AND_CLOSE(pid, st, fd); TEST_ASSERT((WEXITSTATUS(st) == 1), "Exit code is wrong"); @@ -720,7 +722,7 @@ TEST(database_sql_to_categ) { if ((pid = fork()) == 0) { DUP_ALL_OUTPUTS(fd); sql_to_category(ptr, "Unknown", "Nothing"); - exit(0); + exit(0); } else { WAIT_AND_CLOSE(pid, st, fd); TEST_ASSERT((WEXITSTATUS(st) == 1), "Exit code is wrong");