Skip to content

Commit f81a45e

Browse files
committed
use bytes objects for read and write data
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.
1 parent 2c088b6 commit f81a45e

4 files changed

Lines changed: 19 additions & 5 deletions

File tree

example/fioc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class FiocFS(Fuse):
9696

9797
def __init__(self, *args, **kw):
9898
Fuse.__init__(self, *args, **kw)
99-
self.buf = ""
99+
self.buf = b""
100100

101101
def resize(self, new_size):
102102
old_size = len(self.buf)
@@ -106,7 +106,7 @@ def resize(self, new_size):
106106
if new_size < old_size:
107107
self.buf = self.buf[0:new_size]
108108
else:
109-
self.buf = self.buf + "\x00" * (new_size - old_size)
109+
self.buf = self.buf + b"\x00" * (new_size - old_size)
110110

111111
return 0
112112

example/hello.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
fuse.fuse_python_api = (0, 2)
2323

2424
hello_path = '/hello'
25-
hello_str = 'Hello World!\n'
25+
hello_str = b'Hello World!\n'
2626

2727
class MyStat(fuse.Stat):
2828
def __init__(self):
@@ -72,7 +72,7 @@ def read(self, path, size, offset):
7272
size = slen - offset
7373
buf = hello_str[offset:offset+size]
7474
else:
75-
buf = ''
75+
buf = b''
7676
return buf
7777

7878
def main():

example/xmp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030

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

3535
if flags | os.O_APPEND:

fuseparts/_fusemodule.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,22 @@ read_func(const char *path, char *buf, size_t s, off_t off)
517517
PROLOGUE( PYO_CALLWITHFI(fi, read_cb, snK, path, s, off) )
518518
#endif
519519

520+
521+
#if PY_MAJOR_VERSION >= 3
522+
if(PyBytes_Check(v)) {
523+
if(PyBytes_Size(v) > s)
524+
goto OUT_DECREF;
525+
memcpy(buf, PyBytes_AsString(v), PyBytes_Size(v));
526+
ret = PyBytes_Size(v);
527+
}
528+
#else
520529
if(PyString_Check(v)) {
521530
if(PyString_Size(v) > s)
522531
goto OUT_DECREF;
523532
memcpy(buf, PyString_AsString(v), PyString_Size(v));
524533
ret = PyString_Size(v);
525534
}
535+
#endif
526536

527537
EPILOGUE
528538
}
@@ -536,7 +546,11 @@ static int
536546
write_func(const char *path, const char *buf, size_t t, off_t off)
537547
#endif
538548
{
549+
#if PY_MAJOR_VERSION >= 3
550+
PROLOGUE( PYO_CALLWITHFI(fi, write_cb, sy#K, path, buf, t, off) )
551+
#else
539552
PROLOGUE( PYO_CALLWITHFI(fi, write_cb, ss#K, path, buf, t, off) )
553+
#endif
540554
EPILOGUE
541555
}
542556

0 commit comments

Comments
 (0)