app, pdb, plug-ins: improve error feedback for gimp_temp_file().

It is not a fix yet for #14681 but would provide nicer and earlier error
when a call to gimp_temp_file() fails, which may happen in some cases.
E.g. if the temp directory doesn't exist and we fail to create it.

As a test, if I delete /tmp/gimp/3.2/ and give root ownership to
/tmp/gimp/, now the error when opening our gimp-splash.xcf.xz is:

GIMP-Error: Opening '/path/to/gimp/gimp-data/images/gimp-splash.xcf.xz' failed: Error creating directory /tmp/gimp/3.2: Permission denied
This commit is contained in:
Jehan 2026-04-04 17:36:29 +02:00
parent 088b1f2426
commit 1107d0f7af
6 changed files with 37 additions and 9 deletions

View file

@ -1273,8 +1273,9 @@ gimp_image_opened (Gimp *gimp,
} }
GFile * GFile *
gimp_get_temp_file (Gimp *gimp, gimp_get_temp_file (Gimp *gimp,
const gchar *extension) const gchar *extension,
GError **error)
{ {
static gint id = 0; static gint id = 0;
static gint pid; static gint pid;
@ -1293,13 +1294,24 @@ gimp_get_temp_file (Gimp *gimp,
basename = g_strdup_printf ("gimp-temp-%d%d", pid, id++); basename = g_strdup_printf ("gimp-temp-%d%d", pid, id++);
dir = gimp_file_new_for_config_path (GIMP_GEGL_CONFIG (gimp->config)->temp_path, dir = gimp_file_new_for_config_path (GIMP_GEGL_CONFIG (gimp->config)->temp_path,
NULL); error);
if (dir == NULL)
{
g_free (basename);
return NULL;
}
if (! g_file_query_exists (dir, NULL)) if (! g_file_query_exists (dir, NULL))
{ {
/* Try to make the temp directory if it doesn't exist. /* Try to make the temp directory if it doesn't exist.
* Ignore any error. * Ignore any error.
*/ */
g_file_make_directory_with_parents (dir, NULL, NULL); if (! g_file_make_directory_with_parents (dir, NULL, error))
{
g_free (basename);
g_object_unref (dir);
return NULL;
}
} }
file = g_file_get_child (dir, basename); file = g_file_get_child (dir, basename);
g_free (basename); g_free (basename);

View file

@ -256,7 +256,8 @@ void gimp_image_opened (Gimp *gimp,
GFile *file); GFile *file);
GFile * gimp_get_temp_file (Gimp *gimp, GFile * gimp_get_temp_file (Gimp *gimp,
const gchar *extension); const gchar *extension,
GError **error);
GimpDataFactory * GimpDataFactory *
gimp_get_data_factory (Gimp *gimp, gimp_get_data_factory (Gimp *gimp,

View file

@ -258,13 +258,13 @@ file_remote_get_temp_file (Gimp *gimp,
const gchar *ext = strchr (basename, '.'); const gchar *ext = strchr (basename, '.');
if (ext && strlen (ext)) if (ext && strlen (ext))
temp_file = gimp_get_temp_file (gimp, ext + 1); temp_file = gimp_get_temp_file (gimp, ext + 1, NULL);
g_free (basename); g_free (basename);
} }
if (! temp_file) if (! temp_file)
temp_file = gimp_get_temp_file (gimp, "xxx"); temp_file = gimp_get_temp_file (gimp, "xxx", NULL);
return temp_file; return temp_file;
} }

View file

@ -219,7 +219,8 @@ temp_file_invoker (GimpProcedure *procedure,
if (success) if (success)
{ {
file = gimp_get_temp_file (gimp, extension); file = gimp_get_temp_file (gimp, extension, error);
success = (file != NULL);
} }
return_vals = gimp_procedure_get_return_values (procedure, success, return_vals = gimp_procedure_get_return_values (procedure, success,

View file

@ -220,7 +220,8 @@ HELP
%invoke = ( %invoke = (
code => <<'CODE' code => <<'CODE'
{ {
file = gimp_get_temp_file (gimp, extension); file = gimp_get_temp_file (gimp, extension, error);
success = (file != NULL);
} }
CODE CODE
); );

View file

@ -486,6 +486,12 @@ export_image (const CompressorEntry *compressor,
/* get a temp name with the right extension and save into it. */ /* get a temp name with the right extension and save into it. */
tmp_file = gimp_temp_file (ext + 1); tmp_file = gimp_temp_file (ext + 1);
if (tmp_file == NULL)
{
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"%s", gimp_pdb_get_last_error (gimp_get_pdb ()));
return gimp_pdb_get_last_status (gimp_get_pdb ());
}
if (! (gimp_file_save (run_mode, image, tmp_file, options) && if (! (gimp_file_save (run_mode, image, tmp_file, options) &&
valid_file (tmp_file))) valid_file (tmp_file)))
@ -544,6 +550,13 @@ load_image (const CompressorEntry *compressor,
/* find a temp name */ /* find a temp name */
tmp_file = gimp_temp_file (ext + 1); tmp_file = gimp_temp_file (ext + 1);
if (tmp_file == NULL)
{
*status = gimp_pdb_get_last_status (gimp_get_pdb ());
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
"%s", gimp_pdb_get_last_error (gimp_get_pdb ()));
return NULL;
}
if (! compressor->load_fn (file, tmp_file, error)) if (! compressor->load_fn (file, tmp_file, error))
{ {