Skip to content

Commit 9dd3060

Browse files
committed
Added littlefs statvfs implementation
1 parent f1a9815 commit 9dd3060

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

features/filesystem/littlefs/LittleFileSystem.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,33 @@ int LittleFileSystem::stat(const char *name, struct stat *st)
336336
return lfs_toerror(err);
337337
}
338338

339-
int LittleFileSystem::statvfs(const char *path, struct statvfs *buf)
339+
static int lfs_statvfs_count(void *p, lfs_block_t b)
340340
{
341+
*(lfs_size_t *)p += 1;
342+
return 0;
343+
}
344+
345+
int LittleFileSystem::statvfs(const char *name, struct statvfs *st)
346+
{
347+
memset(st, 0, sizeof(struct statvfs));
348+
349+
lfs_size_t in_use = 0;
350+
_mutex.lock();
351+
LFS_INFO("statvfs(\"%s\", %p)", name, st);
352+
int err = lfs_traverse(&_lfs, lfs_statvfs_count, &in_use);
353+
LFS_INFO("statvfs -> %d", lfs_toerror(err));
354+
_mutex.unlock();
355+
if (err) {
356+
return err;
357+
}
358+
359+
st->f_bsize = _config.block_size;
360+
st->f_frsize = _config.block_size;
361+
st->f_blocks = _config.block_count;
362+
st->f_bfree = _config.block_count - in_use;
363+
st->f_bavail = _config.block_count - in_use;
364+
st->f_namemax = LFS_NAME_MAX;
365+
return 0;
341366
}
342367

343368
////// File operations //////

features/filesystem/littlefs/LittleFileSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class LittleFileSystem : public mbed::FileSystem {
149149
* @param buf The stat buffer to write to
150150
* @return 0 on success, negative error code on failure
151151
*/
152-
virtual int statvfs(const char *path, struct statvfs *buf);
152+
virtual int statvfs(const char *path, struct statvfs *buf);
153153

154154
protected:
155155
/** Open a file on the filesystem

0 commit comments

Comments
 (0)