|
@@ -10,12 +10,24 @@
|
|
#include <AK/Random.h>
|
|
#include <AK/Random.h>
|
|
#include <AK/Span.h>
|
|
#include <AK/Span.h>
|
|
#include <AK/Tuple.h>
|
|
#include <AK/Tuple.h>
|
|
|
|
+#include <LibCore/System.h>
|
|
#include <LibWasm/AbstractMachine/Configuration.h>
|
|
#include <LibWasm/AbstractMachine/Configuration.h>
|
|
#include <LibWasm/Wasi.h>
|
|
#include <LibWasm/Wasi.h>
|
|
#include <fcntl.h>
|
|
#include <fcntl.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/stat.h>
|
|
#include <time.h>
|
|
#include <time.h>
|
|
-#include <unistd.h>
|
|
|
|
|
|
+
|
|
|
|
+#ifndef AK_OS_WINDOWS
|
|
|
|
+# include <unistd.h>
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+#define RETURN_ERRNO_IF_ERROR(result) \
|
|
|
|
+ if (result.is_error()) { \
|
|
|
|
+ if (result.error().is_errno()) \
|
|
|
|
+ return errno_value_from_errno(result.error().code()); \
|
|
|
|
+ else \
|
|
|
|
+ return Errno::Invalid; \
|
|
|
|
+ }
|
|
|
|
|
|
namespace Wasm::Wasi::ABI {
|
|
namespace Wasm::Wasi::ABI {
|
|
|
|
|
|
@@ -310,8 +322,10 @@ static ErrorOr<size_t> copy_string_excluding_terminating_null(Configuration& con
|
|
}
|
|
}
|
|
|
|
|
|
static Errno errno_value_from_errno(int value);
|
|
static Errno errno_value_from_errno(int value);
|
|
|
|
+#ifndef AK_OS_WINDOWS
|
|
static FileType file_type_of(struct stat const& buf);
|
|
static FileType file_type_of(struct stat const& buf);
|
|
static FDFlags fd_flags_of(struct stat const& buf);
|
|
static FDFlags fd_flags_of(struct stat const& buf);
|
|
|
|
+#endif
|
|
|
|
|
|
Vector<AK::String> const& Implementation::arguments() const
|
|
Vector<AK::String> const& Implementation::arguments() const
|
|
{
|
|
{
|
|
@@ -434,8 +448,8 @@ ErrorOr<Result<void>> Implementation::impl$fd_close(Configuration&, FD fd)
|
|
{
|
|
{
|
|
return map_fd(fd).visit(
|
|
return map_fd(fd).visit(
|
|
[&](u32 fd) -> Result<void> {
|
|
[&](u32 fd) -> Result<void> {
|
|
- if (close(bit_cast<i32>(fd)) != 0)
|
|
|
|
- return errno_value_from_errno(errno);
|
|
|
|
|
|
+ auto result = Core::System::close(bit_cast<i32>(fd));
|
|
|
|
+ RETURN_ERRNO_IF_ERROR(result);
|
|
return {};
|
|
return {};
|
|
},
|
|
},
|
|
[&](PreopenedDirectoryDescriptor) -> Result<void> {
|
|
[&](PreopenedDirectoryDescriptor) -> Result<void> {
|
|
@@ -456,10 +470,9 @@ ErrorOr<Result<Size>> Implementation::impl$fd_write(Configuration& configuration
|
|
Size bytes_written = 0;
|
|
Size bytes_written = 0;
|
|
for (auto& iovec : TRY(copy_typed_array(configuration, iovs, iovs_len))) {
|
|
for (auto& iovec : TRY(copy_typed_array(configuration, iovs, iovs_len))) {
|
|
auto slice = TRY(slice_typed_memory(configuration, iovec.buf, iovec.buf_len));
|
|
auto slice = TRY(slice_typed_memory(configuration, iovec.buf, iovec.buf_len));
|
|
- auto result = write(fd_value, slice.data(), slice.size());
|
|
|
|
- if (result < 0)
|
|
|
|
- return errno_value_from_errno(errno);
|
|
|
|
- bytes_written += static_cast<Size>(result);
|
|
|
|
|
|
+ auto result = Core::System::write(fd_value, slice);
|
|
|
|
+ RETURN_ERRNO_IF_ERROR(result);
|
|
|
|
+ bytes_written += static_cast<Size>(result.value());
|
|
}
|
|
}
|
|
return bytes_written;
|
|
return bytes_written;
|
|
}
|
|
}
|
|
@@ -509,6 +522,8 @@ ErrorOr<Result<void>> Implementation::impl$fd_prestat_dir_name(Configuration& co
|
|
return Result<void> {};
|
|
return Result<void> {};
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#ifndef AK_OS_WINDOWS
|
|
|
|
+
|
|
ErrorOr<Result<FileStat>> Implementation::impl$path_filestat_get(Configuration& configuration, FD fd, LookupFlags flags, ConstPointer<u8> path, Size path_len)
|
|
ErrorOr<Result<FileStat>> Implementation::impl$path_filestat_get(Configuration& configuration, FD fd, LookupFlags flags, ConstPointer<u8> path, Size path_len)
|
|
{
|
|
{
|
|
auto dir_fd = AT_FDCWD;
|
|
auto dir_fd = AT_FDCWD;
|
|
@@ -751,6 +766,8 @@ ErrorOr<Result<FileStat>> Implementation::impl$fd_filestat_get(Configuration&, F
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#endif
|
|
|
|
+
|
|
ErrorOr<Result<void>> Implementation::impl$random_get(Configuration& configuration, Pointer<u8> buf, Size buf_len)
|
|
ErrorOr<Result<void>> Implementation::impl$random_get(Configuration& configuration, Pointer<u8> buf, Size buf_len)
|
|
{
|
|
{
|
|
auto buffer_slice = TRY(slice_typed_memory(configuration, buf, buf_len));
|
|
auto buffer_slice = TRY(slice_typed_memory(configuration, buf, buf_len));
|
|
@@ -769,14 +786,15 @@ ErrorOr<Result<Size>> Implementation::impl$fd_read(Configuration& configuration,
|
|
Size bytes_read = 0;
|
|
Size bytes_read = 0;
|
|
for (auto& iovec : TRY(copy_typed_array(configuration, iovs, iovs_len))) {
|
|
for (auto& iovec : TRY(copy_typed_array(configuration, iovs, iovs_len))) {
|
|
auto slice = TRY(slice_typed_memory(configuration, iovec.buf, iovec.buf_len));
|
|
auto slice = TRY(slice_typed_memory(configuration, iovec.buf, iovec.buf_len));
|
|
- auto result = read(fd_value, slice.data(), slice.size());
|
|
|
|
- if (result < 0)
|
|
|
|
- return errno_value_from_errno(errno);
|
|
|
|
- bytes_read += static_cast<Size>(result);
|
|
|
|
|
|
+ auto result = Core::System::read(fd_value, slice);
|
|
|
|
+ RETURN_ERRNO_IF_ERROR(result);
|
|
|
|
+ bytes_read += static_cast<Size>(result.value());
|
|
}
|
|
}
|
|
return bytes_read;
|
|
return bytes_read;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#ifndef AK_OS_WINDOWS
|
|
|
|
+
|
|
ErrorOr<Result<FDStat>> Implementation::impl$fd_fdstat_get(Configuration&, FD fd)
|
|
ErrorOr<Result<FDStat>> Implementation::impl$fd_fdstat_get(Configuration&, FD fd)
|
|
{
|
|
{
|
|
auto mapped_fd = map_fd(fd);
|
|
auto mapped_fd = map_fd(fd);
|
|
@@ -822,9 +840,21 @@ ErrorOr<Result<FileSize>> Implementation::impl$fd_seek(Configuration&, FD fd, Fi
|
|
return FileSize(result);
|
|
return FileSize(result);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#endif
|
|
|
|
+
|
|
#pragma GCC diagnostic push
|
|
#pragma GCC diagnostic push
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
|
|
|
|
|
|
+#ifdef AK_OS_WINDOWS
|
|
|
|
+ErrorOr<Result<FileStat>> Implementation::impl$path_filestat_get(Configuration& configuration, FD fd, LookupFlags flags, ConstPointer<u8> path, Size path_len) { return Errno::NoSys; }
|
|
|
|
+ErrorOr<Result<void>> Implementation::impl$path_create_directory(Configuration& configuration, FD fd, Pointer<u8> path, Size path_len) { return Errno::NoSys; }
|
|
|
|
+ErrorOr<Result<FD>> Implementation::impl$path_open(Configuration& configuration, FD fd, LookupFlags lookup_flags, Pointer<u8> path, Size path_len, OFlags o_flags, Rights, Rights, FDFlags fd_flags) { return Errno::NoSys; }
|
|
|
|
+ErrorOr<Result<Timestamp>> Implementation::impl$clock_time_get(Configuration&, ClockID id, Timestamp precision) { return Errno::NoSys; }
|
|
|
|
+ErrorOr<Result<FileStat>> Implementation::impl$fd_filestat_get(Configuration&, FD fd) { return Errno::NoSys; }
|
|
|
|
+ErrorOr<Result<FDStat>> Implementation::impl$fd_fdstat_get(Configuration&, FD fd) { return Errno::NoSys; }
|
|
|
|
+ErrorOr<Result<FileSize>> Implementation::impl$fd_seek(Configuration&, FD fd, FileDelta offset, Whence whence) { return Errno::NoSys; }
|
|
|
|
+#endif
|
|
|
|
+
|
|
ErrorOr<Result<Timestamp>> Implementation::impl$clock_res_get(Configuration&, ClockID id)
|
|
ErrorOr<Result<Timestamp>> Implementation::impl$clock_res_get(Configuration&, ClockID id)
|
|
{
|
|
{
|
|
return Errno::NoSys;
|
|
return Errno::NoSys;
|
|
@@ -1182,8 +1212,10 @@ Errno errno_value_from_errno(int value)
|
|
return Errno::SPipe;
|
|
return Errno::SPipe;
|
|
case ESRCH:
|
|
case ESRCH:
|
|
return Errno::SRCH;
|
|
return Errno::SRCH;
|
|
|
|
+#ifdef ESTALE
|
|
case ESTALE:
|
|
case ESTALE:
|
|
return Errno::Stale;
|
|
return Errno::Stale;
|
|
|
|
+#endif
|
|
case ETIMEDOUT:
|
|
case ETIMEDOUT:
|
|
return Errno::TimedOut;
|
|
return Errno::TimedOut;
|
|
case ETXTBSY:
|
|
case ETXTBSY:
|
|
@@ -1195,6 +1227,7 @@ Errno errno_value_from_errno(int value)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#ifndef AK_OS_WINDOWS
|
|
FileType file_type_of(struct stat const& buf)
|
|
FileType file_type_of(struct stat const& buf)
|
|
{
|
|
{
|
|
switch (buf.st_mode & S_IFMT) {
|
|
switch (buf.st_mode & S_IFMT) {
|
|
@@ -1221,6 +1254,7 @@ FDFlags fd_flags_of(struct stat const&)
|
|
FDFlags::Bits result {};
|
|
FDFlags::Bits result {};
|
|
return FDFlags { result };
|
|
return FDFlags { result };
|
|
}
|
|
}
|
|
|
|
+#endif
|
|
}
|
|
}
|
|
|
|
|
|
namespace AK {
|
|
namespace AK {
|