home · contact · privacy
Alarm about / don't start on finding temp file filesaving leftovers.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 9 Jul 2014 23:04:31 +0000 (01:04 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 9 Jul 2014 23:04:31 +0000 (01:04 +0200)
TODO
src/client/interface_conf.c
src/common/readwrite.c
src/common/readwrite.h
src/server/init.c

diff --git a/TODO b/TODO
index 7557ab3f4a7b144bacf3f097af34a212c01e4520..276d56f003e7369af6da8ffef5953bf302691688 100644 (file)
--- a/TODO
+++ b/TODO
@@ -4,13 +4,15 @@ IN GENERAL:
 
 - expand use of hardcoded_strings module(s)
 
+ROGUELIKE SKRIPT
+
+- do something about the invisibility of error messages
+
 BOTH SERVER/CLIENT:
 
 - make server and client communicate by specific world state info requests 
   in server/out, replacing server/worldstate
 
-- on start, alarm about _tmp files (savefile/record) to avoid gameplay data loss
-
 SERVER:
 
 - consider
index 8fc234b074b5a0c4d02b4602b8715a5492af97b1..853fdaae66ef132b50ed340078e39a52e497f11b 100644 (file)
@@ -16,8 +16,8 @@
                                    * parsetest_too_many_values(),
                                    * parse_id_uniq(), parse_init_entry()
                                    */
-#include "../common/readwrite.h" /* atomic_write_start(), atomic_write_finish()
-                                  * try_fwrite()
+#include "../common/readwrite.h" /* atomic_write_start(), atomic_write_finish(),
+                                  * detect_atomic_leftover(), try_fwrite()
                                   */
 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
 #include "../common/try_malloc.h" /* try_malloc() */
@@ -440,6 +440,7 @@ extern void load_interface_conf()
     tmp_order    = try_malloc(1, f_name);
     tmp_order[0] = '\0';
     tmp_active   = '\0';
+    detect_atomic_leftover(world.path_interface);
     parse_file(world.path_interface, tokens_into_entries);
     char * err = "Not all expected windows defined in config file.";
     exit_err(strlen(world.winDB.legal_ids) != strlen(world.winDB.ids), err);
index 912e70dfa7e443071a07220b7b5c663f258d062d..c9e22d73d3ae16dd7fd37a19b17908cebc07921e 100644 (file)
 
 
 
+/* 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()";
@@ -78,11 +96,7 @@ extern char * try_fgets(char * line, int linemax, FILE * file, char * f)
 extern FILE * atomic_write_start(char * path, char ** path_tmp)
 {
     char * f_name = "atomic_write_start()";
-    char * suffix_tmp = "_tmp";
-    uint16_t size = strlen(path) + strlen(suffix_tmp) + 1;
-    *path_tmp = try_malloc(size, f_name);
-    int test = sprintf(*path_tmp, "%s%s", path, suffix_tmp);
-    exit_trouble(test < 0, f_name, "sprintf()");
+    *path_tmp = build_temp_path(path);
     return try_fopen(*path_tmp, "w", f_name);
 }
 
@@ -121,6 +135,26 @@ extern void atomic_write_finish(FILE * file, char * path, char * 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_width(FILE * file)
 {
     char * f_name = "textfile_width()";
index d03aeacb5fb5924d71a8b542afc7a26a3b3c6bb0..eddd0e66e1875922f5bc7b15be06305080b07d73 100644 (file)
@@ -40,6 +40,9 @@ extern FILE * atomic_write_start(char * path, char ** path_tmp);
  */
 extern void atomic_write_finish(FILE * file, char * path, char * path_tmp);
 
+/* Check for temp file leftover of atomic writing of "path", abort if found. */
+extern void detect_atomic_leftover(char * path);
+
 /* Return largest line length from "file" (including  newline chars). */
 extern uint32_t textfile_width(FILE * file);
 
index ea13bc105afd39ef1ff9dc08b4ca0db77d58ee8b..e9bb3d43836e4590874f0ae125841c7f14fb2a76 100644 (file)
@@ -13,7 +13,8 @@
 #include <time.h> /* time() */
 #include <unistd.h> /* optarg, getopt(), access(), unlink(), getpid() */
 #include "../common/readwrite.h" /* try_fopen(), try_fclose(), textfile_width(),
-                                  * try_fgets(), try_fwrite()
+                                  * try_fgets(), try_fwrite(),
+                                  * detect_atomic_leftover()
                                   */
 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
 #include "../common/try_malloc.h" /* try_malloc() */
@@ -162,6 +163,8 @@ extern void remake_world()
 extern void run_game()
 {
     char * f_name = "run_game()";
+    detect_atomic_leftover(s[S_PATH_SAVE]);
+    detect_atomic_leftover(s[S_PATH_RECORD]);
     if (world.replay)
     {
         replay_game();