summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-07-24 23:52:50 +0800
committerruki <[email protected]>2019-07-24 17:17:02 +0800
commitd9aa7f40e661dd3675d09adb0e1f0c889b05ed7a (patch)
tree4dd04aa63c82c2ba9dec616ff4791ef804d49060
parent192a6448fbd67535dc97b8d308bef53ba61adee8 (diff)
add io.openlock
-rw-r--r--core/src/xmake/io/filelock.h78
-rw-r--r--core/src/xmake/io/openlock.c62
-rw-r--r--core/src/xmake/machine.c6
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--xmake/core/sandbox/modules/io.lua19
5 files changed, 163 insertions, 3 deletions
diff --git a/core/src/xmake/io/filelock.h b/core/src/xmake/io/filelock.h
new file mode 100644
index 000000000..562926a4a
--- /dev/null
+++ b/core/src/xmake/io/filelock.h
@@ -0,0 +1,78 @@
+/*!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 - 2019, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file filelock.h
+ *
+ */
+#ifndef XM_IO_FILE_LOCK_H
+#define XM_IO_FILE_LOCK_H
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * macros
+ */
+#define xm_io_filelock_udata "XM_IO_FILE_LOCK*"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * types
+ */
+
+// the file lock type
+typedef struct __xm_io_filelock_t
+{
+ // the lock reference
+ tb_filelock_ref_t lock_ref;
+
+} xm_io_filelock_t;
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * interfaces
+ */
+static __tb_inline__ xm_io_filelock_t* xm_io_new_filelock(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, tb_null);
+
+ // new file lock
+ xm_io_filelock_t* lock = (xm_io_filelock_t*)lua_newuserdata(lua, sizeof(xm_io_filelock_t));
+ tb_assert_and_check_return_val(lock, tb_null);
+
+ // init file lock
+ lock->lock_ref = tb_null;
+
+ // bind io._filelock metatable
+ luaL_getmetatable(lua, xm_io_filelock_udata);
+ lua_setmetatable(lua, -2);
+ return lock;
+}
+
+static __tb_inline__ xm_io_filelock_t* xm_io_get_filelock(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, tb_null);
+
+ // get file lock
+ xm_io_filelock_t* lock = (xm_io_filelock_t*)luaL_checkudata(lua, 1, xm_io_filelock_udata);
+ tb_assert(lock);
+ return lock;
+}
+
+#endif
diff --git a/core/src/xmake/io/openlock.c b/core/src/xmake/io/openlock.c
new file mode 100644
index 000000000..4475869fb
--- /dev/null
+++ b/core/src/xmake/io/openlock.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 - 2019, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file openlock.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "openlock"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "filelock.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/*
+ * io.openlock(path)
+ */
+tb_int_t xm_io_openlock(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get file path
+ tb_char_t const* path = luaL_checkstring(lua, 1);
+ tb_assert_and_check_return_val(path, 0);
+
+ // init file lock
+ tb_filelock_ref_t lock = tb_filelock_init_from_path(path, tb_file_info(path, tb_null)? TB_FILE_MODE_RO : TB_FILE_MODE_RW | TB_FILE_MODE_CREAT);
+ if (lock)
+ {
+ xm_io_filelock_t* xmlock = xm_io_new_filelock(lua);
+ xmlock->lock_ref = lock;
+ }
+ else
+ {
+ lua_pushnil(lua);
+ lua_pushliteral(lua, "cannot open file lock!");
+ }
+ return 1;
+}
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index b2aa162d3..0e0a34964 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -55,8 +55,6 @@ typedef struct __xm_machine_t
* declaration
*/
-// the global functions
-
// the os functions
tb_int_t xm_os_argv(lua_State* lua);
tb_int_t xm_os_find(lua_State* lua);
@@ -94,6 +92,7 @@ tb_int_t xm_os_getown(lua_State* lua);
// the io functions
tb_int_t xm_io_std(lua_State* lua);
tb_int_t xm_io_open(lua_State* lua);
+tb_int_t xm_io_openlock(lua_State* lua);
// the file functions
tb_int_t xm_io_file_read(lua_State* lua);
@@ -224,6 +223,7 @@ static luaL_Reg const g_winos_functions[] =
static luaL_Reg const g_io_functions[] =
{
{ "open", xm_io_open }
+, { "openlock", xm_io_openlock }
, { tb_null, tb_null }
};
@@ -614,7 +614,7 @@ xm_machine_ref_t xm_machine_init()
xm_machine_register_metatable(machine, "io", "_file", "XM_IO_FILE*", g_io_file_functions);
// bind io.filelock (metatable) functions
- xm_machine_register_metatable(machine, "io", "_filelock", "XM_IO_FILELOCK*", g_io_filelock_functions);
+ xm_machine_register_metatable(machine, "io", "_filelock", "XM_IO_FILE_LOCK*", g_io_filelock_functions);
// add stdin, stdout, stderr to io
xm_io_std(machine->lua);
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index 7232e0fd9..4eb460c70 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -54,6 +54,7 @@ xmake_C_FILES += \
io/file_seek \
io/file_write \
io/open \
+ io/openlock \
io/std \
path/relative \
path/absolute \
diff --git a/xmake/core/sandbox/modules/io.lua b/xmake/core/sandbox/modules/io.lua
index 7aead5368..92310278b 100644
--- a/xmake/core/sandbox/modules/io.lua
+++ b/xmake/core/sandbox/modules/io.lua
@@ -114,7 +114,9 @@ function sandbox_io.open(filepath, mode, opt)
raise(errors)
end
+ -- wrap file
file = { _FILE = file }
+
-- replace print with vformat
setmetatable(file, sandbox_io_file);
@@ -122,6 +124,23 @@ function sandbox_io.open(filepath, mode, opt)
return file
end
+-- open file lock
+function sandbox_io.openlock(filepath)
+
+ -- check
+ assert(filepath)
+
+ -- format it first
+ filepath = vformat(filepath)
+
+ -- open lock
+ local filelock, errors = io.openlock(filepath)
+ if not filelock then
+ raise(errors)
+ end
+ return filelock
+end
+
-- load object from the given file
function sandbox_io.load(filepath, opt)