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:
49  FileSystem(Storage::Partition partition);
50  ~FileSystem();
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 fsetxattr(FileHandle file, AttributeTag tag, const void* data, size_t size) override;
64  int fgetxattr(FileHandle file, AttributeTag tag, void* buffer, size_t size) override;
65  int fenumxattr(FileHandle file, AttributeEnumCallback callback, void* buffer, size_t bufsize) override;
66  int setxattr(const char* path, AttributeTag tag, const void* data, size_t size) override;
67  int getxattr(const char* path, AttributeTag tag, void* buffer, size_t size) override;
68  FileHandle open(const char* path, OpenFlags flags) override;
69  int close(FileHandle file) override;
70  int read(FileHandle file, void* data, size_t size) override;
71  int write(FileHandle file, const void* data, size_t size) override;
72  file_offset_t lseek(FileHandle file, file_offset_t offset, SeekOrigin origin) override;
73  int eof(FileHandle file) override;
74  file_offset_t tell(FileHandle file) override;
75  int ftruncate(FileHandle file, file_size_t new_size) override;
76  int flush(FileHandle file) override;
77  int rename(const char* oldpath, const char* newpath) override;
78  int remove(const char* path) override;
79  int fremove(FileHandle file) override;
80  int format() override;
81  int check() override;
82 
83  bool read_sector(void* buff, uint32_t sector, size_t count);
84  bool write_sector(const void* buff, uint32_t sector, size_t count);
85  bool ioctl(uint8_t cmd, void* buff);
86 
87  S_FATFS* getFatFS() const
88  {
89  return fatfs.get();
90  }
91 
92 private:
93  void fillStat(Stat& stat, const S_FILINFO& inf);
94 
95  Storage::Partition partition;
96  IProfiler* profiler{nullptr};
97  std::unique_ptr<S_FATFS> fatfs;
98  std::unique_ptr<FileDescriptor> fileDescriptors[FATFS_MAX_FDS];
99  ACL rootAcl{};
100  uint8_t sectorSizeShift{0};
101 };
102 
103 } // namespace FAT
104 } // namespace IFS
Represents a flash partition.
Definition: Partition.h:85
int rewinddir(DirHandle dir) override
Reset directory read position to start.
int rename(const char *oldpath, const char *newpath) override
rename a file
int getxattr(const char *path, AttributeTag tag, void *buffer, size_t size) override
Get an attribute from a file given its path.
int close(FileHandle file) override
close an open file
int mount() override
Mount file system, performing any required initialisation.
int fgetxattr(FileHandle file, AttributeTag tag, void *buffer, size_t size) override
Get an extended attribute from an open file.
bool ioctl(uint8_t cmd, void *buff)
int opendir(const char *path, DirHandle &dir) override
open a directory for reading
The String class.
Definition: WString.h:136
file_offset_t tell(FileHandle file) override
get current file position
int fenumxattr(FileHandle file, AttributeEnumCallback callback, void *buffer, size_t bufsize) override
Enumerate attributes.
int eof(FileHandle file) override
determine if current file position is at end of file
bool write_sector(const void *buff, uint32_t sector, size_t count)
int read(FileHandle file, void *data, size_t size) override
read content from a file and advance cursor
Delegate< bool(AttributeEnum &e)> AttributeEnumCallback
Return true to continue enumeration, false to stop.
Definition: Attribute.h:102
BitSet< uint8_t, OpenFlag, size_t(OpenFlag::MAX)> OpenFlags
Definition: OpenFlags.h:69
int flush(FileHandle file) override
flush any buffered data to physical media
int fremove(FileHandle file) override
remove (delete) a file by handle
int16_t FileHandle
File handle.
Definition: Stat.h:59
Definition: DirectoryTemplate.h:36
Filesystems may optionally provide performance statistics.
Definition: Profiler.h:47
int mkdir(const char *path) override
Create a directory.
int closedir(DirHandle dir) override
close a directory object
int32_t file_offset_t
Definition: Components/IFS/src/include/IFS/Types.h:51
File Status structure.
Definition: Stat.h:71
int format() override
format the filing system
bool read_sector(void *buff, uint32_t sector, size_t count)
int getinfo(Info &info) override
get filing system information
int readdir(DirHandle dir, Stat &stat) override
read a directory entry
Definition: Access.h:54
int setProfiler(IProfiler *profiler) override
Set profiler instance to enable debugging and performance assessment.
int stat(const char *path, Stat *stat) override
get file information
FileHandle open(const char *path, OpenFlags flags) override
open a file (or directory) by path
String getErrorString(int err) override
get the text for a returned error code
file_offset_t lseek(FileHandle file, file_offset_t offset, SeekOrigin origin) override
change file read/write position
FileSystem(Storage::Partition partition)
int setxattr(const char *path, AttributeTag tag, const void *data, size_t size) override
Set an extended attribute for a file given its path.
AttributeTag
Identifies a specific attribute.
Definition: Attribute.h:64
IFS::FileHandle FileHandle
Definition: Core/FileSystem.h:24
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
int write(FileHandle file, const void *data, size_t size) override
write content to a file at current position and advance cursor
int fsetxattr(FileHandle file, AttributeTag tag, const void *data, size_t size) override
Set an extended attribute on an open file.
int ftruncate(FileHandle file, file_size_t new_size) override
Truncate (reduce) the size of an open file.
S_FATFS * getFatFS() const
Definition: Libraries/FatIFS/src/include/IFS/FAT/FileSystem.h:117
int fstat(FileHandle file, Stat *stat) override
get file information
IFS::DirHandle DirHandle
Definition: Core/FileSystem.h:25
int check() override
Perform a file system consistency check.
#define FATFS_MAX_FDS
Definition: Libraries/FatIFS/src/include/IFS/FAT/FileSystem.h:63
uint32_t file_size_t
Definition: Components/IFS/src/include/IFS/Types.h:50
int remove(const char *path) override
remove (delete) a file by path