diff options
| author | ruki <[email protected]> | 2019-07-24 23:59:12 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-07-24 17:55:18 +0800 |
| commit | e6874fa26c56dde13461c1b03f9563b57c1fbc55 (patch) | |
| tree | 876f682ef0e4b5aaac4b19eb2a9c8924649ef685 /core/src | |
| parent | d9aa7f40e661dd3675d09adb0e1f0c889b05ed7a (diff) | |
add filelock close/gc
Diffstat (limited to 'core/src')
| -rw-r--r-- | core/src/xmake/io/file.h | 15 | ||||
| -rw-r--r-- | core/src/xmake/io/file___len.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file___tostring.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_close___gc.c | 3 | ||||
| -rw-r--r-- | core/src/xmake/io/file_flush.c | 6 | ||||
| -rw-r--r-- | core/src/xmake/io/file_isatty.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_path.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_read.c | 22 | ||||
| -rw-r--r-- | core/src/xmake/io/file_seek.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/file_write.c | 8 | ||||
| -rw-r--r-- | core/src/xmake/io/filelock.h | 6 | ||||
| -rw-r--r-- | core/src/xmake/io/filelock_close___gc.c | 90 | ||||
| -rw-r--r-- | core/src/xmake/io/open.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/io/openlock.c | 6 | ||||
| -rw-r--r-- | core/src/xmake/io/std.c | 2 | ||||
| -rw-r--r-- | core/src/xmake/machine.c | 8 | ||||
| -rw-r--r-- | core/src/xmake/makefile | 1 |
17 files changed, 142 insertions, 37 deletions
diff --git a/core/src/xmake/io/file.h b/core/src/xmake/io/file.h index 900ef00b6..28ecc77ae 100644 --- a/core/src/xmake/io/file.h +++ b/core/src/xmake/io/file.h @@ -87,7 +87,8 @@ typedef enum __xm_io_file_encoding_e } xm_io_file_encoding_e; -typedef struct __xm_io_file +// the file type +typedef struct __xm_io_file_t { union { @@ -111,34 +112,34 @@ typedef struct __xm_io_file tb_char_t const* path; tb_buffer_t rcache; // the read line cache buffer tb_buffer_t wcache; // the write line cache buffer -} xm_io_file; +} xm_io_file_t; /* ////////////////////////////////////////////////////////////////////////////////////// * interfaces */ -static __tb_inline__ xm_io_file* xm_io_newfile(lua_State* lua) +static __tb_inline__ xm_io_file_t* xm_io_newfile(lua_State* lua) { // check tb_assert_and_check_return_val(lua, tb_null); // new file - xm_io_file* file = (xm_io_file*)lua_newuserdata(lua, sizeof(xm_io_file)); + xm_io_file_t* file = (xm_io_file_t*)lua_newuserdata(lua, sizeof(xm_io_file_t)); tb_assert_and_check_return_val(file, tb_null); // init file luaL_getmetatable(lua, xm_io_file_udata); lua_setmetatable(lua, -2); - tb_memset(file, 0, sizeof(xm_io_file)); + tb_memset(file, 0, sizeof(xm_io_file_t)); return file; } -static __tb_inline__ xm_io_file* xm_io_getfile(lua_State* lua) +static __tb_inline__ xm_io_file_t* xm_io_getfile(lua_State* lua) { // check tb_assert_and_check_return_val(lua, tb_null); // get file - xm_io_file* file = (xm_io_file*)luaL_checkudata(lua, 1, xm_io_file_udata); + xm_io_file_t* file = (xm_io_file_t*)luaL_checkudata(lua, 1, xm_io_file_udata); tb_assert(file); return file; } diff --git a/core/src/xmake/io/file___len.c b/core/src/xmake/io/file___len.c index 449df7d3c..d244edfd4 100644 --- a/core/src/xmake/io/file___len.c +++ b/core/src/xmake/io/file___len.c @@ -42,7 +42,7 @@ tb_int_t xm_io_file___len(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - xm_io_file* file = xm_io_getfile(lua); + xm_io_file_t* file = xm_io_getfile(lua); if (xm_io_file_is_closed(file)) xm_io_file_return_error_closed(lua); else if (xm_io_file_is_file(file)) diff --git a/core/src/xmake/io/file___tostring.c b/core/src/xmake/io/file___tostring.c index 8290d358a..55a5e9167 100644 --- a/core/src/xmake/io/file___tostring.c +++ b/core/src/xmake/io/file___tostring.c @@ -44,7 +44,7 @@ tb_int_t xm_io_file___tostring(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // get file name as string - xm_io_file* file = xm_io_getfile(lua); + xm_io_file_t* file = xm_io_getfile(lua); lua_pushstring(lua, file->name); return 1; } diff --git a/core/src/xmake/io/file_close___gc.c b/core/src/xmake/io/file_close___gc.c index 3ff136f6a..1c59ff3ea 100644 --- a/core/src/xmake/io/file_close___gc.c +++ b/core/src/xmake/io/file_close___gc.c @@ -40,7 +40,8 @@ static tb_int_t xm_io_file_close_impl(lua_State* lua, tb_bool_t allow_closed_fil // check tb_assert_and_check_return_val(lua, 0); - xm_io_file* file = xm_io_getfile(lua); + // close file + xm_io_file_t* file = xm_io_getfile(lua); if (xm_io_file_is_closed(file)) { if (allow_closed_file) diff --git a/core/src/xmake/io/file_flush.c b/core/src/xmake/io/file_flush.c index ccab16310..e63a0952a 100644 --- a/core/src/xmake/io/file_flush.c +++ b/core/src/xmake/io/file_flush.c @@ -34,13 +34,13 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_bool_t xm_io_std_flush_impl(xm_io_file* file) +static tb_bool_t xm_io_std_flush_impl(xm_io_file_t* file) { tb_assert_and_check_return_val(xm_io_file_is_std(file) && !xm_io_file_is_closed(file), tb_false); return (file->std_ref != tb_stdfile_input())? tb_stdfile_flush(file->std_ref) : tb_false; } -static tb_bool_t xm_io_file_flush_impl(xm_io_file* file) +static tb_bool_t xm_io_file_flush_impl(xm_io_file_t* file) { // check tb_assert_and_check_return_val(xm_io_file_is_file(file) && !xm_io_file_is_closed(file), tb_false); @@ -71,7 +71,7 @@ tb_int_t xm_io_file_flush(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // file has been closed? - xm_io_file* file = xm_io_getfile(lua); + xm_io_file_t* file = xm_io_getfile(lua); if (xm_io_file_is_closed(file)) xm_io_file_return_error_closed(lua); diff --git a/core/src/xmake/io/file_isatty.c b/core/src/xmake/io/file_isatty.c index e09ea9b13..ca44ce0a0 100644 --- a/core/src/xmake/io/file_isatty.c +++ b/core/src/xmake/io/file_isatty.c @@ -43,7 +43,7 @@ tb_int_t xm_io_file_isatty(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // is tty? - xm_io_file* file = xm_io_getfile(lua); + xm_io_file_t* file = xm_io_getfile(lua); lua_pushboolean(lua, xm_io_file_is_tty(file)); return 1; } diff --git a/core/src/xmake/io/file_path.c b/core/src/xmake/io/file_path.c index 92ecfa3c5..e4221b6b9 100644 --- a/core/src/xmake/io/file_path.c +++ b/core/src/xmake/io/file_path.c @@ -43,7 +43,7 @@ tb_int_t xm_io_file_path(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // this file has been closed? - xm_io_file* file = xm_io_getfile(lua); + xm_io_file_t* file = xm_io_getfile(lua); if (xm_io_file_is_closed(file)) xm_io_file_return_error_closed(lua); else diff --git a/core/src/xmake/io/file_read.c b/core/src/xmake/io/file_read.c index 7cfae3a93..4e264d87a 100644 --- a/core/src/xmake/io/file_read.c +++ b/core/src/xmake/io/file_read.c @@ -96,7 +96,7 @@ static tb_long_t xm_io_file_buffer_readline(tb_stream_ref_t stream, tb_buffer_re if (linesize) return linesize; else return (eof || tb_stream_beof(stream))? -1 : 0; } -static tb_int_t xm_io_file_buffer_pushline(tb_buffer_ref_t buf, xm_io_file* file, tb_char_t const* continuation, tb_bool_t keep_crlf) +static tb_int_t xm_io_file_buffer_pushline(tb_buffer_ref_t buf, xm_io_file_t* file, tb_char_t const* continuation, tb_bool_t keep_crlf) { // check tb_assert(buf && file && continuation && xm_io_file_is_file(file) && !xm_io_file_is_closed(file)); @@ -175,7 +175,7 @@ static tb_int_t xm_io_file_buffer_pushline(tb_buffer_ref_t buf, xm_io_file* file // return result return result; } -static tb_int_t xm_io_file_read_all_directly(lua_State* lua, xm_io_file* file) +static tb_int_t xm_io_file_read_all_directly(lua_State* lua, xm_io_file_t* file) { // check tb_assert(lua && file && xm_io_file_is_file(file) && !xm_io_file_is_closed(file)); @@ -207,7 +207,7 @@ static tb_int_t xm_io_file_read_all_directly(lua_State* lua, xm_io_file* file) tb_buffer_exit(&buf); return 1; } -static tb_int_t xm_io_file_read_all(lua_State* lua, xm_io_file* file, tb_char_t const* continuation) +static tb_int_t xm_io_file_read_all(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation) { // check tb_assert(lua && file && continuation && xm_io_file_is_file(file) && !xm_io_file_is_closed(file)); @@ -246,7 +246,7 @@ static tb_int_t xm_io_file_read_all(lua_State* lua, xm_io_file* file, tb_char_t } } -static tb_int_t xm_io_file_read_line(lua_State* lua, xm_io_file* file, tb_char_t const* continuation, tb_bool_t keep_crlf) +static tb_int_t xm_io_file_read_line(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation, tb_bool_t keep_crlf) { // check tb_assert(lua && file && continuation && xm_io_file_is_file(file) && !xm_io_file_is_closed(file)); @@ -283,7 +283,7 @@ static tb_int_t xm_io_file_read_line(lua_State* lua, xm_io_file* file, tb_char_t } } -static tb_int_t xm_io_file_read_n(lua_State* lua, xm_io_file* file, tb_char_t const* continuation, tb_long_t n) +static tb_int_t xm_io_file_read_n(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation, tb_long_t n) { // check tb_assert(lua && file && continuation && xm_io_file_is_file(file) && !xm_io_file_is_closed(file)); @@ -322,7 +322,7 @@ static tb_int_t xm_io_file_read_n(lua_State* lua, xm_io_file* file, tb_char_t co return 1; } -static tb_size_t xm_io_file_std_buffer_pushline(tb_buffer_ref_t buf, xm_io_file* file, tb_char_t const* continuation, tb_bool_t keep_crlf) +static tb_size_t xm_io_file_std_buffer_pushline(tb_buffer_ref_t buf, xm_io_file_t* file, tb_char_t const* continuation, tb_bool_t keep_crlf) { // check tb_assert(buf && file && continuation && xm_io_file_is_std(file) && !xm_io_file_is_closed(file)); @@ -374,7 +374,7 @@ static tb_size_t xm_io_file_std_buffer_pushline(tb_buffer_ref_t buf, xm_io_file* return result; } -static tb_int_t xm_io_file_std_read_line(lua_State* lua, xm_io_file* file, tb_char_t const* continuation, tb_bool_t keep_crlf) +static tb_int_t xm_io_file_std_read_line(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation, tb_bool_t keep_crlf) { // check tb_assert(lua && file && continuation && xm_io_file_is_std(file) && !xm_io_file_is_closed(file)); @@ -411,7 +411,7 @@ static tb_int_t xm_io_file_std_read_line(lua_State* lua, xm_io_file* file, tb_ch } } -static tb_int_t xm_io_file_std_read_all(lua_State* lua, xm_io_file* file, tb_char_t const* continuation) +static tb_int_t xm_io_file_std_read_all(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation) { // check tb_assert(lua && file && continuation && xm_io_file_is_std(file) && !xm_io_file_is_closed(file)); @@ -445,7 +445,7 @@ static tb_int_t xm_io_file_std_read_all(lua_State* lua, xm_io_file* file, tb_cha } } -static tb_int_t xm_io_file_std_read_n(lua_State* lua, xm_io_file* file, tb_char_t const* continuation, tb_long_t n) +static tb_int_t xm_io_file_std_read_n(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation, tb_long_t n) { // check tb_assert(lua && file && continuation && xm_io_file_is_std(file) && !xm_io_file_is_closed(file)); @@ -476,7 +476,7 @@ static tb_int_t xm_io_file_std_read_n(lua_State* lua, xm_io_file* file, tb_char_ return 1; } -static tb_int_t xm_io_file_std_read_num(lua_State* lua, xm_io_file* file, tb_char_t const* continuation) +static tb_int_t xm_io_file_std_read_num(lua_State* lua, xm_io_file_t* file, tb_char_t const* continuation) { // check tb_assert(lua && file && continuation && xm_io_file_is_std(file) && !xm_io_file_is_closed(file)); @@ -505,7 +505,7 @@ tb_int_t xm_io_file_read(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // get file and arguments - xm_io_file* file = xm_io_getfile(lua); + xm_io_file_t* file = xm_io_getfile(lua); tb_char_t const* mode = luaL_optstring(lua, 2, "l"); tb_char_t const* continuation = luaL_optstring(lua, 3, ""); tb_assert_and_check_return_val(mode && continuation, 0); diff --git a/core/src/xmake/io/file_seek.c b/core/src/xmake/io/file_seek.c index 37588fe4f..54e0d4026 100644 --- a/core/src/xmake/io/file_seek.c +++ b/core/src/xmake/io/file_seek.c @@ -43,7 +43,7 @@ tb_int_t xm_io_file_seek(lua_State* lua) // check tb_assert_and_check_return_val(lua, 0); - xm_io_file* file = xm_io_getfile(lua); + xm_io_file_t* file = xm_io_getfile(lua); tb_char_t const* whence = luaL_optstring(lua, 2, "cur"); tb_hong_t offset = (tb_hong_t)luaL_optnumber(lua, 3, 0); tb_assert_and_check_return_val(file && whence, 0); diff --git a/core/src/xmake/io/file_write.c b/core/src/xmake/io/file_write.c index 553b91996..d0c371516 100644 --- a/core/src/xmake/io/file_write.c +++ b/core/src/xmake/io/file_write.c @@ -34,7 +34,7 @@ /* ////////////////////////////////////////////////////////////////////////////////////// * private implementation */ -static tb_void_t xm_io_file_write_file_directly(xm_io_file* file, tb_char_t const* data, tb_size_t size) +static tb_void_t xm_io_file_write_file_directly(xm_io_file_t* file, tb_char_t const* data, tb_size_t size) { // check tb_assert(file && data && xm_io_file_is_file(file) && !xm_io_file_is_closed(file)); @@ -42,7 +42,7 @@ static tb_void_t xm_io_file_write_file_directly(xm_io_file* file, tb_char_t cons // write data to file tb_stream_bwrit(file->file_ref, (tb_byte_t const*)data, size); } -static tb_void_t xm_io_file_write_file_transcrlf(xm_io_file* file, tb_char_t const* data, tb_size_t size) +static tb_void_t xm_io_file_write_file_transcrlf(xm_io_file_t* file, tb_char_t const* data, tb_size_t size) { // check tb_assert(file && data && xm_io_file_is_file(file) && !xm_io_file_is_closed(file)); @@ -92,7 +92,7 @@ static tb_void_t xm_io_file_write_file_transcrlf(xm_io_file* file, tb_char_t con return xm_io_file_write_file_directly(file, data, size); #endif } -static tb_void_t xm_io_file_write_std(xm_io_file* file, tb_char_t const* data, tb_size_t size) +static tb_void_t xm_io_file_write_std(xm_io_file_t* file, tb_char_t const* data, tb_size_t size) { // check tb_assert(file && data && xm_io_file_is_std(file) && !xm_io_file_is_closed(file)); @@ -114,7 +114,7 @@ tb_int_t xm_io_file_write(lua_State* lua) tb_assert_and_check_return_val(lua, 0); // get file - xm_io_file* file = xm_io_getfile(lua); + xm_io_file_t* file = xm_io_getfile(lua); tb_int_t narg = lua_gettop(lua); // this file has been closed? diff --git a/core/src/xmake/io/filelock.h b/core/src/xmake/io/filelock.h index 562926a4a..128f51aab 100644 --- a/core/src/xmake/io/filelock.h +++ b/core/src/xmake/io/filelock.h @@ -41,6 +41,9 @@ typedef struct __xm_io_filelock_t // the lock reference tb_filelock_ref_t lock_ref; + // is opened + tb_bool_t is_opened; + } xm_io_filelock_t; /* ////////////////////////////////////////////////////////////////////////////////////// @@ -56,7 +59,8 @@ 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->lock_ref = tb_null; + lock->is_opened = tb_false; // bind io._filelock metatable luaL_getmetatable(lua, xm_io_filelock_udata); diff --git a/core/src/xmake/io/filelock_close___gc.c b/core/src/xmake/io/filelock_close___gc.c new file mode 100644 index 000000000..3e0539dad --- /dev/null +++ b/core/src/xmake/io/filelock_close___gc.c @@ -0,0 +1,90 @@ +/*!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_close___gc.c + * + */ + +/* ////////////////////////////////////////////////////////////////////////////////////// + * trace + */ +#define TB_TRACE_MODULE_NAME "filelock_close___gc" +#define TB_TRACE_MODULE_DEBUG (0) + +/* ////////////////////////////////////////////////////////////////////////////////////// + * includes + */ +#include "filelock.h" + +/* ////////////////////////////////////////////////////////////////////////////////////// + * implementation + */ + +static tb_int_t xm_io_filelock_close_impl(lua_State* lua, tb_bool_t allow_closed_lock) +{ + // check + tb_assert_and_check_return_val(lua, 0); + + // close lock + xm_io_filelock_t* lock = xm_io_get_filelock(lua); + if (!lock->is_opened) + { + if (allow_closed_lock) + { + lua_pushboolean(lua, tb_true); + return 1; + } + else + { + lua_pushnil(lua); + lua_pushliteral(lua, "error: file lock has been closed"); + return 2; + } + } + + // check + tb_assert(lock->lock_ref); + + // close lock + tb_filelock_exit(lock->lock_ref); + lock->lock_ref = tb_null; + lock->is_opened = tb_false; + + // close ok + lua_pushboolean(lua, tb_true); + return 1; +} + +/* ////////////////////////////////////////////////////////////////////////////////////// + * interfaces + */ + +/* + * lock:close() + */ +tb_int_t xm_io_filelock_close(lua_State* lua) +{ + return xm_io_filelock_close_impl(lua, tb_false); +} + +/* + * lock:close() + */ +tb_int_t xm_io_filelock___gc(lua_State* lua) +{ + return xm_io_filelock_close_impl(lua, tb_true); +} diff --git a/core/src/xmake/io/open.c b/core/src/xmake/io/open.c index 4ca820371..b71daf7df 100644 --- a/core/src/xmake/io/open.c +++ b/core/src/xmake/io/open.c @@ -285,7 +285,7 @@ tb_int_t xm_io_open(lua_State* lua) tb_assert_and_check_return_val(path, 0); // new file - xm_io_file* xm_file = xm_io_newfile(lua); + xm_io_file_t* xm_file = xm_io_newfile(lua); xm_file->file_ref = file_ref; xm_file->stream = stream; xm_file->fstream = fstream; diff --git a/core/src/xmake/io/openlock.c b/core/src/xmake/io/openlock.c index 4475869fb..6ba2da76d 100644 --- a/core/src/xmake/io/openlock.c +++ b/core/src/xmake/io/openlock.c @@ -51,12 +51,14 @@ tb_int_t xm_io_openlock(lua_State* lua) if (lock) { xm_io_filelock_t* xmlock = xm_io_new_filelock(lua); - xmlock->lock_ref = lock; + xmlock->lock_ref = lock; + xmlock->is_opened = tb_true; + return 1; } else { lua_pushnil(lua); lua_pushliteral(lua, "cannot open file lock!"); + return 2; } - return 1; } diff --git a/core/src/xmake/io/std.c b/core/src/xmake/io/std.c index 4ee446d85..2ed46a341 100644 --- a/core/src/xmake/io/std.c +++ b/core/src/xmake/io/std.c @@ -102,7 +102,7 @@ static tb_void_t xm_io_std_init(lua_State* lua, tb_size_t type) tb_assert_and_check_return(name && path && fp); // new file - xm_io_file* file = xm_io_newfile(lua); + xm_io_file_t* file = xm_io_newfile(lua); tb_assert_and_check_return(file); lua_setfield(lua, -2, name); diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c index 0e0a34964..6ce258254 100644 --- a/core/src/xmake/machine.c +++ b/core/src/xmake/machine.c @@ -106,6 +106,10 @@ tb_int_t xm_io_file___len(lua_State* lua); 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_close(lua_State* lua); +tb_int_t xm_io_filelock___gc(lua_State* lua); + // the path functions tb_int_t xm_path_relative(lua_State* lua); tb_int_t xm_path_absolute(lua_State* lua); @@ -246,7 +250,9 @@ static luaL_Reg const g_io_file_functions[] = // the filelock functions static luaL_Reg const g_io_filelock_functions[] = { - { tb_null, tb_null } + { "close", xm_io_filelock_close } +, { "__gc", xm_io_filelock___gc } +, { tb_null, tb_null } }; // the path functions diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile index 4eb460c70..e76376a72 100644 --- a/core/src/xmake/makefile +++ b/core/src/xmake/makefile @@ -53,6 +53,7 @@ xmake_C_FILES += \ io/file_read \ io/file_seek \ io/file_write \ + io/filelock_close___gc \ io/open \ io/openlock \ io/std \ |
