save to a temporary file and rename to avoid problems with concurrent

2004-10-23  Sven Neumann  <sven@gimp.org>

	* libgimpthumb/gimpthumbnail.c (gimp_thumbnail_save): save to a
	temporary file and rename to avoid problems with concurrent
	thumbnail creation.
This commit is contained in:
Sven Neumann 2004-10-23 00:27:11 +00:00 committed by Sven Neumann
parent ee7a9923f7
commit 00ff7e3f61
2 changed files with 48 additions and 3 deletions

View File

@ -1,3 +1,9 @@
2004-10-23 Sven Neumann <sven@gimp.org>
* libgimpthumb/gimpthumbnail.c (gimp_thumbnail_save): save to a
temporary file to avoid problems with concurrent thumbnail
creation.
2004-10-23 Michael Natterer <mitch@gimp.org>
* app/dialogs/Makefile.am

View File

@ -30,6 +30,9 @@
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <gdk-pixbuf/gdk-pixbuf.h>
@ -44,7 +47,7 @@
#include "libgimp/libgimp-intl.h"
/* #define GIMP_THUMB_DEBUG */
/* #define GIMP_THUMB_DEBUG */
#if defined (GIMP_THUMB_DEBUG) && defined (__GNUC__)
@ -775,6 +778,9 @@ gimp_thumbnail_save (GimpThumbnail *thumbnail,
{
const gchar *keys[12];
gchar *values[12];
gchar *basename;
gchar *dirname;
gchar *tmpname;
gboolean success;
gint i = 0;
@ -836,7 +842,16 @@ gimp_thumbnail_save (GimpThumbnail *thumbnail,
keys[i] = NULL;
values[i] = NULL;
success = gdk_pixbuf_savev (pixbuf, filename, "png",
basename = g_path_get_basename (filename);
dirname = g_path_get_dirname (filename);
tmpname = g_strdup_printf ("%s%cgimp-thumb-%d-%.8s",
dirname, G_DIR_SEPARATOR, getpid (), basename);
g_free (dirname);
g_free (basename);
success = gdk_pixbuf_savev (pixbuf, tmpname, "png",
(gchar **) keys, values,
error);
@ -846,7 +861,28 @@ gimp_thumbnail_save (GimpThumbnail *thumbnail,
if (success)
{
#ifdef GIMP_THUMB_DEBUG
g_printerr ("thumbnail saved to file %s\n", filename);
g_printerr ("thumbnail saved to temporary file %s\n", tmpname);
#endif
#ifdef G_OS_WIN32
/* win32 rename can't overwrite */
unlink (filename);
#endif
if (rename (tmpname, filename) == -1)
{
g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
_("Could not create thumbnail for %s: %s"),
thumbnail->image_uri, g_strerror (errno));
success = FALSE;
}
}
if (success)
{
#ifdef GIMP_THUMB_DEBUG
g_printerr ("temporary thumbnail file renamed to %s\n", filename);
#endif
success = (chmod (filename, 0600) == 0);
@ -859,6 +895,9 @@ gimp_thumbnail_save (GimpThumbnail *thumbnail,
thumbnail->image_uri, g_strerror (errno));
}
unlink (tmpname);
g_free (tmpname);
return success;
}