[packagekit] packagekit: Branch 'master' - 3 commits

Richard Hughes hughsient at kemper.freedesktop.org
Sat Oct 13 04:25:02 PDT 2007


 TODO               |    3 +
 src/Makefile.am    |    6 ++
 src/pk-backend.c   |   41 ++++++++++++++++-
 src/pk-conf.c      |   75 +++++++++++++++++++++++++++++++
 src/pk-conf.h      |    3 +
 src/pk-inhibit.c   |  126 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/pk-inhibit.h   |   59 ++++++++++++++++++++++++
 src/pk-self-test.c |    2 
 8 files changed, 312 insertions(+), 3 deletions(-)

New commits:
diff-tree 7431bfecfc45f22e9927c2baadf04179ff521022 (from 21e7fc90474ea75d618c115495ec470f09b39687)
Author: Richard Hughes <richard at hughsie.com>
Date:   Sat Oct 13 12:23:18 2007 +0100

    add unit tests for PkConf

diff --git a/TODO b/TODO
index 7a78452..3d94698 100644
--- a/TODO
+++ b/TODO
@@ -29,6 +29,5 @@ http://people.freedesktop.org/~david/hal
 do the g-p-m client stuff ALSO in the client program (for nice error message)
 
 *** add more unit tests ***
-PkConf
 PkInhibit
 
diff --git a/src/Makefile.am b/src/Makefile.am
index f7283a9..bd1bc4c 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -110,10 +110,14 @@ check_PROGRAMS =					\
 	pk-self-test
 
 pk_self_test_SOURCES =					\
+	pk-inhibit.h					\
+	pk-inhibit.c					\
 	pk-backend.h					\
 	pk-backend.c					\
 	pk-spawn.h					\
 	pk-spawn.c					\
+	pk-conf.h					\
+	pk-conf.c					\
 	pk-thread-list.h				\
 	pk-thread-list.c				\
 	pk-transaction-id.h				\
