From 727664fa20fe52824e97c43b146f8165d73fffbd Mon Sep 17 00:00:00 2001 From: Richard Hughes Date: Tue, 27 Oct 2015 09:56:04 +0000 Subject: [PATCH] Clear the in-memory firmware store only after parsing a valid XML file If we send junk to UpdateMetadata() the deamon did something like this: * Clear existing entries from memory * Try to load junk file into memory -> error This left us with no entries in the in-memory store, which required the user to either keep retrying the 'fwupdmgr update' until it worked or just forced them to restart fwupd so it loaded the old valid store from the cache file. Now, only clear the in-memory store and add the new firmware entries when we know the file has been parsed correctly. Fixes: https://github.com/hughsie/fwupd/issues/35 --- src/fu-main.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/fu-main.c b/src/fu-main.c index ccbcc87b3..dc7bb8ce7 100644 --- a/src/fu-main.c +++ b/src/fu-main.c @@ -698,6 +698,9 @@ static gboolean fu_main_daemon_update_metadata (FuMainPrivate *priv, gint fd, gint fd_sig, GError **error) { guint8 magic[2]; + guint i; + GPtrArray *apps; + g_autoptr(AsStore) store = NULL; g_autoptr(GBytes) bytes = NULL; g_autoptr(GBytes) bytes_raw = NULL; g_autoptr(GBytes) bytes_sig = NULL; @@ -754,13 +757,21 @@ fu_main_daemon_update_metadata (FuMainPrivate *priv, gint fd, gint fd_sig, GErro if (!fu_keyring_verify_data (kr, bytes_raw, bytes_sig, error)) return FALSE; - /* merge in the new contents */ - as_store_remove_all (priv->store); - if (!as_store_from_xml (priv->store, + /* load the store locally until we know it is valid */ + store = as_store_new (); + if (!as_store_from_xml (store, g_bytes_get_data (bytes, NULL), NULL, error)) return FALSE; + /* add the new application from the store */ + as_store_remove_all (priv->store); + apps = as_store_get_apps (store); + for (i = 0; i < apps->len; i++) { + AsApp *app = g_ptr_array_index (apps, i); + as_store_add_app (priv->store, app); + } + /* save the new file */ as_store_set_api_version (priv->store, 0.9); file = g_file_new_for_path ("/var/cache/app-info/xmls/fwupd.xml");