[PATCH 11/14] Add the cd command to debugfs

Valerie Aurora val at versity.com
Thu Feb 27 06:16:20 PST 2025


Allow changing the current working directory in debugfs.

Signed-off-by: Valerie Aurora <val at versity.com>
---
 cli/debugfs.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/cli/debugfs.c b/cli/debugfs.c
index ce9b1b4..9816247 100644
--- a/cli/debugfs.c
+++ b/cli/debugfs.c
@@ -1,7 +1,9 @@
 /* SPDX-License-Identifier: GPL-2.0 */
 
+#include <dirent.h>
 #include <signal.h>
 #include <unistd.h>
+#include <sys/stat.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
@@ -34,6 +36,38 @@ struct debugfs_context {
 #define LINE_SIZE (PATH_MAX * 5)
 #define MAX_ARGC ((LINE_SIZE + 1) / 2)
 
+static void cmd_cd(struct debugfs_context *ctx, int argc, char **argv)
+{
+	struct ngnfs_dir_handle hdl;
+	char *name;
+	int ret;
+
+	if (argc != 2) {
+		printf("usage: cd <directory name>\n");
+		return;
+	}
+
+	name = argv[1];
+
+	ret = ngnfs_dir_get_handle(ctx->nfi, ctx->cwd_ino, name, strlen(name), &hdl);
+	if (ret < 0) {
+		if (ret == -ENAMETOOLONG)
+			printf("get_handle: %s name too long\n", name);
+		else if (ret == -ENOENT)
+			printf("get_handle: %s does not exist\n", name);
+		else
+			log("get_handle error: "ENOF"\n", ENOA(-ret));
+		return;
+	}
+
+	if (hdl.dtype != DT_DIR) {
+		printf("not a directory: %s", name);
+		return;
+	}
+
+	ctx->cwd_ino = hdl.ino;
+}
+
 static void cmd_create(struct debugfs_context *ctx, int argc, char **argv)
 {
 	int ret;
@@ -224,6 +258,7 @@ static struct command {
 	char *name;
 	void (*func)(struct debugfs_context *ctx, int argc, char **argv);
 } commands[] = {
+	{ "cd", cmd_cd, },
 	{ "create", cmd_create, },
 	{ "lookup", cmd_lookup, },
 	{ "mkfs", cmd_mkfs, },
-- 
2.48.1




More information about the ngnfs-devel mailing list