libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
LibXR::RamFS Class Reference

轻量级内存文件系统 / Lightweight in-memory file system More...

#include <ramfs.hpp>

Collaboration diagram for LibXR::RamFS:
[legend]

Classes

class  Custom
 自定义节点片段 / Custom-node fragment More...
 
class  Dir
 目录节点片段 / Directory-node fragment More...
 
class  File
 文件节点片段 / File-node fragment More...
 
class  FsNode
 节点基类片段 / Base-node fragment More...
 

Public Types

enum class  FsNodeType : uint8_t { FILE , DIR , CUSTOM }
 文件系统节点类型 / File-system node type More...
 

Public Member Functions

 RamFS (const char *name="ramfs")
 构造 RamFS,并创建根目录和 bin 目录 / Construct RamFS with root and bin directories
 
void Add (File &file)
 添加文件节点到根目录 / Add a file node to the root directory
 
void Add (Dir &dir)
 添加目录节点到根目录 / Add a directory node to the root directory
 
void Add (Custom &custom)
 添加自定义节点到根目录 / Add a custom node to the root directory
 
File * FindFile (const char *name)
 从整个 RamFS 递归查找文件 / Find a file recursively from the RamFS root
 
Dir * FindDir (const char *name)
 从整个 RamFS 递归查找目录 / Find a directory recursively from the RamFS root
 
Custom * FindCustom (const char *name)
 从整个 RamFS 递归查找自定义节点 / Find a custom node recursively from the RamFS root
 

Static Public Member Functions

template<typename DataType >
static File CreateFile (const char *name, DataType &raw)
 工厂与根入口片段 / Factory and root-entry fragment
 
template<typename ArgType >
static File CreateFile (const char *name, int(*exec)(ArgType arg, int argc, char **argv), ArgType &&arg)
 创建可执行文件 / Create an executable file
 
template<typename ArgType >
static File CreateCommand (const char *name, int(*exec)(ArgType arg, int argc, char **argv), ArgType &&arg)
 创建命令兼容入口,返回可执行文件 / Create a command-compatible executable file
 
static File CreateCommand (const char *name, int(*exec)(int argc, char **argv))
 创建不需要执行上下文的命令 / Create a command that needs no execution context
 
static Dir CreateDir (const char *name)
 创建目录节点 / Create a directory node
 

Public Attributes

Dir root_
 
Dir bin_
 预留的可执行文件目录 / Reserved executable-file directory.
 

Detailed Description

轻量级内存文件系统 / Lightweight in-memory file system

RamFS 组织外部内存文件、可执行文件、目录和自定义节点 / RamFS organizes external-memory files, executable files, directories, and custom nodes. 文件数据由调用方持有 / File payload storage is owned by the caller.

Definition at line 22 of file ramfs.hpp.

Member Enumeration Documentation

◆ FsNodeType

enum class LibXR::RamFS::FsNodeType : uint8_t
strong

文件系统节点类型 / File-system node type

Enumerator
FILE 

文件 / File

DIR 

目录 / Directory

CUSTOM 

用户自定义节点 / User-defined node

Definition at line 37 of file ramfs.hpp.

38 {
39 FILE,
40 DIR,
41 CUSTOM,
42 };
@ CUSTOM
用户自定义节点 / User-defined node
@ DIR
目录 / Directory

Constructor & Destructor Documentation

◆ RamFS()

RamFS::RamFS ( const char * name = "ramfs")

构造 RamFS,并创建根目录和 bin 目录 / Construct RamFS with root and bin directories

构造 RamFS,并挂接根目录下的 bin 目录 Construct RamFS and attach the bin directory under the root

Parameters
name根目录名称 / Root directory name
Note
包含动态内存分配 / Contains dynamic memory allocation
Parameters
name根目录名称 / Root directory name

Definition at line 10 of file ramfs.cpp.

10: root_(name), bin_("bin") { root_.Add(bin_); }
void Add(File &file)
添加直属文件节点 / Add a direct child file node
Definition ramfs.hpp:16
Dir bin_
预留的可执行文件目录 / Reserved executable-file directory.
Definition ramfs.hpp:177

Member Function Documentation

◆ Add() [1/3]

void LibXR::RamFS::Add ( Custom & custom)
inline

添加自定义节点到根目录 / Add a custom node to the root directory

Parameters
custom自定义节点 / Custom node

Definition at line 151 of file ramfs.hpp.

163{
172class RamFS
173{
174 public:
182 RamFS(const char* name = "ramfs");
183
187 enum class FsNodeType : uint8_t
188 {
189 FILE,
190 DIR,
191 CUSTOM,
192 };
193
194 class Dir;
195
196 private:
200 using Tree = RBTree<const char*>;
201
205 enum class FileType : uint8_t
206 {
207 READ_ONLY,
208 READ_WRITE,
209 EXEC,
210 };
211
219 static int CompareStr(const char* const& a, const char* const& b);
220
221 public:
225#include "fs_node.hpp"
229#include "file.hpp"
233#include "custom.hpp"
237#include "dir.hpp"
241#include "factory.hpp"
242
243 private:
253 static char* DuplicateName(const char* name);
254};
255} // namespace LibXR
RamFS 的目录节点片段 / Directory-node fragment of RamFS
Definition dir.hpp:9
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

◆ Add() [2/3]

void LibXR::RamFS::Add ( Dir & dir)
inline

添加目录节点到根目录 / Add a directory node to the root directory

Parameters
dir目录节点 / Directory node

Definition at line 145 of file ramfs.hpp.

157{
166class RamFS
167{
168 public:
176 RamFS(const char* name = "ramfs");
177
181 enum class FsNodeType : uint8_t
182 {
183 FILE,
184 DIR,
185 CUSTOM,
186 };
187
188 class Dir;
189
190 private:
194 using Tree = RBTree<const char*>;
195
199 enum class FileType : uint8_t
200 {
201 READ_ONLY,
202 READ_WRITE,
203 EXEC,
204 };
205
213 static int CompareStr(const char* const& a, const char* const& b);
214
215 public:
219#include "fs_node.hpp"
223#include "file.hpp"
227#include "custom.hpp"
231#include "dir.hpp"
235#include "factory.hpp"
236
237 private:
247 static char* DuplicateName(const char* name);
248};
249} // namespace LibXR

◆ Add() [3/3]

void LibXR::RamFS::Add ( File & file)
inline

添加文件节点到根目录 / Add a file node to the root directory

Parameters
file文件节点 / File node
Note
RamFS 只保存节点地址,节点必须是在整个运行期内有效的左值; Add(RamFS::CreateCommand(...)) 传入临时对象会留下悬空指针,因此不接受右值。 RamFS only stores the node address, so the node must be an lvalue that lives for the whole run; passing a temporary from CreateCommand(...) would leave a dangling pointer, which is why rvalues are not accepted.

Definition at line 139 of file ramfs.hpp.

151{
160class RamFS
161{
162 public:
170 RamFS(const char* name = "ramfs");
171
175 enum class FsNodeType : uint8_t
176 {
177 FILE,
178 DIR,
179 CUSTOM,
180 };
181
182 class Dir;
183
184 private:
188 using Tree = RBTree<const char*>;
189
193 enum class FileType : uint8_t
194 {
195 READ_ONLY,
196 READ_WRITE,
197 EXEC,
198 };
199
207 static int CompareStr(const char* const& a, const char* const& b);
208
209 public:
213#include "fs_node.hpp"
217#include "file.hpp"
221#include "custom.hpp"
225#include "dir.hpp"
229#include "factory.hpp"
230
231 private:
241 static char* DuplicateName(const char* name);
242};
243} // namespace LibXR

◆ CreateCommand() [1/2]

template<typename ArgType >
static File LibXR::RamFS::CreateCommand ( const char * name,
int(* exec )(ArgType arg, int argc, char **argv),
ArgType && arg )
inlinestatic

创建命令兼容入口,返回可执行文件 / Create a command-compatible executable file

Template Parameters
ArgType执行上下文参数类型 / Execution context argument type
Parameters
name文件名 / File name
exec执行函数 / Execution function
arg执行上下文参数 / Execution context argument
Returns
可执行文件节点 / Executable file node
Note
包含动态内存分配 / Contains dynamic memory allocation
运行期持久化语义与上面的 executable factory 相同; 命令块按当前设计默认常驻。 This follows the same lifetime-retained semantics as the executable factory above; command blocks are intentionally retained by design.

Definition at line 92 of file ramfs.hpp.

93 :

◆ CreateCommand() [2/2]

static File LibXR::RamFS::CreateCommand ( const char * name,
int(* exec )(int argc, char **argv) )
inlinestatic

创建不需要执行上下文的命令 / Create a command that needs no execution context

Parameters
name文件名 / File name
exec命令函数,参数为 argc 和 argv / Command function taking argc and argv
Returns
可执行文件节点 / Executable file node
Note
等价于带执行上下文的写法,上下文为函数本身; 不再需要 CreateCommand<void*>(name, [](void*, int, char**) {...}, nullptr)。 Equivalent to the form with an execution context, the context being the function itself; CreateCommand<void*>(name, [](void*, int, char**) {...}, nullptr) is no longer needed.
包含动态内存分配,命令块按当前设计默认常驻。 Contains dynamic memory allocation; the command block is retained by design.

Definition at line 112 of file ramfs.hpp.

124{
133class RamFS
134{
135 public:
143 RamFS(const char* name = "ramfs");
144
148 enum class FsNodeType : uint8_t
149 {
150 FILE,
151 DIR,
152 CUSTOM,
153 };
154
155 class Dir;
156
157 private:
161 using Tree = RBTree<const char*>;
162
166 enum class FileType : uint8_t
167 {
168 READ_ONLY,
169 READ_WRITE,
170 EXEC,
171 };
172
180 static int CompareStr(const char* const& a, const char* const& b);
181
182 public:
186#include "fs_node.hpp"
190#include "file.hpp"
194#include "custom.hpp"
198#include "dir.hpp"
202#include "factory.hpp"
203
204 private:
214 static char* DuplicateName(const char* name);
215};
216} // namespace LibXR

◆ CreateDir()

static Dir LibXR::RamFS::CreateDir ( const char * name)
inlinestatic

创建目录节点 / Create a directory node

Parameters
name目录名称 / Directory name
Returns
目录节点 / Directory node
Note
包含动态内存分配 / Contains dynamic memory allocation

Definition at line 127 of file ramfs.hpp.

139{
148class RamFS
149{
150 public:
158 RamFS(const char* name = "ramfs");
159
163 enum class FsNodeType : uint8_t
164 {
165 FILE,
166 DIR,
167 CUSTOM,
168 };
169
170 class Dir;
171
172 private:
176 using Tree = RBTree<const char*>;
177
181 enum class FileType : uint8_t
182 {
183 READ_ONLY,
184 READ_WRITE,
185 EXEC,
186 };
187
195 static int CompareStr(const char* const& a, const char* const& b);
196
197 public:
201#include "fs_node.hpp"
205#include "file.hpp"
209#include "custom.hpp"
213#include "dir.hpp"
217#include "factory.hpp"
218
219 private:
229 static char* DuplicateName(const char* name);
230};
231} // namespace LibXR

◆ CreateFile() [1/2]

template<typename DataType >
static File LibXR::RamFS::CreateFile ( const char * name,
DataType & raw )
inlinestatic

工厂与根入口片段 / Factory and root-entry fragment

RamFS 的工厂与根入口片段 / Factory and root-entry fragment of RamFS

创建引用外部数据的文件 / Create a file referencing external data

Template Parameters
DataType外部数据类型 / External data type
Parameters
name文件名 / File name
raw外部数据引用 / External data reference
Returns
文件节点 / File node
Note
包含动态内存分配 / Contains dynamic memory allocation

Definition at line 15 of file ramfs.hpp.

