1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

pkg/littlefs: add format implementation

This commit is contained in:
Vincent Dupont 2018-01-02 18:36:10 +01:00
parent 6c2d7ecac6
commit 5ede5db234
2 changed files with 56 additions and 13 deletions

View File

@ -123,14 +123,12 @@ static int _dev_sync(const struct lfs_config *c)
return 0;
}
static int _mount(vfs_mount_t *mountp)
static int prepare(littlefs_desc_t *fs)
{
littlefs_desc_t *fs = mountp->private_data;
mutex_init(&fs->lock);
mutex_lock(&fs->lock);
DEBUG("littlefs: mount: mountp=%p\n", (void *)mountp);
memset(&fs->fs, 0, sizeof(fs->fs));
if (!fs->config.block_count) {
fs->config.block_count = fs->dev->sector_count - fs->base_addr;
@ -161,18 +159,36 @@ static int _mount(vfs_mount_t *mountp)
fs->config.prog_buffer = fs->prog_buf;
#endif
mtd_init(fs->dev);
return mtd_init(fs->dev);
}
int ret = lfs_mount(&fs->fs, &fs->config);
if (ret < 0) {
DEBUG("littlefs: formatting\n");
ret = lfs_format(&fs->fs, &fs->config);
if (ret >= 0) {
DEBUG("littlefs: mounting\n");
ret = lfs_mount(&fs->fs, &fs->config);
}
static int _format(vfs_mount_t *mountp)
{
littlefs_desc_t *fs = mountp->private_data;
DEBUG("littlefs: format: mountp=%p\n", (void *)mountp);
int ret = prepare(fs);
if (ret) {
return -ENODEV;
}
ret = lfs_format(&fs->fs, &fs->config);
mutex_unlock(&fs->lock);
return littlefs_err_to_errno(ret);
}
static int _mount(vfs_mount_t *mountp)
{
littlefs_desc_t *fs = mountp->private_data;
DEBUG("littlefs: mount: mountp=%p\n", (void *)mountp);
int ret = prepare(fs);
if (ret) {
return -ENODEV;
}
ret = lfs_mount(&fs->fs, &fs->config);
mutex_unlock(&fs->lock);
return littlefs_err_to_errno(ret);
@ -474,6 +490,7 @@ static int _closedir(vfs_DIR *dirp)
}
static const vfs_file_system_ops_t littlefs_fs_ops = {
.format = _format,
.mount = _mount,
.umount = _umount,
.unlink = _unlink,

View File

@ -144,6 +144,31 @@ static void test_littlefs_teardown(void)
vfs_umount(&_test_littlefs_mount);
}
static void tests_littlefs_format(void)
{
int res;
vfs_umount(&_test_littlefs_mount);
res = mtd_erase(_dev, 0, _dev->page_size * _dev->pages_per_sector * _dev->sector_count);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_mount(&_test_littlefs_mount);
TEST_ASSERT(res < 0);
/* 1. format an invalid file system (failed mount) */
res = vfs_format(&_test_littlefs_mount);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_mount(&_test_littlefs_mount);
TEST_ASSERT_EQUAL_INT(0, res);
res = vfs_umount(&_test_littlefs_mount);
TEST_ASSERT_EQUAL_INT(0, res);
/* 2. format a valid file system */
res = vfs_format(&_test_littlefs_mount);
TEST_ASSERT_EQUAL_INT(0, res);
}
static void tests_littlefs_mount_umount(void)
{
int res;
@ -383,6 +408,7 @@ static void tests_littlefs_statvfs(void)
Test *tests_littlefs_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(tests_littlefs_format),
new_TestFixture(tests_littlefs_mount_umount),
new_TestFixture(tests_littlefs_open_close),
new_TestFixture(tests_littlefs_write),