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

轻量级的内存文件系统,实现基本的文件、目录和设备管理 A lightweight in-memory file system implementing basic file, directory, and device management More...

#include <ramfs.hpp>

Collaboration diagram for LibXR::RamFS:
[legend]

Data Structures

class  Device
 设备类,继承自红黑树节点 DeviceNode Device class inheriting from Red-Black tree node DeviceNode More...
 
class  DeviceNode
 设备节点,继承自 FsNode Device node, inheriting from FsNode More...
 
class  Dir
 目录类,继承自 RBTree 节点,用于管理文件、子目录和设备 Directory class, inheriting from RBTree node, used for managing files, subdirectories, and devices More...
 
class  DirNode
 目录节点,继承自 FsNode Directory node, inheriting from FsNode More...
 
class  FileNode
 文件节点类,继承自 FsNode,表示文件 File node class, inheriting from FsNode, representing a file More...
 
class  FsNode
 文件系统节点基类,所有文件和目录均继承自该类 Base class for file system nodes; all files and directories inherit from this More...
 
struct  StorageBlock
 

Public Types

enum class  FsNodeType : uint8_t {
  FILE , DIR , DEVICE , STORAGE ,
  UNKNOW
}
 文件系统节点类型 Types of file system nodes More...
 
enum class  FileType : uint8_t { READ_ONLY , READ_WRITE , EXEC }
 文件类型 Types of files More...
 
typedef LibXR::RamFS::FileNode FileNode
 
typedef RBTree< const char * >::Node< FileNodeFile
 

Public Member Functions

 RamFS (const char *name="ramfs")
 构造函数,初始化内存文件系统的根目录 Constructor that initializes the root directory of the in-memory file system
 
void Add (File &file)
 向文件系统的根目录添加文件 Adds a file to the root directory of the file system
 
void Add (Dir &dir)
 向文件系统的根目录添加子目录 Adds a subdirectory to the root directory of the file system
 
void Add (Device &dev)
 向文件系统的根目录添加设备 Adds a device to the root directory of the file system
 
FileFindFile (const char *name)
 在整个文件系统中查找文件 Finds a file in the entire file system
 
DirFindDir (const char *name)
 在整个文件系统中查找目录 Finds a directory in the entire file system
 
DeviceFindDevice (const char *name)
 在整个文件系统中查找设备 Finds a device in the entire file system
 

Static Public Member Functions

static int CompareStr (const char *const &a, const char *const &b)
 比较两个字符串 Compares two strings
 
template<typename DataType >
static File CreateFile (const char *name, DataType &raw)
 创建一个新的文件 Creates a new file
 
template<typename ArgType >
static File CreateFile (const char *name, int(*exec)(ArgType arg, int argc, char **argv), ArgType &&arg)
 创建一个可执行文件 Creates an executable file
 
static Dir CreateDir (const char *name)
 创建一个新的目录 Creates a new directory
 

Data Fields

Dir root_
 文件系统的根目录 Root directory of the file system
 
Dir bin_
 bin 目录,用于存放可执行文件 bin directory for storing executable files
 
Dir dev_
 dev 目录,用于存放设备文件 dev directory for storing device files
 

Detailed Description

轻量级的内存文件系统,实现基本的文件、目录和设备管理 A lightweight in-memory file system implementing basic file, directory, and device management

Definition at line 19 of file ramfs.hpp.

Member Typedef Documentation

◆ File

RBTree<const char *>::Node<FileNode> LibXR::RamFS::File

Definition at line 141 of file ramfs.hpp.

Member Enumeration Documentation

◆ FileType

enum class LibXR::RamFS::FileType : uint8_t
strong

文件类型 Types of files

Enumerator
READ_ONLY 

只读 Read-only

READ_WRITE 

读写 Read/Write

EXEC 

可执行 Executable

Definition at line 58 of file ramfs.hpp.

59 {
60 READ_ONLY,
62 EXEC,
63 };
@ READ_ONLY
只读 Read-only
@ READ_WRITE
读写 Read/Write
@ EXEC
可执行 Executable

◆ FsNodeType

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

文件系统节点类型 Types of file system nodes

Enumerator
FILE 

文件 File

DIR 

目录 Directory

DEVICE 

设备 Device

STORAGE 

存储 Storage

UNKNOW 

未知 Unknown

Definition at line 44 of file ramfs.hpp.

45 {
46 FILE,
47 DIR,
48 DEVICE,
49 STORAGE,
50 UNKNOW,
51 };
@ UNKNOW
未知 Unknown
@ STORAGE
存储 Storage
@ DIR
目录 Directory

Constructor & Destructor Documentation

◆ RamFS()

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

构造函数,初始化内存文件系统的根目录 Constructor that initializes the root directory of the in-memory file system

Parameters
name根目录的名称(默认为 "ramfs") Name of the root directory (default: "ramfs")

Definition at line 5 of file ramfs.cpp.

6 : root_(CreateDir(name)), bin_(CreateDir("bin")), dev_(CreateDir("dev"))
7{
10}
void Add(File &file)
添加文件到当前目录 Adds a file to the current directory
Definition ramfs.hpp:236
static Dir CreateDir(const char *name)
创建一个新的目录 Creates a new directory
Definition ramfs.hpp:397
Dir dev_
dev 目录,用于存放设备文件 dev directory for storing device files
Definition ramfs.hpp:469
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

Member Function Documentation

◆ Add() [1/3]

