|
| 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 | +boolean FSClass::mount(){ |
| 25 | + if(_mounted) return true; |
| 26 | + _mounted = spiffs_mount(); |
| 27 | + return _mounted; |
| 28 | +} |
| 29 | + |
| 30 | +void FSClass::unmount(){ |
| 31 | + if(!_mounted) return; |
| 32 | + spiffs_unmount(); |
| 33 | + _mounted = false; |
| 34 | +} |
| 35 | + |
| 36 | +boolean FSClass::format(){ |
| 37 | + return spiffs_format(); |
| 38 | +} |
| 39 | + |
| 40 | +boolean FSClass::exists(const char *filename){ |
| 41 | + spiffs_stat stat = {0}; |
| 42 | + if (SPIFFS_stat(&_filesystemStorageHandle, filename, &stat) < 0) return false; |
| 43 | + return stat.name[0] != '\0'; |
| 44 | +} |
| 45 | + |
| 46 | +boolean FSClass::create(const char *filepath){ |
| 47 | + return SPIFFS_creat(&_filesystemStorageHandle, filepath, 0) == 0; |
| 48 | +} |
| 49 | + |
| 50 | +boolean FSClass::remove(const char *filepath){ |
| 51 | + return SPIFFS_remove(&_filesystemStorageHandle, filepath) == 0; |
| 52 | +} |
| 53 | + |
| 54 | +boolean FSClass::rename(const char *filename, const char *newname){ |
| 55 | + return SPIFFS_rename(&_filesystemStorageHandle, filename, newname) == 0; |
| 56 | +} |
| 57 | + |
| 58 | +FSFile FSClass::open(const char *filename, uint8_t mode){ |
| 59 | + int repeats = 0; |
| 60 | + bool notExist; |
| 61 | + bool canRecreate = (mode & SPIFFS_CREAT) == SPIFFS_CREAT; |
| 62 | + int res; |
| 63 | + |
| 64 | + do{ |
| 65 | + notExist = false; |
| 66 | + res = SPIFFS_open(&_filesystemStorageHandle, filename, (spiffs_flags)mode, 0); |
| 67 | + int code = SPIFFS_errno(&_filesystemStorageHandle); |
| 68 | + if (res < 0){ |
| 69 | + debugf("open errno %d\n", code); |
| 70 | + notExist = (code == SPIFFS_ERR_NOT_FOUND || code == SPIFFS_ERR_DELETED || code == SPIFFS_ERR_FILE_DELETED || code == SPIFFS_ERR_IS_FREE); |
| 71 | + if (notExist && canRecreate) |
| 72 | + remove(filename); // fix for deleted files |
| 73 | + } |
| 74 | + } while (notExist && canRecreate && repeats++ < 3); |
| 75 | + |
| 76 | + if(res){ |
| 77 | + return FSFile(res); |
| 78 | + } |
| 79 | + return FSFile(); |
| 80 | +} |
| 81 | + |
| 82 | +FSClass FS; |
| 83 | + |
| 84 | +FSFile::FSFile(){ |
| 85 | + _file = 0; |
| 86 | + _stats = {0}; |
| 87 | +} |
| 88 | + |
| 89 | +FSFile::FSFile(file_t f){ |
| 90 | + _file = f; |
| 91 | + if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0){ |
| 92 | + debugf("mount errno %d\n", SPIFFS_errno(&_filesystemStorageHandle)); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void FSFile::close(){ |
| 97 | + if (! _file) return; |
| 98 | + SPIFFS_close(&_filesystemStorageHandle, _file); |
| 99 | + _file = 0; |
| 100 | +} |
| 101 | + |
| 102 | +uint32_t FSFile::size(){ |
| 103 | + if(! _file) return 0; |
| 104 | + uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file); |
| 105 | + SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END); |
| 106 | + uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file); |
| 107 | + SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET); |
| 108 | + return size; |
| 109 | +} |
| 110 | + |
| 111 | +uint32_t FSFile::seek(uint32_t pos){ |
| 112 | + if (! _file) return 0; |
| 113 | + return SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET); |
| 114 | +} |
| 115 | + |
| 116 | +uint32_t FSFile::position(){ |
| 117 | + if (! _file) return 0; |
| 118 | + return SPIFFS_tell(&_filesystemStorageHandle, _file); |
| 119 | +} |
| 120 | + |
| 121 | +boolean FSFile::eof(){ |
| 122 | + if (! _file) return 0; |
| 123 | + return SPIFFS_eof(&_filesystemStorageHandle, _file); |
| 124 | +} |
| 125 | + |
| 126 | +boolean FSFile::isDirectory(void){ |
| 127 | + return false; |
| 128 | +} |
| 129 | + |
| 130 | +int FSFile::read(void *buf, uint16_t nbyte){ |
| 131 | + if (! _file) return -1; |
| 132 | + return SPIFFS_read(&_filesystemStorageHandle, _file, buf, nbyte); |
| 133 | +} |
| 134 | + |
| 135 | +int FSFile::read(){ |
| 136 | + if (! _file) return -1; |
| 137 | + int val; |
| 138 | + if(SPIFFS_read(&_filesystemStorageHandle, _file, &val, 1) != 1) return -1; |
| 139 | + return val; |
| 140 | +} |
| 141 | + |
| 142 | +int FSFile::peek() { |
| 143 | + if (! _file) return 0; |
| 144 | + int c = read(); |
| 145 | + SPIFFS_lseek(&_filesystemStorageHandle, _file, -1, SPIFFS_SEEK_CUR); |
| 146 | + return c; |
| 147 | +} |
| 148 | + |
| 149 | +int FSFile::available() { |
| 150 | + if (! _file) return 0; |
| 151 | + uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file); |
| 152 | + SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END); |
| 153 | + uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file); |
| 154 | + SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET); |
| 155 | + return size - pos; |
| 156 | +} |
| 157 | + |
| 158 | +size_t FSFile::write(const uint8_t *buf, size_t size){ |
| 159 | + if (! _file) return 0; |
| 160 | + return SPIFFS_write(&_filesystemStorageHandle, _file, (uint8_t *)buf, size); |
| 161 | +} |
| 162 | + |
| 163 | +size_t FSFile::write(uint8_t val) { |
| 164 | + if (! _file) return 0; |
| 165 | + return write(&val, 1); |
| 166 | +} |
| 167 | + |
| 168 | +void FSFile::flush(){ |
| 169 | + if (! _file) return; |
| 170 | + SPIFFS_fflush(&_filesystemStorageHandle, _file); |
| 171 | +} |
| 172 | + |
| 173 | +uint32_t FSFile::remove(){ |
| 174 | + if (! _file) return 0; |
| 175 | + return SPIFFS_fremove(&_filesystemStorageHandle, _file); |
| 176 | + _file = 0; |
| 177 | +} |
| 178 | + |
| 179 | +int FSFile::lastError(){ |
| 180 | + return SPIFFS_errno(&_filesystemStorageHandle); |
| 181 | +} |
| 182 | + |
| 183 | +void FSFile::clearError(){ |
| 184 | + _filesystemStorageHandle.errno = SPIFFS_OK; |
| 185 | +} |
| 186 | + |
| 187 | +char * FSFile::name(){ |
| 188 | + return 0; |
| 189 | +} |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | +/* |
| 197 | +spiffs_DIR *dirOpen(spiffs_DIR *d){ |
| 198 | + return SPIFFS_opendir(&_filesystemStorageHandle, 0, d); |
| 199 | +} |
| 200 | +
|
| 201 | +int dirClose(spiffs_DIR *d){ |
| 202 | + return SPIFFS_closedir(d); |
| 203 | +} |
| 204 | +
|
| 205 | +file_t dirOpenFile(spiffs_dirent* entry, uint8_t flags){ |
| 206 | + return SPIFFS_open_by_dirent(&_filesystemStorageHandle, entry, (spiffs_flags)flags, 0); |
| 207 | +} |
| 208 | +*/ |
0 commit comments