home · contact · privacy
Refactor atomic writing to atomic_write_start() + atomic_write_finish().
[plomrogue] / src / common / readwrite.h
index b61230fb59487ba05f0feed3a05de66bb0ab0dcb..d03aeacb5fb5924d71a8b542afc7a26a3b3c6bb0 100644 (file)
@@ -29,12 +29,16 @@ extern void try_fputc(uint8_t c, FILE * file, char * f);
 extern int try_fgetc(FILE * file, char * f);
 extern char * try_fgets(char * line, int size, FILE * file, char * f);
 
-/* Wrapper to successive call of fclose() from function called "f" on "file",
- * then unlink() on file at path "p2" if it exists, then rename() from path "p1"
- * to "p2". Used for handling atomic saving of files via temp files.
+/* Write to "path_tmp" "path" + "_tmp" and return a new file at that "path_tmp"
+ * open for writing. "path_tmp" is malloc()'d, must be freed externally.
+*/
+extern FILE * atomic_write_start(char * path, char ** path_tmp);
+
+/* Finish atomic writing started in atomic_write_start(). Wraps successive calls
+ * of fclose() on "file", then unlink() on file at path "path" if it exists,
+ * then rename() from "path_tmp" to "path", then free() on "path_tmp".
  */
-extern void try_fclose_unlink_rename(FILE * file, char * p1, char * p2,
-                                     char * f);
+extern void atomic_write_finish(FILE * file, char * path, char * path_tmp);
 
 /* Return largest line length from "file" (including  newline chars). */
 extern uint32_t textfile_width(FILE * file);