mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-01-17 05:32:45 +01:00
Merge pull request #7345 from OTAkeys/pr/spiffs_statvfs
pkg/spiffs: implement statvfs and improve doc
This commit is contained in:
commit
599ad3784c
@ -216,6 +216,33 @@ static int _rename(vfs_mount_t *mountp, const char *from_path, const char *to_pa
|
||||
return spiffs_err_to_errno(SPIFFS_rename(&fs_desc->fs, from_path, to_path));
|
||||
}
|
||||
|
||||
static int _statvfs(vfs_mount_t *mountp, const char *restrict path, struct statvfs *restrict buf)
|
||||
{
|
||||
(void)path;
|
||||
if (buf == NULL) {
|
||||
return -EFAULT;
|
||||
}
|
||||
spiffs_desc_t *fs_desc = mountp->private_data;
|
||||
memset(buf, 0, sizeof(*buf));
|
||||
|
||||
uint32_t total;
|
||||
uint32_t used;
|
||||
int32_t ret = SPIFFS_info(&fs_desc->fs, &total, &used);
|
||||
if (ret < 0) {
|
||||
return spiffs_err_to_errno(ret);
|
||||
}
|
||||
|
||||
buf->f_bsize = sizeof(uint8_t); /* block size */
|
||||
buf->f_frsize = sizeof(uint8_t); /* fundamental block size */
|
||||
buf->f_blocks = total; /* Blocks total */
|
||||
buf->f_bfree = total - used; /* Blocks free */
|
||||
buf->f_bavail = total - used; /* Blocks available to non-privileged processes */
|
||||
buf->f_flag = ST_NOSUID; /* File system flags */
|
||||
buf->f_namemax = SPIFFS_OBJ_NAME_LEN; /* Maximum file name length */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int _open(vfs_file_t *filp, const char *name, int flags, mode_t mode, const char *abs_path)
|
||||
{
|
||||
spiffs_desc_t *fs_desc = filp->mp->private_data;
|
||||
@ -465,6 +492,7 @@ static const vfs_file_system_ops_t spiffs_fs_ops = {
|
||||
.umount = _umount,
|
||||
.unlink = _unlink,
|
||||
.rename = _rename,
|
||||
.statvfs = _statvfs,
|
||||
};
|
||||
|
||||
static const vfs_file_ops_t spiffs_file_ops = {
|
||||
|
@ -14,6 +14,25 @@
|
||||
* The RIOT integration of SPIFFS follows the SPIFFS wiki:
|
||||
* https://github.com/pellepl/spiffs/wiki/Integrate-spiffs#integrating-spiffs
|
||||
*
|
||||
* The RIOT integration uses by default the @p SPIFFS_HAL_CALLBACK_EXTRA option
|
||||
* and needs a @p mtd device, passed as spiffs_desc_t::dev.
|
||||
*
|
||||
* If one disable @p SPIFFS_HAL_CALLBACK_EXTRA, the mtd device is passed through
|
||||
* the @p SPIFFS_MTD_DEV macro as a @p mtd_dev_t pointer.
|
||||
*
|
||||
* Note that only one filesystem can be used if @p SPIFFS_HAL_CALLBACK_EXTRA is
|
||||
* disabled.
|
||||
*
|
||||
* @p SPIFFS_SINGLETON is disabled by default, the memory layout is retrieved from
|
||||
* the @p mtd device used. If @p SPIFFS_SINGLETON is enabled, the proper SPIFFS
|
||||
* variables must be set (see spiffs_config.h from SPIFFS).
|
||||
*
|
||||
* The default integration enable @p SPIFFS_CACHE and uses the macro
|
||||
* @p SPIFFS_FS_CACHE_SIZE as cache size.
|
||||
*
|
||||
* @p SPIFFS_LOCK and @p SPIFFS_UNLOCK are also defined in the RIOT custom
|
||||
* spiffs_config.h to use @p spiffs_lock() and @p spiffs_unlock()
|
||||
*
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
@ -102,6 +121,26 @@ typedef struct spiffs_desc {
|
||||
/** The SPIFFS vfs driver, a pointer to a spiffs_desc_t must be provided as vfs_mountp::private_data */
|
||||
extern const vfs_file_system_t spiffs_file_system;
|
||||
|
||||
/**
|
||||
* @brief SPIFFS lock function
|
||||
*
|
||||
* This function must be used by @p SPIFFS_LOCK to lock the file system using
|
||||
* the spiffs_desc_t::lock.
|
||||
*
|
||||
* @param fs spiffs descriptor
|
||||
*/
|
||||
void spiffs_lock(struct spiffs_t *fs);
|
||||
|
||||
/**
|
||||
* @brief SPIFFS unlock function
|
||||
*
|
||||
* This function must be used by @p SPIFFS_UNLOCK to lock the file system using
|
||||
* the spiffs_desc_t::lock.
|
||||
*
|
||||
* @param fs spiffs descriptor
|
||||
*/
|
||||
void spiffs_unlock(struct spiffs_t *fs);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -326,6 +326,37 @@ static void tests_spiffs_rename(void)
|
||||
TEST_ASSERT_EQUAL_INT(0, res);
|
||||
}
|
||||
|
||||
static void tests_spiffs_statvfs(void)
|
||||
{
|
||||
const char buf[] = "TESTSTRING";
|
||||
struct statvfs stat1;
|
||||
struct statvfs stat2;
|
||||
|
||||
int res = vfs_statvfs("/test-spiffs/", &stat1);
|
||||
TEST_ASSERT_EQUAL_INT(0, res);
|
||||
TEST_ASSERT_EQUAL_INT(1, stat1.f_bsize);
|
||||
TEST_ASSERT_EQUAL_INT(1, stat1.f_frsize);
|
||||
TEST_ASSERT((_dev->pages_per_sector * _dev->page_size * _dev->sector_count) >=
|
||||
stat1.f_blocks);
|
||||
|
||||
int fd = vfs_open("/test-spiffs/test.txt", O_CREAT | O_RDWR, 0);
|
||||
TEST_ASSERT(fd >= 0);
|
||||
|
||||
res = vfs_write(fd, buf, sizeof(buf));
|
||||
TEST_ASSERT(res == sizeof(buf));
|
||||
|
||||
res = vfs_close(fd);
|
||||
TEST_ASSERT_EQUAL_INT(0, res);
|
||||
|
||||
res = vfs_statvfs("/test-spiffs/", &stat2);
|
||||
TEST_ASSERT_EQUAL_INT(0, res);
|
||||
|
||||
TEST_ASSERT_EQUAL_INT(1, stat2.f_bsize);
|
||||
TEST_ASSERT_EQUAL_INT(1, stat2.f_frsize);
|
||||
TEST_ASSERT(sizeof(buf) <= (stat1.f_bfree - stat2.f_bfree));
|
||||
TEST_ASSERT(sizeof(buf) <= (stat1.f_bavail - stat2.f_bavail));
|
||||
}
|
||||
|
||||
Test *tests_spiffs_tests(void)
|
||||
{
|
||||
EMB_UNIT_TESTFIXTURES(fixtures) {
|
||||
@ -335,6 +366,7 @@ Test *tests_spiffs_tests(void)
|
||||
new_TestFixture(tests_spiffs_unlink),
|
||||
new_TestFixture(tests_spiffs_readdir),
|
||||
new_TestFixture(tests_spiffs_rename),
|
||||
new_TestFixture(tests_spiffs_statvfs),
|
||||
};
|
||||
|
||||
EMB_UNIT_TESTCALLER(spiffs_tests, test_spiffs_setup, test_spiffs_teardown, fixtures);
|
||||
|
Loading…
Reference in New Issue
Block a user