Add shell_get_file_contents_utf8_sync(), use it instead of gio temporarily

Adding correct annotations to Gio.File.load_contents revealed that gjs
doesn't actually support array+length combinations.  For 3.0 this would
be invasive to fix, so add a method to ShellGlobal which does what
we need.

https://bugzilla.gnome.org/show_bug.cgi?id=646333
This commit is contained in:
Colin Walters
2011-03-31 16:13:07 -04:00
committed by Owen W. Taylor
parent d19f2bb6d2
commit 92f09a60f6
4 changed files with 55 additions and 19 deletions

View File

@ -2229,3 +2229,36 @@ shell_get_contact_events (TplLogManager *log_manager,
NULL, NULL,
callback, NULL);
}
/**
* shell_get_file_contents_utf8_sync:
* @path: UTF-8 encoded filename path
* @error: a #GError
*
* Synchronously load the contents of a file as a NUL terminated
* string, validating it as UTF-8. Embedded NUL characters count as
* invalid content.
*
* Returns: (transfer full): File contents
*/
char *
shell_get_file_contents_utf8_sync (const char *path,
GError **error)
{
char *contents;
gsize len;
if (!g_file_get_contents (path, &contents, &len, error))
return NULL;
if (!g_utf8_validate (contents, len, NULL))
{
g_free (contents);
g_set_error (error,
G_IO_ERROR,
G_IO_ERROR_FAILED,
"File %s contains invalid UTF-8",
path);
return NULL;
}
return contents;
}

View File

@ -185,6 +185,9 @@ void shell_get_contact_events (TplLogManager *log_manager,
guint num_events,
GAsyncReadyCallback callback);
char *shell_get_file_contents_utf8_sync (const char *path,
GError **error);
G_END_DECLS
#endif /* __SHELL_GLOBAL_H__ */