sitecopy (1:0.16.6-3) 10_bts410703_preserve_storage_files_sigint.dpatch

Summary

 src/sitestore.c |   31 ++++++++++++++++++++++++++++---
 1 file changed, 28 insertions(+), 3 deletions(-)

    
download this patch

Patch contents

#! /bin/sh /usr/share/dpatch/dpatch-run
## 10_bts-410703_preserve_storage_files_sigint.dpatch by Andreas Henriksson <andreas@fatal.se>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Preserve storage files it SIGINT (Ctrl+C) is sent

@DPATCH@

--- sitecopy-0.16.3/src/sitestore.c	2006-02-04 11:18:08.000000000 +0100
+++ sitecopy-0.16.3-pending/src/sitestore.c	2008-06-08 21:56:23.000000000 +0200
@@ -60,17 +60,42 @@
 /* Opens the storage file for writing */
 FILE *site_open_storage_file(struct site *site) 
 {
+    char filebuf[PATH_MAX];
+
+    /* open a temporary "pending" file, to not corrupt the site file in case
+     * the program aborts while we are updating it.
+     * The site_close_storage_file() function will rename it to it's proper
+     * name.
+     * FIXME: something should clean up old *.pending files,
+     * which never got properly closed.
+     */
+    snprintf(filebuf, sizeof(filebuf), "%s.pending", site->infofile);
+
     if (site->storage_file == NULL) {
-	site->storage_file = fopen(site->infofile, "w" FOPEN_BINARY_FLAGS);
+	site->storage_file = fopen(filebuf, "w" FOPEN_BINARY_FLAGS);
     }
     return site->storage_file;
 }
 
 int site_close_storage_file(struct site *site)
 {
-    int ret = fclose(site->storage_file);
+    char filebuf[PATH_MAX];
+    int err;
+
+    /* close filehandle */
+    err = fclose(site->storage_file);
     site->storage_file = NULL;
-    return ret;
+    if (err) {
+      perror("fclose");
+      return err;
+    }
+
+    /* rename pending file to real filename (overwriting existing file). */
+    snprintf(filebuf, sizeof(filebuf), "%s.pending", site->infofile);
+    err = rename(filebuf, site->infofile);
+    if (err)
+	    perror("rename");
+    return err;
 }
 
 /* Return escaped form of 'filename'; any XML-unsafe characters are