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

FatFS inode 操作实现 More...

#include <fatfs.hpp>

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

Public Member Functions

 FatFsInodeOps (FatFsFileSystem *fs)
 
auto Lookup (vfs::Inode *dir, const char *name) -> Expected< vfs::Inode * > override
 在目录中查找指定名称的 inode
 
auto Create (vfs::Inode *dir, const char *name, vfs::FileType type) -> Expected< vfs::Inode * > override
 在目录中创建文件或子目录
 
auto Unlink (vfs::Inode *dir, const char *name) -> Expected< void > override
 删除目录中的文件条目
 
auto Mkdir (vfs::Inode *dir, const char *name) -> Expected< vfs::Inode * > override
 在目录中创建子目录
 
auto Rmdir (vfs::Inode *dir, const char *name) -> Expected< void > override
 删除目录中的空子目录
 
- Public Member Functions inherited from vfs::InodeOps
 InodeOps ()=default
 
 InodeOps (const InodeOps &)=delete
 
 InodeOps (InodeOps &&)=delete
 
auto operator= (const InodeOps &) -> InodeOps &=delete
 
auto operator= (InodeOps &&) -> InodeOps &=delete
 
virtual ~InodeOps ()=default
 

Private Attributes

FatFsFileSystemfs_
 

Detailed Description

FatFS inode 操作实现

Definition at line 130 of file fatfs.hpp.

Constructor & Destructor Documentation

◆ FatFsInodeOps()

fatfs::FatFsFileSystem::FatFsInodeOps::FatFsInodeOps ( FatFsFileSystem fs)
inlineexplicit

Definition at line 132 of file fatfs.hpp.

132: fs_(fs) {}

Member Function Documentation

◆ Create()

auto fatfs::FatFsFileSystem::FatFsInodeOps::Create ( vfs::Inode dir,
const char *  name,
vfs::FileType  type 
) -> Expected<vfs::Inode*>
overridevirtual

在目录中创建文件或子目录

Parameters
dir父目录 inode
name新条目名称
type文件类型(kRegular 或 kDirectory)
Returns
Expected<vfs::Inode*> 新 inode 或错误

Implements vfs::InodeOps.

Definition at line 334 of file fatfs.cpp.

336 {
337 auto* dir_fi = static_cast<FatInode*>(dir->fs_private);
338 std::array<char, kPathBufSize> full_path{};
339 strncpy(full_path.data(), dir_fi->path.data(), full_path.size() - 1);
340 size_t dir_len = strlen(full_path.data());
341 strncpy(full_path.data() + dir_len, name, full_path.size() - dir_len - 1);
342 full_path[full_path.size() - 1] = '\0';
343
344 if (type == vfs::FileType::kDirectory) {
345 FRESULT fr = f_mkdir(full_path.data());
346 if (fr != FR_OK) {
347 return std::unexpected(Error{FresultToErrorCode(fr)});
348 }
349 } else {
350 FIL fil;
351 FRESULT fr = f_open(&fil, full_path.data(), FA_CREATE_NEW | FA_WRITE);
352 if (fr != FR_OK) {
353 return std::unexpected(Error{FresultToErrorCode(fr)});
354 }
355 (void)f_close(&fil);
356 }
357
358 auto inode_result = fs_->AllocateInode();
359 if (!inode_result) {
360 return std::unexpected(inode_result.error());
361 }
362 vfs::Inode* inode = *inode_result;
363 auto* new_fi = static_cast<FatInode*>(inode->fs_private);
364 inode->ino = 0;
365 inode->type = type;
366 inode->size = 0;
368 inode->link_count = 1;
369 strncpy(new_fi->path.data(), full_path.data(), new_fi->path.size() - 1);
370 new_fi->path[new_fi->path.size() - 1] = '\0';
371 return inode;
372}
static constexpr uint32_t kDefaultFilePermissions
普通文件默认权限位
Definition fatfs.hpp:30
auto AllocateInode() -> Expected< vfs::Inode * > override
分配新 inode(由 FatFS FILINFO 快照支撑)
Definition fatfs.cpp:185
@ kDirectory
目录
#define strlen
#define strncpy
错误类型,用于 std::expected
Definition expected.hpp:343
Inode — 文件元数据(独立于路径名)
Definition vfs.hpp:16
uint32_t link_count
硬链接计数
Definition vfs.hpp:26
uint32_t permissions
权限位(简化版)
Definition vfs.hpp:24
uint64_t size
文件大小(字节)
Definition vfs.hpp:22
FileType type
文件类型
Definition vfs.hpp:20
uint64_t ino
inode 编号(文件系统内唯一)
Definition vfs.hpp:18
void * fs_private
文件系统私有数据指针
Definition vfs.hpp:28

