diff mbox series

[U-Boot,v3,5/9] fs/fat: implement opendir/readdir/closedir

Message ID 20170909171606.20029-6-robdclark@gmail.com
State Accepted
Delegated to: Tom Rini
Headers show
Series fs/fat: cleanups + readdir implementation | expand

Commit Message

Rob Clark Sept. 9, 2017, 5:15 p.m. UTC
Implement the readdir interface using the directory iterators.

Signed-off-by: Rob Clark <robdclark@gmail.com>
Reviewed-by: Łukasz Majewski <lukma@denx.de>
---
 fs/fat/fat.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

Comments

Simon Glass Sept. 12, 2017, 12:29 p.m. UTC | #1
On 9 September 2017 at 11:15, Rob Clark <robdclark@gmail.com> wrote:
> Implement the readdir interface using the directory iterators.
>
> Signed-off-by: Rob Clark <robdclark@gmail.com>
> Reviewed-by: Łukasz Majewski <lukma@denx.de>
> ---
>  fs/fat/fat.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 61 insertions(+)

Reviewed-by: Simon Glass <sjg@chromium.org>

But I still feel unconvinced that you need to add the typedef.
Tom Rini Sept. 16, 2017, 2:32 a.m. UTC | #2
On Sat, Sep 09, 2017 at 01:15:56PM -0400, Rob Clark wrote:

> Implement the readdir interface using the directory iterators.
> 
> Signed-off-by: Rob Clark <robdclark@gmail.com>
> Reviewed-by: Łukasz Majewski <lukma@denx.de>
> Reviewed-by: Simon Glass <sjg@chromium.org>

Applied to u-boot/master, thanks!
diff mbox series

Patch

diff --git a/fs/fat/fat.c b/fs/fat/fat.c
index bbba7947ee..82ddb7eab1 100644
--- a/fs/fat/fat.c
+++ b/fs/fat/fat.c
@@ -14,6 +14,7 @@ 
 #include <config.h>
 #include <exports.h>
 #include <fat.h>
+#include <fs.h>
 #include <asm/byteorder.h>
 #include <part.h>
 #include <malloc.h>
@@ -1146,6 +1147,66 @@  int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
 	return ret;
 }
 
+typedef struct {
+	struct fs_dir_stream parent;
+	struct fs_dirent dirent;
+	fsdata fsdata;
+	fat_itr itr;
+} fat_dir;
+
+int fat_opendir(const char *filename, struct fs_dir_stream **dirsp)
+{
+	fat_dir *dir = malloc(sizeof(*dir));
+	int ret;
+
+	if (!dir)
+		return -ENOMEM;
+
+	ret = fat_itr_root(&dir->itr, &dir->fsdata);
+	if (ret)
+		goto fail;
+
+	ret = fat_itr_resolve(&dir->itr, filename, TYPE_DIR);
+	if (ret)
+		goto fail;
+
+	*dirsp = (struct fs_dir_stream *)dir;
+	return 0;
+
+fail:
+	free(dir);
+	return ret;
+}
+
+int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp)
+{
+	fat_dir *dir = (fat_dir *)dirs;
+	struct fs_dirent *dent = &dir->dirent;
+
+	if (!fat_itr_next(&dir->itr))
+		return -ENOENT;
+
+	memset(dent, 0, sizeof(*dent));
+	strcpy(dent->name, dir->itr.name);
+
+	if (fat_itr_isdir(&dir->itr)) {
+		dent->type = FS_DT_DIR;
+	} else {
+		dent->type = FS_DT_REG;
+		dent->size = FAT2CPU32(dir->itr.dent->size);
+	}
+
+	*dentp = dent;
+
+	return 0;
+}
+
+void fat_closedir(struct fs_dir_stream *dirs)
+{
+	fat_dir *dir = (fat_dir *)dirs;
+	free(dir);
+}
+
 void fat_close(void)
 {
 }