summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastien Helleu <flashcode@flashtux.org>2007-05-09 16:43:01 +0000
committerSebastien Helleu <flashcode@flashtux.org>2007-05-09 16:43:01 +0000
commitc3e9858d55ec3f5b9a40a44d676175c30f318a54 (patch)
treee9f9f6f015ed673039f63f09f1bdab233001211b
parent7290f0f1abbbb50e83ea0ab0e050839aebccb8eb (diff)
downloadweechat-c3e9858d55ec3f5b9a40a44d676175c30f318a54.zip
Improved setup file save: now writes temporary file, then rename it (task #6847)
-rw-r--r--ChangeLog2
-rw-r--r--src/common/weeconfig.c27
-rw-r--r--weechat/ChangeLog2
-rw-r--r--weechat/src/common/weeconfig.c27
4 files changed, 48 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index 482302c0a..a752888c3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -5,6 +5,8 @@ ChangeLog - 2007-05-09
Version 0.2.5 (under dev!):
+ * improved setup file save: now writes temporary file, then rename it
+ (task #6847)
* fixed bug with $nick/$channel/$server variables in commands
* forget current nick when user manually disconnects from server
* fixed nick display in input window
diff --git a/src/common/weeconfig.c b/src/common/weeconfig.c
index 6a4ff380e..2bbc98dec 100644
--- a/src/common/weeconfig.c
+++ b/src/common/weeconfig.c
@@ -2353,9 +2353,9 @@ int
config_write (char *config_name)
{
int filename_length;
- char *filename;
+ char *filename, *filename2;
FILE *file;
- int i, j;
+ int i, j, rc;
time_t current_time;
t_irc_server *ptr_server;
t_weechat_alias *ptr_alias;
@@ -2364,7 +2364,10 @@ config_write (char *config_name)
char *expanded_name, *function_name;
if (config_name)
+ {
+ filename_length = strlen (config_name);
filename = strdup (config_name);
+ }
else
{
filename_length = strlen (weechat_home) +
@@ -2376,12 +2379,21 @@ config_write (char *config_name)
snprintf (filename, filename_length, "%s%s" WEECHAT_CONFIG_NAME,
weechat_home, DIR_SEPARATOR);
}
+
+ filename2 = (char *) malloc ((filename_length + 32) * sizeof (char));
+ if (!filename2)
+ {
+ free (filename);
+ return -2;
+ }
+ snprintf (filename2, filename_length + 32, "%s.weechattmp", filename);
- if ((file = fopen (filename, "w")) == NULL)
+ if ((file = fopen (filename2, "w")) == NULL)
{
gui_printf (NULL, _("%s cannot create file \"%s\"\n"),
- WEECHAT_ERROR, filename);
+ WEECHAT_ERROR, filename2);
free (filename);
+ free (filename2);
return -1;
}
@@ -2531,7 +2543,12 @@ config_write (char *config_name)
}
fclose (file);
- chmod (filename, 0600);
+ chmod (filename2, 0600);
+ unlink (filename);
+ rc = rename (filename2, filename);
free (filename);
+ free (filename2);
+ if (rc != 0)
+ return -1;
return 0;
}
diff --git a/weechat/ChangeLog b/weechat/ChangeLog
index 482302c0a..a752888c3 100644
--- a/weechat/ChangeLog
+++ b/weechat/ChangeLog
@@ -5,6 +5,8 @@ ChangeLog - 2007-05-09
Version 0.2.5 (under dev!):
+ * improved setup file save: now writes temporary file, then rename it
+ (task #6847)
* fixed bug with $nick/$channel/$server variables in commands
* forget current nick when user manually disconnects from server
* fixed nick display in input window
diff --git a/weechat/src/common/weeconfig.c b/weechat/src/common/weeconfig.c
index 6a4ff380e..2bbc98dec 100644
--- a/weechat/src/common/weeconfig.c
+++ b/weechat/src/common/weeconfig.c
@@ -2353,9 +2353,9 @@ int
config_write (char *config_name)
{
int filename_length;
- char *filename;
+ char *filename, *filename2;
FILE *file;
- int i, j;
+ int i, j, rc;
time_t current_time;
t_irc_server *ptr_server;
t_weechat_alias *ptr_alias;
@@ -2364,7 +2364,10 @@ config_write (char *config_name)
char *expanded_name, *function_name;
if (config_name)
+ {
+ filename_length = strlen (config_name);
filename = strdup (config_name);
+ }
else
{
filename_length = strlen (weechat_home) +
@@ -2376,12 +2379,21 @@ config_write (char *config_name)
snprintf (filename, filename_length, "%s%s" WEECHAT_CONFIG_NAME,
weechat_home, DIR_SEPARATOR);
}
+
+ filename2 = (char *) malloc ((filename_length + 32) * sizeof (char));
+ if (!filename2)
+ {
+ free (filename);
+ return -2;
+ }
+ snprintf (filename2, filename_length + 32, "%s.weechattmp", filename);
- if ((file = fopen (filename, "w")) == NULL)
+ if ((file = fopen (filename2, "w")) == NULL)
{
gui_printf (NULL, _("%s cannot create file \"%s\"\n"),
- WEECHAT_ERROR, filename);
+ WEECHAT_ERROR, filename2);
free (filename);
+ free (filename2);
return -1;
}
@@ -2531,7 +2543,12 @@ config_write (char *config_name)
}
fclose (file);
- chmod (filename, 0600);
+ chmod (filename2, 0600);
+ unlink (filename);
+ rc = rename (filename2, filename);
free (filename);
+ free (filename2);
+ if (rc != 0)
+ return -1;
return 0;
}