diff options
| author | ruki <[email protected]> | 2022-07-23 13:42:43 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-07-23 13:42:43 +0800 |
| commit | fad741419fd216e5057bd4c972fc324f16ade47b (patch) | |
| tree | 2665c936e0f7550f78ce975bb4b7cb2175de1456 /core/src | |
| parent | f84814e6c98db75e538007cebd7edee34710b275 (diff) | |
| parent | c6fb2dcb124ead33156e2639423ed7ae9c19dbfa (diff) | |
Merge pull request #2592 from xmake-io/fwatcher
add fs watcher
Diffstat (limited to 'core/src')
| -rwxr-xr-x | core/src/tbox/inc/linux/tbox.config.h | 3 | ||||
| -rw-r--r-- | core/src/tbox/makefile | 1 | ||||
| m--------- | core/src/tbox/tbox | 0 | ||||
| -rw-r--r-- | core/src/xmake/engine.c | 21 | ||||
| -rw-r--r-- | core/src/xmake/fwatcher/add.c | 64 | ||||
| -rw-r--r-- | core/src/xmake/fwatcher/close.c | 57 | ||||
| -rw-r--r-- | core/src/xmake/fwatcher/open.c | 46 | ||||
| -rw-r--r-- | core/src/xmake/fwatcher/prefix.h | 31 | ||||
| -rw-r--r-- | core/src/xmake/fwatcher/remove.c | 62 | ||||
| -rw-r--r-- | core/src/xmake/fwatcher/wait.c | 73 | ||||
| -rw-r--r-- | core/src/xmake/io/poller_wait.c | 17 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 5 |
12 files changed, 379 insertions, 1 deletions
diff --git a/core/src/tbox/inc/linux/tbox.config.h b/core/src/tbox/inc/linux/tbox.config.h index d3e16a6a3..b5e0faa7f 100755 --- a/core/src/tbox/inc/linux/tbox.config.h +++ b/core/src/tbox/inc/linux/tbox.config.h @@ -203,4 +203,7 @@ // valgrind functions /* #undef TB_CONFIG_SYSTEMV_HAVE_VALGRIND_STACK_REGISTER */ +// linux functions +#define TB_CONFIG_LINUX_HAVE_INOTIFY_INIT 1 + #endif diff --git a/core/src/tbox/makefile b/core/src/tbox/makefile index f24303eb1..b03269a78 100644 --- a/core/src/tbox/makefile +++ b/core/src/tbox/makefile @@ -229,6 +229,7 @@ tbox_C_FILES += \ tbox/src/tbox/platform/print \ tbox/src/tbox/platform/stdfile \ tbox/src/tbox/platform/process \ + tbox/src/tbox/platform/fwatcher \ tbox/src/tbox/platform/cpu \ tbox/src/tbox/platform/sched \ tbox/src/tbox/platform/semaphore \ diff --git a/core/src/tbox/tbox b/core/src/tbox/tbox -Subproject 0cdd892038242128bbefa4f00dcb4cc451c751d +Subproject 138ea23adf94e2ef4f7db7984fe432872abf3b5 diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c index d275c21f8..7c904fb38 100644 --- a/core/src/xmake/engine.c +++ b/core/src/xmake/engine.c @@ -245,6 +245,13 @@ tb_int_t xm_process_wait(lua_State* lua); tb_int_t xm_process_kill(lua_State* lua); tb_int_t xm_process_close(lua_State* lua); +// the fwatcher functions +tb_int_t xm_fwatcher_open(lua_State* lua); +tb_int_t xm_fwatcher_add(lua_State* lua); +tb_int_t xm_fwatcher_remove(lua_State* lua); +tb_int_t xm_fwatcher_wait(lua_State* lua); +tb_int_t xm_fwatcher_close(lua_State* lua); + // the sandbox functions tb_int_t xm_sandbox_interactive(lua_State* lua); @@ -488,6 +495,17 @@ static luaL_Reg const g_process_functions[] = , { tb_null, tb_null } }; +// the fwatcher functions +static luaL_Reg const g_fwatcher_functions[] = +{ + { "open", xm_fwatcher_open } +, { "add", xm_fwatcher_add } +, { "remove", xm_fwatcher_remove } +, { "wait", xm_fwatcher_wait } +, { "close", xm_fwatcher_close } +, { tb_null, tb_null } +}; + // the sandbox functions static luaL_Reg const g_sandbox_functions[] = { @@ -1025,6 +1043,9 @@ xm_engine_ref_t xm_engine_init(tb_char_t const* name, xm_engine_lni_initalizer_c // bind process functions xm_lua_register(engine->lua, "process", g_process_functions); + // bind fwatcher functions + xm_lua_register(engine->lua, "fwatcher", g_fwatcher_functions); + // bind sandbox functions xm_lua_register(engine->lua, "sandbox", g_sandbox_functions); diff --git a/core/src/xmake/fwatcher/add.c b/core/src/xmake/fwatcher/add.c new file mode 100644 index 000000000..a71ce83f1 --- /dev/null +++ b/core/src/xmake/fwatcher/add.c @@ -0,0 +1,64 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed 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) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file add.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "fwatcher.add" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// fwatcher.add(watchdir, recursion) +tb_int_t xm_fwatcher_add(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is pointer? + if (!xm_lua_ispointer(lua, 1)) + return 0; + + // get the fwatcher + tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)xm_lua_topointer(lua, 1); + tb_check_return_val(fwatcher, 0); + + // get watchdir + tb_char_t const* watchdir = luaL_checkstring(lua, 2); + tb_check_return_val(watchdir, 0); + + // get recursion + tb_bool_t recursion = lua_toboolean(lua, 3); + + // add watchdir + tb_bool_t ok = tb_fwatcher_add(fwatcher, watchdir, recursion); + + // save result + lua_pushboolean(lua, ok); + return 1; +} diff --git a/core/src/xmake/fwatcher/close.c b/core/src/xmake/fwatcher/close.c new file mode 100644 index 000000000..0430afa00 --- /dev/null +++ b/core/src/xmake/fwatcher/close.c @@ -0,0 +1,57 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed 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) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file close.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "fwatcher.close" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// fwatcher.close(p) +tb_int_t xm_fwatcher_close(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is pointer? + if (!xm_lua_ispointer(lua, 1)) + return 0; + + // get the fwatcher + tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)xm_lua_topointer(lua, 1); + tb_check_return_val(fwatcher, 0); + + // exit fwatcher + tb_fwatcher_exit(fwatcher); + + // save result: ok + lua_pushboolean(lua, tb_true); + return 1; +} diff --git a/core/src/xmake/fwatcher/open.c b/core/src/xmake/fwatcher/open.c new file mode 100644 index 000000000..431762922 --- /dev/null +++ b/core/src/xmake/fwatcher/open.c @@ -0,0 +1,46 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed 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) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file open.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "fwatcher.open" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_fwatcher_open(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // init fwatcher + tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)tb_fwatcher_init(); + if (fwatcher) xm_lua_pushpointer(lua, (tb_pointer_t)fwatcher); + else lua_pushnil(lua); + return 1; +} diff --git a/core/src/xmake/fwatcher/prefix.h b/core/src/xmake/fwatcher/prefix.h new file mode 100644 index 000000000..b203a555e --- /dev/null +++ b/core/src/xmake/fwatcher/prefix.h @@ -0,0 +1,31 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed 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) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file prefix.h + * + */ +#ifndef XM_FWATCHER_PREFIX_H +#define XM_FWATCHER_PREFIX_H + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "../prefix.h" + +#endif + + diff --git a/core/src/xmake/fwatcher/remove.c b/core/src/xmake/fwatcher/remove.c new file mode 100644 index 000000000..334e0a938 --- /dev/null +++ b/core/src/xmake/fwatcher/remove.c @@ -0,0 +1,62 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed 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) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file remove.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "fwatcher.remove" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// fwatcher.remove(watchdir) +tb_int_t xm_fwatcher_remove(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is pointer? + if (!xm_lua_ispointer(lua, 1)) + return 0; + + // get the fwatcher + tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)xm_lua_topointer(lua, 1); + tb_check_return_val(fwatcher, 0); + + // get watchdir + tb_char_t const* watchdir = luaL_checkstring(lua, 2); + tb_check_return_val(watchdir, 0); + + // remove watchdir + tb_bool_t ok = tb_fwatcher_remove(fwatcher, watchdir); + + // save result + lua_pushboolean(lua, ok); + return 1; +} + diff --git a/core/src/xmake/fwatcher/wait.c b/core/src/xmake/fwatcher/wait.c new file mode 100644 index 000000000..f8bbd6b78 --- /dev/null +++ b/core/src/xmake/fwatcher/wait.c @@ -0,0 +1,73 @@ +/*!A cross-platform build utility based on Lua + * + * Licensed 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) 2015-present, TBOOX Open Source Group. + * + * @author ruki + * @file wait.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "fwatcher.wait" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +// fwatcher.wait(p) +tb_int_t xm_fwatcher_wait(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // is pointer? + if (!xm_lua_ispointer(lua, 1)) + return 0; + + // get the fwatcher + tb_fwatcher_ref_t fwatcher = (tb_fwatcher_ref_t)xm_lua_topointer(lua, 1); + tb_check_return_val(fwatcher, 0); + + // get the timeout + tb_long_t timeout = (tb_long_t)luaL_checkinteger(lua, 2); + + // wait fwatcher event + tb_fwatcher_event_t event; + tb_long_t ok = tb_fwatcher_wait(fwatcher, &event, timeout); + + // save result + lua_pushinteger(lua, ok); + if (ok > 0) + { + lua_newtable(lua); + lua_pushstring(lua, "path"); + lua_pushstring(lua, event.filepath); + lua_settable(lua, -3); + + lua_pushstring(lua, "type"); + lua_pushinteger(lua, event.event); + lua_settable(lua, -3); + return 2; + } + return 1; +} diff --git a/core/src/xmake/io/poller_wait.c b/core/src/xmake/io/poller_wait.c index 008f1366e..7389faacf 100644 --- a/core/src/xmake/io/poller_wait.c +++ b/core/src/xmake/io/poller_wait.c @@ -54,7 +54,22 @@ static tb_void_t xm_io_poller_event(tb_poller_ref_t poller, tb_poller_object_ref if (priv) lua_pushstring(g_lua, (tb_char_t const*)priv); else lua_pushlightuserdata(g_lua, object->ref.ptr); lua_rawseti(g_lua, -2, 2); - lua_pushinteger(g_lua, (tb_int_t)events); + if (object->type == TB_POLLER_OBJECT_FWATCHER) + { + lua_newtable(g_lua); + tb_fwatcher_event_t* event = (tb_fwatcher_event_t*)events; + if (event) + { + lua_pushstring(g_lua, "path"); + lua_pushstring(g_lua, event->filepath); + lua_settable(g_lua, -3); + + lua_pushstring(g_lua, "type"); + lua_pushinteger(g_lua, event->event); + lua_settable(g_lua, -3); + } + } + else lua_pushinteger(g_lua, (tb_int_t)events); lua_rawseti(g_lua, -2, 3); lua_rawseti(g_lua, -2, ++g_events_count); } diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 996b76578..5d017f0b0 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -115,6 +115,11 @@ xmake_C_FILES += \ process/wait \ process/kill \ process/close \ + fwatcher/open \ + fwatcher/add \ + fwatcher/remove \ + fwatcher/wait \ + fwatcher/close \ sandbox/interactive \ semver/parse \ semver/compare \ |
