@@ -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;
@@ -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)
{