refactor: Package archive parser now returns meaningful error enums in case of failure.
parent
c7c4043108
commit
4c9b429eaa
|
@ -3,13 +3,21 @@
|
||||||
|
|
||||||
typedef struct vieter_package vieter_package;
|
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.
|
* 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.
|
* Deallocate a package.
|
||||||
|
|
|
@ -40,7 +40,7 @@ vieter_package *vieter_package_init() {
|
||||||
return calloc(sizeof(vieter_package_info), 1);
|
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 *a = archive_read_new();
|
||||||
struct archive_entry *entry = archive_entry_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
|
// Exit early if we weren't able to successfully open the archive for reading
|
||||||
if (r != ARCHIVE_OK) {
|
if (r != ARCHIVE_OK) {
|
||||||
return NULL;
|
return vieter_package_unarchive_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
int compression_code = archive_filter_code(a, 0);
|
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;
|
struct stat stats;
|
||||||
|
|
||||||
if (stat(pkg_path, &stats) != 0) {
|
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;
|
pkg_info->csize = stats.st_size;
|
||||||
|
@ -111,13 +113,12 @@ vieter_package *vieter_package_read_archive(const char *pkg_path) {
|
||||||
archive_read_free(a);
|
archive_read_free(a);
|
||||||
|
|
||||||
// Create final return value
|
// Create final return value
|
||||||
vieter_package *pkg = vieter_package_init();
|
|
||||||
pkg->path = strdup(pkg_path);
|
pkg->path = strdup(pkg_path);
|
||||||
pkg->info = pkg_info;
|
pkg->info = pkg_info;
|
||||||
pkg->files = files;
|
pkg->files = files;
|
||||||
pkg->compression = compression_code;
|
pkg->compression = compression_code;
|
||||||
|
|
||||||
return pkg;
|
return vieter_package_ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vieter_package_sha256sum(vieter_package *pkg, char *res) {
|
void vieter_package_sha256sum(vieter_package *pkg, char *res) {
|
||||||
|
|
|
@ -39,8 +39,9 @@ void test_info_parse() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_pkg_read_archive_files() {
|
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");
|
vieter_package *pkg = vieter_package_init();
|
||||||
TEST_ASSERT_(pkg != NULL, "failure parsing pkg archive");
|
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");
|
FILE *f = fopen("./test/package/files", "r");
|
||||||
TEST_ASSERT_(f != NULL, "could not find test files file in ./test/package");
|
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);
|
TEST_CHECK(pkg->compression = 14);
|
||||||
|
|
||||||
vieter_package_free(&pkg);
|
vieter_package_free(&pkg);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_pkg_read_archive_desc() {
|
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");
|
vieter_package *pkg = vieter_package_init();
|
||||||
TEST_ASSERT_(pkg != NULL, "failure parsing pkg archive");
|
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);
|
char *description = vieter_package_to_description(pkg);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue