summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-05-11 22:23:51 +0800
committerruki <[email protected]>2022-05-11 22:23:51 +0800
commit505f48ad7fc2de2bf837988868aca881843be3c9 (patch)
treefdab3fab977d163a82052cb5a409b6d4ce9846f5
parentcd39302a4f679a14dd7fd8cd57ce32a6379a04d2 (diff)
improve to recv compressed file
-rw-r--r--xmake/core/compress/lz4.lua4
-rw-r--r--xmake/modules/private/service/stream.lua42
2 files changed, 37 insertions, 9 deletions
diff --git a/xmake/core/compress/lz4.lua b/xmake/core/compress/lz4.lua
index b109b1804..6a5c97be0 100644
--- a/xmake/core/compress/lz4.lua
+++ b/xmake/core/compress/lz4.lua
@@ -201,7 +201,7 @@ function _dstream:read(buff, size, opt)
end
-- read it
- local read, data_or_errors = lz4._compress_stream_read(self:cdata(), buff:caddr() + pos, math.min(buff:size() - pos, size))
+ local read, data_or_errors = lz4._decompress_stream_read(self:cdata(), buff:caddr() + pos, math.min(buff:size() - pos, size))
if read > 0 then
data_or_errors = buff:slice(start, read)
end
@@ -240,7 +240,7 @@ function _dstream:write(data, opt)
-- write it
local errors = nil
- local write, errors = lz4._compress_stream_write(self:cdata(), dataaddr + start - 1, last + 1 - start, opt.beof)
+ local write, errors = lz4._decompress_stream_write(self:cdata(), dataaddr + start - 1, last + 1 - start, opt.beof)
if write < 0 and errors then
errors = string.format("%s: %s", self, errors)
end
diff --git a/xmake/modules/private/service/stream.lua b/xmake/modules/private/service/stream.lua
index 221577ad6..8cb802ab8 100644
--- a/xmake/modules/private/service/stream.lua
+++ b/xmake/modules/private/service/stream.lua
@@ -328,10 +328,8 @@ end
function stream:recv_file(filepath)
local size, flags = self:recv_header()
if size then
- local dstfile
if bit.band(flags, HEADER_FLAG_COMPRESS_LZ4) == HEADER_FLAG_COMPRESS_LZ4 then
- dstfile = filepath
- filepath = os.tmpfile()
+ return self:_recv_compressed_file(lz4.decompress_stream(), filepath, size)
end
local buff = self._BUFF
local recv = 0
@@ -345,15 +343,45 @@ function stream:recv_file(filepath)
end
file:close()
if recv == size then
- if dstfile then
- lz4.decompress_file(filepath, dstfile)
- os.tryrm(filepath)
- end
return true
end
end
end
+-- recv compressed file
+function stream:_recv_compressed_file(lz4_stream, filepath, size)
+ local buff = self._BUFF
+ local recv = 0
+ local file = io.open(filepath, "wb")
+ while recv < size do
+ local data = self:recv(buff, math.min(buff:size(), size - recv))
+ if data then
+ local write = 0
+ local writesize = data:size()
+ while write < writesize do
+ local blocksize = math.min(writesize - write, 8192)
+ local real = lz4_stream:write(data, {start = write + 1, last = write + blocksize})
+ if real > 0 then
+ while true do
+ local decompress_size, decompressed_data = lz4_stream:read(buff, 8192)
+ if decompress_size > 0 and decompressed_data then
+ file:write(decompressed_data)
+ else
+ break
+ end
+ end
+ end
+ write = write + blocksize
+ end
+ recv = recv + data:size()
+ end
+ end
+ file:close()
+ if recv == size then
+ return true
+ end
+end
+
function main(sock)
local instance = stream()
instance:init(sock)