Skip to content

Commit 67f1c7d

Browse files
committed
wasi: implement fd_sync and fd_datasync
1 parent 553ce4d commit 67f1c7d

File tree

1 file changed

+61
-4
lines changed

1 file changed

+61
-4
lines changed

lib/wasi.c

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,67 @@ wasi_fd_tell(struct exec_context *ctx, struct host_instance *hi,
16651665
return 0;
16661666
}
16671667

1668+
static int
1669+
wasi_fd_sync(struct exec_context *ctx, struct host_instance *hi,
1670+
const struct functype *ft, const struct cell *params,
1671+
struct cell *results)
1672+
{
1673+
WASI_TRACE;
1674+
struct wasi_instance *wasi = (void *)hi;
1675+
HOST_FUNC_CONVERT_PARAMS(ft, params);
1676+
uint32_t wasifd = HOST_FUNC_PARAM(ft, params, 0, i32);
1677+
struct wasi_fdinfo *fdinfo = NULL;
1678+
int hostfd;
1679+
int ret;
1680+
ret = wasi_hostfd_lookup(wasi, wasifd, &hostfd, &fdinfo);
1681+
if (ret != 0) {
1682+
goto fail;
1683+
}
1684+
ret = fsync(hostfd);
1685+
if (ret == -1) {
1686+
ret = errno;
1687+
assert(ret > 0);
1688+
goto fail;
1689+
}
1690+
fail:
1691+
wasi_fdinfo_release(wasi, fdinfo);
1692+
HOST_FUNC_RESULT_SET(ft, results, 0, i32, wasi_convert_errno(ret));
1693+
return 0;
1694+
}
1695+
1696+
#if defined(__APPLE__)
1697+
/* macOS doesn't have fdatasync */
1698+
#define wasi_fd_datasync wasi_fd_sync
1699+
#else
1700+
static int
1701+
wasi_fd_datasync(struct exec_context *ctx, struct host_instance *hi,
1702+
const struct functype *ft, const struct cell *params,
1703+
struct cell *results)
1704+
{
1705+
WASI_TRACE;
1706+
struct wasi_instance *wasi = (void *)hi;
1707+
HOST_FUNC_CONVERT_PARAMS(ft, params);
1708+
uint32_t wasifd = HOST_FUNC_PARAM(ft, params, 0, i32);
1709+
struct wasi_fdinfo *fdinfo = NULL;
1710+
int hostfd;
1711+
int ret;
1712+
ret = wasi_hostfd_lookup(wasi, wasifd, &hostfd, &fdinfo);
1713+
if (ret != 0) {
1714+
goto fail;
1715+
}
1716+
ret = fdatasync(hostfd);
1717+
if (ret == -1) {
1718+
ret = errno;
1719+
assert(ret > 0);
1720+
goto fail;
1721+
}
1722+
fail:
1723+
wasi_fdinfo_release(wasi, fdinfo);
1724+
HOST_FUNC_RESULT_SET(ft, results, 0, i32, wasi_convert_errno(ret));
1725+
return 0;
1726+
}
1727+
#endif
1728+
16681729
static int
16691730
wasi_fd_renumber(struct exec_context *ctx, struct host_instance *hi,
16701731
const struct functype *ft, const struct cell *params,
@@ -2927,9 +2988,7 @@ const struct host_func wasi_funcs[] = {
29272988
WASI_HOST_FUNC(fd_advise, "(iIIi)i"),
29282989
WASI_HOST_FUNC(fd_allocate, "(iII)i"),
29292990
WASI_HOST_FUNC(fd_close, "(i)i"),
2930-
#if 0 /* TODO */
29312991
WASI_HOST_FUNC(fd_datasync, "(i)i"),
2932-
#endif
29332992
WASI_HOST_FUNC(fd_fdstat_get, "(ii)i"),
29342993
WASI_HOST_FUNC(fd_fdstat_set_flags, "(ii)i"),
29352994
WASI_HOST_FUNC(fd_fdstat_set_rights, "(iII)i"),
@@ -2944,9 +3003,7 @@ const struct host_func wasi_funcs[] = {
29443003
WASI_HOST_FUNC(fd_readdir, "(iiiIi)i"),
29453004
WASI_HOST_FUNC(fd_renumber, "(ii)i"),
29463005
WASI_HOST_FUNC(fd_seek, "(iIii)i"),
2947-
#if 0 /* TODO */
29483006
WASI_HOST_FUNC(fd_sync, "(i)i"),
2949-
#endif
29503007
WASI_HOST_FUNC(fd_tell, "(ii)i"),
29513008
WASI_HOST_FUNC(fd_write, "(iiii)i"),
29523009

0 commit comments

Comments
 (0)