Package: sitecopy / 1:0.16.6-7

10-bts410703-preserve-storage-files-sigint.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
From: Andreas Henriksson <andreas@fatal.se>
Subject: Preserve storage files it SIGINT (Ctrl+C) is sent


--- 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