Add support for writing multiple file formats
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 5s

This commit is contained in:
Stephen Seo 2024-10-02 11:38:39 +09:00
parent f89e5f7640
commit 0f9c8efe63
4 changed files with 57 additions and 1 deletions

View file

@ -1301,6 +1301,8 @@ char *simple_archiver_error_to_string(enum SDArchiverStateReturns error) {
return "Failed to extract symlink (internal error)"; return "Failed to extract symlink (internal error)";
case SDAS_FAILED_TO_CHANGE_CWD: case SDAS_FAILED_TO_CHANGE_CWD:
return "Failed to change current working directory"; return "Failed to change current working directory";
case SDAS_INVALID_WRITE_VERSION:
return "Unsupported write version file format";
default: default:
return "Unknown error"; return "Unknown error";
} }
@ -1332,6 +1334,20 @@ void simple_archiver_free_state(SDArchiverState **state) {
int simple_archiver_write_all(FILE *out_f, SDArchiverState *state, int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
const SDArchiverLinkedList *filenames) { 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. // First create a "set" of absolute paths to given filenames.
__attribute__((cleanup(simple_archiver_hash_map_free))) __attribute__((cleanup(simple_archiver_hash_map_free)))
SDArchiverHashMap *abs_filenames = simple_archiver_hash_map_init(); 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; 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, int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract,
const SDArchiverState *state) { const SDArchiverState *state) {
uint8_t buf[32]; uint8_t buf[32];

View file

@ -51,7 +51,8 @@ enum SDArchiverStateReturns {
SDAS_INTERNAL_ERROR, SDAS_INTERNAL_ERROR,
SDAS_FAILED_TO_CREATE_MAP, SDAS_FAILED_TO_CREATE_MAP,
SDAS_FAILED_TO_EXTRACT_SYMLINK, 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. /// 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, int simple_archiver_write_all(FILE *out_f, SDArchiverState *state,
const SDArchiverLinkedList *filenames); 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. /// Returns zero on success.
int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract, int simple_archiver_parse_archive_info(FILE *in_f, int_fast8_t do_extract,
const SDArchiverState *state); const SDArchiverState *state);

View file

@ -169,6 +169,9 @@ void simple_archiver_print_usage(void) {
fprintf(stderr, fprintf(stderr,
"--temp-files-dir <dir> : where to store temporary files created " "--temp-files-dir <dir> : where to store temporary files created "
"when compressing (defaults to current working directory)\n"); "when compressing (defaults to current working directory)\n");
fprintf(stderr,
"--write-version <version> : Force write version file format "
"(default 1)\n");
fprintf(stderr, fprintf(stderr,
"-- : specifies remaining arguments are files to archive/extract\n"); "-- : specifies remaining arguments are files to archive/extract\n");
fprintf( fprintf(
@ -189,6 +192,7 @@ SDArchiverParsed simple_archiver_create_parsed(void) {
parsed.working_files = NULL; parsed.working_files = NULL;
parsed.temp_dir = NULL; parsed.temp_dir = NULL;
parsed.user_cwd = NULL; parsed.user_cwd = NULL;
parsed.write_version = 0;
return parsed; return parsed;
} }
@ -299,6 +303,26 @@ int simple_archiver_parse_args(int argc, const char **argv,
out->temp_dir = argv[1]; out->temp_dir = argv[1];
--argc; --argc;
++argv; ++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) { } else if (argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == 0) {
is_remaining_args = 1; is_remaining_args = 1;
} else if (argv[0][0] != '-') { } else if (argv[0][0] != '-') {

View file

@ -51,6 +51,8 @@ typedef struct SDArchiverParsed {
const char *temp_dir; const char *temp_dir;
/// Dir specified by "-C". /// Dir specified by "-C".
const char *user_cwd; const char *user_cwd;
/// Currently only 0 and 1 is supported.
uint32_t write_version;
} SDArchiverParsed; } SDArchiverParsed;
typedef struct SDArchiverFileInfo { typedef struct SDArchiverFileInfo {