File.h
Go to the documentation of this file.
1 
20 #pragma once
21 
22 #include "FsBase.h"
23 
24 namespace IFS
25 {
29 class File : public FsBase
30 {
31 public:
32  using FsBase::FsBase;
33 
34  // Common flag combinations
35  static constexpr OpenFlags ReadOnly{OpenFlag::Read};
36  static constexpr OpenFlags WriteOnly{OpenFlag::Write};
37  static constexpr OpenFlags ReadWrite{OpenFlag::Read | OpenFlag::Write};
38  static constexpr OpenFlags Create{OpenFlag::Create};
39  static constexpr OpenFlags Append{OpenFlag::Append};
40  static constexpr OpenFlags Truncate{OpenFlag::Truncate};
41  static constexpr OpenFlags CreateNewAlways{OpenFlag::Create | OpenFlag::Truncate};
42 
44  {
45  close();
46  }
47 
48  explicit operator bool() const
49  {
50  return handle >= 0;
51  }
52 
58  bool stat(Stat& stat)
59  {
60  GET_FS(false);
61  return check(fs->fstat(handle, &stat));
62  }
63 
75  int control(ControlCode code, void* buffer, size_t bufSize)
76  {
78  int res = fs->fcontrol(handle, code, buffer, bufSize);
79  check(res);
80  return res;
81  }
82 
89  template <typename T> bool open(const T& path, OpenFlags flags = OpenFlag::Read)
90  {
91  GET_FS(false);
92  fs->close(handle);
93  handle = fs->open(path, flags);
94  return check(handle);
95  }
96 
101  bool close()
102  {
103  if(handle < 0) {
104  return true;
105  }
106  GET_FS(false);
107  int res = fs->close(handle);
108  handle = -1;
109  return check(res);
110  }
111 
118  int read(void* data, size_t size)
119  {
120  GET_FS(lastError);
121  int res = fs->read(handle, data, size);
122  check(res);
123  return res;
124  }
125 
132  int write(const void* data, size_t size)
133  {
134  GET_FS(lastError);
135  int res = fs->write(handle, data, size);
136  check(res);
137  return res;
138  }
139 
140  bool write(const String& s)
141  {
142  size_t len = s.length();
143  return write(s.c_str(), len) == int(len);
144  }
145 
152  int seek(int offset, SeekOrigin origin)
153  {
154  GET_FS(lastError);
155  int res = fs->lseek(handle, offset, origin);
156  check(res);
157  return res;
158  }
159 
164  bool eof()
165  {
166  auto fs = getFileSystem();
167  if(fs == nullptr) {
168  return true;
169  }
170  int res = fs->eof(handle);
171  check(res);
172  return res != FS_OK;
173  }
174 
179  int32_t tell()
180  {
181  GET_FS(lastError);
182  int res = fs->tell(handle);
183  check(res);
184  return res;
185  }
186 
192  bool truncate(size_t new_size)
193  {
194  GET_FS(lastError);
195  int res = fs->ftruncate(handle, new_size);
196  return check(res);
197  }
198 
203  bool truncate()
204  {
205  GET_FS(lastError);
206  int res = fs->ftruncate(handle);
207  return check(res);
208  }
209 
214  bool flush()
215  {
216  GET_FS(false);
217  return check(fs->flush(handle));
218  }
219 
225  bool setacl(const ACL& acl)
226  {
227  GET_FS(false);
228  return check(fs->setacl(handle, acl));
229  }
230 
236  bool settime(time_t mtime)
237  {
238  GET_FS(false);
239  return check(fs->settime(handle, mtime));
240  }
241 
247  bool setcompression(const Compression& compression)
248  {
249  GET_FS(false);
250  return check(fs->setcompression(handle, compression));
251  }
252 
253  template <typename... ParamTypes> bool setAttribute(AttributeTag tag, ParamTypes... params)
254  {
255  GET_FS(false);
256  return check(fs->setAttribute(handle, tag, params...));
257  }
258 
259  template <typename... ParamTypes> int getAttribute(AttributeTag tag, ParamTypes... params)
260  {
261  GET_FS(lastError);
262  return check(fs->getAttribute(handle, tag, params...));
263  }
264 
265  template <typename... ParamTypes> bool setUserAttribute(uint8_t tagValue, ParamTypes... params)
266  {
267  GET_FS(false);
268  return check(fs->setAttribute(handle, getUserAttributeTag(tagValue), params...));
269  }
270 
271  template <typename... ParamTypes> int getUserAttribute(uint8_t tagValue, ParamTypes... params)
272  {
273  GET_FS(lastError);
274  return check(fs->getUserAttribute(handle, tagValue, params...));
275  }
276 
277  String getUserAttribute(uint8_t tagValue)
278  {
279  GET_FS(nullptr);
280  return fs->getUserAttribute(handle, tagValue);
281  }
282 
283  bool removeUserAttribute(uint8_t tagValue)
284  {
285  GET_FS(false);
286  return check(fs->removeUserAttribute(handle, tagValue));
287  }
288 
289  int enumAttributes(AttributeEnumCallback callback, void* buffer, size_t bufsize)
290  {
291  GET_FS(lastError);
292  int res = fs->fenumxattr(handle, callback, buffer, bufsize);
293  check(res);
294  return res;
295  }
296 
301  bool remove()
302  {
303  GET_FS(false);
304  int res = fs->fremove(handle);
305  if(!check(res)) {
306  return false;
307  }
308  fs->close(handle);
309  handle = -1;
310  return true;
311  }
312 
317  uint32_t getSize()
318  {
319  GET_FS(lastError);
320  return fs->getSize(handle);
321  }
322 
324 
331  int readContent(size_t size, ReadContentCallback callback)
332  {
333  GET_FS(lastError);
334  int res = fs->readContent(handle, size, callback);
335  check(res);
336  return res;
337  }
338 
345  {
346  GET_FS(lastError);
347  int res = fs->readContent(handle, callback);
348  check(res);
349  return res;
350  }
351 
361  {
362  String s;
363  auto len = getSize();
364  if(!s.setLength(len)) {
365  return nullptr;
366  }
367 
368  if(read(s.begin(), len) != int(len)) {
369  return nullptr;
370  }
371 
372  return s;
373  }
374 
375 private:
376  FileHandle handle{-1};
377 };
378 
379 }; // namespace IFS
AttributeTag getUserAttributeTag(uint8_t value)
Definition: Attribute.h:85
bool eof()
determine if current file position is at end of file
Definition: File.h:164
int getUserAttribute(uint8_t tagValue, ParamTypes... params)
Definition: File.h:271
ControlCode
See IFS::IFileSystem::fcontrol
Definition: Control.h:31
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:641
int write(const void *data, size_t size)
write content to a file at current position and advance cursor
Definition: File.h:132
bool flush()
flush any buffered data to physical media
Definition: File.h:214
FsBase(IFileSystem *filesys)
Definition: FsBase.h:36
int seek(int offset, SeekOrigin origin)
change file read/write position
Definition: File.h:152
bool open(const T &path, OpenFlags flags=OpenFlag::Read)
open a file by name/path
Definition: File.h:89
static constexpr OpenFlags Append
Definition: File.h:39
Delegate< int(const char *buffer, size_t size)> ReadContentCallback
Callback for readContent method.
Definition: Components/IFS/src/include/IFS/FileSystem.h:305
bool setacl(const ACL &acl)
Set access control information for file.
Definition: File.h:225
bool write(const String &s)
Definition: File.h:140
static constexpr OpenFlags ReadWrite
Definition: File.h:37
static constexpr OpenFlags ReadOnly
Definition: File.h:35
bool check(int res)
Check file operation result and note error code.
Definition: FsBase.h:76
Wraps up all file access methods.
Definition: File.h:29
int read(void *data, size_t size)
read content from a file and advance cursor
Definition: File.h:118
The String class.
Definition: WString.h:136
bool truncate()
Truncate an open file at the current cursor position.
Definition: File.h:203
bool close()
close an open file
Definition: File.h:101
uint32_t getSize()
Get size of file.
Definition: File.h:317
Definition: Delegate.h:20
char * begin()
Get a modifiable pointer to String content.
Definition: WString.h:650
SeekOrigin
Stream/file seek origins.
Definition: SeekOrigin.h:18
static constexpr OpenFlags WriteOnly
Definition: File.h:36
bool setcompression(const Compression &compression)
Set file compression information.
Definition: File.h:247
int readContent(ReadContentCallback callback)
Read from current file position to end of file and invoke callback for each block read...
Definition: File.h:344
int16_t FileHandle
File handle.
Definition: Stat.h:39
Definition: DirectoryTemplate.h:36
size_t length(void) const
Obtain the String length in characters, excluding NUL terminator.
Definition: WString.h:237
~File()
Definition: File.h:43
bool setLength(size_t length)
set the string length accordingly, expanding if necessary
bool stat(Stat &stat)
get file information
Definition: File.h:58
#define GET_FS(failure)
Definition: FsBase.h:27
bool removeUserAttribute(uint8_t tagValue)
Definition: File.h:283
String getContent()
Read content of a file.
Definition: File.h:360
File Status structure.
Definition: Stat.h:51
Manage a set of bit values using enumeration.
Definition: BitSet.h:43
int32_t tell()
get current file position
Definition: File.h:179
bool settime(time_t mtime)
Set modification time for file.
Definition: File.h:236
String getUserAttribute(uint8_t tagValue)
Definition: File.h:277
int getAttribute(AttributeTag tag, ParamTypes... params)
Definition: File.h:259
AttributeTag
Identifies a specific attribute.
Definition: Attribute.h:45
Definition: Access.h:34
int enumAttributes(AttributeEnumCallback callback, void *buffer, size_t bufsize)
Definition: File.h:289
bool truncate(size_t new_size)
Truncate (reduce) the size of an open file.
Definition: File.h:192
static constexpr OpenFlags CreateNewAlways
Definition: File.h:41
bool setAttribute(AttributeTag tag, ParamTypes... params)
Definition: File.h:253
int readContent(size_t size, ReadContentCallback callback)
Read from current file position and invoke callback for each block read.
Definition: File.h:331
int control(ControlCode code, void *buffer, size_t bufSize)
Low-level and non-standard file control operations.
Definition: File.h:75
Definition: FsBase.h:33
FileSystem * getFileSystem() const
Definition: FsBase.h:63
constexpr ErrorCode FS_OK
Definition: Error.h:128
int lastError
Definition: FsBase.h:87
static constexpr OpenFlags Create
Definition: File.h:38
bool setUserAttribute(uint8_t tagValue, ParamTypes... params)
Definition: File.h:265
static constexpr OpenFlags Truncate
Definition: File.h:40