Components/IFS/src/include/IFS/FWFS/FileSystem.h
Go to the documentation of this file.
1 /****
2  * FileSystem.h
3  * FWFS - Firmware File System
4  *
5  * Created on: 19 Jul 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 
23 #pragma once
24 
25 #include "../IFileSystem.h"
26 #include "Object.h"
27 
28 namespace IFS::FWFS
29 {
30 // File handles start at this value
31 #ifndef FWFS_HANDLE_MIN
32 #define FWFS_HANDLE_MIN 100
33 #endif
34 
35 // Maximum number of file descriptors
36 #ifndef FWFS_MAX_FDS
37 #define FWFS_MAX_FDS 8
38 #endif
39 
40 // Maximum number of volumes - 1 is minimum, the rest are mounted in subdirectories
41 #ifndef FWFS_MAX_VOLUMES
42 #define FWFS_MAX_VOLUMES 4
43 #endif
44 
45 // Maximum file handle value
46 #define FWFS_HANDLE_MAX (FWFS_HANDLE_MIN + FWFS_MAX_FDS - 1)
47 
51 struct FWFileDesc {
53  union {
54  struct {
55  uint32_t dataSize;
56  uint32_t cursor;
57  };
58  // For MountPoint
59  struct {
61  union {
64  };
65  };
66  };
67 
68  bool isAllocated() const
69  {
70  return odFile.obj.typeData != 0;
71  }
72 
73  bool isMountPoint() const
74  {
75  return odFile.obj.isMountPoint();
76  }
77 
78  void reset()
79  {
80  *this = FWFileDesc{};
81  }
82 };
83 
87 struct FWVolume {
88  std::unique_ptr<IFileSystem> fileSystem;
89 };
90 
94 class FileSystem : public IFileSystem
95 {
96 public:
98  {
99  }
100 
101  // IFileSystem methods
102  int mount() override;
103  int getinfo(Info& info) override;
104  String getErrorString(int err) override;
105  int setVolume(uint8_t index, IFileSystem* fileSystem) override;
106  int opendir(const char* path, DirHandle& dir) override;
107  int readdir(DirHandle dir, Stat& stat) override;
108  int rewinddir(DirHandle dir) override;
109  int closedir(DirHandle dir) override;
110  int mkdir(const char* path) override;
111  int stat(const char* path, Stat* stat) override;
112  int fstat(FileHandle file, Stat* stat) override;
113  int fcontrol(FileHandle file, ControlCode code, void* buffer, size_t bufSize) override;
114  int fsetxattr(FileHandle file, AttributeTag tag, const void* data, size_t size) override;
115  int fgetxattr(FileHandle file, AttributeTag tag, void* buffer, size_t size) override;
116  int fenumxattr(FileHandle file, AttributeEnumCallback callback, void* buffer, size_t bufsize) override;
117  int setxattr(const char* path, AttributeTag tag, const void* data, size_t size) override;
118  int getxattr(const char* path, AttributeTag tag, void* buffer, size_t size) override;
119  FileHandle open(const char* path, OpenFlags flags) override;
120  int close(FileHandle file) override;
121  int read(FileHandle file, void* data, size_t size) override;
122  int write(FileHandle file, const void* data, size_t size) override;
123  file_offset_t lseek(FileHandle file, file_offset_t offset, SeekOrigin origin) override;
124  int eof(FileHandle file) override;
125  file_offset_t tell(FileHandle file) override;
126  int ftruncate(FileHandle file, file_size_t new_size) override;
127  int flush(FileHandle file) override;
128  int fgetextents(FileHandle file, Storage::Partition* part, Extent* list, uint16_t extcount) override;
129  int rename(const char* oldpath, const char* newpath) override;
130  int remove(const char* path) override;
131  int fremove(FileHandle file) override;
132  int format() override
133  {
134  return Error::ReadOnly;
135  }
136  int check() override
137  {
138  /* We could implement this, but since problems would indicate corrupted firmware
139  * there isn't much we can do other than suggest a re-flashing. This sort of issue
140  * is better resolved externally using a hash of the entire firmware image. */
141  return Error::NotImplemented;
142  }
143 
144 private:
145  int getMd5Hash(FWFileDesc& fd, void* buffer, size_t bufSize);
146 
147  bool isMounted()
148  {
149  return flags[Flag::mounted];
150  }
151 
158  int readObjectHeader(FWObjDesc& od);
159 
167  int getChildObject(const FWObjDesc& parent, const FWObjDesc& child, FWObjDesc& od);
168 
177  int readChildObjectHeader(const FWObjDesc& parent, FWObjDesc& child);
178 
187  int readObjectContent(const FWObjDesc& od, uint32_t offset, uint32_t size, void* buffer);
188 
193  int findUnusedDescriptor();
194 
195  int findChildObjectHeader(const FWObjDesc& parent, FWObjDesc& child, Object::Type objId);
196  int findChildObject(const FWObjDesc& parent, FWObjDesc& child, const char* name, unsigned namelen);
197  int findObject(Object::ID objId, FWObjDesc& od);
198 
206  int findObjectByPath(const char*& path, FWObjDesc& od);
207 
214  int resolveMountPoint(const FWObjDesc& odMountPoint, IFileSystem*& fileSystem);
215 
216  /*
217  * @brief Resolve path to mounted volume.
218  * @param path Path to parse, consumes path to mount point
219  * @param fileSystem Located filesystem
220  * @retval int error code
221  *
222  * Used for methods which require write access are read-only unless path corresponds to mounted volume.
223  * If path is within this volume then Error::ReadOnly is returned.
224  */
225  int findLinkedObject(const char*& path, IFileSystem*& fileSystem);
226 
233  int getObjectDataSize(FWObjDesc& od, uint32_t& dataSize);
234 
235  int readObjectName(const FWObjDesc& od, NameBuffer& name);
236  int fillStat(Stat& stat, const FWObjDesc& entry);
237  int readAttribute(FWObjDesc& od, AttributeTag tag, void* buffer, size_t size);
238 
239  void printObject(const FWObjDesc& od, bool isChild);
240 
241 protected:
242  enum class Flag {
243  mounted,
244  };
245 
253 };
254 
255 } // namespace IFS::FWFS
#define FWFS_MAX_VOLUMES
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:42
#define FWFS_MAX_FDS
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:37
int32_t file_offset_t
Definition: Components/IFS/src/include/IFS/Types.h:49
uint32_t file_size_t
Definition: Components/IFS/src/include/IFS/Types.h:48
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
Manage a set of bit values using enumeration.
Definition: BitSet.h:45
Delegate class, encapsulates a std::function Added constructor template implements lambda callback wh...
Definition: Delegate.h:24
Implementation of firmware filing system using IFS.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:95
file_offset_t lseek(FileHandle file, file_offset_t offset, SeekOrigin origin) override
change file read/write position
FileSystem(Storage::Partition partition)
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:97
int stat(const char *path, Stat *stat) override
get file information
file_offset_t tell(FileHandle file) override
get current file position
int mount() override
Mount file system, performing any required initialisation.
FWObjDesc odRoot
Reference to root directory object.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:249
int rewinddir(DirHandle dir) override
Reset directory read position to start.
int close(FileHandle file) override
close an open file
int mkdir(const char *path) override
Create a directory.
int write(FileHandle file, const void *data, size_t size) override
write content to a file at current position and advance cursor
int fremove(FileHandle file) override
remove (delete) a file by handle
int ftruncate(FileHandle file, file_size_t new_size) override
Truncate (reduce) the size of an open file.
int rename(const char *oldpath, const char *newpath) override
rename a file
String getErrorString(int err) override
get the text for a returned error code
int fstat(FileHandle file, Stat *stat) override
get file information
Flag
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:242
int remove(const char *path) override
remove (delete) a file by path
int check() override
Perform a file system consistency check.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:136
BitSet< uint8_t, Flag > flags
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:252
FWVolume volumes[FWFS_MAX_VOLUMES]
Volumes mapped to mountpoints by index.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:247
Storage::Partition partition
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:246
int format() override
format the filing system
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:132
int flush(FileHandle file) override
flush any buffered data to physical media
int setVolume(uint8_t index, IFileSystem *fileSystem) override
int setxattr(const char *path, AttributeTag tag, const void *data, size_t size) override
Set an extended attribute for a file given its path.
ACL rootACL
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:251
int fenumxattr(FileHandle file, AttributeEnumCallback callback, void *buffer, size_t bufsize) override
Enumerate attributes.
int fsetxattr(FileHandle file, AttributeTag tag, const void *data, size_t size) override
Set an extended attribute on an open file.
int fcontrol(FileHandle file, ControlCode code, void *buffer, size_t bufSize) override
int opendir(const char *path, DirHandle &dir) override
open a directory for reading
int eof(FileHandle file) override
determine if current file position is at end of file
int fgetextents(FileHandle file, Storage::Partition *part, Extent *list, uint16_t extcount) override
int read(FileHandle file, void *data, size_t size) override
read content from a file and advance cursor
FWFileDesc fileDescriptors[FWFS_MAX_FDS]
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:248
Object::ID volume
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:250
int getxattr(const char *path, AttributeTag tag, void *buffer, size_t size) override
Get an attribute from a file given its path.
FileHandle open(const char *path, OpenFlags flags) override
open a file (or directory) by path
int closedir(DirHandle dir) override
close a directory object
int readdir(DirHandle dir, Stat &stat) override
read a directory entry
int getinfo(Info &info) override
get filing system information
int fgetxattr(FileHandle file, AttributeTag tag, void *buffer, size_t size) override
Get an extended attribute from an open file.
Installable File System base class.
Definition: IFileSystem.h:99
Represents a flash partition.
Definition: Partition.h:86
The String class.
Definition: WString.h:133
Definition: Components/IFS/src/include/IFS/FWFS/ArchiveStream.h:28
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
Definition: Access.h:34
Defines the location of a contiguous run of file data.
Definition: Extent.h:40
FWFS File Descriptor.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:51
FWObjDesc odFile
File object.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:52
FileHandle file
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:62
bool isAllocated() const
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:68
IFileSystem * fileSystem
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:60
void reset()
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:78
uint32_t dataSize
Total size of data.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:55
DirHandle dir
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:63
uint32_t cursor
Current read/write offset within file data.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:56
bool isMountPoint() const
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:73
FWFS Object Descriptor.
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:446
Object obj
The object structure.
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:448
FWFS Volume definition for mount points.
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:87
std::unique_ptr< IFileSystem > fileSystem
Definition: Components/IFS/src/include/IFS/FWFS/FileSystem.h:88
bool isMountPoint() const
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:227
Type
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:150
uint8_t typeData
Stored type plus flag.
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:143
uint32_t ID
Object identifier (offset from start of image)
Definition: Components/IFS/src/include/IFS/FWFS/Object.h:148
Basic information about filing system.
Definition: IFileSystem.h:121
defines a 'safe' name buffer
Definition: NameBuffer.h:44
File Status structure.
Definition: Stat.h:52