Littlefs: Enhancement: List a directory content

Created on 29 Dec 2017  路  2Comments  路  Source: littlefs-project/littlefs

I just implemented the littlefs and it would be great to have a function to list a directory with the following result:

  • type (file, dir)
  • name
  • size

Most helpful comment

Thanks @geky it works perfectly
I will continue the tests

All 2 comments

Hi @guillaumerems, glad to hear you were able to get littlefs working.

You can use the lfs_dir* functions to list a directory like this:

int lfs_ls(lfs_t *lfs, const char *path) {
    lfs_dir_t dir;
    int err = lfs_dir_open(lfs, &dir, path);
    if (err) {
        return err;
    }

    struct lfs_info info;
    while (true) {
        int res = lfs_dir_read(lfs, &dir, &info);
        if (res < 0) {
            return res;
        }

        if (res == 0) {
            break;
        }

        switch (info.type) {
            case LFS_TYPE_REG: printf("reg "); break;
            case LFS_TYPE_DIR: printf("dir "); break;
            default:           printf("?   "); break;
        }

        static const char *prefixes[] = {"", "K", "M", "G"};
        for (int i = sizeof(prefixes)/sizeof(prefixes[0])-1; i >= 0; i--) {
            if (info.size >= (1 << 10*i)-1) {
                printf("%*u%sB ", 4-(i != 0), info.size >> 10*i, prefixes[i]);
                break;
            }
        }

        printf("%s\n", info.name);
    }

    err = lfs_dir_close(lfs, &dir);
    if (err) {
        return err;
    }

    return 0;
}

lfs_ls(&lfs, "/");

However, there are many different ways to list a directory, so this function probably won't end up in this repo.

This repo's goal is to provide a simple low-level (posix-like) implementation of littlefs that contains all of the core functionality needed for a filesystem. User-friendly bash functions (such as ls, cp, recursive remove) are left up to higher-level APIs.

Thanks @geky it works perfectly
I will continue the tests

Was this page helpful?
0 / 5 - 0 ratings

Related issues

roceh picture roceh  路  8Comments

e107steved picture e107steved  路  9Comments

roceh picture roceh  路  6Comments

davidefer picture davidefer  路  7Comments

FreddyBZ picture FreddyBZ  路  3Comments