libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
ramfs.cpp
1#include "ramfs.hpp"
2
3using namespace LibXR;
4
5RamFS::RamFS(const char *name)
6 : root_(CreateDir(name)), bin_(CreateDir("bin")), dev_(CreateDir("dev"))
7{
10}
11
12int RamFS::CompareStr(const char *const &a, const char *const &b) { return strcmp(a, b); }
13
14int RamFS::FileNode::Run(int argc, char **argv)
15{
16 ASSERT(type == FileType::EXEC);
17 return exec(arg, argc, argv);
18}
19
20RamFS::Device::Device(const char *name, const ReadPort &read_port,
21 const WritePort &write_port)
22{
23 char *name_buff = new char[strlen(name) + 1];
24 strcpy(name_buff, name);
25 data_.name = name_buff;
26 data_.type = FsNodeType::DEVICE;
27
28 UNUSED(read_port);
29 UNUSED(write_port);
30}
31
33{
34 auto ans = (*this)->rbt.Search<FsNode>(name);
35 if (ans && ans->data_.type == FsNodeType::FILE)
36 {
37 return reinterpret_cast<File *>(ans);
38 }
39 else
40 {
41 return nullptr;
42 }
43}
44
46{
47 auto ans = FindFile(name);
48 if (ans)
49 {
50 return ans;
51 }
52
53 struct FindFileRevFn
54 {
55 const char *name;
56 RamFS::File *&ans;
57 ErrorCode operator()(RBTree<const char *>::Node<RamFS::FsNode> &item) const
58 {
59 RamFS::FsNode &node = item;
60 if (node.type == FsNodeType::DIR)
61 {
62 auto *dir = reinterpret_cast<RamFS::Dir *>(&item);
63
64 auto f = dir->FindFile(name);
65 if (f)
66 {
67 ans = f;
68 return ErrorCode::FAILED;
69 }
70
71 dir->data_.rbt.Foreach<RamFS::FsNode>(FindFileRevFn{name, ans});
72 return ans ? ErrorCode::FAILED : ErrorCode::OK;
73 }
74 return ErrorCode::OK;
75 }
76 };
77
78 data_.rbt.Foreach<FsNode>(FindFileRevFn{name, ans});
79 return ans;
80}
81
83{
84 if (name[0] == '.' && name[1] == '\0')
85 {
86 return this;
87 }
88
89 if (name[0] == '.' && name[1] == '.' && name[2] == '\0')
90 {
91 return reinterpret_cast<Dir *>(data_.parent);
92 }
93
94 auto ans = (*this)->rbt.Search<RamFS::FsNode>(name);
95
96 if (ans && (*ans)->type == FsNodeType::DIR)
97 {
98 return reinterpret_cast<Dir *>(ans);
99 }
100 else
101 {
102 return nullptr;
103 }
104}
105
107{
108 auto ans = FindDir(name);
109 if (ans)
110 {
111 return ans;
112 }
113
114 struct FindDirRevFn
115 {
116 const char *name;
117 RamFS::Dir *&ans;
118 ErrorCode operator()(RBTree<const char *>::Node<RamFS::FsNode> &item) const
119 {
120 RamFS::FsNode &node = item;
121 if (node.type == FsNodeType::DIR)
122 {
123 auto *dir = reinterpret_cast<RamFS::Dir *>(&item);
124 if (strcmp(dir->data_.name, name) == 0)
125 {
126 ans = dir;
127 return ErrorCode::FAILED;
128 }
129
130 dir->data_.rbt.Foreach<RamFS::FsNode>(FindDirRevFn{name, ans});
131 return ans ? ErrorCode::FAILED : ErrorCode::OK;
132 }
133 return ErrorCode::OK;
134 }
135 };
136
137 data_.rbt.Foreach<FsNode>(FindDirRevFn{name, ans});
138 return ans;
139}
140
142{
143 auto ans = FindDevice(name);
144 if (ans)
145 {
146 return ans;
147 }
148
149 struct FindDevRevFn
150 {
151 const char *name;
152 RamFS::Device *&ans;
153 ErrorCode operator()(RBTree<const char *>::Node<RamFS::FsNode> &item) const
154 {
155 RamFS::FsNode &node = item;
156 if (node.type == FsNodeType::DIR)
157 {
158 auto *dir = reinterpret_cast<RamFS::Dir *>(&item);
159
160 auto d = dir->FindDevice(name);
161 if (d)
162 {
163 ans = d;
164 return ErrorCode::FAILED;
165 }
166
167 dir->data_.rbt.Foreach<RamFS::FsNode>(FindDevRevFn{name, ans});
168 return ans ? ErrorCode::FAILED : ErrorCode::OK;
169 }
170 return ErrorCode::OK;
171 }
172 };
173
174 data_.rbt.Foreach<FsNode>(FindDevRevFn{name, ans});
175 return ans;
176}
177
186{
187 auto ans = (*this)->rbt.Search<FsNode>(name);
188 if (ans && ans->data_.type == FsNodeType::DEVICE)
189 {
190 return reinterpret_cast<Device *>(ans);
191 }
192 else
193 {
194 return nullptr;
195 }
196}
红黑树的泛型数据节点,继承自 BaseNode (Generic data node for Red-Black Tree, inheriting from BaseNode).
Definition rbt.hpp:64
Data data_
存储的数据 (Stored data).
Definition rbt.hpp:99
红黑树实现,支持泛型键和值,并提供线程安全操作 (Red-Black Tree implementation supporting generic keys and values with thread...
Definition rbt.hpp:24
ErrorCode Foreach(Func func)
遍历红黑树并执行用户提供的操作 (Traverse the Red-Black Tree and apply a user-defined function).
Definition rbt.hpp:273
设备类,继承自红黑树节点 DeviceNode Device class inheriting from Red-Black tree node DeviceNode
Definition ramfs.hpp:160
Device(const char *name, const ReadPort &read_port=ReadPort(), const WritePort &write_port=WritePort())
设备构造函数 Device constructor
Definition ramfs.cpp:20
目录类,继承自 RBTree 节点,用于管理文件、子目录和设备 Directory class, inheriting from RBTree node, used for managing files...
Definition ramfs.hpp:229
Device * FindDeviceRev(const char *name)
递归查找设备 Recursively searches for a device
Definition ramfs.cpp:141
File * FindFile(const char *name)
查找当前目录中的文件 Finds a file in the current directory
Definition ramfs.cpp:32
Dir * FindDir(const char *name)
查找当前目录中的子目录 Finds a subdirectory in the current directory
Definition ramfs.cpp:82
Device * FindDevice(const char *name)
在当前目录中查找设备 Finds a device in the current directory
Definition ramfs.cpp:185
File * FindFileRev(const char *name)
递归查找文件 Recursively searches for a file
Definition ramfs.cpp:45
void Add(File &file)
添加文件到当前目录 Adds a file to the current directory
Definition ramfs.hpp:236
Dir * FindDirRev(const char *name)
递归查找子目录 Recursively searches for a subdirectory
Definition ramfs.cpp:106
int(* exec)(void *raw, int argc, char **argv)
可执行文件指针 Executable function pointer
Definition ramfs.hpp:92
FileType type
文件类型 File type
Definition ramfs.hpp:102
void * arg
可执行文件参数 Executable file argument
Definition ramfs.hpp:99
int Run(int argc, char **argv)
运行可执行文件 Runs an executable file
Definition ramfs.cpp:14
文件系统节点基类,所有文件和目录均继承自该类 Base class for file system nodes; all files and directories inherit from this
Definition ramfs.hpp:73
Dir * FindDir(const char *name)
在整个文件系统中查找目录 Finds a directory in the entire file system
Definition ramfs.hpp:443
Device * FindDevice(const char *name)
在整个文件系统中查找设备 Finds a device in the entire file system
Definition ramfs.hpp:451
RamFS(const char *name="ramfs")
构造函数,初始化内存文件系统的根目录 Constructor that initializes the root directory of the in-memory file system
Definition ramfs.cpp:5
@ DIR
目录 Directory
static int CompareStr(const char *const &a, const char *const &b)
比较两个字符串 Compares two strings
Definition ramfs.cpp:12
Dir dev_
dev 目录,用于存放设备文件 dev directory for storing device files
Definition ramfs.hpp:469
@ EXEC
可执行 Executable
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
File * FindFile(const char *name)
在整个文件系统中查找文件 Finds a file in the entire file system
Definition ramfs.hpp:435
ReadPort class for handling read operations.
Definition libxr_rw.hpp:269
WritePort class for handling write operations.
Definition libxr_rw.hpp:403
LibXR 命名空间
Definition ch32_gpio.hpp:9