diff options
| author | ruki <[email protected]> | 2018-09-29 20:17:46 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2018-09-29 20:17:46 +0800 |
| commit | 27658956cfe3f4223753059a3324a9b320320ad7 (patch) | |
| tree | 44406ce7a61553f0af9086e818bb79d5a11682d7 | |
| parent | 2f283df97b8527cc69c3139efec7ad27a6851611 (diff) | |
add os.ln and os.islink
| -rw-r--r-- | core/src/xmake/machine.c | 4 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 2 | ||||
| -rw-r--r-- | core/src/xmake/os/islink.c | 65 | ||||
| -rw-r--r-- | core/src/xmake/os/link.c | 55 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 15 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/os.lua | 54 |
6 files changed, 159 insertions, 36 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index a9541e410..f54ab9df3 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -60,6 +60,7 @@ typedef struct __xm_machine_t // the os functions tb_int_t xm_os_argv(lua_State* lua); tb_int_t xm_os_find(lua_State* lua); +tb_int_t xm_os_link(lua_State* lua); tb_int_t xm_os_isdir(lua_State* lua); tb_int_t xm_os_rmdir(lua_State* lua); tb_int_t xm_os_mkdir(lua_State* lua); @@ -70,6 +71,7 @@ tb_int_t xm_os_sleep(lua_State* lua); tb_int_t xm_os_mclock(lua_State* lua); tb_int_t xm_os_curdir(lua_State* lua); tb_int_t xm_os_tmpdir(lua_State* lua); +tb_int_t xm_os_islink(lua_State* lua); tb_int_t xm_os_isfile(lua_State* lua); tb_int_t xm_os_rmfile(lua_State* lua); tb_int_t xm_os_cpfile(lua_State* lua); @@ -149,6 +151,7 @@ static luaL_Reg const g_os_functions[] = { { "argv", xm_os_argv } , { "find", xm_os_find } +, { "link", xm_os_link } , { "isdir", xm_os_isdir } , { "rmdir", xm_os_rmdir } , { "mkdir", xm_os_mkdir } @@ -159,6 +162,7 @@ static luaL_Reg const g_os_functions[] = , { "mclock", xm_os_mclock } , { "curdir", xm_os_curdir } , { "tmpdir", xm_os_tmpdir } +, { "islink", xm_os_islink } , { "isfile", xm_os_isfile } , { "rmfile", xm_os_rmfile } , { "cpfile", xm_os_cpfile } diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 1b5a939cc..cd05a8b6a 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -16,6 +16,7 @@ xmake_C_FILES += \ machine \ os/argv \ os/find \ + os/link \ os/isdir \ os/rmdir \ os/mkdir \ @@ -27,6 +28,7 @@ xmake_C_FILES += \ os/curdir \ os/tmpdir \ os/isfile \ + os/islink \ os/rmfile \ os/cpfile \ os/rename \ diff --git a/core/src/xmake/os/islink.c b/core/src/xmake/os/islink.c new file mode 100644 index 000000000..3af6166ff --- /dev/null +++ b/core/src/xmake/os/islink.c @@ -0,0 +1,65 @@ +/*!The Make-like Build Utility based on Lua + * + * 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) 2015 - 2018, TBOOX Open Source Group. + * + * @author ruki + * @file islink.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "islink" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" +#ifndef TB_CONFIG_OS_WINDOWS +# include <sys/stat.h> +#endif + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_os_islink(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // get the path + tb_char_t const* path = luaL_checkstring(lua, 1); + tb_check_return_val(path, 0); + + // is link? +#if defined(TB_CONFIG_OS_WINDOWS) + lua_pushboolean(lua, tb_false); +#elif defined(TB_CONFIG_POSIX_HAVE_STAT64) + struct stat64 st = {0}; + lua_pushboolean(lua, !lstat64(path, &st) && S_ISLNK(st.st_mode)); +#else + struct stat st = {0}; + lua_pushboolean(lua, !lstat(path, &st) && S_ISLNK(st.st_mode)); +#endif + + // ok + return 1; +} diff --git a/core/src/xmake/os/link.c b/core/src/xmake/os/link.c new file mode 100644 index 000000000..cf5cf6b7d --- /dev/null +++ b/core/src/xmake/os/link.c @@ -0,0 +1,55 @@ +/*!The Make-like Build Utility based on Lua + * + * 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) 2015 - 2018, TBOOX Open Source Group. + * + * @author ruki + * @file link.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "link" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "prefix.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ +tb_int_t xm_os_link(lua_State* lua) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // get the source and destination + tb_char_t const* src = luaL_checkstring(lua, 1); + tb_char_t const* dst = luaL_checkstring(lua, 2); + tb_check_return_val(src && dst, 0); + + // do os.link(src, dst) + lua_pushboolean(lua, tb_file_link(src, dst)); + + // ok + return 1; +} diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index ac692c445..9f806ba08 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -115,8 +115,8 @@ function os._rm(filedir) -- check assert(filedir) - -- is file? - if os.isfile(filedir) then + -- is file or link? + if os.isfile(filedir) or os.islink(filedir) then -- remove file if not os.rmfile(filedir) then return false, string.format("cannot remove file %s %s", filedir, os.strerror()) @@ -389,6 +389,17 @@ function os.rm(...) return true end +-- link file or directory to the new symfile +function os.ln(filedir, symfile) + if os.host() == "windows" then + return false, string.format("symlink is not supported!") + end + if not os.link(filedir, symfile) then + return false, string.format("link %s to %s failed!", filedir, symfile) + end + return true +end + -- change to directory function os.cd(dir) diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua index ddf5105cd..a3b088bf3 100644 --- a/xmake/core/sandbox/modules/os.lua +++ b/xmake/core/sandbox/modules/os.lua @@ -113,6 +113,14 @@ function sandbox_os.rm(...) end end +-- link file or directory to the new symfile +function sandbox_os.ln(filedir, symfile) + local ok, errors = os.ln(filedir, symfile) + if not ok then + os.raise(errors) + end +end + -- try to copy file or directory function sandbox_os.trycp(...) @@ -389,54 +397,32 @@ end -- is directory? function sandbox_os.isdir(dirpath) - - -- check assert(dirpath) - - -- format it first - dirpath = vformat(dirpath) - - -- done - return os.isdir(dirpath) + return os.isdir(vformat(dirpath)) end --- is directory? +-- is file? function sandbox_os.isfile(filepath) - - -- check assert(filepath) + return os.isfile(vformat(filepath)) +end - -- format it first - filepath = vformat(filepath) - - -- done - return os.isfile(filepath) +-- is symlink? +function sandbox_os.islink(filepath) + assert(filepath) + return os.islink(vformat(filepath)) end -- is execute program? function sandbox_os.isexec(filepath) - - -- check assert(filepath) - - -- format it first - filepath = vformat(filepath) - - -- done - return os.isexec(filepath) + return os.isexec(vformat(filepath)) end -- exists file or directory? -function sandbox_os.exists(file_or_dir) - - -- check - assert(file_or_dir) - - -- format it first - file_or_dir = vformat(file_or_dir) - - -- done - return os.exists(file_or_dir) +function sandbox_os.exists(filedir) + assert(filedir) + return os.exists(vformat(filedir)) end -- return module |
