20int RamFS::CompareStr(
const char*
const& a,
const char*
const& b) {
return strcmp(a, b); }
28char* RamFS::DuplicateName(
const char* name)
30 ASSERT(name !=
nullptr);
32 char* name_buff =
new char[strlen(name) + 1];
33 strcpy(name_buff, name);
50 : name_(other.name_), type_(other.type_), parent_(nullptr), tree_node_(this)
65RamFS::File::File(
const char* name) :
File() { this->
name_ = DuplicateName(name); }
76 ASSERT(file_type_ == FileType::EXEC);
77 ASSERT(exec_ !=
nullptr);
78 return exec_(arg_, argc, argv);
94RamFS::Custom::Custom(
const char* name, uint32_t kind,
void* context) :
Custom()
96 this->
name_ = DuplicateName(name);
112RamFS::Dir::Dir(
const char* name) :
Dir() { this->
name_ = DuplicateName(name); }
119void RamFS::Dir::AddNode(
FsNode& node)
121 ASSERT(node.
name_ !=
nullptr);
122 ASSERT(FindNode(node.
name_) ==
nullptr);
141 auto* node = rbt_.Search<
FsNode*>(name);
142 return node !=
nullptr ? node->data_ :
nullptr;
155 auto* ans = FindNode(name);
156 if (ans ==
nullptr || ans->GetNodeType() != type)
178 auto* ans = FindNodeByType(name, type);
190 ErrorCode operator()(Tree::Node<FsNode*>& item)
192 auto* node = item.data_;
195 return ErrorCode::OK;
198 ans_ =
static_cast<Dir*
>(node)->FindNodeRevByType(name_, type_);
199 return ans_ !=
nullptr ? ErrorCode::FAILED : ErrorCode::OK;
203 FindNodeRevFn find{name, type, ans};
204 rbt_.Foreach<
FsNode*>(find);
246 if (name[0] ==
'.' && name[1] ==
'\0')
251 if (name[0] ==
'.' && name[1] ==
'.' && name[2] ==
'\0')
273 if (name[0] ==
'.' && name[1] ==
'\0')
278 if (name[0] ==
'.' && name[1] ==
'.' && name[2] ==
'\0')
RamFS 的目录节点片段 / Directory-node fragment of RamFS
RamFS 的文件节点片段 / File-node fragment of RamFS
RamFS 的公共节点基类片段 / Common node-base fragment of RamFS
FsNodeType GetNodeType() const
获取节点类型 / Get the node type
Tree::Node< FsNode * > tree_node_
Dir * parent_
父目录;根目录保持为空 / Parent directory; stays null for the root.
const char * name_
节点名称缓冲区 / Retained node-name buffer.
自定义节点片段 / Custom-node fragment
void * context_
用户自管上下文指针 / Caller-owned user context pointer.
uint32_t kind_
用户定义类型标签 / User-defined kind tag.
目录节点片段 / Directory-node fragment
FsNode * FindNode(const char *name)
查找直属子节点 / Find a direct child node
File * FindFile(const char *name)
查找直属文件 / Find a direct child file
Dir * FindDir(const char *name)
查找直属目录,支持 "." 和 ".." / Find a direct child directory, supporting "." and ".."
File * FindFileRev(const char *name)
递归查找文件 / Find a file recursively
void Add(File &file)
添加直属文件节点 / Add a direct child file node
Custom * FindCustom(const char *name)
查找直属自定义节点 / Find a direct child custom node
Custom * FindCustomRev(const char *name)
递归查找自定义节点 / Find a custom node recursively
Dir * FindDirRev(const char *name)
递归查找目录,支持 "." 和 ".." / Find a directory recursively, supporting "." and ".."
文件节点片段 / File-node fragment
int Run(int argc, char **argv)
执行可执行文件 / Run an executable file
节点基类片段 / Base-node fragment
FsNode(FsNodeType node_type)
用指定节点类型构造基类部分 / Construct the base node with a given node type
const char * name_
节点名称缓冲区 / Retained node-name buffer.
轻量级内存文件系统 / Lightweight in-memory file system
RamFS(const char *name="ramfs")
构造 RamFS,并创建根目录和 bin 目录 / Construct RamFS with root and bin directories
FsNodeType
文件系统节点类型 / File-system node type
@ CUSTOM
用户自定义节点 / User-defined node
Dir bin_
预留的可执行文件目录 / Reserved executable-file directory.
LibXR 命名空间 / LibXR namespace.
ErrorCode
定义错误码枚举 / Defines an enumeration for error codes