Abstract POSIX filesystem functions (#2585)
To allow non-POSIX platforms such as Windows to support WASI libc filesystem functionality, create a set of wrapper functions which provide a platform-agnostic interface to interact with the host filesystem. For now, the Windows implementation is stubbed but this will be implemented properly in a future PR. There are no functional changes in this change, just a reorganization of code to move any direct POSIX references out of posix.c in the libc implementation into posix_file.c under the shared POSIX sources. See https://github.com/bytecodealliance/wasm-micro-runtime/issues/2495 for a more detailed overview of the plan to port the WASI libc filesystem to Windows.
This commit is contained in:
256
core/shared/platform/common/libc-util/libc_errno.c
Normal file
256
core/shared/platform/common/libc-util/libc_errno.c
Normal file
@ -0,0 +1,256 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#include "errno.h"
|
||||
#include "libc_errno.h"
|
||||
|
||||
__wasi_errno_t
|
||||
convert_errno(int error)
|
||||
{
|
||||
// The C standard library only requires EDOM, EILSEQ and ERANGE to be
|
||||
// defined. Other error codes are POSIX-specific and hence may or may
|
||||
// not be available on non-POSIX platforms.
|
||||
__wasi_errno_t code = __WASI_ENOSYS;
|
||||
#define X(v) \
|
||||
case v: \
|
||||
code = __WASI_##v; \
|
||||
break;
|
||||
switch (error) {
|
||||
X(EDOM)
|
||||
X(EILSEQ)
|
||||
X(ERANGE)
|
||||
#ifdef E2BIG
|
||||
X(E2BIG)
|
||||
#endif
|
||||
#ifdef EACCES
|
||||
X(EACCES)
|
||||
#endif
|
||||
#ifdef EADDRINUSE
|
||||
X(EADDRINUSE)
|
||||
#endif
|
||||
#ifdef EADDRNOTAVAIL
|
||||
X(EADDRNOTAVAIL)
|
||||
#endif
|
||||
#ifdef EAFNOSUPPORT
|
||||
X(EAFNOSUPPORT)
|
||||
#endif
|
||||
#ifdef EAGAIN
|
||||
X(EAGAIN)
|
||||
#endif
|
||||
#ifdef EALREADY
|
||||
X(EALREADY)
|
||||
#endif
|
||||
#ifdef EBADF
|
||||
X(EBADF)
|
||||
#endif
|
||||
#ifdef EBADMSG
|
||||
X(EBADMSG)
|
||||
#endif
|
||||
#ifdef EBUSY
|
||||
X(EBUSY)
|
||||
#endif
|
||||
#ifdef ECANCELED
|
||||
X(ECANCELED)
|
||||
#endif
|
||||
#ifdef ECHILD
|
||||
X(ECHILD)
|
||||
#endif
|
||||
#ifdef ECONNABORTED
|
||||
X(ECONNABORTED)
|
||||
#endif
|
||||
#ifdef ECONNREFUSED
|
||||
X(ECONNREFUSED)
|
||||
#endif
|
||||
#ifdef ECONNRESET
|
||||
X(ECONNRESET)
|
||||
#endif
|
||||
#ifdef EDEADLK
|
||||
X(EDEADLK)
|
||||
#endif
|
||||
#ifdef EDESTADDRREQ
|
||||
X(EDESTADDRREQ)
|
||||
#endif
|
||||
#ifdef EDQUOT
|
||||
X(EDQUOT)
|
||||
#endif
|
||||
#ifdef EEXIST
|
||||
X(EEXIST)
|
||||
#endif
|
||||
#ifdef EFAULT
|
||||
X(EFAULT)
|
||||
#endif
|
||||
#ifdef EFBIG
|
||||
X(EFBIG)
|
||||
#endif
|
||||
#ifdef EHOSTUNREACH
|
||||
X(EHOSTUNREACH)
|
||||
#endif
|
||||
#ifdef EIDRM
|
||||
X(EIDRM)
|
||||
#endif
|
||||
#ifdef EINPROGRESS
|
||||
X(EINPROGRESS)
|
||||
#endif
|
||||
#ifdef EINTR
|
||||
X(EINTR)
|
||||
#endif
|
||||
#ifdef EINVAL
|
||||
X(EINVAL)
|
||||
#endif
|
||||
#ifdef EIO
|
||||
X(EIO)
|
||||
#endif
|
||||
#ifdef EISCONN
|
||||
X(EISCONN)
|
||||
#endif
|
||||
#ifdef EISDIR
|
||||
X(EISDIR)
|
||||
#endif
|
||||
#ifdef ELOOP
|
||||
X(ELOOP)
|
||||
#endif
|
||||
#ifdef EMFILE
|
||||
X(EMFILE)
|
||||
#endif
|
||||
#ifdef EMLINK
|
||||
X(EMLINK)
|
||||
#endif
|
||||
#ifdef EMSGSIZE
|
||||
X(EMSGSIZE)
|
||||
#endif
|
||||
#ifdef EMULTIHOP
|
||||
X(EMULTIHOP)
|
||||
#endif
|
||||
#ifdef ENAMETOOLONG
|
||||
X(ENAMETOOLONG)
|
||||
#endif
|
||||
#ifdef ENETDOWN
|
||||
X(ENETDOWN)
|
||||
#endif
|
||||
#ifdef ENETRESET
|
||||
X(ENETRESET)
|
||||
#endif
|
||||
#ifdef ENETUNREACH
|
||||
X(ENETUNREACH)
|
||||
#endif
|
||||
#ifdef ENFILE
|
||||
X(ENFILE)
|
||||
#endif
|
||||
#ifdef ENOBUFS
|
||||
X(ENOBUFS)
|
||||
#endif
|
||||
#ifdef ENODEV
|
||||
X(ENODEV)
|
||||
#endif
|
||||
#ifdef ENOENT
|
||||
X(ENOENT)
|
||||
#endif
|
||||
#ifdef ENOEXEC
|
||||
X(ENOEXEC)
|
||||
#endif
|
||||
#ifdef ENOLCK
|
||||
X(ENOLCK)
|
||||
#endif
|
||||
#ifdef ENOLINK
|
||||
X(ENOLINK)
|
||||
#endif
|
||||
#ifdef ENOMEM
|
||||
X(ENOMEM)
|
||||
#endif
|
||||
#ifdef ENOMSG
|
||||
X(ENOMSG)
|
||||
#endif
|
||||
#ifdef ENOPROTOOPT
|
||||
X(ENOPROTOOPT)
|
||||
#endif
|
||||
#ifdef ENOSPC
|
||||
X(ENOSPC)
|
||||
#endif
|
||||
#ifdef ENOSYS
|
||||
X(ENOSYS)
|
||||
#endif
|
||||
#ifdef ENOTCAPABLE
|
||||
X(ENOTCAPABLE)
|
||||
#endif
|
||||
#ifdef ENOTCONN
|
||||
X(ENOTCONN)
|
||||
#endif
|
||||
#ifdef ENOTDIR
|
||||
X(ENOTDIR)
|
||||
#endif
|
||||
#ifdef ENOTEMPTY
|
||||
X(ENOTEMPTY)
|
||||
#endif
|
||||
#ifdef ENOTRECOVERABLE
|
||||
X(ENOTRECOVERABLE)
|
||||
#endif
|
||||
#ifdef ENOTSOCK
|
||||
X(ENOTSOCK)
|
||||
#endif
|
||||
#ifdef ENOTSUP
|
||||
X(ENOTSUP)
|
||||
#endif
|
||||
#ifdef ENOTTY
|
||||
X(ENOTTY)
|
||||
#endif
|
||||
#ifdef ENXIO
|
||||
X(ENXIO)
|
||||
#endif
|
||||
#ifdef EOVERFLOW
|
||||
X(EOVERFLOW)
|
||||
#endif
|
||||
#ifdef EOWNERDEAD
|
||||
X(EOWNERDEAD)
|
||||
#endif
|
||||
#ifdef EPERM
|
||||
X(EPERM)
|
||||
#endif
|
||||
#ifdef EPIPE
|
||||
X(EPIPE)
|
||||
#endif
|
||||
#ifdef EPROTO
|
||||
X(EPROTO)
|
||||
#endif
|
||||
#ifdef EPROTONOSUPPORT
|
||||
X(EPROTONOSUPPORT)
|
||||
#endif
|
||||
#ifdef EPROTOTYPE
|
||||
X(EPROTOTYPE)
|
||||
#endif
|
||||
#ifdef EROFS
|
||||
X(EROFS)
|
||||
#endif
|
||||
#ifdef ESPIPE
|
||||
X(ESPIPE)
|
||||
#endif
|
||||
#ifdef ESRCH
|
||||
X(ESRCH)
|
||||
#endif
|
||||
#ifdef ESTALE
|
||||
X(ESTALE)
|
||||
#endif
|
||||
#ifdef ETIMEDOUT
|
||||
X(ETIMEDOUT)
|
||||
#endif
|
||||
#ifdef ETXTBSY
|
||||
X(ETXTBSY)
|
||||
#endif
|
||||
#ifdef EXDEV
|
||||
X(EXDEV)
|
||||
#endif
|
||||
default:
|
||||
#ifdef EOPNOTSUPP
|
||||
if (error == EOPNOTSUPP)
|
||||
code = __WASI_ENOTSUP;
|
||||
#endif
|
||||
#ifdef EWOULDBLOCK
|
||||
if (error == EWOULDBLOCK)
|
||||
code = __WASI_EAGAIN;
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
#undef X
|
||||
return code;
|
||||
}
|
||||
15
core/shared/platform/common/libc-util/libc_errno.h
Normal file
15
core/shared/platform/common/libc-util/libc_errno.h
Normal file
@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright (C) 2023 Intel Corporation. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
*/
|
||||
|
||||
#ifndef WASI_ERRNO_H
|
||||
#define WASI_ERRNO_H
|
||||
|
||||
#include "platform_wasi.h"
|
||||
|
||||
// Converts an errno error code to a WASI error code.
|
||||
__wasi_errno_t
|
||||
convert_errno(int error);
|
||||
|
||||
#endif /* end of WASI_ERRNO_H */
|
||||
@ -0,0 +1,8 @@
|
||||
# Copyright (C) 2019 Intel Corporation. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
|
||||
set (PLATFORM_COMMON_LIBC_UTIL_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
include_directories(${PLATFORM_COMMON_LIBC_UTIL_DIR})
|
||||
|
||||
file (GLOB_RECURSE PLATFORM_COMMON_LIBC_UTIL_SOURCE ${PLATFORM_COMMON_LIBC_UTIL_DIR}/*.c)
|
||||
@ -5,4 +5,11 @@ set (PLATFORM_COMMON_POSIX_DIR ${CMAKE_CURRENT_LIST_DIR})
|
||||
|
||||
file (GLOB_RECURSE source_all ${PLATFORM_COMMON_POSIX_DIR}/*.c)
|
||||
|
||||
if (NOT WAMR_BUILD_LIBC_WASI EQUAL 1)
|
||||
list(REMOVE_ITEM source_all ${PLATFORM_COMMON_POSIX_DIR}/posix_file.c)
|
||||
else()
|
||||
include (${CMAKE_CURRENT_LIST_DIR}/../libc-util/platform_common_libc_util.cmake)
|
||||
set(source_all ${source_all} ${PLATFORM_COMMON_LIBC_UTIL_SOURCE})
|
||||
endif()
|
||||
|
||||
set (PLATFORM_COMMON_POSIX_SOURCE ${source_all} )
|
||||
|
||||
1013
core/shared/platform/common/posix/posix_file.c
Normal file
1013
core/shared/platform/common/posix/posix_file.c
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user