libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
ramfs.cpp
1#include "ramfs.hpp"
2
3using namespace LibXR;
4
10RamFS::RamFS(const char* name) : root_(name), bin_("bin") { root_.Add(bin_); }
11
20int RamFS::CompareStr(const char* const& a, const char* const& b) { return strcmp(a, b); }
21
28char* RamFS::DuplicateName(const char* name)
29{
30 ASSERT(name != nullptr);
31
32 char* name_buff = new char[strlen(name) + 1];
33 strcpy(name_buff, name);
34 return name_buff;
35}
36
42RamFS::FsNode::FsNode(FsNodeType node_type) : type_(node_type), tree_node_(this) {}
43
50 : name_(other.name_), type_(other.type_), parent_(nullptr), tree_node_(this)
51{
52}
53
58RamFS::File::File() : FsNode(FsNodeType::FILE), addr_(nullptr), arg_(nullptr), size_(0) {}
59
65RamFS::File::File(const char* name) : File() { this->name_ = DuplicateName(name); }
66
74int RamFS::File::Run(int argc, char** argv)
75{
76 ASSERT(file_type_ == FileType::EXEC);
77 ASSERT(exec_ != nullptr);
78 return exec_(arg_, argc, argv);
79}
80
85RamFS::Custom::Custom() : FsNode(FsNodeType::CUSTOM) {}
86
94RamFS::Custom::Custom(const char* name, uint32_t kind, void* context) : Custom()
95{
96 this->name_ = DuplicateName(name);
97 this->kind_ = kind;
98 this->context_ = context;
99}
100
105RamFS::Dir::Dir() : FsNode(FsNodeType::DIR), rbt_(RamFS::CompareStr) {}
106
112RamFS::Dir::Dir(const char* name) : Dir() { this->name_ = DuplicateName(name); }
113
119void RamFS::Dir::AddNode(FsNode& node)
120{
121 ASSERT(node.name_ != nullptr);
122 ASSERT(FindNode(node.name_) == nullptr);
123 node.parent_ = this;
124 rbt_.Insert(node.tree_node_, node.name_);
125}
126
135{
136 if (name == nullptr)
137 {
138 return nullptr;
139 }
140
141 auto* node = rbt_.Search<FsNode*>(name);
142 return node != nullptr ? node->data_ : nullptr;
143}
144
153RamFS::FsNode* RamFS::Dir::FindNodeByType(const char* name, FsNodeType type)
154{
155 auto* ans = FindNode(name);
156 if (ans == nullptr || ans->GetNodeType() != type)
157 {
158 return nullptr;
159 }
160 return ans;
161}
162
176RamFS::FsNode* RamFS::Dir::FindNodeRevByType(const char* name, FsNodeType type)
177{
178 auto* ans = FindNodeByType(name, type);
179 if (ans != nullptr)
180 {
181 return ans;
182 }
183
184 struct FindNodeRevFn
185 {
186 const char* name_;
187 FsNodeType type_;
188 FsNode*& ans_;
189
190 ErrorCode operator()(Tree::Node<FsNode*>& item)
191 {
192 auto* node = item.data_;
193 if (node->GetNodeType() != FsNodeType::DIR)
194 {
195 return ErrorCode::OK;
196 }
197
198 ans_ = static_cast<Dir*>(node)->FindNodeRevByType(name_, type_);
199 return ans_ != nullptr ? ErrorCode::FAILED : ErrorCode::OK;
200 }
201 };
202
203 FindNodeRevFn find{name, type, ans};
204 rbt_.Foreach<FsNode*>(find);
205 return ans;
206}
207
216{
217 return static_cast<File*>(FindNodeByType(name, FsNodeType::FILE));
218}
219
228{
229 return static_cast<File*>(FindNodeRevByType(name, FsNodeType::FILE));
230}
231
240{
241 if (name == nullptr)
242 {
243 return nullptr;
244 }
245
246 if (name[0] == '.' && name[1] == '\0')
247 {
248 return this;
249 }
250
251 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
252 {
253 return parent_;
254 }
255
256 return static_cast<Dir*>(FindNodeByType(name, FsNodeType::DIR));
257}
258
267{
268 if (name == nullptr)
269 {
270 return nullptr;
271 }
272
273 if (name[0] == '.' && name[1] == '\0')
274 {
275 return this;
276 }
277
278 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
279 {
280 return parent_;
281 }
282
283 return static_cast<Dir*>(FindNodeRevByType(name, FsNodeType::DIR));
284}
285
294{
295 return static_cast<Custom*>(FindNodeByType(name, FsNodeType::CUSTOM));
296}
297
306{
307 return static_cast<Custom*>(FindNodeRevByType(name, FsNodeType::CUSTOM));
308}
RamFS 的目录节点片段 / Directory-node fragment of RamFS
Definition dir.hpp:9
RamFS 的文件节点片段 / File-node fragment of RamFS
Definition file.hpp:9
RamFS 的公共节点基类片段 / Common node-base fragment of RamFS
Definition fs_node.hpp:9
FsNodeType GetNodeType() const
获取节点类型 / Get the node type
Definition fs_node.hpp:15
Tree::Node< FsNode * > tree_node_
Definition fs_node.hpp:47
Dir * parent_
父目录;根目录保持为空 / Parent directory; stays null for the root.
Definition fs_node.hpp:26
const char * name_
节点名称缓冲区 / Retained node-name buffer.
Definition fs_node.hpp:24
自定义节点片段 / Custom-node fragment
Definition ramfs.hpp:11
void * context_
用户自管上下文指针 / Caller-owned user context pointer.
Definition ramfs.hpp:24
uint32_t kind_
用户定义类型标签 / User-defined kind tag.
Definition ramfs.hpp:23
目录节点片段 / Directory-node fragment
Definition ramfs.hpp:10
FsNode * FindNode(const char *name)
查找直属子节点 / Find a direct child node
Definition ramfs.cpp:134
File * FindFile(const char *name)
查找直属文件 / Find a direct child file
Definition ramfs.cpp:215
Dir * FindDir(const char *name)
查找直属目录,支持 "." 和 ".." / Find a direct child directory, supporting "." and ".."
Definition ramfs.cpp:239
File * FindFileRev(const char *name)
递归查找文件 / Find a file recursively
Definition ramfs.cpp:227
void Add(File &file)
添加直属文件节点 / Add a direct child file node
Definition ramfs.hpp:16
Custom * FindCustom(const char *name)
查找直属自定义节点 / Find a direct child custom node
Definition ramfs.cpp:293
Custom * FindCustomRev(const char *name)
递归查找自定义节点 / Find a custom node recursively
Definition ramfs.cpp:305
Dir * FindDirRev(const char *name)
递归查找目录,支持 "." 和 ".." / Find a directory recursively, supporting "." and ".."
Definition ramfs.cpp:266
文件节点片段 / File-node fragment
Definition ramfs.hpp:10
int Run(int argc, char **argv)
执行可执行文件 / Run an executable file
Definition ramfs.cpp:74
节点基类片段 / Base-node fragment
Definition ramfs.hpp:10
FsNode(FsNodeType node_type)
用指定节点类型构造基类部分 / Construct the base node with a given node type
Definition ramfs.cpp:42
const char * name_
节点名称缓冲区 / Retained node-name buffer.
Definition ramfs.hpp:25
轻量级内存文件系统 / Lightweight in-memory file system
Definition ramfs.hpp:23
RamFS(const char *name="ramfs")
构造 RamFS,并创建根目录和 bin 目录 / Construct RamFS with root and bin directories
Definition ramfs.cpp:10
FsNodeType
文件系统节点类型 / File-system node type
Definition ramfs.hpp:38
@ CUSTOM
用户自定义节点 / User-defined node
@ DIR
目录 / Directory
Dir bin_
预留的可执行文件目录 / Reserved executable-file directory.
Definition ramfs.hpp:177
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes