[PATCH 043/112] commands: stat: add option for statat

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Jan 3 10:12:03 PST 2024


The stat command is a debugging aid for developing the VFS, so it makes
sense to add support for dirfd, so they can be tested interactively.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 commands/Kconfig |  6 ++++--
 commands/stat.c  | 26 +++++++++++++++++++-------
 fs/fs.c          |  5 ++---
 include/fs.h     |  2 +-
 4 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/commands/Kconfig b/commands/Kconfig
index cd2eab801a84..eddfbee208a1 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -990,12 +990,14 @@ config CMD_STAT
 	help
 	  Display file status
 
-	  Usage: stat [-L] [FILEDIR...]
+	  Usage: stat [-LcC] [FILEDIR...]
 
 	  Display status information about the specified files or directories.
 
 	  Options:
-		  -L	follow symlinks
+		  -L		follow symlinks
+		  -c DIR	lookup file relative to directory DIR
+		  -C DIR	change root to DIR before file lookup
 
 config CMD_MD5SUM
 	tristate
diff --git a/commands/stat.c b/commands/stat.c
index 153eac50f1fa..10662005cdda 100644
--- a/commands/stat.c
+++ b/commands/stat.c
@@ -12,15 +12,23 @@
 
 static int do_stat(int argc, char *argv[])
 {
-	int (*statfn)(const char *, struct stat *) = lstat;
-	int ret, opt, exitcode = 0;
+	int (*statfn)(int dirfd, const char *, struct stat *) = lstatat;
+	int ret, opt, dirfd = AT_FDCWD, extra_flags = 0, exitcode = 0;
 	char **filename;
 	struct stat st;
 
-	while((opt = getopt(argc, argv, "L")) > 0) {
+	while((opt = getopt(argc, argv, "Lc:C:")) > 0) {
 		switch(opt) {
 		case 'L':
-			statfn = stat;
+			statfn = statat;
+			break;
+		case 'C':
+			extra_flags |= O_CHROOT;
+			fallthrough;
+		case 'c':
+			dirfd = open(optarg, O_PATH | extra_flags);
+			if (dirfd < 0)
+				return dirfd;
 			break;
 		default:
 			return COMMAND_ERROR_USAGE;
@@ -31,7 +39,7 @@ static int do_stat(int argc, char *argv[])
 		return COMMAND_ERROR_USAGE;
 
 	for (filename = &argv[optind]; *filename; filename++) {
-		ret = statfn(*filename, &st);
+		ret = statfn(dirfd, *filename, &st);
 
 		if (ret) {
 			printf("%s: %s: %m\n", argv[0], *filename);
@@ -39,9 +47,11 @@ static int do_stat(int argc, char *argv[])
 			continue;
 		}
 
-		stat_print(*filename, &st);
+		stat_print(dirfd, *filename, &st);
 	}
 
+	close(dirfd);
+
 	return exitcode;
 }
 
@@ -51,12 +61,14 @@ BAREBOX_CMD_HELP_TEXT("or directories.")
 BAREBOX_CMD_HELP_TEXT("")
 BAREBOX_CMD_HELP_TEXT("Options:")
 BAREBOX_CMD_HELP_OPT ("-L",  "follow links")
+BAREBOX_CMD_HELP_OPT ("-c DIR",  "lookup file relative to directory DIR")
+BAREBOX_CMD_HELP_OPT ("-C DIR",  "change root to DIR before file lookup")
 BAREBOX_CMD_HELP_END
 
 BAREBOX_CMD_START(stat)
 	.cmd		= do_stat,
 	BAREBOX_CMD_DESC("display file status")
-	BAREBOX_CMD_OPTS("[-L] [FILEDIR...]")
+	BAREBOX_CMD_OPTS("[-LcC] [FILEDIR...]")
 	BAREBOX_CMD_GROUP(CMD_GRP_FILE)
 	BAREBOX_CMD_HELP(cmd_stat_help)
 BAREBOX_CMD_END
diff --git a/fs/fs.c b/fs/fs.c
index 220b4d95cdb1..fc9e56168c9c 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -130,9 +130,8 @@ void cdev_print(const struct cdev *cdev)
 }
 EXPORT_SYMBOL(cdev_print);
 
-void stat_print(const char *filename, const struct stat *st)
+void stat_print(int dirfd, const char *filename, const struct stat *st)
 {
-	int dirfd = AT_FDCWD;
 	struct block_device *bdev = NULL;
 	struct fs_device *fdev;
 	struct cdev *cdev = NULL;
@@ -177,7 +176,7 @@ void stat_print(const char *filename, const struct stat *st)
 		char realname[PATH_MAX] = {};
 		int ret;
 
-		ret = readlink(filename, realname, PATH_MAX - 1);
+		ret = readlinkat(dirfd, filename, realname, PATH_MAX - 1);
 		if (ret)
 			printf(" -> <readlink error %pe>", ERR_PTR(ret));
 		else
diff --git a/include/fs.h b/include/fs.h
index 020761692cad..70903142e89b 100644
--- a/include/fs.h
+++ b/include/fs.h
@@ -147,7 +147,7 @@ int ls(const char *path, ulong flags);
 
 char *mkmodestr(unsigned long mode, char *str);
 
-void stat_print(const char *filename, const struct stat *st);
+void stat_print(int dirfd, const char *filename, const struct stat *st);
 void cdev_print(const struct cdev *cdev);
 
 char *canonicalize_path(int dirfd, const char *pathname);
-- 
2.39.2




More information about the barebox mailing list