mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
To: barebox@lists.infradead.org
Subject: [PATCH 1/3] devfs: add symlink support
Date: Fri, 18 Oct 2013 18:33:10 +0200	[thread overview]
Message-ID: <1382113992-9082-1-git-send-email-plagnioj@jcrosoft.com> (raw)
In-Reply-To: <20131018163147.GZ32444@ns203013.ovh.net>

this will allow have symlink for any device such as named partition

Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 fs/devfs-core.c  | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 fs/devfs.c       | 42 ++++++++++++++++++++++++++++++++++++++++--
 include/driver.h |  4 ++++
 3 files changed, 88 insertions(+), 3 deletions(-)

diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index a92d434..0e1ac9a 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -45,13 +45,56 @@ int devfs_partition_complete(struct string_list *sl, char *instr)
 }
 #endif
 
+struct cdev *devfs_add_symlink_index(const char *filename, struct cdev *src)
+{
+	struct cdev *cdev;
+	int ret;
+
+	if (!filename || !src)
+		return ERR_PTR(-EIO);
+
+	ret = cdev_find_free_index(filename);
+	if (ret == -1)
+		return ERR_PTR(-ENOMEM);
+
+	cdev = xzalloc(sizeof(*cdev));
+	cdev->name = asprintf("%s%d", filename, ret);
+	cdev->symlink = src;
+	ret = devfs_create(cdev);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return cdev;
+}
+
+struct cdev *devfs_add_symlink(const char *filename, struct cdev *src)
+{
+	struct cdev *cdev;
+	int ret;
+
+	if (!filename || !src)
+		return ERR_PTR(-EIO);
+
+	cdev = xzalloc(sizeof(*cdev));
+	cdev->name = xstrdup(filename);
+	cdev->symlink = src;
+	ret = devfs_create(cdev);
+	if (ret)
+		return ERR_PTR(ret);
+
+	return cdev;
+}
+
 struct cdev *cdev_by_name(const char *filename)
 {
 	struct cdev *cdev;
 
 	list_for_each_entry(cdev, &cdev_list, list) {
-		if (!strcmp(cdev->name, filename))
+		if (!strcmp(cdev->name, filename)) {
+			if (cdev->symlink)
+				return cdev->symlink;
 			return cdev;
+		}
 	}
 	return NULL;
 }
diff --git a/fs/devfs.c b/fs/devfs.c
index f089c6f..f4e1e9c 100644
--- a/fs/devfs.c
+++ b/fs/devfs.c
@@ -35,6 +35,17 @@
 
 extern struct list_head cdev_list;
 
+static struct cdev *devfs_by_name(const char *filename)
+{
+	struct cdev *cdev;
+
+	list_for_each_entry(cdev, &cdev_list, list) {
+		if (!strcmp(cdev->name, filename))
+			return cdev;
+	}
+	return NULL;
+}
+
 static int devfs_read(struct device_d *_dev, FILE *f, void *buf, size_t size)
 {
 	struct cdev *cdev = f->inode;
@@ -113,7 +124,7 @@ static int devfs_open(struct device_d *_dev, FILE *f, const char *filename)
 	struct cdev *cdev;
 	int ret;
 
-	cdev = cdev_by_name(filename + 1);
+	cdev = devfs_by_name(filename + 1);
 
 	if (!cdev)
 		return -ENOENT;
@@ -212,10 +223,15 @@ static int devfs_stat(struct device_d *_dev, const char *filename, struct stat *
 {
 	struct cdev *cdev;
 
-	cdev = cdev_by_name(filename + 1);
+	cdev = devfs_by_name(filename + 1);
 	if (!cdev)
 		return -ENOENT;
 
+	if (cdev->symlink) {
+		s->st_mode = S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO;
+		return 0;
+	}
+
 	s->st_mode = S_IFCHR;
 	s->st_size = cdev->size;
 	if (cdev->ops->write)
@@ -235,6 +251,27 @@ static void devfs_delete(struct device_d *dev)
 {
 }
 
+static int devfs_readlink(struct device_d *dev, const char *filename,
+			char *buf, size_t bufsiz)
+{
+	struct cdev *cdev;
+	struct cdev *symlink;
+	int len;
+
+	cdev = devfs_by_name(filename + 1);
+
+	if (!cdev || !cdev->symlink)
+		return -ENOENT;
+
+	symlink = cdev->symlink;
+
+	len = min(bufsiz, strlen(symlink->name));
+
+	memcpy(buf, symlink->name, len);
+
+	return 0;
+}
+
 static struct fs_driver_d devfs_driver = {
 	.read      = devfs_read,
 	.write     = devfs_write,
@@ -251,6 +288,7 @@ static struct fs_driver_d devfs_driver = {
 	.erase     = devfs_erase,
 	.protect   = devfs_protect,
 	.memmap    = devfs_memmap,
+	.readlink  = devfs_readlink,
 	.flags     = FS_DRIVER_NO_DEV,
 	.drv = {
 		.probe  = devfs_probe,
diff --git a/include/driver.h b/include/driver.h
index 7f0532e..2d42597 100644
--- a/include/driver.h
+++ b/include/driver.h
@@ -457,10 +457,14 @@ struct cdev {
 	int open;
 	struct mtd_info *mtd;
 	u8 dos_partition_type;
+
+	struct cdev *symlink;
 };
 
 int devfs_create(struct cdev *);
 int devfs_remove(struct cdev *);
+struct cdev *devfs_add_symlink(const char *filename, struct cdev *src);
+struct cdev *devfs_add_symlink_index(const char *filename, struct cdev *src);
 int cdev_find_free_index(const char *);
 struct cdev *device_find_partition(struct device_d *dev, const char *name);
 struct cdev *cdev_by_name(const char *filename);
-- 
1.8.4.rc3


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2013-10-18 16:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-10-18 16:31 [PATCH 0/3] " Jean-Christophe PLAGNIOL-VILLARD
2013-10-18 16:33 ` Jean-Christophe PLAGNIOL-VILLARD [this message]
2013-10-18 16:33   ` [PATCH 2/3] partition: use symlink for named partition Jean-Christophe PLAGNIOL-VILLARD
2013-10-18 16:33   ` [PATCH 3/3] USB: storage: register disks as usbdiskx symlink Jean-Christophe PLAGNIOL-VILLARD
2013-10-21  8:42   ` [PATCH 1/3] devfs: add symlink support Sascha Hauer

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=1382113992-9082-1-git-send-email-plagnioj@jcrosoft.com \
    --to=plagnioj@jcrosoft.com \
    --cc=barebox@lists.infradead.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