summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-07-31 23:32:36 +0800
committerruki <[email protected]>2022-07-31 23:32:36 +0800
commitbb28bbb01c73ab1d7e455479f3338466a983d7d4 (patch)
treec77b6942e68f972e01ef835ab794c42f4962c27e
parent1f27cde3a9c4b7e6866d677a2dd4e8a9cd6027e6 (diff)
add os.touch
-rw-r--r--core/src/xmake/engine.c2
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--core/src/xmake/os/touch.c49
-rw-r--r--xmake/core/base/os.lua13
-rw-r--r--xmake/core/sandbox/modules/os.lua9
5 files changed, 72 insertions, 2 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 6458dee1c..b3d798e4f 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -98,6 +98,7 @@ 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_touch(lua_State* lua);
tb_int_t xm_os_rmfile(lua_State* lua);
tb_int_t xm_os_cpfile(lua_State* lua);
tb_int_t xm_os_rename(lua_State* lua);
@@ -319,6 +320,7 @@ static luaL_Reg const g_os_functions[] =
, { "tmpdir", xm_os_tmpdir }
, { "islink", xm_os_islink }
, { "isfile", xm_os_isfile }
+, { "touch", xm_os_touch }
, { "rmfile", xm_os_rmfile }
, { "cpfile", xm_os_cpfile }
, { "rename", xm_os_rename }
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index 44fb9a0b3..5ce8942bb 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -30,6 +30,7 @@ xmake_C_FILES += \
os/tmpdir \
os/isfile \
os/islink \
+ os/touch \
os/rmfile \
os/cpfile \
os/rename \
diff --git a/core/src/xmake/os/touch.c b/core/src/xmake/os/touch.c
new file mode 100644
index 000000000..04b655908
--- /dev/null
+++ b/core/src/xmake/os/touch.c
@@ -0,0 +1,49 @@
+/*!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 touch.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "touch"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_os_touch(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ tb_char_t const* path = luaL_checkstring(lua, 1);
+ tb_check_return_val(path, 0);
+
+ tb_time_t atime = (tb_time_t)luaL_checknumber(lua, 2);
+ tb_time_t mtime = (tb_time_t)luaL_checknumber(lua, 3);
+
+ lua_pushboolean(lua, tb_file_touch(path, atime, mtime));
+ return 1;
+}
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 7dd0f3aad..fff5c8408 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -37,6 +37,7 @@ os._getpid = os._getpid or os.getpid
os._exit = os._exit or os.exit
os._mkdir = os._mkdir or os.mkdir
os._rmdir = os._rmdir or os.rmdir
+os._touch = os._touch or os.touch
os._tmpdir = os._tmpdir or os.tmpdir
os._setenv = os._setenv or os.setenv
os._getenvs = os._getenvs or os.getenvs
@@ -551,11 +552,19 @@ function os.cd(dir)
if os._SCHED_CHDIR then
os._SCHED_CHDIR(os.curdir())
end
-
- -- ok
return oldir
end
+-- touch file or directory, it will modify atime/mtime or create a new file
+-- we will do not change it if atime/mtime is zero
+function os.touch(filepath, opt)
+ opt = opt or {}
+ if not os._touch(filepath, opt.atime or 0, opt.mtime or 0) then
+ return false, string.format("cannot touch %s, %s", filepath, os.strerror())
+ end
+ return true
+end
+
-- create directories
function os.mkdir(dir)
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 4c2509cc9..a5744c3b5 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -200,6 +200,15 @@ function sandbox_os.cd(dir)
return oldir
end
+-- touch file or directory
+function sandbox_os.touch(filepath, opt)
+ assert(filepath)
+ local ok, errors = os.touch(vformat(filepath), opt)
+ if not ok then
+ os.raise(errors)
+ end
+end
+
-- create directories
function sandbox_os.mkdir(dir)
assert(dir)