Skip to content

Commit

Permalink
add syserror
Browse files Browse the repository at this point in the history
  • Loading branch information
waruqi committed Mar 27, 2017
1 parent 64b9ef7 commit a9102ac
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 12 deletions.
47 changes: 47 additions & 0 deletions src/tbox/platform/libc/syserror.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*!The Treasure Box Library
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (C) 2009 - 2017, TBOOX Open Source Group.
*
* @author ruki
* @file syserror.c
* @ingroup platform
*/

/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include <errno.h>

/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
tb_size_t tb_syserror_state()
{
// get last error
switch (errno)
{
case EPERM:
case EACCES:
return TB_STATE_SYSERROR_NOT_PERM;
case ENOENT:
return TB_STATE_SYSERROR_NOT_FILEDIR;
default:
return TB_STATE_SYSERROR_UNKNOWN_ERROR;
}
}
1 change: 1 addition & 0 deletions src/tbox/platform/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "barrier.h"
#include "dynamic.h"
#include "process.h"
#include "syserror.h"
#include "addrinfo.h"
#include "spinlock.h"
#include "atomic64.h"
Expand Down
38 changes: 38 additions & 0 deletions src/tbox/platform/syserror.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*!The Treasure Box Library
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (C) 2009 - 2017, TBOOX Open Source Group.
*
* @author ruki
* @file syserror.c
* @ingroup platform
*/

/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "syserror.h"

/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
#ifdef TB_CONFIG_OS_WINDOWS
# include "windows/syserror.c"
#else
# include "libc/syserror.c"
#endif
56 changes: 56 additions & 0 deletions src/tbox/platform/syserror.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*!The Treasure Box Library
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright (C) 2009 - 2017, TBOOX Open Source Group.
*
* @author ruki
* @file syserror.h
* @ingroup platform
*
*/
#ifndef TB_PLATFORM_SYSERROR_H
#define TB_PLATFORM_SYSERROR_H

/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
#include "prefix.h"

/* //////////////////////////////////////////////////////////////////////////////////////
* extern
*/
__tb_extern_c_enter__

/* //////////////////////////////////////////////////////////////////////////////////////
* interfaces
*/

/*! get the last system error state
*
* @note the error code see state.h
*
* @return the last system error state
*/
tb_size_t tb_syserror_state(tb_noarg_t);

/* //////////////////////////////////////////////////////////////////////////////////////
* extern
*/
__tb_extern_c_leave__

#endif
9 changes: 5 additions & 4 deletions src/tbox/prefix/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ tb_char_t const* tb_state_cstr(tb_size_t state)
case TB_STATE_NOT_SUPPORTED: return "not supported";
case TB_STATE_UNKNOWN_ERROR: return "unknown error";

case TB_STATE_FILE_NOT_EXISTS: return "file: not exists";
case TB_STATE_FILE_OPEN_FAILED: return "file: open failed";
case TB_STATE_FILE_UNKNOWN_ERROR: return "file: unknown error";

case TB_STATE_SOCK_DNS_FAILED: return "sock: dns: failed";
case TB_STATE_SOCK_CONNECT_FAILED: return "sock: connect: failed";
case TB_STATE_SOCK_CONNECT_TIMEOUT: return "sock: connect: timeout";
Expand Down Expand Up @@ -140,6 +136,11 @@ tb_char_t const* tb_state_cstr(tb_size_t state)
case TB_STATE_DATABASE_VALUE_COUNT_ERROR: return "database: value count error";
case TB_STATE_DATABASE_UNKNOWN_HOST: return "database: unknown host";
case TB_STATE_DATABASE_UNKNOWN_ERROR: return "database: unknown error";

case TB_STATE_SYSERROR_NOT_PERM: return "syserror: not permitted";
case TB_STATE_SYSERROR_NOT_FILEDIR: return "syserror: not file or directory";
case TB_STATE_SYSERROR_UNKNOWN_ERROR: return "syserror: unknown error";

default: return "unknown";
}

Expand Down
14 changes: 7 additions & 7 deletions src/tbox/prefix/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,9 @@ typedef enum __tb_state_type_e
{
TB_STATE_TYPE_NONE = 0
, TB_STATE_TYPE_SOCK = 1
, TB_STATE_TYPE_FILE = 2
, TB_STATE_TYPE_HTTP = 3
, TB_STATE_TYPE_DATABASE = 4
, TB_STATE_TYPE_HTTP = 2
, TB_STATE_TYPE_DATABASE = 3
, TB_STATE_TYPE_SYSERROR = 4

}tb_state_type_e;

Expand Down Expand Up @@ -87,10 +87,6 @@ typedef enum __tb_state_e
, TB_STATE_NOT_SUPPORTED = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 22)
, TB_STATE_UNKNOWN_ERROR = TB_STATE_DEFINE(TB_STATE_TYPE_NONE, 23)

, TB_STATE_FILE_NOT_EXISTS = TB_STATE_DEFINE(TB_STATE_TYPE_FILE, 1)
, TB_STATE_FILE_OPEN_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_FILE, 2)
, TB_STATE_FILE_UNKNOWN_ERROR = TB_STATE_DEFINE(TB_STATE_TYPE_FILE, 3)

, TB_STATE_SOCK_DNS_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 1)
, TB_STATE_SOCK_CONNECT_FAILED = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 2)
, TB_STATE_SOCK_CONNECT_TIMEOUT = TB_STATE_DEFINE(TB_STATE_TYPE_SOCK, 3)
Expand Down Expand Up @@ -166,6 +162,10 @@ typedef enum __tb_state_e
, TB_STATE_DATABASE_UNKNOWN_HOST = TB_STATE_DEFINE(TB_STATE_TYPE_DATABASE, 7)
, TB_STATE_DATABASE_UNKNOWN_ERROR = TB_STATE_DEFINE(TB_STATE_TYPE_DATABASE, 8)

, TB_STATE_SYSERROR_NOT_PERM = TB_STATE_DEFINE(TB_STATE_TYPE_SYSERROR, 1)
, TB_STATE_SYSERROR_NOT_FILEDIR = TB_STATE_DEFINE(TB_STATE_TYPE_SYSERROR, 2)
, TB_STATE_SYSERROR_UNKNOWN_ERROR = TB_STATE_DEFINE(TB_STATE_TYPE_SYSERROR, 3)

}tb_state_e;

/* //////////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/tbox/stream/impl/stream/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ static tb_bool_t tb_stream_file_open(tb_stream_ref_t stream)
if (!stream_file->file)
{
// save state
tb_stream_state_set(stream, tb_file_info(url, tb_null)? TB_STATE_FILE_OPEN_FAILED : TB_STATE_FILE_NOT_EXISTS);
tb_stream_state_set(stream, tb_syserror_state());
return tb_false;
}

Expand Down

0 comments on commit a9102ac

Please sign in to comment.