Skip to content

Commit

Permalink
use bytes objects for read and write data
Browse files Browse the repository at this point in the history
This changes the data type for the buffer of the read and write syscalls
from string to bytes. On Python 2, this has no effect. On Python 3, it
is a breaking change, but fixes a serious usibility bug that limits file
data to valid UTF-8 data. With these changes, files can contain arbitrary
data.
  • Loading branch information
dlech committed Mar 14, 2019
1 parent 2c088b6 commit f81a45e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
4 changes: 2 additions & 2 deletions example/fioc.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class FiocFS(Fuse):

def __init__(self, *args, **kw):
Fuse.__init__(self, *args, **kw)
self.buf = ""
self.buf = b""

def resize(self, new_size):
old_size = len(self.buf)
Expand All @@ -106,7 +106,7 @@ def resize(self, new_size):
if new_size < old_size:
self.buf = self.buf[0:new_size]
else:
self.buf = self.buf + "\x00" * (new_size - old_size)
self.buf = self.buf + b"\x00" * (new_size - old_size)

return 0

Expand Down
4 changes: 2 additions & 2 deletions example/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
fuse.fuse_python_api = (0, 2)

hello_path = '/hello'
hello_str = 'Hello World!\n'
hello_str = b'Hello World!\n'

class MyStat(fuse.Stat):
def __init__(self):
Expand Down Expand Up @@ -72,7 +72,7 @@ def read(self, path, size, offset):
size = slen - offset
buf = hello_str[offset:offset+size]
else:
buf = ''
buf = b''
return buf

def main():
Expand Down
2 changes: 1 addition & 1 deletion example/xmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


def flag2mode(flags):
md = {os.O_RDONLY: 'r', os.O_WRONLY: 'w', os.O_RDWR: 'w+'}
md = {os.O_RDONLY: 'rb', os.O_WRONLY: 'wb', os.O_RDWR: 'wb+'}
m = md[flags & (os.O_RDONLY | os.O_WRONLY | os.O_RDWR)]

if flags | os.O_APPEND:
Expand Down
14 changes: 14 additions & 0 deletions fuseparts/_fusemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -517,12 +517,22 @@ read_func(const char *path, char *buf, size_t s, off_t off)
PROLOGUE( PYO_CALLWITHFI(fi, read_cb, snK, path, s, off) )
#endif


#if PY_MAJOR_VERSION >= 3
if(PyBytes_Check(v)) {
if(PyBytes_Size(v) > s)
goto OUT_DECREF;
memcpy(buf, PyBytes_AsString(v), PyBytes_Size(v));
ret = PyBytes_Size(v);
}
#else
if(PyString_Check(v)) {
if(PyString_Size(v) > s)
goto OUT_DECREF;
memcpy(buf, PyString_AsString(v), PyString_Size(v));
ret = PyString_Size(v);
}
#endif

EPILOGUE
}
Expand All @@ -536,7 +546,11 @@ static int
write_func(const char *path, const char *buf, size_t t, off_t off)
#endif
{
#if PY_MAJOR_VERSION >= 3
PROLOGUE( PYO_CALLWITHFI(fi, write_cb, sy#K, path, buf, t, off) )
#else
PROLOGUE( PYO_CALLWITHFI(fi, write_cb, ss#K, path, buf, t, off) )
#endif
EPILOGUE
}

Expand Down

0 comments on commit f81a45e

Please sign in to comment.