IFileSystem.h
Go to the documentation of this file.
1 /****
2  * IFileSystem.h
3  * Abstract filesystem interface definitions
4  *
5  * Created: August 2018
6  *
7  * Copyright 2019 mikee47 <mike@sillyhouse.net>
8  *
9  * This file is part of the IFS Library
10  *
11  * This library is free software: you can redistribute it and/or modify it under the terms of the
12  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along with this library.
19  * If not, see <https://www.gnu.org/licenses/>.
20  *
21  ****/
22 
28 #pragma once
29 
30 #include "Stat.h"
31 #include "OpenFlags.h"
32 #include <Storage/Partition.h>
33 #include "Error.h"
34 #include "Control.h"
35 #include "Profiler.h"
36 #include "Attribute.h"
37 #include "Extent.h"
38 #include <Data/Stream/SeekOrigin.h>
39 
44 #define FILESYSTEM_TYPE_MAP(XX) \
45  XX(Unknown, NULL, "Unknown") \
46  XX(FWFS, FWFS, "Firmware File System") \
47  XX(SPIFFS, SPIF, "SPI Flash File System (SPIFFS)") \
48  XX(LittleFS, LFS, "Little FS") \
49  XX(Hybrid, HYFS, "Hybrid File System") \
50  XX(Host, HOST, "Host File System") \
51  XX(Fat, FAT, "FAT File System") \
52  XX(Fat32, FAT32, "FAT32 File System") \
53  XX(ExFat, exFAT, "EXFAT File System")
54 
58 #define FILE_SYSTEM_ATTR_MAP(XX) \
59  XX(Mounted, "Filing system is mounted and in use") \
60  XX(ReadOnly, "Writing not permitted to this volume") \
61  XX(Virtual, "Virtual filesystem, doesn't host files directly") \
62  XX(Check, "Volume check recommended") \
63  XX(NoMeta, "Metadata unsupported")
64 
65 namespace IFS
66 {
67 class IFileSystem;
68 
69 /*
70  * Opaque structure for directory reading
71  */
72 using DirHandle = struct ImplFileDir*;
73 
74 #if DEBUG_BUILD
75 #define debug_ifserr(err, func, ...) \
76  do { \
77  [[maybe_unused]] int errorCode = err; \
78  debug_e(func ": %s (%d)", ##__VA_ARGS__, getErrorString(errorCode).c_str(), err); \
79  } while(0)
80 #else
81 #define debug_ifserr(err, func, ...) \
82  do { \
83  } while(0)
84 #endif
85 
99 {
100 public:
101  enum class Type {
102 #define XX(_name, _tag, _desc) _name,
104 #undef XX
105  MAX
106  };
107 
108  enum class Attribute {
109 #define XX(_tag, _comment) _tag,
111 #undef XX
112  MAX
113  };
114 
115  // The set of attributes
116  using Attributes = BitSet<uint8_t, Attribute, size_t(Attribute::MAX)>;
117 
121  struct Info {
122  Type type{};
124  size_t maxNameLength{255};
125  size_t maxPathLength{255};
127  uint32_t volumeID{0};
132 
133  Info() = default;
134 
135  Info(char* namebuf, unsigned buflen) : name(namebuf, buflen)
136  {
137  }
138 
140  {
141  return volumeSize - freeSpace;
142  }
143 
144  Info& operator=(const Info& rhs)
145  {
146  type = rhs.type;
147  partition = rhs.partition;
148  attr = rhs.attr;
149  volumeID = rhs.volumeID;
150  name.copy(rhs.name);
151  volumeSize = rhs.volumeSize;
152  freeSpace = rhs.freeSpace;
153  return *this;
154  }
155 
156  void clear()
157  {
158  *this = Info{};
159  }
160 
161  size_t printTo(Print& p) const;
162  };
163 
167  struct NameInfo : public Info {
168  public:
169  NameInfo() : Info(buffer, sizeof(buffer))
170  {
171  }
172 
173  private:
174  char buffer[256];
175  };
176 
180  virtual ~IFileSystem() = default;
181 
186  virtual int mount() = 0;
187 
193  virtual int getinfo(Info& info) = 0;
194 
200  virtual int setProfiler(IProfiler*)
201  {
202  return Error::NotImplemented;
203  }
204 
209  virtual String getErrorString(int err)
210  {
211  return Error::toString(err);
212  }
213 
220  virtual int setVolume([[maybe_unused]] uint8_t index, [[maybe_unused]] IFileSystem* fileSystem)
221  {
222  return Error::NotSupported;
223  }
224 
231  virtual int opendir(const char* path, DirHandle& dir) = 0;
232 
241  virtual int readdir(DirHandle dir, Stat& stat) = 0;
242 
248  virtual int rewinddir(DirHandle dir) = 0;
249 
255  virtual int closedir(DirHandle dir) = 0;
256 
266  virtual int mkdir(const char* path) = 0;
267 
285  virtual int stat(const char* path, Stat* stat) = 0;
286 
293  virtual int fstat(FileHandle file, Stat* stat) = 0;
294 
307  virtual int fcontrol([[maybe_unused]] FileHandle file, [[maybe_unused]] ControlCode code,
308  [[maybe_unused]] void* buffer, [[maybe_unused]] size_t bufSize)
309  {
310  return Error::NotSupported;
311  }
312 
323  virtual FileHandle open(const char* path, OpenFlags flags) = 0;
324 
330  virtual int close(FileHandle file) = 0;
331 
339  virtual int read(FileHandle file, void* data, size_t size) = 0;
340 
348  virtual int write(FileHandle file, const void* data, size_t size) = 0;
349 
357  virtual file_offset_t lseek(FileHandle file, file_offset_t offset, SeekOrigin origin) = 0;
358 
364  virtual int eof(FileHandle file) = 0;
365 
371  virtual file_offset_t tell(FileHandle file) = 0;
372 
381  virtual int ftruncate(FileHandle file, file_size_t new_size) = 0;
382 
388  virtual int flush(FileHandle file) = 0;
389 
399  virtual int fsetxattr(FileHandle file, AttributeTag tag, const void* data, size_t size) = 0;
400 
409  virtual int fgetxattr(FileHandle file, AttributeTag tag, void* buffer, size_t size) = 0;
410 
419  virtual int fenumxattr(FileHandle file, AttributeEnumCallback callback, void* buffer, size_t bufsize) = 0;
420 
429  virtual int setxattr(const char* path, AttributeTag tag, const void* data, size_t size) = 0;
430 
439  virtual int getxattr(const char* path, AttributeTag tag, void* buffer, size_t size) = 0;
440 
449  virtual int fgetextents([[maybe_unused]] FileHandle file, [[maybe_unused]] Storage::Partition* part,
450  [[maybe_unused]] Extent* list, [[maybe_unused]] uint16_t extcount)
451  {
452  return Error::NotImplemented;
453  }
454 
461  virtual int rename(const char* oldpath, const char* newpath) = 0;
462 
468  virtual int remove(const char* path) = 0;
469 
475  virtual int fremove(FileHandle file) = 0;
476 
484  virtual int format() = 0;
485 
493  virtual int check()
494  {
495  return Error::NotImplemented;
496  }
497 };
498 
499 } // namespace IFS
500 
505 
510 
int32_t file_offset_t
Definition: Components/IFS/src/include/IFS/Types.h:49
storage_size_t volume_size_t
Definition: Components/IFS/src/include/IFS/Types.h:35
uint32_t file_size_t
Definition: Components/IFS/src/include/IFS/Types.h:48
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
Delegate class, encapsulates a std::function Added constructor template implements lambda callback wh...
Definition: Delegate.h:24
Installable File System base class.
Definition: IFileSystem.h:99
virtual file_offset_t tell(FileHandle file)=0
get current file position
virtual int fsetxattr(FileHandle file, AttributeTag tag, const void *data, size_t size)=0
Set an extended attribute on an open file.
virtual int rename(const char *oldpath, const char *newpath)=0
rename a file
virtual int rewinddir(DirHandle dir)=0
Reset directory read position to start.
virtual String getErrorString(int err)
get the text for a returned error code
Definition: IFileSystem.h:209
virtual int fgetextents([[maybe_unused]] FileHandle file, [[maybe_unused]] Storage::Partition *part, [[maybe_unused]] Extent *list, [[maybe_unused]] uint16_t extcount)
Get extents for a file.
Definition: IFileSystem.h:449
Type
Definition: IFileSystem.h:101
XX(_name, _tag, _desc)
virtual int close(FileHandle file)=0
close an open file
virtual int opendir(const char *path, DirHandle &dir)=0
open a directory for reading
virtual int remove(const char *path)=0
remove (delete) a file by path
virtual int setProfiler(IProfiler *)
Set profiler instance to enable debugging and performance assessment.
Definition: IFileSystem.h:200
virtual int fcontrol([[maybe_unused]] FileHandle file, [[maybe_unused]] ControlCode code, [[maybe_unused]] void *buffer, [[maybe_unused]] size_t bufSize)
Low-level and non-standard file control operations.
Definition: IFileSystem.h:307
virtual int flush(FileHandle file)=0
flush any buffered data to physical media
virtual int closedir(DirHandle dir)=0
close a directory object
virtual int stat(const char *path, Stat *stat)=0
get file information
virtual int setVolume([[maybe_unused]] uint8_t index, [[maybe_unused]] IFileSystem *fileSystem)
Set volume for mountpoint.
Definition: IFileSystem.h:220
virtual int fstat(FileHandle file, Stat *stat)=0
get file information
virtual int getxattr(const char *path, AttributeTag tag, void *buffer, size_t size)=0
Get an attribute from a file given its path.
virtual ~IFileSystem()=default
Filing system implementations should dismount and cleanup here.
virtual int check()
Perform a file system consistency check.
Definition: IFileSystem.h:493
virtual int write(FileHandle file, const void *data, size_t size)=0
write content to a file at current position and advance cursor
virtual int fgetxattr(FileHandle file, AttributeTag tag, void *buffer, size_t size)=0
Get an extended attribute from an open file.
virtual int getinfo(Info &info)=0
get filing system information
Attribute
Definition: IFileSystem.h:108
virtual int fremove(FileHandle file)=0
remove (delete) a file by handle
virtual int mount()=0
Mount file system, performing any required initialisation.
virtual int ftruncate(FileHandle file, file_size_t new_size)=0
Truncate (reduce) the size of an open file.
virtual int read(FileHandle file, void *data, size_t size)=0
read content from a file and advance cursor
virtual file_offset_t lseek(FileHandle file, file_offset_t offset, SeekOrigin origin)=0
change file read/write position
virtual int readdir(DirHandle dir, Stat &stat)=0
read a directory entry
virtual int format()=0
format the filing system
virtual int setxattr(const char *path, AttributeTag tag, const void *data, size_t size)=0
Set an extended attribute for a file given its path.
virtual int eof(FileHandle file)=0
determine if current file position is at end of file
virtual FileHandle open(const char *path, OpenFlags flags)=0
open a file (or directory) by path
virtual int mkdir(const char *path)=0
Create a directory.
virtual int fenumxattr(FileHandle file, AttributeEnumCallback callback, void *buffer, size_t bufsize)=0
Enumerate attributes.
Filesystems may optionally provide performance statistics.
Definition: Profiler.h:31
Provides formatted output to stream.
Definition: Print.h:37
Represents a flash partition.
Definition: Partition.h:86
The String class.
Definition: WString.h:133
String toString(IFS::IFileSystem::Type type)
Get String for filesystem type.
String toString(int err)
get text for an error code
#define FILE_SYSTEM_ATTR_MAP(XX)
Attribute flags for filing system.
Definition: IFileSystem.h:58
#define FILESYSTEM_TYPE_MAP(XX)
Four-character tag to identify type of filing system.
Definition: IFileSystem.h:44
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
Defines the location of a contiguous run of file data.
Definition: Extent.h:40
Basic information about filing system.
Definition: IFileSystem.h:121
TimeStamp creationTime
Definition: IFileSystem.h:131
size_t maxPathLength
Maximum length of a full file path.
Definition: IFileSystem.h:125
void clear()
Definition: IFileSystem.h:156
Storage::Partition partition
Definition: IFileSystem.h:126
Info & operator=(const Info &rhs)
Definition: IFileSystem.h:144
size_t maxNameLength
Maximum length of a single file name.
Definition: IFileSystem.h:124
volume_size_t volumeSize
Size of volume, in bytes.
Definition: IFileSystem.h:129
Type type
The filing system type identifier.
Definition: IFileSystem.h:122
Attributes attr
Attribute flags.
Definition: IFileSystem.h:123
uint32_t volumeID
Unique identifier for volume.
Definition: IFileSystem.h:127
NameBuffer name
Buffer for name.
Definition: IFileSystem.h:128
size_t printTo(Print &p) const
Info(char *namebuf, unsigned buflen)
Definition: IFileSystem.h:135
volume_size_t used() const
Definition: IFileSystem.h:139
volume_size_t freeSpace
Available space, in bytes.
Definition: IFileSystem.h:130
Filing system information with buffer for name.
Definition: IFileSystem.h:167
NameInfo()
Definition: IFileSystem.h:169
defines a 'safe' name buffer
Definition: NameBuffer.h:44
int copy(const char *src, uint16_t srclen)
copies text from a source buffer into a name buffer
Definition: NameBuffer.h:93
File Status structure.
Definition: Stat.h:52
Manage IFS timestamps stored as an unsigned 32-bit value.
Definition: TimeStamp.h:37