mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/2] commands: stat: print symlink destination when called without -L
@ 2023-05-31 13:23 Ahmad Fatoum
  2023-05-31 13:23 ` [PATCH 2/2] commands: stat: add basic handling for cdev links Ahmad Fatoum
  2023-06-01  7:06 ` [PATCH 1/2] commands: stat: print symlink destination when called without -L Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2023-05-31 13:23 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

This aligns behavior with usual implementations of stat.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 fs/fs.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/fs/fs.c b/fs/fs.c
index 368458cc54f8..311571ba3088 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -130,8 +130,6 @@ void stat_print(const char *filename, const struct stat *st)
 		case S_IFREG:    type = "regular file"; break;
 	}
 
-	printf("  File: %s\n", filename);
-
 	if (st->st_mode & S_IFCHR) {
 		char *path;
 
@@ -147,6 +145,21 @@ void stat_print(const char *filename, const struct stat *st)
 		}
 	}
 
+	printf("  File: %s", filename);
+
+	if (S_ISLNK(st->st_mode)) {
+		char realname[PATH_MAX] = {};
+		int ret;
+
+		ret = readlink(filename, realname, PATH_MAX - 1);
+		if (ret)
+			printf(" -> <readlink error %pe>", ERR_PTR(ret));
+		else
+			printf(" -> %s", realname);
+	}
+
+	printf("\n");
+
 	printf("  Size: %-20llu", st->st_size);
 	if (bdev)
 		printf("Blocks: %llu\tIO Block: %u\t",
-- 
2.39.2




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

* [PATCH 2/2] commands: stat: add basic handling for cdev links
  2023-05-31 13:23 [PATCH 1/2] commands: stat: print symlink destination when called without -L Ahmad Fatoum
@ 2023-05-31 13:23 ` Ahmad Fatoum
  2023-06-01  7:06 ` [PATCH 1/2] commands: stat: print symlink destination when called without -L Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Ahmad Fatoum @ 2023-05-31 13:23 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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@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




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

* Re: [PATCH 1/2] commands: stat: print symlink destination when called without -L
  2023-05-31 13:23 [PATCH 1/2] commands: stat: print symlink destination when called without -L Ahmad Fatoum
  2023-05-31 13:23 ` [PATCH 2/2] commands: stat: add basic handling for cdev links Ahmad Fatoum
@ 2023-06-01  7:06 ` Sascha Hauer
  1 sibling, 0 replies; 3+ messages in thread
From: Sascha Hauer @ 2023-06-01  7:06 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Wed, May 31, 2023 at 03:23:30PM +0200, Ahmad Fatoum wrote:
> This aligns behavior with usual implementations of stat.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
> ---
>  fs/fs.c | 17 +++++++++++++++--
>  1 file changed, 15 insertions(+), 2 deletions(-)

Applied, thanks

Sascha

> 
> diff --git a/fs/fs.c b/fs/fs.c
> index 368458cc54f8..311571ba3088 100644
> --- a/fs/fs.c
> +++ b/fs/fs.c
> @@ -130,8 +130,6 @@ void stat_print(const char *filename, const struct stat *st)
>  		case S_IFREG:    type = "regular file"; break;
>  	}
>  
> -	printf("  File: %s\n", filename);
> -
>  	if (st->st_mode & S_IFCHR) {
>  		char *path;
>  
> @@ -147,6 +145,21 @@ void stat_print(const char *filename, const struct stat *st)
>  		}
>  	}
>  
> +	printf("  File: %s", filename);
> +
> +	if (S_ISLNK(st->st_mode)) {
> +		char realname[PATH_MAX] = {};
> +		int ret;
> +
> +		ret = readlink(filename, realname, PATH_MAX - 1);
> +		if (ret)
> +			printf(" -> <readlink error %pe>", ERR_PTR(ret));
> +		else
> +			printf(" -> %s", realname);
> +	}
> +
> +	printf("\n");
> +
>  	printf("  Size: %-20llu", st->st_size);
>  	if (bdev)
>  		printf("Blocks: %llu\tIO Block: %u\t",
> -- 
> 2.39.2
> 
> 
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



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

end of thread, other threads:[~2023-06-01  7:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-31 13:23 [PATCH 1/2] commands: stat: print symlink destination when called without -L Ahmad Fatoum
2023-05-31 13:23 ` [PATCH 2/2] commands: stat: add basic handling for cdev links Ahmad Fatoum
2023-06-01  7:06 ` [PATCH 1/2] commands: stat: print symlink destination when called without -L Sascha Hauer

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