summaryrefslogtreecommitdiffstats
path: root/gluster
diff options
context:
space:
mode:
authorThiago da Silva <thiago@redhat.com>2014-02-21 18:23:29 -0500
committerGerrit Code Review <review@dev.gluster.org>2014-02-24 19:25:23 -0800
commit4185c26e9f6ff6f5395c326e4fb71f60b8d1656e (patch)
tree6785031207a1052fddf1b1a19ce41668e8ae6492 /gluster
parentf51edb0687dba2d6f6b07f8697c91f2df2f4cdda (diff)
changed write and read functions to support binary data
both functions were only supporting text data and needed to be changed to support binary data. The issue was found while testing libgfapi-python with smallfile (https://github.com/bengland2/smallfile). When calling gflfs_write, ctypes has no problem converting strings to the correct C data type, but is unable to handle bytearray: b = bytearray(1024) with vol.create(path, os.O_WRONLY | os.O_EXCL, 0644) as fd: fd.write(b) It would throw this error: exception: argument 2: <type 'exceptions.TypeError'>: Don't know how to convert parameter 2 reference: http://docs.python.org/2/library/ctypes.html#calling-functions-continued Change-Id: Ia2bb47343880cbf7121fed9510e4cfa085fe23bd Signed-off-by: Thiago da Silva <thiago@redhat.com>
Diffstat (limited to 'gluster')
-rw-r--r--gluster/gfapi.py12
1 files changed, 9 insertions, 3 deletions
diff --git a/gluster/gfapi.py b/gluster/gfapi.py
index 7d09102..b1f14d2 100644
--- a/gluster/gfapi.py
+++ b/gluster/gfapi.py
@@ -101,15 +101,21 @@ class File(object):
rbuf = ctypes.create_string_buffer(buflen)
ret = api.glfs_read(self.fd, rbuf, buflen, flags)
if ret > 0:
- return rbuf.value[:ret]
+ return rbuf
elif ret < 0:
err = ctypes.get_errno()
raise OSError(err, os.strerror(err))
else:
return ret
- def write(self, data, flags=0):
- ret = api.glfs_write(self.fd, data, len(data), flags)
+ def write(self, data):
+ # creating a ctypes.c_ubyte buffer to handle converting bytearray
+ # to the required C data type
+ if type(data) is bytearray:
+ buf = (ctypes.c_ubyte * len(data)).from_buffer(data)
+ else:
+ buf = data
+ ret = api.glfs_write(self.fd, buf, len(buf))
if ret < 0:
err = ctypes.get_errno()
raise OSError(err, os.strerror(err))