aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-xml-utils.c
diff options
context:
space:
mode:
authorJeffrey Stedfast <fejj@ximian.com>2002-08-07 11:41:19 +0800
committerJeffrey Stedfast <fejj@src.gnome.org>2002-08-07 11:41:19 +0800
commit698c3b1afc1d332229b81ca556a391a38a24306e (patch)
treed6db837af670ea0d99d4dfbaf5d997447a028c0e /e-util/e-xml-utils.c
parent7cc54f371cf51ca0879126becd4c138eb1f90ad2 (diff)
downloadgsoc2013-evolution-698c3b1afc1d332229b81ca556a391a38a24306e.tar
gsoc2013-evolution-698c3b1afc1d332229b81ca556a391a38a24306e.tar.gz
gsoc2013-evolution-698c3b1afc1d332229b81ca556a391a38a24306e.tar.bz2
gsoc2013-evolution-698c3b1afc1d332229b81ca556a391a38a24306e.tar.lz
gsoc2013-evolution-698c3b1afc1d332229b81ca556a391a38a24306e.tar.xz
gsoc2013-evolution-698c3b1afc1d332229b81ca556a391a38a24306e.tar.zst
gsoc2013-evolution-698c3b1afc1d332229b81ca556a391a38a24306e.zip
Changed to handle saving to a temp file first, this allows us to remove a
2002-08-06 Jeffrey Stedfast <fejj@ximian.com> * gal/util/e-xml-utils.c (e_xml_save_file): Changed to handle saving to a temp file first, this allows us to remove a lot of duplicate code from everywhere. svn path=/trunk/; revision=17725
Diffstat (limited to 'e-util/e-xml-utils.c')
-rw-r--r--e-util/e-xml-utils.c29
1 files changed, 26 insertions, 3 deletions
diff --git a/e-util/e-xml-utils.c b/e-util/e-xml-utils.c
index 7e3560fdfb..6483b4122c 100644
--- a/e-util/e-xml-utils.c
+++ b/e-util/e-xml-utils.c
@@ -26,8 +26,13 @@
#include <config.h>
#endif
+#ifdef HAVE_ALLOCA_H
+#include <alloca.h>
+#endif
+
#include "e-xml-utils.h"
+#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
@@ -690,19 +695,27 @@ xmlDocContentDump (xmlBufferPtr buf, xmlDocPtr cur)
int
e_xml_save_file (const char *filename, xmlDocPtr doc)
{
+ char *filesave, *slash;
size_t n, written = 0;
xmlBufferPtr buf;
int errnosave;
ssize_t w;
int fd;
- fd = open (filename, O_WRONLY | O_CREAT | O_EXCL, 0600);
+ filesave = alloca (strlen (filename) + 5);
+ slash = strrchr (filename, '/');
+ if (slash)
+ sprintf (filesave, "%.*s.#%s", slash - filename + 1, filename, slash + 1);
+ else
+ sprintf (filesave, ".#%s", filename);
+
+ fd = open (filesave, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (fd == -1)
return -1;
if (!(buf = xmlBufferCreate ())) {
close (fd);
- unlink (filename);
+ unlink (filesave);
errno = ENOMEM;
return -1;
}
@@ -724,12 +737,22 @@ e_xml_save_file (const char *filename, xmlDocPtr doc)
if (written < n || fsync (fd) == -1) {
errnosave = errno;
close (fd);
- unlink (filename);
+ unlink (filesave);
errno = errnosave;
return -1;
}
close (fd);
+ if (errno != 0)
+ return -1;
+
+ if (rename (filesave, filename) == -1) {
+ errnosave = errno;
+ unlink (filesave);
+ errno = errnosave;
+ return -1;
+ }
+
return 0;
}