Components/IFS/src/Arch/Host/include/IFS/Host/FileSystem.h
Go to the documentation of this file.
1 /****
2  * FileSystem.h
3  * IFS wrapper for Host (POSIX) file system
4  *
5  * Created on: 11 September 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 <IFS/IFileSystem.h>
26 
27 namespace IFS::Host
28 {
29 struct os_stat_t;
30 
34 class FileSystem : public IFileSystem
35 {
36 public:
37  FileSystem(const char* rootpath = nullptr) : rootpath(rootpath), mounted(!rootpath)
38  {
39  }
40 
41  ~FileSystem() override = default;
42 
43  int mount() override;
44 
45  // IFileSystem methods
46  int getinfo(Info& info) override;
47  String getErrorString(int err) override;
48  int opendir(const char* path, DirHandle& dir) override;
49  int rewinddir(DirHandle dir) override;
50  int readdir(DirHandle dir, Stat& stat) override;
51  int closedir(DirHandle dir) override;
52  int mkdir(const char* path) override;
53  int stat(const char* path, Stat* stat) override;
54  int fstat(FileHandle file, Stat* stat) override;
55  int fsetxattr(FileHandle file, AttributeTag tag, const void* data, size_t size) override;
56  int fgetxattr(FileHandle file, AttributeTag tag, void* buffer, size_t size) override;
57  int fenumxattr(FileHandle file, AttributeEnumCallback callback, void* buffer, size_t bufsize) override;
58  int setxattr(const char* path, AttributeTag tag, const void* data, size_t size) override;
59  int getxattr(const char* path, AttributeTag tag, void* buffer, size_t size) override;
60  FileHandle open(const char* path, OpenFlags flags) override;
61  int close(FileHandle file) override;
62  int read(FileHandle file, void* data, size_t size) override;
63  int write(FileHandle file, const void* data, size_t size) override;
64  file_offset_t lseek(FileHandle file, file_offset_t offset, SeekOrigin origin) override;
65  int eof(FileHandle file) override;
66  file_offset_t tell(FileHandle file) override;
67  int ftruncate(FileHandle file, file_size_t new_size) override;
68  int flush(FileHandle file) override;
69  int rename(const char* oldpath, const char* newpath) override;
70  int remove(const char* path) override;
71  int fremove(FileHandle) override
72  {
73  return Error::NotImplemented;
74  }
75  int format() override
76  {
77  return Error::ReadOnly;
78  }
79  int check() override
80  {
81  return Error::NotImplemented;
82  }
83 
84 private:
85  String resolvePath(const char* path);
86  void fillStat(const os_stat_t& s, Stat& stat);
87  String rootpath;
88  bool mounted;
89 };
90 
91 } // namespace IFS::Host
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
IFS implementation of Host filing system.
Definition: Components/IFS/src/Arch/Host/include/IFS/Host/FileSystem.h:35
String getErrorString(int err) override
get the text for a returned error code
file_offset_t lseek(FileHandle file, file_offset_t offset, SeekOrigin origin) override
change file read/write position
int remove(const char *path) override
remove (delete) a file by path
int fsetxattr(FileHandle file, AttributeTag tag, const void *data, size_t size) override
Set an extended attribute on an open file.
FileHandle open(const char *path, OpenFlags flags) override
open a file (or directory) by path
int fremove(FileHandle) override
remove (delete) a file by handle
Definition: Components/IFS/src/Arch/Host/include/IFS/Host/FileSystem.h:71
int getxattr(const char *path, AttributeTag tag, void *buffer, size_t size) override
Get an attribute from a file given its path.
int mkdir(const char *path) override
Create a directory.
int fstat(FileHandle file, Stat *stat) override
get file information
int getinfo(Info &info) override
get filing system information
int opendir(const char *path, DirHandle &dir) override
open a directory for reading
int fenumxattr(FileHandle file, AttributeEnumCallback callback, void *buffer, size_t bufsize) override
Enumerate attributes.
int format() override
format the filing system
Definition: Components/IFS/src/Arch/Host/include/IFS/Host/FileSystem.h:75
int rename(const char *oldpath, const char *newpath) override
rename a file
int setxattr(const char *path, AttributeTag tag, const void *data, size_t size) override
Set an extended attribute for a file given its path.
int close(FileHandle file) override
close an open file
int flush(FileHandle file) override
flush any buffered data to physical media
int write(FileHandle file, const void *data, size_t size) override
write content to a file at current position and advance cursor
int read(FileHandle file, void *data, size_t size) override
read content from a file and advance cursor
~FileSystem() override=default
int fgetxattr(FileHandle file, AttributeTag tag, void *buffer, size_t size) override
Get an extended attribute from an open file.
int mount() override
Mount file system, performing any required initialisation.
int closedir(DirHandle dir) override
close a directory object
FileSystem(const char *rootpath=nullptr)
Definition: Components/IFS/src/Arch/Host/include/IFS/Host/FileSystem.h:37
int stat(const char *path, Stat *stat) override
get file information
file_offset_t tell(FileHandle file) override
get current file position
int rewinddir(DirHandle dir) override
Reset directory read position to start.
int check() override
Perform a file system consistency check.
Definition: Components/IFS/src/Arch/Host/include/IFS/Host/FileSystem.h:79
int ftruncate(FileHandle file, file_size_t new_size) override
Truncate (reduce) the size of an open file.
int eof(FileHandle file) override
determine if current file position is at end of file
int readdir(DirHandle dir, Stat &stat) override
read a directory entry
Installable File System base class.
Definition: IFileSystem.h:99
The String class.
Definition: WString.h:133
Definition: Components/IFS/src/Arch/Host/include/IFS/Host/FileSystem.h:28
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
Basic information about filing system.
Definition: IFileSystem.h:121
File Status structure.
Definition: Stat.h:52