serialize to a temporary file and rename it to the target filename when it

2002-05-13  Sven Neumann  <sven@gimp.org>

	* app/config/gimpconfig.c (gimp_config_serialize): serialize to a
	temporary file and rename it to the target filename when it is
	completely written.
This commit is contained in:
Sven Neumann 2002-05-13 21:39:59 +00:00 committed by Sven Neumann
parent 84d244c1ed
commit 1ab94ead7e
3 changed files with 48 additions and 24 deletions

View File

@ -1,3 +1,9 @@
2002-05-13 Sven Neumann <sven@gimp.org>
* app/config/gimpconfig.c (gimp_config_serialize): serialize to a
temporary file and rename it to the target filename when it is
completely written.
2002-05-13 Michael Natterer <mitch@gimp.org>
* app/core/gimpparasite.c: parse parasiterc using GScanner instead

View File

@ -23,6 +23,7 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
@ -32,10 +33,6 @@
#include <glib-object.h>
#ifdef G_OS_WIN32
#include <io.h>
#endif
#include "gimpconfig.h"
#include "gimpconfig-serialize.h"
#include "gimpconfig-deserialize.h"
@ -181,6 +178,7 @@ gimp_config_serialize (GObject *object,
GError **error)
{
GimpConfigInterface *gimp_config_iface;
gchar *tmpname;
gint fd;
g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
@ -191,20 +189,17 @@ gimp_config_serialize (GObject *object,
g_return_val_if_fail (gimp_config_iface != NULL, FALSE);
fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC,
#ifndef G_OS_WIN32
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
#else
_S_IREAD | _S_IWRITE
#endif
);
tmpname = g_strconcat (filename, "XXXXXX", NULL);
fd = g_mkstemp (tmpname);
if (fd == -1)
{
g_set_error (error,
GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_FILE,
_("Failed to open file: '%s': %s"),
_("Failed to generate temporary file for '%s': %s"),
filename, g_strerror (errno));
g_free (tmpname);
return FALSE;
}
@ -212,6 +207,20 @@ gimp_config_serialize (GObject *object,
close (fd);
if (rename (tmpname, filename) == -1)
{
g_set_error (error,
GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_FILE,
_("Failed to open file '%s' to '%s': %s"),
tmpname, filename, g_strerror (errno));
unlink (tmpname);
g_free (tmpname);
return FALSE;
}
g_free (tmpname);
return TRUE;
}

View File

@ -23,6 +23,7 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
@ -32,10 +33,6 @@
#include <glib-object.h>
#ifdef G_OS_WIN32
#include <io.h>
#endif
#include "gimpconfig.h"
#include "gimpconfig-serialize.h"
#include "gimpconfig-deserialize.h"
@ -181,6 +178,7 @@ gimp_config_serialize (GObject *object,
GError **error)
{
GimpConfigInterface *gimp_config_iface;
gchar *tmpname;
gint fd;
g_return_val_if_fail (G_IS_OBJECT (object), FALSE);
@ -191,20 +189,17 @@ gimp_config_serialize (GObject *object,
g_return_val_if_fail (gimp_config_iface != NULL, FALSE);
fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC,
#ifndef G_OS_WIN32
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
#else
_S_IREAD | _S_IWRITE
#endif
);
tmpname = g_strconcat (filename, "XXXXXX", NULL);
fd = g_mkstemp (tmpname);
if (fd == -1)
{
g_set_error (error,
GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_FILE,
_("Failed to open file: '%s': %s"),
_("Failed to generate temporary file for '%s': %s"),
filename, g_strerror (errno));
g_free (tmpname);
return FALSE;
}
@ -212,6 +207,20 @@ gimp_config_serialize (GObject *object,
close (fd);
if (rename (tmpname, filename) == -1)
{
g_set_error (error,
GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_FILE,
_("Failed to open file '%s' to '%s': %s"),
tmpname, filename, g_strerror (errno));
unlink (tmpname);
g_free (tmpname);
return FALSE;
}
g_free (tmpname);
return TRUE;
}