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
|
Reference in New Issue
Block a user