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:

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
 
File * FindFile (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

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

Definition at line 153 of file ramfs.hpp.

Member Enumeration Documentation

◆ FileType

文件类型 Types of files

Enumerator
READ_ONLY 

只读 Read-only

READ_WRITE 

读写 Read/Write

EXEC 

可执行 Executable

Definition at line 66 of file ramfs.hpp.

67 {
68 READ_ONLY,
70 EXEC,
71 };
@ READ_ONLY
只读 Read-only
@ READ_WRITE
读写 Read/Write
@ EXEC
可执行 Executable

◆ FsNodeType

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

Enumerator
FILE 

文件 File

DIR 

目录 Directory

DEVICE 

设备 Device

STORAGE 

存储 Storage

UNKNOW 

未知 Unknown

Definition at line 52 of file ramfs.hpp.

53 {
54 FILE,
55 DIR,
56 DEVICE,
57 STORAGE,
58 UNKNOW,
59 };
@ UNKNOW
未知 Unknown
@ STORAGE
存储 Storage
@ DIR
目录 Directory

Constructor & Destructor Documentation

◆ RamFS()

LibXR::RamFS::RamFS ( const char name = "ramfs")
inline

构造函数,初始化内存文件系统的根目录 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 28 of file ramfs.hpp.

29 : root_(CreateDir(name)), bin_(CreateDir("bin")), dev_(CreateDir("dev"))
30 {
31 root_.Add(bin_);
32 root_.Add(dev_);
33 }
void Add(File &file)
添加文件到当前目录 Adds a file to the current directory
Definition ramfs.hpp:257
static Dir CreateDir(const char *name)
创建一个新的目录 Creates a new directory
Definition ramfs.hpp:564
Dir dev_
dev 目录,用于存放设备文件 dev directory for storing device files
Definition ramfs.hpp:636
Dir bin_
bin 目录,用于存放可执行文件 bin directory for storing executable files
Definition ramfs.hpp:630
Dir root_
文件系统的根目录 Root directory of the file system
Definition ramfs.hpp:624

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

593{ root_.Add(dev); }
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值

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

587{ 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 581 of file ramfs.hpp.

581{ root_.Add(file); }

◆ CompareStr()

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

比较两个字符串 Compares two strings

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

Definition at line 42 of file ramfs.hpp.

43 {
44 return strcmp(a, b);
45 }

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

565 {
566 Dir dir;
567
568 char *name_buff = new char[strlen(name) + 1];
569 strcpy(name_buff, name);
570 dir->name = name_buff;
571 dir->type = FsNodeType::DIR;
572
573 return dir;
574 }

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

494 {
495 File file;
496 char *name_buff = new char[strlen(name) + 1];
497 strcpy(name_buff, name);
498 file->name = name_buff;
499
500 if (std::is_const<DataType>())
501 {
502 file->type = FileType::READ_ONLY;
503 file->addr_const = &raw;
504 }
505 else
506 {
507 file->type = FileType::READ_WRITE;
508 file->addr = &raw;
509 }
510
511 file->size = sizeof(DataType);
512
513 return file;
514 }

◆ CreateFile() [2/2]

template<typename ArgType >
static File LibXR::RamFS::CreateFile ( const char name,
int(*)(ArgType arg, int argc, char **argv exec,
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 526 of file ramfs.hpp.

528 {
529 typedef struct
530 {
531 ArgType arg;
532 typeof(exec) exec_fun;
533 } FileBlock;
534
535 File file;
536
537 char *name_buff = new char[strlen(name) + 1];
538 strcpy(name_buff, name);
539 file->name = name_buff;
540 file->type = FileType::EXEC;
541
542 auto block = new FileBlock;
543 block->arg = std::forward<ArgType>(arg);
544 block->exec_fun = exec;
545 file->arg = block;
546
547 auto fun = [](void *arg, int argc, char **argv)
548 {
549 auto block = reinterpret_cast<FileBlock *>(arg);
550 return block->exec_fun(block->arg, argc, argv);
551 };
552
553 file->exec = fun;
554
555 return file;
556 }

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

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

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

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

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

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

Field Documentation

◆ bin_

Dir LibXR::RamFS::bin_

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

Definition at line 630 of file ramfs.hpp.

◆ dev_

Dir LibXR::RamFS::dev_

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

Definition at line 636 of file ramfs.hpp.

◆ root_

Dir LibXR::RamFS::root_

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

Definition at line 624 of file ramfs.hpp.


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