From: Denis Cheng <crquan@gmail.com>
To: Greg Kroah-Hartman <gregkh@suse.de>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] [sysfs]: make readlink result shorter when the symlink and its target shared some base sysfs subdirectory
Date: Wed, 31 Oct 2007 18:34:20 +0800 [thread overview]
Message-ID: <1193826860-6166-1-git-send-email-crquan@gmail.com> (raw)
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
next reply other threads:[~2007-10-31 10:33 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-10-31 10:34 Denis Cheng [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1193826860-6166-1-git-send-email-crquan@gmail.com \
--to=crquan@gmail.com \
--cc=gregkh@suse.de \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome