libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::RamFS Class Reference

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

#include <ramfs.hpp>

Collaboration diagram for LibXR::RamFS:
[legend]

Data Structures

class  Custom
 用户自定义节点,RamFS 仅负责命名和查找 / User-defined node; RamFS only stores and finds it by name More...
 
class  Dir
 目录节点,管理直属子节点 / Directory node that owns a child namespace More...
 
class  File
 内存文件或可执行文件 / Memory file or executable file More...
 
class  FsNode
 文件系统节点基类 / Base class for all RamFS nodes 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
 
FileFindFile (const char *name)
 从整个 RamFS 递归查找文件 / Find a file recursively from the RamFS root
 
DirFindDir (const char *name)
 从整个 RamFS 递归查找目录 / Find a directory recursively from the RamFS root
 
CustomFindCustom (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)
 创建引用外部数据的文件 / Create a file referencing external data
 
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 Dir CreateDir (const char *name)
 创建目录节点 / Create a directory node
 

Data Fields

Dir root_
 根目录 / Root directory
 
Dir bin_
 可执行文件目录 / Executable-file directory
 

Private Types

enum class  FileType : uint8_t { READ_ONLY , READ_WRITE , EXEC }
 
using Tree = RBTree<const char*>
 

Static Private Member Functions

static int CompareStr (const char *const &a, const char *const &b)
 
static char * DuplicateName (const char *name)
 

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 21 of file ramfs.hpp.

Member Typedef Documentation

◆ Tree

using LibXR::RamFS::Tree = RBTree<const char*>
private

Definition at line 46 of file ramfs.hpp.

Member Enumeration Documentation

◆ FileType

enum class LibXR::RamFS::FileType : uint8_t
strongprivate

Definition at line 47 of file ramfs.hpp.

48 {
49 READ_ONLY,
50 READ_WRITE,
51 EXEC,
52 };

◆ 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 36 of file ramfs.hpp.

37 {
38 FILE,
39 DIR,
40 CUSTOM,
41 };
@ 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

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

Definition at line 5 of file ramfs.cpp.

5: root_(name), bin_("bin") { root_.Add(bin_); }
void Add(File &file)
添加直属文件节点 / Add a direct child file node
Definition ramfs.hpp:283
Dir bin_
可执行文件目录 / Executable-file directory
Definition ramfs.hpp:509
Dir root_
根目录 / Root directory
Definition ramfs.hpp:508

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 484 of file ramfs.hpp.

484{ root_.Add(custom); }

◆ 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 478 of file ramfs.hpp.

478{ root_.Add(dir); }

◆ Add() [3/3]

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

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

Parameters
file文件节点 / File node

Definition at line 472 of file ramfs.hpp.

472{ root_.Add(file); }

◆ CompareStr()

int RamFS::CompareStr ( const char *const & a,
const char *const & b )
staticprivate

Definition at line 7 of file ramfs.cpp.

7{ return strcmp(a, b); }

◆ CreateCommand()

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

Definition at line 452 of file ramfs.hpp.

455 {
456 return CreateFile(name, exec, std::forward<ArgType>(arg));
457 }
static File CreateFile(const char *name, DataType &raw)
创建引用外部数据的文件 / Create a file referencing external data
Definition ramfs.hpp:384

◆ 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 466 of file ramfs.hpp.

466{ return Dir(name); }

◆ CreateFile() [1/2]

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

创建引用外部数据的文件 / 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 384 of file ramfs.hpp.

385 {
386 using StoredType = std::remove_reference_t<DataType>;
387
388 File file(name);
389 if constexpr (std::is_const_v<StoredType>)
390 {
391 file.file_type_ = FileType::READ_ONLY;
392 file.addr_const_ = &raw;
393 }
394 else
395 {
396 file.file_type_ = FileType::READ_WRITE;
397 file.addr_ = &raw;
398 }
399
400 file.size_ = sizeof(StoredType);
401 return file;
402 }

◆ 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

Definition at line 415 of file ramfs.hpp.

417 {
418 using StoredArgType = std::remove_reference_t<ArgType>;
419 struct ExecutableBlock
420 {
421 StoredArgType arg_;
422 decltype(exec) exec_fun_;
423 };
424
425 File file(name);
426
427 auto block = new ExecutableBlock{std::forward<ArgType>(arg), exec};
428 file.file_type_ = FileType::EXEC;
429 file.arg_ = block;
430
431 file.exec_ = [](void* raw, int argc, char** argv)
432 {
433 auto* block = static_cast<ExecutableBlock*>(raw);
434 return block->exec_fun_(block->arg_, argc, argv);
435 };
436
437 return file;
438 }

◆ DuplicateName()

char * RamFS::DuplicateName ( const char * name)
staticprivate

Definition at line 9 of file ramfs.cpp.

10{
11 ASSERT(name != nullptr);
12 if (name == nullptr)
13 {
14 return nullptr;
15 }
16
17 char* name_buff = new char[strlen(name) + 1];
18 strcpy(name_buff, name);
19 return name_buff;
20}

◆ 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 506 of file ramfs.hpp.

506{ return root_.FindCustomRev(name); }
Custom * FindCustomRev(const char *name)
递归查找自定义节点 / Find a custom node recursively
Definition ramfs.cpp:169

◆ 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 498 of file ramfs.hpp.

498{ return root_.FindDirRev(name); }
Dir * FindDirRev(const char *name)
递归查找目录,支持 "." 和 ".." / Find a directory recursively, supporting "." and ".."
Definition ramfs.cpp:144

◆ 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 491 of file ramfs.hpp.

491{ return root_.FindFileRev(name); }
File * FindFileRev(const char *name)
递归查找文件 / Find a file recursively
Definition ramfs.cpp:119

Field Documentation

◆ bin_

Dir LibXR::RamFS::bin_

可执行文件目录 / Executable-file directory

Definition at line 509 of file ramfs.hpp.

◆ root_

Dir LibXR::RamFS::root_

根目录 / Root directory

Definition at line 508 of file ramfs.hpp.


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