]> git.seodisparate.com - SimpleArchiver/commitdiff
Impl. being able to set dir for temporary files
authorStephen Seo <seo.disparate@gmail.com>
Thu, 25 Jul 2024 01:42:31 +0000 (10:42 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 25 Jul 2024 01:42:31 +0000 (10:42 +0900)
src/archiver.c
src/parser.c

index 36a59bce9fc891523d4671f3bb1b28ac79103b67..07d38f47be6604074cd680b9c71463f34240e789 100644 (file)
@@ -39,7 +39,7 @@
 
 #include "helpers.h"
 
-#define TEMP_FILENAME_CMP "simple_archiver_compressed_%u.tmp"
+#define TEMP_FILENAME_CMP "%s%ssimple_archiver_compressed_%u.tmp"
 
 #if SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_COSMOPOLITAN || \
     SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC ||          \
@@ -168,15 +168,21 @@ int write_files_fn(void *data, void *ud) {
     SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_MAC ||          \
     SIMPLE_ARCHIVER_PLATFORM == SIMPLE_ARCHIVER_PLATFORM_LINUX
       // Use temp file to store compressed data.
-      char temp_filename[128];
+      char temp_filename[512];
       unsigned int idx = 0;
-      snprintf(temp_filename, 128, TEMP_FILENAME_CMP, idx);
+      unsigned int temp_dir_end = strlen(state->parsed->temp_dir);
+      snprintf(temp_filename, 512, TEMP_FILENAME_CMP, state->parsed->temp_dir,
+               state->parsed->temp_dir[temp_dir_end - 1] == '/' ? "" : "/",
+               idx);
       do {
         FILE *test_fd = fopen(temp_filename, "rb");
         if (test_fd) {
           // File exists.
           fclose(test_fd);
-          snprintf(temp_filename, 128, TEMP_FILENAME_CMP, ++idx);
+          snprintf(temp_filename, 512, TEMP_FILENAME_CMP,
+                   state->parsed->temp_dir,
+                   state->parsed->temp_dir[temp_dir_end - 1] == '/' ? "" : "/",
+                   ++idx);
         } else if (idx > 0xFFFF) {
           // Sanity check.
           return 1;
@@ -192,14 +198,14 @@ int write_files_fn(void *data, void *ud) {
       }
       __attribute__((cleanup(free_FILE_helper))) FILE *tmp_fd =
           fopen(temp_filename, "wb");
+      if (!tmp_fd) {
+        fprintf(stderr, "ERROR: Unable to create temp file for compressing!\n");
+        return 1;
+      }
       __attribute__((cleanup(cleanup_temp_filename_delete))) void **ptrs_array =
           malloc(sizeof(void *) * 2);
       ptrs_array[0] = temp_filename;
       ptrs_array[1] = &tmp_fd;
-      if (!tmp_fd) {
-        // Unable to create temp file.
-        return 1;
-      }
 
       // Handle SIGPIPE.
       signal(SIGPIPE, handle_sig_pipe);
index 747e1aebd382ef9738ca35f44c17e01ce9f78cc0..b18b5e7350835c27341d61ce5c14823d5667d079 100644 (file)
@@ -174,6 +174,7 @@ SDArchiverParsed simple_archiver_create_parsed(void) {
   parsed.compressor = NULL;
   parsed.decompressor = NULL;
   parsed.working_files = NULL;
+  parsed.temp_dir = NULL;
 
   return parsed;
 }
@@ -202,6 +203,7 @@ int simple_archiver_parse_args(int argc, const char **argv,
   while (argc > 0) {
     if (!is_remaining_args) {
       if (strcmp(argv[0], "-h") == 0 || strcmp(argv[0], "--help") == 0) {
+        simple_archiver_free_parsed(out);
         simple_archiver_print_usage();
         exit(0);
       } else if (strcmp(argv[0], "-c") == 0) {
@@ -319,6 +321,10 @@ int simple_archiver_parse_args(int argc, const char **argv,
     ++argv;
   }
 
+  if (!out->temp_dir) {
+    out->temp_dir = "./";
+  }
+
   return 0;
 }