diff --git a/src/pk-conf.c b/src/pk-conf.c
index 1ed949f..89ac2b1 100644
--- a/src/pk-conf.c
+++ b/src/pk-conf.c
@@ -55,6 +55,8 @@ pk_conf_get_string (PkConf *conf, const 
 	GError *error = NULL;
 	value = g_key_file_get_string (conf->priv->keyfile, "Daemon", key, &error);
 	if (error != NULL) {
+		/* set to missing value */
+		value = PK_CONF_VALUE_STRING_MISSING;
 		pk_debug ("%s read error: %s", key, error->message);
 		g_error_free (error);
 	}
@@ -67,10 +69,12 @@ pk_conf_get_string (PkConf *conf, const 
 gint
 pk_conf_get_int (PkConf *conf, const gchar *key)
 {
-	gint value = 0;
+	gint value;
 	GError *error = NULL;
 	value = g_key_file_get_integer (conf->priv->keyfile, "Daemon", key, &error);
 	if (error != NULL) {
+		/* set to missing value */
+		value = PK_CONF_VALUE_INT_MISSING;
 		pk_debug ("%s read error: %s", key, error->message);
 		g_error_free (error);
 	}
@@ -155,3 +159,72 @@ pk_conf_new (void)
 	return PK_CONF (pk_conf_object);
 }
 
+/***************************************************************************
+ ***                          MAKE CHECK TESTS                           ***
+ ***************************************************************************/
+#ifdef PK_BUILD_TESTS
+#include <libselftest.h>
+
+void
+libst_conf (LibSelfTest *test)
+{
+	PkConf *conf;
+	gchar *text;
+	gint value;
+
+	if (libst_start (test, "PkConf", CLASS_AUTO) == FALSE) {
+		return;
+	}
+
+
+	/************************************************************/
+	libst_title (test, "get an instance");
+	conf = pk_conf_new ();
+	if (conf != NULL) {
+		libst_success (test, NULL);
+	} else {
+		libst_failed (test, NULL);
+	}
+
+	/************************************************************/
+	libst_title (test, "get the default backend");
+	text = pk_conf_get_string (conf, "DefaultBackend");
+	if (text != PK_CONF_VALUE_STRING_MISSING) {
+		libst_success (test, "got default backend '%s'", text);
+	} else {
+		libst_failed (test, "got NULL!");
+	}
+
+	/************************************************************/
+	libst_title (test, "get a string that doesn't exist");
+	text = pk_conf_get_string (conf, "FooBarBaz");
+	if (text == PK_CONF_VALUE_STRING_MISSING) {
+		libst_success (test, "got NULL", text);
+	} else {
+		libst_failed (test, "got return value '%s'", text);
+	}
+
+	/************************************************************/
+	libst_title (test, "get the shutdown timeout");
+	value = pk_conf_get_int (conf, "ShutdownTimeout");
+	if (value != PK_CONF_VALUE_INT_MISSING) {
+		libst_success (test, "got ShutdownTimeout '%i'", value);
+	} else {
+		libst_failed (test, "got %i", value);
+	}
+
+	/************************************************************/
+	libst_title (test, "get an int that doesn't exist");
+	value = pk_conf_get_int (conf, "FooBarBaz");
+	if (value == PK_CONF_VALUE_INT_MISSING) {
+		libst_success (test, "got %i", value);
+	} else {
+		libst_failed (test, "got return value '%i'", value);
+	}
+
+	g_object_unref (conf);
+
+	libst_end (test);
+}
+#endif
+
diff --git a/src/pk-conf.h b/src/pk-conf.h
index e181ee4..41ccb45 100644
--- a/src/pk-conf.h
+++ b/src/pk-conf.h
@@ -46,6 +46,9 @@ typedef struct
 	GObjectClass	parent_class;
 } PkConfClass;
 
+#define PK_CONF_VALUE_INT_MISSING	-1
+#define PK_CONF_VALUE_STRING_MISSING	NULL
+
 GType		 pk_conf_get_type		(void);
 PkConf		*pk_conf_new			(void);
 
diff --git a/src/pk-self-test.c b/src/pk-self-test.c
index b807cf2..ea73015 100644
--- a/src/pk-self-test.c
+++ b/src/pk-self-test.c
@@ -25,6 +25,7 @@
 #include <pk-debug.h>
 
 /* prototypes */
+void libst_conf (LibSelfTest *test);
 void libst_spawn (LibSelfTest *test);
 void libst_thread_list (LibSelfTest *test);
 void libst_transaction_list (LibSelfTest *test);
@@ -42,6 +43,7 @@ main (int argc, char **argv)
 	pk_debug_init (TRUE);
 
 	/* tests go here */
+	libst_conf (&test);
 	libst_spawn (&test);
 	libst_thread_list (&test);
 	libst_transaction_list (&test);
diff-tree 21e7fc90474ea75d618c115495ec470f09b39687 (from 5ad51f2e73c78fbfa8b4aaa1692d028d3801965c)
Author: Richard Hughes <richard at hughsie.com>
Date:   Sat Oct 13 12:05:32 2007 +0100

    use the inhibit stuff, even tho there's no code there yet...

diff --git a/TODO b/TODO
index 54e42fe..7a78452 100644
--- a/TODO
+++ b/TODO
@@ -28,3 +28,7 @@ Do the HAL inhibit in packagekitd:
 http://people.freedesktop.org/~david/hal-spec/hal-spec.html
 do the g-p-m client stuff ALSO in the client program (for nice error message)
 
+*** add more unit tests ***
+PkConf
+PkInhibit
+
diff --git a/src/pk-backend.c b/src/pk-backend.c
index 079ff53..8893af8 100644
--- a/src/pk-backend.c
+++ b/src/pk-backend.c
@@ -46,6 +46,7 @@
 #include "pk-marshal.h"
 #include "pk-enum.h"
 #include "pk-spawn.h"
+#include "pk-inhibit.h"
 #include "pk-thread-list.h"
 
 #define PK_BACKEND_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PK_TYPE_BACKEND, PkBackendPrivate))
@@ -72,6 +73,7 @@ struct _PkBackendPrivate
 	gboolean		 assigned;
 	gboolean		 set_error;
 	PkNetwork		*network;
+	PkInhibit		*inhibit;
 	/* needed for gui coldplugging */
 	guint			 last_percentage;
 	guint			 last_subpercentage;
@@ -907,6 +909,9 @@ pk_backend_finished (PkBackend *backend)
 		pk_error ("Internal error, cannot continue (will segfault in the near future...)");
 	}
 
+	/* remove any inhibit */
+	pk_inhibit_remove (backend->priv->inhibit, backend);
+
 	/* we have to run this idle as the command may finish before the transaction
 	 * has been sent to the client. I love async... */
 	pk_debug ("adding finished %p to timeout loop", backend);
