[2135] Avoiding openning same device multiple times in device iterator.

"David S. Miller" <[email protected]>
Newsgroups gmane.comp.boot-loaders.grub.cvs
Message-ID <[email protected]>
Revision: 2135
          http://svn.sv.gnu.org/viewvc/?view=rev&root=grub&revision=2135
Author:   davem
Date:     2009-04-24 12:04:10 +0000 (Fri, 24 Apr 2009)
Log Message:
-----------
	Avoiding openning same device multiple times in device iterator.

	* kern/device.c (grub_device_iterate): Define struct part_ent,
	and use it to build a list of partitions in interate_disk() and
	iterate_partition().

Modified Paths:
--------------
    trunk/grub2/ChangeLog
    trunk/grub2/kern/device.c

Modified: trunk/grub2/ChangeLog
===================================================================
--- trunk/grub2/ChangeLog	2009-04-23 10:03:41 UTC (rev 2134)
+++ trunk/grub2/ChangeLog	2009-04-24 12:04:10 UTC (rev 2135)
@@ -1,3 +1,11 @@
+2009-04-24  David S. Miller  <[email protected]>
+
+	Avoiding openning same device multiple times in device iterator.
+
+	* kern/device.c: (grub_device_iterate): Define struct part_ent,
+	and use it to build a list of partitions in interate_disk() and
+	iterate_partition().
+
 2009-04-23  David S. Miller  <[email protected]>
 
 	* kern/sparc64/ieee1275/openfw.c: Unused, delete.

Modified: trunk/grub2/kern/device.c
===================================================================
--- trunk/grub2/kern/device.c	2009-04-23 10:03:41 UTC (rev 2134)
+++ trunk/grub2/kern/device.c	2009-04-24 12:04:10 UTC (rev 2135)
@@ -83,6 +83,12 @@
   auto int iterate_partition (grub_disk_t disk,
 			      const grub_partition_t partition);
   
+  struct part_ent
+  {
+    struct part_ent *next;
+    char *name;
+  } *ents = NULL;
+
   int iterate_disk (const char *disk_name)
     {
       grub_device_t dev;
@@ -95,12 +101,29 @@
 	return 0;
       
       if (dev->disk && dev->disk->has_partitions)
-	if (grub_partition_iterate (dev->disk, iterate_partition))
-	  {
-	    grub_device_close (dev);
-	    return 1;
-	  }
+	{
+	  struct part_ent *p;
+	  int ret = 0;
 
+	  (void) grub_partition_iterate (dev->disk, iterate_partition);
+	  grub_device_close (dev);
+
+	  p = ents;
+	  ents = NULL;
+	  while (p != NULL)
+	    {
+	      struct part_ent *next = p->next;
+
+	      if (!ret)
+		ret = hook (p->name);
+	      grub_free (p->name);
+	      grub_free (p);
+	      p = next;
+	    }
+
+	  return ret;
+	}
+
       grub_device_close (dev);
       return 0;
     }
@@ -108,27 +131,32 @@
   int iterate_partition (grub_disk_t disk, const grub_partition_t partition)
     {
       char *partition_name;
-      char *device_name;
-      int ret;
+      struct part_ent *p;
       
       partition_name = grub_partition_get_name (partition);
       if (! partition_name)
 	return 1;
-      
-      device_name = grub_malloc (grub_strlen (disk->name) + 1
-				 + grub_strlen (partition_name) + 1);
-      if (! device_name)
+
+      p = grub_malloc (sizeof (*p));
+      if (!p)
+	return 1;
+
+      p->name = grub_malloc (grub_strlen (disk->name) + 1
+			     + grub_strlen (partition_name) + 1);
+      if (! p->name)
 	{
+	  grub_free (p);
 	  grub_free (partition_name);
 	  return 1;
 	}
 
-      grub_sprintf (device_name, "%s,%s", disk->name, partition_name);
+      grub_sprintf (p->name, "%s,%s", disk->name, partition_name);
       grub_free (partition_name);
 
-      ret = hook (device_name);
-      grub_free (device_name);
-      return ret;
+      p->next = ents;
+      ents = p;
+
+      return 0;
     }
 
   /* Only disk devices are supported at the moment.  */
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.