SimpleKernel 1.17.0
Loading...
Searching...
No Matches
fatfs::FatFsFileSystem Class Reference

FatFS VFS 适配器 More...

#include <fatfs.hpp>

Inheritance diagram for fatfs::FatFsFileSystem:
Inheritance graph
Collaboration diagram for fatfs::FatFsFileSystem:
Collaboration graph

Classes

struct  FatDirHandle
 DIR 对象池条目 More...
 
struct  FatFileHandle
 FIL 对象池 More...
 
class  FatFsFileOps
 FatFS 文件操作实现 More...
 
class  FatFsInodeOps
 FatFS inode 操作实现 More...
 
struct  FatInode
 FatFS inode 私有数据 More...
 

Public Member Functions

 FatFsFileSystem (uint8_t volume_id)
 构造函数,绑定到指定 FatFS 卷号
 
auto GetName () const -> const char *override
 返回 "fatfs"
 
auto Mount (vfs::BlockDevice *device) -> Expected< vfs::Inode * > override
 挂载 FatFS 卷
 
auto Unmount () -> Expected< void > override
 卸载 FatFS 卷
 
auto Sync () -> Expected< void > override
 刷新所有脏缓冲区
 
auto AllocateInode () -> Expected< vfs::Inode * > override
 分配新 inode(由 FatFS FILINFO 快照支撑)
 
auto FreeInode (vfs::Inode *inode) -> Expected< void > override
 释放 inode
 
auto GetFileOps () -> vfs::FileOps *override
 返回本文件系统的 FileOps 实例
 
auto OpenFil (vfs::Inode *inode, vfs::OpenFlags open_flags) -> Expected< void >
 为 inode 打开底层 FatFS FIL 对象
 
构造/析构函数
 ~FatFsFileSystem () override
 
 FatFsFileSystem (const FatFsFileSystem &)=delete
 
 FatFsFileSystem (FatFsFileSystem &&)=delete
 
auto operator= (const FatFsFileSystem &) -> FatFsFileSystem &=delete
 
auto operator= (FatFsFileSystem &&) -> FatFsFileSystem &=delete
 
- Public Member Functions inherited from vfs::FileSystem
virtual ~FileSystem ()=default
 

Static Public Member Functions

static auto SetBlockDevice (uint8_t pdrv, vfs::BlockDevice *device) -> void
 注册块设备(由 Mount 调用,供 diskio.cpp 的 C 回调使用)
 
static auto GetBlockDevice (uint8_t pdrv) -> vfs::BlockDevice *
 获取指定驱动器的块设备
 

Static Public Attributes

static constexpr uint32_t kRootDirPermissions = 0755U
 根目录权限位
 
static constexpr uint32_t kDefaultFilePermissions = 0644U
 普通文件默认权限位
 
static constexpr size_t kPathBufSize = 512
 路径缓冲区大小(字节)
 
static constexpr size_t kMaxInodes = 256
 inode 池容量
 
static constexpr size_t kMaxOpenFiles = 16
 同时打开文件数上限
 
static constexpr size_t kMaxOpenDirs = 8
 同时打开目录数上限
 

Private Member Functions

auto AllocateFatInode () -> FatInode *
 从 inode 池中分配一个空闲槽位
 
auto FreeFatInode (FatInode *fi) -> void
 释放 inode 池槽位
 
auto AllocateFil () -> FIL *
 从 FIL 池中分配一个空闲 FIL 对象
 
auto FreeFil (FIL *fil) -> void
 归还 FIL 对象到池中
 
auto AllocateDir () -> DIR *
 从 DIR 池中分配一个空闲 DIR 对象
 
auto FreeDir (DIR *dir) -> void
 归还 DIR 对象到池中
 

Private Attributes

uint8_t volume_id_
 FatFS 逻辑驱动器号
 
FATFS fatfs_obj_
 FatFS 文件系统对象(每卷一个)
 
vfs::Inoderoot_inode_ = nullptr
 根目录 inode(Mount 时设置)
 
bool mounted_ {false}
 当前卷是否已挂载
 
std::array< FatInode, kMaxInodesinodes_
 
std::array< FatFileHandle, kMaxOpenFilesfil_pool_
 
std::array< FatDirHandle, kMaxOpenDirsdir_pool_
 
FatFsInodeOps inode_ops_
 inode 操作单例
 
FatFsFileOps file_ops_
 文件操作单例
 

Static Private Attributes

static std::array< vfs::BlockDevice *, FF_VOLUMESblock_devices_ {}
 每卷块设备注册表(静态,供 diskio.cpp C 回调访问)
 

Friends

class FatFsInodeOps
 
class FatFsFileOps
 

Detailed Description

FatFS VFS 适配器

将 FatFS (f_mount / f_open / f_read / ...) 封装在 vfs::FileSystem 接口后。每个 FatFsFileSystem 实例独占一个 FatFS 逻辑驱动器(卷)。

Precondition
FF_VOLUMES >= 1 (ffconf.h)

Definition at line 25 of file fatfs.hpp.

Constructor & Destructor Documentation

◆ FatFsFileSystem() [1/3]

fatfs::FatFsFileSystem::FatFsFileSystem ( uint8_t  volume_id)
explicit

构造函数,绑定到指定 FatFS 卷号

Parameters
volume_idFatFS 逻辑驱动器编号 (0 .. FF_VOLUMES-1)
Precondition
volume_id < FF_VOLUMES

Definition at line 92 of file fatfs.cpp.

93 : volume_id_(volume_id),
94 fatfs_obj_{},
95 inodes_{},
96 fil_pool_{},
97 dir_pool_{},
98 inode_ops_(this),
99 file_ops_(this) {}
uint8_t volume_id_
FatFS 逻辑驱动器号
Definition fatfs.hpp:245
std::array< FatFileHandle, kMaxOpenFiles > fil_pool_
Definition fatfs.hpp:274
FatFsInodeOps inode_ops_
inode 操作单例
Definition fatfs.hpp:285
FatFsFileOps file_ops_
文件操作单例
Definition fatfs.hpp:287
std::array< FatDirHandle, kMaxOpenDirs > dir_pool_
Definition fatfs.hpp:282
std::array< FatInode, kMaxInodes > inodes_
Definition fatfs.hpp:266
FATFS fatfs_obj_
FatFS 文件系统对象(每卷一个)
Definition fatfs.hpp:247

◆ ~FatFsFileSystem()

fatfs::FatFsFileSystem::~FatFsFileSystem ( )
override

Definition at line 101 of file fatfs.cpp.

101 {
102 if (mounted_) {
103 (void)Unmount();
104 }
105}
bool mounted_
当前卷是否已挂载
Definition fatfs.hpp:251
auto Unmount() -> Expected< void > override
卸载 FatFS 卷
Definition fatfs.cpp:155
Here is the call graph for this function:

◆ FatFsFileSystem() [2/3]

fatfs::FatFsFileSystem::FatFsFileSystem ( const FatFsFileSystem )
delete

◆ FatFsFileSystem() [3/3]

fatfs::FatFsFileSystem::FatFsFileSystem ( FatFsFileSystem &&  )
delete

Member Function Documentation

◆ AllocateDir()

auto fatfs::FatFsFileSystem::AllocateDir ( ) -> DIR*
private

从 DIR 池中分配一个空闲 DIR 对象

Returns
DIR* 成功返回指针,池满返回 nullptr

Definition at line 244 of file fatfs.cpp.

244 {
245 for (auto& dh : dir_pool_) {
246 if (!dh.in_use) {
247 dh = FatDirHandle{};
248 dh.in_use = true;
249 return &dh.dir;
250 }
251 }
252 return nullptr;
253}

◆ AllocateFatInode()

auto fatfs::FatFsFileSystem::AllocateFatInode ( ) -> FatInode*
private

从 inode 池中分配一个空闲槽位

Returns
FatInode* 成功返回指针,池满返回 nullptr

Definition at line 207 of file fatfs.cpp.

207 {
208 for (auto& fi : inodes_) {
209 if (!fi.in_use) {
210 fi = FatInode{};
211 fi.in_use = true;
212 return &fi;
213 }
214 }
215 return nullptr;
216}

◆ AllocateFil()

