hal/hald util.c,1.9,1.10 util.h,1.7,1.8

David Zeuthen david at freedesktop.org
Mon Mar 7 11:08:43 PST 2005


Update of /cvs/hal/hal/hald
In directory gabe:/tmp/cvs-serv20251/hald

Modified Files:
	util.c util.h 
Log Message:
2005-03-07  David Zeuthen  <davidz at redhat.com>

	* doc/TODO: Updated this to reflect reality

	* hald/linux2/pmu.c (battery_refresh): Pass reuse=FALSE
	(ac_adapter_refresh): Pass reuse=FALSE

	* hald/linux2/classdev.c (sound_add): pass reuse=FALSE

	* hald/linux2/acpi.c: Poll every five secs instead of every two secs
	as this actually is a very expensive operation - someone needs to
	optimize hald/util.c:hal_util_grep_file before we can change this -
	see the TODO in that function
	(battery_refresh): Use the hal_util_grep_discard_existing_data() since
	we are passing reuse=TRUE. Minimize amount of reads.
	(fan_refresh): Don't set processor.number here (!)

	* hald/util.c (hal_util_grep_discard_existing_data): New function to
	purge "reused" data
	(hal_util_grep_file): Take the reuse parameter
	(hal_util_grep_string_elem_from_file): Take and pass reuse param
	(hal_util_grep_int_elem_from_file): Take and pass reuse param
	(hal_util_set_string_elem_from_file): Take and pass reuse param
	(hal_util_set_int_elem_from_file): Take and pass reuse param
	(hal_util_set_bool_elem_from_file): Take and pass reuse param

	* hald/util.h: Fixup prototypes for functions above and add prototype
	for hal_util_grep_discard_existing_data



Index: util.c
===================================================================
RCS file: /cvs/hal/hal/hald/util.c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- util.c	2 Mar 2005 19:22:10 -0000	1.9
+++ util.c	7 Mar 2005 19:08:41 -0000	1.10
@@ -645,6 +645,14 @@
 	return TRUE;
 }
 
+static gboolean _grep_can_reuse = FALSE;
+
+void 
+hal_util_grep_discard_existing_data (void)
+{
+	_grep_can_reuse = FALSE;
+}
+
 /** Given a directory and filename, open the file and search for the
  *  first line that starts with the given linestart string. Returns
  *  the rest of the line as a string if found.
@@ -652,6 +660,9 @@
  *  @param  directory           Directory, e.g. "/proc/acpi/battery/BAT0"
  *  @param  file                File, e.g. "info"
  *  @param  linestart           Start of line, e.g. "serial number"
+ *  @param  reuse               Whether we should reuse the file contents
+ *                              if the file is the same; can be cleared
+ *                              with hal_util_grep_discard_existing_data()
  *  @return                     NULL if not found, otherwise the remainder
  *                              of the line, e.g. ":           21805" if
  *                              the file /proc/acpi/battery/BAT0 contains
@@ -660,7 +671,7 @@
  *                              invocation of this function.
  */
 gchar *
