Fix error handling when writing out typelib

Return a non-zero result when opening the output file fails and
don't use g_error() for other failures when writing out the file,
since such errors should not produce a core dump.
This commit is contained in:
Owen W. Taylor 2010-12-22 15:40:53 -05:00 committed by Evan Welsh
parent 70a1394652
commit 5fa171ea21

View File

@ -45,7 +45,7 @@ gboolean include_cwd = FALSE;
gboolean debug = FALSE; gboolean debug = FALSE;
gboolean verbose = FALSE; gboolean verbose = FALSE;
static void static gboolean
write_out_typelib (gchar *prefix, write_out_typelib (gchar *prefix,
GITypelib *typelib) GITypelib *typelib)
{ {
@ -56,6 +56,7 @@ write_out_typelib (gchar *prefix,
GFile *tmp_file_obj; GFile *tmp_file_obj;
gchar *tmp_filename; gchar *tmp_filename;
GError *error = NULL; GError *error = NULL;
gboolean success = FALSE;
if (output == NULL) if (output == NULL)
{ {
@ -81,16 +82,16 @@ write_out_typelib (gchar *prefix,
if (file == NULL) if (file == NULL)
{ {
g_fprintf (stderr, "failed to open '%s': %s\n", g_fprintf (stderr, "failed to open '%s': %s\n",
tmp_filename, g_strerror (errno)); tmp_filename, g_strerror (errno));
goto out; goto out;
} }
} }
written = fwrite (typelib->data, 1, typelib->len, file); written = fwrite (typelib->data, 1, typelib->len, file);
if (written < typelib->len) { if (written < typelib->len) {
g_error ("ERROR: Could not write the whole output: %s", g_fprintf (stderr, "ERROR: Could not write the whole output: %s",
strerror(errno)); strerror(errno));
goto out; goto out;
} }
@ -100,13 +101,17 @@ write_out_typelib (gchar *prefix,
{ {
if (!g_file_move (tmp_file_obj, file_obj, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error)) if (!g_file_move (tmp_file_obj, file_obj, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, &error))
{ {
g_error ("ERROR: failed to rename %s to %s: %s", tmp_filename, filename, error->message); g_fprintf (stderr, "ERROR: failed to rename %s to %s: %s", tmp_filename, filename, error->message);
g_clear_error (&error); g_clear_error (&error);
goto out;
} }
} }
success = TRUE;
out: out:
g_free (filename); g_free (filename);
g_free (tmp_filename); g_free (tmp_filename);
return success;
} }
GLogLevelFlags logged_levels; GLogLevelFlags logged_levels;
@ -210,7 +215,8 @@ main (int argc, char ** argv)
g_error ("Invalid typelib for module '%s': %s", g_error ("Invalid typelib for module '%s': %s",
module->name, error->message); module->name, error->message);
write_out_typelib (NULL, typelib); if (!write_out_typelib (NULL, typelib))
return 1;
g_typelib_free (typelib); g_typelib_free (typelib);
typelib = NULL; typelib = NULL;
} }