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
15
namespace
vfs
{
16
17
auto
GetVfsState
() ->
VfsState
& {
18
static
VfsState
state;
19
return
state;
20
}
21
22
auto
SkipLeadingSlashes
(
const
char
* path) ->
const
char
* {
23
while
(*path ==
'/'
) {
24
++path;
25
}
26
return
path;
27
}
28
29
auto
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
38
auto
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
53
auto
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
63
auto
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
80
auto
Init
() ->
Expected<void>
{
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
96
auto
GetRootDentry
() ->
Dentry
* {
return
GetVfsState
().root_dentry; }
97
98
// 内部接口:设置根 dentry
99
auto
SetRootDentry
(
Dentry
* dentry) ->
void
{
100
GetVfsState
().root_dentry = dentry;
101
}
102
103
// 内部接口:获取挂载表
104
auto
GetMountTableInternal
() ->
MountTable
* {
105
return
GetVfsState
().mount_table;
106
}
107
108
}
// namespace vfs
LockGuard
RAII 风格的锁守卫模板类
Definition
spinlock.hpp:131
vfs::MountTable
挂载表管理器
Definition
mount.hpp:34
Expected
std::expected< T, Error > Expected
std::expected 别名模板
Definition
expected.hpp:365
filesystem.hpp
kernel_log.hpp
mount.hpp
klog::Info
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
Definition
kernel_log.hpp:182
vfs
Definition
device_node.hpp:28
vfs::RemoveChild
auto RemoveChild(Dentry *parent, Dentry *child) -> void
从父 dentry 中移除子 dentry
Definition
vfs.cpp:63
vfs::GetMountTableInternal
auto GetMountTableInternal() -> MountTable *
Definition
vfs.cpp:104
vfs::AddChild
auto AddChild(Dentry *parent, Dentry *child) -> void
添加子 dentry
Definition
vfs.cpp:53
vfs::FindChild
auto FindChild(Dentry *parent, const char *name) -> Dentry *
在 dentry 的子节点中查找指定名称
Definition
vfs.cpp:38
vfs::SkipLeadingSlashes
auto SkipLeadingSlashes(const char *path) -> const char *
跳过路径中的前导斜杠
Definition
vfs.cpp:22
vfs::SetRootDentry
auto SetRootDentry(Dentry *dentry) -> void
Definition
vfs.cpp:99
vfs::GetRootDentry
auto GetRootDentry() -> Dentry *
获取根目录 dentry
Definition
vfs.cpp:96
vfs::GetVfsState
auto GetVfsState() -> VfsState &
Definition
vfs.cpp:17
vfs::CopyPathComponent
auto CopyPathComponent(const char *src, char *dst, size_t dst_size) -> size_t
复制路径组件到缓冲区
Definition
vfs.cpp:29
vfs::GetMountTable
auto GetMountTable() -> MountTable &
获取全局挂载表实例
Definition
mount.cpp:234
vfs::Init
auto Init() -> Expected< void >
VFS 全局初始化
Definition
vfs.cpp:80
strcmp
#define strcmp
Definition
sk_string_test.cpp:16
spinlock.hpp
vfs::Dentry
Dentry — 目录项缓存(路径名 ↔ Inode 的映射)
Definition
vfs.hpp:41
vfs::Dentry::children
Dentry * children
子目录项链表头
Definition
vfs.hpp:49
vfs::Dentry::name
char name[256]
文件/目录名
Definition
vfs.hpp:43
vfs::Dentry::parent
Dentry * parent
父目录项
Definition
vfs.hpp:47
vfs::Dentry::next_sibling
Dentry * next_sibling
兄弟目录项(同一父目录下)
Definition
vfs.hpp:51
vfs::VfsState
VFS 全局状态结构体
Definition
vfs_internal.hpp:17
vfs.hpp
vfs_internal.hpp
src
filesystem
vfs
vfs.cpp
Generated by
1.9.8