home · contact · privacy
Alarm about / don't start on finding temp file filesaving leftovers.
[plomrogue] / src / common / readwrite.c
index 026c9add7aba8e5434f158bb97c765d957d88d55..c9e22d73d3ae16dd7fd37a19b17908cebc07921e 100644 (file)
@@ -2,28 +2,51 @@
 
 #include "readwrite.h"
 #include <stddef.h> /* size_t */
-#include <stdint.h> /* uint8_t, uint16_t, uint32_t */
+#include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
 #include <stdio.h> /* FILE, fseek(), sprintf(), fgets(), fgetc(), ferror(),
-                    * fputc(), fwrite(), fclose(), fopen()
+                    * fputc(), fwrite(), fclose(), fopen(), clearerr()
                     */
+#include <stdlib.h> /* free() */
 #include <string.h> /* strlen() */
-#include <unistd.h> /* for access(), unlink() */
+#include <unistd.h> /* access(), unlink() */
 #include "rexit.h" /* exit_err(), exit_trouble() */
+#include "try_malloc.h" /* try_malloc() */
+
+
+
+/* Return "path" + suffix "_tmp". Value is malloc'd, must be free externally. */
+static char * build_temp_path(char * path);
+
+
+
+static char * build_temp_path(char * path)
+{
+    char * f_name = "build_temp_path";
+    char * suffix_tmp = "_tmp";
+    uint16_t size = strlen(path) + strlen(suffix_tmp) + 1;
+    char * path_tmp = try_malloc(size, f_name);
+    int test = sprintf(path_tmp, "%s%s", path, suffix_tmp);
+    exit_trouble(test < 0, f_name, "sprintf()");
+    return path_tmp;
+}
 
 
 
 extern FILE * try_fopen(char * path, char * mode, char * f)
 {
+    char * f_name = "try_fopen()";
     char * msg1 = "Trouble in ";
     char * msg2 = " with fopen() (mode '";
     char * msg3 = "') on path '";
     char * msg4 = "'.";
     uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg3) + strlen(msg4)
                     + strlen(f) + strlen(path) + strlen(mode) + 1;
-    char msg[size];
-    sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, mode, msg3, path, msg4);
+    char * msg = try_malloc(size, f_name);
+    int test = sprintf(msg, "%s%s%s%s%s%s%s", msg1,f,msg2,mode,msg3,path,msg4);
+    exit_trouble(test < 0, f_name, "sprintf()");
     FILE * file_p = fopen(path, mode);
     exit_err(NULL == file_p, msg);
+    free(msg);
     return file_p;
 }
 
