From 0f9c8efe639d762f8b4d3dca54aa03b470102e7a Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 2 Oct 2024 11:38:39 +0900 Subject: [PATCH] Add support for writing multiple file formats --- src/archiver.c | 23 +++++++++++++++++++++++ src/archiver.h | 9 ++++++++- src/parser.c | 24 ++++++++++++++++++++++++ src/parser.h | 2 ++ 4 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/archiver.c b/src/archiver.c index fbb91a8..89b2927 100644 --- a/src/archiver.c +++ b/src/archiver.c @@ -1301,6 +1301,8 @@ char *simple_archiver_error_to_string(enum SDArchiverStateReturns error) { return "Failed to extract symlink (internal error)"; case SDAS_FAILED_TO_CHANGE_CWD: return "Failed to change current working directory"; + case SDAS_INVALID_WRITE_VERSION: + return "Unsupported write version file format"; default: return "Unknown error"; } @@ -1332,6 +1334,20 @@ void simple_archiver_free_state(SDArchiverState **state) { int simple_archiver_write_all(FILE *out_f, SDArchiverState *state, const SDArchiverLinkedList *filenames) { + switch (state->parsed->write_version) { + case 0: + return simple_archiver_write_v0(out_f, state, filenames); + case 1: + return simple_archiver_write_v1(out_f, state, filenames); + default: + fprintf(stderr, "ERROR: Unsupported write version %u!\n", + state->parsed->write_version); + return SDAS_INVALID_WRITE_VERSION; + } +} + +int simple_archiver_write_v0(FILE *out_f, SDArchiverState *state, + const SDArchiverLinkedList *filenames) { // First create a "set" of absolute paths to given filenames. __attribute__((cleanup(simple_archiver_hash_map_free))) SDArchiverHashMap *abs_filenames = simple_archiver_hash_map_init(); @@ -1471,6 +1487,13 @@ int simple_archiver_write_all(FILE *out_f, SDArchiverState *state, return SDAS_SUCCESS; } +int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state, + const SDArchiverLinkedList *filenames) { + // TODO Impl. + fprintf(stderr, "Writing v1 unimplemented\n"); + return SDAS_INTERNAL_ERROR; +} + int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract, const SDArchiverState *state) { uint8_t buf[32]; diff --git a/src/archiver.h b/src/archiver.h index 51b6abc..2b8fbf2 100644 --- a/src/archiver.h +++ b/src/archiver.h @@ -51,7 +51,8 @@ enum SDArchiverStateReturns { SDAS_INTERNAL_ERROR, SDAS_FAILED_TO_CREATE_MAP, SDAS_FAILED_TO_EXTRACT_SYMLINK, - SDAS_FAILED_TO_CHANGE_CWD + SDAS_FAILED_TO_CHANGE_CWD, + SDAS_INVALID_WRITE_VERSION }; /// Returned pointer must not be freed. @@ -65,6 +66,12 @@ void simple_archiver_free_state(SDArchiverState **state); int simple_archiver_write_all(FILE *out_f, SDArchiverState *state, const SDArchiverLinkedList *filenames); +int simple_archiver_write_v0(FILE *out_f, SDArchiverState *state, + const SDArchiverLinkedList *filenames); + +int simple_archiver_write_v1(FILE *out_f, SDArchiverState *state, + const SDArchiverLinkedList *filenames); + /// Returns zero on success. int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract, const SDArchiverState *state); diff --git a/src/parser.c b/src/parser.c index 5da4653..2bc5b25 100644 --- a/src/parser.c +++ b/src/parser.c @@ -169,6 +169,9 @@ void simple_archiver_print_usage(void) { fprintf(stderr, "--temp-files-dir : where to store temporary files created " "when compressing (defaults to current working directory)\n"); + fprintf(stderr, + "--write-version : Force write version file format " + "(default 1)\n"); fprintf(stderr, "-- : specifies remaining arguments are files to archive/extract\n"); fprintf( @@ -189,6 +192,7 @@ SDArchiverParsed simple_archiver_create_parsed(void) { parsed.working_files = NULL; parsed.temp_dir = NULL; parsed.user_cwd = NULL; + parsed.write_version = 0; return parsed; } @@ -299,6 +303,26 @@ int simple_archiver_parse_args(int argc, const char **argv, out->temp_dir = argv[1]; --argc; ++argv; + } else if (strcmp(argv[0], "--write-version") == 0) { + if (argc < 2) { + fprintf(stderr, + "ERROR: --write-version expects an integer argument!\n"); + simple_archiver_print_usage(); + return 1; + } + int version = atoi(argv[1]); + if (version < 0) { + fprintf(stderr, "ERROR: --write-version cannot be negative!\n"); + simple_archiver_print_usage(); + return 1; + } else if (version > 1) { + fprintf(stderr, "ERROR: --write-version must be 0 or 1!\n"); + simple_archiver_print_usage(); + return 1; + } + out->write_version = (uint32_t)version; + --argc; + ++argv; } else if (argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == 0) { is_remaining_args = 1; } else if (argv[0][0] != '-') { diff --git a/src/parser.h b/src/parser.h index ad2e472..1c74427 100644 --- a/src/parser.h +++ b/src/parser.h @@ -51,6 +51,8 @@ typedef struct SDArchiverParsed { const char *temp_dir; /// Dir specified by "-C". const char *user_cwd; + /// Currently only 0 and 1 is supported. + uint32_t write_version; } SDArchiverParsed; typedef struct SDArchiverFileInfo {