Skip to contentSkip to conversation

When dumping from file system show results in sane order

PATCH In review Matthew McClintock 3 participants 5 messages Oct 5, 2010 – Oct 12, 2010 2 messages not in the archive

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 When dumping from file system show results in sane order

Currently, when we run the following

$> dtc -I fs -O dts /proc/device-tree

We get the output in a "reverse order", this patch will reverse that output order and should resemble your initial device more closely

Code copied over from kexec-tools

---
 fstree.c |   29 ++++++++++++++++++++++-------
 1 files changed, 22 insertions(+), 7 deletions(-)
fstree.c +22 -7
index f377453..fd2658d 100644
@@ -23,21 +23,35 @@
 #include <dirent.h>
 #include <sys/stat.h>
 
+static int comparefunc(const void *dentry1, const void *dentry2)
+{
+	const char *str1 = (*(struct dirent * const *)dentry1)->d_name;
+	const char *str2 = (*(struct dirent * const *)dentry2)->d_name;
+
+	if (strchr(str1, '@') && strchr(str2, '@') &&
+		(strlen(str1) > strlen(str2)))
+		return 1;
+
+	return strcmp(str1, str2);
+}
+
 static struct node *read_fstree(const char *dirname)
 {
-	DIR *d;
-	struct dirent *de;
 	struct stat st;
 	struct node *tree;
+	struct dirent **namelist;
+	int numlist, i;
 
-	d = opendir(dirname);
-	if (!d)
-		die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
+	numlist = scandir(dirname, &namelist, 0, comparefunc);
+	if (numlist <= 0)
+		die("unrecoverable error: could not scan \"%s\": %s\n",
+                    dirname, strerror(errno));
 
 	tree = build_node(NULL, NULL);
 
-	while ((de = readdir(d)) != NULL) {
+	for (i = 0; i < numlist; i++) {
 		char *tmpnam;
+		struct dirent *de = namelist[i];
 
 		if (streq(de->d_name, ".")
 		    || streq(de->d_name, ".."))
@@ -72,10 +86,11 @@ static struct node *read_fstree(const char *dirname)
 			add_child(tree, newchild);
 		}
 
+		free(de);
 		free(tmpnam);
 	}
+	free(namelist);
 
-	closedir(d);
 	return tree;
 }
 
Signature · 1 line
1.6.0.6

A message that is not in the archive (sent off-list or not yet received): 1286305641-23388-1-git-send-email-msm-KZfg59tc24xl57MIdRCFDg@public.gmane.org

#2 lore raw
Quoted 10 lines · Matthew McClintock

Currently, when we run the following

$> dtc -I fs -O dts /proc/device-tree

We get the output in a "reverse order", this patch will reverse that output order and should resemble your initial device more closely

Code copied over from kexec-tools

Hi Matthew. David just posted a patch implementing a generic search option when outputting a tree in dts format which would be applicable to more use cases than just sucking up the fstree. Would that do the job for you?

g.

Quoted 71 lines
---
 fstree.c |   29 ++++++++++++++++++++++-------
 1 files changed, 22 insertions(+), 7 deletions(-)
fstree.c +22 -7
index f377453..fd2658d 100644
@@ -23,21 +23,35 @@
 #include <dirent.h>
 #include <sys/stat.h>
 
+static int comparefunc(const void *dentry1, const void *dentry2)
+{
+	const char *str1 = (*(struct dirent * const *)dentry1)->d_name;
+	const char *str2 = (*(struct dirent * const *)dentry2)->d_name;
+
+	if (strchr(str1, '@') && strchr(str2, '@') &&
+		(strlen(str1) > strlen(str2)))
+		return 1;
+
+	return strcmp(str1, str2);
+}
+
 static struct node *read_fstree(const char *dirname)
 {
-	DIR *d;
-	struct dirent *de;
 	struct stat st;
 	struct node *tree;
+	struct dirent **namelist;
+	int numlist, i;
 
-	d = opendir(dirname);
-	if (!d)
-		die("Couldn't opendir() \"%s\": %s\n", dirname, strerror(errno));
+	numlist = scandir(dirname, &namelist, 0, comparefunc);
+	if (numlist <= 0)
+		die("unrecoverable error: could not scan \"%s\": %s\n",
+                    dirname, strerror(errno));
 
 	tree = build_node(NULL, NULL);
 
-	while ((de = readdir(d)) != NULL) {
+	for (i = 0; i < numlist; i++) {
 		char *tmpnam;
+		struct dirent *de = namelist[i];
 
 		if (streq(de->d_name, ".")
 		    || streq(de->d_name, ".."))
@@ -72,10 +86,11 @@ static struct node *read_fstree(const char *dirname)
 			add_child(tree, newchild);
 		}
 
+		free(de);
 		free(tmpnam);
 	}
+	free(namelist);
 
-	closedir(d);
 	return tree;
 }
 
Signature · 7 lines
1.6.0.6


_______________________________________________
devicetree-discuss mailing list
devicetree-discuss-uLR06cmDAlY/[email protected]
https://lists.ozlabs.org/listinfo/devicetree-discuss

A message that is not in the archive (sent off-list or not yet received): 20101011055448.GL23588-MrY2KI0G/OVr83L8+7iqerDks+cytr/[email protected]

#4 lore raw
Quoted 10 lines · Grant Likely
Quoted 9 lines · Matthew McClintock

Currently, when we run the following

$> dtc -I fs -O dts /proc/device-tree

We get the output in a "reverse order", this patch will reverse that output order and should resemble your initial device more closely

Code copied over from kexec-tools

Quoted 8 lines
Quoted 1 line

Hi Matthew. David just posted a patch implementing a generic search option when outputting a tree in dts format which would be applicable to more use cases than just sucking up the fstree. Would that do the job for you?

g.

I just noticed that, I suspect it will work. I will test it out and get back to everyone.

-M

#5 lore raw
In reply to a message not in the archive
Quoted 6 lines · Matthew McClintock

Currently, when we run the following

$> dtc -I fs -O dts /proc/device-tree

We get the output in a "reverse order", this patch will reverse that output order and should resemble your initial device more closely

Reverse order from what? Directory entries are unordered for most intents and purposes - there's no guarantee that readdir() will return entries in a consistent order, and behaviour in practice will vary from one filesystem to another.

Your patch doesn't reverse the order - it sorts the items. If that's what you actually want, then my general dtdiff and sorting patch will serve you better, as Grant suggests.

Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#6 lore raw
Quoted 5 lines · David Gibson
Quoted 4 lines · Matthew McClintock

Currently, when we run the following

$> dtc -I fs -O dts /proc/device-tree

Quoted 7 lines
Quoted 2 lines

We get the output in a "reverse order", this patch will reverse that output order and should resemble your initial device more closely

Reverse order from what? Directory entries are unordered for most intents and purposes - there's no guarantee that readdir() will return entries in a consistent order, and behaviour in practice will vary from one filesystem to another.

Ok, maybe I should word it as now ordered. =)

Quoted 5 lines

Your patch doesn't reverse the order - it sorts the items. If that's what you actually want, then my general dtdiff and sorting patch will serve you better, as Grant suggests.

Yep, I concur your version is better and I would like to see that accepted for my own reasons.

Cheers,
Matthew