mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH] commands: add new stat command
Date: Mon, 10 Oct 2022 08:43:58 +0200	[thread overview]
Message-ID: <20221010064358.2119629-1-a.fatoum@pengutronix.de> (raw)

We have a couple of commands to help with debugging the VFS:
ll, devinfo, devlookup, but we lack a command that can just dump all
information we have in a struct stat or struct cdev. Add stat as such a
command. For most uses, it's not needed, but it can come in handy for
development. The stat_print and cdev_print functions underlying it are
exported, so they can called for debugging purposes.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 commands/Kconfig  |  14 +++++++
 commands/Makefile |   1 +
 commands/stat.c   |  62 ++++++++++++++++++++++++++++
 fs/fs.c           | 100 +++++++++++++++++++++++++++++++++++++++++++++-
 include/fs.h      |   3 ++
 5 files changed, 179 insertions(+), 1 deletion(-)
 create mode 100644 commands/stat.c

diff --git a/commands/Kconfig b/commands/Kconfig
index 3e21dc4c0500..a561a474b404 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -939,6 +939,20 @@ config CMD_LS
 		  -C	column format (opposite of long format)
 		  -R	list subdirectories recursively
 
+config CMD_STAT
+	tristate
+	prompt "stat"
+	select PRINTF_UUID
+	help
+	  Display file status
+
+	  Usage: stat [-L] [FILEDIR...]
+
+	  Display status information about the specified files or directories.
+
+	  Options:
+		  -L	follow symlinks
+
 config CMD_MD5SUM
 	tristate
 	select COMPILE_HASH
diff --git a/commands/Makefile b/commands/Makefile
index 0aae8893d696..7e304e40771b 100644
--- a/commands/Makefile
+++ b/commands/Makefile
@@ -26,6 +26,7 @@ obj-$(CONFIG_CMD_POWEROFF)	+= poweroff.o
 obj-$(CONFIG_CMD_GO)		+= go.o
 obj-$(CONFIG_CMD_PARTITION)	+= partition.o
 obj-$(CONFIG_CMD_LS)		+= ls.o
+obj-$(CONFIG_CMD_STAT)		+= stat.o
 obj-$(CONFIG_CMD_CD)		+= cd.o
 obj-$(CONFIG_CMD_PWD)		+= pwd.o
 obj-$(CONFIG_CMD_MKDIR)		+= mkdir.o
diff --git a/commands/stat.c b/commands/stat.c
new file mode 100644
index 000000000000..53c22c53aef1
--- /dev/null
+++ b/commands/stat.c
@@ -0,0 +1,62 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// SPDX-FileCopyrightText: © 2022 Ahmad Fatoum, Pengutronix
+
+#include <common.h>
+#include <command.h>
+#include <fs.h>
+#include <linux/stat.h>
+#include <errno.h>
+#include <malloc.h>
+#include <getopt.h>
+#include <stringlist.h>
+
+static int do_stat(int argc, char *argv[])
+{
+	int (*statfn)(const char *, struct stat *) = stat;
+	int ret, opt, exitcode = 0;
+	char **filename;
+	struct stat st;
+
+	while((opt = getopt(argc, argv, "L")) > 0) {
+		switch(opt) {
+		case 'L':
+			statfn = lstat;
+			break;
+		default:
+			return COMMAND_ERROR_USAGE;
+		}
+	}
+
+	if (optind == argc)
+		return COMMAND_ERROR_USAGE;
+
+	for (filename = &argv[optind]; *filename; filename++) {
+		ret = statfn(*filename, &st);
+
+		if (ret) {
+			printf("%s: %s: %m\n", argv[0], *filename);
+			exitcode = COMMAND_ERROR;
+			continue;
+		}
+
+		stat_print(*filename, &st);
+	}
+
+	return exitcode;
+}
+
+BAREBOX_CMD_HELP_START(stat)
+BAREBOX_CMD_HELP_TEXT("Display status information about the specified files")
+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_END
+
+BAREBOX_CMD_START(stat)
+	.cmd		= do_stat,
+	BAREBOX_CMD_DESC("display file status")
+	BAREBOX_CMD_OPTS("[-L] [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 6583e0a6e9b3..0dcc28669e8c 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -67,6 +67,105 @@ char *mkmodestr(unsigned long mode, char *str)
 }
 EXPORT_SYMBOL(mkmodestr);
 
