Committer:
borlanic
Date:
Thu Mar 29 07:02:09 2018 +0000
Revision:
0:380207fcb5c1
Encoder, IMU --> OK; Controller --> in bearbeitung

Who changed what in which revision?

UserRevisionLine numberNew contents of line
borlanic 0:380207fcb5c1 1 /* mbed Microcontroller Library
borlanic 0:380207fcb5c1 2 * Copyright (c) 2017 ARM Limited
borlanic 0:380207fcb5c1 3 *
borlanic 0:380207fcb5c1 4 * Licensed under the Apache License, Version 2.0 (the "License");
borlanic 0:380207fcb5c1 5 * you may not use this file except in compliance with the License.
borlanic 0:380207fcb5c1 6 * You may obtain a copy of the License at
borlanic 0:380207fcb5c1 7 *
borlanic 0:380207fcb5c1 8 * http://www.apache.org/licenses/LICENSE-2.0
borlanic 0:380207fcb5c1 9 *
borlanic 0:380207fcb5c1 10 * Unless required by applicable law or agreed to in writing, software
borlanic 0:380207fcb5c1 11 * distributed under the License is distributed on an "AS IS" BASIS,
borlanic 0:380207fcb5c1 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
borlanic 0:380207fcb5c1 13 * See the License for the specific language governing permissions and
borlanic 0:380207fcb5c1 14 * limitations under the License.
borlanic 0:380207fcb5c1 15 */
borlanic 0:380207fcb5c1 16 #include "mbed.h"
borlanic 0:380207fcb5c1 17 #include "LittleFileSystem.h"
borlanic 0:380207fcb5c1 18 #include "errno.h"
borlanic 0:380207fcb5c1 19 extern "C" {
borlanic 0:380207fcb5c1 20 #include "lfs.h"
borlanic 0:380207fcb5c1 21 #include "lfs_util.h"
borlanic 0:380207fcb5c1 22 }
borlanic 0:380207fcb5c1 23
borlanic 0:380207fcb5c1 24
borlanic 0:380207fcb5c1 25 ////// Conversion functions //////
borlanic 0:380207fcb5c1 26 static int lfs_toerror(int err)
borlanic 0:380207fcb5c1 27 {
borlanic 0:380207fcb5c1 28 switch (err) {
borlanic 0:380207fcb5c1 29 case LFS_ERR_OK: return 0;
borlanic 0:380207fcb5c1 30 case LFS_ERR_IO: return -EIO;
borlanic 0:380207fcb5c1 31 case LFS_ERR_NOENT: return -ENOENT;
borlanic 0:380207fcb5c1 32 case LFS_ERR_EXIST: return -EEXIST;
borlanic 0:380207fcb5c1 33 case LFS_ERR_NOTDIR: return -ENOTDIR;
borlanic 0:380207fcb5c1 34 case LFS_ERR_ISDIR: return -EISDIR;
borlanic 0:380207fcb5c1 35 case LFS_ERR_INVAL: return -EINVAL;
borlanic 0:380207fcb5c1 36 case LFS_ERR_NOSPC: return -ENOSPC;
borlanic 0:380207fcb5c1 37 case LFS_ERR_NOMEM: return -ENOMEM;
borlanic 0:380207fcb5c1 38 default: return err;
borlanic 0:380207fcb5c1 39 }
borlanic 0:380207fcb5c1 40 }
borlanic 0:380207fcb5c1 41
borlanic 0:380207fcb5c1 42 static int lfs_fromflags(int flags)
borlanic 0:380207fcb5c1 43 {
borlanic 0:380207fcb5c1 44 return (
borlanic 0:380207fcb5c1 45 (((flags & 3) == O_RDONLY) ? LFS_O_RDONLY : 0) |
borlanic 0:380207fcb5c1 46 (((flags & 3) == O_WRONLY) ? LFS_O_WRONLY : 0) |
borlanic 0:380207fcb5c1 47 (((flags & 3) == O_RDWR) ? LFS_O_RDWR : 0) |
borlanic 0:380207fcb5c1 48 ((flags & O_CREAT) ? LFS_O_CREAT : 0) |
borlanic 0:380207fcb5c1 49 ((flags & O_EXCL) ? LFS_O_EXCL : 0) |
borlanic 0:380207fcb5c1 50 ((flags & O_TRUNC) ? LFS_O_TRUNC : 0) |
borlanic 0:380207fcb5c1 51 ((flags & O_APPEND) ? LFS_O_APPEND : 0));
borlanic 0:380207fcb5c1 52 }
borlanic 0:380207fcb5c1 53
borlanic 0:380207fcb5c1 54 static int lfs_fromwhence(int whence)
borlanic 0:380207fcb5c1 55 {
borlanic 0:380207fcb5c1 56 switch (whence) {
borlanic 0:380207fcb5c1 57 case SEEK_SET: return LFS_SEEK_SET;
borlanic 0:380207fcb5c1 58 case SEEK_CUR: return LFS_SEEK_CUR;
borlanic 0:380207fcb5c1 59 case SEEK_END: return LFS_SEEK_END;
borlanic 0:380207fcb5c1 60 default: return whence;
borlanic 0:380207fcb5c1 61 }
borlanic 0:380207fcb5c1 62 }
borlanic 0:380207fcb5c1 63
borlanic 0:380207fcb5c1 64 static int lfs_tomode(int type)
borlanic 0:380207fcb5c1 65 {
borlanic 0:380207fcb5c1 66 int mode = S_IRWXU | S_IRWXG | S_IRWXO;
borlanic 0:380207fcb5c1 67 switch (type) {
borlanic 0:380207fcb5c1 68 case LFS_TYPE_DIR: return mode | S_IFDIR;
borlanic 0:380207fcb5c1 69 case LFS_TYPE_REG: return mode | S_IFREG;
borlanic 0:380207fcb5c1 70 default: return 0;
borlanic 0:380207fcb5c1 71 }
borlanic 0:380207fcb5c1 72 }
borlanic 0:380207fcb5c1 73
borlanic 0:380207fcb5c1 74 static int lfs_totype(int type)
borlanic 0:380207fcb5c1 75 {
borlanic 0:380207fcb5c1 76 switch (type) {
borlanic 0:380207fcb5c1 77 case LFS_TYPE_DIR: return DT_DIR;
borlanic 0:380207fcb5c1 78 case LFS_TYPE_REG: return DT_REG;
borlanic 0:380207fcb5c1 79 default: return DT_UNKNOWN;
borlanic 0:380207fcb5c1 80 }
borlanic 0:380207fcb5c1 81 }
borlanic 0:380207fcb5c1 82
borlanic 0:380207fcb5c1 83
borlanic 0:380207fcb5c1 84 ////// Block device operations //////
borlanic 0:380207fcb5c1 85 static int lfs_bd_read(const struct lfs_config *c, lfs_block_t block,
borlanic 0:380207fcb5c1 86 lfs_off_t off, void *buffer, lfs_size_t size) {
borlanic 0:380207fcb5c1 87 BlockDevice *bd = (BlockDevice *)c->context;
borlanic 0:380207fcb5c1 88 return bd->read(buffer, (bd_addr_t)block*c->block_size + off, size);
borlanic 0:380207fcb5c1 89 }
borlanic 0:380207fcb5c1 90
borlanic 0:380207fcb5c1 91 static int lfs_bd_prog(const struct lfs_config *c, lfs_block_t block,
borlanic 0:380207fcb5c1 92 lfs_off_t off, const void *buffer, lfs_size_t size) {
borlanic 0:380207fcb5c1 93 BlockDevice *bd = (BlockDevice *)c->context;
borlanic 0:380207fcb5c1 94 return bd->program(buffer, (bd_addr_t)block*c->block_size + off, size);
borlanic 0:380207fcb5c1 95 }
borlanic 0:380207fcb5c1 96
borlanic 0:380207fcb5c1 97 static int lfs_bd_erase(const struct lfs_config *c, lfs_block_t block)
borlanic 0:380207fcb5c1 98 {
borlanic 0:380207fcb5c1 99 BlockDevice *bd = (BlockDevice *)c->context;
borlanic 0:380207fcb5c1 100 return bd->erase((bd_addr_t)block*c->block_size, c->block_size);
borlanic 0:380207fcb5c1 101 }
borlanic 0:380207fcb5c1 102
borlanic 0:380207fcb5c1 103 static int lfs_bd_sync(const struct lfs_config *c)
borlanic 0:380207fcb5c1 104 {
borlanic 0:380207fcb5c1 105 BlockDevice *bd = (BlockDevice *)c->context;
borlanic 0:380207fcb5c1 106 return bd->sync();
borlanic 0:380207fcb5c1 107 }
borlanic 0:380207fcb5c1 108
borlanic 0:380207fcb5c1 109
borlanic 0:380207fcb5c1 110 ////// Generic filesystem operations //////
borlanic 0:380207fcb5c1 111
borlanic 0:380207fcb5c1 112 // Filesystem implementation (See LittleFileSystem.h)
borlanic 0:380207fcb5c1 113 LittleFileSystem::LittleFileSystem(const char *name, BlockDevice *bd,
borlanic 0:380207fcb5c1 114 lfs_size_t read_size, lfs_size_t prog_size,
borlanic 0:380207fcb5c1 115 lfs_size_t block_size, lfs_size_t lookahead)
borlanic 0:380207fcb5c1 116 : FileSystem(name)
borlanic 0:380207fcb5c1 117 , _read_size(read_size)
borlanic 0:380207fcb5c1 118 , _prog_size(prog_size)
borlanic 0:380207fcb5c1 119 , _block_size(block_size)
borlanic 0:380207fcb5c1 120 , _lookahead(lookahead) {
borlanic 0:380207fcb5c1 121 if (bd) {
borlanic 0:380207fcb5c1 122 mount(bd);
borlanic 0:380207fcb5c1 123 }
borlanic 0:380207fcb5c1 124 }
borlanic 0:380207fcb5c1 125
borlanic 0:380207fcb5c1 126 LittleFileSystem::~LittleFileSystem() {
borlanic 0:380207fcb5c1 127 // nop if unmounted
borlanic 0:380207fcb5c1 128 unmount();
borlanic 0:380207fcb5c1 129 }
borlanic 0:380207fcb5c1 130
borlanic 0:380207fcb5c1 131 int LittleFileSystem::mount(BlockDevice *bd)
borlanic 0:380207fcb5c1 132 {
borlanic 0:380207fcb5c1 133 _mutex.lock();
borlanic 0:380207fcb5c1 134 LFS_INFO("mount(%p)", bd);
borlanic 0:380207fcb5c1 135 _bd = bd;
borlanic 0:380207fcb5c1 136 int err = _bd->init();
borlanic 0:380207fcb5c1 137 if (err) {
borlanic 0:380207fcb5c1 138 LFS_INFO("mount -> %d", err);
borlanic 0:380207fcb5c1 139 _mutex.unlock();
borlanic 0:380207fcb5c1 140 return err;
borlanic 0:380207fcb5c1 141 }
borlanic 0:380207fcb5c1 142
borlanic 0:380207fcb5c1 143 memset(&_config, 0, sizeof(_config));
borlanic 0:380207fcb5c1 144 _config.context = bd;
borlanic 0:380207fcb5c1 145 _config.read = lfs_bd_read;
borlanic 0:380207fcb5c1 146 _config.prog = lfs_bd_prog;
borlanic 0:380207fcb5c1 147 _config.erase = lfs_bd_erase;
borlanic 0:380207fcb5c1 148 _config.sync = lfs_bd_sync;
borlanic 0:380207fcb5c1 149 _config.read_size = bd->get_read_size();
borlanic 0:380207fcb5c1 150 if (_config.read_size < _read_size) {
borlanic 0:380207fcb5c1 151 _config.read_size = _read_size;
borlanic 0:380207fcb5c1 152 }
borlanic 0:380207fcb5c1 153 _config.prog_size = bd->get_program_size();
borlanic 0:380207fcb5c1 154 if (_config.prog_size < _prog_size) {
borlanic 0:380207fcb5c1 155 _config.prog_size = _prog_size;
borlanic 0:380207fcb5c1 156 }
borlanic 0:380207fcb5c1 157 _config.block_size = bd->get_erase_size();
borlanic 0:380207fcb5c1 158 if (_config.block_size < _block_size) {
borlanic 0:380207fcb5c1 159 _config.block_size = _block_size;
borlanic 0:380207fcb5c1 160 }
borlanic 0:380207fcb5c1 161 _config.block_count = bd->size() / _config.block_size;
borlanic 0:380207fcb5c1 162 _config.lookahead = 32 * ((_config.block_count+31)/32);
borlanic 0:380207fcb5c1 163 if (_config.lookahead > _lookahead) {
borlanic 0:380207fcb5c1 164 _config.lookahead = _lookahead;
borlanic 0:380207fcb5c1 165 }
borlanic 0:380207fcb5c1 166
borlanic 0:380207fcb5c1 167 err = lfs_mount(&_lfs, &_config);
borlanic 0:380207fcb5c1 168 LFS_INFO("mount -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 169 _mutex.unlock();
borlanic 0:380207fcb5c1 170 return lfs_toerror(err);
borlanic 0:380207fcb5c1 171 }
borlanic 0:380207fcb5c1 172
borlanic 0:380207fcb5c1 173 int LittleFileSystem::unmount()
borlanic 0:380207fcb5c1 174 {
borlanic 0:380207fcb5c1 175 _mutex.lock();
borlanic 0:380207fcb5c1 176 LFS_INFO("unmount(%s)", "");
borlanic 0:380207fcb5c1 177 if (_bd) {
borlanic 0:380207fcb5c1 178 int err = lfs_unmount(&_lfs);
borlanic 0:380207fcb5c1 179 if (err) {
borlanic 0:380207fcb5c1 180 LFS_INFO("unmount -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 181 _mutex.unlock();
borlanic 0:380207fcb5c1 182 return lfs_toerror(err);
borlanic 0:380207fcb5c1 183 }
borlanic 0:380207fcb5c1 184
borlanic 0:380207fcb5c1 185 err = _bd->deinit();
borlanic 0:380207fcb5c1 186 if (err) {
borlanic 0:380207fcb5c1 187 LFS_INFO("unmount -> %d", err);
borlanic 0:380207fcb5c1 188 _mutex.unlock();
borlanic 0:380207fcb5c1 189 return err;
borlanic 0:380207fcb5c1 190 }
borlanic 0:380207fcb5c1 191
borlanic 0:380207fcb5c1 192 _bd = NULL;
borlanic 0:380207fcb5c1 193 }
borlanic 0:380207fcb5c1 194
borlanic 0:380207fcb5c1 195 LFS_INFO("unmount -> %d", 0);
borlanic 0:380207fcb5c1 196 _mutex.unlock();
borlanic 0:380207fcb5c1 197 return 0;
borlanic 0:380207fcb5c1 198 }
borlanic 0:380207fcb5c1 199
borlanic 0:380207fcb5c1 200 int LittleFileSystem::format(BlockDevice *bd,
borlanic 0:380207fcb5c1 201 lfs_size_t read_size, lfs_size_t prog_size,
borlanic 0:380207fcb5c1 202 lfs_size_t block_size, lfs_size_t lookahead) {
borlanic 0:380207fcb5c1 203 LFS_INFO("format(%p, %ld, %ld, %ld, %ld)",
borlanic 0:380207fcb5c1 204 bd, read_size, prog_size, block_size, lookahead);
borlanic 0:380207fcb5c1 205 int err = bd->init();
borlanic 0:380207fcb5c1 206 if (err) {
borlanic 0:380207fcb5c1 207 LFS_INFO("format -> %d", err);
borlanic 0:380207fcb5c1 208 return err;
borlanic 0:380207fcb5c1 209 }
borlanic 0:380207fcb5c1 210
borlanic 0:380207fcb5c1 211 lfs_t _lfs;
borlanic 0:380207fcb5c1 212 struct lfs_config _config;
borlanic 0:380207fcb5c1 213
borlanic 0:380207fcb5c1 214 memset(&_config, 0, sizeof(_config));
borlanic 0:380207fcb5c1 215 _config.context = bd;
borlanic 0:380207fcb5c1 216 _config.read = lfs_bd_read;
borlanic 0:380207fcb5c1 217 _config.prog = lfs_bd_prog;
borlanic 0:380207fcb5c1 218 _config.erase = lfs_bd_erase;
borlanic 0:380207fcb5c1 219 _config.sync = lfs_bd_sync;
borlanic 0:380207fcb5c1 220 _config.read_size = bd->get_read_size();
borlanic 0:380207fcb5c1 221 if (_config.read_size < read_size) {
borlanic 0:380207fcb5c1 222 _config.read_size = read_size;
borlanic 0:380207fcb5c1 223 }
borlanic 0:380207fcb5c1 224 _config.prog_size = bd->get_program_size();
borlanic 0:380207fcb5c1 225 if (_config.prog_size < prog_size) {
borlanic 0:380207fcb5c1 226 _config.prog_size = prog_size;
borlanic 0:380207fcb5c1 227 }
borlanic 0:380207fcb5c1 228 _config.block_size = bd->get_erase_size();
borlanic 0:380207fcb5c1 229 if (_config.block_size < block_size) {
borlanic 0:380207fcb5c1 230 _config.block_size = block_size;
borlanic 0:380207fcb5c1 231 }
borlanic 0:380207fcb5c1 232 _config.block_count = bd->size() / _config.block_size;
borlanic 0:380207fcb5c1 233 _config.lookahead = 32 * ((_config.block_count+31)/32);
borlanic 0:380207fcb5c1 234 if (_config.lookahead > lookahead) {
borlanic 0:380207fcb5c1 235 _config.lookahead = lookahead;
borlanic 0:380207fcb5c1 236 }
borlanic 0:380207fcb5c1 237
borlanic 0:380207fcb5c1 238 err = lfs_format(&_lfs, &_config);
borlanic 0:380207fcb5c1 239 if (err) {
borlanic 0:380207fcb5c1 240 LFS_INFO("format -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 241 return lfs_toerror(err);
borlanic 0:380207fcb5c1 242 }
borlanic 0:380207fcb5c1 243
borlanic 0:380207fcb5c1 244 err = bd->deinit();
borlanic 0:380207fcb5c1 245 if (err) {
borlanic 0:380207fcb5c1 246 LFS_INFO("format -> %d", err);
borlanic 0:380207fcb5c1 247 return err;
borlanic 0:380207fcb5c1 248 }
borlanic 0:380207fcb5c1 249
borlanic 0:380207fcb5c1 250 LFS_INFO("format -> %d", 0);
borlanic 0:380207fcb5c1 251 return 0;
borlanic 0:380207fcb5c1 252 }
borlanic 0:380207fcb5c1 253
borlanic 0:380207fcb5c1 254 int LittleFileSystem::reformat(BlockDevice *bd)
borlanic 0:380207fcb5c1 255 {
borlanic 0:380207fcb5c1 256 _mutex.lock();
borlanic 0:380207fcb5c1 257 LFS_INFO("reformat(%p)", bd);
borlanic 0:380207fcb5c1 258 if (_bd) {
borlanic 0:380207fcb5c1 259 if (!bd) {
borlanic 0:380207fcb5c1 260 bd = _bd;
borlanic 0:380207fcb5c1 261 }
borlanic 0:380207fcb5c1 262
borlanic 0:380207fcb5c1 263 int err = unmount();
borlanic 0:380207fcb5c1 264 if (err) {
borlanic 0:380207fcb5c1 265 LFS_INFO("reformat -> %d", err);
borlanic 0:380207fcb5c1 266 _mutex.unlock();
borlanic 0:380207fcb5c1 267 return err;
borlanic 0:380207fcb5c1 268 }
borlanic 0:380207fcb5c1 269 }
borlanic 0:380207fcb5c1 270
borlanic 0:380207fcb5c1 271 if (!bd) {
borlanic 0:380207fcb5c1 272 LFS_INFO("reformat -> %d", -ENODEV);
borlanic 0:380207fcb5c1 273 _mutex.unlock();
borlanic 0:380207fcb5c1 274 return -ENODEV;
borlanic 0:380207fcb5c1 275 }
borlanic 0:380207fcb5c1 276
borlanic 0:380207fcb5c1 277 int err = LittleFileSystem::format(bd,
borlanic 0:380207fcb5c1 278 _read_size, _prog_size, _block_size, _lookahead);
borlanic 0:380207fcb5c1 279 if (err) {
borlanic 0:380207fcb5c1 280 LFS_INFO("reformat -> %d", err);
borlanic 0:380207fcb5c1 281 _mutex.unlock();
borlanic 0:380207fcb5c1 282 return err;
borlanic 0:380207fcb5c1 283 }
borlanic 0:380207fcb5c1 284
borlanic 0:380207fcb5c1 285 err = mount(bd);
borlanic 0:380207fcb5c1 286 if (err) {
borlanic 0:380207fcb5c1 287 LFS_INFO("reformat -> %d", err);
borlanic 0:380207fcb5c1 288 _mutex.unlock();
borlanic 0:380207fcb5c1 289 return err;
borlanic 0:380207fcb5c1 290 }
borlanic 0:380207fcb5c1 291
borlanic 0:380207fcb5c1 292 LFS_INFO("reformat -> %d", 0);
borlanic 0:380207fcb5c1 293 _mutex.unlock();
borlanic 0:380207fcb5c1 294 return 0;
borlanic 0:380207fcb5c1 295 }
borlanic 0:380207fcb5c1 296
borlanic 0:380207fcb5c1 297 int LittleFileSystem::remove(const char *filename)
borlanic 0:380207fcb5c1 298 {
borlanic 0:380207fcb5c1 299 _mutex.lock();
borlanic 0:380207fcb5c1 300 LFS_INFO("remove(\"%s\")", filename);
borlanic 0:380207fcb5c1 301 int err = lfs_remove(&_lfs, filename);
borlanic 0:380207fcb5c1 302 LFS_INFO("remove -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 303 _mutex.unlock();
borlanic 0:380207fcb5c1 304 return lfs_toerror(err);
borlanic 0:380207fcb5c1 305 }
borlanic 0:380207fcb5c1 306
borlanic 0:380207fcb5c1 307 int LittleFileSystem::rename(const char *oldname, const char *newname)
borlanic 0:380207fcb5c1 308 {
borlanic 0:380207fcb5c1 309 _mutex.lock();
borlanic 0:380207fcb5c1 310 LFS_INFO("rename(\"%s\", \"%s\")", oldname, newname);
borlanic 0:380207fcb5c1 311 int err = lfs_rename(&_lfs, oldname, newname);
borlanic 0:380207fcb5c1 312 LFS_INFO("rename -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 313 _mutex.unlock();
borlanic 0:380207fcb5c1 314 return lfs_toerror(err);
borlanic 0:380207fcb5c1 315 }
borlanic 0:380207fcb5c1 316
borlanic 0:380207fcb5c1 317 int LittleFileSystem::mkdir(const char *name, mode_t mode)
borlanic 0:380207fcb5c1 318 {
borlanic 0:380207fcb5c1 319 _mutex.lock();
borlanic 0:380207fcb5c1 320 LFS_INFO("mkdir(\"%s\", 0x%lx)", name, mode);
borlanic 0:380207fcb5c1 321 int err = lfs_mkdir(&_lfs, name);
borlanic 0:380207fcb5c1 322 LFS_INFO("mkdir -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 323 _mutex.unlock();
borlanic 0:380207fcb5c1 324 return lfs_toerror(err);
borlanic 0:380207fcb5c1 325 }
borlanic 0:380207fcb5c1 326
borlanic 0:380207fcb5c1 327 int LittleFileSystem::stat(const char *name, struct stat *st)
borlanic 0:380207fcb5c1 328 {
borlanic 0:380207fcb5c1 329 struct lfs_info info;
borlanic 0:380207fcb5c1 330 _mutex.lock();
borlanic 0:380207fcb5c1 331 LFS_INFO("stat(\"%s\", %p)", name, st);
borlanic 0:380207fcb5c1 332 int err = lfs_stat(&_lfs, name, &info);
borlanic 0:380207fcb5c1 333 LFS_INFO("stat -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 334 _mutex.unlock();
borlanic 0:380207fcb5c1 335 st->st_size = info.size;
borlanic 0:380207fcb5c1 336 st->st_mode = lfs_tomode(info.type);
borlanic 0:380207fcb5c1 337 return lfs_toerror(err);
borlanic 0:380207fcb5c1 338 }
borlanic 0:380207fcb5c1 339
borlanic 0:380207fcb5c1 340 static int lfs_statvfs_count(void *p, lfs_block_t b)
borlanic 0:380207fcb5c1 341 {
borlanic 0:380207fcb5c1 342 *(lfs_size_t *)p += 1;
borlanic 0:380207fcb5c1 343 return 0;
borlanic 0:380207fcb5c1 344 }
borlanic 0:380207fcb5c1 345
borlanic 0:380207fcb5c1 346 int LittleFileSystem::statvfs(const char *name, struct statvfs *st)
borlanic 0:380207fcb5c1 347 {
borlanic 0:380207fcb5c1 348 memset(st, 0, sizeof(struct statvfs));
borlanic 0:380207fcb5c1 349
borlanic 0:380207fcb5c1 350 lfs_size_t in_use = 0;
borlanic 0:380207fcb5c1 351 _mutex.lock();
borlanic 0:380207fcb5c1 352 LFS_INFO("statvfs(\"%s\", %p)", name, st);
borlanic 0:380207fcb5c1 353 int err = lfs_traverse(&_lfs, lfs_statvfs_count, &in_use);
borlanic 0:380207fcb5c1 354 LFS_INFO("statvfs -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 355 _mutex.unlock();
borlanic 0:380207fcb5c1 356 if (err) {
borlanic 0:380207fcb5c1 357 return err;
borlanic 0:380207fcb5c1 358 }
borlanic 0:380207fcb5c1 359
borlanic 0:380207fcb5c1 360 st->f_bsize = _config.block_size;
borlanic 0:380207fcb5c1 361 st->f_frsize = _config.block_size;
borlanic 0:380207fcb5c1 362 st->f_blocks = _config.block_count;
borlanic 0:380207fcb5c1 363 st->f_bfree = _config.block_count - in_use;
borlanic 0:380207fcb5c1 364 st->f_bavail = _config.block_count - in_use;
borlanic 0:380207fcb5c1 365 st->f_namemax = LFS_NAME_MAX;
borlanic 0:380207fcb5c1 366 return 0;
borlanic 0:380207fcb5c1 367 }
borlanic 0:380207fcb5c1 368
borlanic 0:380207fcb5c1 369 ////// File operations //////
borlanic 0:380207fcb5c1 370 int LittleFileSystem::file_open(fs_file_t *file, const char *path, int flags)
borlanic 0:380207fcb5c1 371 {
borlanic 0:380207fcb5c1 372 lfs_file_t *f = new lfs_file_t;
borlanic 0:380207fcb5c1 373 _mutex.lock();
borlanic 0:380207fcb5c1 374 LFS_INFO("file_open(%p, \"%s\", 0x%x)", *file, path, flags);
borlanic 0:380207fcb5c1 375 int err = lfs_file_open(&_lfs, f, path, lfs_fromflags(flags));
borlanic 0:380207fcb5c1 376 LFS_INFO("file_open -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 377 _mutex.unlock();
borlanic 0:380207fcb5c1 378 if (!err) {
borlanic 0:380207fcb5c1 379 *file = f;
borlanic 0:380207fcb5c1 380 } else {
borlanic 0:380207fcb5c1 381 delete f;
borlanic 0:380207fcb5c1 382 }
borlanic 0:380207fcb5c1 383 return lfs_toerror(err);
borlanic 0:380207fcb5c1 384 }
borlanic 0:380207fcb5c1 385
borlanic 0:380207fcb5c1 386 int LittleFileSystem::file_close(fs_file_t file)
borlanic 0:380207fcb5c1 387 {
borlanic 0:380207fcb5c1 388 lfs_file_t *f = (lfs_file_t *)file;
borlanic 0:380207fcb5c1 389 _mutex.lock();
borlanic 0:380207fcb5c1 390 LFS_INFO("file_close(%p)", file);
borlanic 0:380207fcb5c1 391 int err = lfs_file_close(&_lfs, f);
borlanic 0:380207fcb5c1 392 LFS_INFO("file_close -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 393 _mutex.unlock();
borlanic 0:380207fcb5c1 394 delete f;
borlanic 0:380207fcb5c1 395 return lfs_toerror(err);
borlanic 0:380207fcb5c1 396 }
borlanic 0:380207fcb5c1 397
borlanic 0:380207fcb5c1 398 ssize_t LittleFileSystem::file_read(fs_file_t file, void *buffer, size_t len)
borlanic 0:380207fcb5c1 399 {
borlanic 0:380207fcb5c1 400 lfs_file_t *f = (lfs_file_t *)file;
borlanic 0:380207fcb5c1 401 _mutex.lock();
borlanic 0:380207fcb5c1 402 LFS_INFO("file_read(%p, %p, %d)", file, buffer, len);
borlanic 0:380207fcb5c1 403 lfs_ssize_t res = lfs_file_read(&_lfs, f, buffer, len);
borlanic 0:380207fcb5c1 404 LFS_INFO("file_read -> %d", lfs_toerror(res));
borlanic 0:380207fcb5c1 405 _mutex.unlock();
borlanic 0:380207fcb5c1 406 return lfs_toerror(res);
borlanic 0:380207fcb5c1 407 }
borlanic 0:380207fcb5c1 408
borlanic 0:380207fcb5c1 409 ssize_t LittleFileSystem::file_write(fs_file_t file, const void *buffer, size_t len)
borlanic 0:380207fcb5c1 410 {
borlanic 0:380207fcb5c1 411 lfs_file_t *f = (lfs_file_t *)file;
borlanic 0:380207fcb5c1 412 _mutex.lock();
borlanic 0:380207fcb5c1 413 LFS_INFO("file_write(%p, %p, %d)", file, buffer, len);
borlanic 0:380207fcb5c1 414 lfs_ssize_t res = lfs_file_write(&_lfs, f, buffer, len);
borlanic 0:380207fcb5c1 415 LFS_INFO("file_write -> %d", lfs_toerror(res));
borlanic 0:380207fcb5c1 416 _mutex.unlock();
borlanic 0:380207fcb5c1 417 return lfs_toerror(res);
borlanic 0:380207fcb5c1 418 }
borlanic 0:380207fcb5c1 419
borlanic 0:380207fcb5c1 420 int LittleFileSystem::file_sync(fs_file_t file)
borlanic 0:380207fcb5c1 421 {
borlanic 0:380207fcb5c1 422 lfs_file_t *f = (lfs_file_t *)file;
borlanic 0:380207fcb5c1 423 _mutex.lock();
borlanic 0:380207fcb5c1 424 LFS_INFO("file_sync(%p)", file);
borlanic 0:380207fcb5c1 425 int err = lfs_file_sync(&_lfs, f);
borlanic 0:380207fcb5c1 426 LFS_INFO("file_sync -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 427 _mutex.unlock();
borlanic 0:380207fcb5c1 428 return lfs_toerror(err);
borlanic 0:380207fcb5c1 429 }
borlanic 0:380207fcb5c1 430
borlanic 0:380207fcb5c1 431 off_t LittleFileSystem::file_seek(fs_file_t file, off_t offset, int whence)
borlanic 0:380207fcb5c1 432 {
borlanic 0:380207fcb5c1 433 lfs_file_t *f = (lfs_file_t *)file;
borlanic 0:380207fcb5c1 434 _mutex.lock();
borlanic 0:380207fcb5c1 435 LFS_INFO("file_seek(%p, %ld, %d)", file, offset, whence);
borlanic 0:380207fcb5c1 436 off_t res = lfs_file_seek(&_lfs, f, offset, lfs_fromwhence(whence));
borlanic 0:380207fcb5c1 437 LFS_INFO("file_seek -> %d", lfs_toerror(res));
borlanic 0:380207fcb5c1 438 _mutex.unlock();
borlanic 0:380207fcb5c1 439 return lfs_toerror(res);
borlanic 0:380207fcb5c1 440 }
borlanic 0:380207fcb5c1 441
borlanic 0:380207fcb5c1 442 off_t LittleFileSystem::file_tell(fs_file_t file)
borlanic 0:380207fcb5c1 443 {
borlanic 0:380207fcb5c1 444 lfs_file_t *f = (lfs_file_t *)file;
borlanic 0:380207fcb5c1 445 _mutex.lock();
borlanic 0:380207fcb5c1 446 LFS_INFO("file_tell(%p)", file);
borlanic 0:380207fcb5c1 447 off_t res = lfs_file_tell(&_lfs, f);
borlanic 0:380207fcb5c1 448 LFS_INFO("file_tell -> %d", lfs_toerror(res));
borlanic 0:380207fcb5c1 449 _mutex.unlock();
borlanic 0:380207fcb5c1 450 return lfs_toerror(res);
borlanic 0:380207fcb5c1 451 }
borlanic 0:380207fcb5c1 452
borlanic 0:380207fcb5c1 453 off_t LittleFileSystem::file_size(fs_file_t file)
borlanic 0:380207fcb5c1 454 {
borlanic 0:380207fcb5c1 455 lfs_file_t *f = (lfs_file_t *)file;
borlanic 0:380207fcb5c1 456 _mutex.lock();
borlanic 0:380207fcb5c1 457 LFS_INFO("file_size(%p)", file);
borlanic 0:380207fcb5c1 458 off_t res = lfs_file_size(&_lfs, f);
borlanic 0:380207fcb5c1 459 LFS_INFO("file_size -> %d", lfs_toerror(res));
borlanic 0:380207fcb5c1 460 _mutex.unlock();
borlanic 0:380207fcb5c1 461 return lfs_toerror(res);
borlanic 0:380207fcb5c1 462 }
borlanic 0:380207fcb5c1 463
borlanic 0:380207fcb5c1 464
borlanic 0:380207fcb5c1 465 ////// Dir operations //////
borlanic 0:380207fcb5c1 466 int LittleFileSystem::dir_open(fs_dir_t *dir, const char *path)
borlanic 0:380207fcb5c1 467 {
borlanic 0:380207fcb5c1 468 lfs_dir_t *d = new lfs_dir_t;
borlanic 0:380207fcb5c1 469 _mutex.lock();
borlanic 0:380207fcb5c1 470 LFS_INFO("dir_open(%p, \"%s\")", *dir, path);
borlanic 0:380207fcb5c1 471 int err = lfs_dir_open(&_lfs, d, path);
borlanic 0:380207fcb5c1 472 LFS_INFO("dir_open -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 473 _mutex.unlock();
borlanic 0:380207fcb5c1 474 if (!err) {
borlanic 0:380207fcb5c1 475 *dir = d;
borlanic 0:380207fcb5c1 476 } else {
borlanic 0:380207fcb5c1 477 delete d;
borlanic 0:380207fcb5c1 478 }
borlanic 0:380207fcb5c1 479 return lfs_toerror(err);
borlanic 0:380207fcb5c1 480 }
borlanic 0:380207fcb5c1 481
borlanic 0:380207fcb5c1 482 int LittleFileSystem::dir_close(fs_dir_t dir)
borlanic 0:380207fcb5c1 483 {
borlanic 0:380207fcb5c1 484 lfs_dir_t *d = (lfs_dir_t *)dir;
borlanic 0:380207fcb5c1 485 _mutex.lock();
borlanic 0:380207fcb5c1 486 LFS_INFO("dir_close(%p)", dir);
borlanic 0:380207fcb5c1 487 int err = lfs_dir_close(&_lfs, d);
borlanic 0:380207fcb5c1 488 LFS_INFO("dir_close -> %d", lfs_toerror(err));
borlanic 0:380207fcb5c1 489 _mutex.unlock();
borlanic 0:380207fcb5c1 490 delete d;
borlanic 0:380207fcb5c1 491 return lfs_toerror(err);
borlanic 0:380207fcb5c1 492 }
borlanic 0:380207fcb5c1 493
borlanic 0:380207fcb5c1 494 ssize_t LittleFileSystem::dir_read(fs_dir_t dir, struct dirent *ent)
borlanic 0:380207fcb5c1 495 {
borlanic 0:380207fcb5c1 496 lfs_dir_t *d = (lfs_dir_t *)dir;
borlanic 0:380207fcb5c1 497 struct lfs_info info;
borlanic 0:380207fcb5c1 498 _mutex.lock();
borlanic 0:380207fcb5c1 499 LFS_INFO("dir_read(%p, %p)", dir, ent);
borlanic 0:380207fcb5c1 500 int res = lfs_dir_read(&_lfs, d, &info);
borlanic 0:380207fcb5c1 501 LFS_INFO("dir_read -> %d", lfs_toerror(res));
borlanic 0:380207fcb5c1 502 _mutex.unlock();
borlanic 0:380207fcb5c1 503 if (res == 1) {
borlanic 0:380207fcb5c1 504 ent->d_type = lfs_totype(info.type);
borlanic 0:380207fcb5c1 505 strcpy(ent->d_name, info.name);
borlanic 0:380207fcb5c1 506 }
borlanic 0:380207fcb5c1 507 return lfs_toerror(res);
borlanic 0:380207fcb5c1 508 }
borlanic 0:380207fcb5c1 509
borlanic 0:380207fcb5c1 510 void LittleFileSystem::dir_seek(fs_dir_t dir, off_t offset)
borlanic 0:380207fcb5c1 511 {
borlanic 0:380207fcb5c1 512 lfs_dir_t *d = (lfs_dir_t *)dir;
borlanic 0:380207fcb5c1 513 _mutex.lock();
borlanic 0:380207fcb5c1 514 LFS_INFO("dir_seek(%p, %ld)", dir, offset);
borlanic 0:380207fcb5c1 515 lfs_dir_seek(&_lfs, d, offset);
borlanic 0:380207fcb5c1 516 LFS_INFO("dir_seek -> %s", "void");
borlanic 0:380207fcb5c1 517 _mutex.unlock();
borlanic 0:380207fcb5c1 518 }
borlanic 0:380207fcb5c1 519
borlanic 0:380207fcb5c1 520 off_t LittleFileSystem::dir_tell(fs_dir_t dir)
borlanic 0:380207fcb5c1 521 {
borlanic 0:380207fcb5c1 522 lfs_dir_t *d = (lfs_dir_t *)dir;
borlanic 0:380207fcb5c1 523 _mutex.lock();
borlanic 0:380207fcb5c1 524 LFS_INFO("dir_tell(%p)", dir);
borlanic 0:380207fcb5c1 525 lfs_soff_t res = lfs_dir_tell(&_lfs, d);
borlanic 0:380207fcb5c1 526 LFS_INFO("dir_tell -> %d", lfs_toerror(res));
borlanic 0:380207fcb5c1 527 _mutex.unlock();
borlanic 0:380207fcb5c1 528 return lfs_toerror(res);
borlanic 0:380207fcb5c1 529 }
borlanic 0:380207fcb5c1 530
borlanic 0:380207fcb5c1 531 void LittleFileSystem::dir_rewind(fs_dir_t dir)
borlanic 0:380207fcb5c1 532 {
borlanic 0:380207fcb5c1 533 lfs_dir_t *d = (lfs_dir_t *)dir;
borlanic 0:380207fcb5c1 534 _mutex.lock();
borlanic 0:380207fcb5c1 535 LFS_INFO("dir_rewind(%p)", dir);
borlanic 0:380207fcb5c1 536 lfs_dir_rewind(&_lfs, d);
borlanic 0:380207fcb5c1 537 LFS_INFO("dir_rewind -> %s", "void");
borlanic 0:380207fcb5c1 538 _mutex.unlock();
borlanic 0:380207fcb5c1 539 }
borlanic 0:380207fcb5c1 540