libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
ramfs.cpp
1#include "ramfs.hpp"
2
3using namespace LibXR;
4
5RamFS::RamFS(const char *name)
6 : root_(CreateDir(name)), bin_(CreateDir("bin")), dev_(CreateDir("dev"))
7{
10}
11
12int RamFS::CompareStr(const char *const &a, const char *const &b) { return strcmp(a, b); }
13
14int RamFS::FileNode::Run(int argc, char **argv)
15{
16 ASSERT(type == FileType::EXEC);
17 return exec(arg, argc, argv);
18}
19
20RamFS::Device::Device(const char *name, const ReadPort &read_port,
21 const WritePort &write_port)
22{
23 char *name_buff = new char[strlen(name) + 1];
24 strcpy(name_buff, name);
25 data_.name = name_buff;
26 data_.type = FsNodeType::DEVICE;
27
28 UNUSED(read_port);
29 UNUSED(write_port);
30}
31
33{
34 auto ans = (*this)->rbt.Search<FsNode>(name);
35 if (ans && ans->data_.type == FsNodeType::FILE)
36 {
37 return reinterpret_cast<File *>(ans);
38 }
39 else
40 {
41 return nullptr;
42 }
43}
44
46{
47 auto ans = FindFile(name);
48
49 std::function<ErrorCode(RBTree<const char *>::Node<FsNode> &)> fun;
50
51 fun = [&](RBTree<const char *>::Node<FsNode> &item) -> ErrorCode
52 {
53 FsNode &node = item;
54 if (node.type == FsNodeType::DIR)
55 {
56 Dir *dir = reinterpret_cast<Dir *>(&item);
57
58 ans = dir->FindFile(name);
59 if (ans)
60 {
61 return ErrorCode::FAILED;
62 }
63
65 { return fun(child); });
66
67 return ans ? ErrorCode::FAILED : ErrorCode::OK;
68 }
69 return ErrorCode::OK;
70 };
71
72 if (ans == nullptr)
73 {
74 data_.rbt.Foreach<FsNode>([&](RBTree<const char *>::Node<FsNode> &item)
75 { return fun(item); });
76 }
77
78 return ans;
79}
80
82{
83 if (name[0] == '.' && name[1] == '\0')
84 {
85 return this;
86 }
87
88 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
89 {
90 return reinterpret_cast<Dir *>(data_.parent);
91 }
92
93 auto ans = (*this)->rbt.Search<RamFS::FsNode>(name);
94
95 if (ans && (*ans)->type == FsNodeType::DIR)
96 {
97 return reinterpret_cast<Dir *>(ans);
98 }
99 else
100 {
101 return nullptr;
102 }
103}
104
106{
107 auto ans = FindDir(name);
108
109 std::function<ErrorCode(RBTree<const char *>::Node<FsNode> &)> fun;
110
111 fun = [&](RBTree<const char *>::Node<FsNode> &item) -> ErrorCode
112 {
113 FsNode &node = item;
114 if (node.type == FsNodeType::DIR)
115 {
116 Dir *dir = reinterpret_cast<Dir *>(&item);
117 if (strcmp(dir->data_.name, name) == 0)
118 {
119 ans = dir;
120 return ErrorCode::OK;
121 }
122 else
123 {
125 { return fun(child); });
126
127 return ans ? ErrorCode::FAILED : ErrorCode::OK;
128 }
129 }
130 return ErrorCode::OK;
131 };
132
133 if (ans == nullptr)
134 {
135 data_.rbt.Foreach<FsNode>([&](RBTree<const char *>::Node<FsNode> &item)
136 { return fun(item); });
137 }
138
139 return ans;
140}
141
143{
144 auto ans = FindDevice(name);
145
146 std::function<ErrorCode(RBTree<const char *>::Node<FsNode> &)> fun;
147
148 fun = [&](RBTree<const char *>::Node<FsNode> &item) -> ErrorCode
149 {
150 FsNode &node = item;
151 if (node.type == FsNodeType::DIR)
152 {
153 Dir *dir = reinterpret_cast<Dir *>(&item);
154
155 ans = dir->FindDevice(name);
156 if (ans)
157 {
158 return ErrorCode::FAILED;
159 }
160
162 { return fun(child); });
163
164 return ans ? ErrorCode::FAILED : ErrorCode::OK;
165 }
166 return ErrorCode::OK;
167 };
168
169 if (ans == nullptr)
170 {
171 data_.rbt.Foreach<FsNode>([&](RBTree<const char *>::Node<FsNode> &item)
172 { return fun(item); });
173 }
174 return ans;
175}
176
185{
186 auto ans = (*this)->rbt.Search<FsNode>(name);
187 if (ans && ans->data_.type == FsNodeType::DEVICE)
188 {
189 return reinterpret_cast<Device *>(ans);
190 }
191 else
192 {
193 return nullptr;
194 }
195}
红黑树的泛型数据节点,继承自 BaseNode (Generic data node for Red-Black Tree, inheriting from BaseNode).
Definition rbt.hpp:64
Data data_
存储的数据 (Stored data).
Definition rbt.hpp:99
红黑树实现,支持泛型键和值,并提供线程安全操作 (Red-Black Tree implementation supporting generic keys and values with thread...
Definition rbt.hpp:24
ErrorCode Foreach(Func func)
遍历红黑树并执行用户提供的操作 (Traverse the Red-Black Tree and apply a user-defined function).
Definition rbt.hpp:273
设备类,继承自红黑树节点 DeviceNode Device class inheriting from Red-Black tree node DeviceNode
Definition ramfs.hpp:160
Device(const char *name, const ReadPort &read_port=ReadPort(), const WritePort &write_port=WritePort())
设备构造函数 Device constructor
Definition ramfs.cpp:20
目录类,继承自 RBTree 节点,用于管理文件、子目录和设备 Directory class, inheriting from RBTree node, used for managing files...
Definition ramfs.hpp:229
Device * FindDeviceRev(const char *name)
递归查找设备 Recursively searches for a device
Definition ramfs.cpp:142
File * FindFile(const char *name)
查找当前目录中的文件 Finds a file in the current directory
Definition ramfs.cpp:32
Dir * FindDir(const char *name)
查找当前目录中的子目录 Finds a subdirectory in the current directory
Definition ramfs.cpp:81
Device * FindDevice(const char *name)
在当前目录中查找设备 Finds a device in the current directory
Definition ramfs.cpp:184
File * FindFileRev(const char *name)
递归查找文件 Recursively searches for a file
Definition ramfs.cpp:45
void Add(File &file)
添加文件到当前目录 Adds a file to the current directory
Definition ramfs.hpp:236
Dir * FindDirRev(const char *name)
递归查找子目录 Recursively searches for a subdirectory
Definition ramfs.cpp:105
RBTree< const char * > rbt
目录中的文件树 File tree in the directory
Definition ramfs.hpp:219
int(* exec)(void *raw, int argc, char **argv)
可执行文件指针 Executable function pointer
Definition ramfs.hpp:92
FileType type
文件类型 File type
Definition ramfs.hpp:102
void * arg
可执行文件参数 Executable file argument
Definition ramfs.hpp:99
int Run(int argc, char **argv)
运行可执行文件 Runs an executable file
Definition ramfs.cpp:14
文件系统节点基类,所有文件和目录均继承自该类 Base class for file system nodes; all files and directories inherit from this
Definition ramfs.hpp:73
Dir * FindDir(const char *name)
在整个文件系统中查找目录 Finds a directory in the entire file system
Definition ramfs.hpp:443
Device * FindDevice(const char *name)
在整个文件系统中查找设备 Finds a device in the entire file system
Definition ramfs.hpp:451
RamFS(const char *name="ramfs")
构造函数,初始化内存文件系统的根目录 Constructor that initializes the root directory of the in-memory file system
Definition ramfs.cpp:5
@ DIR
目录 Directory
static int CompareStr(const char *const &a, const char *const &b)
比较两个字符串 Compares two strings
Definition ramfs.cpp:12
Dir dev_
dev 目录,用于存放设备文件 dev directory for storing device files
Definition ramfs.hpp:469
@ EXEC
可执行 Executable
Dir bin_
bin 目录,用于存放可执行文件 bin directory for storing executable files
Definition ramfs.hpp:463
Dir root_
文件系统的根目录 Root directory of the file system
Definition ramfs.hpp:457
File * FindFile(const char *name)
在整个文件系统中查找文件 Finds a file in the entire file system
Definition ramfs.hpp:435
ReadPort class for handling read operations.
Definition libxr_rw.hpp:268
WritePort class for handling write operations.
Definition libxr_rw.hpp:403
LibXR 命名空间
Definition ch32_gpio.hpp:9