Core/FileSystem.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * FileSystem.h
8  *
9  ****/
10 
16 #pragma once
17 
18 #include <IFS/Helpers.h>
19 #include <IFS/File.h>
20 #include <IFS/Directory.h>
21 #include <Spiffs.h>
22 
32 using File = IFS::File;
34 constexpr int FS_OK = IFS::FS_OK;
35 
36 namespace SmingInternal
37 {
47 
48 } // namespace SmingInternal
49 
50 /*
51  * Boilerplate check for file function wrappers to catch undefined filesystem.
52  */
53 #define CHECK_FS(_method) \
54  auto fileSystem = static_cast<IFS::FileSystem*>(SmingInternal::activeFileSystem); \
55  if(fileSystem == nullptr) { \
56  debug_e("ERROR in %s(): No active file system", __FUNCTION__); \
57  return FileHandle(IFS::Error::NoFileSystem); \
58  }
59 
65 {
66  if(SmingInternal::activeFileSystem == nullptr) {
67  debug_e("ERROR: No active file system");
68  }
70 }
71 
81 
82 inline void fileFreeFileSystem()
83 {
84  fileSetFileSystem(nullptr);
85 }
86 
91 
96 bool fwfs_mount();
97 
103 
109 inline IFS::FileSystem* fileMountArchive(const String& filename)
110 {
111  auto fs = getFileSystem();
112  return fs ? IFS::mountArchive(*fs, filename) : nullptr;
113 }
114 
120 template <typename T> inline FileHandle fileOpen(const T& path, FileOpenFlags flags = File::ReadOnly)
121 {
122  CHECK_FS(open)
123  return fileSystem->open(path, flags);
124 }
125 
130 inline int fileClose(FileHandle file)
131 {
132  CHECK_FS(close)
133  return fileSystem->close(file);
134 }
135 
142 inline int fileWrite(FileHandle file, const void* data, size_t size)
143 {
144  CHECK_FS(write);
145  return fileSystem->write(file, data, size);
146 }
147 
152 inline int fileTouch(FileHandle file)
153 {
154  return fileWrite(file, nullptr, 0);
155 }
156 
163 inline int fileRead(FileHandle file, void* data, size_t size)
164 {
165  CHECK_FS(read)
166  return fileSystem->read(file, data, size);
167 }
168 
176 {
177  CHECK_FS(seek)
178  return fileSystem->lseek(file, offset, origin);
179 }
180 
185 inline bool fileIsEOF(FileHandle file)
186 {
187  auto fileSystem = getFileSystem();
188  return fileSystem ? (fileSystem->eof(file) != 0) : true;
189 }
190 
196 {
197  CHECK_FS(tell)
198  return fileSystem->tell(file);
199 }
200 
205 inline int fileFlush(FileHandle file)
206 {
207  CHECK_FS(flush)
208  return fileSystem->flush(file);
209 }
210 
215 inline String fileGetErrorString(int err)
216 {
217  auto fileSystem = getFileSystem();
218  if(fileSystem == nullptr) {
219  return IFS::Error::toString(err);
220  }
221  return fileSystem->getErrorString(err);
222 }
223 
232 template <typename TFileName> inline int fileSetContent(const TFileName& fileName, const char* content, size_t length)
233 {
234  CHECK_FS(setContent)
235  return fileSystem->setContent(fileName, content, length);
236 }
237 
238 template <typename TFileName, typename TContent>
239 inline int fileSetContent(const TFileName& fileName, const TContent& content)
240 {
241  CHECK_FS(setContent)
242  return fileSystem->setContent(fileName, content);
243 }
244 
249 template <typename TFileName> inline file_size_t fileGetSize(const TFileName& fileName)
250 {
251  auto fileSystem = getFileSystem();
252  return fileSystem ? fileSystem->getSize(fileName) : 0;
253 }
254 
260 inline int fileTruncate(FileHandle file, file_size_t newSize)
261 {
262  CHECK_FS(truncate);
263  return fileSystem->ftruncate(file, newSize);
264 }
265 
270 inline int fileTruncate(FileHandle file)
271 {
272  CHECK_FS(truncate);
273  return fileSystem->ftruncate(file);
274 }
275 
281 template <typename TFileName> int fileTruncate(const TFileName& fileName, file_size_t newSize)
282 {
283  CHECK_FS(truncate);
284  return fileSystem->truncate(fileName, newSize);
285 }
286 
292 inline int fileRename(const char* oldName, const char* newName)
293 {
294  CHECK_FS(rename)
295  return fileSystem->rename(oldName, newName);
296 }
297 
298 inline int fileRename(const String& oldName, const String& newName)
299 {
300  return fileRename(oldName.c_str(), newName.c_str());
301 }
302 
310 template <typename TFileName> String fileGetContent(const TFileName& fileName)
311 {
312  auto fileSystem = getFileSystem();
313  return fileSystem ? fileSystem->getContent(fileName) : nullptr;
314 }
315 
327 template <typename TFileName> inline size_t fileGetContent(const TFileName& fileName, char* buffer, size_t bufSize)
328 {
329  auto fileSystem = getFileSystem();
330  return fileSystem ? fileSystem->getContent(fileName, buffer, bufSize) : 0;
331 }
332 
333 template <typename TFileName> inline size_t fileGetContent(const TFileName& fileName, char* buffer)
334 {
335  auto fileSystem = getFileSystem();
336  return fileSystem ? fileSystem->getContent(fileName, buffer) : 0;
337 }
338 
344 inline int fileStats(const char* fileName, FileStat& stat)
345 {
346  CHECK_FS(stat)
347  return fileSystem->stat(fileName, &stat);
348 }
349 
350 inline int fileStats(const String& fileName, FileStat& stat)
351 {
352  return fileStats(fileName.c_str(), stat);
353 }
354 
360 inline int fileStats(FileHandle file, FileStat& stat)
361 {
362  CHECK_FS(fstat)
363  return fileSystem->fstat(file, &stat);
364 }
365 
370 inline int fileDelete(const char* fileName)
371 {
372  CHECK_FS(remove)
373  return fileSystem->remove(fileName);
374 }
375 
376 inline int fileDelete(const String& fileName)
377 {
378  return fileDelete(fileName.c_str());
379 }
380 
385 inline int fileDelete(FileHandle file)
386 {
387  CHECK_FS(fremove)
388  return fileSystem->fremove(file);
389 }
390 
395 inline bool fileExist(const char* fileName)
396 {
397  CHECK_FS(stat)
398  FileStat stat;
399  return fileSystem->stat(fileName, &stat) >= 0 && !stat.attr[FileAttribute::Directory];
400 }
401 
402 inline bool fileExist(const String& fileName)
403 {
404  return fileExist(fileName.c_str());
405 }
406 
411 inline bool dirExist(const char* dirName)
412 {
413  CHECK_FS(stat)
414  FileStat stat;
415  return fileSystem->stat(dirName, &stat) >= 0 && stat.attr[FileAttribute::Directory];
416 }
417 
418 inline bool dirExist(const String& dirName)
419 {
420  return dirExist(dirName.c_str());
421 }
422 
429 inline int fileOpenDir(const char* dirName, DirHandle& dir)
430 {
431  CHECK_FS(opendir)
432  return fileSystem->opendir(dirName, dir);
433 }
434 
435 inline int fileOpenDir(const String& dirName, DirHandle& dir)
436 {
437  return fileOpenDir(dirName.c_str(), dir);
438 }
439 
440 inline int fileOpenRootDir(DirHandle& dir)
441 {
442  return fileOpenDir(nullptr, dir);
443 }
444 
449 inline int fileCloseDir(DirHandle dir)
450 {
451  CHECK_FS(closedir)
452  return fileSystem->closedir(dir);
453 }
454 
460 inline int fileReadDir(DirHandle dir, FileStat& stat)
461 {
462  CHECK_FS(readdir)
463  return fileSystem->readdir(dir, stat);
464 }
465 
470 inline int fileRewindDir(DirHandle dir)
471 {
472  CHECK_FS(rewinddir)
473  return fileSystem->rewinddir(dir);
474 }
475 
480 {
481  CHECK_FS(getinfo)
482  return fileSystem->getinfo(info);
483 }
484 
489 
493 inline int fileSystemFormat()
494 {
496  return fileSystem->format();
497 }
498 
502 inline int fileSystemCheck()
503 {
504  CHECK_FS(check)
505  return fileSystem->check();
506 }
507 
513 inline int fileSetACL(FileHandle file, const IFS::ACL& acl)
514 {
515  CHECK_FS(setacl)
516  return fileSystem->setacl(file, acl);
517 }
518 
524 template <typename T> int fileSetAttr(const T& filename, FileAttributes attr)
525 {
526  CHECK_FS(setattr)
527  return fileSystem->setattr(filename, attr);
528 }
529 
535 inline int fileSetTime(FileHandle file, time_t mtime)
536 {
537  CHECK_FS(settime)
538  return fileSystem->settime(file, mtime);
539 }
540 
545 template <typename T> int createDirectory(const T& path)
546 {
547  CHECK_FS(mkdir);
548  return fileSystem->mkdir(path);
549 }
550 
555 template <typename T> int createDirectories(const T& path)
556 {
557  CHECK_FS(mkdir);
558  return fileSystem->makedirs(path);
559 }
560 
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
Wrapper class for enumerating a directory.
Definition: Directory.h:32
Installable File System base class.
Definition: Components/IFS/src/include/IFS/FileSystem.h:40
Wraps up all file access methods.
Definition: File.h:30
static constexpr OpenFlags ReadOnly
Read-only.
Definition: File.h:38
Installable File System base class.
Definition: IFileSystem.h:99
Type
Definition: IFileSystem.h:101
Represents a flash partition.
Definition: Partition.h:86
The String class.
Definition: WString.h:133
const char * c_str() const
Get a constant (un-modifiable) pointer to String content.
Definition: WString.h:609
#define debug_e(fmt,...)
Definition: debug_progmem.h:77
int fileTruncate(FileHandle file, file_size_t newSize)
Truncate (reduce) the size of an open file.
Definition: Core/FileSystem.h:260
IFS::Directory Directory
Definition: Core/FileSystem.h:33
bool fwfs_mount()
Mount the first available FWFS volume.
int fileRename(const char *oldName, const char *newName)
Rename file.
Definition: Core/FileSystem.h:292
int fileSetACL(FileHandle file, const IFS::ACL &acl)
Set access control information.
Definition: Core/FileSystem.h:513
FileHandle fileOpen(const T &path, FileOpenFlags flags=File::ReadOnly)
Open file by path.
Definition: Core/FileSystem.h:120
bool fileIsEOF(FileHandle file)
Check if at end of file.
Definition: Core/FileSystem.h:185
int fileSystemCheck()
Perform a consistency check/repair on the active file system.
Definition: Core/FileSystem.h:502
String toString(int err)
get text for an error code
int createDirectory(const T &path)
Create a directory.
Definition: Core/FileSystem.h:545
int fileOpenDir(const char *dirName, DirHandle &dir)
Open a named directory for reading.
Definition: Core/FileSystem.h:429
IFS::FileHandle file_t
Definition: Core/FileSystem.h:23
int fileRead(FileHandle file, void *data, size_t size)
Read from file.
Definition: Core/FileSystem.h:163
IFS::FileSystem::Type fileSystemType()
Get the type of file system currently mounted (if any)
int fileCloseDir(DirHandle dir)
close a directory object
Definition: Core/FileSystem.h:449
IFS::FileSystem * fileMountArchive(const String &filename)
Mount a backup archive.
Definition: Core/FileSystem.h:109
IFS::DirHandle DirHandle
Definition: Core/FileSystem.h:25
void fileSetFileSystem(IFS::IFileSystem *fileSystem)
Sets the currently active file system.
bool fileMountFileSystem(IFS::IFileSystem *fs)
Mount a constructed filesystem with debug messages.
bool dirExist(const char *dirName)
Check if a directory exists on file system.
Definition: Core/FileSystem.h:411
int fileSetAttr(const T &filename, FileAttributes attr)
Set file attributes.
Definition: Core/FileSystem.h:524
int fileSetContent(const TFileName &fileName, const char *content, size_t length)
Create or replace file with defined content.
Definition: Core/FileSystem.h:232
IFS::FileSystem * getFileSystem()
Get the currently active file system, if any.
Definition: Core/FileSystem.h:64
constexpr int FS_OK
Definition: Core/FileSystem.h:34
IFS::File File
Definition: Core/FileSystem.h:32
int fileSystemFormat()
Format the active file system.
Definition: Core/FileSystem.h:493
int fileStats(const char *fileName, FileStat &stat)
Get file statistics.
Definition: Core/FileSystem.h:344
int fileRewindDir(DirHandle dir)
Rewind to start of directory entries.
Definition: Core/FileSystem.h:470
IFS::FileHandle FileHandle
Definition: Core/FileSystem.h:24
int fileFlush(FileHandle file)
Flush pending writes.
Definition: Core/FileSystem.h:205
int fileWrite(FileHandle file, const void *data, size_t size)
Write to file.
Definition: Core/FileSystem.h:142
file_offset_t fileSeek(FileHandle file, file_offset_t offset, SeekOrigin origin)
Position file cursor.
Definition: Core/FileSystem.h:175
#define CHECK_FS(_method)
Definition: Core/FileSystem.h:53
int fileDelete(const char *fileName)
Delete file.
Definition: Core/FileSystem.h:370
constexpr ErrorCode FS_OK
Definition: Components/IFS/src/include/IFS/Error.h:130
file_offset_t fileTell(FileHandle file)
Get position in file.
Definition: Core/FileSystem.h:195
bool fileExist(const char *fileName)
Check if a file exists on file system.
Definition: Core/FileSystem.h:395
int fileTouch(FileHandle file)
Update file modification time.
Definition: Core/FileSystem.h:152
void fileFreeFileSystem()
Definition: Core/FileSystem.h:82
int fileSetTime(FileHandle file, time_t mtime)
Set access control information for file.
Definition: Core/FileSystem.h:535
int fileReadDir(DirHandle dir, FileStat &stat)
Read a directory entry.
Definition: Core/FileSystem.h:460
String fileGetErrorString(int err)
get the text for a returned error code
Definition: Core/FileSystem.h:215
String fileGetContent(const TFileName &fileName)
Read content of a file.
Definition: Core/FileSystem.h:310
int fileGetSystemInfo(IFS::FileSystem::Info &info)
Get basic file system information.
Definition: Core/FileSystem.h:479
int createDirectories(const T &path)
Create a directory and all required parent directories.
Definition: Core/FileSystem.h:555
int fileOpenRootDir(DirHandle &dir)
Definition: Core/FileSystem.h:440
file_size_t fileGetSize(const TFileName &fileName)
Get size of file.
Definition: Core/FileSystem.h:249
int fileClose(FileHandle file)
Clode file.
Definition: Core/FileSystem.h:130
FileSystem * mountArchive(FileSystem &fs, const String &filename)
Mount an FWFS archive.
BitSet< uint8_t, FileAttribute, size_t(FileAttribute::MAX)> FileAttributes
File attributes are stored as a bitmask.
Definition: FileAttributes.h:56
FileAttribute
Definition: FileAttributes.h:46
struct ImplFileDir * DirHandle
Definition: IFileSystem.h:72
OpenFlag
Definition: OpenFlags.h:42
BitSet< uint8_t, OpenFlag, size_t(OpenFlag::MAX)> OpenFlags
Definition: OpenFlags.h:50
int16_t FileHandle
File handle.
Definition: Stat.h:40
Timer2Clock::Ticks< uint32_t > read()
Get elapsed watchdog time since last reset.
Definition: Core/FileSystem.h:37
IFS::FileSystem * activeFileSystem
Global file system instance.
Definition: Access.h:34
Basic information about filing system.
Definition: IFileSystem.h:121
version of Stat with integrated name buffer
Definition: Stat.h:104
File Status structure.
Definition: Stat.h:52
FileAttributes attr
Definition: Stat.h:59