◆ Lookup()

auto fatfs::FatFsFileSystem::FatFsInodeOps::Lookup ( vfs::Inode dir,
const char *  name 
) -> Expected<vfs::Inode*>
overridevirtual

在目录中查找指定名称的 inode

Parameters
dir目录 inode
name文件或子目录名称
Returns
Expected<vfs::Inode*> 找到的 inode 或错误

Implements vfs::InodeOps.

Definition at line 301 of file fatfs.cpp.

302 {
303 auto* dir_fi = static_cast<FatInode*>(dir->fs_private);
304
305 // 拼接完整路径:dir_path + name
306 std::array<char, kPathBufSize> full_path{};
307 strncpy(full_path.data(), dir_fi->path.data(), full_path.size() - 1);
308 size_t dir_len = strlen(full_path.data());
309 strncpy(full_path.data() + dir_len, name, full_path.size() - dir_len - 1);
310 full_path[full_path.size() - 1] = '\0';
311
312 FILINFO fi_info;
313 FRESULT fr = f_stat(full_path.data(), &fi_info);
314 if (fr != FR_OK) {
315 return std::unexpected(Error{FresultToErrorCode(fr)});
316 }
317
318 auto inode_result = fs_->AllocateInode();
319 if (!inode_result) {
320 return std::unexpected(inode_result.error());
321 }
322 vfs::Inode* inode = *inode_result;
323 auto* new_fi = static_cast<FatInode*>(inode->fs_private);
324 inode->ino = 0;
325 inode->type = FilInfoToFileType(fi_info);
326 inode->size = fi_info.fsize;
328 inode->link_count = 1;
329 strncpy(new_fi->path.data(), full_path.data(), new_fi->path.size() - 1);
330 new_fi->path[new_fi->path.size() - 1] = '\0';
331 return inode;
332}

◆ Mkdir()

auto fatfs::FatFsFileSystem::FatFsInodeOps::Mkdir ( vfs::Inode dir,
const char *  name 
) -> Expected<vfs::Inode*>
overridevirtual

在目录中创建子目录

Parameters
dir父目录 inode
name子目录名称
Returns
Expected<vfs::Inode*> 新目录 inode 或错误

Implements vfs::InodeOps.

Definition at line 385 of file fatfs.cpp.

386 {
387 return Create(dir, name, vfs::FileType::kDirectory);
388}
auto Create(vfs::Inode *dir, const char *name, vfs::FileType type) -> Expected< vfs::Inode * > override
在目录中创建文件或子目录
Definition fatfs.cpp:334

◆ Rmdir()

auto fatfs::FatFsFileSystem::FatFsInodeOps::Rmdir ( vfs::Inode dir,
const char *  name 
) -> Expected<void>
overridevirtual

删除目录中的空子目录

Parameters
dir父目录 inode
name子目录名称
Returns
Expected<void> 成功或错误

Implements vfs::InodeOps.

Definition at line 390 of file fatfs.cpp.

391 {
392 // f_unlink 同时处理文件和空目录
393 return Unlink(dir, name);
394}
auto Unlink(vfs::Inode *dir, const char *name) -> Expected< void > override
删除目录中的文件条目
Definition fatfs.cpp:374

◆ Unlink()

auto fatfs::FatFsFileSystem::FatFsInodeOps::Unlink ( vfs::Inode dir,
const char *  name 
) -> Expected<void>
overridevirtual

删除目录中的文件条目

Parameters
dir父目录 inode
name文件名称
Returns
Expected<void> 成功或错误

Implements vfs::InodeOps.

Definition at line 374 of file fatfs.cpp.

375 {
376 auto* dir_fi = static_cast<FatInode*>(dir->fs_private);
377 std::array<char, kPathBufSize> full_path{};
378 strncpy(full_path.data(), dir_fi->path.data(), full_path.size() - 1);
379 size_t dir_len = strlen(full_path.data());
380 strncpy(full_path.data() + dir_len, name, full_path.size() - dir_len - 1);
381 full_path[full_path.size() - 1] = '\0';
382 return FresultToExpected(f_unlink(full_path.data()));
383}

Member Data Documentation

◆ fs_

FatFsFileSystem* fatfs::FatFsFileSystem::FatFsInodeOps::fs_
private

Definition at line 179 of file fatfs.hpp.


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