[patches] [fio 1/1] fio: Read with empty len parameter

imarkov imarkov at tarantool.org
Wed Jan 31 16:26:11 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   | 42 ++++++++++++++++++++++++++++++++++++++++++
 test/app/fio.test.lua | 13 ++++++++++++-
 3 files changed, 65 insertions(+), 3 deletions(-)

diff --git a/src/lua/fio.lua b/src/lua/fio.lua
index e3e7a78..714d846 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, err = self:stat()
+        if st == nil then
+            return nil, err
+        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..dcf168b 100644
--- a/test/app/fio.result
+++ b/test/app/fio.result
@@ -774,6 +774,37 @@ buf = buffer.ibuf()
 tmpdir = fio.tempdir()
 ---
 ...
+tmpfile = fio.pathjoin(tmpdir, "test1")
+---
+...
+fh = fio.open(tmpfile, { 'O_RDWR', 'O_TRUNC', 'O_CREAT' }, 0777)
+---
+...
+fh:write('helloworld!')
+---
+- true
+...
+fh:seek(0)
+---
+- 0
+...
+fh:read()
+---
+- helloworld!
+...
+fh:close()
+---
+- true
+...
+fh:read()
+---
+- null
+- 'fio: Bad file descriptor'
+...
+fio.unlink(tmpfile)
+---
+- true
+...
 tmpfile = fio.pathjoin(tmpdir, "test")
 ---
 ...
@@ -788,6 +819,17 @@ 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..fbfcf78 100644
--- a/test/app/fio.test.lua
+++ b/test/app/fio.test.lua
@@ -249,11 +249,22 @@ fio.copytree("/no/such/dir", "/some/where")
 buf = buffer.ibuf()
 
 tmpdir = fio.tempdir()
-tmpfile = fio.pathjoin(tmpdir, "test")
+tmpfile = fio.pathjoin(tmpdir, "test1")
 fh = fio.open(tmpfile, { 'O_RDWR', 'O_TRUNC', 'O_CREAT' }, 0777)
+fh:write('helloworld!')
+fh:seek(0)
+fh:read()
+fh:close()
+fh:read()
+fio.unlink(tmpfile)
 
+tmpfile = fio.pathjoin(tmpdir, "test")
+fh = fio.open(tmpfile, { 'O_RDWR', 'O_TRUNC', 'O_CREAT' }, 0777)
 fh:write('helloworld!')
 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