summaryrefslogtreecommitdiffstats
path: root/gluster
diff options
context:
space:
mode:
authorPrashanth Pai <ppai@redhat.com>2015-06-23 20:04:30 +0530
committerPrashanth Pai <ppai@redhat.com>2016-02-24 11:16:57 +0530
commit14c16992b563a77330478bcc6fecdb54df4300b5 (patch)
treea21195304749786c8564fb4339ac7c3120fbb9d1 /gluster
parent6df97fd49fa9be6394bd066c6c64fd7c06959a77 (diff)
Add readinto() API
readinto() This method is useful when you have to read a large file over multiple read calls. While read() allocates a buffer every time it's invoked, readinto() copies data to an already allocated buffer passed to it. Change-Id: Ic8a3aa0e544e09e05101c983b329c91864832e4a Signed-off-by: Prashanth Pai <ppai@redhat.com>
Diffstat (limited to 'gluster')
-rwxr-xr-xgluster/gfapi.py23
1 files changed, 23 insertions, 0 deletions
diff --git a/gluster/gfapi.py b/gluster/gfapi.py
index fa0e1b3..d6f847d 100755
--- a/gluster/gfapi.py
+++ b/gluster/gfapi.py
@@ -328,6 +328,29 @@ class File(object):
err = ctypes.get_errno()
raise OSError(err, os.strerror(err))
+ def readinto(self, buf):
+ """
+ Read up to len(buf) bytes into buf which must be a bytearray.
+ (buf cannot be a string as strings are immutable in python)
+
+ This method is useful when you have to read a large file over
+ multiple read calls. While read() allocates a buffer every time
+ it's invoked, readinto() copies data to an already allocated
+ buffer passed to it.
+
+ Returns the number of bytes read (0 for EOF).
+ """
+ if type(buf) is bytearray:
+ buf_ptr = (ctypes.c_ubyte * len(buf)).from_buffer(buf)
+ else:
+ # TODO: Allow reading other types such as array.array
+ raise TypeError("buffer must of type bytearray")
+ nread = api.glfs_read(self.fd, buf_ptr, len(buf_ptr), 0)
+ if nread < 0:
+ err = ctypes.get_errno()
+ raise OSError(err, os.strerror(err))
+ return nread
+
def write(self, data, flags=0):
"""
Write data to the file.