@@ -942,6 +947,14 @@ pk_backend_allow_interrupt (PkBackend *b
 
 	pk_debug ("emit allow-interrupt %i", allow_restart);
 	backend->priv->is_killable = allow_restart;
+
+	/* remove or add the hal inhibit */
+	if (allow_restart == TRUE) {
+		pk_inhibit_remove (backend->priv->inhibit, backend);
+	} else {
+		pk_inhibit_add (backend->priv->inhibit, backend);
+	}
+
 	g_signal_emit (backend, signals [PK_BACKEND_ALLOW_INTERRUPT], 0);
 	return TRUE;
 }
@@ -979,8 +992,8 @@ pk_backend_cancel (PkBackend *backend)
 /**
  * pk_backend_run:
  */
-gboolean
-pk_backend_run (PkBackend *backend)
+static gboolean
+pk_backend_set_running (PkBackend *backend)
 {
 	g_return_val_if_fail (backend != NULL, FALSE);
 
@@ -1047,6 +1060,24 @@ pk_backend_run (PkBackend *backend)
 }
 
 /**
+ * pk_backend_run:
+ */
+gboolean
+pk_backend_run (PkBackend *backend)
+{
+	gboolean ret;
+	g_return_val_if_fail (backend != NULL, FALSE);
+
+	ret = pk_backend_set_running (backend);
+	if (ret == TRUE) {
+		/* we start inhibited, it's up to the backed to
+		 * release early if a shutdown is possible */
+		pk_inhibit_add (backend->priv->inhibit, backend);
+	}
+	return ret;
+}
+
+/**
  * pk_backend_get_depends:
  */
 gboolean
@@ -1489,6 +1520,11 @@ pk_backend_finalize (GObject *object)
 	if (backend->priv->spawn != NULL) {
 		pk_backend_spawn_helper_delete (backend);
 	}
+
+	/* remove any inhibit, it's okay to call this function when it's not needed */
+	pk_inhibit_remove (backend->priv->inhibit, backend);
+	g_object_unref (backend->priv->inhibit);
+
 	g_object_unref (backend->priv->network);
 	g_object_unref (backend->priv->thread_list);
 
@@ -1602,6 +1638,7 @@ pk_backend_init (PkBackend *backend)
 	backend->priv->role = PK_ROLE_ENUM_UNKNOWN;
 	backend->priv->status = PK_STATUS_ENUM_UNKNOWN;
 	backend->priv->exit = PK_EXIT_ENUM_SUCCESS;
+	backend->priv->inhibit = pk_inhibit_new ();
 	backend->priv->network = pk_network_new ();
 	backend->priv->thread_list = pk_thread_list_new ();
 }
diff-tree 5ad51f2e73c78fbfa8b4aaa1692d028d3801965c (from 8fe0fa647216a28af14389772f954db514484af3)
Author: Richard Hughes <richard at hughsie.com>
Date:   Sat Oct 13 11:47:34 2007 +0100

    add the inhibit framework for the HAL locking

diff --git a/src/Makefile.am b/src/Makefile.am
index c0191fd..f7283a9 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -51,6 +51,8 @@ packagekitd_SOURCES =					\
 	pk-spawn.h					\
 	pk-engine.h					\
 	pk-engine.c					\
+	pk-inhibit.h					\
+	pk-inhibit.c					\
 	pk-thread-list.h				\
 	pk-thread-list.c				\
 	pk-transaction-db.h				\
