libmpm

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

commit 41407479e7b375185383ea4e5513612cff86ff29
parent 1e50ed6dd0ed887976cbfcf5fb57e03257c5dea6
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Wed,  3 May 2017 19:37:53 +0200

Add(Packer): Beginning of reading files in archive:

Update some tests too

Diffstat:
Msrc/packer.c | 42++++++++++++++++++++++++++++++++++++++++--
Mtests/test_packer.c | 9+++++----
2 files changed, 45 insertions(+), 6 deletions(-)

diff --git a/src/packer.c b/src/packer.c @@ -879,12 +879,45 @@ cleanup: return false; } +static bool read_package_files(char *content, packer_t *ctx, off_t total_size) { + if (content == NULL || ctx == NULL) + return false; + + off_t size = 0; + off_t ctr = 0; + char *name = NULL; + char *f_content = NULL; + char hash[crypto_hash_sha256_BYTES] = { '\0' }; + + while (total_size > ctr) { + + /* File name */ + name = strdup(content + ctr); + ctr += strlen(name) + 1; + + /* File content size */ + memcpy(&size, content + ctr, sizeof(size)); + ctr += sizeof(size); + + if (size > 0) { + /* File hash */ + memcpy(hash, content + ctr, crypto_hash_sha256_BYTES); + ctr += crypto_hash_sha256_BYTES; + + /* File actual content */ + ctr += size; + } + } + return true; +} + bool packer_read_archive(packer_t *ctx) { int fd; bool ret; char *archive = NULL; int cur = 0; + off_t size; if (ctx->type != PACKER_TYPE_ARCHIVE) return false; @@ -894,11 +927,16 @@ bool packer_read_archive(packer_t *ctx) return false; archive = mpm_read_file_from_fd(fd); + size = mpm_get_file_size_from_fd(fd); - printf("%d\n", cur); ret = read_package_header(archive, ctx, &cur); - printf("%d\n", cur); + if (!ret) + goto cleanup; + + ret = read_package_files(archive + cur, ctx, size - cur); +cleanup: close(fd); + free(archive); return ret; } diff --git a/tests/test_packer.c b/tests/test_packer.c @@ -311,22 +311,23 @@ TEST(packer_create_archive_2) { TEST_ASSERT(packer_read_dir(ptr) == true, "An error happened"); set_malloc_fail(2); - TEST_ASSERT(packer_create_archive(ptr, "package.mpx") == false, + TEST_ASSERT(packer_create_archive(ptr, "package_2.mpx") == false, "Error did not raise") set_malloc_fail(3); - TEST_ASSERT(packer_create_archive(ptr, "package.mpx") == false, + TEST_ASSERT(packer_create_archive(ptr, "package_2.mpx") == false, "Error did not raise") set_malloc_fail(8); - TEST_ASSERT(packer_create_archive(ptr, "package.mpx") == false, + TEST_ASSERT(packer_create_archive(ptr, "package_2.mpx") == false, "Error did not raise") set_malloc_fail(15); - TEST_ASSERT(packer_create_archive(ptr, "package.mpx") == false, + TEST_ASSERT(packer_create_archive(ptr, "package_2.mpx") == false, "Error did not raise") packer_free(ptr); + unlink("tests/package_2.mpx"); return TEST_SUCCESS; }