[gnome-software/wip/temp/ubuntu-xenial-rebased: 232/329] Refresh the apt database
- From: Iain Lane <iainl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-software/wip/temp/ubuntu-xenial-rebased: 232/329] Refresh the apt database
- Date: Fri, 29 Apr 2016 14:04:51 +0000 (UTC)
commit 4f6f30e2bd15e8eb15e07464643fc9b2ff2a6454
Author: William Hua <william hua canonical com>
Date: Wed Apr 13 18:11:20 2016 -0400
Refresh the apt database
src/plugins/gs-plugin-apt.cc | 91 ++++++++++++++++++++++++++----------------
1 files changed, 57 insertions(+), 34 deletions(-)
---
diff --git a/src/plugins/gs-plugin-apt.cc b/src/plugins/gs-plugin-apt.cc
index f9fa832..37ab6e2 100644
--- a/src/plugins/gs-plugin-apt.cc
+++ b/src/plugins/gs-plugin-apt.cc
@@ -80,7 +80,8 @@ typedef struct {
#include "ubuntu-unity-launcher-proxy.h"
struct GsPluginPrivate {
- gsize loaded;
+ GMutex mutex;
+ gboolean loaded;
GHashTable *package_info;
GHashTable *installed_files;
GList *installed_packages;
@@ -149,6 +150,8 @@ gs_plugin_initialize (GsPlugin *plugin)
g_free,
g_free);
+ g_mutex_init (&plugin->priv->mutex);
+
pkgInitConfig (*_config);
pkgInitSystem (*_config, _system);
}
@@ -156,10 +159,14 @@ gs_plugin_initialize (GsPlugin *plugin)
void
gs_plugin_destroy (GsPlugin *plugin)
{
- g_hash_table_unref (plugin->priv->package_info);
- g_hash_table_unref (plugin->priv->installed_files);
- g_list_free (plugin->priv->installed_packages);
- g_list_free (plugin->priv->updatable_packages);
+ g_mutex_lock (&plugin->priv->mutex);
+ plugin->priv->loaded = FALSE;
+ g_clear_pointer (&plugin->priv->package_info, g_hash_table_unref);
+ g_clear_pointer (&plugin->priv->installed_files, g_hash_table_unref);
+ g_clear_pointer (&plugin->priv->installed_packages, g_list_free);
+ g_clear_pointer (&plugin->priv->updatable_packages, g_list_free);
+ g_mutex_unlock (&plugin->priv->mutex);
+ g_mutex_clear (&plugin->priv->mutex);
}
@@ -412,9 +419,14 @@ load_apt_db (GsPlugin *plugin, GError **error)
pkgSourceList *list;
pkgPolicy *policy;
pkgCacheFile cachefile;
- pkgCache *cache = cachefile.GetPkgCache();
+ pkgCache *cache;
pkgCache::PkgIterator P;
+ g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&plugin->priv->mutex);
+
+ if (plugin->priv->loaded)
+ return TRUE;
+ cache = cachefile.GetPkgCache();
list = cachefile.GetSourceList();
policy = cachefile.GetPolicy();
if (cache == NULL || _error->PendingError()) {
@@ -437,10 +449,23 @@ load_apt_db (GsPlugin *plugin, GError **error)
/* load filename -> package map into plugin->priv->installed_files */
look_for_files (plugin);
+ plugin->priv->loaded = TRUE;
return TRUE;
}
static void
+unload_apt_db (GsPlugin *plugin)
+{
+ g_autoptr(GMutexLocker) locker = g_mutex_locker_new (&plugin->priv->mutex);
+
+ plugin->priv->loaded = FALSE;
+ g_hash_table_remove_all (plugin->priv->package_info);
+ g_hash_table_remove_all (plugin->priv->installed_files);
+ g_clear_pointer (&plugin->priv->installed_packages, g_list_free);
+ g_clear_pointer (&plugin->priv->updatable_packages, g_list_free);
+}
+
+static void
get_changelog (GsPlugin *plugin, GsApp *app)
{
guint i;
@@ -546,16 +571,12 @@ gs_plugin_refine (GsPlugin *plugin,
GsApp *app;
PackageInfo *info;
const gchar *tmp;
+ g_autoptr(GMutexLocker) locker = NULL;
- /* Load database once */
- if (g_once_init_enter (&plugin->priv->loaded)) {
- gboolean ret;
+ if (!load_apt_db (plugin, error))
+ return FALSE;
- ret = load_apt_db (plugin, error);
- g_once_init_leave (&plugin->priv->loaded, TRUE);
- if (!ret)
- return FALSE;
- }
+ locker = g_mutex_locker_new (&plugin->priv->mutex);
for (link = *list; link; link = link->next) {
app = (GsApp *) link->data;
@@ -655,17 +676,12 @@ gs_plugin_add_installed (GsPlugin *plugin,
GError **error)
{
GList *link;
+ g_autoptr(GMutexLocker) locker = NULL;
- /* Load database once */
- if (g_once_init_enter (&plugin->priv->loaded)) {
- gboolean ret;
-
- ret = load_apt_db (plugin, error);
- g_once_init_leave (&plugin->priv->loaded, TRUE);
- if (!ret)
- return FALSE;
- }
+ if (!load_apt_db (plugin, error))
+ return FALSE;
+ locker = g_mutex_locker_new (&plugin->priv->mutex);
for (link = plugin->priv->installed_packages; link; link = link->next) {
PackageInfo *info = (PackageInfo *) link->data;
@@ -952,7 +968,12 @@ gs_plugin_refresh (GsPlugin *plugin,
if ((flags & GS_PLUGIN_REFRESH_FLAGS_UPDATES) == 0)
return TRUE;
- return aptd_transaction (plugin, "UpdateCache", NULL, NULL, NULL, error);
+ if (!aptd_transaction (plugin, "UpdateCache", NULL, NULL, NULL, error))
+ return FALSE;
+
+ unload_apt_db (plugin);
+
+ return TRUE;
}
gboolean
@@ -962,16 +983,12 @@ gs_plugin_add_updates (GsPlugin *plugin,
GError **error)
{
GList *link;
+ g_autoptr(GMutexLocker) locker = NULL;
- /* Load database once */
- if (g_once_init_enter (&plugin->priv->loaded)) {
- gboolean ret;
+ if (!load_apt_db (plugin, error))
+ return FALSE;
- ret = load_apt_db (plugin, error);
- g_once_init_leave (&plugin->priv->loaded, TRUE);
- if (!ret)
- return FALSE;
- }
+ locker = g_mutex_locker_new (&plugin->priv->mutex);
for (link = plugin->priv->updatable_packages; link; link = link->next) {
PackageInfo *info = (PackageInfo *) link->data;
@@ -1027,6 +1044,8 @@ gs_plugin_update (GsPlugin *plugin,
if (aptd_transaction (plugin, "UpgradeSystem", NULL, apps, g_variant_new_parsed ("(true,)"), error)) {
set_list_state (apps, AS_APP_STATE_INSTALLED);
+ unload_apt_db (plugin);
+
return TRUE;
} else {
set_list_state (apps, AS_APP_STATE_UPDATABLE_LIVE);
@@ -1067,6 +1086,8 @@ gs_plugin_update_app (GsPlugin *plugin,
for (i = 0; i < apps->len; i++)
gs_app_set_state (GS_APP (g_ptr_array_index (apps, i)),
AS_APP_STATE_INSTALLED);
+
+ unload_apt_db (plugin);
} else {
gs_app_set_state (app, AS_APP_STATE_UPDATABLE_LIVE);
@@ -1078,9 +1099,11 @@ gs_plugin_update_app (GsPlugin *plugin,
} else if (app_is_ours (app)) {
gs_app_set_state (app, AS_APP_STATE_INSTALLING);
- if (aptd_transaction (plugin, "UpgradePackages", app, NULL, NULL, error))
+ if (aptd_transaction (plugin, "UpgradePackages", app, NULL, NULL, error)) {
gs_app_set_state (app, AS_APP_STATE_INSTALLED);
- else {
+
+ unload_apt_db (plugin);
+ } else {
gs_app_set_state (app, AS_APP_STATE_UPDATABLE_LIVE);
return FALSE;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]