23{
24 public:
32 RamFS(const char* name = "ramfs");
33

◆ CreateFile() [2/2]

template<typename ArgType >
static File LibXR::RamFS::CreateFile ( const char * name,
int(* exec )(ArgType arg, int argc, char **argv),
ArgType && arg )
inlinestatic

创建可执行文件 / Create an executable file

Template Parameters
ArgType执行上下文参数类型 / Execution context argument type
Parameters
name文件名 / File name
exec执行函数 / Execution function
arg执行上下文参数 / Execution context argument
Returns
可执行文件节点 / Executable file node
Note
包含动态内存分配 / Contains dynamic memory allocation
可执行块按当前 RamFS 设计属于初始化后常驻状态; 当前实现不提供运行期回收路径。 Executable blocks are retained for startup / lifetime registration in the current RamFS design; the implementation does not provide a runtime reclamation path for them.

Definition at line 51 of file ramfs.hpp.

55 : uint8_t
56 {
57 READ_ONLY,
58 READ_WRITE,
59 EXEC,
60 };
61
69 static int CompareStr(const char* const& a, const char* const& b);
70
71 public:

◆ FindCustom()

Custom * LibXR::RamFS::FindCustom ( const char * name)
inline

从整个 RamFS 递归查找自定义节点 / Find a custom node recursively from the RamFS root

Parameters
name节点名称 / Node name
Returns
自定义节点指针;未找到返回 nullptr / Custom node pointer, or nullptr

Definition at line 173 of file ramfs.hpp.

185{
194class RamFS
195{
196 public:
204 RamFS(const char* name = "ramfs");
205
209 enum class FsNodeType : uint8_t
210 {
211 FILE,
212 DIR,
213 CUSTOM,
214 };
215
216 class Dir;
217
218 private:
222 using Tree = RBTree<const char*>;
223
227 enum class FileType : uint8_t
228 {
229 READ_ONLY,
230 READ_WRITE,
231 EXEC,
232 };
233
241 static int CompareStr(const char* const& a, const char* const& b);
242
243 public:
247#include "fs_node.hpp"
251#include "file.hpp"
255#include "custom.hpp"
259#include "dir.hpp"
263#include "factory.hpp"
264
265 private:
275 static char* DuplicateName(const char* name);
276};
277} // namespace LibXR

◆ FindDir()

Dir * LibXR::RamFS::FindDir ( const char * name)
inline

从整个 RamFS 递归查找目录 / Find a directory recursively from the RamFS root

Parameters
name目录名 / Directory name
Returns
目录指针;未找到返回 nullptr / Directory pointer, or nullptr

Definition at line 165 of file ramfs.hpp.

177{
186class RamFS
187{
188 public:
196 RamFS(const char* name = "ramfs");
197
201 enum class FsNodeType : uint8_t
202 {
203 FILE,
204 DIR,
205 CUSTOM,
206 };
207
208 class Dir;
209
210 private:
214 using Tree = RBTree<const char*>;
215
219 enum class FileType : uint8_t
220 {
221 READ_ONLY,
222 READ_WRITE,
223 EXEC,
224 };
225
233 static int CompareStr(const char* const& a, const char* const& b);
234
235 public:
239#include "fs_node.hpp"
243#include "file.hpp"
247#include "custom.hpp"
251#include "dir.hpp"
255#include "factory.hpp"
256
257 private:
267 static char* DuplicateName(const char* name);
268};
269} // namespace LibXR

◆ FindFile()

File * LibXR::RamFS::FindFile ( const char * name)
inline

从整个 RamFS 递归查找文件 / Find a file recursively from the RamFS root

Parameters
name文件名 / File name
Returns
文件指针;未找到返回 nullptr / File pointer, or nullptr

Definition at line 158 of file ramfs.hpp.

170{
179class RamFS
180{
181 public:
189 RamFS(const char* name = "ramfs");
190
194 enum class FsNodeType : uint8_t
195 {
196 FILE,
197 DIR,
198 CUSTOM,
199 };
200
201 class Dir;
202
203 private:
207 using Tree = RBTree<const char*>;
208
212 enum class FileType : uint8_t
213 {
214 READ_ONLY,
215 READ_WRITE,
216 EXEC,
217 };
218
226 static int CompareStr(const char* const& a, const char* const& b);
227
228 public:
232#include "fs_node.hpp"
236#include "file.hpp"
240#include "custom.hpp"
244#include "dir.hpp"
248#include "factory.hpp"
249
250 private:
260 static char* DuplicateName(const char* name);
261};
262} // namespace LibXR

Member Data Documentation

◆ bin_

Dir LibXR::RamFS::bin_

预留的可执行文件目录 / Reserved executable-file directory.

Definition at line 177 of file ramfs.hpp.

◆ root_

Dir LibXR::RamFS::root_

根目录;所有外部 Add()/Find*() 默认都从这里进入 / Root directory; all external Add()/Find*() entry through here.

Definition at line 175 of file ramfs.hpp.


The documentation for this class was generated from the following files: