From 92f09a60f6b3f1c25dc0b3ad2f5e67e177c7880b Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Thu, 31 Mar 2011 16:13:07 -0400 Subject: [PATCH] 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 --- js/ui/extensionSystem.js | 8 +++++++- js/ui/search.js | 30 ++++++++++++------------------ src/shell-global.c | 33 +++++++++++++++++++++++++++++++++ src/shell-global.h | 3 +++ 4 files changed, 55 insertions(+), 19 deletions(-) diff --git a/js/ui/extensionSystem.js b/js/ui/extensionSystem.js index f2b88a537..eb82bc3bf 100644 --- a/js/ui/extensionSystem.js +++ b/js/ui/extensionSystem.js @@ -67,7 +67,13 @@ function loadExtension(dir, enabled, type) { return; } - let [success, metadataContents, len, etag] = metadataFile.load_contents(null); + let metadataContents; + try { + metadataContents = Shell.get_file_contents_utf8_sync(metadataFile.get_path()); + } catch (e) { + global.logError(baseErrorString + 'Failed to load metadata.json: ' + e); + return; + } let meta; try { meta = JSON.parse(metadataContents); diff --git a/js/ui/search.js b/js/ui/search.js index 7e30fc000..1244def89 100644 --- a/js/ui/search.js +++ b/js/ui/search.js @@ -274,24 +274,18 @@ OpenSearchSystem.prototype = { }, _addProvider: function(fileName) { - let file = Gio.file_new_for_path(global.datadir + '/search_providers/' + fileName); - let source = ''; - - file.load_contents_async(null, Lang.bind(this, function (obj, res) { - let [success, source] = file.load_contents_finish(res); - if (source) { - let [success, name, url, langs, icon_uri] = global.parse_search_provider(source); - let provider ={ name: name, - url: url, - id: this._providers.length, - icon_uri: icon_uri, - langs: langs }; - if (this._checkSupportedProviderLanguage(provider)) { - this._providers.push(provider); - this.emit('changed'); - } - } - })); + let path = global.datadir + '/search_providers/' + fileName; + let source = Shell.get_file_contents_utf8_sync(path); + let [success, name, url, langs, icon_uri] = global.parse_search_provider(source); + let provider ={ name: name, + url: url, + id: this._providers.length, + icon_uri: icon_uri, + langs: langs }; + if (this._checkSupportedProviderLanguage(provider)) { + this._providers.push(provider); + this.emit('changed'); + } }, _refresh: function() { diff --git a/src/shell-global.c b/src/shell-global.c index 2123e8aee..29caaa9cf 100644 --- a/src/shell-global.c +++ b/src/shell-global.c @@ -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; +} diff --git a/src/shell-global.h b/src/shell-global.h index 6e16abee9..f965e2bd8 100644 --- a/src/shell-global.h +++ b/src/shell-global.h @@ -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__ */