summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-07-25 00:35:27 +0800
committerruki <[email protected]>2019-07-24 21:32:43 +0800
commit493817cfeca5b40401e99ee857a96b3defa81421 (patch)
tree8219ce3917ce9f697ef2e2f4d2cec8ccb830c54d
parente6874fa26c56dde13461c1b03f9563b57c1fbc55 (diff)
add lock path and name
-rw-r--r--core/src/xmake/io/file_path.c29
-rw-r--r--core/src/xmake/io/filelock.h9
-rw-r--r--core/src/xmake/io/filelock___tostring.c50
-rw-r--r--core/src/xmake/io/filelock_close___gc.c12
-rw-r--r--core/src/xmake/io/filelock_path.c55
-rw-r--r--core/src/xmake/io/openlock.c21
-rw-r--r--core/src/xmake/machine.c10
-rw-r--r--core/src/xmake/makefile2
-rw-r--r--xmake/core/sandbox/modules/io.lua7
9 files changed, 170 insertions, 25 deletions
diff --git a/core/src/xmake/io/file_path.c b/core/src/xmake/io/file_path.c
index e4221b6b9..1fcdb752a 100644
--- a/core/src/xmake/io/file_path.c
+++ b/core/src/xmake/io/file_path.c
@@ -14,42 +14,45 @@
*
* Copyright (C) 2015 - 2019, TBOOX Open Source Group.
*
- * @author OpportunityLiu
- * @file file_path.c
+ * @author ruki
+ * @file filelock_path.c
*
*/
/* //////////////////////////////////////////////////////////////////////////////////////
* trace
*/
-#define TB_TRACE_MODULE_NAME "file_path"
+#define TB_TRACE_MODULE_NAME "filelock_path"
#define TB_TRACE_MODULE_DEBUG (0)
/* //////////////////////////////////////////////////////////////////////////////////////
* includes
*/
-#include "file.h"
-#include "prefix.h"
+#include "filelock.h"
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
-/* file:path()
+/* filelock:path()
*/
-tb_int_t xm_io_file_path(lua_State* lua)
+tb_int_t xm_io_filelock_path(lua_State* lua)
{
// check
tb_assert_and_check_return_val(lua, 0);
- // this file has been closed?
- xm_io_file_t* file = xm_io_getfile(lua);
- if (xm_io_file_is_closed(file))
- xm_io_file_return_error_closed(lua);
+ // this lock has been closed?
+ xm_io_filelock_t* lock = xm_io_get_filelock(lua);
+ if (!lock->is_opened)
+ {
+ lua_pushnil(lua);
+ lua_pushliteral(lua, "file lock has been closed!");
+ return 2;
+ }
else
{
- // return file path
- lua_pushstring(lua, file->path);
+ // return lock path
+ lua_pushstring(lua, lock->path);
return 1;
}
}
diff --git a/core/src/xmake/io/filelock.h b/core/src/xmake/io/filelock.h
index 128f51aab..e7e3536e8 100644
--- a/core/src/xmake/io/filelock.h
+++ b/core/src/xmake/io/filelock.h
@@ -44,6 +44,12 @@ typedef struct __xm_io_filelock_t
// is opened
tb_bool_t is_opened;
+ // the lock name
+ tb_char_t name[64];
+
+ // the lock path
+ tb_char_t const* path;
+
} xm_io_filelock_t;
/* //////////////////////////////////////////////////////////////////////////////////////
@@ -59,8 +65,7 @@ static __tb_inline__ xm_io_filelock_t* xm_io_new_filelock(lua_State* lua)
tb_assert_and_check_return_val(lock, tb_null);
// init file lock
- lock->lock_ref = tb_null;
- lock->is_opened = tb_false;
+ tb_memset(lock, 0, sizeof(xm_io_filelock_t));
// bind io._filelock metatable
luaL_getmetatable(lua, xm_io_filelock_udata);
diff --git a/core/src/xmake/io/filelock___tostring.c b/core/src/xmake/io/filelock___tostring.c
new file mode 100644
index 000000000..8002b9eb3
--- /dev/null
+++ b/core/src/xmake/io/filelock___tostring.c
@@ -0,0 +1,50 @@
+/*!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___tostring.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "filelock___tostring"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "filelock.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/*
+ * tostring(lock)
+ */
+tb_int_t xm_io_filelock___tostring(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get lock name as string
+ xm_io_filelock_t* lock = xm_io_get_filelock(lua);
+ lua_pushstring(lua, lock->name);
+ return 1;
+}
+
diff --git a/core/src/xmake/io/filelock_close___gc.c b/core/src/xmake/io/filelock_close___gc.c
index 3e0539dad..cadc02299 100644
--- a/core/src/xmake/io/filelock_close___gc.c
+++ b/core/src/xmake/io/filelock_close___gc.c
@@ -51,7 +51,7 @@ static tb_int_t xm_io_filelock_close_impl(lua_State* lua, tb_bool_t allow_closed
else
{
lua_pushnil(lua);
- lua_pushliteral(lua, "error: file lock has been closed");
+ lua_pushliteral(lua, "file lock has been closed");
return 2;
}
}
@@ -64,6 +64,16 @@ static tb_int_t xm_io_filelock_close_impl(lua_State* lua, tb_bool_t allow_closed
lock->lock_ref = tb_null;
lock->is_opened = tb_false;
+ // free lock path
+ if (lock->path)
+ {
+ tb_free(lock->path);
+ lock->path = tb_null;
+ }
+
+ // mark this lock as closed
+ tb_strlcpy(lock->name, "lock: (closed lock)", tb_arrayn(lock->name));
+
// close ok
lua_pushboolean(lua, tb_true);
return 1;
diff --git a/core/src/xmake/io/filelock_path.c b/core/src/xmake/io/filelock_path.c
new file mode 100644
index 000000000..e4221b6b9
--- /dev/null
+++ b/core/src/xmake/io/filelock_path.c
@@ -0,0 +1,55 @@
+/*!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 OpportunityLiu
+ * @file file_path.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "file_path"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "file.h"
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/* file:path()
+ */
+tb_int_t xm_io_file_path(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // this file has been closed?
+ xm_io_file_t* file = xm_io_getfile(lua);
+ if (xm_io_file_is_closed(file))
+ xm_io_file_return_error_closed(lua);
+ else
+ {
+ // return file path
+ lua_pushstring(lua, file->path);
+ return 1;
+ }
+}
diff --git a/core/src/xmake/io/openlock.c b/core/src/xmake/io/openlock.c
index 6ba2da76d..5df847563 100644
--- a/core/src/xmake/io/openlock.c
+++ b/core/src/xmake/io/openlock.c
@@ -50,9 +50,30 @@ tb_int_t xm_io_openlock(lua_State* lua)
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)
{
+ // init file lock
xm_io_filelock_t* xmlock = xm_io_new_filelock(lua);
xmlock->lock_ref = lock;
xmlock->is_opened = tb_true;
+
+ // save lock path
+ tb_size_t pathlen = tb_strlen(path);
+ xmlock->path = tb_malloc_cstr(pathlen + 1);
+ if (xmlock->path)
+ {
+ tb_strncpy((tb_char_t*)xmlock->path, path, pathlen);
+ ((tb_char_t*)xmlock->path)[pathlen] = '\0';
+ }
+
+ // save lock name
+ tb_size_t name_maxn = tb_arrayn(xmlock->name);
+ tb_strlcpy(xmlock->name, "lock: ", name_maxn);
+ if (pathlen < name_maxn - tb_arrayn("lock: "))
+ tb_strcat(xmlock->name, path);
+ else
+ {
+ tb_strcat(xmlock->name, "...");
+ tb_strcat(xmlock->name, path + (pathlen - name_maxn + tb_arrayn("lock: ") + tb_arrayn("...")));
+ }
return 1;
}
else
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index 6ce258254..66bf45c8b 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -107,8 +107,10 @@ tb_int_t xm_io_file___tostring(lua_State* lua);
tb_int_t xm_io_file___gc(lua_State* lua);
// the filelock functions
+tb_int_t xm_io_filelock_path(lua_State* lua);
tb_int_t xm_io_filelock_close(lua_State* lua);
tb_int_t xm_io_filelock___gc(lua_State* lua);
+tb_int_t xm_io_filelock___tostring(lua_State* lua);
// the path functions
tb_int_t xm_path_relative(lua_State* lua);
@@ -250,9 +252,11 @@ static luaL_Reg const g_io_file_functions[] =
// the filelock functions
static luaL_Reg const g_io_filelock_functions[] =
{
- { "close", xm_io_filelock_close }
-, { "__gc", xm_io_filelock___gc }
-, { tb_null, tb_null }
+ { "path", xm_io_filelock_path }
+, { "close", xm_io_filelock_close }
+, { "__gc", xm_io_filelock___gc }
+, { "__tostring", xm_io_filelock___tostring }
+, { tb_null, tb_null }
};
// the path functions
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index e76376a72..ad3ddaad2 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -53,6 +53,8 @@ xmake_C_FILES += \
io/file_read \
io/file_seek \
io/file_write \
+ io/filelock_path \
+ io/filelock___tostring \
io/filelock_close___gc \
io/open \
io/openlock \
diff --git a/xmake/core/sandbox/modules/io.lua b/xmake/core/sandbox/modules/io.lua
index fff22e1ff..87354c459 100644
--- a/xmake/core/sandbox/modules/io.lua
+++ b/xmake/core/sandbox/modules/io.lua
@@ -28,9 +28,8 @@ local vformat = require("sandbox/modules/vformat")
-- define module
local sandbox_io = sandbox_io or {}
local sandbox_io_file = sandbox_io._file or {}
-local sandbox_io_filelock = sandbox_io._filelock or {}
sandbox_io._file = sandbox_io_file
-sandbox_io._filelock = sandbox_io_filelock
+sandbox_io._filelock = io._filelock
-- inherit some builtin interfaces
sandbox_io.lines = io.lines
@@ -134,10 +133,6 @@ function sandbox_io.openlock(filepath)
if not lock then
raise(errors)
end
-
- -- bind metatable
- lock = { _LOCK = lock }
- setmetatable(lock, sandbox_io_filelock);
return lock
end