2019-01-24 18:03:27 +00:00
|
|
|
/*
|
2020-02-20 15:35:16 +00:00
|
|
|
Copyright (c) 2016-2020 Chung, Hyung-Hwan. All rights reserved.
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
2022-06-11 05:32:01 +00:00
|
|
|
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
2019-01-24 18:03:27 +00:00
|
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
#include "hio-prv.h"
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
2020-02-11 09:02:46 +00:00
|
|
|
#include <string.h>
|
2019-01-24 18:03:27 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
static hio_errnum_t errno_to_errnum (int errcode)
|
2019-01-24 18:03:27 +00:00
|
|
|
{
|
|
|
|
switch (errcode)
|
|
|
|
{
|
2021-07-22 07:30:20 +00:00
|
|
|
case ENOMEM: return HIO_ESYSMEM;
|
|
|
|
case EINVAL: return HIO_EINVAL;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
#if defined(EBUSY)
|
2021-07-22 07:30:20 +00:00
|
|
|
case EBUSY: return HIO_EBUSY;
|
2019-01-24 18:03:27 +00:00
|
|
|
#endif
|
2021-07-22 07:30:20 +00:00
|
|
|
case EACCES: return HIO_EACCES;
|
2019-01-24 18:03:27 +00:00
|
|
|
#if defined(EPERM)
|
2021-07-22 07:30:20 +00:00
|
|
|
case EPERM: return HIO_EPERM;
|
2019-01-24 18:03:27 +00:00
|
|
|
#endif
|
2022-11-15 14:31:14 +00:00
|
|
|
#if defined(EISDIR)
|
|
|
|
case EISDIR: return HIO_EISDIR;
|
|
|
|
#endif
|
2019-01-24 18:03:27 +00:00
|
|
|
#if defined(ENOTDIR)
|
2021-07-22 07:30:20 +00:00
|
|
|
case ENOTDIR: return HIO_ENOTDIR;
|
2019-01-24 18:03:27 +00:00
|
|
|
#endif
|
2021-07-22 07:30:20 +00:00
|
|
|
case ENOENT: return HIO_ENOENT;
|
2019-01-24 18:03:27 +00:00
|
|
|
#if defined(EEXIST)
|
2021-07-22 07:30:20 +00:00
|
|
|
case EEXIST: return HIO_EEXIST;
|
2019-01-24 18:03:27 +00:00
|
|
|
#endif
|
|
|
|
#if defined(EINTR)
|
2021-07-22 07:30:20 +00:00
|
|
|
case EINTR: return HIO_EINTR;
|
2019-01-24 18:03:27 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(EPIPE)
|
2021-07-22 07:30:20 +00:00
|
|
|
case EPIPE: return HIO_EPIPE;
|
2019-01-24 18:03:27 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(EAGAIN) && defined(EWOULDBLOCK) && (EAGAIN != EWOULDBLOCK)
|
2023-01-11 14:59:41 +00:00
|
|
|
case EAGAIN:
|
2021-07-22 07:30:20 +00:00
|
|
|
case EWOULDBLOCK: return HIO_EAGAIN;
|
2019-01-24 18:03:27 +00:00
|
|
|
#elif defined(EAGAIN)
|
2021-07-22 07:30:20 +00:00
|
|
|
case EAGAIN: return HIO_EAGAIN;
|
2019-01-24 18:03:27 +00:00
|
|
|
#elif defined(EWOULDBLOCK)
|
2021-07-22 07:30:20 +00:00
|
|
|
case EWOULDBLOCK: return HIO_EAGAIN;
|
2019-01-24 18:03:27 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(EBADF)
|
2021-07-22 07:30:20 +00:00
|
|
|
case EBADF: return HIO_EBADHND;
|
2019-01-24 18:03:27 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(EIO)
|
2021-07-22 07:30:20 +00:00
|
|
|
case EIO: return HIO_EIOERR;
|
2019-01-24 18:03:27 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-27 02:09:22 +00:00
|
|
|
#if defined(EMFILE)
|
|
|
|
case EMFILE:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_EMFILE;
|
2019-01-27 02:09:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ENFILE)
|
|
|
|
case ENFILE:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_ENFILE;
|
2019-01-27 02:09:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ECONNREFUSED)
|
|
|
|
case ECONNREFUSED:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_ECONRF;
|
2019-01-27 02:09:22 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ECONNRESETD)
|
|
|
|
case ECONNRESET:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_ECONRS;
|
2019-01-27 02:09:22 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
default: return HIO_ESYSERR;
|
2019-01-24 18:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2021-07-22 07:30:20 +00:00
|
|
|
static hio_errnum_t winerr_to_errnum (DWORD errcode)
|
2019-01-24 18:03:27 +00:00
|
|
|
{
|
|
|
|
switch (errcode)
|
|
|
|
{
|
|
|
|
case ERROR_NOT_ENOUGH_MEMORY:
|
|
|
|
case ERROR_OUTOFMEMORY:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_ESYSMEM;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
case ERROR_INVALID_PARAMETER:
|
|
|
|
case ERROR_INVALID_NAME:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_EINVAL;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
case ERROR_INVALID_HANDLE:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_EBADHND;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
case ERROR_SHARING_VIOLATION:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_EACCES;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
#if defined(ERROR_IO_PRIVILEGE_FAILED)
|
|
|
|
case ERROR_IO_PRIVILEGE_FAILED:
|
|
|
|
#endif
|
|
|
|
case ERROR_PRIVILEGE_NOT_HELD:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_EPERM;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_ENOENT;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
case ERROR_ALREADY_EXISTS:
|
|
|
|
case ERROR_FILE_EXISTS:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_EEXIST;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
case ERROR_BROKEN_PIPE:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_EPIPE;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
default:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_ESYSERR;
|
2019-01-24 18:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(__OS2__)
|
2021-07-22 07:30:20 +00:00
|
|
|
static hio_errnum_t os2err_to_errnum (APIRET errcode)
|
2019-01-24 18:03:27 +00:00
|
|
|
{
|
|
|
|
/* APIRET e */
|
|
|
|
switch (errcode)
|
|
|
|
{
|
|
|
|
case ERROR_NOT_ENOUGH_MEMORY:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_ESYSMEM;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
case ERROR_INVALID_PARAMETER:
|
2019-01-24 18:03:27 +00:00
|
|
|
case ERROR_INVALID_NAME:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_EINVAL;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
case ERROR_INVALID_HANDLE:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_EBADHND;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
2023-01-11 14:59:41 +00:00
|
|
|
case ERROR_ACCESS_DENIED:
|
2019-01-24 18:03:27 +00:00
|
|
|
case ERROR_SHARING_VIOLATION:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_EACCES;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_ENOENT;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
case ERROR_ALREADY_EXISTS:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_EEXIST;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
/*TODO: add more mappings */
|
|
|
|
default:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_ESYSERR;
|
2019-01-24 18:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(macintosh)
|
2021-07-22 07:30:20 +00:00
|
|
|
static hio_errnum_t macerr_to_errnum (int errcode)
|
2019-01-24 18:03:27 +00:00
|
|
|
{
|
|
|
|
switch (e)
|
|
|
|
{
|
|
|
|
case notEnoughMemoryErr:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_ESYSMEM;
|
2019-01-24 18:03:27 +00:00
|
|
|
case paramErr:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_EINVAL;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
case qErr: /* queue element not found during deletion */
|
|
|
|
case fnfErr: /* file not found */
|
|
|
|
case dirNFErr: /* direcotry not found */
|
|
|
|
case resNotFound: /* resource not found */
|
|
|
|
case resFNotFound: /* resource file not found */
|
|
|
|
case nbpNotFound: /* name not found on remove */
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_ENOENT;
|
2019-01-24 18:03:27 +00:00
|
|
|
|
|
|
|
/*TODO: add more mappings */
|
2023-01-11 14:59:41 +00:00
|
|
|
default:
|
2021-07-22 07:30:20 +00:00
|
|
|
return HIO_ESYSERR;
|
2019-01-24 18:03:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
hio_errnum_t hio_sys_syserrstrb (hio_t* hio, int syserr_type, int syserr_code, hio_bch_t* buf, hio_oow_t len)
|
2019-01-24 18:03:27 +00:00
|
|
|
{
|
|
|
|
switch (syserr_type)
|
|
|
|
{
|
2023-01-11 14:59:41 +00:00
|
|
|
case 1:
|
2019-01-24 18:03:27 +00:00
|
|
|
#if defined(_WIN32)
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
DWORD rc;
|
|
|
|
rc = FormatMessageA (
|
|
|
|
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
2023-01-11 14:59:41 +00:00
|
|
|
NULL, syserr_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
2021-07-22 07:30:20 +00:00
|
|
|
buf, len, HIO_NULL
|
2019-01-24 18:03:27 +00:00
|
|
|
);
|
|
|
|
while (rc > 0 && buf[rc - 1] == '\r' || buf[rc - 1] == '\n') buf[--rc] = '\0';
|
|
|
|
}
|
|
|
|
return winerr_to_errnum(syserr_code);
|
|
|
|
#elif defined(__OS2__)
|
|
|
|
/* TODO: convert code to string */
|
2021-07-22 07:30:20 +00:00
|
|
|
if (buf) hio_copy_bcstr (buf, len, "system error");
|
2019-01-24 18:03:27 +00:00
|
|
|
return os2err_to_errnum(syserr_code);
|
|
|
|
#elif defined(macintosh)
|
|
|
|
/* TODO: convert code to string */
|
2021-07-22 07:30:20 +00:00
|
|
|
if (buf) hio_copy_bcstr (buf, len, "system error");
|
2019-01-24 18:03:27 +00:00
|
|
|
return os2err_to_errnum(syserr_code);
|
|
|
|
#else
|
|
|
|
/* in other systems, errno is still the native system error code.
|
|
|
|
* fall thru */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
#if defined(HAVE_STRERROR_R)
|
|
|
|
if (buf) strerror_r (syserr_code, buf, len);
|
|
|
|
#else
|
|
|
|
/* this is not thread safe */
|
2021-07-22 07:30:20 +00:00
|
|
|
if (buf) hio_copy_bcstr (buf, len, strerror(syserr_code));
|
2019-01-24 18:03:27 +00:00
|
|
|
#endif
|
|
|
|
return errno_to_errnum(syserr_code);
|
|
|
|
}
|
|
|
|
|
2020-02-11 09:02:46 +00:00
|
|
|
|
2021-07-22 07:30:20 +00:00
|
|
|
if (buf) hio_copy_bcstr (buf, len, "system error");
|
|
|
|
return HIO_ESYSERR;
|
2019-01-24 18:03:27 +00:00
|
|
|
}
|
|
|
|
|