summaryrefslogtreecommitdiff
path: root/core/src
diff options
context:
space:
mode:
authorruki <[email protected]>2019-08-20 22:40:06 +0800
committerruki <[email protected]>2019-08-20 10:00:39 +0800
commit0a691680dd292e59fcd63e9710b7cdeaf6a52ce7 (patch)
treee389f8de5007e286ca0cafa91d0d85b1e95c2068 /core/src
parent0970525b07224b26c1b10b5a771fec2715cac7c0 (diff)
get rawfd from io.file
Diffstat (limited to 'core/src')
m---------core/src/tbox/tbox0
-rw-r--r--core/src/xmake/io/file_rawfd.c80
-rw-r--r--core/src/xmake/io/file_size.c2
-rw-r--r--core/src/xmake/machine.c2
-rw-r--r--core/src/xmake/makefile1
5 files changed, 84 insertions, 1 deletions
diff --git a/core/src/tbox/tbox b/core/src/tbox/tbox
-Subproject 0cdb41ee2f944466e4d69219922f2feb5b76151
+Subproject f9d6c3eefcb40c50ed8fca38cb54d745ec5500b
diff --git a/core/src/xmake/io/file_rawfd.c b/core/src/xmake/io/file_rawfd.c
new file mode 100644
index 000000000..a46224472
--- /dev/null
+++ b/core/src/xmake/io/file_rawfd.c
@@ -0,0 +1,80 @@
+/*!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 file_rawfd.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "file_rawfd"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * macros
+ */
+
+// file to fd
+#ifdef TB_CONFIG_OS_WINDOWS
+# define xm_io_file2fd(file) (lua_Number)((tb_size_t)(file))
+#else
+# define xm_io_file2fd(file) (lua_Number)tb_file2fd(file)
+#endif
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+/* io.file_rawfd(file)
+ *
+ * @note this interface is very dangerous and is only used in some special/hacking cases.
+ *
+ * e.g. set VS_UNICODE_OUTPUT=fd to enable vs unicode output, @see https://github.com/xmake-io/xmake/issues/528
+ */
+tb_int_t xm_io_file_rawfd(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // is user data?
+ if (!lua_isuserdata(lua, 1))
+ xm_io_file_return_error(lua, "get rawfd for invalid file!");
+
+ // get file
+ xm_io_file_t* file = (xm_io_file_t*)lua_touserdata(lua, 1);
+ tb_check_return_val(file, 0);
+
+ // get file raw fd
+ if (xm_io_file_is_file(file))
+ {
+ tb_file_ref_t file = tb_null;
+ if (tb_stream_ctrl(file->stream, TB_STREAM_CTRL_FILE_GET_FILE, &file))
+ {
+ lua_pushnumber(lua, xm_io_file2fd(file));
+ return 1;
+ }
+ }
+
+ // get rawfd failed
+ xm_io_file_return_error(lua, "get rawfd for invalid file!");
+}
diff --git a/core/src/xmake/io/file_size.c b/core/src/xmake/io/file_size.c
index f8f126bbf..4e502bbfb 100644
--- a/core/src/xmake/io/file_size.c
+++ b/core/src/xmake/io/file_size.c
@@ -56,5 +56,5 @@ tb_int_t xm_io_file_size(lua_State* lua)
lua_pushnumber(lua, (lua_Number)tb_stream_size(file->stream));
return 1;
}
- else xm_io_file_return_error(lua, "getting file size for this file is invalid");
+ else xm_io_file_return_error(lua, "get size for invalid file!");
}
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index 86f1018c2..d9dc111c3 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -95,6 +95,7 @@ tb_int_t xm_io_file_open(lua_State* lua);
tb_int_t xm_io_file_read(lua_State* lua);
tb_int_t xm_io_file_seek(lua_State* lua);
tb_int_t xm_io_file_size(lua_State* lua);
+tb_int_t xm_io_file_rawfd(lua_State* lua);
tb_int_t xm_io_file_write(lua_State* lua);
tb_int_t xm_io_file_flush(lua_State* lua);
tb_int_t xm_io_file_close(lua_State* lua);
@@ -232,6 +233,7 @@ static luaL_Reg const g_io_functions[] =
, { "file_flush", xm_io_file_flush }
, { "file_isatty", xm_io_file_isatty }
, { "file_close", xm_io_file_close }
+, { "file_rawfd", xm_io_file_rawfd }
, { "filelock_open", xm_io_filelock_open }
, { "filelock_lock", xm_io_filelock_lock }
, { "filelock_trylock", xm_io_filelock_trylock }
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index 6d0f02117..ae9ebbbe4 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/file_rawfd \
io/filelock_open \
io/filelock_lock \
io/filelock_unlock \