refactor: Package archive parser now returns meaningful error enums in case of failure.

This commit is contained in:
GreekStapler 2023-01-31 21:08:09 +00:00
parent c7c4043108
commit 4c9b429eaa
3 changed files with 22 additions and 12 deletions

View file

@ -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) {