|
| 1 | +/* |
| 2 | + FileSystem.cpp - SPIFS implementation for esp8266 |
| 3 | +
|
| 4 | + Copyright (c) 2015 Hristo Gochkov. All rights reserved. |
| 5 | + This file is part of the esp8266 core for Arduino environment. |
| 6 | + |
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +*/ |
| 21 | +#include "FileSystem.h" |
| 22 | +#include "Arduino.h" |
| 23 | + |
| 24 | +bool FSClass::mount() { |
| 25 | + if (_mounted) |
| 26 | + return true; |
| 27 | + |
| 28 | + _mounted = spiffs_mount(); |
| 29 | + return _mounted; |
| 30 | +} |
| 31 | + |
| 32 | +void FSClass::unmount() { |
| 33 | + if (!_mounted) |
| 34 | + return; |
| 35 | + |
| 36 | + spiffs_unmount(); |
| 37 | + _mounted = false; |
| 38 | +} |
| 39 | + |
| 40 | +bool FSClass::format() { |
| 41 | + return spiffs_format(); |
| 42 | +} |
| 43 | + |
| 44 | +bool FSClass::exists(const char *filename) { |
| 45 | + spiffs_stat stat = {0}; |
| 46 | + if (SPIFFS_stat(&_filesystemStorageHandle, filename, &stat) < 0) |
| 47 | + return false; |
| 48 | + return stat.name[0] != '\0'; |
| 49 | +} |
| 50 | + |
| 51 | +bool FSClass::create(const char *filepath){ |
| 52 | + return SPIFFS_creat(&_filesystemStorageHandle, filepath, 0) == 0; |
| 53 | +} |
| 54 | + |
| 55 | +bool FSClass::remove(const char *filepath){ |
| 56 | + return SPIFFS_remove(&_filesystemStorageHandle, filepath) == 0; |
| 57 | +} |
| 58 | + |
| 59 | +bool FSClass::rename(const char *filename, const char *newname) { |
| 60 | + return SPIFFS_rename(&_filesystemStorageHandle, filename, newname) == 0; |
| 61 | +} |
| 62 | + |
| 63 | +FSFile FSClass::open(const char *filename, uint8_t mode) { |
| 64 | + int repeats = 0; |
| 65 | + bool notExist; |
| 66 | + bool canRecreate = (mode & SPIFFS_CREAT) == SPIFFS_CREAT; |
| 67 | + int res; |
| 68 | + |
| 69 | + do{ |
| 70 | + notExist = false; |
| 71 | + res = SPIFFS_open(&_filesystemStorageHandle, filename, (spiffs_flags)mode, 0); |
| 72 | + int code = SPIFFS_errno(&_filesystemStorageHandle); |
| 73 | + if (res < 0){ |
| 74 | + debugf("open errno %d\n", code); |
| 75 | + notExist = (code == SPIFFS_ERR_NOT_FOUND || code == SPIFFS_ERR_DELETED || code == SPIFFS_ERR_FILE_DELETED || code == SPIFFS_ERR_IS_FREE); |
| 76 | + if (notExist && canRecreate) |
| 77 | + remove(filename); // fix for deleted files |
| 78 | + } |
| 79 | + } while (notExist && canRecreate && repeats++ < 3); |
| 80 | + |
| 81 | + if(res){ |
| 82 | + return FSFile(res); |
| 83 | + } |
| 84 | + return FSFile(); |
| 85 | +} |
| 86 | + |
| 87 | +FSClass FS; |
| 88 | + |
| 89 | +FSFile::FSFile() { |
| 90 | + _file = 0; |
| 91 | + _stats = {0}; |
| 92 | +} |
| 93 | + |
| 94 | +FSFile::FSFile(file_t f) { |
| 95 | + _file = f; |
| 96 | + if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0){ |
| 97 | + debugf("mount errno %d\n", SPIFFS_errno(&_filesystemStorageHandle)); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +void FSFile::close() { |
| 102 | + if (! _file) return; |
| 103 | + SPIFFS_close(&_filesystemStorageHandle, _file); |
| 104 | + _file = 0; |
| 105 | +} |
| 106 | + |
| 107 | +uint32_t FSFile::size() { |
| 108 | + if(! _file) return 0; |
| 109 | + uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file); |
| 110 | + SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END); |
| 111 | + uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file); |
| 112 | + SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET); |
| 113 | + return size; |
| 114 | +} |
| 115 | + |
| 116 | +uint32_t FSFile::seek(uint32_t pos) { |
| 117 | + if (! _file) return 0; |
| 118 | + return SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET); |
| 119 | +} |
| 120 | + |
| 121 | +uint32_t FSFile::position() { |
| 122 | + if (! _file) return 0; |
| 123 | + return SPIFFS_tell(&_filesystemStorageHandle, _file); |
| 124 | +} |
| 125 | + |
| 126 | +bool FSFile::eof() { |
| 127 | + if (! _file) return 0; |
| 128 | + return SPIFFS_eof(&_filesystemStorageHandle, _file); |
| 129 | +} |
| 130 | + |
| 131 | +bool FSFile::isDirectory(void) { |
| 132 | + return false; |
| 133 | +} |
| 134 | + |
| 135 | +int FSFile::read(void *buf, uint16_t nbyte) { |
| 136 | + if (! _file) return -1; |
| 137 | + return SPIFFS_read(&_filesystemStorageHandle, _file, buf, nbyte); |
| 138 | +} |
| 139 | + |
| 140 | +int FSFile::read() { |
| 141 | + if (! _file) return -1; |
| 142 | + int val; |
| 143 | + if(SPIFFS_read(&_filesystemStorageHandle, _file, &val, 1) != 1) return -1; |
| 144 | + return val; |
| 145 | +} |
| 146 | + |
| 147 | +int FSFile::peek() { |
| 148 | + if (! _file) return 0; |
| 149 | + int c = read(); |
| 150 | + SPIFFS_lseek(&_filesystemStorageHandle, _file, -1, SPIFFS_SEEK_CUR); |
| 151 | + return c; |
| 152 | +} |
| 153 | + |
| 154 | +int FSFile::available() { |
| 155 | + if (! _file) return 0; |
| 156 | + uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file); |
| 157 | + SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END); |
| 158 | + uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file); |
| 159 | + SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET); |
| 160 | + return size - pos; |
| 161 | +} |
| 162 | + |
| 163 | +size_t FSFile::write(const uint8_t *buf, size_t size){ |
| 164 | + if (! _file) return 0; |
| 165 | + return SPIFFS_write(&_filesystemStorageHandle, _file, (uint8_t *)buf, size); |
| 166 | +} |
| 167 | + |
| 168 | +size_t FSFile::write(uint8_t val) { |
| 169 | + if (! _file) return 0; |
| 170 | + return write(&val, 1); |
| 171 | +} |
| 172 | + |
| 173 | +void FSFile::flush(){ |
| 174 | + if (! _file) return; |
| 175 | + SPIFFS_fflush(&_filesystemStorageHandle, _file); |
| 176 | +} |
| 177 | + |
| 178 | +uint32_t FSFile::remove(){ |
| 179 | + if (! _file) return 0; |
| 180 | + return SPIFFS_fremove(&_filesystemStorageHandle, _file); |
| 181 | + _file = 0; |
| 182 | +} |
| 183 | + |
| 184 | +int FSFile::lastError(){ |
| 185 | + return SPIFFS_errno(&_filesystemStorageHandle); |
| 186 | +} |
| 187 | + |
| 188 | +void FSFile::clearError(){ |
| 189 | + _filesystemStorageHandle.errno = SPIFFS_OK; |
| 190 | +} |
| 191 | + |
| 192 | +char * FSFile::name(){ |
| 193 | + return 0; |
| 194 | +} |
| 195 | + |
| 196 | + |
| 197 | + |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | +/* |
| 202 | +spiffs_DIR *dirOpen(spiffs_DIR *d){ |
| 203 | + return SPIFFS_opendir(&_filesystemStorageHandle, 0, d); |
| 204 | +} |
| 205 | +
|
| 206 | +int dirClose(spiffs_DIR *d){ |
| 207 | + return SPIFFS_closedir(d); |
| 208 | +} |
| 209 | +
|
| 210 | +file_t dirOpenFile(spiffs_dirent* entry, uint8_t flags){ |
| 211 | + return SPIFFS_open_by_dirent(&_filesystemStorageHandle, entry, (spiffs_flags)flags, 0); |
| 212 | +} |
| 213 | +*/ |
0 commit comments