Skip to content

Commit

Permalink
lkl: add lkl_fdopendir and lkl_rewinddir
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Zimmermann <sigmaepsilon92@gmail.com>
  • Loading branch information
M1cha committed Sep 22, 2016
1 parent e8301b1 commit a3fc590
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
16 changes: 16 additions & 0 deletions tools/lkl/include/lkl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
35 changes: 32 additions & 3 deletions tools/lkl/lib/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand All @@ -263,19 +263,48 @@ 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;
lkl_host_ops.mem_free(dir);
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;
Expand Down

0 comments on commit a3fc590

Please sign in to comment.