auto fatfs::FatFsFileSystem::AllocateFil ( ) -> FIL*
private

从 FIL 池中分配一个空闲 FIL 对象

Returns
FIL* 成功返回指针,池满返回 nullptr

Definition at line 224 of file fatfs.cpp.

224 {
225 for (auto& fh : fil_pool_) {
226 if (!fh.in_use) {
227 fh = FatFileHandle{};
228 fh.in_use = true;
229 return &fh.fil;
230 }
231 }
232 return nullptr;
233}

◆ AllocateInode()

auto fatfs::FatFsFileSystem::AllocateInode ( ) -> Expected<vfs::Inode*>
overridevirtual

分配新 inode(由 FatFS FILINFO 快照支撑)

Returns
Expected<vfs::Inode*> 新 inode 或错误

Implements vfs::FileSystem.

Definition at line 185 of file fatfs.cpp.

185 {
186 FatInode* fi = AllocateFatInode();
187 if (fi == nullptr) {
188 return std::unexpected(Error{ErrorCode::kOutOfMemory});
189 }
190 fi->inode.fs = this;
191 fi->inode.ops = &inode_ops_;
192 fi->inode.fs_private = fi;
193 return &fi->inode;
194}
auto AllocateFatInode() -> FatInode *
从 inode 池中分配一个空闲槽位
Definition fatfs.cpp:207
错误类型,用于 std::expected
Definition expected.hpp:343

◆ FreeDir()

auto fatfs::FatFsFileSystem::FreeDir ( DIR *  dir) -> void
private

归还 DIR 对象到池中

Parameters
dir要归还的 DIR 指针

Definition at line 255 of file fatfs.cpp.

255 {
256 for (auto& dh : dir_pool_) {
257 if (&dh.dir == dir) {
258 dh.in_use = false;
259 return;
260 }
261 }
262}

◆ FreeFatInode()

auto fatfs::FatFsFileSystem::FreeFatInode ( FatInode fi) -> void
private

释放 inode 池槽位

Parameters
fi要释放的槽位,允许为 nullptr(此时无操作)

Definition at line 218 of file fatfs.cpp.

218 {
219 if (fi != nullptr) {
220 fi->in_use = false;
221 }
222}

◆ FreeFil()

auto fatfs::FatFsFileSystem::FreeFil ( FIL *  fil) -> void
private

归还 FIL 对象到池中

Parameters
fil要归还的 FIL 指针

Definition at line 235 of file fatfs.cpp.

235 {
236 for (auto& fh : fil_pool_) {
237 if (&fh.fil == fil) {
238 fh.in_use = false;
239 return;
240 }
241 }
242}

◆ FreeInode()

auto fatfs::FatFsFileSystem::FreeInode ( vfs::Inode inode) -> Expected<void>
overridevirtual

释放 inode

Parameters
inode要释放的 inode,不能为 nullptr
Returns
Expected<void> 成功或错误
Precondition
inode != nullptr
inode->link_count == 0

Implements vfs::FileSystem.

Definition at line 196 of file fatfs.cpp.

196 {
197 if (inode == nullptr) {
198 return std::unexpected(Error{ErrorCode::kInvalidArgument});
199 }
200 auto* fi = static_cast<FatInode*>(inode->fs_private);
201 FreeFatInode(fi);
202 return {};
203}
auto FreeFatInode(FatInode *fi) -> void
释放 inode 池槽位
Definition fatfs.cpp:218
@ kInvalidArgument
void * fs_private
文件系统私有数据指针
Definition vfs.hpp:28

◆ GetBlockDevice()

auto fatfs::FatFsFileSystem::GetBlockDevice ( uint8_t  pdrv) -> vfs::BlockDevice*
static

获取指定驱动器的块设备

Parameters
pdrvFatFS 物理驱动器号
Returns
vfs::BlockDevice* 若未注册则返回 nullptr

Definition at line 85 of file fatfs.cpp.

85 {
86 if (pdrv >= FF_VOLUMES) {
87 return nullptr;
88 }
89 return block_devices_[pdrv];
90}
static std::array< vfs::BlockDevice *, FF_VOLUMES > block_devices_
每卷块设备注册表(静态,供 diskio.cpp C 回调访问)
Definition fatfs.hpp:290
#define FF_VOLUMES
Definition ffconf.h:33
Here is the caller graph for this function:

