From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 4/6] commands: implement tree command
Date: Thu, 20 Mar 2025 06:20:17 +0100 [thread overview]
Message-ID: <20250320052019.1726331-5-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250320052019.1726331-1-a.fatoum@pengutronix.de>
This is a very simple tree command inspired by busybox. We already have
ls -R for recursive directory listings, but tree uses indentation to
make it easier to see the directory structure.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
commands/Kconfig | 9 +++
commands/Makefile | 1 +
commands/tree.c | 159 ++++++++++++++++++++++++++++++++++++++++++++++
fs/fs.c | 6 ++
include/dirent.h | 1 +
5 files changed, 176 insertions(+)
create mode 100644 commands/tree.c
diff --git a/commands/Kconfig b/commands/Kconfig
index fe459fa862f5..8a7a6e94f025 100644
--- a/commands/Kconfig
+++ b/commands/Kconfig
@@ -1021,6 +1021,15 @@ config CMD_LS
-C column format (opposite of long format)
-R list subdirectories recursively
+config CMD_TREE
+ tristate
+ default y
+ prompt "tree"
+ help
+ list contents of directories in a tree-like format.
+
+ Usage: tree [FILEDIR...]
+
config CMD_STAT
tristate
prompt "stat"
diff --git a/commands/Makefile b/commands/Makefile
index e152e4148b04..fa7de443cc13 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_TREE) += tree.o
obj-$(CONFIG_CMD_STAT) += stat.o
obj-$(CONFIG_CMD_CD) += cd.o
obj-$(CONFIG_CMD_PWD) += pwd.o
diff --git a/commands/tree.c b/commands/tree.c
new file mode 100644
index 000000000000..9b660bb35584
--- /dev/null
+++ b/commands/tree.c
@@ -0,0 +1,159 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// SPDX-FileCopyrightText: © 2022 Roger Knecht <rknecht@pm.me>
+// SPDX-FileCopyrightText: © 2025 Ahmad Fatoum
+
+#include <stdio.h>
+#include <command.h>
+#include <dirent.h>
+#include <fcntl.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <errno.h>
+
+#define TREE_GAP " "
+#define TREE_BAR "| "
+#define TREE_MID "|-- "
+#define TREE_END "`-- "
+
+struct tree_buf {
+ char buf[80];
+};
+
+struct tree_iter {
+ struct tree_buf prefix;
+ unsigned ndirs;
+ unsigned nfiles;
+};
+
+static int tree_prefix_append(struct tree_buf *prefix, int pos, const char *suffix)
+{
+ int strsz = strlen(suffix);
+
+ if (pos + strsz + 1 < sizeof(prefix->buf))
+ memcpy(prefix->buf + pos, suffix, strsz + 1);
+
+ return pos + strsz;
+}
+
+static int tree(int dirfd, const char *path, int prefix_pos, struct tree_iter *iter)
+{
+ DIR *dir;
+ struct dirent *d;
+ int total, ret = 0;
+
+ if (ctrlc())
+ return -EINTR;
+
+ dirfd = openat(dirfd, path, O_DIRECTORY);
+ if (dirfd < 0)
+ return -errno;
+
+ printf("%s\n", path);
+
+ dir = fdopendir(dirfd);
+ if (!dir) {
+ ret = -errno;
+ goto out_close;
+ }
+
+ iter->ndirs++;
+
+ total = countdir(dir);
+ if (total < 0) {
+ ret = -errno;
+ goto out_close;
+ }
+
+ for (int i = 1; (d = readdir(dir)); i++) {
+ struct stat st;
+ int pos;
+
+ if (d->d_name[0] == '.')
+ continue;
+
+ ret = lstatat(dirfd, d->d_name, &st);
+ if (ret)
+ st.st_mode = S_IFREG;
+
+ if (i == total)
+ pos = tree_prefix_append(&iter->prefix, prefix_pos, TREE_END);
+ else
+ pos = tree_prefix_append(&iter->prefix, prefix_pos, TREE_MID);
+
+ printf("%*s", pos, iter->prefix.buf);
+
+ switch (st.st_mode & S_IFMT) {
+ char realname[PATH_MAX];
+ case S_IFLNK:
+ realname[PATH_MAX - 1] = '\0';
+
+ printf("%s", d->d_name);
+ if (readlinkat(dirfd, d->d_name, realname, PATH_MAX - 1) >= 0)
+ printf(" -> %s", realname);
+ printf("\n");
+
+ if (statat(dirfd, d->d_name, &st) || !S_ISDIR(st.st_mode))
+ iter->nfiles++;
+ else
+ iter->ndirs++;
+
+ break;
+ case S_IFDIR:
+ if (i == total)
+ pos = tree_prefix_append(&iter->prefix, prefix_pos, TREE_GAP);
+ else
+ pos = tree_prefix_append(&iter->prefix, prefix_pos, TREE_BAR);
+
+ ret = tree(dirfd, d->d_name, pos, iter);
+ if (ret)
+ goto out_closedir;
+ break;
+ default:
+ printf("%s\n", d->d_name);
+ iter->nfiles++;
+ }
+ }
+
+out_closedir:
+ closedir(dir);
+out_close:
+ close(dirfd);
+ return ret;
+}
+
+static int do_tree(int argc, char *argv[])
+{
+ const char *cwd, **args;
+ struct tree_iter iter;
+ int ret, exitcode = 0;
+
+ if (argc > 1) {
+ args = (const char **)argv + 1;
+ argc--;
+ } else {
+ cwd = getcwd();
+ args = &cwd;
+ }
+
+ iter.nfiles = iter.ndirs = 0;
+
+ for (int i = 0; i < argc; i++) {
+ iter.prefix.buf[0] = 0;
+ ret = tree(AT_FDCWD, args[i], 0, &iter);
+ if (ret) {
+ printf("%s [error opening dir: %pe]\n", args[i], ERR_PTR(ret));
+ exitcode = 1;
+ }
+ }
+
+ printf("\n%u directories, %u files\n", iter.ndirs, iter.nfiles);
+
+ return exitcode;
+}
+
+BAREBOX_CMD_START(tree)
+ .cmd = do_tree,
+ BAREBOX_CMD_DESC("list contents of directories in a tree-like format.")
+ BAREBOX_CMD_OPTS("[FILEDIR...]")
+ BAREBOX_CMD_GROUP(CMD_GRP_FILE)
+BAREBOX_CMD_END
diff --git a/fs/fs.c b/fs/fs.c
index 0c698981227f..3e5eb6dc8479 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -1062,6 +1062,12 @@ int unreaddir(DIR *dir, const struct dirent *d)
}
EXPORT_SYMBOL(unreaddir);
+int countdir(DIR *dir)
+{
+ return list_count_nodes(&dir->entries);
+}
+EXPORT_SYMBOL(countdir);
+
struct dirent *readdir(DIR *dir)
{
struct readdir_entry *entry;
diff --git a/include/dirent.h b/include/dirent.h
index f74541d83d26..b6806aa24799 100644
--- a/include/dirent.h
+++ b/include/dirent.h
@@ -22,6 +22,7 @@ DIR *fdopendir(int fd);
struct dirent *readdir(DIR *dir);
int unreaddir(DIR *dir, const struct dirent *d);
int rewinddir(DIR *dir);
+int countdir(DIR *dir);
int closedir(DIR *dir);
#endif /* __DIRENT_H */
--
2.39.5
next prev parent reply other threads:[~2025-03-20 5:20 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-20 5:20 [PATCH 0/6] fs: implement tree and truncate Ahmad Fatoum
2025-03-20 5:20 ` [PATCH 1/6] fs: use filename_create/filename_lookup instead of open-coding Ahmad Fatoum
2025-03-20 5:20 ` [PATCH 2/6] fs: implement O_DIRECTORY Ahmad Fatoum
2025-03-20 5:20 ` [PATCH 3/6] fs: implement opendir in terms of fdopendir Ahmad Fatoum
2025-03-20 5:20 ` Ahmad Fatoum [this message]
2025-03-20 5:20 ` [PATCH 5/6] commands: add new truncate command Ahmad Fatoum
2025-03-20 5:20 ` [PATCH 6/6] Documentation: devel: add short section on file systems Ahmad Fatoum
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=20250320052019.1726331-5-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