diff --git a/src/pk-inhibit.c b/src/pk-inhibit.c
new file mode 100644
index 0000000..00bbf45
--- /dev/null
+++ b/src/pk-inhibit.c
@@ -0,0 +1,126 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007 Richard Hughes <richard at hughsie.com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_INHIBITIG_H
+#  include <inhibitig.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/time.h>
+#include <sys/types.h>
+
+#include <glib/gi18n.h>
+#include <glib.h>
+
+#include "pk-debug.h"
+#include "pk-inhibit.h"
+
+#define PK_INHIBIT_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PK_TYPE_INHIBIT, PkInhibitPrivate))
+
+struct PkInhibitPrivate
+{
+	GPtrArray		*array;
+};
+
+G_DEFINE_TYPE (PkInhibit, pk_inhibit, G_TYPE_OBJECT)
+static gpointer pk_inhibit_object = NULL;
+
+/**
+ * pk_inhibit_add:
+ **/
+gboolean
+pk_inhibit_add (PkInhibit *inhibit, gpointer data)
+{
+	g_return_val_if_fail (inhibit != NULL, FALSE);
+	g_return_val_if_fail (PK_IS_INHIBIT (inhibit), FALSE);
+	return TRUE;
+}
+
+/**
+ * pk_inhibit_remove:
+ **/
+gboolean
+pk_inhibit_remove (PkInhibit *inhibit, gpointer data)
+{
+	g_return_val_if_fail (inhibit != NULL, FALSE);
+	g_return_val_if_fail (PK_IS_INHIBIT (inhibit), FALSE);
+	return TRUE;
+}
+
+/**
+ * pk_inhibit_finalize:
+ **/
+static void
+pk_inhibit_finalize (GObject *object)
+{
+	PkInhibit *inhibit;
+	g_return_if_fail (object != NULL);
+	g_return_if_fail (PK_IS_INHIBIT (object));
+	inhibit = PK_INHIBIT (object);
+
+	g_ptr_array_free (inhibit->priv->array, TRUE);
+
+	G_OBJECT_CLASS (pk_inhibit_parent_class)->finalize (object);
+}
+
+/**
+ * pk_inhibit_class_init:
+ **/
+static void
+pk_inhibit_class_init (PkInhibitClass *klass)
+{
+	GObjectClass *object_class = G_OBJECT_CLASS (klass);
+	object_class->finalize = pk_inhibit_finalize;
+	g_type_class_add_private (klass, sizeof (PkInhibitPrivate));
+}
+
+/**
+ * pk_inhibit_init:
+ *
+ * initialises the inhibit class. NOTE: We expect inhibit objects
+ * to *NOT* be removed or added during the session.
+ * We only control the first inhibit object if there are more than one.
+ **/
+static void
+pk_inhibit_init (PkInhibit *inhibit)
+{
+	inhibit->priv = PK_INHIBIT_GET_PRIVATE (inhibit);
+	inhibit->priv->array = g_ptr_array_new ();
+}
+
+/**
+ * pk_inhibit_new:
+ * Return value: A new inhibit class instance.
+ **/
+PkInhibit *
+pk_inhibit_new (void)
+{
+	if (pk_inhibit_object != NULL) {
+		g_object_ref (pk_inhibit_object);
+	} else {
+		pk_inhibit_object = g_object_new (PK_TYPE_INHIBIT, NULL);
+		g_object_add_weak_pointer (pk_inhibit_object, &pk_inhibit_object);
+	}
+	return PK_INHIBIT (pk_inhibit_object);
+}
+
diff --git a/src/pk-inhibit.h b/src/pk-inhibit.h
new file mode 100644
index 0000000..d71784b
--- /dev/null
+++ b/src/pk-inhibit.h
@@ -0,0 +1,59 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
+ *
+ * Copyright (C) 2007 Richard Hughes <richard at hughsie.com>
+ *
+ * Licensed under the GNU General Public License Version 2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#ifndef __PK_INHIBIT_H
+#define __PK_INHIBIT_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define PK_TYPE_INHIBIT		(pk_inhibit_get_type ())
+#define PK_INHIBIT(o)		(G_TYPE_CHECK_INSTANCE_CAST ((o), PK_TYPE_INHIBIT, PkInhibit))
+#define PK_INHIBIT_CLASS(k)	(G_TYPE_CHECK_CLASS_CAST((k), PK_TYPE_INHIBIT, PkInhibitClass))
+#define PK_IS_INHIBIT(o)	(G_TYPE_CHECK_INSTANCE_TYPE ((o), PK_TYPE_INHIBIT))
+#define PK_IS_INHIBIT_CLASS(k)	(G_TYPE_CHECK_CLASS_TYPE ((k), PK_TYPE_INHIBIT))
+#define PK_INHIBIT_GET_CLASS(o)	(G_TYPE_INSTANCE_GET_CLASS ((o), PK_TYPE_INHIBIT, PkInhibitClass))
+
+typedef struct PkInhibitPrivate PkInhibitPrivate;
+
+typedef struct
+{
+	GObject		      parent;
+	PkInhibitPrivate     *priv;
+} PkInhibit;
+
+typedef struct
+{
+	GObjectClass	parent_class;
+} PkInhibitClass;
+
+GType		 pk_inhibit_get_type		(void);
+PkInhibit	*pk_inhibit_new			(void);
+
+gboolean	 pk_inhibit_add			(PkInhibit	*inhibit,
+						 gpointer	 data);
+gboolean	 pk_inhibit_remove		(PkInhibit	*inhibit,
+						 gpointer	 data);
+
+G_END_DECLS
+
+#endif /* __PK_INHIBIT_H */



More information about the PackageKit mailing list