◆ GetFileOps()

auto fatfs::FatFsFileSystem::GetFileOps ( ) -> vfs::FileOps*
overridevirtual

返回本文件系统的 FileOps 实例

Implements vfs::FileSystem.

Definition at line 205 of file fatfs.cpp.

205{ return &file_ops_; }

◆ GetName()

auto fatfs::FatFsFileSystem::GetName ( ) const -> const char*
overridevirtual

返回 "fatfs"

Implements vfs::FileSystem.

Definition at line 107 of file fatfs.cpp.

107{ return "fatfs"; }

◆ Mount()

auto fatfs::FatFsFileSystem::Mount ( vfs::BlockDevice device) -> Expected<vfs::Inode*>
overridevirtual

挂载 FatFS 卷

Parameters
device提供存储的块设备,不能为 nullptr
Returns
Expected<vfs::Inode*> 根目录 inode 或错误
Precondition
device != nullptr
Postcondition
返回的 inode->type == vfs::FileType::kDirectory

Implements vfs::FileSystem.

Definition at line 109 of file fatfs.cpp.

109 {
110 if (device == nullptr) {
111 klog::Err("FatFsFileSystem::Mount: device is nullptr");
112 return std::unexpected(Error{ErrorCode::kInvalidArgument});
113 }
114 if (volume_id_ >= FF_VOLUMES) {
115 return std::unexpected(Error{ErrorCode::kInvalidArgument});
116 }
117
118 // 注册块设备,使 diskio.cpp 的回调能够访问它
119 SetBlockDevice(volume_id_, device);
120
121 // 构造 FatFS 挂载路径:"0:/" 或 "1:/" 等
122 char path[4] = {static_cast<char>('0' + volume_id_), ':', '/', '\0'};
123
124 FRESULT fr = f_mount(&fatfs_obj_, path, 1);
125 if (fr != FR_OK) {
126 SetBlockDevice(volume_id_, nullptr);
127 klog::Err("FatFsFileSystem::Mount: f_mount failed ({})",
128 static_cast<int>(fr));
129 return std::unexpected(Error{FresultToErrorCode(fr)});
130 }
131
132 // 构建根 inode
133 FatInode* fi = AllocateFatInode();
134 if (fi == nullptr) {
135 (void)f_mount(nullptr, path, 0);
136 SetBlockDevice(volume_id_, nullptr);
137 return std::unexpected(Error{ErrorCode::kOutOfMemory});
138 }
139 fi->inode.ino = 0;
140 fi->inode.type = vfs::FileType::kDirectory;
141 fi->inode.size = 0;
142 fi->inode.permissions = kRootDirPermissions;
143 fi->inode.link_count = 1;
144 fi->inode.fs = this;
145 fi->inode.ops = &inode_ops_;
146 fi->inode.fs_private = fi;
147 strncpy(fi->path.data(), path, fi->path.size() - 1);
148 fi->path[fi->path.size() - 1] = '\0';
149
150 root_inode_ = &fi->inode;
151 mounted_ = true;
152 return root_inode_;
153}
static auto SetBlockDevice(uint8_t pdrv, vfs::BlockDevice *device) -> void
注册块设备(由 Mount 调用,供 diskio.cpp 的 C 回调使用)
Definition fatfs.cpp:78
static constexpr uint32_t kRootDirPermissions
根目录权限位
Definition fatfs.hpp:28
vfs::Inode * root_inode_
根目录 inode(Mount 时设置)
Definition fatfs.hpp:249
auto Err(etl::format_string< Args... > fmt, Args &&... args) -> void
以 ERROR 级别记录日志
@ kDirectory
目录
#define strncpy
Here is the call graph for this function:
Here is the caller graph for this function:

◆ OpenFil()

auto fatfs::FatFsFileSystem::OpenFil ( vfs::Inode inode,
vfs::OpenFlags  open_flags 
) -> Expected<void>

为 inode 打开底层 FatFS FIL 对象

