1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00
esp8266/cores/esp8266/spiffs_api.cpp
Earle F. Philhower, III 83166f948b
Deprecate SPIFFS, move examples to LittleFS (#7263)
* Deprecate SPIFFS, move examples to LittleFS

SPIFFS has been a great filesystem, but it has significant problems in
many cases (and it's also pretty slow).  Development seems to have
slowed/stopped on the upstream version, and we're not able to provide
support or fix the known issues with it as-is.

Deprecate SPIFFS variable.

Update all examples to use LittleFS instead of SPIFFS.

Also, minor cleanup on very old examples which has obsolete delays
waiting for the Serial port to come up, or which were stuck at 9600 baud
because of their ancient AVR heritage.

Fixes #7095

* Remove leftover debug code

* Clean up comments in some examples

* Update documentation on SPIFFS deprecation

* Fix host tests to avoid deprecation warnings

* Fix cut-n-paste error

* Restore SpeedTest.ino, adjust to allow custom FSes

Co-authored-by: Develo <deveyes@gmail.com>
2020-05-04 14:22:50 -04:00

158 lines
4.8 KiB
C++

/*
spiffs_api.cpp - file system wrapper for SPIFFS
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 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 "spiffs_api.h"
using namespace fs;
// Deprecated functions, to be deleted in next release
int32_t spiffs_hal_write(uint32_t addr, uint32_t size, uint8_t *src) {
return flash_hal_write(addr, size, src);
}
int32_t spiffs_hal_erase(uint32_t addr, uint32_t size) {
return flash_hal_erase(addr, size);
}
int32_t spiffs_hal_read(uint32_t addr, uint32_t size, uint8_t *dst) {
return flash_hal_read(addr, size, dst);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
namespace spiffs_impl {
FileImplPtr SPIFFSImpl::open(const char* path, OpenMode openMode, AccessMode accessMode)
{
if (!isSpiffsFilenameValid(path)) {
DEBUGV("SPIFFSImpl::open: invalid path=`%s` \r\n", path);
return FileImplPtr();
}
int mode = getSpiffsMode(openMode, accessMode);
int fd = SPIFFS_open(&_fs, path, mode, 0);
if (fd < 0 && _fs.err_code == SPIFFS_ERR_DELETED && (openMode & OM_CREATE)) {
DEBUGV("SPIFFSImpl::open: fd=%d path=`%s` openMode=%d accessMode=%d err=%d, trying to remove\r\n",
fd, path, openMode, accessMode, _fs.err_code);
auto rc = SPIFFS_remove(&_fs, path);
if (rc != SPIFFS_OK) {
DEBUGV("SPIFFSImpl::open: SPIFFS_ERR_DELETED, but failed to remove path=`%s` openMode=%d accessMode=%d err=%d\r\n",
path, openMode, accessMode, _fs.err_code);
return FileImplPtr();
}
fd = SPIFFS_open(&_fs, path, mode, 0);
}
if (fd < 0) {
DEBUGV("SPIFFSImpl::open: fd=%d path=`%s` openMode=%d accessMode=%d err=%d\r\n",
fd, path, openMode, accessMode, _fs.err_code);
return FileImplPtr();
}
return std::make_shared<SPIFFSFileImpl>(this, fd);
}
bool SPIFFSImpl::exists(const char* path)
{
if (!isSpiffsFilenameValid(path)) {
DEBUGV("SPIFFSImpl::exists: invalid path=`%s` \r\n", path);
return false;
}
spiffs_stat stat;
int rc = SPIFFS_stat(&_fs, path, &stat);
return rc == SPIFFS_OK;
}
DirImplPtr SPIFFSImpl::openDir(const char* path)
{
if (strlen(path) > 0 && !isSpiffsFilenameValid(path)) {
DEBUGV("SPIFFSImpl::openDir: invalid path=`%s` \r\n", path);
return DirImplPtr();
}
spiffs_DIR dir;
spiffs_DIR* result = SPIFFS_opendir(&_fs, path, &dir);
if (!result) {
DEBUGV("SPIFFSImpl::openDir: path=`%s` err=%d\r\n", path, _fs.err_code);
return DirImplPtr();
}
return std::make_shared<SPIFFSDirImpl>(path, this, dir);
}
int getSpiffsMode(OpenMode openMode, AccessMode accessMode)
{
int mode = 0;
if (openMode & OM_CREATE) {
mode |= SPIFFS_CREAT;
}
if (openMode & OM_APPEND) {
mode |= SPIFFS_APPEND;
}
if (openMode & OM_TRUNCATE) {
mode |= SPIFFS_TRUNC;
}
if (accessMode & AM_READ) {
mode |= SPIFFS_RDONLY;
}
if (accessMode & AM_WRITE) {
mode |= SPIFFS_WRONLY;
}
return mode;
}
bool isSpiffsFilenameValid(const char* name)
{
if (name == nullptr) {
return false;
}
auto len = strlen(name);
return len > 0 && len < SPIFFS_OBJ_NAME_LEN;
}
}; // namespace
// these symbols should be defined in the linker script for each flash layout
#ifndef CORE_MOCK
#ifdef ARDUINO
#ifndef SPIFFS_MAX_OPEN_FILES
#define SPIFFS_MAX_OPEN_FILES 5
#endif
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SPIFFS)
FS SPIFFS = FS(FSImplPtr(new spiffs_impl::SPIFFSImpl(
FS_PHYS_ADDR,
FS_PHYS_SIZE,
FS_PHYS_PAGE,
FS_PHYS_BLOCK,
SPIFFS_MAX_OPEN_FILES)));
extern "C" void spiffs_request_end(void)
{
// override default weak function
//ets_printf("debug: not weak spiffs end\n");
SPIFFS.end();
}
#pragma GCC diagnostic pop
#endif // ARDUINO
#endif // !CORE_MOCK
#endif