|
| 1 | +/* |
| 2 | + FS.cpp - file system wrapper |
| 3 | + Copyright (c) 2015 Ivan Grokhotkov. All rights reserved. |
| 4 | + This file is part of the esp8266 core for Arduino environment. |
| 5 | +
|
| 6 | + This library is free software; you can redistribute it and/or |
| 7 | + modify it under the terms of the GNU Lesser General Public |
| 8 | + License as published by the Free Software Foundation; either |
| 9 | + version 2.1 of the License, or (at your option) any later version. |
| 10 | +
|
| 11 | + This library is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + Lesser General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU Lesser General Public |
| 17 | + License along with this library; if not, write to the Free Software |
| 18 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 | + */ |
| 20 | + |
| 21 | +#include "FS.h" |
| 22 | +#include "FSImpl.h" |
| 23 | + |
| 24 | +static bool sflags(const char* mode, OpenMode& om, AccessMode& am); |
| 25 | + |
| 26 | +size_t File::write(uint8_t c) { |
| 27 | + if (!_p) |
| 28 | + return 0; |
| 29 | + |
| 30 | + _p->write(&c, 1); |
| 31 | +} |
| 32 | + |
| 33 | +size_t File::write(const uint8_t *buf, size_t size) { |
| 34 | + if (!_p) |
| 35 | + return 0; |
| 36 | + |
| 37 | + _p->write(buf, size); |
| 38 | +} |
| 39 | + |
| 40 | +int File::available() { |
| 41 | + if (!_p) |
| 42 | + return false; |
| 43 | + |
| 44 | + return _p->position() < _p->size(); |
| 45 | +} |
| 46 | + |
| 47 | +int File::read() { |
| 48 | + if (!_p) |
| 49 | + return -1; |
| 50 | + |
| 51 | + uint8_t result; |
| 52 | + if (_p->read(&result, 1) != 1) { |
| 53 | + return -1; |
| 54 | + } |
| 55 | + |
| 56 | + return result; |
| 57 | +} |
| 58 | + |
| 59 | +size_t File::read(uint8_t* buf, size_t size) { |
| 60 | + if (!_p) |
| 61 | + return -1; |
| 62 | + |
| 63 | + return _p->read(buf, size); |
| 64 | +} |
| 65 | + |
| 66 | +int File::peek() { |
| 67 | + if (!_p) |
| 68 | + return -1; |
| 69 | + |
| 70 | + size_t curPos = _p->position(); |
| 71 | + int result = read(); |
| 72 | + seek(curPos, SeekSet); |
| 73 | + return result; |
| 74 | +} |
| 75 | + |
| 76 | +void File::flush() { |
| 77 | + if (!_p) |
| 78 | + return; |
| 79 | + |
| 80 | + _p->flush(); |
| 81 | +} |
| 82 | + |
| 83 | +bool File::seek(uint32_t pos, SeekMode mode) { |
| 84 | + if (!_p) |
| 85 | + return false; |
| 86 | + |
| 87 | + return _p->seek(pos, mode); |
| 88 | +} |
| 89 | + |
| 90 | +size_t File::position() const { |
| 91 | + if (!_p) |
| 92 | + return 0; |
| 93 | + |
| 94 | + return _p->position(); |
| 95 | +} |
| 96 | + |
| 97 | +size_t File::size() const { |
| 98 | + if (!_p) |
| 99 | + return 0; |
| 100 | + |
| 101 | + return _p->size(); |
| 102 | +} |
| 103 | + |
| 104 | +void File::close() { |
| 105 | + if (_p) { |
| 106 | + _p->close(); |
| 107 | + _p = nullptr; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +File::operator bool() const { |
| 112 | + return !!_p; |
| 113 | +} |
| 114 | + |
| 115 | +File Dir::openFile(const char* mode) { |
| 116 | + if (!_impl) { |
| 117 | + return File(); |
| 118 | + } |
| 119 | + |
| 120 | + OpenMode om; |
| 121 | + AccessMode am; |
| 122 | + if (!sflags(mode, om, am)) { |
| 123 | + DEBUGV("Dir::openFile: invalid mode `%s`\r\n", mode); |
| 124 | + return File(); |
| 125 | + } |
| 126 | + |
| 127 | + return File(_impl->openFile(om, am)); |
| 128 | +} |
| 129 | + |
| 130 | +String Dir::fileName() { |
| 131 | + if (!_impl) { |
| 132 | + return String(); |
| 133 | + } |
| 134 | + |
| 135 | + return _impl->fileName(); |
| 136 | +} |
| 137 | + |
| 138 | +bool Dir::next() { |
| 139 | + if (!_impl) { |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + return _impl->next(); |
| 144 | +} |
| 145 | + |
| 146 | + |
| 147 | + |
| 148 | +File FS::open(const char* path, const char* mode) { |
| 149 | + if (!_impl) { |
| 150 | + return File(); |
| 151 | + } |
| 152 | + |
| 153 | + OpenMode om; |
| 154 | + AccessMode am; |
| 155 | + if (!sflags(mode, om, am)) { |
| 156 | + DEBUGV("FS::open: invalid mode `%s`\r\n", mode); |
| 157 | + return File(); |
| 158 | + } |
| 159 | + |
| 160 | + return File(_impl->open(path, om, am)); |
| 161 | +} |
| 162 | + |
| 163 | +File FS::open(const String& path, const char* mode) { |
| 164 | + return FS::open(path.c_str(), mode); |
| 165 | +} |
| 166 | + |
| 167 | +Dir FS::openDir(const char* path) { |
| 168 | + if (!_impl) { |
| 169 | + return Dir(); |
| 170 | + } |
| 171 | + |
| 172 | + return Dir(_impl->openDir(path)); |
| 173 | +} |
| 174 | + |
| 175 | +Dir FS::openDir(const String& path) { |
| 176 | + return FS::openDir(path.c_str()); |
| 177 | +} |
| 178 | + |
| 179 | +struct MountEntry { |
| 180 | + FSImplPtr fs; |
| 181 | + String path; |
| 182 | + MountEntry* next; |
| 183 | +}; |
| 184 | + |
| 185 | +static MountEntry* s_mounted = nullptr; |
| 186 | + |
| 187 | +template<> |
| 188 | +bool mount<FS>(FS& fs, const char* mountPoint) { |
| 189 | + FSImplPtr p = fs._impl; |
| 190 | + if (!p || !p->mount()) { |
| 191 | + DEBUGV("FSImpl mount failed\r\n"); |
| 192 | + return false; |
| 193 | + } |
| 194 | + |
| 195 | + MountEntry* entry = new MountEntry; |
| 196 | + entry->fs = p; |
| 197 | + entry->path = mountPoint; |
| 198 | + entry->next = s_mounted; |
| 199 | + s_mounted = entry; |
| 200 | + return true; |
| 201 | +} |
| 202 | + |
| 203 | +static bool sflags(const char* mode, OpenMode& om, AccessMode& am) { |
| 204 | + switch (mode[0]) { |
| 205 | + case 'r': |
| 206 | + am = AM_READ; |
| 207 | + om = OM_DEFAULT; |
| 208 | + break; |
| 209 | + case 'w': |
| 210 | + am = AM_WRITE; |
| 211 | + om = (OpenMode) (OM_CREATE | OM_TRUNCATE); |
| 212 | + break; |
| 213 | + case 'a': |
| 214 | + am = AM_WRITE; |
| 215 | + om = (OpenMode) (OM_CREATE | OM_APPEND); |
| 216 | + break; |
| 217 | + default: |
| 218 | + return false; |
| 219 | + } |
| 220 | + switch(mode[1]) { |
| 221 | + case '+': |
| 222 | + am = (AccessMode) (AM_WRITE | AM_READ); |
| 223 | + break; |
| 224 | + case 0: |
| 225 | + break; |
| 226 | + default: |
| 227 | + return false; |
| 228 | + } |
| 229 | + return true; |
| 230 | +} |
0 commit comments