+void cdev_print(const struct cdev *cdev)
+{
+	bool uuid_set;
+
+	if (cdev->dev || cdev->master || cdev->partname) {
+		printf("Origin: %s", dev_name(cdev->dev) ?: "None");
+		if (cdev->master)
+			printf("\tMaster: %s", cdev->master->name);
+		if (cdev->partname)
+			printf("\tPartition: %s", cdev->partname);
+		printf("\n");
+	}
+	printf("Ocount: %d\tFlags: 0x%02x", cdev->open, cdev->flags);
+	if (cdev->flags) {
+		printf(" (");
+		if (cdev->flags & DEVFS_IS_CHARACTER_DEV)
+			printf(" cdev");
+		if (cdev->flags & DEVFS_PARTITION_FIXED)
+			printf(" fixed-partition");
+		if (cdev->flags & DEVFS_PARTITION_READONLY)
+			printf(" readonly-partition");
+		if (cdev->flags & DEVFS_PARTITION_FROM_TABLE)
+			printf(" table-partition");
+		if (cdev->flags & DEVFS_IS_MCI_MAIN_PART_DEV)
+			printf(" mci-main-partition");
+		if (cdev->mtd)
+			printf(" mtd");
+		printf(" )");
+	}
+	printf("\n");
+
+	uuid_set = memchr_inv(cdev->uuid, 0x00 ,sizeof(cdev->uuid));
+	if (cdev->filetype || cdev->dos_partition_type || uuid_set) {
+		if (cdev->filetype)
+			printf("Filetype: %s\t", file_type_to_string(cdev->filetype));
+		if (cdev->dos_partition_type)
+			printf("DOS parttype: 0x%02x\t", cdev->dos_partition_type);
+		if (uuid_set)
+			printf("UUID: %pUl", cdev->uuid);
+		printf("\n");
+	}
+}
+EXPORT_SYMBOL(cdev_print);
+
+static struct fs_device_d *get_fsdevice_by_path(const char *path);
+
+void stat_print(const char *filename, const struct stat *st)
+{
+	struct block_device *bdev = NULL;
+	struct fs_device_d *fdev;
+	struct cdev *cdev = NULL;
+	const char *type = NULL;
+	char modestr[11];
+
+	mkmodestr(st->st_mode, modestr);
+
+	switch (st->st_mode & S_IFMT) {
+		case S_IFDIR:    type = "directory"; break;
+		case S_IFBLK:    type = "block special file"; break;
+		case S_IFCHR:    type = "character special file"; break;
+		case S_IFIFO:    type = "fifo"; break;
+		case S_IFLNK:    type = "symbolic link"; break;
+		case S_IFSOCK:   type = "socket"; break;
+		case S_IFREG:    type = "regular file"; break;
+	}
+
+	printf("  File: %s\n", filename);
+
+	if (st->st_mode & S_IFCHR) {
+		const char *devicefile;
+
+		devicefile = devpath_to_name(filename);
+
+		cdev = cdev_by_name(devicefile);
+		if (cdev)
+			bdev = cdev_get_block_device(cdev);
+	}
+
+	printf("  Size: %-20llu", st->st_size);
+	if (bdev)
+		printf("Blocks: %llu\tIO Block: %u\t",
+		       (u64)bdev->num_blocks, 1 << bdev->blockbits);
+
+	if (type)
+		printf("  %s", type);
+
+	fdev = get_fsdevice_by_path(filename);
+
+	printf("\nDevice: %s\tInode: %lu\tLinks: %u\n",
+	       fdev ? dev_name(&fdev->dev) : "<unknown>",
+	       st->st_ino, st->st_nlink);
+	printf("Access: (%04o/%s)\tUid: (%u)\tGid: (%u)\n",
+	       st->st_mode & 07777, modestr, st->st_uid, st->st_gid);
+
+	if (cdev)
+		cdev_print(cdev);
+}
+EXPORT_SYMBOL(stat_print);
+
 static char *cwd;
 static struct dentry *cwd_dentry;
 static struct vfsmount *cwd_mnt;
@@ -91,7 +190,6 @@ postcore_initcall(init_fs);
 
 struct filename;
 
-static struct fs_device_d *get_fsdevice_by_path(const char *path);
 static int filename_lookup(struct filename *name, unsigned flags,
 			   struct path *path);;
 static struct filename *getname(const char *filename);
diff --git a/include/fs.h b/include/fs.h
index 894cae3e4c2f..e82b3d8ceb6a 100644
--- a/include/fs.h
+++ b/include/fs.h
@@ -147,6 +147,9 @@ 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 cdev_print(const struct cdev *cdev);
+
 char *canonicalize_path(const char *pathname);
 
 char *get_mounted_path(const char *path);
-- 
2.30.2




             reply	other threads:[~2022-10-10  6:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-10  6:43 Ahmad Fatoum [this message]
2022-10-13  7:15 ` 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=20221010064358.2119629-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --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