[patches] [fio 1/1] fio: Read with empty len parameter
imarkov
imarkov at tarantool.org
Wed Jan 31 15:31:04 MSK 2018
From: IlyaMarkovMipt <markovilya197 at gmail.com>
* Add possibility to use file:read without len parameter.
In this case, whole file will be read.
Closes #2925
Signed-off-by: imarkov <imarkov at tarantool.org>
---
src/lua/fio.lua | 13 +++++++++++--
test/app/fio.result | 19 +++++++++++++++++++
test/app/fio.test.lua | 5 +++++
3 files changed, 35 insertions(+), 2 deletions(-)
diff --git a/src/lua/fio.lua b/src/lua/fio.lua
index e3e7a78..d26b034 100644
--- a/src/lua/fio.lua
+++ b/src/lua/fio.lua
@@ -24,12 +24,22 @@ end
local fio_methods = {}
+-- read() -> str
+-- read(buf) -> len
-- read(size) -> str
-- read(buf, size) -> len
fio_methods.read = function(self, buf, size)
local tmpbuf
+ if (not ffi.istype(const_char_ptr_t, buf) and buf == nil) or
+ (ffi.istype(const_char_ptr_t, buf) and size == nil) then
+ local st = self:stat()
+ if st == nil then
+ return 0
+ end
+ size = st.size
+ end
if not ffi.istype(const_char_ptr_t, buf) then
- size = buf
+ size = buf or size
tmpbuf = buffer.IBUF_SHARED
tmpbuf:reset()
buf = tmpbuf:reserve(size)
@@ -149,7 +159,6 @@ fio_methods.stat = function(self)
return internal.fstat(self.fh)
end
-
local fio_mt = { __index = fio_methods }
fio.open = function(path, flags, mode)
diff --git a/test/app/fio.result b/test/app/fio.result
index 4b55da6..d96581f 100644
--- a/test/app/fio.result
+++ b/test/app/fio.result
@@ -788,6 +788,25 @@ fh:seek(0)
---
- 0
...
+fh:read()
+---
+- helloworld!
+...
+fh:seek(0)
+---
+- 0
+...
+len = fh:read(buf:reserve(12))
+---
+...
+ffi.string(buf:alloc(len), len)
+---
+- helloworld!
+...
+fh:seek(0)
+---
+- 0
+...
len = fh:read(buf:reserve(5), 5)
---
...
diff --git a/test/app/fio.test.lua b/test/app/fio.test.lua
index bef9448..4d374a1 100644
--- a/test/app/fio.test.lua
+++ b/test/app/fio.test.lua
@@ -254,6 +254,11 @@ fh = fio.open(tmpfile, { 'O_RDWR', 'O_TRUNC', 'O_CREAT' }, 0777)
fh:write('helloworld!')
fh:seek(0)
+fh:read()
+fh:seek(0)
+len = fh:read(buf:reserve(12))
+ffi.string(buf:alloc(len), len)
+fh:seek(0)
len = fh:read(buf:reserve(5), 5)
ffi.string(buf:alloc(len), len)
--
2.7.4
More information about the Tarantool-patches
mailing list