Add a generic implementation of 'ls' using opendir/readdir/closedir, and
replace fat's custom implementation.  Other filesystems should move to
the generic implementation after they add opendir/readdir/closedir
support.

Signed-off-by: Rob Clark <robdcl...@gmail.com>
---
 fs/fat/fat.c  | 32 --------------------------------
 fs/fs.c       | 35 +++++++++++++++++++++++++++++++++--
 include/fat.h |  5 ++++-
 3 files changed, 37 insertions(+), 35 deletions(-)

diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index fe5819315b..08a066d80d 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -1002,38 +1002,6 @@ int file_fat_detectfs(void)
        return 0;
 }
 
-int file_fat_ls(const char *dir)
-{
-       fsdata fsdata;
-       fat_itr itrblock, *itr = &itrblock;
-       int files = 0, dirs = 0;
-       int ret;
-
-       ret = fat_itr_root(itr, &fsdata);
-       if (ret)
-               return ret;
-
-       ret = fat_itr_resolve(itr, dir, TYPE_DIR);
-       if (ret)
-               return ret;
-
-       while (fat_itr_next(itr)) {
-               if (fat_itr_isdir(itr)) {
-                       printf("            %s/\n", itr->name);
-                       dirs++;
-               } else {
-                       printf(" %8u   %s\n",
-                              FAT2CPU32(itr->dent->size),
-                              itr->name);
-                       files++;
-               }
-       }
-
-       printf("\n%d file(s), %d dir(s)\n\n", files, dirs);
-
-       return 0;
-}
-
 int fat_exists(const char *filename)
 {
        fsdata fsdata;
diff --git a/fs/fs.c b/fs/fs.c
index 1b4f41920d..374f319161 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -37,6 +37,35 @@ static inline int fs_ls_unsupported(const char *dirname)
        return -1;
 }
 
+/* generic implementation of ls in terms of opendir/readdir/closedir */
+__maybe_unused
+static int fs_ls_generic(const char *dirname)
+{
+       FS_DIR *dirp;
+       struct fs_dirent *dent;
+       int files = 0, dirs = 0;
+
+       dirp = fs_opendir(dirname);
+       if (!dirp)
+               return -errno;
+
+       while ((dent = fs_readdir(dirp))) {
+               if (dent->type == FS_DT_DIR) {
+                       printf("            %s/\n", dent->name);
+                       dirs++;
+               } else {
+                       printf(" %8lld   %s\n", dent->size, dent->name);
+                       files++;
+               }
+       }
+
+       fs_closedir(dirp);
+
+       printf("\n%d file(s), %d dir(s)\n\n", files, dirs);
+
+       return 0;
+}
+
 static inline int fs_exists_unsupported(const char *filename)
 {
        return 0;
@@ -111,7 +140,7 @@ static struct fstype_info fstypes[] = {
                .null_dev_desc_ok = false,
                .probe = fat_set_blk_dev,
                .close = fat_close,
-               .ls = file_fat_ls,
+               .ls = fs_ls_generic,
                .exists = fat_exists,
                .size = fat_size,
                .read = fat_read_file,
@@ -121,7 +150,9 @@ static struct fstype_info fstypes[] = {
                .write = fs_write_unsupported,
 #endif
                .uuid = fs_uuid_unsupported,
-               .opendir = fs_opendir_unsupported,
+               .opendir = fat_opendir,
+               .readdir = fat_readdir,
+               .closedir = fat_closedir,
        },
 #endif
 #ifdef CONFIG_FS_EXT4
diff --git a/include/fat.h b/include/fat.h
index 1e8bc44e9a..b2d4b952fd 100644
--- a/include/fat.h
+++ b/include/fat.h
@@ -11,6 +11,7 @@
 #define _FAT_H_
 
 #include <asm/byteorder.h>
+#include <fs.h>
 
 #define CONFIG_SUPPORT_VFAT
 /* Maximum Long File Name length supported here is 128 UTF-16 code units */
@@ -172,7 +173,6 @@ typedef struct {
 } fsdata;
 
 int file_fat_detectfs(void);
-int file_fat_ls(const char *dir);
 int fat_exists(const char *filename);
 int fat_size(const char *filename, loff_t *size);
 int file_fat_read_at(const char *filename, loff_t pos, void *buffer,
@@ -185,5 +185,8 @@ int file_fat_write(const char *filename, void *buf, loff_t 
offset, loff_t len,
                   loff_t *actwrite);
 int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
                  loff_t *actread);
+int fat_opendir(const char *filename, FS_DIR **dirp);
+int fat_readdir(FS_DIR *dirp);
+void fat_closedir(FS_DIR *dirp);
 void fat_close(void);
 #endif /* _FAT_H_ */
-- 
2.13.0

_______________________________________________
U-Boot mailing list
U-Boot@lists.denx.de
https://lists.denx.de/listinfo/u-boot

Reply via email to