summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-16 16:35:21 +0800
committerruki <[email protected]>2022-04-16 16:35:21 +0800
commit983e8ed319b4e0d84a47d43bcaa3a154e7c74175 (patch)
tree0d97437585a0c70eaddacee3bf8ec230d8f565a0
parentc72bd4e4c7727efb224085183fd15b965c023cbd (diff)
improve write file
-rw-r--r--core/src/xmake/io/file_write.c36
-rw-r--r--xmake/core/base/io.lua11
-rw-r--r--xmake/modules/private/service/remote_build/client.lua2
-rw-r--r--xmake/modules/private/service/remote_build/session.lua9
-rw-r--r--xmake/modules/private/service/stream.lua51
5 files changed, 98 insertions, 11 deletions
diff --git a/core/src/xmake/io/file_write.c b/core/src/xmake/io/file_write.c
index d2d150353..a374057a4 100644
--- a/core/src/xmake/io/file_write.c
+++ b/core/src/xmake/io/file_write.c
@@ -63,15 +63,12 @@ static tb_void_t xm_io_file_write_file_utfbom(xm_io_file_t* file)
break;
}
}
-static tb_void_t xm_io_file_write_file_directly(xm_io_file_t* 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_byte_t const* data, tb_size_t size)
{
- // check
tb_assert(file && data && xm_io_file_is_file(file) && file->file_ref);
-
- // write data to file
- tb_stream_bwrit(file->file_ref, (tb_byte_t const*)data, size);
+ tb_stream_bwrit(file->file_ref, data, 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)
+static tb_void_t xm_io_file_write_file_transcrlf(xm_io_file_t* file, tb_byte_t const* data, tb_size_t size)
{
// check
tb_assert(file && data && xm_io_file_is_file(file) && file->file_ref);
@@ -121,7 +118,7 @@ static tb_void_t xm_io_file_write_file_transcrlf(xm_io_file_t* file, tb_char_t c
return xm_io_file_write_file_directly(file, data, size);
#endif
}
-static tb_void_t xm_io_file_write_std(xm_io_file_t* 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_byte_t const* data, tb_size_t size)
{
// check
tb_assert(file && data && xm_io_file_is_std(file));
@@ -131,7 +128,7 @@ static tb_void_t xm_io_file_write_std(xm_io_file_t* file, tb_char_t const* data,
tb_check_return(type != XM_IO_FILE_TYPE_STDIN);
// write data to stdout/stderr
- tb_stdfile_writ(file->std_ref, (tb_byte_t const*)data, size);
+ tb_stdfile_writ(file->std_ref, data, size);
}
/* //////////////////////////////////////////////////////////////////////////////////////
@@ -161,7 +158,28 @@ tb_int_t xm_io_file_write(lua_State* lua)
{
// get data
size_t datasize = 0;
- tb_char_t const* data = luaL_checklstring(lua, i, &datasize);
+ tb_byte_t const* data = tb_null;
+ if (lua_isstring(lua, i))
+ data = (tb_byte_t const*)luaL_checklstring(lua, i, &datasize);
+ else if (lua_istable(lua, i))
+ {
+ // get bytes data
+ lua_pushstring(lua, "data");
+ lua_gettable(lua, i);
+ if (lua_isnumber(lua, -1))
+ data = (tb_byte_t const*)(tb_size_t)(tb_long_t)lua_tonumber(lua, -1);
+ lua_pop(lua, 1);
+
+ lua_pushstring(lua, "size");
+ lua_gettable(lua, i);
+ if (lua_isnumber(lua, -1))
+ datasize = (tb_size_t)lua_tonumber(lua, -1);
+ lua_pop(lua, 1);
+
+ // mark as binary data
+ is_binary = tb_true;
+ }
+
tb_check_continue(datasize);
tb_assert_and_check_break(data);
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua
index 54d84522d..16b0efc46 100644
--- a/xmake/core/base/io.lua
+++ b/xmake/core/base/io.lua
@@ -201,8 +201,17 @@ function _file:write(...)
return false, errors
end
+ -- data items
+ local items = table.pack(...)
+ for idx, item in ipairs(items) do
+ if type(item) == "table" and item.caddr and item.size then
+ -- write bytes
+ items[idx] = {data = item:caddr(), size = item:size()}
+ end
+ end
+
-- write file
- ok, errors = io.file_write(self:cdata(), ...)
+ ok, errors = io.file_write(self:cdata(), table.unpack(items))
if not ok and errors then
errors = string.format("%s: %s", self, errors)
end
diff --git a/xmake/modules/private/service/remote_build/client.lua b/xmake/modules/private/service/remote_build/client.lua
index f60ee1d3d..10cc3bc02 100644
--- a/xmake/modules/private/service/remote_build/client.lua
+++ b/xmake/modules/private/service/remote_build/client.lua
@@ -398,7 +398,7 @@ function remote_build_client:_archive_diff_files(diff_files)
table.insert(filelist, fileitem)
end
archive_files(archivefile, filelist, {curdir = self:projectdir()})
- vprint("archive file ok, %s", archivefile)
+ vprint("archive file ok, size: %s", os.filesize(archivefile))
return archivefile
end
diff --git a/xmake/modules/private/service/remote_build/session.lua b/xmake/modules/private/service/remote_build/session.lua
index ff15a3d20..8f333f430 100644
--- a/xmake/modules/private/service/remote_build/session.lua
+++ b/xmake/modules/private/service/remote_build/session.lua
@@ -109,8 +109,17 @@ end
-- sync files
function session:sync(respmsg)
local body = respmsg:body()
+ local stream = self:stream()
local manifest = body.manifest
+ local archivefile = os.tmpfile() .. ".zip"
vprint("%s: sync files in %s ..", self, self:sourcedir())
+ if stream:recv_file(archivefile) then
+ vprint("receive archive file, size: %d", os.filesize(archivefile))
+ -- do sync
+ else
+ raise("receive files failed!")
+ end
+ --os.tryrm(archivefile)
vprint("%s: sync files ok", self)
end
diff --git a/xmake/modules/private/service/stream.lua b/xmake/modules/private/service/stream.lua
index 408b67dc3..5f9aa127b 100644
--- a/xmake/modules/private/service/stream.lua
+++ b/xmake/modules/private/service/stream.lua
@@ -121,6 +121,36 @@ function stream:send_string(str)
end
end
+-- send file
+function stream:send_file(filepath)
+
+ -- send size
+ local buff = self._BUFF
+ local size = os.filesize(filepath)
+ buff:u16be_set(1, size)
+ if not self:send(buff, 1, 2) then
+ return
+ end
+
+ -- flush cache data first
+ if not self:flush() then
+ return
+ end
+
+ -- send file
+ local ok = false
+ local sock = self._SOCK
+ local file = io.open(filepath, 'rb')
+ if file then
+ local send = sock:sendfile(file, {block = true})
+ if send > 0 then
+ ok = true
+ end
+ file:close()
+ end
+ return ok
+end
+
-- recv the given bytes
function stream:recv(buff, size)
assert(size <= buff:size())
@@ -220,6 +250,27 @@ function stream:recv_string()
end
end
+-- recv file
+function stream:recv_file(filepath)
+ local size = self:recv_u16be()
+ if size then
+ local buff = self._BUFF
+ local recv = 0
+ local file = io.open(filepath, "wb")
+ while recv < size do
+ local data = self:recv(buff, math.min(8192, size - recv))
+ if data then
+ file:write(data)
+ recv = recv + data:size()
+ end
+ end
+ file:close()
+ if recv == size then
+ return true
+ end
+ end
+end
+
function main(sock)
local instance = stream()
instance:init(sock)