Components/IFS/src/include/IFS/FWFS/FileSystem.h
Go to the documentation of this file.
1 
23 #pragma once
24 
25 #include "../IFileSystem.h"
26 #include "Object.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 
53 struct FWFileDesc {
55  union {
56  struct {
57  uint32_t dataSize;
58  uint32_t cursor;
59  };
60  // For MountPoint
61  struct {
63  union {
66  };
67  };
68  };
69 
70  bool isAllocated() const
71  {
72  return odFile.obj.typeData != 0;
73  }
74 
75  bool isMountPoint() const
76  {
77  return odFile.obj.isMountPoint();
78  }
79 
80  void reset()
81  {
82  *this = FWFileDesc{};
83  }
84 };
85 
89 struct FWVolume {
90  std::unique_ptr<IFileSystem> fileSystem;
91 };
92 
96 class FileSystem : public IFileSystem
97 {
98 public:
99  FileSystem(Storage::Partition partition) : partition(partition)
100  {
101  }
102 
103  // IFileSystem methods
104  int mount() override;
105  int getinfo(Info& info) override;
106  String getErrorString(int err) override;
107  int setVolume(uint8_t index, IFileSystem* fileSystem) override;
108  int opendir(const char* path, DirHandle& dir) override;
109  int readdir(DirHandle dir, Stat& stat) override;
110  int rewinddir(DirHandle dir) override;
111  int closedir(DirHandle dir) override;
112  int mkdir(const char* path) override;
113  int stat(const char* path, Stat* stat) override;
114  int fstat(FileHandle file, Stat* stat) override;
115  int fcontrol(FileHandle file, ControlCode code, void* buffer, size_t bufSize) override;
116  int fsetxattr(FileHandle file, AttributeTag tag, const void* data, size_t size) override;
117  int fgetxattr(FileHandle file, AttributeTag tag, void* buffer, size_t size) override;
118  int fenumxattr(FileHandle file, AttributeEnumCallback callback, void* buffer, size_t bufsize) override;
119  int setxattr(const char* path, AttributeTag tag, const void* data, size_t size) override;
120  int getxattr(const char* path, AttributeTag tag, void* buffer, size_t size) override;
121  FileHandle open(const char* path, OpenFlags flags) override;
122  int close(FileHandle file) override;
123  int read(FileHandle file, void* data, size_t size) override;
124  int write(FileHandle file, const void* data, size_t size) override;
125  int lseek(FileHandle file, int offset, SeekOrigin origin) override;
126  int eof(FileHandle file) override;
127  int32_t tell(FileHandle file) override;
128  int ftruncate(FileHandle file, size_t new_size) override;
129  int flush(FileHandle file) override;
130  int rename(const char* oldpath, const char* newpath) override;
131  int remove(const char* path) override;
132  int fremove(FileHandle file) override;
133  int format() override
134  {
135  return Error::ReadOnly;
136  }
137  int check() override
138  {
139  /* We could implement this, but since problems would indicate corrupted firmware
140  * there isn't much we can do other than suggest a re-flashing. This sort of issue
141  * is better resolved externally using a hash of the entire firmware image. */
142  return Error::NotImplemented;
143  }
144 
145 private:
146  int getMd5Hash(FWFileDesc& fd, void* buffer, size_t bufSize);
147 
148  bool isMounted()
149  {
150  return flags[Flag::mounted];
151  }
152 
159  int readObjectHeader(FWObjDesc& od);
160 
168  int getChildObject(const FWObjDesc& parent, const FWObjDesc& child, FWObjDesc& od);
169 
178  int readChildObjectHeader(const FWObjDesc& parent, FWObjDesc& child);
179 
188  int readObjectContent(const FWObjDesc& od, uint32_t offset, uint32_t size, void* buffer);
189 
194  int findUnusedDescriptor();
195 
196  int findChildObjectHeader(const FWObjDesc& parent, FWObjDesc& child, Object::Type objId);
197  int findChildObject(const FWObjDesc& parent, FWObjDesc& child, const char* name, unsigned namelen);
198  int findObject(Object::ID objId, FWObjDesc& od);
199 
207  int findObjectByPath(const char*& path, FWObjDesc& od);
208 
215  int resolveMountPoint(const FWObjDesc& odMountPoint, IFileSystem*& fileSystem);
216 
217  /*
218  * @brief Resolve path to mounted volume.
219  * @param path Path to parse, consumes path to mount point
220  * @param fileSystem Located filesystem
221  * @retval int error code
222  *
223  * Used for methods which require write access are read-only unless path corresponds to mounted volume.
224  * If path is within this volume then Error::ReadOnly is returned.
225  */
226  int findLinkedObject(const char*& path, IFileSystem*& fileSystem);
227 
234  int getObjectDataSize(FWObjDesc& od, size_t& dataSize);
235 
236  int readObjectName(const FWObjDesc& od, NameBuffer& name);
237  int fillStat(Stat& stat, const FWObjDesc& entry);
238  int readAttribute(FWObjDesc& od, AttributeTag tag, void* buffer, size_t size);
239 
240  void printObject(const FWObjDesc& od, bool isChild);
241 
242 private:
243  enum class Flag {
244  mounted,
245  };
246 
247  Storage::Partition partition;
248  FWVolume volumes[FWFS_MAX_VOLUMES];
249  FWFileDesc fileDescriptors[FWFS_MAX_FDS];
250  FWObjDesc odRoot;
251  Object::ID volume;
252  ACL rootACL{};
253  BitSet<uint8_t, Flag> flags;
254 };
255 
256 } // namespace FWFS
257 
258 } // namespace IFS
Basic information about filing system.
Definition: IFileSystem.h:118
#define FWFS_MAX_FDS
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:39
FWObjDesc odFile
File object.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:54
uint32_t dataSize
Total size of data.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:57
ControlCode
See IFS::IFileSystem::fcontrol
Definition: Control.h:31
Installable File System base class.
Definition: IFileSystem.h:95
FWFS Object Descriptor.
Definition: Object.h:449
Object obj
The object structure.
Definition: Object.h:451
struct ImplFileDir * DirHandle
Definition: IFileSystem.h:68
uint32_t cursor
Current read/write offset within file data.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:58
bool isMountPoint() const
Definition: Object.h:230
The String class.
Definition: WString.h:136
uint8_t typeData
Stored type plus flag.
Definition: Object.h:145
uint32_t ID
Object identifier (offset from start of image)
Definition: Object.h:150
Definition: Delegate.h:20
#define FWFS_MAX_VOLUMES
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:44
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
IFileSystem * fileSystem
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:62
Type
Definition: Object.h:152
FileHandle file
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:64
int16_t FileHandle
File handle.
Definition: Stat.h:39
Definition: DirectoryTemplate.h:36
bool isAllocated() const
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:70
Implementation of firmware filing system using IFS.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:96
FWFS Volume definition for mount points.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:89
int fsetxattr(int file, const char *name, const void *value, size_t size, int flags)
File Status structure.
Definition: Stat.h:51
Manage a set of bit values using enumeration.
Definition: BitSet.h:43
FWFS File Descriptor.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:53
void reset()
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:80
int fgetxattr(int file, const char *name, void *value, size_t size)
std::unique_ptr< IFileSystem > fileSystem
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:90
String getErrorString(int err)
int check() override
Perform a file system consistency check.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:137
Represents a flash partition.
Definition: Partition.h:78
AttributeTag
Identifies a specific attribute.
Definition: Attribute.h:45
Definition: Access.h:34
DirHandle dir
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:65
bool isMountPoint() const
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:75
FileSystem(Storage::Partition partition)
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:99
defines a &#39;safe&#39; name buffer
Definition: NameBuffer.h:44
int format() override
format the filing system
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:133