[Concept,27/34] cmd: Add VFS file and directory tests to the test command

Message ID 20260403140523.1998228-28-sjg@u-boot.org
State New
Headers
Series Add a virtual filesystem (VFS) layer to U-Boot |

Commit Message

Simon Glass April 3, 2026, 2:04 p.m. UTC
  From: Simon Glass <sjg@chromium.org>

Add -f and -d operators to the test command when VFS is enabled. These
use vfs_stat() to check whether a path is a regular file (-f) or a
directory (-d), complementing the existing -e operator which uses the
legacy filesystem interface.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 cmd/test.c   | 24 ++++++++++++++++++++++++
 test/dm/fs.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
  

Patch

diff --git a/cmd/test.c b/cmd/test.c
index 6ff7adf2074..93e654a8597 100644
--- a/cmd/test.c
+++ b/cmd/test.c
@@ -6,6 +6,8 @@ 
 
 #include <command.h>
 #include <fs_cmd.h>
+#include <fs_common.h>
+#include <vfs.h>
 #include <log.h>
 #include <slre.h>
 #include <vsprintf.h>
@@ -29,6 +31,8 @@ 
 #define OP_INT_GE	15
 #define OP_FILE_EXISTS	16
 #define OP_REGEX	17
+#define OP_VFS_FILE	18
+#define OP_VFS_DIR	19
 
 const struct {
 	int arg;
@@ -52,6 +56,10 @@  const struct {
 	{0, "-z", OP_STR_EMPTY, 2},
 	{0, "-n", OP_STR_NEMPTY, 2},
 	{0, "-e", OP_FILE_EXISTS, 4},
+#if IS_ENABLED(CONFIG_VFS)
+	{0, "-f", OP_VFS_FILE, 2},
+	{0, "-d", OP_VFS_DIR, 2},
+#endif
 #ifdef CONFIG_REGEX
 	{1, "=~", OP_REGEX, 3},
 #endif
@@ -147,6 +155,22 @@  static int do_test(struct cmd_tbl *cmdtp, int flag, int argc,
 		case OP_FILE_EXISTS:
 			expr = file_exists(ap[1], ap[2], ap[3], FS_TYPE_ANY);
 			break;
+#if IS_ENABLED(CONFIG_VFS)
+		case OP_VFS_FILE: {
+			struct fs_dirent dent;
+
+			expr = !vfs_stat(ap[1], &dent) &&
+			       dent.type == FS_DT_REG;
+			break;
+		}
+		case OP_VFS_DIR: {
+			struct fs_dirent dent;
+
+			expr = !vfs_stat(ap[1], &dent) &&
+			       dent.type == FS_DT_DIR;
+			break;
+		}
+#endif
 #ifdef CONFIG_REGEX
 		case OP_REGEX: {
 			struct slre slre;
diff --git a/test/dm/fs.c b/test/dm/fs.c
index 60a0408dd99..e0c7f37e155 100644
--- a/test/dm/fs.c
+++ b/test/dm/fs.c
@@ -1020,6 +1020,37 @@  static int dm_test_vfs_stat(struct unit_test_state *uts)
 }
 DM_TEST(dm_test_vfs_stat, UTF_SCAN_FDT);
 
+/* Test the -f and -d operators in the test command */
+static int dm_test_vfs_test_fd(struct unit_test_state *uts)
+{
+	ut_assertok(vfs_init());
+
+	ut_assertok(run_command("mount hostfs /host", 0));
+	ut_assert_console_end();
+
+	/* -f should succeed for a regular file */
+	ut_assertok(run_command("test -f /host/README", 0));
+
+	/* -f should fail for a directory */
+	ut_asserteq(1, run_command("test -f /host/cmd", 0));
+
+	/* -d should succeed for a directory */
+	ut_assertok(run_command("test -d /host/cmd", 0));
+
+	/* -d should fail for a regular file */
+	ut_asserteq(1, run_command("test -d /host/README", 0));
+
+	/* Both should fail for non-existent paths */
+	ut_asserteq(1, run_command("test -f /host/no-such-file", 0));
+	ut_asserteq(1, run_command("test -d /host/no-such-dir", 0));
+
+	ut_assertok(run_command("umount /host", 0));
+	ut_assert_console_end();
+
+	return 0;
+}
+DM_TEST(dm_test_vfs_test_fd, UTF_SCAN_FDT);
+
 /* Test the cat command via VFS */
 static int dm_test_vfs_cat(struct unit_test_state *uts)
 {