Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When getting string size, count UTF8 bytes #79

Merged
merged 8 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions example/xattr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Example by github @vignedev from https://github.com/libfuse/python-fuse/issues/77

import fuse
import stat, errno
from fuse import Fuse, Stat, Direntry

fuse.fuse_python_api = (0, 2)

BROKEN_FILE = '/utf8_attr'
FATTR_NAME = 'user.xdg.comment'
FATTR_VALUE = 'ああ、メッセージは切り取られていない'

class EmptyStat(Stat):
def __init__(self):
self.st_mode = 0
self.st_ino = 0
self.st_dev = 0
self.st_nlink = 0
self.st_uid = 0
self.st_gid = 0
self.st_size = 0
self.st_atime = 0
self.st_mtime = 0
self.st_ctime = 0

class GetAttrBug(Fuse):
def getattr(self, path):
ret_stat = EmptyStat()
if path == '/':
ret_stat.st_mode = stat.S_IFDIR | int(0e755)
return ret_stat

if path == BROKEN_FILE:
ret_stat.st_mode = stat.S_IFREG | int(0e000)
return ret_stat

return -errno.ENOENT

def readdir(self, path, offset):
yield Direntry('.')
yield Direntry('..')
yield Direntry(BROKEN_FILE[1:])

def open(self, path, flags):
return -errno.EACCES

def read(self, path, size, offset):
return

def listxattr(self, path, size):
if size == 0: return 1
else: return [ FATTR_NAME ]

def getxattr(self, path, attr, size):
if size == 0: return len(FATTR_VALUE.encode('utf8'))
else: return FATTR_VALUE

if __name__ == '__main__':
server = GetAttrBug(dash_s_do='setsingle')
server.parse(errex=1)
server.main()
2 changes: 1 addition & 1 deletion example/xmp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python

# Copyright (C) 2001 Jeff Epler <jepler@unpythonic.dhs.org>
# Copyright (C) 2001 Jeff Epler <jepler@gmail.com>
# Copyright (C) 2006 Csaba Henk <[email protected]>
#
# This program can be distributed under the terms of the GNU LGPL.
Expand Down
2 changes: 1 addition & 1 deletion fuse.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (C) 2001 Jeff Epler <jepler@unpythonic.dhs.org>
# Copyright (C) 2001 Jeff Epler <jepler@gmail.com>
# Copyright (C) 2006 Csaba Henk <[email protected]>
#
# This program can be distributed under the terms of the GNU LGPL.
Expand Down
22 changes: 14 additions & 8 deletions fuseparts/_fusemodule.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2001 Jeff Epler <jepler@unpythonic.dhs.org>
Copyright (C) 2001 Jeff Epler <jepler@gmail.com>

This program can be distributed under the terms of the GNU LGPL.
See the file COPYING.
Expand Down Expand Up @@ -94,12 +94,16 @@
PyErr_SetString(PyExc_ValueError, "non-decodable filename");
return NULL;
}

static inline Py_ssize_t PyString_Size(PyObject *s) {
Py_ssize_t result = -1;
(void)PyUnicode_AsUTF8AndSize(s, &result);
return result;
}
#else
#define PyString_AsString PyUnicode_AsUTF8
#define PyString_Size PyUnicode_GET_LENGTH
#endif
#define PyString_Check PyUnicode_Check
#define PyString_Size PyUnicode_GET_LENGTH
#endif

#ifdef FIX_PATH_DECODING
Expand Down Expand Up @@ -683,14 +687,14 @@ read_func(const char *path, char *buf, size_t s, off_t off)
if(PyObject_CheckBuffer(v)) {
PyObject_GetBuffer(v, &buffer, PyBUF_SIMPLE);

if(buffer.len <= s) {
if((size_t)buffer.len <= s) {
memcpy(buf, buffer.buf, buffer.len);
ret = buffer.len;
}

PyBuffer_Release(&buffer);
} else if(PyBytes_Check(v)) {
if(PyBytes_Size(v) > s)
if((size_t)PyBytes_Size(v) > s)
goto OUT_DECREF;
memcpy(buf, PyBytes_AsString(v), PyBytes_Size(v));
ret = PyBytes_Size(v);
Expand Down Expand Up @@ -944,7 +948,7 @@ getxattr_func(const char *path, const char *name, char *value, size_t size)
/* If the size of the value buffer is too small to hold the result, errno
* is set to ERANGE.
*/
if (PyString_Size(v) > size) {
if ((size_t)PyString_Size(v) > size) {
ret = -ERANGE;
goto OUT_DECREF;
}
Expand Down Expand Up @@ -980,9 +984,9 @@ listxattr_func(const char *path, char *list, size_t size)
}

for (;;) {
int ilen;
size_t ilen;

w = PyIter_Next(iter);
w = PyIter_Next(iter);
if (!w) {
ret = lx - list;
break;
Expand Down Expand Up @@ -1302,7 +1306,9 @@ pyfuse_loop_mt(struct fuse *f)
#ifdef WITH_THREAD
PyThreadState *save;

#if PY_VERSION_HEX < 0x03070000
PyEval_InitThreads();
#endif
interp = PyThreadState_Get()->interp;
save = PyEval_SaveThread();
err = fuse_loop_mt(f);
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def setup(**kwargs):
package_data={'': ['COPYING', 'AUTHORS', 'FAQ', 'INSTALL',
'README.md', 'README.new_fusepy_api.rst',
'README.package_maintainers.rst']},
author = 'Jeff Epler <[email protected]>, Csaba Henk <[email protected]>, Steven James, Miklos Szeredi <[email protected]>, Sébastien Delafond<[email protected]>',
author = 'Csaba Henk <[email protected]>, Steven James, Miklos Szeredi <[email protected]>, Sébastien Delafond<[email protected]>',
maintainer = 'Sébastien Delafond',
maintainer_email = '[email protected]',
ext_modules = [fusemodule],
Expand Down
3 changes: 3 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ def test_fioc(filesystem):
fcntl.ioctl(f.fileno(), FIOC_GET_SIZE, b)
assert struct.unpack("L", b)[0] == 42

@pytest.mark.fstype("xattr")
def test_xattr(filesystem):
assert os.getxattr(filesystem / "utf8_attr", "user.xdg.comment").decode("utf-8") == 'ああ、メッセージは切り取られていない'
Loading