diff options
author | Ahmad Fatoum <a.fatoum@pengutronix.de> | 2022-10-26 08:38:19 +0200 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2022-10-27 09:43:57 +0200 |
commit | e6a09861b9a76b5527aa1310cff3d4e772b292b8 (patch) | |
tree | 4aec60947a1f1c72342a17c970d5435f080d40f6 /commands | |
parent | f9f0c098048f8fa76f9f4945b0fe16e9333530d5 (diff) | |
download | barebox-e6a09861b9a76b5527aa1310cff3d4e772b292b8.tar.gz |
commands: add new stat command
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>
Link: https://lore.barebox.org/20221026063819.2355568-1-a.fatoum@pengutronix.de
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'commands')
-rw-r--r-- | commands/Kconfig | 14 | ||||
-rw-r--r-- | commands/Makefile | 1 | ||||
-rw-r--r-- | commands/stat.c | 62 |
3 files changed, 77 insertions, 0 deletions
diff --git a/commands/Kconfig b/commands/Kconfig index 9894ecb9aa..2ce990b561 100644 --- a/commands/Kconfig +++ b/commands/Kconfig @@ -947,6 +947,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 068fbb32da..68d0d05365 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 0000000000..153eac50f1 --- /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 *) = lstat; + int ret, opt, exitcode = 0; + char **filename; + struct stat st; + + while((opt = getopt(argc, argv, "L")) > 0) { + switch(opt) { + case 'L': + statfn = stat; + 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 |