[PATCH 2/2] commands: stat: add basic handling for cdev links

Ahmad Fatoum a.fatoum at pengutronix.de
Wed May 31 06:23:31 PDT 2023


cdev links are not symlinks and cdev_by_name will always resolve them.
As the barebox stat command is a convenience for VFS developers, it's
useful to have the command identify links, so let's teach it just that.

There's no behavior difference between specifying -L and not. This
should be rather achieved by removing the concept of cdev links and
using symlinks instead, but that's some refactoring for another time.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 fs/devfs-core.c |  3 +++
 fs/fs.c         | 14 +++++++++++---
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/fs/devfs-core.c b/fs/devfs-core.c
index fbcf68e81597..68a41ed20dd1 100644
--- a/fs/devfs-core.c
+++ b/fs/devfs-core.c
@@ -49,6 +49,9 @@ int devfs_partition_complete(struct string_list *sl, char *instr)
 
 struct cdev *cdev_readlink(struct cdev *cdev)
 {
+	if (!cdev)
+		return NULL;
+
 	if (cdev->link)
 		cdev = cdev->link;
 
diff --git a/fs/fs.c b/fs/fs.c
index 311571ba3088..68e6bf5735f0 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -115,7 +115,8 @@ void stat_print(const char *filename, const struct stat *st)
 	struct block_device *bdev = NULL;
 	struct fs_device *fdev;
 	struct cdev *cdev = NULL;
-	const char *type = NULL;
+	const char *type = NULL, *typeprefix = "";
+	bool is_cdev_link = false;
 	char modestr[11];
 
 	mkmodestr(st->st_mode, modestr);
@@ -136,8 +137,12 @@ void stat_print(const char *filename, const struct stat *st)
 		path = canonicalize_path(filename);
 		if (path) {
 			const char *devicefile = devpath_to_name(path);
+			struct cdev *lcdev;
 
-			cdev = cdev_by_name(devicefile);
+			lcdev = lcdev_by_name(devicefile);
+			cdev = cdev_readlink(lcdev);
+			if (cdev != lcdev)
+				is_cdev_link = true;
 			if (cdev)
 				bdev = cdev_get_block_device(cdev);
 
@@ -156,6 +161,9 @@ void stat_print(const char *filename, const struct stat *st)
 			printf(" -> <readlink error %pe>", ERR_PTR(ret));
 		else
 			printf(" -> %s", realname);
+	} else if (is_cdev_link) {
+		printf(" ~> %s", cdev->name);
+		typeprefix = "cdev link to ";
 	}
 
 	printf("\n");
@@ -166,7 +174,7 @@ void stat_print(const char *filename, const struct stat *st)
 		       (u64)bdev->num_blocks, 1 << bdev->blockbits);
 
 	if (type)
-		printf("  %s", type);
+		printf("  %s%s", typeprefix, type);
 
 	fdev = get_fsdevice_by_path(filename);
 
-- 
2.39.2




More information about the barebox mailing list