Libraries/FatIFS/src/include/IFS/FAT/FileSystem.h
Go to the documentation of this file.
1 /****
2  * FileSystem.h - Provides an IFS FileSystem implementation for FAT.
3  *
4  * This file is part of the FatIFS Library
5  *
6  * This library is free software: you can redistribute it and/or modify it under the terms of the
7  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along with this library.
14  * If not, see <https://www.gnu.org/licenses/>.
15  *
16  */
17 
18 #pragma once
19 
20 #include <IFS/FileSystem.h>
21 
22 namespace IFS
23 {
24 namespace FAT
25 {
26 // File handles start at this value
27 #ifndef FATFS_HANDLE_MIN
28 #define FATFS_HANDLE_MIN 300
29 #endif
30 
31 // Maximum number of file descriptors
32 #ifndef FATFS_MAX_FDS
33 #define FATFS_MAX_FDS 5
34 #endif
35 
36 // Maximum file handle value
37 #define FATFS_HANDLE_MAX (FATFS_HANDLE_MIN + FATFS_MAX_FDS - 1)
38 
39 struct S_FATFS;
40 struct S_FILINFO;
41 struct FileDescriptor;
42 
46 class FileSystem : public IFileSystem
47 {
48 public:
51 
52  int mount() override;
53  int getinfo(Info& info) override;
54  int setProfiler(IProfiler* profiler) override;
55  String getErrorString(int err) override;
56  int opendir(const char* path, DirHandle& dir) override;
57  int readdir(DirHandle dir, Stat& stat) override;
58  int rewinddir(DirHandle dir) override;
59  int closedir(DirHandle dir) override;
60  int mkdir(const char* path) override;
61  int stat(const char* path, Stat* stat) override;
62  int fstat(FileHandle file, Stat* stat) override;
63  int fcontrol(FileHandle file, ControlCode code, void* buffer, size_t bufSize) override;
64  int fsetxattr(FileHandle file, AttributeTag tag, const void* data, size_t size) override;
65  int fgetxattr(FileHandle file, AttributeTag tag, void* buffer, size_t size) override;
66  int fenumxattr(FileHandle file, AttributeEnumCallback callback, void* buffer, size_t bufsize) override;
67  int setxattr(const char* path, AttributeTag tag, const void* data, size_t size) override;
68  int getxattr(const char* path, AttributeTag tag, void* buffer, size_t size) override;
69  FileHandle open(const char* path, OpenFlags flags) override;
70  int close(FileHandle file) override;
71  int read(FileHandle file, void* data, size_t size) override;
72  int write(FileHandle file, const void* data, size_t size) override;
73  file_offset_t lseek(FileHandle file, file_offset_t offset, SeekOrigin origin) override;
74  int eof(FileHandle file) override;
75  file_offset_t tell(FileHandle file) override;
76  int ftruncate(FileHandle file, file_size_t new_size) override;
77  int flush(FileHandle file) override;
78  int rename(const char* oldpath, const char* newpath) override;
79  int remove(const char* path) override;
80  int fremove(FileHandle file) override;
81  int format() override;
82  int check() override;
83 
84  bool read_sector(void* buff, uint32_t sector, size_t count);
85  bool write_sector(const void* buff, uint32_t sector, size_t count);
86  bool ioctl(uint8_t cmd, void* buff);
87 
88  S_FATFS* getFatFS() const
89  {
90  return fatfs.get();
91  }
92 
93 private:
94  void fillStat(Stat& stat, const S_FILINFO& inf);
95 
96  Storage::Partition partition;
97  IProfiler* profiler{nullptr};
98  std::unique_ptr<S_FATFS> fatfs;
99  std::unique_ptr<FileDescriptor> fileDescriptors[FATFS_MAX_FDS];
100  ACL rootAcl{};
101  uint8_t sectorSizeShift{0};
102 };
103 
104 } // namespace FAT
105 } // namespace IFS
int32_t file_offset_t
Definition: Components/IFS/src/include/IFS/Types.h:51
uint32_t file_size_t
Definition: Components/IFS/src/include/IFS/Types.h:50
#define FATFS_MAX_FDS
Definition: Libraries/FatIFS/src/include/IFS/FAT/FileSystem.h:33
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
Manage a set of bit values using enumeration.
Definition: BitSet.h:45
Definition: Delegate.h:20
Definition: Libraries/FatIFS/src/include/IFS/FAT/FileSystem.h:47
int setProfiler(IProfiler *profiler) override
Set profiler instance to enable debugging and performance assessment.
int setxattr(const char *path, AttributeTag tag, const void *data, size_t size) override
Set an extended attribute for a file given its path.
int write(FileHandle file, const void *data, size_t size) override
write content to a file at current position and advance cursor
int mkdir(const char *path) override
Create a directory.
int closedir(DirHandle dir) override
close a directory object
S_FATFS * getFatFS() const
Definition: Libraries/FatIFS/src/include/IFS/FAT/FileSystem.h:88
bool write_sector(const void *buff, uint32_t sector, size_t count)
int close(FileHandle file) override
close an open file
int remove(const char *path) override
remove (delete) a file by path
int stat(const char *path, Stat *stat) override
get file information
int rewinddir(DirHandle dir) override
Reset directory read position to start.
int format() override
format the filing system
int mount() override
Mount file system, performing any required initialisation.
file_offset_t tell(FileHandle file) override
get current file position
int eof(FileHandle file) override
determine if current file position is at end of file
bool ioctl(uint8_t cmd, void *buff)
int fstat(FileHandle file, Stat *stat) override
get file information
int fremove(FileHandle file) override
remove (delete) a file by handle
FileSystem(Storage::Partition partition)
int ftruncate(FileHandle file, file_size_t new_size) override
Truncate (reduce) the size of an open file.
int getxattr(const char *path, AttributeTag tag, void *buffer, size_t size) override
Get an attribute from a file given its path.
String getErrorString(int err) override
get the text for a returned error code
int getinfo(Info &info) override
get filing system information
int readdir(DirHandle dir, Stat &stat) override
read a directory entry
bool read_sector(void *buff, uint32_t sector, size_t count)
file_offset_t lseek(FileHandle file, file_offset_t offset, SeekOrigin origin) override
change file read/write position
int read(FileHandle file, void *data, size_t size) override
read content from a file and advance cursor
int fenumxattr(FileHandle file, AttributeEnumCallback callback, void *buffer, size_t bufsize) override
Enumerate attributes.
int fgetxattr(FileHandle file, AttributeTag tag, void *buffer, size_t size) override
Get an extended attribute from an open file.
int opendir(const char *path, DirHandle &dir) override
open a directory for reading
int fsetxattr(FileHandle file, AttributeTag tag, const void *data, size_t size) override
Set an extended attribute on an open file.
int flush(FileHandle file) override
flush any buffered data to physical media
int rename(const char *oldpath, const char *newpath) override
rename a file
int fcontrol(FileHandle file, ControlCode code, void *buffer, size_t bufSize) override
Low-level and non-standard file control operations.
int check() override
Perform a file system consistency check.
FileHandle open(const char *path, OpenFlags flags) override
open a file (or directory) by path
Installable File System base class.
Definition: IFileSystem.h:100
Filesystems may optionally provide performance statistics.
Definition: Profiler.h:31
Represents a flash partition.
Definition: Partition.h:86
The String class.
Definition: WString.h:137
Definition: DirectoryTemplate.h:37
ControlCode
See IFS::IFileSystem::fcontrol
Definition: Components/IFS/src/include/IFS/Control.h:31
struct ImplFileDir * DirHandle
Definition: IFileSystem.h:72
AttributeTag
Identifies a specific attribute.
Definition: Attribute.h:45
int16_t FileHandle
File handle.
Definition: Stat.h:40
Definition: Access.h:34
Basic information about filing system.
Definition: IFileSystem.h:122
File Status structure.
Definition: Stat.h:52