-hal_util_grep_file (const gchar *directory, const gchar *file, const gchar *linestart)
+hal_util_grep_file (const gchar *directory, const gchar *file, const gchar *linestart, gboolean reuse)
 {
 	FILE *f;
 	static gchar buf[512];
@@ -670,6 +681,10 @@
 
 	result = NULL;
 
+	/* TODO: use reuse and _grep_can_reuse parameters to avoid loading
+	 *       the file again and again
+	 */
+
 	if (file != NULL && strlen (file) > 0)
 		snprintf (filename, sizeof (filename), "%s/%s", directory, file);
 	else
@@ -709,7 +724,7 @@
 
 gchar *
 hal_util_grep_string_elem_from_file (const gchar *directory, const gchar *file, 
-				     const gchar *linestart, guint elem)
+				     const gchar *linestart, guint elem, gboolean reuse)
 {
 	gchar *line;
 	gchar *res;
@@ -720,7 +735,7 @@
 	res = NULL;
 	tokens = NULL;
 
-	if (((line = hal_util_grep_file (directory, file, linestart)) == NULL) || (strlen (line) == 0))
+	if (((line = hal_util_grep_file (directory, file, linestart, reuse)) == NULL) || (strlen (line) == 0))
 		goto out;
 
 	tokens = g_strsplit_set (line, " \t:", 0);
@@ -744,7 +759,7 @@
 
 gint
 hal_util_grep_int_elem_from_file (const gchar *directory, const gchar *file, 
-				  const gchar *linestart, guint elem, guint base)
+				  const gchar *linestart, guint elem, guint base, gboolean reuse)
 {
 	gchar *endptr;
 	gchar *strvalue;
@@ -752,7 +767,7 @@
 
 	value = G_MAXINT;
 
-	if ((strvalue = hal_util_grep_string_elem_from_file (directory, file, linestart, elem)) == NULL)
+	if ((strvalue = hal_util_grep_string_elem_from_file (directory, file, linestart, elem, reuse)) == NULL)
 		goto out;
 
 	value = strtol (strvalue, &endptr, base);
@@ -790,14 +805,14 @@
 gboolean
 hal_util_set_string_elem_from_file (HalDevice *d, const gchar *key, 
 				    const gchar *directory, const gchar *file, 
-				    const gchar *linestart, guint elem)
+				    const gchar *linestart, guint elem, gboolean reuse)
 {
 	gboolean res;
 	gchar *value;
 
 	res = FALSE;
 
-	if ((value = hal_util_grep_string_elem_from_file (directory, file, linestart, elem)) == NULL)
+	if ((value = hal_util_grep_string_elem_from_file (directory, file, linestart, elem, reuse)) == NULL)
 		goto out;
 
 	res = hal_device_property_set_string (d, key, value);
@@ -830,7 +845,7 @@
 gboolean
 hal_util_set_int_elem_from_file (HalDevice *d, const gchar *key, 
 				 const gchar *directory, const gchar *file, 
-				 const gchar *linestart, guint elem, guint base)
+				 const gchar *linestart, guint elem, guint base, gboolean reuse)
 {
 	gchar *endptr;
 	gboolean res;
@@ -839,7 +854,7 @@
 
 	res = FALSE;
 
-	if ((strvalue = hal_util_grep_string_elem_from_file (directory, file, linestart, elem)) == NULL)
+	if ((strvalue = hal_util_grep_string_elem_from_file (directory, file, linestart, elem, reuse)) == NULL)
 		goto out;
 
 	value = strtol (strvalue, &endptr, base);
@@ -886,7 +901,7 @@
 gboolean
 hal_util_set_bool_elem_from_file (HalDevice *d, const gchar *key, 
 				  const gchar *directory, const gchar *file, 
-				  const gchar *linestart, guint elem, const gchar *expected)
+				  const gchar *linestart, guint elem, const gchar *expected, gboolean reuse)
 {
 	gchar *line;
 	gboolean res;
@@ -896,7 +911,7 @@
 	res = FALSE;
 	tokens = NULL;
 
-	if (((line = hal_util_grep_file (directory, file, linestart)) == NULL) || (strlen (line) == 0))
+	if (((line = hal_util_grep_file (directory, file, linestart, reuse)) == NULL) || (strlen (line) == 0))
 		goto out;
 
 	tokens = g_strsplit_set (line, " \t:", 0);

Index: util.h
===================================================================
RCS file: /cvs/hal/hal/hald/util.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- util.h	28 Feb 2005 19:43:29 -0000	1.7
+++ util.h	7 Mar 2005 19:08:41 -0000	1.8
@@ -55,26 +55,28 @@
 
 gboolean hal_util_path_ascend (gchar *path);
 
+void hal_util_grep_discard_existing_data (void);
 
-gchar *hal_util_grep_file (const gchar *directory, const gchar *file, const gchar *linestart);
+gchar *hal_util_grep_file (const gchar *directory, const gchar *file, const gchar *linestart, gboolean reuse_file);
 
 gint hal_util_grep_int_elem_from_file (const gchar *directory, const gchar *file, 
-				       const gchar *linestart, guint elem, guint base);
+				       const gchar *linestart, guint elem, guint base, gboolean reuse_file);
 
 gchar *hal_util_grep_string_elem_from_file (const gchar *directory, const gchar *file, 
-					    const gchar *linestart, guint elem);
+					    const gchar *linestart, guint elem, gboolean reuse_file);
 
 gboolean hal_util_set_string_elem_from_file (HalDevice *d, const gchar *key, 
 					     const gchar *directory, const gchar *file, 
-					     const gchar *linestart, guint elem);
+					     const gchar *linestart, guint elem, gboolean reuse_file);
 
 gboolean hal_util_set_int_elem_from_file (HalDevice *d, const gchar *key, 
 					  const gchar *directory, const gchar *file, 
-					  const gchar *linestart, guint elem, guint base);
+					  const gchar *linestart, guint elem, guint base, gboolean reuse_file);
 
 gboolean hal_util_set_bool_elem_from_file (HalDevice *d, const gchar *key, 
 					   const gchar *directory, const gchar *file, 
-					   const gchar *linestart, guint elem, const gchar *expected);
+					   const gchar *linestart, guint elem, const gchar *expected, 
+					   gboolean reuse_file);
 
 struct HalHelperData_s;
 typedef struct HalHelperData_s HalHelperData;




More information about the hal-commit mailing list