littlefs icon indicating copy to clipboard operation
littlefs copied to clipboard

file list

Open WanCooder opened this issue 3 years ago • 3 comments

How to read the file list?

WanCooder avatar Mar 16 '21 13:03 WanCooder

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;
}

geky avatar Mar 23 '21 06:03 geky

    int res = lfs_dir_read(&lfs, &dir, &info);
    if (res < 0) {
        return err;
   }

Can I leave the directory opened here?

hagibr avatar Apr 14 '22 17:04 hagibr

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.

geky avatar Apr 15 '22 02:04 geky