From 79c40986aa14f8ab67f88cb37b7c246bc16e3858 Mon Sep 17 00:00:00 2001 From: snoire Date: Thu, 17 Oct 2024 13:23:13 +0800 Subject: [PATCH] Improve mkfs path handling with basename The mkfs tool previously expected a hardcoded 'user/' prefix, which limited its usability. This commit switches to using basename to handle file paths, allowing mkfs to work with any file path. --- mkfs/mkfs.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c index f39983d7bf..61ee7d3df2 100644 --- a/mkfs/mkfs.c +++ b/mkfs/mkfs.c @@ -4,6 +4,7 @@ #include #include #include +#include #define stat xv6_stat // avoid clash with host struct stat #include "kernel/types.h" @@ -129,12 +130,9 @@ main(int argc, char *argv[]) for(i = 2; i < argc; i++){ // get rid of "user/" - char *shortname; - if(strncmp(argv[i], "user/", 5) == 0) - shortname = argv[i] + 5; - else - shortname = argv[i]; - + char *filepath = strdup(argv[i]); + char *shortname = basename(filepath); + assert(index(shortname, '/') == 0); if((fd = open(argv[i], 0)) < 0) @@ -160,6 +158,7 @@ main(int argc, char *argv[]) iappend(inum, buf, cc); close(fd); + free(filepath); } // fix size of root inode dir