Parameters
inode要打开的 inode(不能为 nullptr,类型必须为 kRegular)
open_flagsvfs OpenFlags 位掩码
Returns
Expected<void> 成功或错误
Precondition
inode != nullptr && inode->type == vfs::FileType::kRegular
Postcondition
成功时 inode->fs_private->fil != nullptr

Definition at line 264 of file fatfs.cpp.

265 {
266 auto* fi = static_cast<FatInode*>(inode->fs_private);
267 if (fi->fil != nullptr) {
268 // 已打开
269 return {};
270 }
271 FIL* fil = AllocateFil();
272 if (fil == nullptr) {
273 return std::unexpected(Error{ErrorCode::kFsFdTableFull});
274 }
275 BYTE fa_mode = 0;
276 // kOReadOnly == 0,需单独检查
277 if (open_flags == vfs::OpenFlags::kOReadOnly) {
278 fa_mode = FA_READ;
279 }
280 if ((open_flags & vfs::OpenFlags::kOWriteOnly) != 0U) {
281 fa_mode = FA_WRITE;
282 }
283 if ((open_flags & vfs::OpenFlags::kOReadWrite) != 0U) {
284 fa_mode = FA_READ | FA_WRITE;
285 }
286 if ((open_flags & vfs::OpenFlags::kOCreate) != 0U) {
287 fa_mode |= FA_OPEN_ALWAYS;
288 }
289 if ((open_flags & vfs::OpenFlags::kOTruncate) != 0U) {
290 fa_mode |= FA_CREATE_ALWAYS;
291 }
292 FRESULT fr = f_open(fil, fi->path.data(), fa_mode);
293 if (fr != FR_OK) {
294 FreeFil(fil);
295 return std::unexpected(Error{FresultToErrorCode(fr)});
296 }
297 fi->fil = fil;
298 return {};
299}
auto FreeFil(FIL *fil) -> void
归还 FIL 对象到池中
Definition fatfs.cpp:235
auto AllocateFil() -> FIL *
从 FIL 池中分配一个空闲 FIL 对象
Definition fatfs.cpp:224

◆ operator=() [1/2]

auto fatfs::FatFsFileSystem::operator= ( const FatFsFileSystem ) -> FatFsFileSystem &=delete
delete

◆ operator=() [2/2]

auto fatfs::FatFsFileSystem::operator= ( FatFsFileSystem &&  ) -> FatFsFileSystem &=delete
delete

◆ SetBlockDevice()

auto fatfs::FatFsFileSystem::SetBlockDevice ( uint8_t  pdrv,
vfs::BlockDevice device 
) -> void
static

注册块设备(由 Mount 调用,供 diskio.cpp 的 C 回调使用)

Parameters
pdrvFatFS 物理驱动器号(== volume_id)
device块设备指针(nullptr 表示注销)

Definition at line 78 of file fatfs.cpp.

79 {
80 if (pdrv < FF_VOLUMES) {
81 block_devices_[pdrv] = device;
82 }
83}

◆ Sync()

auto fatfs::FatFsFileSystem::Sync ( ) -> Expected<void>
overridevirtual

刷新所有脏缓冲区

Returns
Expected<void> 成功或错误

Implements vfs::FileSystem.

Definition at line 177 of file fatfs.cpp.

177 {
178 auto* dev = GetBlockDevice(volume_id_);
179 if (dev != nullptr) {
180 return dev->Flush();
181 }
182 return {};
183}
static auto GetBlockDevice(uint8_t pdrv) -> vfs::BlockDevice *
获取指定驱动器的块设备
Definition fatfs.cpp:85

◆ Unmount()

auto fatfs::FatFsFileSystem::Unmount ( ) -> Expected<void>
overridevirtual

卸载 FatFS 卷

Returns
Expected<void> 成功或错误
Precondition
没有引用本卷的打开文件对象

Implements vfs::FileSystem.

Definition at line 155 of file fatfs.cpp.

