From fe9f255aa83af29fc78c04357a48bbc4900e47ba Mon Sep 17 00:00:00 2001 From: hiro Date: Wed, 19 Nov 2008 04:52:32 +0000 Subject: keep backups of config files for four generations. git-svn-id: svn://sylpheed.sraoss.jp/sylpheed/trunk@2071 ee746299-78ed-0310-b773-934348b2243d --- libsylph/prefs.c | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) (limited to 'libsylph') diff --git a/libsylph/prefs.c b/libsylph/prefs.c index 030cc746..230d2dc5 100644 --- a/libsylph/prefs.c +++ b/libsylph/prefs.c @@ -348,6 +348,7 @@ gint prefs_file_close(PrefFile *pfile) gchar *path; gchar *tmppath; gchar *bakpath = NULL; + gint ret = 0; g_return_val_if_fail(pfile != NULL, -1); @@ -358,37 +359,54 @@ gint prefs_file_close(PrefFile *pfile) tmppath = g_strconcat(path, ".tmp", NULL); if (fclose(fp) == EOF) { FILE_OP_ERROR(tmppath, "fclose"); - g_unlink(tmppath); - g_free(path); - g_free(tmppath); - return -1; + ret = -1; + goto finish; } if (is_file_exist(path)) { bakpath = g_strconcat(path, ".bak", NULL); + if (is_file_exist(bakpath)) { + gint i; + gchar *bakpath_n, *bakpath_p; + + for (i = 4; i > 0; i--) { + bakpath_n = g_strdup_printf("%s.%d", bakpath, + i); + if (i == 1) + bakpath_p = g_strdup(bakpath); + else + bakpath_p = g_strdup_printf + ("%s.%d", bakpath, i - 1); + if (is_file_exist(bakpath_p)) { + if (rename_force(bakpath_p, bakpath_n) < 0) { + FILE_OP_ERROR(bakpath_p, + "rename"); + } + } + g_free(bakpath_p); + g_free(bakpath_n); + } + } if (rename_force(path, bakpath) < 0) { FILE_OP_ERROR(path, "rename"); - g_unlink(tmppath); - g_free(path); - g_free(tmppath); - g_free(bakpath); - return -1; + ret = -1; + goto finish; } } if (rename_force(tmppath, path) < 0) { FILE_OP_ERROR(tmppath, "rename"); - g_unlink(tmppath); - g_free(path); - g_free(tmppath); - g_free(bakpath); - return -1; + ret = -1; + goto finish; } +finish: + if (ret < 0) + g_unlink(tmppath); g_free(path); g_free(tmppath); g_free(bakpath); - return 0; + return ret; } gint prefs_file_close_revert(PrefFile *pfile) -- cgit v1.2.3