From 41fabe21a282c4406fde234b8092134d5632cc25 Mon Sep 17 00:00:00 2001 From: GreekStapler Date: Tue, 31 Jan 2023 21:08:09 +0000 Subject: [PATCH] refactor: Package archive parser now returns meaningful error enums in case of failure. --- include/vieter_package.h | 12 ++++++++++-- src/package/vieter_package.c | 11 ++++++----- test/package/test_package.c | 11 ++++++----- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/include/vieter_package.h b/include/vieter_package.h index 32fb0eb..c9a941e 100644 --- a/include/vieter_package.h +++ b/include/vieter_package.h @@ -3,13 +3,21 @@ typedef struct vieter_package vieter_package; +typedef enum vieter_package_error { + vieter_package_ok = 0, + vieter_package_unarchive_error = 1, + vieter_package_stat_error = 2 +} vieter_package_error; +/* + * Allocate an empty package + */ +vieter_package *vieter_package_init(); /* * Parse package file into something usable by libvieter. - * The pointer returned by this function will need to freed at a later point. */ -vieter_package *vieter_package_read_archive(const char *pkg_path); +vieter_package_error vieter_package_read_archive(vieter_package *pkg, const char *pkg_path); /* * Deallocate a package. diff --git a/src/package/vieter_package.c b/src/package/vieter_package.c index 031fe15..cf273d8 100644 --- a/src/package/vieter_package.c +++ b/src/package/vieter_package.c @@ -40,7 +40,7 @@ vieter_package *vieter_package_init() { return calloc(sizeof(vieter_package_info), 1); } -vieter_package *vieter_package_read_archive(const char *pkg_path) { +vieter_package_error vieter_package_read_archive(vieter_package *pkg, const char *pkg_path) { struct archive *a = archive_read_new(); struct archive_entry *entry = archive_entry_new(); @@ -57,7 +57,7 @@ vieter_package *vieter_package_read_archive(const char *pkg_path) { // Exit early if we weren't able to successfully open the archive for reading if (r != ARCHIVE_OK) { - return NULL; + return vieter_package_unarchive_error; } int compression_code = archive_filter_code(a, 0); @@ -103,7 +103,9 @@ vieter_package *vieter_package_read_archive(const char *pkg_path) { struct stat stats; if (stat(pkg_path, &stats) != 0) { - return NULL; + // errno is set if stat() fails; the calling function should check + // the value of errno in case vieter_package_stat_error is returned + return vieter_package_stat_error; } pkg_info->csize = stats.st_size; @@ -111,13 +113,12 @@ vieter_package *vieter_package_read_archive(const char *pkg_path) { archive_read_free(a); // Create final return value - vieter_package *pkg = vieter_package_init(); pkg->path = strdup(pkg_path); pkg->info = pkg_info; pkg->files = files; pkg->compression = compression_code; - return pkg; + return vieter_package_ok; } void vieter_package_sha256sum(vieter_package *pkg, char *res) { diff --git a/test/package/test_package.c b/test/package/test_package.c index 5bbda26..3c8b249 100644 --- a/test/package/test_package.c +++ b/test/package/test_package.c @@ -39,8 +39,9 @@ void test_info_parse() { } void test_pkg_read_archive_files() { - vieter_package *pkg = vieter_package_read_archive("./test/package/xcursor-dmz-0.4.5-2-any.pkg.tar.zst"); - TEST_ASSERT_(pkg != NULL, "failure parsing pkg archive"); + vieter_package *pkg = vieter_package_init(); + vieter_package_error e = vieter_package_read_archive(pkg, "./test/package/xcursor-dmz-0.4.5-2-any.pkg.tar.zst"); + TEST_ASSERT_(e == vieter_package_ok, "failure parsing pkg archive"); FILE *f = fopen("./test/package/files", "r"); TEST_ASSERT_(f != NULL, "could not find test files file in ./test/package"); @@ -58,12 +59,12 @@ void test_pkg_read_archive_files() { TEST_CHECK(pkg->compression = 14); vieter_package_free(&pkg); - } void test_pkg_read_archive_desc() { - vieter_package *pkg = vieter_package_read_archive("./test/package/xcursor-dmz-0.4.5-2-any.pkg.tar.zst"); - TEST_ASSERT_(pkg != NULL, "failure parsing pkg archive"); + vieter_package *pkg = vieter_package_init(); + vieter_package_error e = vieter_package_read_archive(pkg, "./test/package/xcursor-dmz-0.4.5-2-any.pkg.tar.zst"); + TEST_ASSERT_(e == vieter_package_ok, "failure parsing pkg archive"); char *description = vieter_package_to_description(pkg);