diff --git a/tools/lkl/include/lkl.h b/tools/lkl/include/lkl.h index bf0ee945c1a228..34d2db8704967f 100644 --- a/tools/lkl/include/lkl.h +++ b/tools/lkl/include/lkl.h @@ -163,6 +163,22 @@ long lkl_umount_timeout(char *path, int flags, long timeout_ms); */ struct lkl_dir *lkl_opendir(const char *path, int *err); +/** + * lkl_fdopendir - open a directory + * + * @fd - file descriptor + * @err - pointer to store the error in case of failure + * @returns - a handle to be used when calling lkl_readdir + */ +struct lkl_dir *lkl_fdopendir(int fd, int *err); + +/** + * lkl_rewinddir - reset directory stream + * + * @dir - the directory handler as returned by lkl_opendir + */ +void lkl_rewinddir(struct lkl_dir *dir); + /** * lkl_closedir - close the directory * diff --git a/tools/lkl/lib/fs.c b/tools/lkl/lib/fs.c index 62c743859401f0..9475ded21b2d0f 100644 --- a/tools/lkl/lib/fs.c +++ b/tools/lkl/lib/fs.c @@ -254,7 +254,7 @@ struct lkl_dir { int len; }; -struct lkl_dir *lkl_opendir(const char *path, int *err) +static struct lkl_dir *lkl_dir_alloc(int *err) { struct lkl_dir *dir = lkl_host_ops.mem_alloc(sizeof(struct lkl_dir)); @@ -263,6 +263,19 @@ struct lkl_dir *lkl_opendir(const char *path, int *err) return NULL; } + dir->len = 0; + dir->pos = NULL; + + return dir; +} + +struct lkl_dir *lkl_opendir(const char *path, int *err) +{ + struct lkl_dir *dir = lkl_dir_alloc(err); + + if (!dir) + return NULL; + dir->fd = lkl_sys_open(path, LKL_O_RDONLY | LKL_O_DIRECTORY, 0); if (dir->fd < 0) { *err = dir->fd; @@ -270,12 +283,28 @@ struct lkl_dir *lkl_opendir(const char *path, int *err) return NULL; } - dir->len = 0; - dir->pos = NULL; + return dir; +} + +struct lkl_dir *lkl_fdopendir(int fd, int *err) +{ + struct lkl_dir *dir = lkl_dir_alloc(err); + + if (!dir) + return NULL; + + dir->fd = fd; return dir; } +void lkl_rewinddir(struct lkl_dir *dir) +{ + lkl_sys_lseek(dir->fd, 0, SEEK_SET); + dir->len = 0; + dir->pos = NULL; +} + int lkl_closedir(struct lkl_dir *dir) { int ret;