Skip to content

Commit

Permalink
littlefs: fix coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
0xc0170 committed Nov 8, 2018
1 parent bc4101e commit 20646d3
Show file tree
Hide file tree
Showing 14 changed files with 305 additions and 279 deletions.
126 changes: 76 additions & 50 deletions features/storage/filesystem/littlefs/LittleFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ using namespace mbed;

extern "C" void lfs_crc(uint32_t *crc, const void *buffer, size_t size)
{
uint32_t initial_xor = *crc;
uint32_t initial_xor = *crc;
MbedCRC<POLY_32BIT_REV_ANSI, 32> ct(initial_xor, 0x0, true, false);
ct.compute((void *)buffer, size, (uint32_t *) crc);
}
Expand All @@ -33,79 +33,102 @@ extern "C" void lfs_crc(uint32_t *crc, const void *buffer, size_t size)
static int lfs_toerror(int err)
{
switch (err) {
case LFS_ERR_OK: return 0;
case LFS_ERR_IO: return -EIO;
case LFS_ERR_NOENT: return -ENOENT;
case LFS_ERR_EXIST: return -EEXIST;
case LFS_ERR_NOTDIR: return -ENOTDIR;
case LFS_ERR_ISDIR: return -EISDIR;
case LFS_ERR_INVAL: return -EINVAL;
case LFS_ERR_NOSPC: return -ENOSPC;
case LFS_ERR_NOMEM: return -ENOMEM;
case LFS_ERR_CORRUPT: return -EILSEQ;
default: return err;
case LFS_ERR_OK:
return 0;
case LFS_ERR_IO:
return -EIO;
case LFS_ERR_NOENT:
return -ENOENT;
case LFS_ERR_EXIST:
return -EEXIST;
case LFS_ERR_NOTDIR:
return -ENOTDIR;
case LFS_ERR_ISDIR:
return -EISDIR;
case LFS_ERR_INVAL:
return -EINVAL;
case LFS_ERR_NOSPC:
return -ENOSPC;
case LFS_ERR_NOMEM:
return -ENOMEM;
case LFS_ERR_CORRUPT:
return -EILSEQ;
default:
return err;
}
}

static int lfs_fromflags(int flags)
{
return (
(((flags & 3) == O_RDONLY) ? LFS_O_RDONLY : 0) |
(((flags & 3) == O_WRONLY) ? LFS_O_WRONLY : 0) |
(((flags & 3) == O_RDWR) ? LFS_O_RDWR : 0) |
((flags & O_CREAT) ? LFS_O_CREAT : 0) |
((flags & O_EXCL) ? LFS_O_EXCL : 0) |
((flags & O_TRUNC) ? LFS_O_TRUNC : 0) |
((flags & O_APPEND) ? LFS_O_APPEND : 0));
(((flags & 3) == O_RDONLY) ? LFS_O_RDONLY : 0) |
(((flags & 3) == O_WRONLY) ? LFS_O_WRONLY : 0) |
(((flags & 3) == O_RDWR) ? LFS_O_RDWR : 0) |
((flags & O_CREAT) ? LFS_O_CREAT : 0) |
((flags & O_EXCL) ? LFS_O_EXCL : 0) |
((flags & O_TRUNC) ? LFS_O_TRUNC : 0) |
((flags & O_APPEND) ? LFS_O_APPEND : 0));
}

static int lfs_fromwhence(int whence)
{
switch (whence) {
case SEEK_SET: return LFS_SEEK_SET;
case SEEK_CUR: return LFS_SEEK_CUR;
case SEEK_END: return LFS_SEEK_END;
default: return whence;
case SEEK_SET:
return LFS_SEEK_SET;
case SEEK_CUR:
return LFS_SEEK_CUR;
case SEEK_END:
return LFS_SEEK_END;
default:
return whence;
}
}

static int lfs_tomode(int type)
{
int mode = S_IRWXU | S_IRWXG | S_IRWXO;
switch (type) {
case LFS_TYPE_DIR: return mode | S_IFDIR;
case LFS_TYPE_REG: return mode | S_IFREG;
default: return 0;
case LFS_TYPE_DIR:
return mode | S_IFDIR;
case LFS_TYPE_REG:
return mode | S_IFREG;
default:
return 0;
}
}

static int lfs_totype(int type)
{
switch (type) {
case LFS_TYPE_DIR: return DT_DIR;
case LFS_TYPE_REG: return DT_REG;
default: return DT_UNKNOWN;
case LFS_TYPE_DIR:
return DT_DIR;
case LFS_TYPE_REG:
return DT_REG;
default:
return DT_UNKNOWN;
}
}


////// Block device operations //////
static int lfs_bd_read(const struct lfs_config *c, lfs_block_t block,
lfs_off_t off, void *buffer, lfs_size_t size) {
lfs_off_t off, void *buffer, lfs_size_t size)
{
BlockDevice *bd = (BlockDevice *)c->context;
return bd->read(buffer, (bd_addr_t)block*c->block_size + off, size);
return bd->read(buffer, (bd_addr_t)block * c->block_size + off, size);
}

static int lfs_bd_prog(const struct lfs_config *c, lfs_block_t block,
lfs_off_t off, const void *buffer, lfs_size_t size) {
lfs_off_t off, const void *buffer, lfs_size_t size)
{
BlockDevice *bd = (BlockDevice *)c->context;
return bd->program(buffer, (bd_addr_t)block*c->block_size + off, size);
return bd->program(buffer, (bd_addr_t)block * c->block_size + off, size);
}

static int lfs_bd_erase(const struct lfs_config *c, lfs_block_t block)
{
BlockDevice *bd = (BlockDevice *)c->context;
return bd->erase((bd_addr_t)block*c->block_size, c->block_size);
return bd->erase((bd_addr_t)block * c->block_size, c->block_size);
}

static int lfs_bd_sync(const struct lfs_config *c)
Expand All @@ -119,19 +142,21 @@ static int lfs_bd_sync(const struct lfs_config *c)

// Filesystem implementation (See LittleFileSystem.h)
LittleFileSystem::LittleFileSystem(const char *name, BlockDevice *bd,
lfs_size_t read_size, lfs_size_t prog_size,
lfs_size_t block_size, lfs_size_t lookahead)
: FileSystem(name)
, _read_size(read_size)
, _prog_size(prog_size)
, _block_size(block_size)
, _lookahead(lookahead) {
lfs_size_t read_size, lfs_size_t prog_size,
lfs_size_t block_size, lfs_size_t lookahead)
: FileSystem(name)
, _read_size(read_size)
, _prog_size(prog_size)
, _block_size(block_size)
, _lookahead(lookahead)
{
if (bd) {
mount(bd);
}
}

LittleFileSystem::~LittleFileSystem() {
LittleFileSystem::~LittleFileSystem()
{
// nop if unmounted
unmount();
}
Expand Down Expand Up @@ -168,7 +193,7 @@ int LittleFileSystem::mount(BlockDevice *bd)
_config.block_size = _block_size;
}
_config.block_count = bd->size() / _config.block_size;
_config.lookahead = 32 * ((_config.block_count+31)/32);
_config.lookahead = 32 * ((_config.block_count + 31) / 32);
if (_config.lookahead > _lookahead) {
_config.lookahead = _lookahead;
}
Expand Down Expand Up @@ -204,17 +229,18 @@ int LittleFileSystem::unmount()

_bd = NULL;
}

LFS_INFO("unmount -> %d", res);
_mutex.unlock();
return res;
}

int LittleFileSystem::format(BlockDevice *bd,
lfs_size_t read_size, lfs_size_t prog_size,
lfs_size_t block_size, lfs_size_t lookahead) {
lfs_size_t read_size, lfs_size_t prog_size,
lfs_size_t block_size, lfs_size_t lookahead)
{
LFS_INFO("format(%p, %ld, %ld, %ld, %ld)",
bd, read_size, prog_size, block_size, lookahead);
bd, read_size, prog_size, block_size, lookahead);
int err = bd->init();
if (err) {
LFS_INFO("format -> %d", err);
Expand All @@ -223,7 +249,7 @@ int LittleFileSystem::format(BlockDevice *bd,

lfs_t _lfs;
struct lfs_config _config;

memset(&_config, 0, sizeof(_config));
_config.context = bd;
_config.read = lfs_bd_read;
Expand All @@ -243,7 +269,7 @@ int LittleFileSystem::format(BlockDevice *bd,
_config.block_size = block_size;
}
_config.block_count = bd->size() / _config.block_size;
_config.lookahead = 32 * ((_config.block_count+31)/32);
_config.lookahead = 32 * ((_config.block_count + 31) / 32);
if (_config.lookahead > lookahead) {
_config.lookahead = lookahead;
}
Expand Down Expand Up @@ -288,7 +314,7 @@ int LittleFileSystem::reformat(BlockDevice *bd)
}

int err = LittleFileSystem::format(bd,
_read_size, _prog_size, _block_size, _lookahead);
_read_size, _prog_size, _block_size, _lookahead);
if (err) {
LFS_INFO("reformat -> %d", err);
_mutex.unlock();
Expand Down
24 changes: 12 additions & 12 deletions features/storage/filesystem/littlefs/LittleFileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ class LittleFileSystem : public mbed::FileSystem {
* The lookahead buffer requires only 1 bit per block so it can be quite
* large with little ram impact. Should be a multiple of 32.
*/
LittleFileSystem(const char *name=NULL, BlockDevice *bd=NULL,
lfs_size_t read_size=MBED_LFS_READ_SIZE,
lfs_size_t prog_size=MBED_LFS_PROG_SIZE,
lfs_size_t block_size=MBED_LFS_BLOCK_SIZE,
lfs_size_t lookahead=MBED_LFS_LOOKAHEAD);
LittleFileSystem(const char *name = NULL, BlockDevice *bd = NULL,
lfs_size_t read_size = MBED_LFS_READ_SIZE,
lfs_size_t prog_size = MBED_LFS_PROG_SIZE,
lfs_size_t block_size = MBED_LFS_BLOCK_SIZE,
lfs_size_t lookahead = MBED_LFS_LOOKAHEAD);
virtual ~LittleFileSystem();

/** Formats a block device with the LittleFileSystem
*
* The block device to format should be mounted when this function is called.
Expand All @@ -82,10 +82,10 @@ class LittleFileSystem : public mbed::FileSystem {
* large with little ram impact. Should be a multiple of 32.
*/
static int format(BlockDevice *bd,
lfs_size_t read_size=MBED_LFS_READ_SIZE,
lfs_size_t prog_size=MBED_LFS_PROG_SIZE,
lfs_size_t block_size=MBED_LFS_BLOCK_SIZE,
lfs_size_t lookahead=MBED_LFS_LOOKAHEAD);
lfs_size_t read_size = MBED_LFS_READ_SIZE,
lfs_size_t prog_size = MBED_LFS_PROG_SIZE,
lfs_size_t block_size = MBED_LFS_BLOCK_SIZE,
lfs_size_t lookahead = MBED_LFS_LOOKAHEAD);

/** Mounts a filesystem to a block device
*
Expand Down Expand Up @@ -182,7 +182,7 @@ class LittleFileSystem : public mbed::FileSystem {
*
* @param file File handle
* @param buffer The buffer to write from
* @param size The number of bytes to write
* @param size The number of bytes to write
* @return The number of bytes written, negative error on failure
*/
virtual ssize_t file_write(mbed::fs_file_t file, const void *buffer, size_t size);
Expand Down Expand Up @@ -263,7 +263,7 @@ class LittleFileSystem : public mbed::FileSystem {
* @param dir Dir handle
*/
virtual void dir_rewind(mbed::fs_dir_t dir);

private:
lfs_t _lfs; // _the actual filesystem
struct lfs_config _config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ static int file_scanf(File *file, const char *format, ...)
int res = file->read(buf, sizeof(buf) - 1);
TEST_ASSERT_OR_EXIT(res >= 0);

va_start (args, format);
int count = vsscanf((char*)buf, format, args);
va_end (args);
va_start(args, format);
int count = vsscanf((char *)buf, format, args);
va_end(args);
TEST_ASSERT_OR_EXIT(count >= 0);

return count;
Expand All @@ -176,9 +176,9 @@ static int file_printf(File *file, const char *format, ...)
{
uint8_t buf[BUFFER_SIZE];
va_list args;
va_start (args, format);
int size = vsprintf((char*)buf, format, args);
va_end (args);
va_start(args, format);
int size = vsprintf((char *)buf, format, args);
va_end(args);
TEST_ASSERT_OR_EXIT((size >= 0) && (size <= (int)sizeof(buf)));

if (file_write(file, buf, size)) {
Expand Down Expand Up @@ -254,7 +254,7 @@ static void check_file_rename(FileSystem *fs)

int files = 0;
int valids = 0;
const char * const filenames[] = {FILE_RENAME_A, FILE_RENAME_B};
const char *const filenames[] = {FILE_RENAME_A, FILE_RENAME_B};

for (int i = 0; i < 2; i++) {
File file;
Expand Down Expand Up @@ -300,7 +300,7 @@ static void setup_file_rename_replace(FileSystem *fs)
uint32_t count = 0;
uint8_t buf[BUFFER_SIZE];
memset(buf, 0, sizeof(buf));
const int length = sprintf((char*)buf, FILE_RENAME_REPLACE_FMT, count);
const int length = sprintf((char *)buf, FILE_RENAME_REPLACE_FMT, count);
TEST_ASSERT_OR_EXIT(length > 0);

res = file.write(buf, length);
Expand Down Expand Up @@ -602,7 +602,7 @@ static bool format_required(BlockDevice *bd)

// Get the test version
uint32_t version = 0;
res = sscanf((char*)buf, FILE_SETUP_COMPLETE_FMT, &version);
res = sscanf((char *)buf, FILE_SETUP_COMPLETE_FMT, &version);
if (res != 1) {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ void test_multi_block_directory()
res = fs.mkdir("cactus", 0777);
TEST_ASSERT_EQUAL(0, res);
for (int i = 0; i < 128; i++) {
sprintf((char*)buffer, "cactus/test%d", i);
res = fs.mkdir((char*)buffer, 0777);
sprintf((char *)buffer, "cactus/test%d", i);
res = fs.mkdir((char *)buffer, 0777);
TEST_ASSERT_EQUAL(0, res);
}
res = fs.unmount();
Expand All @@ -326,10 +326,10 @@ void test_multi_block_directory()
res = ent.d_type;
TEST_ASSERT_EQUAL(DT_DIR, res);
for (int i = 0; i < 128; i++) {
sprintf((char*)buffer, "test%d", i);
sprintf((char *)buffer, "test%d", i);
res = dir[0].read(&ent);
TEST_ASSERT_EQUAL(1, res);
res = strcmp(ent.d_name, (char*)buffer);
res = strcmp(ent.d_name, (char *)buffer);
TEST_ASSERT_EQUAL(0, res);
}
res = dir[0].read(&ent);
Expand Down
Loading

0 comments on commit 20646d3

Please sign in to comment.