IFileSystem.h
Go to the documentation of this file.
1 
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 <Data/Stream/SeekOrigin.h>
36 
41 #define FILESYSTEM_TYPE_MAP(XX) \
42  XX(Unknown, NULL, "Unknown") \
43  XX(FWFS, FWFS, "Firmware File System") \
44  XX(SPIFFS, SPIF, "SPI Flash File System (SPIFFS)") \
45  XX(Hybrid, HYFS, "Hybrid File System") \
46  XX(Host, HOST, "Host File System")
47 
51 #define FILE_SYSTEM_ATTR_MAP(XX) \
52  XX(Mounted, "Filing system is mounted and in use") \
53  XX(ReadOnly, "Writing not permitted to this volume") \
54  XX(Virtual, "Virtual filesystem, doesn't host files directly") \
55  XX(Check, "Volume check recommended") \
56  XX(NoMeta, "Metadata unsupported")
57 
58 namespace IFS
59 {
60 class IFileSystem;
61 
62 /*
63  * Opaque structure for directory reading
64  */
65 using DirHandle = struct FileDir*;
66 
73 time_t fsGetTimeUTC();
74 
75 #if DEBUG_BUILD
76 #define debug_ifserr(err, func, ...) \
77  do { \
78  int errorCode = err; \
79  (void)errorCode; \
80  debug_e(func ": %s (%d)", ##__VA_ARGS__, getErrorString(errorCode).c_str(), err); \
81  } while(0)
82 #else
83 #define debug_ifserr(err, func, ...) \
84  do { \
85  } while(0)
86 #endif
87 
101 {
102 public:
103  enum class Type {
104 #define XX(_name, _tag, _desc) _name,
106 #undef XX
107  MAX
108  };
109 
110  enum class Attribute {
111 #define XX(_tag, _comment) _tag,
113 #undef XX
114  MAX
115  };
116 
117  // The set of attributes
119 
123  struct Info {
124  Type type{};
125  Attributes attr{};
126  size_t maxNameLength;
127  size_t maxPathLength;
129  uint32_t volumeID{0};
131  uint32_t volumeSize{0};
132  uint32_t freeSpace{0};
133 
135  {
136  }
137 
138  Info(char* namebuf, unsigned buflen) : name(namebuf, buflen)
139  {
140  }
141 
142  Info& operator=(const Info& rhs)
143  {
144  type = rhs.type;
145  partition = rhs.partition;
146  attr = rhs.attr;
147  volumeID = rhs.volumeID;
148  name.copy(rhs.name);
149  volumeSize = rhs.volumeSize;
150  freeSpace = rhs.freeSpace;
151  return *this;
152  }
153 
154  void clear()
155  {
156  *this = Info{};
157  }
158  };
159 
163  virtual ~IFileSystem()
164  {
165  }
166 
171  virtual int mount() = 0;
172 
178  virtual int getinfo(Info& info) = 0;
179 
184  virtual String getErrorString(int err)
185  {
186  return Error::toString(err);
187  }
188 
195  virtual int opendir(const char* path, DirHandle& dir) = 0;
196 
203  virtual int fopendir(const Stat* stat, DirHandle& dir)
204  {
205  return opendir(stat == nullptr ? nullptr : stat->name.buffer, dir);
206  }
207 
216  virtual int readdir(DirHandle dir, Stat& stat) = 0;
217 
223  virtual int rewinddir(DirHandle dir) = 0;
224 
230  virtual int closedir(DirHandle dir) = 0;
231 
241  virtual int mkdir(const char* path) = 0;
242 
249  virtual int stat(const char* path, Stat* stat) = 0;
250 
257  virtual int fstat(FileHandle file, Stat* stat) = 0;
258 
271  virtual int fcontrol(FileHandle file, ControlCode code, void* buffer, size_t bufSize)
272  {
273  return Error::NotSupported;
274  }
275 
282  virtual FileHandle open(const char* path, OpenFlags flags) = 0;
283 
290  virtual FileHandle fopen(const Stat& stat, OpenFlags flags) = 0;
291 
297  virtual int close(FileHandle file) = 0;
298 
306  virtual int read(FileHandle file, void* data, size_t size) = 0;
307 
315  virtual int write(FileHandle file, const void* data, size_t size) = 0;
316 
324  virtual int lseek(FileHandle file, int offset, SeekOrigin origin) = 0;
325 
331  virtual int eof(FileHandle file) = 0;
332 
338  virtual int32_t tell(FileHandle file) = 0;
339 
348  virtual int ftruncate(FileHandle file, size_t new_size) = 0;
349 
355  virtual int flush(FileHandle file) = 0;
356 
363  virtual int setacl(FileHandle file, const ACL& acl) = 0;
364 
371  virtual int setattr(const char* path, FileAttributes attr) = 0;
372 
379  virtual int settime(FileHandle file, time_t mtime) = 0;
380 
387  virtual int setcompression(FileHandle file, const Compression& compression) = 0;
388 
395  virtual int rename(const char* oldpath, const char* newpath) = 0;
396 
402  virtual int remove(const char* path) = 0;
403 
409  virtual int fremove(FileHandle file) = 0;
410 
418  virtual int format() = 0;
419 
427  virtual int check()
428  {
429  return Error::NotImplemented;
430  }
431 };
432 
433 } // namespace IFS
434 
439 
444 
virtual int fcontrol(FileHandle file, ControlCode code, void *buffer, size_t bufSize)
Low-level and non-standard file control operations.
Definition: IFileSystem.h:271
Basic information about filing system.
Definition: IFileSystem.h:123
Attributes attr
Attribute flags.
Definition: IFileSystem.h:125
#define FILE_SYSTEM_ATTR_MAP(XX)
Attribute flags for filing system.
Definition: IFileSystem.h:51
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:427
struct FileDir * DirHandle
Definition: IFileSystem.h:65
#define FILESYSTEM_TYPE_MAP(XX)
Four-character tag to identify type of filing system.
Definition: IFileSystem.h:41
virtual int setacl(FileHandle file, const ACL &acl)=0
Set access control information for file.
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.
char * buffer
Buffer to store name.
Definition: NameBuffer.h:45
virtual int setattr(const char *path, FileAttributes attr)=0
Set file attributes.
virtual int eof(FileHandle file)=0
determine if current file position is at end of file
virtual int rename(const char *oldpath, const char *newpath)=0
rename a file
Attribute
Definition: IFileSystem.h:110
virtual int setcompression(FileHandle file, const Compression &compression)=0
Set file compression information.
Installable File System base class.
Definition: IFileSystem.h:100
virtual FileHandle open(const char *path, OpenFlags flags)=0
open a file by name/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:90
virtual int mkdir(const char *path)=0
Create a directory.
The String class.
Definition: WString.h:136
size_t maxPathLength
Maximum length of a full file path.
Definition: IFileSystem.h:127
virtual int fremove(FileHandle file)=0
remove (delete) a file by handle
uint32_t volumeSize
Size of volume, in bytes.
Definition: IFileSystem.h:131
virtual ~IFileSystem()
Filing system implementations should dismount and cleanup here.
Definition: IFileSystem.h:163
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:39
Type
Definition: IFileSystem.h:103
Definition: DirectoryTemplate.h:36
virtual int read(FileHandle file, void *data, size_t size)=0
read content from a file and advance cursor
virtual int settime(FileHandle file, time_t mtime)=0
Set modificatino tiem for file.
virtual int readdir(DirHandle dir, Stat &stat)=0
read a directory entry
virtual int ftruncate(FileHandle file, size_t new_size)=0
Truncate (reduce) the size of an open file.
NameBuffer name
Buffer for name.
Definition: IFileSystem.h:130
virtual int flush(FileHandle file)=0
flush any buffered data to physical media
Type type
The filing system type identifier.
Definition: IFileSystem.h:124
virtual int stat(const char *path, Stat *stat)=0
get file information
uint32_t volumeID
Unique identifier for volume.
Definition: IFileSystem.h:129
size_t maxNameLength
Maximum length of a single file name.
Definition: IFileSystem.h:126
File Status structure.
Definition: Stat.h:51
virtual int32_t tell(FileHandle file)=0
get current file position
time_t fsGetTimeUTC()
Get current timestamp in UTC.
void clear()
Definition: IFileSystem.h:154
virtual FileHandle fopen(const Stat &stat, OpenFlags flags)=0
open a file from it&#39;s stat structure
uint32_t freeSpace
Available space, in bytes.
Definition: IFileSystem.h:132
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:77
Info & operator=(const Info &rhs)
Definition: IFileSystem.h:142
Definition: Access.h:34
virtual int rewinddir(DirHandle dir)=0
Reset directory read position to start.
virtual int fstat(FileHandle file, Stat *stat)=0
get file information
virtual int getinfo(Info &info)=0
get filing system information
Storage::Partition partition
Definition: IFileSystem.h:128
virtual int lseek(FileHandle file, int offset, SeekOrigin origin)=0
change file read/write position
XX(_name, _tag, _desc)
Info()
Definition: IFileSystem.h:134
Info(char *namebuf, unsigned buflen)
Definition: IFileSystem.h:138
virtual int fopendir(const Stat *stat, DirHandle &dir)
open a directory for reading
Definition: IFileSystem.h:203
defines a &#39;safe&#39; name buffer
Definition: NameBuffer.h:44
NameBuffer name
Name of file.
Definition: Stat.h:53
virtual String getErrorString(int err)
get the text for a returned error code
Definition: IFileSystem.h:184
A compression descriptor.
Definition: Compression.h:38