public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] [sysfs]: make readlink result shorter when the symlink and its target shared some base sysfs subdirectory
@ 2007-10-31 10:34 Denis Cheng
  2007-10-31 14:34 ` Greg KH
  0 siblings, 1 reply; 5+ messages in thread
From: Denis Cheng @ 2007-10-31 10:34 UTC (permalink / raw)
  To: Greg Kroah-Hartman; +Cc: linux-kernel

this is especially useful after /sys/slab introduced, for example:

$ ls -l /sys/slab/mm_struct
lrwxrwxrwx 1 root root 0 2007-10-31 17:40 /sys/slab/mm_struct -> :0000448

instead of:

$ ls -l /sys/slab/mm_struct
lrwxrwxrwx 1 root root 0 2007-10-31 17:40 /sys/slab/mm_struct -> ../slab/:0000448

Signed-off-by: Denis Cheng <crquan@gmail.com>
---
 fs/sysfs/symlink.c |   38 +++++++++++++++++++++++++++-----------
 1 files changed, 27 insertions(+), 11 deletions(-)

diff --git a/fs/sysfs/symlink.c b/fs/sysfs/symlink.c
index 3eac20c..230a925 100644
--- a/fs/sysfs/symlink.c
+++ b/fs/sysfs/symlink.c
@@ -19,30 +19,41 @@
 
 #include "sysfs.h"
 
-static int object_depth(struct sysfs_dirent *sd)
+static struct sysfs_dirent *object_base(struct sysfs_dirent *parent,
+		struct sysfs_dirent *sd)
+{
+	for (; sd->s_parent; sd = sd->s_parent)
+		if (sd->s_parent == parent)
+			return parent;
+	return NULL;
+}
+
+static int object_depth(struct sysfs_dirent *base, struct sysfs_dirent *sd)
 {
 	int depth = 0;
 
-	for (; sd->s_parent; sd = sd->s_parent)
+	for (; sd->s_parent && sd != base; sd = sd->s_parent)
 		depth++;
 
 	return depth;
 }
 
-static int object_path_length(struct sysfs_dirent * sd)
+static int object_path_length(struct sysfs_dirent *base,
+		struct sysfs_dirent *sd)
 {
 	int length = 1;
 
-	for (; sd->s_parent; sd = sd->s_parent)
+	for (; sd->s_parent && sd != base; sd = sd->s_parent)
 		length += strlen(sd->s_name) + 1;
 
 	return length;
 }
 
-static void fill_object_path(struct sysfs_dirent *sd, char *buffer, int length)
+static void fill_object_path(struct sysfs_dirent *base,
+		struct sysfs_dirent *sd, char *buffer, int length)
 {
 	--length;
-	for (; sd->s_parent; sd = sd->s_parent) {
+	for (; sd->s_parent && sd != base; sd = sd->s_parent) {
 		int cur = strlen(sd->s_name);
 
 		/* back up enough to print this bus id with '/' */
@@ -129,18 +140,23 @@ static int sysfs_get_target_path(struct sysfs_dirent * parent_sd,
 {
 	char * s;
 	int depth, size;
+	struct sysfs_dirent *base;
 
-	depth = object_depth(parent_sd);
-	size = object_path_length(target_sd) + depth * 3 - 1;
+	base = object_base(parent_sd, target_sd);
+	depth = object_depth(base, parent_sd);
+	size = object_path_length(base, target_sd) + depth * 3 - 1;
 	if (size > PATH_MAX)
 		return -ENAMETOOLONG;
 
-	pr_debug("%s: depth = %d, size = %d\n", __FUNCTION__, depth, size);
+	pr_debug("%s: base = %s, depth = %d, size = %d\n",
+			__FUNCTION__,
+			base ? base->s_name : "/sys",
+			depth, size);
 
 	for (s = path; depth--; s += 3)
-		strcpy(s,"../");
+		strcpy(s, "../");
 
-	fill_object_path(target_sd, path, size);
+	fill_object_path(base, target_sd, path, size);
 	pr_debug("%s: path = '%s'\n", __FUNCTION__, path);
 
 	return 0;
-- 
1.5.3.4


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2007-11-01 19:18 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-31 10:34 [PATCH] [sysfs]: make readlink result shorter when the symlink and its target shared some base sysfs subdirectory Denis Cheng
2007-10-31 14:34 ` Greg KH
2007-10-31 18:15   ` rae l
2007-11-01 18:59   ` Kay Sievers
2007-11-01 19:20     ` Kay Sievers

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox