mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-14 13:41:23 +03:00
Add LittleFS as an optional filesystem, API compatible w/SPIFFS (but not on-flash-format compatible) (#5511)
* Add LittleFS as internal flash filesystem Adds a LittleFS object which uses the ARMmbed littlefs embedded filesystem, https://github.com/ARMmbed/littlefs, to enable a new filesystem for onboard flash utilizing the exact same API as the existing SPIFFS filesystem. LittleFS is built for low memory systems that are subject to random power losses, is actively supported by the ARMmbed community, supports directories, and seems to be much faster in the large-ish read-mostly applications I use. LittleFS, however, has a larger minimum file allocation unit and does not do static wear levelling. This means that for systems that need many little files (<4K), have small SPIFFS areas (64K), or which have a large static set of files covering the majority of flash coupled with a frequently updated set of other files, it may not perform as well. Simply replace SPIFFS.begin() with LittleFS.begin() in your sketch, use LittleFS.open in place of SPIFFS.open to open files, and everything else just works thanks to the magic of @igrr's File base class. **LITTLEFS FLASH LAYOUT IS INCOMPATIBLE WITH SPIFFS** Since it is a completely different filesystem, you will need to reformat your flash (and lose any data therein) to use it. Tools to build the flash filesystem and upload are at https://github.com/earlephilhower/arduino-esp8266littlefs-plugin and https://github.com/earlephilhower/mklittlefs/ . The mklittlefs tool is installed as part of the Arduino platform installation, automatically. The included example shows a contrived read-mostly example and demonstrates how the same calls work on either SPIFFS.* or LittleFS.* Host tests are also included as part of CI. Directories are fully supported in LittleFS. This means that LittleFS will have a slight difference vs. SPIFFS when you use LittleFS.openDir()/Dir.next(). On SPIFFS dir.next() will return all filesystem entries, including ones in "subdirs" (because in SPIFFS there are no subdirs and "/" is the same as any other character in a filename). On LittleFS, dir.next() will only return entries in the directory specified, not subdirs. So to list files in "/subdir/..." you need to actually openDir("/subdir") and use Dir.next() to parse through just those elements. The returned filenames also only have the filename returned, not full paths. So on a FS with "/a/1", "/a/2" when you do openDir("/a"); dir.next().getName(); you get "1" and "2" and not "/a/1" and "/a/2" like in SPIFFS. This is consistent with POSIX ideas about reading directories and more natural for a FS. Most code will not be affected by this, but if you depend on openDir/Dir.next() you need to be aware of it. Corresponding ::mkdir, ::rmdir, ::isDirectory, ::isFile, ::openNextFile, and ::rewind methods added to Filesystem objects. Documentation has been updated with this and other LittleFS information. Subdirectories are made silently when they do not exist when you try and create a file in a subdir. They are silently removed when the last file in them is deleted. This is consistent with what SPIFFS does but is obviously not normal POSIX behavior. Since there has never been a "FS.mkdir()" method this is the only way to be compatible with legacy SPIFFS code. SPIFFS code has been refactored to pull out common flash_hal_* ops and placed in its own namespace, like LittleFS. * Fix up merge blank line issue * Merge in the FSConfig changs from SDFS PR Enable setConfig for LittleFS as well plys merge the SPIFFS changes done in the SDFS PR. * Fix merge errors * Update to use v2-alpha branch The V2-alpha branch supports small file optimizations which can help increase the utilization of flash when small files are prevalent. It also adds support for metadata, which means we can start adding things like file creation times, if desired (not yet). * V2 of littlefs is now in upstream/master * Update test to support non-creation-ordered files In a directory, the order in which "readNextFile()" will return a name is undefined. SPIFFS may return it in order, but LittleFS does not as of V2. Update the test to look for files by name when doing readNextFile() testing. * Fix LittleFS.truncate implementation * Fix SDFS tests SDFS, SPIFFS, and LittleFS now all share the same common set of tests, greatly increasing the SDFS test coverage. * Update to point to mklittlefs v2 Upgrade mklittlefs to V2 format support * Remove extra FS::write(const char *s) method This was removed in #5861 and erroneously re-introduced here. * Minimize spurious differences from master * Dramatically reduce memory usage Reduce the program and read chunk sizes which impacts performance minimally but reduces per-file RAM usage of 16KB to <1KB. * Add @d-a-v's host emulation for LittleFS * Fix SW Serial library version * Fix free space reporting Thanks to @TD-er for discovering the issue * Update littlefs to latest upstream * Remove sdfat version included by accident * Update SDFAT to include MOCK changes required * Update to include SD.h test of file append
This commit is contained in:
committed by
david gauchard
parent
b55199227b
commit
a389a995fb
198
libraries/LittleFS/src/LittleFS.cpp
Normal file
198
libraries/LittleFS/src/LittleFS.cpp
Normal file
@ -0,0 +1,198 @@
|
||||
/*
|
||||
LittleFS.cpp - Wrapper for LittleFS for ESP8266
|
||||
Copyright (c_ 2019 Earle F. Philhower, III. All rights reserved.
|
||||
|
||||
Based extensively off of the ESP8266 SPIFFS code, which is
|
||||
Copyright (c) 2015 Ivan Grokhotkov. All rights reserved.
|
||||
This file is part of the esp8266 core for Arduino environment.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <stdlib.h>
|
||||
#include <algorithm>
|
||||
#include "LittleFS.h"
|
||||
#include "debug.h"
|
||||
#include "flash_hal.h"
|
||||
|
||||
extern "C" {
|
||||
#include "c_types.h"
|
||||
#include "spi_flash.h"
|
||||
}
|
||||
|
||||
namespace littlefs_impl {
|
||||
|
||||
FileImplPtr LittleFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode) {
|
||||
if (!_mounted) {
|
||||
DEBUGV("LittleFSImpl::open() called on unmounted FS\n");
|
||||
return FileImplPtr();
|
||||
}
|
||||
if (!path || !path[0]) {
|
||||
DEBUGV("LittleFSImpl::open() called with invalid filename\n");
|
||||
return FileImplPtr();
|
||||
}
|
||||
if (!LittleFSImpl::pathValid(path)) {
|
||||
DEBUGV("LittleFSImpl::open() called with too long filename\n");
|
||||
return FileImplPtr();
|
||||
}
|
||||
|
||||
int flags = _getFlags(openMode, accessMode);
|
||||
auto fd = std::make_shared<lfs_file_t>();
|
||||
|
||||
if ((openMode && OM_CREATE) && strchr(path, '/')) {
|
||||
// For file creation, silently make subdirs as needed. If any fail,
|
||||
// it will be caught by the real file open later on
|
||||
char *pathStr = strdup(path);
|
||||
if (pathStr) {
|
||||
// Make dirs up to the final fnamepart
|
||||
char *ptr = strchr(pathStr, '/');
|
||||
while (ptr) {
|
||||
*ptr = 0;
|
||||
lfs_mkdir(&_lfs, pathStr);
|
||||
*ptr = '/';
|
||||
ptr = strchr(ptr+1, '/');
|
||||
}
|
||||
}
|
||||
free(pathStr);
|
||||
}
|
||||
int rc = lfs_file_open(&_lfs, fd.get(), path, flags);
|
||||
if (rc == LFS_ERR_ISDIR) {
|
||||
// To support the SD.openNextFile, a null FD indicates to the LittleFSFile this is just
|
||||
// a directory whose name we are carrying around but which cannot be read or written
|
||||
return std::make_shared<LittleFSFileImpl>(this, path, nullptr);
|
||||
} else if (rc == 0) {
|
||||
return std::make_shared<LittleFSFileImpl>(this, path, fd);
|
||||
} else {
|
||||
DEBUGV("LittleFSDirImpl::openFile: rc=%d fd=%p path=`%s` openMode=%d accessMode=%d err=%d\n",
|
||||
rc, fd.get(), path, openMode, accessMode, rc);
|
||||
return FileImplPtr();
|
||||
}
|
||||
}
|
||||
|
||||
DirImplPtr LittleFSImpl::openDir(const char *path) {
|
||||
if (!_mounted || !path) {
|
||||
return DirImplPtr();
|
||||
}
|
||||
char *pathStr = strdup(path); // Allow edits on our scratch copy
|
||||
// Get rid of any trailing slashes
|
||||
while (strlen(pathStr) && (pathStr[strlen(pathStr)-1]=='/')) {
|
||||
pathStr[strlen(pathStr)-1] = 0;
|
||||
}
|
||||
// At this point we have a name of "blah/blah/blah" or "blah" or ""
|
||||
// If that references a directory, just open it and we're done.
|
||||
lfs_info info;
|
||||
auto dir = std::make_shared<lfs_dir_t>();
|
||||
int rc;
|
||||
const char *filter = "";
|
||||
if (!pathStr[0]) {
|
||||
// openDir("") === openDir("/")
|
||||
rc = lfs_dir_open(&_lfs, dir.get(), "/");
|
||||
filter = "";
|
||||
} else if (lfs_stat(&_lfs, pathStr, &info) >= 0) {
|
||||
if (info.type == LFS_TYPE_DIR) {
|
||||
// Easy peasy, path specifies an existing dir!
|
||||
rc = lfs_dir_open(&_lfs, dir.get(), pathStr);
|
||||
filter = "";
|
||||
} else {
|
||||
// This is a file, so open the containing dir
|
||||
char *ptr = strrchr(pathStr, '/');
|
||||
if (!ptr) {
|
||||
// No slashes, open the root dir
|
||||
rc = lfs_dir_open(&_lfs, dir.get(), "/");
|
||||
filter = pathStr;
|
||||
} else {
|
||||
// We've got slashes, open the dir one up
|
||||
*ptr = 0; // Remove slash, truncate string
|
||||
rc = lfs_dir_open(&_lfs, dir.get(), pathStr);
|
||||
filter = ptr + 1;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Name doesn't exist, so use the parent dir of whatever was sent in
|
||||
// This is a file, so open the containing dir
|
||||
char *ptr = strrchr(pathStr, '/');
|
||||
if (!ptr) {
|
||||
// No slashes, open the root dir
|
||||
rc = lfs_dir_open(&_lfs, dir.get(), "/");
|
||||
filter = pathStr;
|
||||
} else {
|
||||
// We've got slashes, open the dir one up
|
||||
*ptr = 0; // Remove slash, truncate string
|
||||
rc = lfs_dir_open(&_lfs, dir.get(), pathStr);
|
||||
filter = ptr + 1;
|
||||
}
|
||||
}
|
||||
if (rc < 0) {
|
||||
DEBUGV("LittleFSImpl::openDir: path=`%s` err=%d\n", path, rc);
|
||||
free(pathStr);
|
||||
return DirImplPtr();
|
||||
}
|
||||
// Skip the . and .. entries
|
||||
lfs_info dirent;
|
||||
lfs_dir_read(&_lfs, dir.get(), &dirent);
|
||||
lfs_dir_read(&_lfs, dir.get(), &dirent);
|
||||
|
||||
auto ret = std::make_shared<LittleFSDirImpl>(filter, this, dir, pathStr);
|
||||
free(pathStr);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int LittleFSImpl::lfs_flash_read(const struct lfs_config *c,
|
||||
lfs_block_t block, lfs_off_t off, void *dst, lfs_size_t size) {
|
||||
LittleFSImpl *me = reinterpret_cast<LittleFSImpl*>(c->context);
|
||||
uint32_t addr = me->_start + (block * me->_blockSize) + off;
|
||||
return flash_hal_read(addr, size, static_cast<uint8_t*>(dst)) == FLASH_HAL_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
int LittleFSImpl::lfs_flash_prog(const struct lfs_config *c,
|
||||
lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size) {
|
||||
LittleFSImpl *me = reinterpret_cast<LittleFSImpl*>(c->context);
|
||||
uint32_t addr = me->_start + (block * me->_blockSize) + off;
|
||||
const uint8_t *src = reinterpret_cast<const uint8_t *>(buffer);
|
||||
return flash_hal_write(addr, size, static_cast<const uint8_t*>(src)) == FLASH_HAL_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
int LittleFSImpl::lfs_flash_erase(const struct lfs_config *c, lfs_block_t block) {
|
||||
LittleFSImpl *me = reinterpret_cast<LittleFSImpl*>(c->context);
|
||||
uint32_t addr = me->_start + (block * me->_blockSize);
|
||||
uint32_t size = me->_blockSize;
|
||||
return flash_hal_erase(addr, size) == FLASH_HAL_OK ? 0 : -1;
|
||||
}
|
||||
|
||||
int LittleFSImpl::lfs_flash_sync(const struct lfs_config *c) {
|
||||
/* NOOP */
|
||||
(void) c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
}; // namespace
|
||||
|
||||
// these symbols should be defined in the linker script for each flash layout
|
||||
#ifndef CORE_MOCK
|
||||
#ifdef ARDUINO
|
||||
#ifndef FS_MAX_OPEN_FILES
|
||||
#define FS_MAX_OPEN_FILES 5
|
||||
#endif
|
||||
|
||||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_LITTLEFS)
|
||||
FS LittleFS = FS(FSImplPtr(new littlefs_impl::LittleFSImpl(FS_PHYS_ADDR, FS_PHYS_SIZE, FS_PHYS_PAGE, FS_PHYS_BLOCK, FS_MAX_OPEN_FILES)));
|
||||
#endif
|
||||
|
||||
#endif // !CORE_MOCK
|
||||
|
||||
|
||||
#endif
|
557
libraries/LittleFS/src/LittleFS.h
Normal file
557
libraries/LittleFS/src/LittleFS.h
Normal file
@ -0,0 +1,557 @@
|
||||
/*
|
||||
LittleFS.h - Filesystem wrapper for LittleFS on the ESP8266
|
||||
Copyright (c) 2019 Earle F. Philhower, III. All rights reserved.
|
||||
|
||||
Based heavily off of the SPIFFS equivalent code in the ESP8266 core
|
||||
"Copyright (c) 2015 Ivan Grokhotkov. All rights reserved."
|
||||
|
||||
This code was influenced by NodeMCU and Sming libraries, and first version of
|
||||
Arduino wrapper written by Hristo Gochkov.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __LITTLEFS_H
|
||||
#define __LITTLEFS_H
|
||||
|
||||
#include <limits>
|
||||
#include <FS.h>
|
||||
#include <FSImpl.h>
|
||||
#include <debug.h>
|
||||
#include <flash_utils.h>
|
||||
#include <flash_hal.h>
|
||||
|
||||
#define LFS_NAME_MAX 32
|
||||
#include "../lib/littlefs/lfs.h"
|
||||
|
||||
using namespace fs;
|
||||
|
||||
namespace littlefs_impl {
|
||||
|
||||
class LittleFSFileImpl;
|
||||
class LittleFSDirImpl;
|
||||
|
||||
class LittleFSConfig : public FSConfig
|
||||
{
|
||||
public:
|
||||
LittleFSConfig(bool autoFormat = true) {
|
||||
_type = LittleFSConfig::fsid::FSId;
|
||||
_autoFormat = autoFormat;
|
||||
}
|
||||
enum fsid { FSId = 0x4c495454 };
|
||||
};
|
||||
|
||||
class LittleFSImpl : public FSImpl
|
||||
{
|
||||
public:
|
||||
LittleFSImpl(uint32_t start, uint32_t size, uint32_t pageSize, uint32_t blockSize, uint32_t maxOpenFds)
|
||||
: _start(start) , _size(size) , _pageSize(pageSize) , _blockSize(blockSize) , _maxOpenFds(maxOpenFds),
|
||||
_mounted(false) {
|
||||
memset(&_lfs, 0, sizeof(_lfs));
|
||||
memset(&_lfs_cfg, 0, sizeof(_lfs_cfg));
|
||||
_lfs_cfg.context = (void*) this;
|
||||
_lfs_cfg.read = lfs_flash_read;
|
||||
_lfs_cfg.prog = lfs_flash_prog;
|
||||
_lfs_cfg.erase = lfs_flash_erase;
|
||||
_lfs_cfg.sync = lfs_flash_sync;
|
||||
_lfs_cfg.read_size = 64;
|
||||
_lfs_cfg.prog_size = 64;
|
||||
_lfs_cfg.block_size = _blockSize;
|
||||
_lfs_cfg.block_count = _size / _blockSize;
|
||||
_lfs_cfg.block_cycles = 16; // TODO - need better explanation
|
||||
_lfs_cfg.cache_size = 64;
|
||||
_lfs_cfg.lookahead_size = 64;
|
||||
_lfs_cfg.read_buffer = nullptr;
|
||||
_lfs_cfg.prog_buffer = nullptr;
|
||||
_lfs_cfg.lookahead_buffer = nullptr;
|
||||
_lfs_cfg.name_max = 0;
|
||||
_lfs_cfg.file_max = 0;
|
||||
_lfs_cfg.attr_max = 0;
|
||||
}
|
||||
|
||||
~LittleFSImpl() {
|
||||
if (_mounted) {
|
||||
lfs_unmount(&_lfs);
|
||||
}
|
||||
}
|
||||
|
||||
FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) override;
|
||||
DirImplPtr openDir(const char *path) override;
|
||||
|
||||
bool exists(const char* path) override {
|
||||
if ( !_mounted || !path || !path[0] ) {
|
||||
return false;
|
||||
}
|
||||
lfs_info info;
|
||||
int rc = lfs_stat(&_lfs, path, &info);
|
||||
return rc == 0;
|
||||
}
|
||||
|
||||
bool rename(const char* pathFrom, const char* pathTo) override {
|
||||
if (!_mounted || !pathFrom || !pathFrom[0] || !pathTo || !pathTo[0]) {
|
||||
return false;
|
||||
}
|
||||
int rc = lfs_rename(&_lfs, pathFrom, pathTo);
|
||||
if (rc != 0) {
|
||||
DEBUGV("lfs_rename: rc=%d, from=`%s`, to=`%s`\n", rc, pathFrom, pathTo);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool info(FSInfo& info) override {
|
||||
if (!_mounted) {
|
||||
return false;
|
||||
}
|
||||
info.maxOpenFiles = _maxOpenFds;
|
||||
info.blockSize = _blockSize;
|
||||
info.pageSize = _pageSize;
|
||||
info.maxOpenFiles = _maxOpenFds;
|
||||
info.maxPathLength = LFS_NAME_MAX;
|
||||
info.totalBytes = _size;
|
||||
info.usedBytes = _getUsedBlocks() * _blockSize;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool remove(const char* path) override {
|
||||
if (!_mounted || !path || !path[0]) {
|
||||
return false;
|
||||
}
|
||||
int rc = lfs_remove(&_lfs, path);
|
||||
if (rc != 0) {
|
||||
DEBUGV("lfs_remove: rc=%d path=`%s`\n", rc, path);
|
||||
return false;
|
||||
}
|
||||
// Now try and remove any empty subdirs this makes, silently
|
||||
char *pathStr = strdup(path);
|
||||
if (pathStr) {
|
||||
char *ptr = strrchr(pathStr, '/');
|
||||
while (ptr) {
|
||||
*ptr = 0;
|
||||
lfs_remove(&_lfs, pathStr); // Don't care if fails if there are files left
|
||||
ptr = strrchr(pathStr, '/');
|
||||
}
|
||||
free(pathStr);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool mkdir(const char* path) override {
|
||||
if (!_mounted || !path || !path[0]) {
|
||||
return false;
|
||||
}
|
||||
int rc = lfs_mkdir(&_lfs, path);
|
||||
return (rc==0);
|
||||
}
|
||||
|
||||
bool rmdir(const char* path) override {
|
||||
return remove(path); // Same call on LittleFS
|
||||
}
|
||||
|
||||
bool setConfig(const FSConfig &cfg) override {
|
||||
if ((cfg._type != LittleFSConfig::fsid::FSId) || _mounted) {
|
||||
return false;
|
||||
}
|
||||
_cfg = *static_cast<const LittleFSConfig *>(&cfg);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool begin() override {
|
||||
if (_size <= 0) {
|
||||
DEBUGV("LittleFS size is <= zero");
|
||||
return false;
|
||||
}
|
||||
if (_tryMount()) {
|
||||
return true;
|
||||
}
|
||||
if (!_cfg._autoFormat || !format()) {
|
||||
return false;
|
||||
}
|
||||
return _tryMount();
|
||||
}
|
||||
|
||||
void end() override {
|
||||
if (!_mounted) {
|
||||
return;
|
||||
}
|
||||
lfs_unmount(&_lfs);
|
||||
_mounted = false;
|
||||
}
|
||||
|
||||
bool format() override {
|
||||
if (_size == 0) {
|
||||
DEBUGV("lfs size is zero\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool wasMounted = _mounted;
|
||||
if (_mounted) {
|
||||
lfs_unmount(&_lfs);
|
||||
_mounted = false;
|
||||
}
|
||||
|
||||
memset(&_lfs, 0, sizeof(_lfs));
|
||||
int rc = lfs_format(&_lfs, &_lfs_cfg);
|
||||
if (rc != 0) {
|
||||
DEBUGV("lfs_format: rc=%d\n", rc);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (wasMounted) {
|
||||
return _tryMount();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
friend class LittleFSFileImpl;
|
||||
friend class LittleFSDirImpl;
|
||||
|
||||
lfs_t* getFS() {
|
||||
return &_lfs;
|
||||
}
|
||||
|
||||
bool _tryMount() {
|
||||
if (_mounted) {
|
||||
lfs_unmount(&_lfs);
|
||||
_mounted = false;
|
||||
}
|
||||
memset(&_lfs, 0, sizeof(_lfs));
|
||||
int rc = lfs_mount(&_lfs, &_lfs_cfg);
|
||||
if (rc==0) {
|
||||
_mounted = true;
|
||||
}
|
||||
return _mounted;
|
||||
}
|
||||
|
||||
int _getUsedBlocks() {
|
||||
if (!_mounted) {
|
||||
return 0;
|
||||
}
|
||||
return lfs_fs_size(&_lfs);
|
||||
}
|
||||
|
||||
static int _getFlags(OpenMode openMode, AccessMode accessMode) {
|
||||
int mode = 0;
|
||||
if (openMode & OM_CREATE) {
|
||||
mode |= LFS_O_CREAT;
|
||||
}
|
||||
if (openMode & OM_APPEND) {
|
||||
mode |= LFS_O_APPEND;
|
||||
}
|
||||
if (openMode & OM_TRUNCATE) {
|
||||
mode |= LFS_O_TRUNC;
|
||||
}
|
||||
if (accessMode & AM_READ) {
|
||||
mode |= LFS_O_RDONLY;
|
||||
}
|
||||
if (accessMode & AM_WRITE) {
|
||||
mode |= LFS_O_WRONLY;
|
||||
}
|
||||
return mode;
|
||||
}
|
||||
|
||||
// Check that no components of path beyond max len
|
||||
static bool pathValid(const char *path) {
|
||||
while (*path) {
|
||||
const char *slash = strchr(path, '/');
|
||||
if (!slash) {
|
||||
if (strlen(path) >= LFS_NAME_MAX) {
|
||||
// Terminal filename is too long
|
||||
return false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if ((slash - path) >= LFS_NAME_MAX) {
|
||||
// This subdir name too long
|
||||
return false;
|
||||
}
|
||||
path = slash + 1;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// The actual flash accessing routines
|
||||
static int lfs_flash_read(const struct lfs_config *c, lfs_block_t block,
|
||||
lfs_off_t off, void *buffer, lfs_size_t size);
|
||||
static int lfs_flash_prog(const struct lfs_config *c, lfs_block_t block,
|
||||
lfs_off_t off, const void *buffer, lfs_size_t size);
|
||||
static int lfs_flash_erase(const struct lfs_config *c, lfs_block_t block);
|
||||
static int lfs_flash_sync(const struct lfs_config *c);
|
||||
|
||||
lfs_t _lfs;
|
||||
lfs_config _lfs_cfg;
|
||||
|
||||
LittleFSConfig _cfg;
|
||||
|
||||
uint32_t _start;
|
||||
uint32_t _size;
|
||||
uint32_t _pageSize;
|
||||
uint32_t _blockSize;
|
||||
uint32_t _maxOpenFds;
|
||||
|
||||
bool _mounted;
|
||||
};
|
||||
|
||||
|
||||
class LittleFSFileImpl : public FileImpl
|
||||
{
|
||||
public:
|
||||
LittleFSFileImpl(LittleFSImpl* fs, const char *name, std::shared_ptr<lfs_file_t> fd) : _fs(fs), _fd(fd), _opened(true) {
|
||||
_name = std::shared_ptr<char>(new char[strlen(name) + 1], std::default_delete<char[]>());
|
||||
strcpy(_name.get(), name);
|
||||
}
|
||||
|
||||
~LittleFSFileImpl() override {
|
||||
if (_opened) {
|
||||
close();
|
||||
}
|
||||
}
|
||||
|
||||
size_t write(const uint8_t *buf, size_t size) override {
|
||||
if (!_opened || !_fd || !buf) {
|
||||
return 0;
|
||||
}
|
||||
int result = lfs_file_write(_fs->getFS(), _getFD(), (void*) buf, size);
|
||||
if (result < 0) {
|
||||
DEBUGV("lfs_write rc=%d\n", result);
|
||||
return 0;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t read(uint8_t* buf, size_t size) override {
|
||||
if (!_opened || !_fd | !buf) {
|
||||
return 0;
|
||||
}
|
||||
int result = lfs_file_read(_fs->getFS(), _getFD(), (void*) buf, size);
|
||||
if (result < 0) {
|
||||
DEBUGV("lfs_read rc=%d\n", result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void flush() override {
|
||||
if (!_opened || !_fd) {
|
||||
return;
|
||||
}
|
||||
int rc = lfs_file_sync(_fs->getFS(), _getFD());
|
||||
if (rc < 0) {
|
||||
DEBUGV("lfs_file_sync rc=%d\n", rc);
|
||||
}
|
||||
}
|
||||
|
||||
bool seek(uint32_t pos, SeekMode mode) override {
|
||||
if (!_opened || !_fd) {
|
||||
return false;
|
||||
}
|
||||
int32_t offset = static_cast<int32_t>(pos);
|
||||
if (mode == SeekEnd) {
|
||||
offset = -offset; // TODO - this seems like its plain wrong vs. POSIX
|
||||
}
|
||||
int rc = lfs_file_seek(_fs->getFS(), _getFD(), offset, (int)mode); // NB. SeekMode === LFS_SEEK_TYPES
|
||||
if (rc < 0) {
|
||||
DEBUGV("lfs_file_seek rc=%d\n", rc);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
size_t position() const override {
|
||||
if (!_opened || !_fd) {
|
||||
return 0;
|
||||
}
|
||||
int result = lfs_file_tell(_fs->getFS(), _getFD());
|
||||
if (result < 0) {
|
||||
DEBUGV("lfs_file_tell rc=%d\n", result);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t size() const override {
|
||||
return (_opened && _fd)? lfs_file_size(_fs->getFS(), _getFD()) : 0;
|
||||
}
|
||||
|
||||
bool truncate(uint32_t size) override {
|
||||
if (!_opened || !_fd) {
|
||||
return false;
|
||||
}
|
||||
int rc = lfs_file_truncate(_fs->getFS(), _getFD(), size);
|
||||
if (rc < 0) {
|
||||
DEBUGV("lfs_file_truncate rc=%d\n", rc);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void close() override {
|
||||
if (_opened && _fd) {
|
||||
lfs_file_close(_fs->getFS(), _getFD());
|
||||
_opened = false;
|
||||
DEBUGV("lfs_file_close: fd=%p\n", _getFD());
|
||||
}
|
||||
}
|
||||
|
||||
const char* name() const override {
|
||||
if (!_opened) {
|
||||
return nullptr;
|
||||
} else {
|
||||
const char *p = _name.get();
|
||||
const char *slash = strrchr(p, '/');
|
||||
return (slash && slash[1]) ? slash + 1 : p;
|
||||
}
|
||||
}
|
||||
|
||||
const char* fullName() const override {
|
||||
return _opened ? _name.get() : nullptr;
|
||||
}
|
||||
|
||||
bool isFile() const override {
|
||||
if (!_opened || !_fd) {
|
||||
return false;
|
||||
}
|
||||
lfs_info info;
|
||||
int rc = lfs_stat(_fs->getFS(), fullName(), &info);
|
||||
return (rc == 0) && (info.type == LFS_TYPE_REG);
|
||||
}
|
||||
|
||||
bool isDirectory() const override {
|
||||
if (!_opened) {
|
||||
return false;
|
||||
} else if (!_fd) {
|
||||
return true;
|
||||
}
|
||||
lfs_info info;
|
||||
int rc = lfs_stat(_fs->getFS(), fullName(), &info);
|
||||
return (rc == 0) && (info.type == LFS_TYPE_DIR);
|
||||
}
|
||||
|
||||
protected:
|
||||
lfs_file_t *_getFD() const {
|
||||
return _fd.get();
|
||||
}
|
||||
|
||||
LittleFSImpl *_fs;
|
||||
std::shared_ptr<lfs_file_t> _fd;
|
||||
std::shared_ptr<char> _name;
|
||||
bool _opened;
|
||||
};
|
||||
|
||||
class LittleFSDirImpl : public DirImpl
|
||||
{
|
||||
public:
|
||||
LittleFSDirImpl(const String& pattern, LittleFSImpl* fs, std::shared_ptr<lfs_dir_t> dir, const char *dirPath = nullptr)
|
||||
: _pattern(pattern) , _fs(fs) , _dir(dir) , _dirPath(nullptr), _valid(false), _opened(true)
|
||||
{
|
||||
memset(&_dirent, 0, sizeof(_dirent));
|
||||
if (dirPath) {
|
||||
_dirPath = std::shared_ptr<char>(new char[strlen(dirPath) + 1], std::default_delete<char[]>());
|
||||
strcpy(_dirPath.get(), dirPath);
|
||||
}
|
||||
}
|
||||
|
||||
~LittleFSDirImpl() override {
|
||||
if (_opened) {
|
||||
lfs_dir_close(_fs->getFS(), _getDir());
|
||||
}
|
||||
}
|
||||
|
||||
FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) override {
|
||||
if (!_valid) {
|
||||
return FileImplPtr();
|
||||
}
|
||||
int nameLen = 3; // Slashes, terminator
|
||||
nameLen += _dirPath.get() ? strlen(_dirPath.get()) : 0;
|
||||
nameLen += strlen(_dirent.name);
|
||||
char *tmpName = (char*)malloc(nameLen);
|
||||
if (!tmpName) {
|
||||
return FileImplPtr();
|
||||
}
|
||||
snprintf(tmpName, nameLen, "%s%s%s", _dirPath.get() ? _dirPath.get() : "", _dirPath.get()&&_dirPath.get()[0]?"/":"", _dirent.name);
|
||||
auto ret = _fs->open((const char *)tmpName, openMode, accessMode);
|
||||
free(tmpName);
|
||||
return ret;
|
||||
}
|
||||
|
||||
const char* fileName() override {
|
||||
if (!_valid) {
|
||||
return nullptr;
|
||||
}
|
||||
return (const char*) _dirent.name;
|
||||
}
|
||||
|
||||
size_t fileSize() override {
|
||||
if (!_valid) {
|
||||
return 0;
|
||||
}
|
||||
return _dirent.size;
|
||||
}
|
||||
|
||||
bool isFile() const override {
|
||||
return _valid && (_dirent.type == LFS_TYPE_REG);
|
||||
}
|
||||
|
||||
bool isDirectory() const override {
|
||||
return _valid && (_dirent.type == LFS_TYPE_DIR);
|
||||
}
|
||||
|
||||
bool rewind() override {
|
||||
_valid = false;
|
||||
int rc = lfs_dir_rewind(_fs->getFS(), _getDir());
|
||||
return (rc == 0);
|
||||
}
|
||||
|
||||
bool next() override {
|
||||
const int n = _pattern.length();
|
||||
bool match;
|
||||
do {
|
||||
_dirent.name[0] = 0;
|
||||
int rc = lfs_dir_read(_fs->getFS(), _getDir(), &_dirent);
|
||||
_valid = (rc == 1);
|
||||
match = (!n || !strncmp((const char*) _dirent.name, _pattern.c_str(), n));
|
||||
} while (_valid && !match);
|
||||
return _valid;
|
||||
}
|
||||
|
||||
protected:
|
||||
lfs_dir_t *_getDir() const {
|
||||
return _dir.get();
|
||||
}
|
||||
|
||||
String _pattern;
|
||||
LittleFSImpl *_fs;
|
||||
std::shared_ptr<lfs_dir_t> _dir;
|
||||
std::shared_ptr<char> _dirPath;
|
||||
lfs_info _dirent;
|
||||
bool _valid;
|
||||
bool _opened;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_LITTLEFS)
|
||||
extern FS LittleFS;
|
||||
using littlefs_impl::LittleFSConfig;
|
||||
#endif // ARDUINO
|
||||
|
||||
|
||||
#endif // !defined(__LITTLEFS_H)
|
10
libraries/LittleFS/src/lfs.c
Normal file
10
libraries/LittleFS/src/lfs.c
Normal file
@ -0,0 +1,10 @@
|
||||
// Can't place library in ths src/ directory, Arduino will attempt to build the tests/etc.
|
||||
// Just have a stub here that redirects to the actual source file
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
|
||||
#define LFS_NAME_MAX 32
|
||||
#define LFS_NO_DEBUG
|
||||
#define LFS_NO_WARN
|
||||
#define LFS_NO_ERROR
|
||||
|
||||
#include "../lib/littlefs/lfs.c"
|
6
libraries/LittleFS/src/lfs_util.c
Normal file
6
libraries/LittleFS/src/lfs_util.c
Normal file
@ -0,0 +1,6 @@
|
||||
#define LFS_NAME_MAX 32
|
||||
#define LFS_NO_DEBUG
|
||||
#define LFS_NO_WARN
|
||||
#define LFS_NO_ERROR
|
||||
|
||||
#include "../lib/littlefs/lfs_util.c"
|
Reference in New Issue
Block a user