155 {
156 if (!mounted_) {
157 return {};
158 }
159 char path[4] = {static_cast<char>('0' + volume_id_), ':', '/', '\0'};
160 FRESULT fr = f_mount(nullptr, path, 0);
161 SetBlockDevice(volume_id_, nullptr);
162 mounted_ = false;
163 root_inode_ = nullptr;
164 for (auto& node : inodes_) {
165 if (node.in_use) {
166 if (node.dir != nullptr) {
167 (void)f_closedir(node.dir);
168 FreeDir(node.dir);
169 node.dir = nullptr;
170 }
171 }
172 node.in_use = false;
173 }
174 return FresultToExpected(fr);
175}
auto FreeDir(DIR *dir) -> void
归还 DIR 对象到池中
Definition fatfs.cpp:255
Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ FatFsFileOps

friend class FatFsFileOps
friend

Definition at line 241 of file fatfs.hpp.

◆ FatFsInodeOps

friend class FatFsInodeOps
friend

Definition at line 240 of file fatfs.hpp.

Member Data Documentation

◆ block_devices_

std::array< vfs::BlockDevice *, FF_VOLUMES > fatfs::FatFsFileSystem::block_devices_ {}
staticprivate

每卷块设备注册表(静态,供 diskio.cpp C 回调访问)

Definition at line 290 of file fatfs.hpp.

◆ dir_pool_

std::array<FatDirHandle, kMaxOpenDirs> fatfs::FatFsFileSystem::dir_pool_
private

Definition at line 282 of file fatfs.hpp.

◆ fatfs_obj_

FATFS fatfs::FatFsFileSystem::fatfs_obj_
private

FatFS 文件系统对象(每卷一个)

Definition at line 247 of file fatfs.hpp.

◆ fil_pool_

std::array<FatFileHandle, kMaxOpenFiles> fatfs::FatFsFileSystem::fil_pool_
private

Definition at line 274 of file fatfs.hpp.

◆ file_ops_

FatFsFileOps fatfs::FatFsFileSystem::file_ops_
private

文件操作单例

Definition at line 287 of file fatfs.hpp.

◆ inode_ops_

FatFsInodeOps fatfs::FatFsFileSystem::inode_ops_
private

inode 操作单例

Definition at line 285 of file fatfs.hpp.

◆ inodes_

std::array<FatInode, kMaxInodes> fatfs::FatFsFileSystem::inodes_
private

Definition at line 266 of file fatfs.hpp.

◆ kDefaultFilePermissions

constexpr uint32_t fatfs::FatFsFileSystem::kDefaultFilePermissions = 0644U
staticconstexpr

普通文件默认权限位

Definition at line 30 of file fatfs.hpp.

◆ kMaxInodes

constexpr size_t fatfs::FatFsFileSystem::kMaxInodes = 256
staticconstexpr

inode 池容量

Definition at line 34 of file fatfs.hpp.

◆ kMaxOpenDirs

constexpr size_t fatfs::FatFsFileSystem::kMaxOpenDirs = 8
staticconstexpr

同时打开目录数上限

Definition at line 38 of file fatfs.hpp.

◆ kMaxOpenFiles

constexpr size_t fatfs::FatFsFileSystem::kMaxOpenFiles = 16
staticconstexpr

同时打开文件数上限

Definition at line 36 of file fatfs.hpp.

◆ kPathBufSize

constexpr size_t fatfs::FatFsFileSystem::kPathBufSize = 512
staticconstexpr

路径缓冲区大小(字节)

Definition at line 32 of file fatfs.hpp.

◆ kRootDirPermissions

constexpr uint32_t fatfs::FatFsFileSystem::kRootDirPermissions = 0755U
staticconstexpr

根目录权限位

Definition at line 28 of file fatfs.hpp.

◆ mounted_

bool fatfs::FatFsFileSystem::mounted_ {false}
private

当前卷是否已挂载

Definition at line 251 of file fatfs.hpp.

251{false};

◆ root_inode_

vfs::Inode* fatfs::FatFsFileSystem::root_inode_ = nullptr
private

根目录 inode(Mount 时设置)

Definition at line 249 of file fatfs.hpp.

◆ volume_id_

uint8_t fatfs::FatFsFileSystem::volume_id_
private

FatFS 逻辑驱动器号

Definition at line 245 of file fatfs.hpp.


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