diff --git a/epitar/src/main.c b/epitar/src/main.c index 1e12eb4..7c84e2d 100644 --- a/epitar/src/main.c +++ b/epitar/src/main.c @@ -3,6 +3,52 @@ #include "utils/config.h" #include "utils/errors.h" +static int handle_err_code(enum error_code err, struct config *config) +{ + int ret_code = 0; + switch (err) + { + case SUCCESS: + ret_code = 0; + break; + + // case UNKNOWN_ERROR: + // printf("epitar: error extracting tarball %s\n", + // config.archive_file); ret_code = 3; break; + + case BAD_CHECKSUM: + puts("epitar: bad checksum"); + ret_code = 2; + break; + + case NOT_IMPLEMENTED: + puts("epitar: Function not implemented"); + ret_code = 2; + break; + + case FILE_NOT_FOUND: + if (config->mode == ARCHIVE) + { + // Rest of code should have printed the first part of the line + printf(" to tarball %s\n", config->archive_file); + ret_code = 3; + break; + } + __attribute__((fallthrough)); + + case EMPTY_ARCHIVE: + puts("epitar: cowardly refusing to create an empty archive"); + ret_code = 1; + break; + + default: + printf("epitar: error extracting tarball %s\n", config->archive_file); + ret_code = 3; + break; + } + return ret_code; +} + int main(int argc, char **argv) { // Handle args @@ -32,48 +78,7 @@ int main(int argc, char **argv) err = archive(config.archive_file, config.input_files, config.verbose); } - int ret_code = 0; - switch (err) - { - case SUCCESS: - ret_code = 0; - break; - - // case UNKNOWN_ERROR: - // printf("epitar: error extracting tarball %s\n", - // config.archive_file); ret_code = 3; break; - - case BAD_CHECKSUM: - puts("epitar: bad checksum"); - ret_code = 2; - break; - - case NOT_IMPLEMENTED: - puts("epitar: Function not implemented"); - ret_code = 2; - break; - - case FILE_NOT_FOUND: - if (config.mode == ARCHIVE) - { - // Rest of code should have printed the first part of the line - printf(" to tarball %s\n", config.archive_file); - ret_code = 3; - break; - } - __attribute__((fallthrough)); - - case EMPTY_ARCHIVE: - puts("epitar: cowardly refusing to create an empty archive"); - ret_code = 1; - break; - - default: - printf("epitar: error extracting tarball %s\n", config.archive_file); - ret_code = 3; - break; - } - + int ret_code = handle_err_code(err, &config); destroy_config(&config); return ret_code; } diff --git a/epitar/src/tar/archive.h b/epitar/src/tar/archive.h index 546f4ff..f9dd21f 100644 --- a/epitar/src/tar/archive.h +++ b/epitar/src/tar/archive.h @@ -1,10 +1,10 @@ #ifndef ARCHIVE_H #define ARCHIVE_H -#include "../utils/errors.h" - #include +#include "../utils/errors.h" + /* @brief Archives the listed `files` into `archive_name` * @return 0 on success, the corresponding error code otherwise */ diff --git a/epitar/src/tar/extract.c b/epitar/src/tar/extract.c index 77ffcbd..b085ef5 100644 --- a/epitar/src/tar/extract.c +++ b/epitar/src/tar/extract.c @@ -49,9 +49,14 @@ static enum error_code extract_ustar(FILE *stream, struct ustar_header *header) static enum error_code extract_unixtar(FILE *stream, struct unixtar_header *header) { - (void)stream; - (void)header; - return NOT_IMPLEMENTED; + struct file_stats stats = { .name = header->name, + .length = strtol(header->size, NULL, 8), + .content_stream = stream, + .mode = header->mode, + .uid = header->uid, + .gid = header->gid, + .mtime = header->mtime }; + return create_file(&stats); } /** diff --git a/epitar/src/utils/filesystem.c b/epitar/src/utils/filesystem.c index 6f3f237..9116034 100644 --- a/epitar/src/utils/filesystem.c +++ b/epitar/src/utils/filesystem.c @@ -180,7 +180,7 @@ char *listdirectory(char *path) return de->d_name; } -void freedirectory() +void freedirectory(void) { if (current_dir != NULL) { diff --git a/epitar/src/utils/filesystem.h b/epitar/src/utils/filesystem.h index b504c4d..160c800 100644 --- a/epitar/src/utils/filesystem.h +++ b/epitar/src/utils/filesystem.h @@ -59,6 +59,6 @@ char *listdirectory(char *path); * related allocated memory * @warn Use this function if you only partially listed a directory previously. */ -void freedirectory(); +void freedirectory(void); #endif // FILESYSTEM_H diff --git a/epitar/src/utils/misc.c b/epitar/src/utils/misc.c deleted file mode 100644 index 0271150..0000000 --- a/epitar/src/utils/misc.c +++ /dev/null @@ -1,3 +0,0 @@ -#include "utils.h" - -#include diff --git a/epitar/src/utils/misc.h b/epitar/src/utils/misc.h deleted file mode 100644 index 3d20ecc..0000000 --- a/epitar/src/utils/misc.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef MISC_H -#define MISC_H - -void print_str_array(const char **input_files); - -#endif /* MISC_H */