2007-10-03 [colin] 3.0.2cvs6
authorColin Leroy <colin@colino.net>
Wed, 3 Oct 2007 18:38:34 +0000 (18:38 +0000)
committerColin Leroy <colin@colino.net>
Wed, 3 Oct 2007 18:38:34 +0000 (18:38 +0000)
* src/common/template.c
Fix templates writing (was prone to disk full problems)

ChangeLog
PATCHSETS
configure.ac
src/common/template.c

index a3d2220ba4a52a76ed14f529a22d2f657f150968..c3a1416826e2cb73c05ce3e13b446558a72fa25c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2007-10-03 [colin]     3.0.2cvs6
+
+       * src/common/template.c
+               Fix templates writing (was prone to disk full problems)
+
 2007-10-03 [colin]     3.0.2cvs5
 
        * src/Makefile.am
index 718bce7731992519b09aa3478e9e2c0e0df1320f..3c6cb4b2bd4c5ace2f52b782fb4b8d32cc8d2ea2 100644 (file)
--- a/PATCHSETS
+++ b/PATCHSETS
 ( cvs diff -u -r 1.115.2.167 -r 1.115.2.168 src/main.c;  cvs diff -u -r 1.36.2.114 -r 1.36.2.115 src/common/utils.c;  cvs diff -u -r 1.20.2.51 -r 1.20.2.52 src/common/utils.h;  ) > 3.0.2cvs3.patchset
 ( cvs diff -u -r 1.75.2.48 -r 1.75.2.49 src/matcher.c;  cvs diff -u -r 1.1.2.78 -r 1.1.2.79 src/gtk/quicksearch.c;  ) > 3.0.2cvs4.patchset
 ( cvs diff -u -r 1.155.2.75 -r 1.155.2.76 src/Makefile.am;  diff -u /dev/null src/addrduplicates.c;  diff -u /dev/null src/addrduplicates.h;  cvs diff -u -r 1.60.2.98 -r 1.60.2.99 src/addressbook.c;  ) > 3.0.2cvs5.patchset
+( cvs diff -u -r 1.2.4.20 -r 1.2.4.21 src/common/template.c;  ) > 3.0.2cvs6.patchset
index 969b9050ccd453370ed02d2ad22fa8b58e1986b6..81e2f113ff383f0445751d2ae0af55ec31dcbeb0 100644 (file)
@@ -11,7 +11,7 @@ MINOR_VERSION=0
 MICRO_VERSION=2
 INTERFACE_AGE=0
 BINARY_AGE=0
-EXTRA_VERSION=5
+EXTRA_VERSION=6
 EXTRA_RELEASE=
 EXTRA_GTK2_VERSION=
 
index c2c6f794a0465a616497d31ea65d3b2c55cfcc63..ccf65d1acd1010cd1dcf68c47226713541be1f78 100644 (file)
@@ -188,6 +188,18 @@ GSList *template_read_config(void)
        return tmpl_list;
 }
 
+#define TRY(func) { \
+if (!(func)) \
+{ \
+       g_warning("Failed to write template to file\n"); \
+       if (fp) fclose(fp); \
+       if (new) g_unlink(new); \
+       g_free(new); \
+       g_free(filename); \
+       return; \
+} \
+}
+
 void template_write_config(GSList *tmpl_list)
 {
        const gchar *path;
@@ -209,39 +221,50 @@ void template_write_config(GSList *tmpl_list)
                        return;
        }
 
-       remove_all_files(path);
-
        for (cur = tmpl_list, tmpl_num = 1; cur != NULL;
             cur = cur->next, tmpl_num++) {
-               gchar *filename;
+               gchar *filename, *new = NULL;
 
                tmpl = cur->data;
 
                filename = g_strconcat(path, G_DIR_SEPARATOR_S,
                                       itos(tmpl_num), NULL);
 
-               if ((fp = g_fopen(filename, "wb")) == NULL) {
-                       FILE_OP_ERROR(filename, "fopen");
+               if (is_file_exist(filename)) {
+                       new = g_strconcat(filename, ".new", NULL);
+               }
+
+               if ((fp = g_fopen(new?new:filename, "wb")) == NULL) {
+                       FILE_OP_ERROR(new?new:filename, "fopen");
+                       g_free(new);
                        g_free(filename);
                        return;
                }
 
-               fprintf(fp, "Name: %s\n", tmpl->name);
+               TRY(fprintf(fp, "Name: %s\n", tmpl->name) > 0);
                if (tmpl->subject && *tmpl->subject != '\0')
-                       fprintf(fp, "Subject: %s\n", tmpl->subject);
+                       TRY(fprintf(fp, "Subject: %s\n", tmpl->subject) > 0);
                if (tmpl->to && *tmpl->to != '\0')
-                       fprintf(fp, "To: %s\n", tmpl->to);
+                       TRY(fprintf(fp, "To: %s\n", tmpl->to) > 0);
                if (tmpl->cc && *tmpl->cc != '\0')
-                       fprintf(fp, "Cc: %s\n", tmpl->cc);
+                       TRY(fprintf(fp, "Cc: %s\n", tmpl->cc) > 0);
                if (tmpl->bcc && *tmpl->bcc != '\0')
-                       fprintf(fp, "Bcc: %s\n", tmpl->bcc);                                            
-               fputs("\n", fp);
-               if (tmpl->value && *tmpl->value != '\0')
-                       fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp);
-               else
-                       fwrite("", sizeof(gchar), 1, fp);
-
-               fclose(fp);
+                       TRY(fprintf(fp, "Bcc: %s\n", tmpl->bcc) > 0);
+
+               TRY(fputs("\n", fp) != EOF);
+
+               if (tmpl->value && *tmpl->value != '\0') {
+                       TRY(fwrite(tmpl->value, sizeof(gchar), strlen(tmpl->value), fp) == strlen(tmpl->value));
+               } else {
+                       TRY(fwrite("", sizeof(gchar), 1, fp) == 1);
+               }
+               TRY(fclose(fp) != EOF);
+
+               if (new) {
+                       g_unlink(filename);
+                       rename_force(new, filename);
+               }
+               g_free(new);
                g_free(filename);
        }
 }