From d3cf3f08d1343d6b265516d5a79ce2d197b59d26 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Sun, 4 Jan 2026 14:05:49 +0800 Subject: feat(file-system): implement read and write FAT32 File System and basic shell --- src/fs/fs.cc | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/fs/fs.cc') diff --git a/src/fs/fs.cc b/src/fs/fs.cc index c5a34ee..9e3d50e 100644 --- a/src/fs/fs.cc +++ b/src/fs/fs.cc @@ -149,6 +149,7 @@ namespace VFS Inode *ip, *next; struct Proc *p = myproc(); + // 1. 确定起点 if (*path == '/') { ip = get_root(); @@ -170,6 +171,23 @@ namespace VFS while ((path = skipelem(path, name)) != nullptr) { + if (strcmp(name, ".") == 0) + { + continue; + } + + if (strcmp(name, "..") == 0) + { + Inode *root = get_root(); + bool is_root = (ip->inum == root->inum); + iput(root); + + if (is_root) + { + continue; + } + } + ilock(ip); if (ip->type != T_DIR) @@ -189,4 +207,38 @@ namespace VFS return ip; } + Inode *nameiparent(const char* path, char* name) + { + Inode* ip; + Inode* next; + + if (*path == '/') + ip = get_root(); + else + ip = idup(myproc()->cwd ? myproc()->cwd : get_root()); + + while ((path = skipelem(path, name)) != nullptr) + { + if (*path == 0) + { + return ip; + } + + ilock(ip); + if (ip->type != T_DIR) + { + iunlockput(ip); + return nullptr; + } + + next = ip->lookup(name); + iunlockput(ip); + + ip = next; + if (!ip) + return nullptr; + } + iput(ip); + return nullptr; + } } // namespace VFS -- cgit v1.2.3