void LibXR::RamFS::Add ( Device & dev)
inline

向文件系统的根目录添加设备 Adds a device to the root directory of the file system

Parameters
dev要添加的设备 The device to be added

Definition at line 426 of file ramfs.hpp.

426{ root_.Add(dev); }

◆ Add() [2/3]

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

向文件系统的根目录添加子目录 Adds a subdirectory to the root directory of the file system

Parameters
dir要添加的子目录 The subdirectory to be added

Definition at line 420 of file ramfs.hpp.

420{ root_.Add(dir); }

◆ Add() [3/3]

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

向文件系统的根目录添加文件 Adds a file to the root directory of the file system

Parameters
file要添加的文件 The file to be added

Definition at line 414 of file ramfs.hpp.

414{ root_.Add(file); }

◆ CompareStr()

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

比较两个字符串 Compares two strings

Parameters
a字符串 A String A
b字符串 B String B
Returns
int 比较结果 Comparison result

Definition at line 12 of file ramfs.cpp.

12{ return strcmp(a, b); }

◆ CreateDir()

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

创建一个新的目录 Creates a new directory

Parameters
name目录名称 The name of the directory
Returns
Dir 创建的目录对象 The created directory object

Definition at line 397 of file ramfs.hpp.

398 {
399 Dir dir;
400
401 char *name_buff = new char[strlen(name) + 1];
402 strcpy(name_buff, name);
403 dir->name = name_buff;
404 dir->type = FsNodeType::DIR;
405
406 return dir;
407 }

◆ CreateFile() [1/2]

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

创建一个新的文件 Creates a new file

Template Parameters
DataType文件存储的数据类型 Data type stored in the file
Parameters
name文件名 The name of the file
raw文件存储的数据 Data stored in the file
Returns
File 创建的文件对象 The created file object

Definition at line 326 of file ramfs.hpp.

327 {
328 File file;
329 char *name_buff = new char[strlen(name) + 1];
330 strcpy(name_buff, name);
331 file->name = name_buff;
332
333 if (std::is_const<DataType>())
334 {
335 file->type = FileType::READ_ONLY;
336 file->addr_const = &raw;
337 }
338 else
339 {
340 file->type = FileType::READ_WRITE;
341 file->addr = &raw;
342 }
343
344 file->size = sizeof(DataType);
345
346 return file;
347 }

◆ 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

创建一个可执行文件 Creates an executable file

Template Parameters
ArgType可执行文件的参数类型 The argument type for the executable file
Parameters
name文件名 The name of the file
exec可执行函数 The executable function
arg可执行文件的参数 The argument for the executable file
Returns
File 创建的可执行文件对象 The created executable file object

Definition at line 359 of file ramfs.hpp.

361 {
362 typedef struct
363 {
364 ArgType arg;
365 decltype(exec) exec_fun;
366 } FileBlock;
367
368 File file;
369
370 char *name_buff = new char[strlen(name) + 1];
371 strcpy(name_buff, name);
372 file->name = name_buff;
373 file->type = FileType::EXEC;
374
375 auto block = new FileBlock;
376 block->arg = std::forward<ArgType>(arg);
377 block->exec_fun = exec;
378 file->arg = block;
379
380 auto fun = [](void *arg, int argc, char **argv)
381 {
382 auto block = reinterpret_cast<FileBlock *>(arg);
383 return block->exec_fun(block->arg, argc, argv);
384 };
385
386 file->exec = fun;
387
388 return file;
389 }

◆ FindDevice()

Device * LibXR::RamFS::FindDevice ( const char * name)
inline

在整个文件系统中查找设备 Finds a device in the entire file system

Parameters
name设备名 The name of the device
Returns
Device* 指向找到的设备的指针,如果未找到则返回 nullptr Pointer to the found device, or nullptr if not found

Definition at line 451 of file ramfs.hpp.

451{ return root_.FindDeviceRev(name); }
Device * FindDeviceRev(const char *name)
递归查找设备 Recursively searches for a device
Definition ramfs.cpp:142

◆ FindDir()

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

在整个文件系统中查找目录 Finds a directory in the entire file system

Parameters
name目录名 The name of the directory
Returns
Dir* 指向找到的目录的指针,如果未找到则返回 nullptr Pointer to the found directory, or nullptr if not found

Definition at line 443 of file ramfs.hpp.

443{ return root_.FindDirRev(name); }
Dir * FindDirRev(const char *name)
递归查找子目录 Recursively searches for a subdirectory
Definition ramfs.cpp:105

◆ FindFile()

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

在整个文件系统中查找文件 Finds a file in the entire file system

Parameters
name文件名 The name of the file
Returns
File* 指向找到的文件的指针,如果未找到则返回 nullptr Pointer to the found file, or nullptr if not found

Definition at line 435 of file ramfs.hpp.

435{ return root_.FindFileRev(name); }
File * FindFileRev(const char *name)
递归查找文件 Recursively searches for a file
Definition ramfs.cpp:45

Field Documentation

◆ bin_

Dir LibXR::RamFS::bin_

bin 目录,用于存放可执行文件 bin directory for storing executable files

Definition at line 463 of file ramfs.hpp.

◆ dev_

Dir LibXR::RamFS::dev_

dev 目录,用于存放设备文件 dev directory for storing device files

Definition at line 469 of file ramfs.hpp.

◆ root_

Dir LibXR::RamFS::root_

文件系统的根目录 Root directory of the file system

Definition at line 457 of file ramfs.hpp.


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