littlefs
littlefs copied to clipboard
file list
How to read the file list?
Hello, you can read the files with lfs_dir_read
, it behaves like readdir:
lfs_dir_t dir;
struct lfs_info info;
int err = lfs_dir_open(&lfs, &dir, "/");
if (err) {
return err;
}
while (true) {
int res = lfs_dir_read(&lfs, &dir, &info);
if (res < 0) {
lfs_dir_close(&lfs, &dir);
return err;
}
if (!res) {
break;
}
printf("%s %d", info.name, info.type);
}
err = lfs_dir_close(&lfs, &dir);
if (err) {
return err;
}
int res = lfs_dir_read(&lfs, &dir, &info);
if (res < 0) {
return err;
}
Can I leave the directory opened here?
Hi @alexandrehagihara, good catch. We should make sure to release any resources if there is an error.
I've updated the example in the comment above.
Not closing the directory would have only been acceptable if the device halted afterwards.