]> git.seodisparate.com - SimpleArchiver/commitdiff
Add "--overwrite-create"
authorStephen Seo <seo.disparate@gmail.com>
Thu, 11 Jul 2024 07:56:23 +0000 (16:56 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 11 Jul 2024 07:56:23 +0000 (16:56 +0900)
Default behavior is now to NOT overwrite existing archive files for
storing output unless "--overwrite-create" is specified.

src/main.c
src/parser.c
src/parser.h

index b5f2c372c1443406c95d1c7294145b602031cf90..da67c9d470983d681b53be7e7e52cf12e8592042 100644 (file)
@@ -32,13 +32,24 @@ int print_list_fn(void *data, __attribute__((unused)) void *ud) {
 }
 
 int main(int argc, const char **argv) {
-  simple_archiver_print_usage();
-
-  __attribute__((cleanup(simple_archiver_free_parsed)))
-  SDArchiverParsed parsed = simple_archiver_create_parsed();
+  __attribute__((
+      cleanup(simple_archiver_free_parsed))) SDArchiverParsed parsed =
+      simple_archiver_create_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)))
   SDArchiverLinkedList *filenames =
       simple_archiver_parsed_to_filenames(&parsed);
index c14f2aa3854994314cf0eec7045e8dd8dbd6368e..37e174e10a3d2be4f0da178903ae1680ddea5414 100644 (file)
@@ -140,6 +140,7 @@ void simple_archiver_print_usage(void) {
   puts("-f <filename> : filename to work on");
   puts("--compressor <full_compress_cmd> : requires --decompressor");
   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("If creating archive file, remaining args specify files to archive.");
   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);
         --argc;
         ++argv;
+      } else if (strcmp(argv[0], "--overwrite-create") == 0) {
+        out->flags |= 0x2;
       } else if (argv[0][0] == '-' && argv[0][1] == '-' && argv[0][2] == 0) {
         is_remaining_args = 1;
       } else if (argv[0][0] != '-') {
index 19630d0f4447ed21a258357e8659b6d9fff44b7e..7e582e4d108822bb01a3c64f6426774e23536581 100644 (file)
 
 typedef struct SDArchiverParsed {
   /// Each bit is a flag.
-  /// 0b0 - is creating.
-  /// 0b1 - is extracting.
+  ///  0b0 - is creating.
+  ///  0b1 - is extracting.
+  /// 0b0x - Do NOT allow create archive overwrite.
+  /// 0b1x - Allow create archive overwrite.
   unsigned int flags;
   /// Null-terminated string.
   char *filename;