summaryrefslogtreecommitdiff
path: root/xmake/core/base/pipe.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2022-04-06 22:40:37 +0800
committerruki <[email protected]>2022-04-06 22:40:37 +0800
commit90ad0edfa8645b5b7044db79090ad90ac22a85a2 (patch)
tree0f24a012796c0235d4ce7b26376c2483e5f1030d /xmake/core/base/pipe.lua
parentcc8c6dee305ab37f2ba18bbd4d04cc62ad0a9f2e (diff)
improve pipe read
Diffstat (limited to 'xmake/core/base/pipe.lua')
-rw-r--r--xmake/core/base/pipe.lua31
1 files changed, 8 insertions, 23 deletions
diff --git a/xmake/core/base/pipe.lua b/xmake/core/base/pipe.lua
index 92ad42bc6..bb6908cde 100644
--- a/xmake/core/base/pipe.lua
+++ b/xmake/core/base/pipe.lua
@@ -118,7 +118,7 @@ function _instance:write(data, opt)
end
-- read data from pipe
-function _instance:read(size, opt)
+function _instance:read(buff, size, opt)
-- ensure opened
local ok, errors = self:_ensure_opened()
@@ -126,6 +126,11 @@ function _instance:read(size, opt)
return -1, errors
end
+ -- check buffer
+ if not buff and buff:size() < size then
+ return -1, string.format("%s: too small buffer!", self)
+ end
+
-- check size
if size == 0 then
return 0
@@ -141,12 +146,9 @@ function _instance:read(size, opt)
if opt.block then
local results = {}
while read < size do
- local buff = self:_readbuff()
- real, data_or_errors = io.pipe_read(self:cdata(), buff:caddr(), math.min(buff:size(), size - read))
+ real, data_or_errors = io.pipe_read(self:cdata(), buff:caddr() + read, math.min(buff:size() - read, size - read))
if real > 0 then
read = read + real
- table.insert(results, bytes(buff, 1, real))
- self:_readbuff_clear()
elseif real == 0 then
local events, waiterrs = _instance.wait(self, pipe.EV_READ, opt.timeout or -1)
if events ~= pipe.EV_READ then
@@ -158,16 +160,14 @@ function _instance:read(size, opt)
end
end
if read == size then
- data_or_errors = bytes(results)
+ data_or_errors = bytes(buff, 1, read)
else
read = -1
end
else
- local buff = self:_readbuff()
read, data_or_errors = io.pipe_read(self:cdata(), buff:caddr(), math.min(buff:size(), size))
if read > 0 then
data_or_errors = bytes(buff, 1, read)
- self:_readbuff_clear()
end
end
if read < 0 and data_or_errors then
@@ -255,21 +255,6 @@ function _instance:close()
return ok
end
--- get the read buffer
-function _instance:_readbuff()
- local readbuff = self._READBUFF
- if not readbuff then
- readbuff = bytes(8192)
- self._READBUFF = readbuff
- end
- return readbuff
-end
-
--- clear the read buffer
-function _instance:_readbuff_clear()
- self._READBUFF = nil
-end
-
-- ensure the pipe is opened
function _instance:_ensure_opened()
if not self:cdata() then