Components/IFS/src/include/IFS/FileSystem.h
Go to the documentation of this file.
1 /****
2  * FileSystem.h
3  *
4  * Created August 2018 by mikee471
5  *
6  * Copyright 2019 mikee47 <mike@sillyhouse.net>
7  *
8  * This file is part of the IFS Library
9  *
10  * This library is free software: you can redistribute it and/or modify it under the terms of the
11  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
12  *
13  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  * See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with this library.
18  * If not, see <https://www.gnu.org/licenses/>.
19  *
20  ****/
21 
27 #pragma once
28 
29 #include "IFileSystem.h"
30 #include <Delegate.h>
31 
32 namespace IFS
33 {
39 class FileSystem : public IFileSystem
40 {
41 public:
42  static constexpr FileSystem& cast(IFileSystem& fs)
43  {
44  return static_cast<FileSystem&>(fs);
45  }
46 
47  static constexpr FileSystem* cast(IFileSystem* fs)
48  {
49  return static_cast<FileSystem*>(fs);
50  }
51 
56  int opendir(const String& path, DirHandle& dir)
57  {
58  return opendir(path.c_str(), dir);
59  }
60 
61  using IFileSystem::mkdir;
62 
63  int mkdir(const String& path)
64  {
65  return mkdir(path.c_str());
66  }
67 
73  int makedirs(const char* path);
74 
75  int makedirs(const String& path)
76  {
77  return makedirs(path.c_str());
78  }
79 
80  using IFileSystem::stat;
81  int stat(const String& path, Stat* s)
82  {
83  return stat(path.c_str(), s);
84  }
85  int stat(const String& path, Stat& s)
86  {
87  return stat(path.c_str(), &s);
88  }
89 
90  using IFileSystem::fstat;
91  int fstat(FileHandle file, Stat& stat)
92  {
93  return fstat(file, &stat);
94  }
95 
96  using IFileSystem::open;
97  FileHandle open(const String& path, OpenFlags flags = OpenFlag::Read)
98  {
99  return open(path.c_str(), flags);
100  }
101 
104  {
105  auto pos = tell(file);
106  return (pos < 0) ? pos : ftruncate(file, pos);
107  }
108 
114  int truncate(const char* fileName, file_size_t newSize);
115 
116  int truncate(const String& fileName, file_size_t newSize)
117  {
118  return truncate(fileName.c_str(), newSize);
119  }
120 
121  using IFileSystem::rename;
129  int rename(const String& oldpath, const String& newpath)
130  {
131  return rename(oldpath.c_str(), newpath.c_str());
132  }
133 
134  using IFileSystem::remove;
140  int remove(const String& path)
141  {
142  return remove(path.c_str());
143  }
144 
145  int setAttribute(FileHandle file, AttributeTag tag, const void* data, size_t size)
146  {
147  return fsetxattr(file, tag, data, size);
148  }
149 
150  int setAttribute(const char* file, AttributeTag tag, const void* data, size_t size)
151  {
152  return setxattr(file, tag, data, size);
153  }
154 
155  int setAttribute(const String& file, AttributeTag tag, const void* data, size_t size)
156  {
157  return setxattr(file.c_str(), tag, data, size);
158  }
159 
160  template <typename T> int setAttribute(const T& file, AttributeTag tag, const String& data)
161  {
162  return setAttribute(file, tag, data.c_str(), data.length());
163  }
164 
165  int getAttribute(FileHandle file, AttributeTag tag, void* buffer, size_t size)
166  {
167  return fgetxattr(file, tag, buffer, size);
168  }
169 
170  int getAttribute(const char* file, AttributeTag tag, void* buffer, size_t size)
171  {
172  return getxattr(file, tag, buffer, size);
173  }
174 
175  int getAttribute(const String& file, AttributeTag tag, void* buffer, size_t size)
176  {
177  return getxattr(file.c_str(), tag, buffer, size);
178  }
179 
180  template <typename T> int getAttribute(const T& file, AttributeTag tag, String& value)
181  {
182  char buffer[256];
183  int len = getAttribute(file, tag, buffer, sizeof(buffer));
184  if(len != Error::BufferTooSmall) {
185  if(len >= 0) {
186  value.setString(buffer, len);
187  if(!value) {
188  return Error::NoMem;
189  }
190  }
191  return len;
192  }
193  if(!value.setLength(len)) {
194  return Error::NoMem;
195  }
196  return getAttribute(file, tag, value.begin(), value.length());
197  }
198 
199  template <typename T> String getAttribute(const T& file, AttributeTag tag)
200  {
201  String value;
202  int err = getAttribute(file, tag, value);
203  return (err < 0) ? nullptr : value;
204  }
205 
206  template <typename T> int removeAttribute(const T& file, AttributeTag tag)
207  {
208  return setAttribute(file, tag, nullptr, 0);
209  }
210 
211  template <typename T, typename... ParamTypes>
212  int setUserAttribute(const T& file, uint8_t tagValue, ParamTypes... params)
213  {
214  return setAttribute(file, getUserAttributeTag(tagValue), params...);
215  }
216 
217  template <typename T, typename... ParamTypes>
218  int getUserAttribute(const T& file, uint8_t tagValue, ParamTypes... params)
219  {
220  return getAttribute(file, getUserAttributeTag(tagValue), params...);
221  }
222 
223  template <typename T> String getUserAttribute(const T& file, uint8_t tagValue)
224  {
225  return getAttribute(file, getUserAttributeTag(tagValue));
226  }
227 
228  template <typename T> bool removeUserAttribute(const T& file, uint8_t tagValue)
229  {
230  return removeAttribute(file, getUserAttributeTag(tagValue));
231  }
232 
239  template <typename T> int setacl(const T& file, const ACL& acl)
240  {
241  int res1 = setAttribute(file, AttributeTag::ReadAce, &acl.readAccess, sizeof(acl.readAccess));
242  int res2 = setAttribute(file, AttributeTag::WriteAce, &acl.writeAccess, sizeof(acl.writeAccess));
243  return std::min(res1, res2);
244  }
245 
252  template <typename T> int setattr(const T& file, FileAttributes attr)
253  {
254  return setAttribute(file, AttributeTag::FileAttributes, &attr, sizeof(attr));
255  }
256 
263  template <typename T> int settime(const T& file, time_t mtime)
264  {
265  TimeStamp ts;
266  ts = mtime;
267  return setAttribute(file, AttributeTag::ModifiedTime, &ts, sizeof(ts));
268  }
269 
276  template <typename T> int setcompression(const T& file, const Compression& compression)
277  {
278  return setAttribute(file, AttributeTag::Compression, &compression, sizeof(compression));
279  }
280 
286 
291  file_size_t getSize(const char* fileName);
292 
293  file_size_t getSize(const String& fileName)
294  {
295  return getSize(fileName.c_str());
296  }
297 
305  using ReadContentCallback = Delegate<int(const char* buffer, size_t size)>;
306 
314  file_offset_t readContent(FileHandle file, size_t size, const ReadContentCallback& callback);
315 
323 
330  file_offset_t readContent(const String& filename, const ReadContentCallback& callback);
331 
348  size_t getContent(const char* fileName, char* buffer, size_t bufSize);
349 
350  size_t getContent(const String& fileName, char* buffer, size_t bufSize)
351  {
352  return getContent(fileName.c_str(), buffer, bufSize);
353  }
354 
365  String getContent(const String& fileName);
366 
379  int setContent(const char* fileName, const void* content, size_t length);
380 
381  int setContent(const char* fileName, const char* content)
382  {
383  return setContent(fileName, content, (content == nullptr) ? 0 : strlen(content));
384  }
385 
386  int setContent(const String& fileName, const char* content)
387  {
388  return setContent(fileName.c_str(), content);
389  }
390 
391  int setContent(const String& fileName, const void* content, size_t length)
392  {
393  return setContent(fileName.c_str(), content, length);
394  }
395 
396  int setContent(const String& fileName, const String& content)
397  {
398  return setContent(fileName.c_str(), content.c_str(), content.length());
399  }
400 
402 };
403 
404 #ifdef ARCH_HOST
405 namespace Host
406 {
408 }
409 #endif
410 
415 
416 } // namespace IFS
417 
int32_t file_offset_t
Definition: Components/IFS/src/include/IFS/Types.h:51
uint32_t file_size_t
Definition: Components/IFS/src/include/IFS/Types.h:50
Manage a set of bit values using enumeration.
Definition: BitSet.h:45
Definition: Delegate.h:20
Installable File System base class.
Definition: Components/IFS/src/include/IFS/FileSystem.h:40
String getAttribute(const T &file, AttributeTag tag)
Definition: Components/IFS/src/include/IFS/FileSystem.h:199
int getAttribute(const char *file, AttributeTag tag, void *buffer, size_t size)
Definition: Components/IFS/src/include/IFS/FileSystem.h:170
int makedirs(const char *path)
Create a directory and any intermediate directories if they do not already exist.
int removeAttribute(const T &file, AttributeTag tag)
Definition: Components/IFS/src/include/IFS/FileSystem.h:206
virtual int rename(const char *oldpath, const char *newpath)=0
rename a file
int settime(const T &file, time_t mtime)
Set modification time for file.
Definition: Components/IFS/src/include/IFS/FileSystem.h:263
int setContent(const String &fileName, const char *content)
Definition: Components/IFS/src/include/IFS/FileSystem.h:386
int setattr(const T &file, FileAttributes attr)
Set file attributes.
Definition: Components/IFS/src/include/IFS/FileSystem.h:252
int setcompression(const T &file, const Compression &compression)
Set file compression information.
Definition: Components/IFS/src/include/IFS/FileSystem.h:276
bool removeUserAttribute(const T &file, uint8_t tagValue)
Definition: Components/IFS/src/include/IFS/FileSystem.h:228
virtual int opendir(const char *path, DirHandle &dir)=0
open a directory for reading
virtual int remove(const char *path)=0
remove (delete) a file by path
int stat(const String &path, Stat *s)
Definition: Components/IFS/src/include/IFS/FileSystem.h:81
file_size_t getSize(const String &fileName)
Definition: Components/IFS/src/include/IFS/FileSystem.h:293
virtual int stat(const char *path, Stat *stat)=0
get file information
file_offset_t readContent(const String &filename, const ReadContentCallback &callback)
Read entire file content in blocks, invoking callback after every read.
int setContent(const char *fileName, const char *content)
Definition: Components/IFS/src/include/IFS/FileSystem.h:381
virtual int fstat(FileHandle file, Stat *stat)=0
get file information
size_t getContent(const char *fileName, char *buffer, size_t bufSize)
file_offset_t readContent(FileHandle file, const ReadContentCallback &callback)
Read from current file position to end of file and invoke callback for each block read.
FileHandle open(const String &path, OpenFlags flags=OpenFlag::Read)
Definition: Components/IFS/src/include/IFS/FileSystem.h:97
int getUserAttribute(const T &file, uint8_t tagValue, ParamTypes... params)
Definition: Components/IFS/src/include/IFS/FileSystem.h:218
int setContent(const char *fileName, const void *content, size_t length)
int setContent(const String &fileName, const void *content, size_t length)
Definition: Components/IFS/src/include/IFS/FileSystem.h:391
int ftruncate(FileHandle file)
Definition: Components/IFS/src/include/IFS/FileSystem.h:103
size_t getContent(const String &fileName, char *buffer, size_t bufSize)
Definition: Components/IFS/src/include/IFS/FileSystem.h:350
int makedirs(const String &path)
Definition: Components/IFS/src/include/IFS/FileSystem.h:75
String getContent(const String &fileName)
Read content of a file.
int setAttribute(const char *file, AttributeTag tag, const void *data, size_t size)
Definition: Components/IFS/src/include/IFS/FileSystem.h:150
int setUserAttribute(const T &file, uint8_t tagValue, ParamTypes... params)
Definition: Components/IFS/src/include/IFS/FileSystem.h:212
int opendir(const String &path, DirHandle &dir)
open a directory for reading
Definition: Components/IFS/src/include/IFS/FileSystem.h:56
int setAttribute(const String &file, AttributeTag tag, const void *data, size_t size)
Definition: Components/IFS/src/include/IFS/FileSystem.h:155
virtual int ftruncate(FileHandle file, file_size_t new_size)=0
Truncate (reduce) the size of an open file.
int setContent(const String &fileName, const String &content)
Definition: Components/IFS/src/include/IFS/FileSystem.h:396
file_size_t getSize(FileHandle file)
Get size of file.
int getAttribute(const String &file, AttributeTag tag, void *buffer, size_t size)
Definition: Components/IFS/src/include/IFS/FileSystem.h:175
int stat(const String &path, Stat &s)
Definition: Components/IFS/src/include/IFS/FileSystem.h:85
int setAttribute(FileHandle file, AttributeTag tag, const void *data, size_t size)
Definition: Components/IFS/src/include/IFS/FileSystem.h:145
file_offset_t readContent(FileHandle file, size_t size, const ReadContentCallback &callback)
Read from current file position and invoke callback for each block read.
int setAttribute(const T &file, AttributeTag tag, const String &data)
Definition: Components/IFS/src/include/IFS/FileSystem.h:160
int rename(const String &oldpath, const String &newpath)
Definition: Components/IFS/src/include/IFS/FileSystem.h:129
int fstat(FileHandle file, Stat &stat)
Definition: Components/IFS/src/include/IFS/FileSystem.h:91
int truncate(const char *fileName, file_size_t newSize)
Truncate a file to a specific size.
String getUserAttribute(const T &file, uint8_t tagValue)
Definition: Components/IFS/src/include/IFS/FileSystem.h:223
int truncate(const String &fileName, file_size_t newSize)
Definition: Components/IFS/src/include/IFS/FileSystem.h:116
int getAttribute(const T &file, AttributeTag tag, String &value)
Definition: Components/IFS/src/include/IFS/FileSystem.h:180
static constexpr FileSystem * cast(IFileSystem *fs)
Definition: Components/IFS/src/include/IFS/FileSystem.h:47
virtual FileHandle open(const char *path, OpenFlags flags)=0
open a file (or directory) by path
int getAttribute(FileHandle file, AttributeTag tag, void *buffer, size_t size)
Definition: Components/IFS/src/include/IFS/FileSystem.h:165
virtual int mkdir(const char *path)=0
Create a directory.
int mkdir(const String &path)
Definition: Components/IFS/src/include/IFS/FileSystem.h:63
int remove(const String &path)
remove (delete) a file by path
Definition: Components/IFS/src/include/IFS/FileSystem.h:140
int setacl(const T &file, const ACL &acl)
Set access control information for file.
Definition: Components/IFS/src/include/IFS/FileSystem.h:239
file_size_t getSize(const char *fileName)
Get size of file.
static constexpr FileSystem & cast(IFileSystem &fs)
Definition: Components/IFS/src/include/IFS/FileSystem.h:42
IFS implementation of Host filing system.
Definition: Components/IFS/src/Arch/Host/include/IFS/Host/FileSystem.h:37
Installable File System base class.
Definition: IFileSystem.h:100
virtual file_offset_t tell(FileHandle file)=0
get current file position
virtual int fsetxattr(FileHandle file, AttributeTag tag, const void *data, size_t size)=0
Set an extended attribute on an open file.
virtual int rename(const char *oldpath, const char *newpath)=0
rename a file
virtual int opendir(const char *path, DirHandle &dir)=0
open a directory for reading
virtual int remove(const char *path)=0
remove (delete) a file by path
virtual int stat(const char *path, Stat *stat)=0
get file information
virtual int fstat(FileHandle file, Stat *stat)=0
get file information
virtual int getxattr(const char *path, AttributeTag tag, void *buffer, size_t size)=0
Get an attribute from a file given its path.
virtual int fgetxattr(FileHandle file, AttributeTag tag, void *buffer, size_t size)=0
Get an extended attribute from an open file.
virtual int ftruncate(FileHandle file, file_size_t new_size)=0
Truncate (reduce) the size of an open file.
virtual int setxattr(const char *path, AttributeTag tag, const void *data, size_t size)=0
Set an extended attribute for a file given its path.
virtual FileHandle open(const char *path, OpenFlags flags)=0
open a file (or directory) by path
virtual int mkdir(const char *path)=0
Create a directory.
The String class.
Definition: WString.h:137
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:617
bool setLength(size_t length)
set the string length accordingly, expanding if necessary
void setString(const char *cstr)
char * begin()
Get a modifiable pointer to String content.
Definition: WString.h:626
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:244
IFS::FileAttributes FileAttributes
Definition: Core/FileSystem.h:29
Definition: Streams.h:25
FileSystem & getFileSystem()
Definition: DirectoryTemplate.h:37
struct ImplFileDir * DirHandle
Definition: IFileSystem.h:72
AttributeTag
Identifies a specific attribute.
Definition: Attribute.h:45
FileSystem * getDefaultFileSystem()
Framework should implement this method.
AttributeTag getUserAttributeTag(uint8_t value)
Definition: Attribute.h:85
int16_t FileHandle
File handle.
Definition: Stat.h:40
Definition: Access.h:34
UserRole writeAccess
Definition: Access.h:37
UserRole readAccess
Definition: Access.h:36
A compression descriptor.
Definition: Compression.h:38
File Status structure.
Definition: Stat.h:52
Manage IFS timestamps stored as an unsigned 32-bit value.
Definition: TimeStamp.h:37