Components/IFS/src/include/IFS/FWFS/FileSystem.h
Go to the documentation of this file.
1 
23 #pragma once
24 
25 #include "../FileSystem.h"
26 #include "../ObjectStore.h"
27 
28 namespace IFS
29 {
30 namespace FWFS
31 {
32 // File handles start at this value
33 #ifndef FWFS_HANDLE_MIN
34 #define FWFS_HANDLE_MIN 100
35 #endif
36 
37 // Maximum number of file descriptors
38 #ifndef FWFS_MAX_FDS
39 #define FWFS_MAX_FDS 8
40 #endif
41 
42 // Maximum number of volumes - 1 is minimum, the rest are mounted in subdirectories
43 #ifndef FWFS_MAX_VOLUMES
44 #define FWFS_MAX_VOLUMES 4
45 #endif
46 
47 // Maximum file handle value
48 #define FWFS_HANDLE_MAX (FWFS_HANDLE_MIN + FWFS_MAX_FDS - 1)
49 
54 enum class FWFileDescAttr {
55  allocated,
56 };
57 
59 
63 struct FWFileDesc {
65  uint32_t dataSize{0};
66  uint32_t cursor{0};
68 };
69 
73 struct FWVolume {
74  IObjectStore* store{nullptr};
76 
77  bool isMounted()
78  {
79  return store ? store->isMounted() : false;
80  }
81 };
82 
86 class FileSystem : public IFileSystem
87 {
88 public:
90  {
91  }
92 
94  {
95  setVolume(0, store);
96  }
97 
99  {
100  for(auto& vol : volumes) {
101  delete vol.store;
102  }
103  }
104 
110  int setVolume(uint8_t num, IObjectStore* store);
111 
112  // IFileSystem methods
113  int mount() override;
114  int getinfo(Info& info) override;
115  int opendir(const char* path, DirHandle& dir) override;
116  int fopendir(const Stat* stat, DirHandle& dir) override;
117  int readdir(DirHandle dir, Stat& stat) override;
118  int rewinddir(DirHandle dir) override;
119  int closedir(DirHandle dir) override;
120  int mkdir(const char* path) override
121  {
122  return Error::ReadOnly;
123  }
124  int stat(const char* path, Stat* stat) override;
125  int fstat(FileHandle file, Stat* stat) override;
126  int fcontrol(FileHandle file, ControlCode code, void* buffer, size_t bufSize) override;
127  int setacl(FileHandle file, const ACL& acl) override
128  {
129  return Error::ReadOnly;
130  }
131  int setattr(const char* path, FileAttributes attr)
132  {
133  return Error::ReadOnly;
134  }
135  int settime(FileHandle file, time_t mtime) override
136  {
137  return Error::ReadOnly;
138  }
139  int setcompression(FileHandle file, const Compression& compression)
140  {
141  return Error::ReadOnly;
142  }
143  FileHandle open(const char* path, OpenFlags flags) override;
144  FileHandle fopen(const Stat& stat, OpenFlags flags) override;
145  int close(FileHandle file) override;
146  int read(FileHandle file, void* data, size_t size) override;
147  int write(FileHandle file, const void* data, size_t size) override
148  {
149  return Error::ReadOnly;
150  }
151  int lseek(FileHandle file, int offset, SeekOrigin origin) override;
152  int eof(FileHandle file) override;
153  int32_t tell(FileHandle file) override;
154  int ftruncate(FileHandle file, size_t new_size) override
155  {
156  return Error::ReadOnly;
157  }
158  int flush(FileHandle file) override
159  {
160  return Error::ReadOnly;
161  }
162  int rename(const char* oldpath, const char* newpath) override
163  {
164  return Error::ReadOnly;
165  }
166  int remove(const char* path) override
167  {
168  return Error::ReadOnly;
169  }
170  int fremove(FileHandle file) override
171  {
172  return Error::ReadOnly;
173  }
174  int format() override
175  {
176  return Error::ReadOnly;
177  }
178  int check() override
179  {
180  /* We could implement this, but since problems would indicate corrupted firmware
181  * there isn't much we can do other than suggest a re-flashing. This sort of issue
182  * is better resolved externally using a hash of the entire firmware image. */
183  return Error::NotImplemented;
184  }
185 
191  int getFilePath(FileID fileid, NameBuffer& path);
192 
193  int getMd5Hash(FileHandle file, void* buffer, size_t bufSize);
194 
195 private:
196  int seekFilePath(FWObjDesc& parent, FileID fileid, NameBuffer& path);
197 
202  int mountVolume(FWVolume& volume);
203 
206  int getStore(const ObjRef& ref, IObjectStore*& store)
207  {
208  if(ref.storenum >= FWFS_MAX_VOLUMES) {
209  return Error::BadStore;
210  }
211  store = volumes[ref.storenum].store;
212  return store ? FS_OK : Error::NotMounted;
213  }
214 
215  int getStore(const FWObjDesc& od, IObjectStore*& store)
216  {
217  return getStore(od.ref, store);
218  }
219 
220  int openRootObject(const FWVolume& volume, FWObjDesc& odRoot);
221 
222  int openObject(FWObjDesc& od)
223  {
224  assert(od.ref.refCount == 0);
225 
226  IObjectStore* store;
227  int res = getStore(od.ref, store);
228  if(res >= 0) {
229  res = store->open(od);
230  if(res >= 0) {
231  ++od.ref.refCount;
232  }
233  }
234 
235  return res;
236  }
237 
238  int openChildObject(const FWObjDesc& parent, const FWObjDesc& child, FWObjDesc& od)
239  {
240  assert(od.ref.refCount == 0);
241 
242  IObjectStore* store;
243  int res = getStore(parent.ref, store);
244  if(res >= 0) {
245  res = store->openChild(parent, child, od);
246  if(res >= 0) {
247  ++od.ref.refCount;
248  }
249  }
250 
251  return res;
252  }
253 
254  int closeObject(FWObjDesc& od)
255  {
256  assert(od.ref.refCount == 1);
257 
258  IObjectStore* store;
259  int res = getStore(od.ref, store);
260  if(res >= 0) {
261  res = store->close(od);
262  if(res >= 0) {
263  --od.ref.refCount;
264  }
265  }
266 
267  return res;
268  }
269 
270  int readObjectHeader(FWObjDesc& od)
271  {
272  IObjectStore* store;
273  int res = getStore(od.ref, store);
274  return res < 0 ? res : store->readHeader(od);
275  }
276 
277  int readChildObjectHeader(const FWObjDesc& parent, FWObjDesc& child)
278  {
279  assert(parent.obj.isNamed());
280  // Child must be in same store as parent
281  child.ref.storenum = parent.ref.storenum;
282  IObjectStore* store;
283  int res = getStore(parent.ref, store);
284  return res < 0 ? res : store->readChildHeader(parent, child);
285  }
286 
287  int readObjectContent(const FWObjDesc& od, uint32_t offset, uint32_t size, void* buffer)
288  {
289  IObjectStore* store;
290  int res = getStore(od.ref, store);
291  return res < 0 ? res : store->readContent(od, offset, size, buffer);
292  }
293 
297  int findUnusedDescriptor();
298 
299  int findChildObjectHeader(const FWObjDesc& parent, FWObjDesc& child, Object::Type objId);
300  int findChildObject(const FWObjDesc& parent, FWObjDesc& child, const char* name, unsigned namelen);
301  int findObjectByPath(const char* path, FWObjDesc& od);
302  int resolveMountPoint(const FWObjDesc& odMountPoint, FWObjDesc& odResolved);
303 
304  int readObjectName(const FWObjDesc& od, NameBuffer& name);
305  FileHandle allocateFileDescriptor(FWObjDesc& odFile);
306  int fillStat(Stat& stat, const FWObjDesc& entry);
307 
308  void printObject(const FWObjDesc& od);
309 
310 private:
311  FWVolume volumes[FWFS_MAX_VOLUMES];
312  ACL rootACL;
313  FWFileDesc fileDescriptors[FWFS_MAX_FDS];
314 };
315 
316 } // namespace FWFS
317 
318 } // namespace IFS
Basic information about filing system.
Definition: IFileSystem.h:123
#define FWFS_MAX_FDS
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:39
int settime(FileHandle file, time_t mtime) override
Set modificatino tiem for file.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:135
struct FileDir * DirHandle
Definition: IFileSystem.h:65
FWObjDesc odFile
File object.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:64
ControlCode
See IFS::IFileSystem::fcontrol
Definition: Control.h:31
virtual int close(FWObjDesc &od)=0
close an object descriptor
int flush(FileHandle file) override
flush any buffered data to physical media
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:158
Definition: ObjectStore.h:108
uint8_t storenum
Object store number.
Definition: ObjectStore.h:36
Installable File System base class.
Definition: IFileSystem.h:100
bool isMounted()
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:77
bool isNamed() const
Definition: Object.h:183
int write(FileHandle file, const void *data, size_t size) override
write content to a file at current position and advance cursor
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:147
virtual int readHeader(FWObjDesc &od)=0
read a root object header
Object obj
The object structure.
Definition: ObjectStore.h:70
~FileSystem()
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:98
int setattr(const char *path, FileAttributes attr)
Set file attributes.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:131
FWFileDescAttr
file descriptor attributes
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:54
virtual int readChildHeader(const FWObjDesc &parent, FWObjDesc &child)=0
fetch child object header
#define FWFS_MAX_VOLUMES
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:44
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
int ftruncate(FileHandle file, size_t new_size) override
Truncate (reduce) the size of an open file.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:154
int16_t FileHandle
File handle.
Definition: Stat.h:39
Definition: DirectoryTemplate.h:36
Implementation of firmware filing system using IFS.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:86
FWFS Object Descriptor.
Definition: ObjectStore.h:69
FWFS Volume definition - identifies object store and volume object after mounting.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:73
virtual int openChild(const FWObjDesc &parent, const FWObjDesc &child, FWObjDesc &od)=0
open a descriptor for a child object
File Status structure.
Definition: Stat.h:51
uint32_t FileID
File identifier.
Definition: Stat.h:46
FWFS File Descriptor.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:63
int fremove(FileHandle file) override
remove (delete) a file by handle
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:170
gives the identity and location of an FWFS object
Definition: ObjectStore.h:32
ObjRef ref
Volume reference.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:75
int mkdir(const char *path) override
Create a directory.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:120
int check() override
Perform a file system consistency check.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:178
FileSystem(IObjectStore *store)
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:93
Definition: Access.h:34
ObjRef ref
location
Definition: ObjectStore.h:71
int setacl(FileHandle file, const ACL &acl) override
Set access control information for file.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:127
FWFileDescAttributes attr
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:67
uint8_t refCount
For testing open/close correctness.
Definition: ObjectStore.h:38
int rename(const char *oldpath, const char *newpath) override
rename a file
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:162
constexpr ErrorCode FS_OK
Definition: Error.h:130
defines a &#39;safe&#39; name buffer
Definition: NameBuffer.h:44
FileSystem()
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:89
int setcompression(FileHandle file, const Compression &compression)
Set file compression information.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:139
virtual int open(FWObjDesc &od)=0
find an object and return a descriptor for it
Type
Definition: Object.h:145
virtual int readContent(const FWObjDesc &od, uint32_t offset, uint32_t size, void *buffer)=0
read object content
int format() override
format the filing system
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:174
A compression descriptor.
Definition: Compression.h:38