@@ -53,6 +76,7 @@ extern void try_fputc(uint8_t c, FILE * file, char * f)
 
 extern int try_fgetc(FILE * file, char * f)
 {
+    clearerr(file); /* OSX' (BSD?) fgetc() needs this to undo previous EOFs. */
     int test = fgetc(file);
     exit_trouble(EOF == test && ferror(file), f, "fgetc()");
     return test;
@@ -69,38 +93,73 @@ extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
 
 
 
-extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
-                                     char * f)
+extern FILE * atomic_write_start(char * path, char ** path_tmp)
 {
-    try_fclose(file, f);
+    char * f_name = "atomic_write_start()";
+    *path_tmp = build_temp_path(path);
+    return try_fopen(*path_tmp, "w", f_name);
+}
+
+
+
+extern void atomic_write_finish(FILE * file, char * path, char * path_tmp)
+{
+    char * f_name = "atomic_write_finish()";
+    try_fclose(file, f_name);
     char * msg1 = "Trouble in ";
     char * msg4 = "'.";
-    if (!access(p2, F_OK))
+    if (!access(path, F_OK))
     {
         char * msg2 = " with unlink() on path '";
         uint16_t size = strlen(msg1) + strlen(msg2) + strlen(msg4)
-                        + strlen(f) + strlen(p2) + 1;
-        char msg[size];
-        sprintf(msg, "%s%s%s%s%s", msg1, f, msg2, p2, msg4);
-        exit_err(unlink(p2), msg);
+                        + strlen(f_name) + strlen(path) + 1;
+        char * msg = try_malloc(size, f_name);
+        int test = sprintf(msg, "%s%s%s%s%s", msg1, f_name, msg2, path, msg4);
+        exit_trouble(test < 0, f_name, "sprintf()");
+        exit_err(unlink(path), msg);
+        free(msg);
     }
     char * msg2 = " with rename() from '";
     char * msg3 = "' to '";
-    uint16_t size = strlen(msg1) + strlen(f) + strlen(msg2) + strlen(p1)
-                    + strlen(msg3) + strlen(p2) + strlen(msg4) + 1;
-    char msg[size];
-    sprintf(msg, "%s%s%s%s%s%s%s", msg1, f, msg2, p1, msg3, p2, msg4);
-    exit_err(rename(p1, p2), msg);
+    uint16_t size =   strlen(msg1) + strlen(f_name) + strlen(msg2) +
+                    + strlen(path_tmp) + strlen(msg3) + strlen(path)
+                    + strlen(msg4) + 1;
+    char * msg = try_malloc(size, f_name);
+    int test = sprintf(msg, "%s%s%s%s%s%s%s",
+                            msg1, f_name, msg2, path_tmp, msg3, path, msg4);
+    exit_trouble(test < 0, f_name, "sprintf()");
+    exit_err(rename(path_tmp, path), msg);
+    free(msg);
+    free(path_tmp);
+}
+
+
+
+extern void detect_atomic_leftover(char * path)
+{
+    char * f_name = "detect_atomic_leftover()";
+    char * path_tmp = build_temp_path(path);
+    char * part1 = "Found file '";
+    char * part2 = "' that may be a leftover from an aborted previous attempt "
+                   "to write '";
+    char * part3 = "'. Aborting until the matter is solved by (re-)moving it.";
+    uint32_t size =   strlen(part1) + strlen(path_tmp) + strlen(part2)
+                    + strlen(path) + strlen(part3) + 1;
+    char * msg = try_malloc(size, f_name);
+    int test = sprintf(msg, "%s%s%s%s%s", part1, path_tmp, part2, path, part3);
+    exit_trouble(test < 0, f_name, "sprintf()");
+    exit_err(!access(path_tmp, F_OK), msg);
+    free(msg);
+    free(path_tmp);
 }
 
 
 
-extern uint32_t textfile_sizes(FILE * file, uint32_t * n_lines_p)
+extern uint32_t textfile_width(FILE * file)
 {
-    char * f_name = "textfile_sizes()";
+    char * f_name = "textfile_width()";
     int c = 0;
     uint32_t c_count = 0;
-    uint32_t n_lines = 0;
     uint32_t linemax = 0;
     while (1)
     {
@@ -110,6 +169,7 @@ extern uint32_t textfile_sizes(FILE * file, uint32_t * n_lines_p)
             break;
         }
         c_count++;
+        exit_trouble(UINT32_MAX == c_count, f_name, "too large text file line");
         if ('\n' == c)
         {
             if (c_count > linemax)
@@ -117,10 +177,6 @@ extern uint32_t textfile_sizes(FILE * file, uint32_t * n_lines_p)
                 linemax = c_count;
             }
             c_count = 0;
-            if (n_lines_p)
-            {
-                n_lines++;
-            }
         }
     }
   if (0 == linemax && 0 < c_count) /* Handle files that consist of only one */
@@ -128,9 +184,5 @@ extern uint32_t textfile_sizes(FILE * file, uint32_t * n_lines_p)
       linemax = c_count;
    }
    exit_trouble(-1 == fseek(file, 0, SEEK_SET), f_name, "fseek()");
-   if (n_lines_p)
-   {
-       * n_lines_p = n_lines;
-   }
    return linemax;
 }