Add "--overwrite-create"
All checks were successful
Run Unit Tests / build-and-run-unit-tests (push) Successful in 45s

Default behavior is now to NOT overwrite existing archive files for
storing output unless "--overwrite-create" is specified.
This commit is contained in:
Stephen Seo 2024-07-11 16:56:23 +09:00
parent e315ac5c33
commit efffb8c147
3 changed files with 22 additions and 6 deletions

View file

@ -32,13 +32,24 @@ int print_list_fn(void *data, __attribute__((unused)) void *ud) {
} }
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
simple_archiver_print_usage(); __attribute__((
cleanup(simple_archiver_free_parsed))) SDArchiverParsed parsed =
__attribute__((cleanup(simple_archiver_free_parsed))) simple_archiver_create_parsed();
SDArchiverParsed parsed = simple_archiver_create_parsed();
simple_archiver_parse_args(argc, argv, &parsed); simple_archiver_parse_args(argc, argv, &parsed);
if ((parsed.flags & 0x2) == 0) {
FILE *file = fopen(parsed.filename, "r");
if (file != NULL) {
fclose(file);
fprintf(
stderr,
"ERROR: Archive file exists but --overwrite-create not specified!\n");
simple_archiver_print_usage();
return 1;
}
}
__attribute__((cleanup(simple_archiver_list_free))) __attribute__((cleanup(simple_archiver_list_free)))
SDArchiverLinkedList *filenames = SDArchiverLinkedList *filenames =
simple_archiver_parsed_to_filenames(&parsed); simple_archiver_parsed_to_filenames(&parsed);

View file

@ -140,6 +140,7 @@ void simple_archiver_print_usage(void) {
puts("-f <filename> : filename to work on"); puts("-f <filename> : filename to work on");
puts("--compressor <full_compress_cmd> : requires --decompressor"); puts("--compressor <full_compress_cmd> : requires --decompressor");
puts("--decompressor <full_decompress_cmd> : requires --compressor"); puts("--decompressor <full_decompress_cmd> : requires --compressor");
puts("--overwrite-create : allows overwriting an archive file");
puts("-- : specifies remaining arguments are files to archive/extract"); puts("-- : specifies remaining arguments are files to archive/extract");
puts("If creating archive file, remaining args specify files to archive."); puts("If creating archive file, remaining args specify files to archive.");
puts("If extracting archive file, remaining args specify files to extract."); puts("If extracting archive file, remaining args specify files to extract.");
@ -204,6 +205,8 @@ int simple_archiver_parse_args(int argc, const char **argv,
strncpy(out->decompressor, argv[1], size); strncpy(out->decompressor, argv[1], size);
--argc; --argc;
++argv; ++argv;
} else if (strcmp(argv[0], "--overwrite-create") == 0) {
out->flags |= 0x2;
} 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

@ -23,8 +23,10 @@
typedef struct SDArchiverParsed { typedef struct SDArchiverParsed {
/// Each bit is a flag. /// Each bit is a flag.
/// 0b0 - is creating. /// 0b0 - is creating.
/// 0b1 - is extracting. /// 0b1 - is extracting.
/// 0b0x - Do NOT allow create archive overwrite.
/// 0b1x - Allow create archive overwrite.
unsigned int flags; unsigned int flags;
/// Null-terminated string. /// Null-terminated string.
char *filename; char *filename;