diff options
author | Jo-Philipp Wich <jo@mein.io> | 2020-09-08 23:14:43 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2020-09-08 23:14:43 +0200 |
commit | a0171d56898234980f0694d87f0ef5528243b8b0 (patch) | |
tree | 5007b2ac46d2c4b454403be871e9290c9d0556d5 /lib | |
parent | c1467018a6476619af4a415e09090e50f5560ff0 (diff) |
fs: implement seek() and tell() for directory handles
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/fs.c | 39 |
1 files changed, 39 insertions, 0 deletions
@@ -284,6 +284,43 @@ ut_fs_readdir(struct ut_state *s, uint32_t off, struct json_object *args) } static struct json_object * +ut_fs_telldir(struct ut_state *s, uint32_t off, struct json_object *args) +{ + DIR **dp = (DIR **)ops->get_type(s->ctx, "fs.dir"); + long position; + + if (!dp || !*dp) + err_return(EBADF); + + position = telldir(*dp); + + if (position == -1) + err_return(errno); + + return json_object_new_int64((int64_t)position); +} + +static struct json_object * +ut_fs_seekdir(struct ut_state *s, uint32_t off, struct json_object *args) +{ + struct json_object *ofs = json_object_array_get_idx(args, 0); + DIR **dp = (DIR **)ops->get_type(s->ctx, "fs.dir"); + long position; + + if (!json_object_is_type(ofs, json_type_int)) + err_return(EINVAL); + + if (!dp || !*dp) + err_return(EBADF); + + position = (long)json_object_get_int64(ofs); + + seekdir(*dp, position); + + return json_object_new_boolean(true); +} + +static struct json_object * ut_fs_closedir(struct ut_state *s, uint32_t off, struct json_object *args) { DIR **dp = (DIR **)ops->get_type(s->ctx, "fs.dir"); @@ -566,6 +603,8 @@ void ut_module_init(const struct ut_ops *ut, struct ut_state *s, struct json_obj if (dir_proto) { ops->register_function(s, dir_proto, "readdir", ut_fs_readdir); + ops->register_function(s, dir_proto, "seek", ut_fs_seekdir); + ops->register_function(s, dir_proto, "tell", ut_fs_telldir); ops->register_function(s, dir_proto, "closedir", ut_fs_closedir); } } |