SimpleKernel 1.17.0
Loading...
Searching...
No Matches
vfs.cpp
Go to the documentation of this file.
1
5#include "vfs.hpp"
6
7#include "filesystem.hpp"
8#include "kernel_log.hpp"
9#include "kstd_cstdio"
10#include "kstd_cstring"
11#include "mount.hpp"
12#include "spinlock.hpp"
13#include "vfs_internal.hpp"
14
15namespace vfs {
16
18 static VfsState state;
19 return state;
20}
21
22auto SkipLeadingSlashes(const char* path) -> const char* {
23 while (*path == '/') {
24 ++path;
25 }
26 return path;
27}
28
29auto CopyPathComponent(const char* src, char* dst, size_t dst_size) -> size_t {
30 size_t i = 0;
31 while (*src != '\0' && *src != '/' && i < dst_size - 1) {
32 dst[i++] = *src++;
33 }
34 dst[i] = '\0';
35 return i;
36}
37
38auto FindChild(Dentry* parent, const char* name) -> Dentry* {
39 if (parent == nullptr || name == nullptr) {
40 return nullptr;
41 }
42
43 Dentry* child = parent->children;
44 while (child != nullptr) {
45 if (strcmp(child->name, name) == 0) {
46 return child;
47 }
48 child = child->next_sibling;
49 }
50 return nullptr;
51}
52
53auto AddChild(Dentry* parent, Dentry* child) -> void {
54 if (parent == nullptr || child == nullptr) {
55 return;
56 }
57
58 child->parent = parent;
59 child->next_sibling = parent->children;
60 parent->children = child;
61}
62
63auto RemoveChild(Dentry* parent, Dentry* child) -> void {
64 if (parent == nullptr || child == nullptr) {
65 return;
66 }
67
68 Dentry** current = &parent->children;
69 while (*current != nullptr) {
70 if (*current == child) {
71 *current = child->next_sibling;
72 child->parent = nullptr;
73 child->next_sibling = nullptr;
74 return;
75 }
76 current = &(*current)->next_sibling;
77 }
78}
79
81 if (GetVfsState().initialized) {
82 return {};
83 }
84
85 LockGuard<SpinLock> guard(GetVfsState().vfs_lock_);
86 klog::Info("VFS: initializing...");
87
88 // 初始化挂载表(使用全局单例,与 GetMountTable() 统一)
89 GetVfsState().mount_table = &GetMountTable();
90
91 GetVfsState().initialized = true;
92 klog::Info("VFS: initialization complete");
93 return {};
94}
95
96auto GetRootDentry() -> Dentry* { return GetVfsState().root_dentry; }
97
98// 内部接口:设置根 dentry
99auto SetRootDentry(Dentry* dentry) -> void {
100 GetVfsState().root_dentry = dentry;
101}
102
103// 内部接口:获取挂载表
105 return GetVfsState().mount_table;
106}
107
108} // namespace vfs
RAII 风格的锁守卫模板类
Definition spinlock.hpp:131
挂载表管理器
Definition mount.hpp:34
std::expected< T, Error > Expected
std::expected 别名模板
Definition expected.hpp:365
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
auto RemoveChild(Dentry *parent, Dentry *child) -> void
从父 dentry 中移除子 dentry
Definition vfs.cpp:63
auto GetMountTableInternal() -> MountTable *
Definition vfs.cpp:104
auto AddChild(Dentry *parent, Dentry *child) -> void
添加子 dentry
Definition vfs.cpp:53
auto FindChild(Dentry *parent, const char *name) -> Dentry *
在 dentry 的子节点中查找指定名称
Definition vfs.cpp:38
auto SkipLeadingSlashes(const char *path) -> const char *
跳过路径中的前导斜杠
Definition vfs.cpp:22
auto SetRootDentry(Dentry *dentry) -> void
Definition vfs.cpp:99
auto GetRootDentry() -> Dentry *
获取根目录 dentry
Definition vfs.cpp:96
auto GetVfsState() -> VfsState &
Definition vfs.cpp:17
auto CopyPathComponent(const char *src, char *dst, size_t dst_size) -> size_t
复制路径组件到缓冲区
Definition vfs.cpp:29
auto GetMountTable() -> MountTable &
获取全局挂载表实例
Definition mount.cpp:234
auto Init() -> Expected< void >
VFS 全局初始化
Definition vfs.cpp:80
#define strcmp
Dentry — 目录项缓存(路径名 ↔ Inode 的映射)
Definition vfs.hpp:41
Dentry * children
子目录项链表头
Definition vfs.hpp:49
char name[256]
文件/目录名
Definition vfs.hpp:43
Dentry * parent
父目录项
Definition vfs.hpp:47
Dentry * next_sibling
兄弟目录项(同一父目录下)
Definition vfs.hpp:51
VFS 全局状态结构体