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 <Data/Stream/SeekOrigin.h>
38 
43 #define FILESYSTEM_TYPE_MAP(XX) \
44  XX(Unknown, NULL, "Unknown") \
45  XX(FWFS, FWFS, "Firmware File System") \
46  XX(SPIFFS, SPIF, "SPI Flash File System (SPIFFS)") \
47  XX(LittleFS, LFS, "Little FS") \
48  XX(Hybrid, HYFS, "Hybrid File System") \
49  XX(Host, HOST, "Host File System") \
50  XX(Fat, FAT, "FAT File System") \
51  XX(Fat32, FAT32, "FAT32 File System") \
52  XX(ExFat, exFAT, "EXFAT File System")
53 
57 #define FILE_SYSTEM_ATTR_MAP(XX) \
58  XX(Mounted, "Filing system is mounted and in use") \
59  XX(ReadOnly, "Writing not permitted to this volume") \
60  XX(Virtual, "Virtual filesystem, doesn't host files directly") \
61  XX(Check, "Volume check recommended") \
62  XX(NoMeta, "Metadata unsupported")
63 
64 namespace IFS
65 {
66 class IFileSystem;
67 
68 /*
69  * Opaque structure for directory reading
70  */
71 using DirHandle = struct ImplFileDir*;
72 
73 #if DEBUG_BUILD
74 #define debug_ifserr(err, func, ...) \
75  do { \
76  int errorCode = err; \
77  (void)errorCode; \
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
117 
121  struct Info {
122  Type type{};
123  Attributes attr{};
124  size_t maxNameLength{255};
125  size_t maxPathLength{255};
127  uint32_t volumeID{0};
129  volume_size_t volumeSize{0};
130  volume_size_t freeSpace{0};
131  TimeStamp creationTime{};
132 
134  {
135  }
136 
137  Info(char* namebuf, unsigned buflen) : name(namebuf, buflen)
138  {
139  }
140 
142  {
143  return volumeSize - freeSpace;
144  }
145 
146  Info& operator=(const Info& rhs)
147  {
148  type = rhs.type;
149  partition = rhs.partition;
150  attr = rhs.attr;
151  volumeID = rhs.volumeID;
152  name.copy(rhs.name);
153  volumeSize = rhs.volumeSize;
154  freeSpace = rhs.freeSpace;
155  return *this;
156  }
157 
158  void clear()
159  {
160  *this = Info{};
161  }
162 
163  size_t printTo(Print& p) const;
164  };
165 
169  struct NameInfo : public Info {
170  public:
171  NameInfo() : Info(buffer, sizeof(buffer))
172  {
173  }
174 
175  private:
176  char buffer[256];
177  };
178 
182  virtual ~IFileSystem()
183  {
184  }
185 
190  virtual int mount() = 0;
191 
197  virtual int getinfo(Info& info) = 0;
198 
204  virtual int setProfiler(IProfiler* profiler)
205  {
206  return Error::NotImplemented;
207  }
208 
213  virtual String getErrorString(int err)
214  {
215  return Error::toString(err);
216  }
217 
224  virtual int setVolume(uint8_t index, IFileSystem* fileSystem)
225  {
226  return Error::NotSupported;
227  }
228 
235  virtual int opendir(const char* path, DirHandle& dir) = 0;
236 
245  virtual int readdir(DirHandle dir, Stat& stat) = 0;
246 
252  virtual int rewinddir(DirHandle dir) = 0;
253 
259  virtual int closedir(DirHandle dir) = 0;
260 
270  virtual int mkdir(const char* path) = 0;
271 
289  virtual int stat(const char* path, Stat* stat) = 0;
290 
297  virtual int fstat(FileHandle file, Stat* stat) = 0;
298 
311  virtual int fcontrol(FileHandle file, ControlCode code, void* buffer, size_t bufSize)
312  {
313  return Error::NotSupported;
314  }
315 
326  virtual FileHandle open(const char* path, OpenFlags flags) = 0;
327 
333  virtual int close(FileHandle file) = 0;
334 
342  virtual int read(FileHandle file, void* data, size_t size) = 0;
343 
351  virtual int write(FileHandle file, const void* data, size_t size) = 0;
352 
360  virtual file_offset_t lseek(FileHandle file, file_offset_t offset, SeekOrigin origin) = 0;
361 
367  virtual int eof(FileHandle file) = 0;
368 
374  virtual file_offset_t tell(FileHandle file) = 0;
375 
384  virtual int ftruncate(FileHandle file, file_size_t new_size) = 0;
385 
391  virtual int flush(FileHandle file) = 0;
392 
402  virtual int fsetxattr(FileHandle file, AttributeTag tag, const void* data, size_t size) = 0;
403 
412  virtual int fgetxattr(FileHandle file, AttributeTag tag, void* buffer, size_t size) = 0;
413 
422  virtual int fenumxattr(FileHandle file, AttributeEnumCallback callback, void* buffer, size_t bufsize) = 0;
423 
432  virtual int setxattr(const char* path, AttributeTag tag, const void* data, size_t size) = 0;
433 
442  virtual int getxattr(const char* path, AttributeTag tag, void* buffer, size_t size) = 0;
443 
450  virtual int rename(const char* oldpath, const char* newpath) = 0;
451 
457  virtual int remove(const char* path) = 0;
458 
464  virtual int fremove(FileHandle file) = 0;
465 
473  virtual int format() = 0;
474 
482  virtual int check()
483  {
484  return Error::NotImplemented;
485  }
486 };
487 
488 } // namespace IFS
489 
494 
499 
virtual int fcontrol(FileHandle file, ControlCode code, void *buffer, size_t bufSize)
Low-level and non-standard file control operations.
Definition: IFileSystem.h:311
Basic information about filing system.
Definition: IFileSystem.h:121
Attributes attr
Attribute flags.
Definition: IFileSystem.h:123
#define FILE_SYSTEM_ATTR_MAP(XX)
Attribute flags for filing system.
Definition: IFileSystem.h:57
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 check()
Perform a file system consistency check.
Definition: IFileSystem.h:482
virtual int fgetxattr(FileHandle file, AttributeTag tag, void *buffer, size_t size)=0
Get an extended attribute from an open file.
#define FILESYSTEM_TYPE_MAP(XX)
Four-character tag to identify type of filing system.
Definition: IFileSystem.h:43
virtual file_offset_t lseek(FileHandle file, file_offset_t offset, SeekOrigin origin)=0
change file read/write position
virtual int close(FileHandle file)=0
close an open file
ControlCode
See IFS::IFileSystem::fcontrol
Definition: Control.h:31
virtual int mount()=0
Mount file system, performing any required initialisation.
virtual int fsetxattr(FileHandle file, AttributeTag tag, const void *data, size_t size)=0
Set an extended attribute on an open file.
virtual int eof(FileHandle file)=0
determine if current file position is at end of file
virtual int setVolume(uint8_t index, IFileSystem *fileSystem)
Set volume for mountpoint.
Definition: IFileSystem.h:224
virtual int rename(const char *oldpath, const char *newpath)=0
rename a file
volume_size_t freeSpace
Available space, in bytes.
Definition: IFileSystem.h:130
Attribute
Definition: IFileSystem.h:108
Installable File System base class.
Definition: IFileSystem.h:98
virtual FileHandle open(const char *path, OpenFlags flags)=0
open a file (or directory) by path
virtual int opendir(const char *path, DirHandle &dir)=0
open a directory for reading
int copy(const char *src, uint16_t srclen)
copies text from a source buffer into a name buffer
Definition: NameBuffer.h:95
virtual int mkdir(const char *path)=0
Create a directory.
struct ImplFileDir * DirHandle
Definition: IFileSystem.h:71
virtual int fenumxattr(FileHandle file, AttributeEnumCallback callback, void *buffer, size_t bufsize)=0
Enumerate attributes.
uint32_t file_size_t
Definition: Components/IFS/src/include/IFS/Types.h:50
The String class.
Definition: WString.h:136
virtual int fremove(FileHandle file)=0
remove (delete) a file by handle
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()
Filing system implementations should dismount and cleanup here.
Definition: IFileSystem.h:182
Provides formatted output to stream.
Definition: Print.h:36
Definition: Delegate.h:20
NameInfo()
Definition: IFileSystem.h:171
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.
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
virtual int closedir(DirHandle dir)=0
close a directory object
String toString(int err)
get text for an error code
int16_t FileHandle
File handle.
Definition: Stat.h:40
Type
Definition: IFileSystem.h:101
Filing system information with buffer for name.
Definition: IFileSystem.h:169
Definition: DirectoryTemplate.h:36
Manage IFS timestamps stored as an unsigned 32-bit value.
Definition: TimeStamp.h:36
virtual int read(FileHandle file, void *data, size_t size)=0
read content from a file and advance cursor
virtual int readdir(DirHandle dir, Stat &stat)=0
read a directory entry
NameBuffer name
Buffer for name.
Definition: IFileSystem.h:128
volume_size_t used() const
Definition: IFileSystem.h:141
storage_size_t volume_size_t
Definition: Components/IFS/src/include/IFS/Types.h:37
virtual int ftruncate(FileHandle file, file_size_t new_size)=0
Truncate (reduce) the size of an open file.
virtual int flush(FileHandle file)=0
flush any buffered data to physical media
Type type
The filing system type identifier.
Definition: IFileSystem.h:122
virtual int stat(const char *path, Stat *stat)=0
get file information
virtual int setProfiler(IProfiler *profiler)
Set profiler instance to enable debugging and performance assessment.
Definition: IFileSystem.h:204
uint32_t volumeID
Unique identifier for volume.
Definition: IFileSystem.h:127
File Status structure.
Definition: Stat.h:52
void clear()
Definition: IFileSystem.h:158
virtual int format()=0
format the filing system
String toString(IFS::IFileSystem::Type type)
Get String for filesystem type.
Represents a flash partition.
Definition: Partition.h:85
Info & operator=(const Info &rhs)
Definition: IFileSystem.h:146
Filesystems may optionally provide performance statistics.
Definition: Profiler.h:30
AttributeTag
Identifies a specific attribute.
Definition: Attribute.h:45
virtual int rewinddir(DirHandle dir)=0
Reset directory read position to start.
int32_t file_offset_t
Definition: Components/IFS/src/include/IFS/Types.h:51
virtual int fstat(FileHandle file, Stat *stat)=0
get file information
volume_size_t volumeSize
Size of volume, in bytes.
Definition: IFileSystem.h:129
virtual int getinfo(Info &info)=0
get filing system information
Storage::Partition partition
Definition: IFileSystem.h:126
XX(_name, _tag, _desc)
Info()
Definition: IFileSystem.h:133
Info(char *namebuf, unsigned buflen)
Definition: IFileSystem.h:137
defines a &#39;safe&#39; name buffer
Definition: NameBuffer.h:44
virtual file_offset_t tell(FileHandle file)=0
get current file position
virtual String getErrorString(int err)
get the text for a returned error code
Definition: IFileSystem.h:213