Bug 628937 - gracefully handle broken schemas

Implement the second feature requested in the bug: silently ignore
override files that attempt to override schemas that are not currently
installed.

Also, support 'strictness' being optional for other errors when parsing
override files (ie: inability to open the file, unknown key name, parse
errors, out of range).  We don't completely back out the file in this
case — as that is difficult with the current implementation — but just
ignore the override for the single key.
This commit is contained in:
Ryan Lortie 2010-09-27 11:10:11 -04:00
parent bd290081ff
commit 3e771509b4

View File

@ -1685,9 +1685,10 @@ compare_strings (gconstpointer a,
static gboolean static gboolean
set_overrides (GHashTable *schema_table, set_overrides (GHashTable *schema_table,
gchar **files, gchar **files,
GError **error) gboolean strict)
{ {
const gchar *filename; const gchar *filename;
GError *error = NULL;
while ((filename = *files++)) while ((filename = *files++))
{ {
@ -1696,10 +1697,19 @@ set_overrides (GHashTable *schema_table,
gint i; gint i;
key_file = g_key_file_new (); key_file = g_key_file_new ();
if (!g_key_file_load_from_file (key_file, filename, 0, error)) if (!g_key_file_load_from_file (key_file, filename, 0, &error))
{ {
fprintf (stderr, "%s: %s. ", filename, error->message);
g_key_file_free (key_file); g_key_file_free (key_file);
g_clear_error (&error);
if (!strict)
{
fprintf (stderr, _("Ignoring this file.\n"));
continue;
}
fprintf (stderr, _("--strict was specified; exiting.\n"));
return FALSE; return FALSE;
} }
@ -1715,16 +1725,11 @@ set_overrides (GHashTable *schema_table,
schema = g_hash_table_lookup (schema_table, group); schema = g_hash_table_lookup (schema_table, group);
if (schema == NULL) if (schema == NULL)
{ /* Having the schema not be installed is expected to be a
g_set_error (error, G_KEY_FILE_ERROR, * common case. Don't even emit an error message about
G_KEY_FILE_ERROR_GROUP_NOT_FOUND, * that.
_("No such schema `%s' specified in " */
"override file `%s'"), group, filename); continue;
g_key_file_free (key_file);
g_strfreev (groups);
return FALSE;
}
keys = g_key_file_get_keys (key_file, group, NULL, NULL); keys = g_key_file_get_keys (key_file, group, NULL, NULL);
g_assert (keys != NULL); g_assert (keys != NULL);
@ -1740,11 +1745,17 @@ set_overrides (GHashTable *schema_table,
if (state == NULL) if (state == NULL)
{ {
g_set_error (error, G_KEY_FILE_ERROR, fprintf (stderr, _("No such key `%s' in schema `%s' as "
G_KEY_FILE_ERROR_KEY_NOT_FOUND, "specified in override file `%s'"),
_("No such key `%s' in schema `%s' as " key, group, filename);
"specified in override file `%s'"),
key, group, filename); if (!strict)
{
fprintf (stderr, _("; ignoring override for this key.\n"));
continue;
}
fprintf (stderr, _(" and --strict was specified; exiting.\n"));
g_key_file_free (key_file); g_key_file_free (key_file);
g_strfreev (groups); g_strfreev (groups);
g_strfreev (keys); g_strfreev (keys);
@ -1756,14 +1767,28 @@ set_overrides (GHashTable *schema_table,
g_assert (string != NULL); g_assert (string != NULL);
value = g_variant_parse (state->type, string, value = g_variant_parse (state->type, string,
NULL, NULL, error); NULL, NULL, &error);
if (value == NULL) if (value == NULL)
{ {
fprintf (stderr, _("error parsing key `%s' in schema `%s' "
"as specified in override file `%s': "
"%s. "),
key, group, filename, error->message);
g_clear_error (&error);
g_free (string);
if (!strict)
{
fprintf (stderr, _("Ignoring override for this key.\n"));
continue;
}
fprintf (stderr, _("--strict was specified; exiting.\n"));
g_key_file_free (key_file); g_key_file_free (key_file);
g_strfreev (groups); g_strfreev (groups);
g_strfreev (keys); g_strfreev (keys);
g_free (string);
return FALSE; return FALSE;
} }
@ -1773,18 +1798,25 @@ set_overrides (GHashTable *schema_table,
if (g_variant_compare (value, state->minimum) < 0 || if (g_variant_compare (value, state->minimum) < 0 ||
g_variant_compare (value, state->maximum) > 0) g_variant_compare (value, state->maximum) > 0)
{ {
g_set_error (error, G_MARKUP_ERROR, fprintf (stderr,
G_MARKUP_ERROR_INVALID_CONTENT, _("override for key `%s' in schema `%s' in "
_("override for key `%s' in schema `%s' in " "override file `%s' is out of the range "
"override file `%s' is out of the range " "given in the schema"),
"given in the schema"), key, group, filename);
key, group, filename);
g_key_file_free (key_file);
g_variant_unref (value); g_variant_unref (value);
g_free (string);
if (!strict)
{
fprintf (stderr, _("; ignoring override for this key.\n"));
continue;
}
fprintf (stderr, _(" and --strict was specified; exiting.\n"));
g_key_file_free (key_file);
g_strfreev (groups); g_strfreev (groups);
g_strfreev (keys); g_strfreev (keys);
g_free (string);
return FALSE; return FALSE;
} }
@ -1794,18 +1826,25 @@ set_overrides (GHashTable *schema_table,
{ {
if (!is_valid_choices (value, state->strinfo)) if (!is_valid_choices (value, state->strinfo))
{ {
g_set_error (error, G_MARKUP_ERROR, fprintf (stderr,
G_MARKUP_ERROR_INVALID_CONTENT, _("override for key `%s' in schema `%s' in "
_("override for key `%s' in schema `%s' in " "override file `%s' is not in the list "
"override file `%s' is not in the list " "of valid choices"),
"of valid choices"), key, group, filename);
key, group, filename);
g_key_file_free (key_file);
g_variant_unref (value); g_variant_unref (value);
g_free (string);
if (!strict)
{
fprintf (stderr, _("; ignoring override for this key.\n"));
continue;
}
fprintf (stderr, _(" and --strict was specified; exiting.\n"));
g_key_file_free (key_file);
g_strfreev (groups); g_strfreev (groups);
g_strfreev (keys); g_strfreev (keys);
g_free (string);
return FALSE; return FALSE;
} }
@ -1813,9 +1852,9 @@ set_overrides (GHashTable *schema_table,
g_variant_unref (state->default_value); g_variant_unref (state->default_value);
state->default_value = value; state->default_value = value;
g_free (string);
} }
g_strfreev (keys); g_strfreev (keys);
} }
@ -1939,8 +1978,14 @@ main (int argc, char **argv)
return 1; return 1;
} }
if ((override_files != NULL && !set_overrides (table, override_files, &error)) || if (override_files != NULL &&
(!dry_run && !write_to_file (table, target, &error))) !set_overrides (table, override_files, strict))
{
g_free (target);
return 1;
}
if (!dry_run && !write_to_file (table, target, &error))
{ {
fprintf (stderr, "%s\n", error->message); fprintf (stderr, "%s\n", error->message);
g_free (target); g_free (target);